@@ -271,3 +271,95 @@ pub(crate) fn proto_origin_to_authorizer_origin(
271
271
272
272
Ok ( new_origin)
273
273
}
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
+ }
0 commit comments