Skip to content

Commit 88d8e8b

Browse files
committed
add roundtrip tests for snapshots
1 parent c8057ce commit 88d8e8b

File tree

3 files changed

+94
-2
lines changed

3 files changed

+94
-2
lines changed

biscuit-auth/src/datalog/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,7 @@ impl World {
733733
}
734734

735735
/// runtime limits for the Datalog engine
736-
#[derive(Debug, Clone)]
736+
#[derive(Debug, Clone, PartialEq, Eq)]
737737
pub struct RunLimits {
738738
/// maximum number of Datalog facts (memory usage)
739739
pub max_facts: u64,

biscuit-auth/src/token/authorizer/snapshot.rs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,3 +271,95 @@ pub(crate) fn proto_origin_to_authorizer_origin(
271271

272272
Ok(new_origin)
273273
}
274+
275+
#[cfg(test)]
276+
mod tests {
277+
use std::collections::HashMap;
278+
use std::time::Duration;
279+
280+
use crate::{datalog::RunLimits, Algorithm, AuthorizerBuilder};
281+
use crate::{Authorizer, BiscuitBuilder, KeyPair};
282+
283+
#[test]
284+
fn roundtrip_builder() {
285+
let secp_pubkey = KeyPair::new_with_algorithm(Algorithm::Secp256r1).public();
286+
let ed_pubkey = KeyPair::new_with_algorithm(Algorithm::Ed25519).public();
287+
let builder = AuthorizerBuilder::new()
288+
.limits(RunLimits {
289+
max_facts: 42,
290+
max_iterations: 42,
291+
max_time: Duration::from_secs(1),
292+
})
293+
.code_with_params(
294+
r#"
295+
fact(true);
296+
head($a) <- fact($a);
297+
check if head(true) trusting authority, {ed_pubkey}, {secp_pubkey};
298+
allow if head(true);
299+
deny if head(false);
300+
"#,
301+
HashMap::default(),
302+
HashMap::from([
303+
("ed_pubkey".to_string(), ed_pubkey),
304+
("secp_pubkey".to_string(), secp_pubkey),
305+
]),
306+
)
307+
.unwrap();
308+
let snapshot = builder.snapshot().unwrap();
309+
310+
let parsed = AuthorizerBuilder::from_snapshot(snapshot).unwrap();
311+
assert_eq!(parsed.dump_code(), builder.dump_code());
312+
assert_eq!(parsed.limits, builder.limits);
313+
}
314+
315+
#[test]
316+
fn roundtrip_with_token_pre_run() {
317+
let secp_pubkey = KeyPair::new_with_algorithm(Algorithm::Secp256r1).public();
318+
let ed_pubkey = KeyPair::new_with_algorithm(Algorithm::Ed25519).public();
319+
let builder = AuthorizerBuilder::new()
320+
.limits(RunLimits {
321+
max_facts: 42,
322+
max_iterations: 42,
323+
max_time: Duration::from_secs(1),
324+
})
325+
.code_with_params(
326+
r#"
327+
fact(true);
328+
head($a) <- fact($a);
329+
check if head(true) trusting authority, {ed_pubkey}, {secp_pubkey};
330+
allow if head(true);
331+
deny if head(false);
332+
"#,
333+
HashMap::default(),
334+
HashMap::from([
335+
("ed_pubkey".to_string(), ed_pubkey),
336+
("secp_pubkey".to_string(), secp_pubkey),
337+
]),
338+
)
339+
.unwrap();
340+
let biscuit = BiscuitBuilder::new()
341+
.code_with_params(
342+
r#"
343+
bfact(true);
344+
bhead($a) <- fact($a);
345+
check if bhead(true) trusting authority, {ed_pubkey}, {secp_pubkey};
346+
"#,
347+
HashMap::default(),
348+
HashMap::from([
349+
("ed_pubkey".to_string(), ed_pubkey),
350+
("secp_pubkey".to_string(), secp_pubkey),
351+
]),
352+
)
353+
.unwrap()
354+
.build(&KeyPair::new())
355+
.unwrap();
356+
357+
let authorizer = builder.build(&biscuit).unwrap();
358+
359+
let snapshot = authorizer.snapshot().unwrap();
360+
361+
let parsed = Authorizer::from_snapshot(snapshot).unwrap();
362+
assert_eq!(parsed.dump_code(), authorizer.dump_code());
363+
assert_eq!(parsed.limits(), authorizer.limits());
364+
}
365+
}

biscuit-auth/src/token/builder/authorizer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub struct AuthorizerBuilder {
3131
authorizer_block_builder: BlockBuilder,
3232
policies: Vec<Policy>,
3333
extern_funcs: HashMap<String, ExternFunc>,
34-
limits: AuthorizerLimits,
34+
pub(crate) limits: AuthorizerLimits,
3535
}
3636

3737
impl AuthorizerBuilder {

0 commit comments

Comments
 (0)