From 763e3af091c44f150dcb81029d60b22c8e4cb4c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Corbi=C3=A8re?= Date: Sat, 21 Nov 2026 23:49:37 +0100 Subject: [PATCH 01/11] Add IntelliJ file ignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index a17d056e..b1589eb8 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ # IntelliJ IDEA .idea/ +*.iml # Mobile Tools for Java (J2ME) .mtj.tmp/ From e0228eae3cdd00f68786d7fad367ab291aaa6c23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Corbi=C3=A8re?= Date: Mon, 23 Nov 2026 07:50:45 +0100 Subject: [PATCH 02/11] Update with latest version of schema.proto --- biscuit-core/src/main/proto/schema.proto | 57 +- biscuit/src/test/resources/samples/README.md | 150 +++- .../src/test/resources/samples/samples.json | 690 +++++++++++++----- .../resources/samples/test034_array_map.bc | Bin 1953 -> 2010 bytes .../src/test/resources/samples/test035_ffi.bc | Bin 0 -> 236 bytes .../test039_forward_symbol_reference.bc | Bin 0 -> 431 bytes 6 files changed, 665 insertions(+), 232 deletions(-) create mode 100644 biscuit/src/test/resources/samples/test035_ffi.bc create mode 100644 biscuit/src/test/resources/samples/test039_forward_symbol_reference.bc diff --git a/biscuit-core/src/main/proto/schema.proto b/biscuit-core/src/main/proto/schema.proto index 8e08468b..048db536 100644 --- a/biscuit-core/src/main/proto/schema.proto +++ b/biscuit-core/src/main/proto/schema.proto @@ -44,9 +44,9 @@ message Block { repeated string symbols = 1; optional string context = 2; optional uint32 version = 3; - repeated FactV2 facts_v2 = 4; - repeated RuleV2 rules_v2 = 5; - repeated CheckV2 checks_v2 = 6; + repeated Fact facts = 4; + repeated Rule rules = 5; + repeated Check checks = 6; repeated Scope scope = 7; repeated PublicKey publicKeys = 8; } @@ -63,19 +63,19 @@ message Scope { } } -message FactV2 { - required PredicateV2 predicate = 1; +message Fact { + required Predicate predicate = 1; } -message RuleV2 { - required PredicateV2 head = 1; - repeated PredicateV2 body = 2; - repeated ExpressionV2 expressions = 3; +message Rule { + required Predicate head = 1; + repeated Predicate body = 2; + repeated Expression expressions = 3; repeated Scope scope = 4; } -message CheckV2 { - repeated RuleV2 queries = 1; +message Check { + repeated Rule queries = 1; optional Kind kind = 2; enum Kind { @@ -85,12 +85,12 @@ message CheckV2 { } } -message PredicateV2 { +message Predicate { required uint64 name = 1; - repeated TermV2 terms = 2; + repeated Term terms = 2; } -message TermV2 { +message Term { oneof Content { uint32 variable = 1; int64 integer = 2; @@ -106,11 +106,11 @@ message TermV2 { } message TermSet { - repeated TermV2 set = 1; + repeated Term set = 1; } message Array { - repeated TermV2 array = 1; + repeated Term array = 1; } message Map { @@ -119,7 +119,7 @@ message Map { message MapEntry { required MapKey key = 1; - required TermV2 value = 2; + required Term value = 2; } message MapKey { @@ -129,13 +129,13 @@ message MapKey { } } -message ExpressionV2 { +message Expression { repeated Op ops = 1; } message Op { oneof Content { - TermV2 value = 1; + Term value = 1; OpUnary unary = 2; OpBinary Binary = 3; OpClosure closure = 4; @@ -186,7 +186,7 @@ message OpBinary { Any = 26; Get = 27; Ffi = 28; - Try = 29; + TryOr = 29; } required Kind kind = 1; @@ -204,16 +204,16 @@ message Policy { Deny = 1; } - repeated RuleV2 queries = 1; + repeated Rule queries = 1; required Kind kind = 2; } message AuthorizerPolicies { repeated string symbols = 1; optional uint32 version = 2; - repeated FactV2 facts = 3; - repeated RuleV2 rules = 4; - repeated CheckV2 checks = 5; + repeated Fact facts = 3; + repeated Rule rules = 4; + repeated Check checks = 5; repeated Policy policies = 6; } @@ -233,6 +233,7 @@ message AuthorizerSnapshot { required RunLimits limits = 1; required uint64 executionTime = 2; required AuthorizerWorld world = 3; + optional uint64 elapsedSteps = 4; } message RunLimits { @@ -264,15 +265,15 @@ message Empty {} message GeneratedFacts { repeated Origin origins = 1; - repeated FactV2 facts = 2; + repeated Fact facts = 2; } message SnapshotBlock { optional string context = 1; optional uint32 version = 2; - repeated FactV2 facts_v2 = 3; - repeated RuleV2 rules_v2 = 4; - repeated CheckV2 checks_v2 = 5; + repeated Fact facts = 3; + repeated Rule rules = 4; + repeated Check checks = 5; repeated Scope scope = 6; optional PublicKey externalKey = 7; } diff --git a/biscuit/src/test/resources/samples/README.md b/biscuit/src/test/resources/samples/README.md index efd33f9c..d5b975da 100644 --- a/biscuit/src/test/resources/samples/README.md +++ b/biscuit/src/test/resources/samples/README.md @@ -84,6 +84,7 @@ World { policies: [ "allow if true", ] + elapsed steps: 1 } ``` @@ -119,7 +120,7 @@ check if resource($0), operation("read"), right($0, "read"); ### validation -result: `Err(Format(Signature(InvalidSignature("signature error: Verification equation was not satisfied"))))` +result: `Err(Format(Signature(InvalidSignature("signature error"))))` ------------------------------ @@ -187,7 +188,7 @@ check if resource($0), operation("read"), right($0, "read"); ### validation -result: `Err(Format(Signature(InvalidSignature("signature error: Verification equation was not satisfied"))))` +result: `Err(Format(Signature(InvalidSignature("signature error"))))` ------------------------------ @@ -221,7 +222,7 @@ check if resource($0), operation("read"), right($0, "read"); ### validation -result: `Err(Format(Signature(InvalidSignature("signature error: Verification equation was not satisfied"))))` +result: `Err(Format(Signature(InvalidSignature("signature error"))))` ------------------------------ @@ -266,7 +267,7 @@ check if resource("file1"); ### validation -result: `Err(Format(Signature(InvalidSignature("signature error: Verification equation was not satisfied"))))` +result: `Err(Format(Signature(InvalidSignature("signature error"))))` ------------------------------ @@ -382,6 +383,7 @@ World { policies: [ "allow if true", ] + elapsed steps: 5 } ``` @@ -489,6 +491,7 @@ World { policies: [ "allow if true", ] + elapsed steps: 3 } ``` @@ -567,6 +570,7 @@ World { policies: [ "allow if true", ] + elapsed steps: 3 } ``` @@ -664,6 +668,7 @@ World { policies: [ "allow if true", ] + elapsed steps: 2 } ``` @@ -739,6 +744,7 @@ World { policies: [ "allow if true", ] + elapsed steps: 2 } ``` @@ -802,6 +808,7 @@ World { policies: [ "allow if true", ] + elapsed steps: 1 } ``` @@ -847,6 +854,7 @@ World { policies: [ "allow if true", ] + elapsed steps: 1 } ``` @@ -957,6 +965,7 @@ World { policies: [ "allow if true", ] + elapsed steps: 14 } ``` @@ -1024,6 +1033,7 @@ World { policies: [ "allow if true", ] + elapsed steps: 5 } ``` @@ -1085,6 +1095,7 @@ World { policies: [ "allow if true", ] + elapsed steps: 2 } ``` @@ -1128,6 +1139,7 @@ World { policies: [ "allow if true", ] + elapsed steps: 2 } ``` @@ -1191,6 +1203,7 @@ World { policies: [ "allow if true", ] + elapsed steps: 1 } ``` @@ -1264,6 +1277,7 @@ World { policies: [ "allow if true", ] + elapsed steps: 0 } ``` @@ -1390,6 +1404,7 @@ World { policies: [ "allow if true", ] + elapsed steps: 53 } ``` @@ -1515,6 +1530,7 @@ World { policies: [ "allow if true", ] + elapsed steps: 4 } ``` @@ -1604,6 +1620,7 @@ World { policies: [ "allow if true", ] + elapsed steps: 3 } ``` @@ -1667,6 +1684,7 @@ World { policies: [ "allow if true", ] + elapsed steps: 1 } ``` @@ -1784,6 +1802,7 @@ World { policies: [ "allow if true", ] + elapsed steps: 28 } ``` @@ -1881,6 +1900,7 @@ World { policies: [ "allow if true", ] + elapsed steps: 1 } ``` @@ -1976,6 +1996,7 @@ World { policies: [ "allow if true", ] + elapsed steps: 2 } ``` @@ -2050,6 +2071,7 @@ World { policies: [ "allow if true", ] + elapsed steps: 6 } ``` @@ -2105,6 +2127,7 @@ World { policies: [ "allow if true", ] + elapsed steps: 6 } ``` @@ -2148,6 +2171,7 @@ World { policies: [ "allow if true", ] + elapsed steps: 0 } ``` @@ -2387,6 +2411,7 @@ World { "deny if query(0) trusting ed25519/acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189", "allow if true", ] + elapsed steps: 31 } ``` @@ -2441,6 +2466,7 @@ World { policies: [ "allow if true", ] + elapsed steps: 1 } ``` @@ -2503,6 +2529,7 @@ World { policies: [ "allow if true", ] + elapsed steps: 9 } ``` @@ -2564,6 +2591,7 @@ World { policies: [ "allow if true", ] + elapsed steps: 1 } ``` @@ -2607,6 +2635,7 @@ World { policies: [ "allow if true", ] + elapsed steps: 1 } ``` @@ -2670,6 +2699,7 @@ World { policies: [ "allow if true", ] + elapsed steps: 4 } ``` @@ -2714,6 +2744,7 @@ World { policies: [ "allow if true", ] + elapsed steps: 4 } ``` @@ -2758,6 +2789,7 @@ World { policies: [ "allow if true", ] + elapsed steps: 4 } ``` @@ -2802,6 +2834,7 @@ World { policies: [ "allow if true", ] + elapsed steps: 4 } ``` @@ -2905,6 +2938,7 @@ World { policies: [ "allow if true", ] + elapsed steps: 23 } ``` @@ -2980,6 +3014,7 @@ World { policies: [ "allow if true", ] + elapsed steps: 24 } ``` @@ -3048,6 +3083,7 @@ World { policies: [ "allow if true", ] + elapsed steps: 61 } ``` @@ -3089,6 +3125,7 @@ World { policies: [ "allow if {\"true\"}.any($p -> {\"true\"}.all($p -> $p))", ] + elapsed steps: 64 } ``` @@ -3197,6 +3234,7 @@ World { policies: [ "allow if true", ] + elapsed steps: 42 } ``` @@ -3227,6 +3265,7 @@ check if [1, 2, 3].starts_with([1, 2]); check if [4, 5, 6].ends_with([6]); check if [1, 2, "a"].get(2) == "a"; check if [1, 2].get(3) == null; +check if [1, 2].get(-1) == null; check if [1, 2, 3].all($p -> $p > 0); check if [1, 2, 3].any($p -> $p > 2); check if {"a": 1, "b": 2, "c": 3, "d": 4}.length() == 4; @@ -3253,7 +3292,7 @@ allow if true; ``` revocation ids: -- `b22238a06ca9c015d3c49d4ebaa7e8ab6e0d69119b3264033618e726d62fc6f4757a7bebc25f255444aba39994554a62a53ecc13b68802efab8da85ace62390d` +- `0227fc29d6bf85cc52503f497b0b232235ffe5192c68933388136eae3fc3419229e9489b85d1bb57525554edac7cda87c984c96639bfb4171acc499312ffe30e` authorizer world: ``` @@ -3277,6 +3316,7 @@ World { "check if [1, 2, 3].all($p -> $p > 0)", "check if [1, 2, 3].any($p -> $p > 2)", "check if [1, 2, 3].starts_with([1, 2])", + "check if [1, 2].get(-1) == null", "check if [1, 2].get(3) == null", "check if [4, 5, 6].ends_with([6])", "check if {\"a\": 1, \"b\": 2, \"c\": 3, \"d\": 4}.contains(\"d\")", @@ -3299,11 +3339,64 @@ World { policies: [ "allow if true", ] + elapsed steps: 72 } ``` result: `Ok(0)` + +------------------------------ + +## test ffi calls (v6 blocks): test035_ffi.bc +### token + +authority: +symbols: ["test", "a", "equal strings"] + +public keys: [] + +block version: 6 + +``` +check if true.extern::test(), "a".extern::test("a") == "equal strings"; +``` + +### validation + +authorizer code: +``` +allow if true; +``` + +revocation ids: +- `d1719fd101c2695d2dac4df67569918363f691b6167670e1dbbf8026f639a7aa1ec2e13707f4d34cadbb2adce5c6e8a816577dd069a8717e0f5cb4ea3cec5b04` + +authorizer world: +``` +World { + facts: [] + rules: [] + checks: [ + Checks { + origin: Some( + 0, + ), + checks: [ + "check if true.extern::test(), \"a\".extern::test(\"a\") == \"equal strings\"", + ], + }, +] + policies: [ + "allow if true", +] + elapsed steps: 3 +} +``` + +result: `Ok(0)` + + ------------------------------ ## ECDSA secp256r1 signatures: test036_secp256r1.bc @@ -3387,6 +3480,7 @@ World { policies: [ "allow if true", ] + elapsed steps: 3 } ``` @@ -3498,6 +3592,7 @@ World { policies: [ "allow if true", ] + elapsed steps: 4 } ``` @@ -3552,6 +3647,7 @@ World { policies: [ "allow if true", ] + elapsed steps: 10 } ``` @@ -3596,8 +3692,52 @@ World { policies: [ "allow if true", ] + elapsed steps: 1 } ``` result: `Err(Execution(InvalidType))` + +------------------------------ + +## block referencing a symbol defined in a later block: test039_forward_symbol_reference.bc +### token + +authority: +symbols: ["file1"] + +public keys: [] + +block version: 3 + +``` +right("file1", "read"); +``` + +1: +symbols: [] + +public keys: [] + +block version: 3 + +``` +check if resource("file2"); +``` + +2: +symbols: ["file2"] + +public keys: [] + +block version: 3 + +``` +check if resource("file2"); +``` + +### validation + +result: `Err(Format(UnknownSymbol(1025)))` + diff --git a/biscuit/src/test/resources/samples/samples.json b/biscuit/src/test/resources/samples/samples.json index ebecb1d6..e0011114 100644 --- a/biscuit/src/test/resources/samples/samples.json +++ b/biscuit/src/test/resources/samples/samples.json @@ -60,18 +60,24 @@ ], "policies": [ "allow if true" - ] + ], + "elapsed_steps": 1 }, "result": { - "Err": { - "FailedLogic": { - "Unauthorized": { + "type": "err", + "value": { + "type": "failed_logic", + "value": { + "type": "unauthorized", + "value": { "policy": { - "Allow": 0 + "type": "allow", + "value": 0 }, "checks": [ { - "Block": { + "type": "block", + "value": { "block_id": 1, "check_id": 0, "rule": "check if resource($0), operation(\"read\"), right($0, \"read\")" @@ -117,10 +123,14 @@ "": { "world": null, "result": { - "Err": { - "Format": { - "Signature": { - "InvalidSignature": "signature error: Verification equation was not satisfied" + "type": "err", + "value": { + "type": "format", + "value": { + "type": "signature", + "value": { + "type": "invalid_signature", + "value": "signature error" } } } @@ -158,9 +168,12 @@ "": { "world": null, "result": { - "Err": { - "Format": { - "BlockSignatureDeserializationError": "block signature deserialization error: [117, 149, 161, 18, 161, 235, 91, 129, 166, 227, 152, 133, 46, 97, 24, 183]" + "type": "err", + "value": { + "type": "format", + "value": { + "type": "block_signature_deserialization_error", + "value": "block signature deserialization error: [117, 149, 161, 18, 161, 235, 91, 129, 166, 227, 152, 133, 46, 97, 24, 183]" } } }, @@ -197,10 +210,14 @@ "": { "world": null, "result": { - "Err": { - "Format": { - "Signature": { - "InvalidSignature": "signature error: Verification equation was not satisfied" + "type": "err", + "value": { + "type": "format", + "value": { + "type": "signature", + "value": { + "type": "invalid_signature", + "value": "signature error" } } } @@ -238,10 +255,14 @@ "": { "world": null, "result": { - "Err": { - "Format": { - "Signature": { - "InvalidSignature": "signature error: Verification equation was not satisfied" + "type": "err", + "value": { + "type": "format", + "value": { + "type": "signature", + "value": { + "type": "invalid_signature", + "value": "signature error" } } } @@ -286,10 +307,14 @@ "": { "world": null, "result": { - "Err": { - "Format": { - "Signature": { - "InvalidSignature": "signature error: Verification equation was not satisfied" + "type": "err", + "value": { + "type": "format", + "value": { + "type": "signature", + "value": { + "type": "invalid_signature", + "value": "signature error" } } } @@ -383,18 +408,24 @@ ], "policies": [ "allow if true" - ] + ], + "elapsed_steps": 5 }, "result": { - "Err": { - "FailedLogic": { - "Unauthorized": { + "type": "err", + "value": { + "type": "failed_logic", + "value": { + "type": "unauthorized", + "value": { "policy": { - "Allow": 0 + "type": "allow", + "value": 0 }, "checks": [ { - "Block": { + "type": "block", + "value": { "block_id": 1, "check_id": 0, "rule": "check if resource($0), operation(\"read\"), right($0, \"read\")" @@ -487,18 +518,24 @@ ], "policies": [ "allow if true" - ] + ], + "elapsed_steps": 3 }, "result": { - "Err": { - "FailedLogic": { - "Unauthorized": { + "type": "err", + "value": { + "type": "failed_logic", + "value": { + "type": "unauthorized", + "value": { "policy": { - "Allow": 0 + "type": "allow", + "value": 0 }, "checks": [ { - "Block": { + "type": "block", + "value": { "block_id": 1, "check_id": 0, "rule": "check if resource($0), operation(\"read\"), right($0, \"read\")" @@ -566,18 +603,24 @@ ], "policies": [ "allow if true" - ] + ], + "elapsed_steps": 3 }, "result": { - "Err": { - "FailedLogic": { - "Unauthorized": { + "type": "err", + "value": { + "type": "failed_logic", + "value": { + "type": "unauthorized", + "value": { "policy": { - "Allow": 0 + "type": "allow", + "value": 0 }, "checks": [ { - "Block": { + "type": "block", + "value": { "block_id": 1, "check_id": 1, "rule": "check if time($time), $time <= 2018-12-20T00:00:00Z" @@ -660,18 +703,24 @@ ], "policies": [ "allow if true" - ] + ], + "elapsed_steps": 2 }, "result": { - "Err": { - "FailedLogic": { - "Unauthorized": { + "type": "err", + "value": { + "type": "failed_logic", + "value": { + "type": "unauthorized", + "value": { "policy": { - "Allow": 0 + "type": "allow", + "value": 0 }, "checks": [ { - "Authorizer": { + "type": "authorizer", + "value": { "check_id": 0, "rule": "check if right($0, $1), resource($0), operation($1)" } @@ -736,18 +785,24 @@ ], "policies": [ "allow if true" - ] + ], + "elapsed_steps": 2 }, "result": { - "Err": { - "FailedLogic": { - "Unauthorized": { + "type": "err", + "value": { + "type": "failed_logic", + "value": { + "type": "unauthorized", + "value": { "policy": { - "Allow": 0 + "type": "allow", + "value": 0 }, "checks": [ { - "Authorizer": { + "type": "authorizer", + "value": { "check_id": 0, "rule": "check if right($0, $1), resource($0), operation($1)" } @@ -803,10 +858,12 @@ ], "policies": [ "allow if true" - ] + ], + "elapsed_steps": 1 }, "result": { - "Ok": 0 + "type": "ok", + "value": 0 }, "authorizer_code": "resource(\"file1\");\noperation(\"read\");\n\nallow if true;\n", "revocation_ids": [ @@ -837,18 +894,24 @@ ], "policies": [ "allow if true" - ] + ], + "elapsed_steps": 1 }, "result": { - "Err": { - "FailedLogic": { - "Unauthorized": { + "type": "err", + "value": { + "type": "failed_logic", + "value": { + "type": "unauthorized", + "value": { "policy": { - "Allow": 0 + "type": "allow", + "value": 0 }, "checks": [ { - "Block": { + "type": "block", + "value": { "block_id": 0, "check_id": 0, "rule": "check if resource(\"file1\")" @@ -943,10 +1006,12 @@ ], "policies": [ "allow if true" - ] + ], + "elapsed_steps": 14 }, "result": { - "Ok": 0 + "type": "ok", + "value": 0 }, "authorizer_code": "resource(\"file1\");\ntime(2020-12-21T09:23:12Z);\n\nallow if true;\n", "revocation_ids": [ @@ -995,18 +1060,24 @@ ], "policies": [ "allow if true" - ] + ], + "elapsed_steps": 5 }, "result": { - "Err": { - "FailedLogic": { - "Unauthorized": { + "type": "err", + "value": { + "type": "failed_logic", + "value": { + "type": "unauthorized", + "value": { "policy": { - "Allow": 0 + "type": "allow", + "value": 0 }, "checks": [ { - "Block": { + "type": "block", + "value": { "block_id": 1, "check_id": 0, "rule": "check if valid_date($0), resource($0)" @@ -1064,18 +1135,24 @@ ], "policies": [ "allow if true" - ] + ], + "elapsed_steps": 2 }, "result": { - "Err": { - "FailedLogic": { - "Unauthorized": { + "type": "err", + "value": { + "type": "failed_logic", + "value": { + "type": "unauthorized", + "value": { "policy": { - "Allow": 0 + "type": "allow", + "value": 0 }, "checks": [ { - "Block": { + "type": "block", + "value": { "block_id": 0, "check_id": 0, "rule": "check if resource($0), $0.matches(\"file[0-9]+.txt\")" @@ -1114,10 +1191,12 @@ ], "policies": [ "allow if true" - ] + ], + "elapsed_steps": 2 }, "result": { - "Ok": 0 + "type": "ok", + "value": 0 }, "authorizer_code": "resource(\"file123.txt\");\n\nallow if true;\n", "revocation_ids": [ @@ -1165,10 +1244,12 @@ ], "policies": [ "allow if true" - ] + ], + "elapsed_steps": 1 }, "result": { - "Ok": 0 + "type": "ok", + "value": 0 }, "authorizer_code": "check if must_be_present($0) or must_be_present($0);\n\nallow if true;\n", "revocation_ids": [ @@ -1224,18 +1305,24 @@ ], "policies": [ "allow if true" - ] + ], + "elapsed_steps": 0 }, "result": { - "Err": { - "FailedLogic": { - "Unauthorized": { + "type": "err", + "value": { + "type": "failed_logic", + "value": { + "type": "unauthorized", + "value": { "policy": { - "Allow": 0 + "type": "allow", + "value": 0 }, "checks": [ { - "Block": { + "type": "block", + "value": { "block_id": 0, "check_id": 0, "rule": "check if resource(\"hello\")" @@ -1333,10 +1420,12 @@ ], "policies": [ "allow if true" - ] + ], + "elapsed_steps": 53 }, "result": { - "Ok": 0 + "type": "ok", + "value": 0 }, "authorizer_code": "allow if true;\n", "revocation_ids": [ @@ -1372,9 +1461,12 @@ "": { "world": null, "result": { - "Err": { - "FailedLogic": { - "InvalidBlockRule": [ + "type": "err", + "value": { + "type": "failed_logic", + "value": { + "type": "invalid_block_rule", + "value": [ 0, "operation($unbound, \"read\") <- operation($any1, $any2)" ] @@ -1450,18 +1542,24 @@ ], "policies": [ "allow if true" - ] + ], + "elapsed_steps": 4 }, "result": { - "Err": { - "FailedLogic": { - "Unauthorized": { + "type": "err", + "value": { + "type": "failed_logic", + "value": { + "type": "unauthorized", + "value": { "policy": { - "Allow": 0 + "type": "allow", + "value": 0 }, "checks": [ { - "Block": { + "type": "block", + "value": { "block_id": 0, "check_id": 0, "rule": "check if operation(\"read\")" @@ -1539,10 +1637,12 @@ ], "policies": [ "allow if true" - ] + ], + "elapsed_steps": 3 }, "result": { - "Ok": 0 + "type": "ok", + "value": 0 }, "authorizer_code": "resource(\"file1\");\noperation(\"read\");\n\nallow if true;\n", "revocation_ids": [ @@ -1591,10 +1691,12 @@ ], "policies": [ "allow if true" - ] + ], + "elapsed_steps": 1 }, "result": { - "Ok": 0 + "type": "ok", + "value": 0 }, "authorizer_code": "check if ns::fact_123(\"hello é\t😁\");\n\nallow if true;\n", "revocation_ids": [ @@ -1666,10 +1768,12 @@ ], "policies": [ "allow if true" - ] + ], + "elapsed_steps": 28 }, "result": { - "Ok": 0 + "type": "ok", + "value": 0 }, "authorizer_code": "check if read(0), write(1), resource(2), operation(3), right(4), time(5), role(6), owner(7), tenant(8), namespace(9), user(10), team(11), service(12), admin(13), email(14), group(15), member(16), ip_address(17), client(18), client_ip(19), domain(20), path(21), version(22), cluster(23), node(24), hostname(25), nonce(26), query(27);\n\nallow if true;\n", "revocation_ids": [ @@ -1743,18 +1847,24 @@ ], "policies": [ "allow if true" - ] + ], + "elapsed_steps": 1 }, "result": { - "Err": { - "FailedLogic": { - "Unauthorized": { + "type": "err", + "value": { + "type": "failed_logic", + "value": { + "type": "unauthorized", + "value": { "policy": { - "Allow": 0 + "type": "allow", + "value": 0 }, "checks": [ { - "Block": { + "type": "block", + "value": { "block_id": 2, "check_id": 1, "rule": "check if block1_fact($var)" @@ -1833,10 +1943,12 @@ ], "policies": [ "allow if true" - ] + ], + "elapsed_steps": 2 }, "result": { - "Ok": 0 + "type": "ok", + "value": 0 }, "authorizer_code": "allow if true;\n", "revocation_ids": [ @@ -1897,10 +2009,12 @@ ], "policies": [ "allow if true" - ] + ], + "elapsed_steps": 6 }, "result": { - "Ok": 0 + "type": "ok", + "value": 0 }, "authorizer_code": "operation(\"A\");\noperation(\"B\");\n\nallow if true;\n", "revocation_ids": [ @@ -1939,18 +2053,24 @@ ], "policies": [ "allow if true" - ] + ], + "elapsed_steps": 6 }, "result": { - "Err": { - "FailedLogic": { - "Unauthorized": { + "type": "err", + "value": { + "type": "failed_logic", + "value": { + "type": "unauthorized", + "value": { "policy": { - "Allow": 0 + "type": "allow", + "value": 0 }, "checks": [ { - "Block": { + "type": "block", + "value": { "block_id": 0, "check_id": 0, "rule": "check all operation($op), allowed_operations($allowed), $allowed.contains($op)" @@ -1989,18 +2109,24 @@ ], "policies": [ "allow if true" - ] + ], + "elapsed_steps": 0 }, "result": { - "Err": { - "FailedLogic": { - "Unauthorized": { + "type": "err", + "value": { + "type": "failed_logic", + "value": { + "type": "unauthorized", + "value": { "policy": { - "Allow": 0 + "type": "allow", + "value": 0 }, "checks": [ { - "Block": { + "type": "block", + "value": { "block_id": 0, "check_id": 0, "rule": "check all operation($op), allowed_operations($allowed), $allowed.contains($op)" @@ -2181,10 +2307,12 @@ "deny if query(1, 2)", "deny if query(0) trusting ed25519/acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189", "allow if true" - ] + ], + "elapsed_steps": 31 }, "result": { - "Ok": 3 + "type": "ok", + "value": 3 }, "authorizer_code": "check if query(1, 2) trusting ed25519/acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189, ed25519/a060270db7e9c9f06e8f9cc33a64e99f6596af12cb01c4b638df8afc7b642463;\n\ndeny if query(3);\ndeny if query(1, 2);\ndeny if query(0) trusting ed25519/acdd6d5b53bfee478bf689f8e012fe7988bf755e3d7c5152947abc149bc20189;\nallow if true;\n", "revocation_ids": [ @@ -2226,11 +2354,16 @@ ], "policies": [ "allow if true" - ] + ], + "elapsed_steps": 1 }, "result": { - "Err": { - "Execution": "Overflow" + "type": "err", + "value": { + "type": "execution", + "value": { + "type": "overflow" + } } }, "authorizer_code": "allow if true;\n", @@ -2276,10 +2409,12 @@ ], "policies": [ "allow if true" - ] + ], + "elapsed_steps": 9 }, "result": { - "Ok": 0 + "type": "ok", + "value": 0 }, "authorizer_code": "allow if true;\n", "revocation_ids": [ @@ -2326,10 +2461,12 @@ ], "policies": [ "allow if true" - ] + ], + "elapsed_steps": 1 }, "result": { - "Ok": 0 + "type": "ok", + "value": 0 }, "authorizer_code": "test(false);\n\nallow if true;\n", "revocation_ids": [ @@ -2359,18 +2496,24 @@ ], "policies": [ "allow if true" - ] + ], + "elapsed_steps": 1 }, "result": { - "Err": { - "FailedLogic": { - "Unauthorized": { + "type": "err", + "value": { + "type": "failed_logic", + "value": { + "type": "unauthorized", + "value": { "policy": { - "Allow": 0 + "type": "allow", + "value": 0 }, "checks": [ { - "Block": { + "type": "block", + "value": { "block_id": 0, "check_id": 0, "rule": "reject if test($test), $test" @@ -2428,10 +2571,12 @@ ], "policies": [ "allow if true" - ] + ], + "elapsed_steps": 4 }, "result": { - "Ok": 0 + "type": "ok", + "value": 0 }, "authorizer_code": "fact(null, null);\n\nallow if true;\n", "revocation_ids": [ @@ -2462,25 +2607,32 @@ ], "policies": [ "allow if true" - ] + ], + "elapsed_steps": 4 }, "result": { - "Err": { - "FailedLogic": { - "Unauthorized": { + "type": "err", + "value": { + "type": "failed_logic", + "value": { + "type": "unauthorized", + "value": { "policy": { - "Allow": 0 + "type": "allow", + "value": 0 }, "checks": [ { - "Block": { + "type": "block", + "value": { "block_id": 0, "check_id": 0, "rule": "check if fact(null, $value), $value == null" } }, { - "Block": { + "type": "block", + "value": { "block_id": 0, "check_id": 1, "rule": "reject if fact(null, $value), $value != null" @@ -2520,25 +2672,32 @@ ], "policies": [ "allow if true" - ] + ], + "elapsed_steps": 4 }, "result": { - "Err": { - "FailedLogic": { - "Unauthorized": { + "type": "err", + "value": { + "type": "failed_logic", + "value": { + "type": "unauthorized", + "value": { "policy": { - "Allow": 0 + "type": "allow", + "value": 0 }, "checks": [ { - "Block": { + "type": "block", + "value": { "block_id": 0, "check_id": 0, "rule": "check if fact(null, $value), $value == null" } }, { - "Block": { + "type": "block", + "value": { "block_id": 0, "check_id": 1, "rule": "reject if fact(null, $value), $value != null" @@ -2578,25 +2737,32 @@ ], "policies": [ "allow if true" - ] + ], + "elapsed_steps": 4 }, "result": { - "Err": { - "FailedLogic": { - "Unauthorized": { + "type": "err", + "value": { + "type": "failed_logic", + "value": { + "type": "unauthorized", + "value": { "policy": { - "Allow": 0 + "type": "allow", + "value": 0 }, "checks": [ { - "Block": { + "type": "block", + "value": { "block_id": 0, "check_id": 0, "rule": "check if fact(null, $value), $value == null" } }, { - "Block": { + "type": "block", + "value": { "block_id": 0, "check_id": 1, "rule": "reject if fact(null, $value), $value != null" @@ -2677,10 +2843,12 @@ ], "policies": [ "allow if true" - ] + ], + "elapsed_steps": 23 }, "result": { - "Ok": 0 + "type": "ok", + "value": 0 }, "authorizer_code": "fact(1, 1);\nfact2(1, 2);\n\nallow if true;\n", "revocation_ids": [ @@ -2737,31 +2905,39 @@ ], "policies": [ "allow if true" - ] + ], + "elapsed_steps": 24 }, "result": { - "Err": { - "FailedLogic": { - "Unauthorized": { + "type": "err", + "value": { + "type": "failed_logic", + "value": { + "type": "unauthorized", + "value": { "policy": { - "Allow": 0 + "type": "allow", + "value": 0 }, "checks": [ { - "Authorizer": { + "type": "authorizer", + "value": { "check_id": 0, "rule": "check if false != false" } }, { - "Block": { + "type": "block", + "value": { "block_id": 0, "check_id": 19, "rule": "check if fact(1, $value), 1 == $value" } }, { - "Block": { + "type": "block", + "value": { "block_id": 0, "check_id": 20, "rule": "check if fact2(1, $value), 1 != $value" @@ -2819,10 +2995,12 @@ ], "policies": [ "allow if true" - ] + ], + "elapsed_steps": 61 }, "result": { - "Ok": 0 + "type": "ok", + "value": 0 }, "authorizer_code": "allow if true;\n", "revocation_ids": [ @@ -2852,11 +3030,16 @@ ], "policies": [ "allow if {\"true\"}.any($p -> {\"true\"}.all($p -> $p))" - ] + ], + "elapsed_steps": 64 }, "result": { - "Err": { - "Execution": "ShadowedVariable" + "type": "err", + "value": { + "type": "execution", + "value": { + "type": "shadowed_variable" + } } }, "authorizer_code": "allow if {\"true\"}.any($p -> {\"true\"}.all($p -> $p));\n", @@ -2938,10 +3121,12 @@ ], "policies": [ "allow if true" - ] + ], + "elapsed_steps": 42 }, "result": { - "Ok": 0 + "type": "ok", + "value": 0 }, "authorizer_code": "allow if true;\n", "revocation_ids": [ @@ -2968,7 +3153,7 @@ ], "public_keys": [], "external_key": null, - "code": "check if [1, 2, 1].length() == 3;\ncheck if [\"a\", \"b\"] != true;\ncheck if [\"a\", \"b\"] != [1, 2, 3];\ncheck if [\"a\", \"b\"] == [\"a\", \"b\"];\ncheck if [\"a\", \"b\"] === [\"a\", \"b\"];\ncheck if [\"a\", \"b\"] !== [\"a\", \"c\"];\ncheck if [\"a\", \"b\", \"c\"].contains(\"c\");\ncheck if [1, 2, 3].starts_with([1, 2]);\ncheck if [4, 5, 6].ends_with([6]);\ncheck if [1, 2, \"a\"].get(2) == \"a\";\ncheck if [1, 2].get(3) == null;\ncheck if [1, 2, 3].all($p -> $p > 0);\ncheck if [1, 2, 3].any($p -> $p > 2);\ncheck if {\"a\": 1, \"b\": 2, \"c\": 3, \"d\": 4}.length() == 4;\ncheck if {1: \"a\", 2: \"b\"} != true;\ncheck if {1: \"a\", 2: \"b\"} != {\"a\": 1, \"b\": 2};\ncheck if {1: \"a\", 2: \"b\"} == {1: \"a\", 2: \"b\"};\ncheck if {1: \"a\", 2: \"b\"} !== {\"a\": 1, \"b\": 2};\ncheck if {1: \"a\", 2: \"b\"} === {1: \"a\", 2: \"b\"};\ncheck if {\"a\": 1, \"b\": 2, \"c\": 3, \"d\": 4}.contains(\"d\");\ncheck if {1: \"A\", \"a\": 1, \"b\": 2}.get(\"a\") == 1;\ncheck if {1: \"A\", \"a\": 1, \"b\": 2}.get(1) == \"A\";\ncheck if {1: \"A\", \"a\": 1, \"b\": 2}.get(\"c\") == null;\ncheck if {1: \"A\", \"a\": 1, \"b\": 2}.get(2) == null;\ncheck if {\"a\": 1, \"b\": 2}.all($kv -> $kv.get(0) != \"c\" && $kv.get(1) < 3);\ncheck if {1: \"A\", \"a\": 1, \"b\": 2}.any($kv -> $kv.get(0) == 1 && $kv.get(1) == \"A\");\ncheck if {\"user\": {\"id\": 1, \"roles\": [\"admin\"]}}.get(\"user\").get(\"roles\").contains(\"admin\");\n", + "code": "check if [1, 2, 1].length() == 3;\ncheck if [\"a\", \"b\"] != true;\ncheck if [\"a\", \"b\"] != [1, 2, 3];\ncheck if [\"a\", \"b\"] == [\"a\", \"b\"];\ncheck if [\"a\", \"b\"] === [\"a\", \"b\"];\ncheck if [\"a\", \"b\"] !== [\"a\", \"c\"];\ncheck if [\"a\", \"b\", \"c\"].contains(\"c\");\ncheck if [1, 2, 3].starts_with([1, 2]);\ncheck if [4, 5, 6].ends_with([6]);\ncheck if [1, 2, \"a\"].get(2) == \"a\";\ncheck if [1, 2].get(3) == null;\ncheck if [1, 2].get(-1) == null;\ncheck if [1, 2, 3].all($p -> $p > 0);\ncheck if [1, 2, 3].any($p -> $p > 2);\ncheck if {\"a\": 1, \"b\": 2, \"c\": 3, \"d\": 4}.length() == 4;\ncheck if {1: \"a\", 2: \"b\"} != true;\ncheck if {1: \"a\", 2: \"b\"} != {\"a\": 1, \"b\": 2};\ncheck if {1: \"a\", 2: \"b\"} == {1: \"a\", 2: \"b\"};\ncheck if {1: \"a\", 2: \"b\"} !== {\"a\": 1, \"b\": 2};\ncheck if {1: \"a\", 2: \"b\"} === {1: \"a\", 2: \"b\"};\ncheck if {\"a\": 1, \"b\": 2, \"c\": 3, \"d\": 4}.contains(\"d\");\ncheck if {1: \"A\", \"a\": 1, \"b\": 2}.get(\"a\") == 1;\ncheck if {1: \"A\", \"a\": 1, \"b\": 2}.get(1) == \"A\";\ncheck if {1: \"A\", \"a\": 1, \"b\": 2}.get(\"c\") == null;\ncheck if {1: \"A\", \"a\": 1, \"b\": 2}.get(2) == null;\ncheck if {\"a\": 1, \"b\": 2}.all($kv -> $kv.get(0) != \"c\" && $kv.get(1) < 3);\ncheck if {1: \"A\", \"a\": 1, \"b\": 2}.any($kv -> $kv.get(0) == 1 && $kv.get(1) == \"A\");\ncheck if {\"user\": {\"id\": 1, \"roles\": [\"admin\"]}}.get(\"user\").get(\"roles\").contains(\"admin\");\n", "version": 6 } ], @@ -2992,17 +3177,18 @@ "check if [1, 2, 3].all($p -> $p > 0)", "check if [1, 2, 3].any($p -> $p > 2)", "check if [1, 2, 3].starts_with([1, 2])", + "check if [1, 2].get(-1) == null", "check if [1, 2].get(3) == null", "check if [4, 5, 6].ends_with([6])", "check if {\"a\": 1, \"b\": 2, \"c\": 3, \"d\": 4}.contains(\"d\")", "check if {\"a\": 1, \"b\": 2, \"c\": 3, \"d\": 4}.length() == 4", "check if {\"a\": 1, \"b\": 2}.all($kv -> $kv.get(0) != \"c\" && $kv.get(1) < 3)", "check if {\"user\": {\"id\": 1, \"roles\": [\"admin\"]}}.get(\"user\").get(\"roles\").contains(\"admin\")", - "check if {\"a\": 1, \"b\": 2, 1: \"A\"}.any($kv -> $kv.get(0) == 1 && $kv.get(1) == \"A\")", - "check if {\"a\": 1, \"b\": 2, 1: \"A\"}.get(\"a\") == 1", - "check if {\"a\": 1, \"b\": 2, 1: \"A\"}.get(\"c\") == null", - "check if {\"a\": 1, \"b\": 2, 1: \"A\"}.get(1) == \"A\"", - "check if {\"a\": 1, \"b\": 2, 1: \"A\"}.get(2) == null", + "check if {1: \"A\", \"a\": 1, \"b\": 2}.any($kv -> $kv.get(0) == 1 && $kv.get(1) == \"A\")", + "check if {1: \"A\", \"a\": 1, \"b\": 2}.get(\"a\") == 1", + "check if {1: \"A\", \"a\": 1, \"b\": 2}.get(\"c\") == null", + "check if {1: \"A\", \"a\": 1, \"b\": 2}.get(1) == \"A\"", + "check if {1: \"A\", \"a\": 1, \"b\": 2}.get(2) == null", "check if {1: \"a\", 2: \"b\"} != true", "check if {1: \"a\", 2: \"b\"} != {\"a\": 1, \"b\": 2}", "check if {1: \"a\", 2: \"b\"} !== {\"a\": 1, \"b\": 2}", @@ -3013,14 +3199,61 @@ ], "policies": [ "allow if true" - ] + ], + "elapsed_steps": 72 + }, + "result": { + "type": "ok", + "value": 0 + }, + "authorizer_code": "allow if true;\n", + "revocation_ids": [ + "0227fc29d6bf85cc52503f497b0b232235ffe5192c68933388136eae3fc3419229e9489b85d1bb57525554edac7cda87c984c96639bfb4171acc499312ffe30e" + ] + } + } + }, + { + "title": "test ffi calls (v6 blocks)", + "filename": "test035_ffi.bc", + "token": [ + { + "symbols": [ + "test", + "a", + "equal strings" + ], + "public_keys": [], + "external_key": null, + "code": "check if true.extern::test(), \"a\".extern::test(\"a\") == \"equal strings\";\n", + "version": 6 + } + ], + "validations": { + "": { + "world": { + "facts": [], + "rules": [], + "checks": [ + { + "origin": 0, + "checks": [ + "check if true.extern::test(), \"a\".extern::test(\"a\") == \"equal strings\"" + ] + } + ], + "policies": [ + "allow if true" + ], + "elapsed_steps": 3 }, "result": { - "Ok": 0 + "type": "ok", + "value": 0 }, "authorizer_code": "allow if true;\n", "revocation_ids": [ - "b22238a06ca9c015d3c49d4ebaa7e8ab6e0d69119b3264033618e726d62fc6f4757a7bebc25f255444aba39994554a62a53ecc13b68802efab8da85ace62390d" + "d1719fd101c2695d2dac4df67569918363f691b6167670e1dbbf8026f639a7aa1ec2e13707f4d34cadbb2adce5c6e8a816577dd069a8717e0f5cb4ea3cec5b04" ] } } @@ -3084,10 +3317,12 @@ ], "policies": [ "allow if true" - ] + ], + "elapsed_steps": 3 }, "result": { - "Ok": 0 + "type": "ok", + "value": 0 }, "authorizer_code": "resource(\"file1\");\noperation(\"read\");\n\nallow if true;\n", "revocation_ids": [ @@ -3174,10 +3409,12 @@ ], "policies": [ "allow if true" - ] + ], + "elapsed_steps": 4 }, "result": { - "Ok": 0 + "type": "ok", + "value": 0 }, "authorizer_code": "resource(\"file1\");\noperation(\"read\");\n\nallow if true;\n", "revocation_ids": [ @@ -3216,10 +3453,12 @@ ], "policies": [ "allow if true" - ] + ], + "elapsed_steps": 10 }, "result": { - "Ok": 0 + "type": "ok", + "value": 0 }, "authorizer_code": "allow if true;\n", "revocation_ids": [ @@ -3248,11 +3487,16 @@ ], "policies": [ "allow if true" - ] + ], + "elapsed_steps": 1 }, "result": { - "Err": { - "Execution": "InvalidType" + "type": "err", + "value": { + "type": "execution", + "value": { + "type": "invalid_type" + } } }, "authorizer_code": "check if true.try_or(true === 12);\n\nallow if true;\n", @@ -3261,6 +3505,54 @@ ] } } + }, + { + "title": "block referencing a symbol defined in a later block", + "filename": "test039_forward_symbol_reference.bc", + "token": [ + { + "symbols": [ + "file1" + ], + "public_keys": [], + "external_key": null, + "code": "right(\"file1\", \"read\");\n", + "version": 3 + }, + { + "symbols": [], + "public_keys": [], + "external_key": null, + "code": "check if resource(\"file2\");\n", + "version": 3 + }, + { + "symbols": [ + "file2" + ], + "public_keys": [], + "external_key": null, + "code": "check if resource(\"file2\");\n", + "version": 3 + } + ], + "validations": { + "": { + "world": null, + "result": { + "type": "err", + "value": { + "type": "format", + "value": { + "type": "unknown_symbol", + "value": 1025 + } + } + }, + "authorizer_code": "", + "revocation_ids": [] + } + } } ] } diff --git a/biscuit/src/test/resources/samples/test034_array_map.bc b/biscuit/src/test/resources/samples/test034_array_map.bc index 9deb150af51632d8d064ffc6841549bc54fac8b3..8107cb16b2d8280a00b4e25224854f378788e685 100644 GIT binary patch delta 127 zcmZ3;e~X`0Xfr?8v5l;nOlsy_rd&)M(o*_dJX~B}99&ESj6lr9#mmJl@E-;kH%l`q zusSfQ|Ixg*zx7N|fW2omx3ZGy|EH2V8Iz4Wg!9(fA9kFi`O;%{>&4ySL7^dU*VNo< aKiP6J&2s-1aj7$&lZF02=9^s2t_A=GQZJLIslXM}#GOvMr|7G04QUaZGGqfb u80RL|FUItBdVA}_UnNvTtD~8eRZ3!|KFkxghyw4cji_4AVmS?yvX zA5+_9lsq%Wc$s^|s`VSyqnU*swMaQ!ES!Il@la;0?i$~3rI{0(lfO;eCRSGP@b>-& zwQrWoSIHfEXwLrSvd`MxT6dlvd$B?+y!Jxoio!bnm@Tht-bAx#Fe)i=Da?FP%{MV3 fSnl0c=KrEhrY{1f+wTxO*Tx}~wXT8;I zO!bz3S<;h{6PBLfptjO~$L%gzrh6r|-{fZgnw9)tqhc+CR4tdV1hWx87cUnRhqMqo z2NTcV22~9L!=sx|i&z<;{x0d^8OQ)~;=fCyhX1l$&qAp4~u-w=pE1#yn z^v1qzcP8&-$y-p^aO&!MyFkmCj6aT_>``xs3|r!(o{%TjrEx)%?<1d;`d!XrUz7Q^ zq)#amIkJpJs)dnD3K9lJIGtg3_-*q0Hi1^pv@0K+*n2DfzCO;nlxeN@gwKCczVF*T zUCLp)#dNkM3-kovYF&PztTa1qPE*tQ<*}(k&z4`>5ca#7zy4xOgp##&x3ZL7 Date: Tue, 24 Nov 2026 08:21:35 +0100 Subject: [PATCH 03/11] Update of the associated code --- .../org/eclipse/biscuit/datalog/Check.java | 16 +-- .../org/eclipse/biscuit/datalog/Fact.java | 10 +- .../eclipse/biscuit/datalog/Predicate.java | 12 +- .../org/eclipse/biscuit/datalog/Rule.java | 18 +-- .../org/eclipse/biscuit/datalog/Term.java | 104 +++++++++--------- .../datalog/expressions/Expression.java | 10 +- .../biscuit/datalog/expressions/Op.java | 18 +-- .../java/org/eclipse/biscuit/token/Block.java | 24 ++-- .../token/DeserializationLimitsTest.java | 4 +- 9 files changed, 108 insertions(+), 108 deletions(-) diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Check.java b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Check.java index 49767fdc..64cd64a6 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Check.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Check.java @@ -5,8 +5,8 @@ package org.eclipse.biscuit.datalog; -import static biscuit.format.schema.Schema.CheckV2.Kind.All; -import static biscuit.format.schema.Schema.CheckV2.Kind.Reject; +import static biscuit.format.schema.Schema.Check.Kind.All; +import static biscuit.format.schema.Schema.Check.Kind.Reject; import biscuit.format.schema.Schema; import java.util.ArrayList; @@ -42,8 +42,8 @@ public List queries() { return queries; } - public Schema.CheckV2 serialize() { - Schema.CheckV2.Builder b = Schema.CheckV2.newBuilder(); + public Schema.Check serialize() { + Schema.Check.Builder b = Schema.Check.newBuilder(); // do not set the kind to One to keep compatibility with older library versions switch (this.kind) { @@ -63,8 +63,8 @@ public Schema.CheckV2 serialize() { return b.build(); } - public static Result deserializeV2( - Schema.CheckV2 check, DeserializationLimit.PerBlock limits, SymbolTable symbols) { + public static Result deserialize( + Schema.Check check, DeserializationLimit.PerBlock limits, SymbolTable symbols) { ArrayList queries = new ArrayList<>(); Kind kind; @@ -95,8 +95,8 @@ public static Result deserializeV2( Error.FormatError.DeserializationLimitError.Reason.TOO_MANY_QUERIES)); } - for (Schema.RuleV2 query : check.getQueriesList()) { - var res = Rule.deserializeV2(query, limits, symbols); + for (Schema.Rule query : check.getQueriesList()) { + var res = Rule.deserialize(query, limits, symbols); if (res.isErr()) { return Result.err(res.getErr()); } else { diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Fact.java b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Fact.java index 8bc45874..be5478fc 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Fact.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Fact.java @@ -53,13 +53,13 @@ public String toString() { return this.predicate.toString(); } - public Schema.FactV2 serialize() { - return Schema.FactV2.newBuilder().setPredicate(this.predicate.serialize()).build(); + public Schema.Fact serialize() { + return Schema.Fact.newBuilder().setPredicate(this.predicate.serialize()).build(); } - public static Result deserializeV2( - Schema.FactV2 fact, DeserializationLimit.PerBlock limits, SymbolTable symbols) { - var res = Predicate.deserializeV2(fact.getPredicate(), limits, symbols); + public static Result deserialize( + Schema.Fact fact, DeserializationLimit.PerBlock limits, SymbolTable symbols) { + var res = Predicate.deserialize(fact.getPredicate(), limits, symbols); if (res.isErr()) { return Result.err(res.getErr()); } else { diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Predicate.java b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Predicate.java index 0d18a996..90d47fe4 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Predicate.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Predicate.java @@ -83,8 +83,8 @@ public String toString() { + ")"; } - public Schema.PredicateV2 serialize() { - Schema.PredicateV2.Builder builder = Schema.PredicateV2.newBuilder().setName(this.name); + public Schema.Predicate serialize() { + Schema.Predicate.Builder builder = Schema.Predicate.newBuilder().setName(this.name); for (Term term : this.terms) { builder.addTerms(term.serializeTerm()); @@ -93,8 +93,8 @@ public Schema.PredicateV2 serialize() { return builder.build(); } - public static Result deserializeV2( - Schema.PredicateV2 predicate, DeserializationLimit.PerBlock limits, SymbolTable symbols) { + public static Result deserialize( + Schema.Predicate predicate, DeserializationLimit.PerBlock limits, SymbolTable symbols) { if (predicate.getTermsCount() > limits.getMaxTermsPerPredicate()) { return Result.err( new Error.FormatError.DeserializationLimitError( @@ -106,8 +106,8 @@ public static Result deserializeV2( } ArrayList terms = new ArrayList<>(); - for (Schema.TermV2 id : predicate.getTermsList()) { - var res = Term.deserializeEnumV2(id, limits, symbols); + for (Schema.Term id : predicate.getTermsList()) { + var res = Term.deserializeEnum(id, limits, symbols); if (res.isErr()) { return Result.err(res.getErr()); } else { diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Rule.java b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Rule.java index ff595b49..bb4302a9 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Rule.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Rule.java @@ -207,8 +207,8 @@ public Rule( this.scopes = scopes; } - public Schema.RuleV2 serialize() { - Schema.RuleV2.Builder b = Schema.RuleV2.newBuilder().setHead(this.head.serialize()); + public Schema.Rule serialize() { + Schema.Rule.Builder b = Schema.Rule.newBuilder().setHead(this.head.serialize()); for (Predicate predicate : this.body) { b.addBody(predicate.serialize()); @@ -225,8 +225,8 @@ public Schema.RuleV2 serialize() { return b.build(); } - public static Result deserializeV2( - Schema.RuleV2 rule, DeserializationLimit.PerBlock limits, SymbolTable symbols) { + public static Result deserialize( + Schema.Rule rule, DeserializationLimit.PerBlock limits, SymbolTable symbols) { if (rule.getBodyCount() > limits.getMaxPredicatesPerBody()) { return Result.err( new Error.FormatError.DeserializationLimitError( @@ -239,8 +239,8 @@ public static Result deserializeV2( } ArrayList body = new ArrayList<>(); - for (Schema.PredicateV2 predicate : rule.getBodyList()) { - var res = Predicate.deserializeV2(predicate, limits, symbols); + for (Schema.Predicate predicate : rule.getBodyList()) { + var res = Predicate.deserialize(predicate, limits, symbols); if (res.isErr()) { return Result.err(res.getErr()); } else { @@ -249,8 +249,8 @@ public static Result deserializeV2( } ArrayList expressions = new ArrayList<>(); - for (Schema.ExpressionV2 expression : rule.getExpressionsList()) { - var res = Expression.deserializeV2(expression, limits, symbols); + for (Schema.Expression expression : rule.getExpressionsList()) { + var res = Expression.deserialize(expression, limits, symbols); if (res.isErr()) { return Result.err(res.getErr()); } else { @@ -268,7 +268,7 @@ public static Result deserializeV2( } } - var res = Predicate.deserializeV2(rule.getHead(), limits, symbols); + var res = Predicate.deserialize(rule.getHead(), limits, symbols); if (res.isErr()) { Error.FormatError e = res.getErr(); return Result.err(e); diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Term.java b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Term.java index e770b5b5..c96da1e2 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Term.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Term.java @@ -23,7 +23,7 @@ public abstract class Term extends Op { public abstract boolean match(Term other); - public abstract Schema.TermV2 serializeTerm(); + public abstract Schema.Term serializeTerm(); public abstract String typeOf(); @@ -61,8 +61,8 @@ public String print(Deque stack, SymbolTable symbols) { return s; } - public static Result deserializeEnumV2( - Schema.TermV2 term, DeserializationLimit.PerBlock limits, SymbolTable symbols) { + public static Result deserializeEnum( + Schema.Term term, DeserializationLimit.PerBlock limits, SymbolTable symbols) { var allowedTypes = limits.getAllowedTermTypes(); // a variable is not a value, so it has no type var type = termType(term.getContentCase()); @@ -74,39 +74,39 @@ public static Result deserializeEnumV2( switch (term.getContentCase()) { case DATE: - return Date.deserializeV2(term); + return Date.deserialize(term); case INTEGER: - return Integer.deserializeV2(term); + return Integer.deserialize(term); case STRING: var stringError = limits.checkString(symbols, term.getString()); if (stringError != null) { return Result.err(stringError); } - return Str.deserializeV2(term); + return Str.deserialize(term); case BYTES: if (term.getBytes().size() > limits.getMaxBytesLength()) { return Result.err( new Error.FormatError.DeserializationLimitError( Error.FormatError.DeserializationLimitError.Reason.BYTES_TOO_LONG)); } - return Bytes.deserializeV2(term); + return Bytes.deserialize(term); case VARIABLE: var nameError = limits.checkName(symbols, java.lang.Integer.toUnsignedLong(term.getVariable())); if (nameError != null) { return Result.err(nameError); } - return Variable.deserializeV2(term); + return Variable.deserialize(term); case BOOL: - return Bool.deserializeV2(term); + return Bool.deserialize(term); case SET: - return Set.deserializeV2(term, limits, symbols); + return Set.deserialize(term, limits, symbols); case NULL: - return Null.deserializeV2(term); + return Null.deserialize(term); case ARRAY: - return Array.deserializeV2(term, limits, symbols); + return Array.deserialize(term, limits, symbols); case MAP: - return Map.deserializeV2(term, limits, symbols); + return Map.deserialize(term, limits, symbols); default: return Result.err( new Error.FormatError.DeserializationError( @@ -114,7 +114,7 @@ public static Result deserializeEnumV2( } } - private static DeserializationLimit.TermType termType(Schema.TermV2.ContentCase content) { + private static DeserializationLimit.TermType termType(Schema.Term.ContentCase content) { switch (content) { case INTEGER: return DeserializationLimit.TermType.INTEGER; @@ -172,15 +172,15 @@ public String toString() { return "null"; } - public Schema.TermV2 serializeTerm() { - return Schema.TermV2.newBuilder().setNull(Schema.Empty.newBuilder().build()).build(); + public Schema.Term serializeTerm() { + return Schema.Term.newBuilder().setNull(Schema.Empty.newBuilder().build()).build(); } public String typeOf() { return "null"; } - public static Result deserializeV2(Schema.TermV2 term) { + public static Result deserialize(Schema.Term term) { if (!term.hasNull()) { return Result.err( new Error.FormatError.DeserializationError("invalid Term kind, expected null")); @@ -237,15 +237,15 @@ public String toString() { return "@" + this.value; } - public Schema.TermV2 serializeTerm() { - return Schema.TermV2.newBuilder().setDate(this.value).build(); + public Schema.Term serializeTerm() { + return Schema.Term.newBuilder().setDate(this.value).build(); } public String typeOf() { return "date"; } - public static Result deserializeV2(Schema.TermV2 term) { + public static Result deserialize(Schema.Term term) { if (!term.hasDate()) { return Result.err( new Error.FormatError.DeserializationError("invalid Term kind, expected date")); @@ -304,8 +304,8 @@ public String toString() { return String.valueOf(this.value); } - public Schema.TermV2 serializeTerm() { - return Schema.TermV2.newBuilder().setInteger(this.value).build(); + public Schema.Term serializeTerm() { + return Schema.Term.newBuilder().setInteger(this.value).build(); } public String typeOf() { @@ -316,7 +316,7 @@ public Schema.MapKey serializeMapKey() { return Schema.MapKey.newBuilder().setInteger(this.value).build(); } - public static Result deserializeV2(Schema.TermV2 term) { + public static Result deserialize(Schema.Term term) { if (!term.hasInteger()) { return Result.err( new Error.FormatError.DeserializationError("invalid Term kind, expected integer")); @@ -386,15 +386,15 @@ public String toString() { return this.value.toString(); } - public Schema.TermV2 serializeTerm() { - return Schema.TermV2.newBuilder().setBytes(ByteString.copyFrom(this.value)).build(); + public Schema.Term serializeTerm() { + return Schema.Term.newBuilder().setBytes(ByteString.copyFrom(this.value)).build(); } public String typeOf() { return "bytes"; } - public static Result deserializeV2(Schema.TermV2 term) { + public static Result deserialize(Schema.Term term) { if (!term.hasBytes()) { return Result.err( new Error.FormatError.DeserializationError("invalid Term kind, expected byte array")); @@ -448,8 +448,8 @@ public int hashCode() { return Long.hashCode(value); } - public Schema.TermV2 serializeTerm() { - return Schema.TermV2.newBuilder().setString(this.value).build(); + public Schema.Term serializeTerm() { + return Schema.Term.newBuilder().setString(this.value).build(); } public String typeOf() { @@ -460,7 +460,7 @@ public Schema.MapKey serializeMapKey() { return Schema.MapKey.newBuilder().setString(this.value).build(); } - public static Result deserializeV2(Schema.TermV2 term) { + public static Result deserialize(Schema.Term term) { if (!term.hasString()) { return Result.err( new Error.FormatError.DeserializationError("invalid Term kind, expected string")); @@ -528,15 +528,15 @@ public String toString() { return this.value + "?"; } - public Schema.TermV2 serializeTerm() { - return Schema.TermV2.newBuilder().setVariable((int) this.value).build(); + public Schema.Term serializeTerm() { + return Schema.Term.newBuilder().setVariable((int) this.value).build(); } public String typeOf() { throw new InternalError(); } - public static Result deserializeV2(Schema.TermV2 term) { + public static Result deserialize(Schema.Term term) { if (!term.hasVariable()) { return Result.err( new Error.FormatError.DeserializationError("invalid Term kind, expected variable")); @@ -596,15 +596,15 @@ public String toString() { return String.valueOf(this.value); } - public Schema.TermV2 serializeTerm() { - return Schema.TermV2.newBuilder().setBool(this.value).build(); + public Schema.Term serializeTerm() { + return Schema.Term.newBuilder().setBool(this.value).build(); } public String typeOf() { return "bool"; } - public static Result deserializeV2(Schema.TermV2 term) { + public static Result deserialize(Schema.Term term) { if (!term.hasBool()) { return Result.err( new Error.FormatError.DeserializationError("invalid Term kind, expected boolean")); @@ -663,22 +663,22 @@ public String toString() { return String.valueOf(value); } - public Schema.TermV2 serializeTerm() { + public Schema.Term serializeTerm() { Schema.Array.Builder s = Schema.Array.newBuilder(); for (Term l : this.value) { s.addArray(l.serializeTerm()); } - return Schema.TermV2.newBuilder().setArray(s).build(); + return Schema.Term.newBuilder().setArray(s).build(); } public String typeOf() { return "array"; } - public static Result deserializeV2( - Schema.TermV2 term, DeserializationLimit.PerBlock limits, SymbolTable symbols) { + public static Result deserialize( + Schema.Term term, DeserializationLimit.PerBlock limits, SymbolTable symbols) { if (!term.hasArray()) { return Result.err( new Error.FormatError.DeserializationError("invalid Term kind, expected array")); @@ -692,8 +692,8 @@ public static Result deserializeV2( Error.FormatError.DeserializationLimitError.Reason.ARRAY_TOO_LONG)); } - for (Schema.TermV2 l : s.getArrayList()) { - var res = Term.deserializeEnumV2(l, limits, symbols); + for (Schema.Term l : s.getArrayList()) { + var res = Term.deserializeEnum(l, limits, symbols); if (res.isErr()) { return Result.err(res.getErr()); } else { @@ -772,7 +772,7 @@ public String toString() { return String.valueOf(value); } - public Schema.TermV2 serializeTerm() { + public Schema.Term serializeTerm() { Schema.Map.Builder s = Schema.Map.newBuilder(); for (java.util.Map.Entry i : this.value.entrySet()) { @@ -782,15 +782,15 @@ public Schema.TermV2 serializeTerm() { s.addEntries(m); } - return Schema.TermV2.newBuilder().setMap(s).build(); + return Schema.Term.newBuilder().setMap(s).build(); } public String typeOf() { return "map"; } - public static Result deserializeV2( - Schema.TermV2 term, DeserializationLimit.PerBlock limits, SymbolTable symbols) { + public static Result deserialize( + Schema.Term term, DeserializationLimit.PerBlock limits, SymbolTable symbols) { if (!term.hasMap()) { return Result.err( new Error.FormatError.DeserializationError("invalid Term kind, expected map")); @@ -809,7 +809,7 @@ public static Result deserializeV2( if (resKey.isErr()) { return Result.err(resKey.getErr()); } - var resValue = Term.deserializeEnumV2(l.getValue(), limits, symbols); + var resValue = Term.deserializeEnum(l.getValue(), limits, symbols); if (resValue.isErr()) { return Result.err(resValue.getErr()); } @@ -898,22 +898,22 @@ public String toString() { return s + "}"; } - public Schema.TermV2 serializeTerm() { + public Schema.Term serializeTerm() { Schema.TermSet.Builder s = Schema.TermSet.newBuilder(); for (Term l : this.value) { s.addSet(l.serializeTerm()); } - return Schema.TermV2.newBuilder().setSet(s).build(); + return Schema.Term.newBuilder().setSet(s).build(); } public String typeOf() { return "set"; } - public static Result deserializeV2( - Schema.TermV2 term, DeserializationLimit.PerBlock limits, SymbolTable symbols) { + public static Result deserialize( + Schema.Term term, DeserializationLimit.PerBlock limits, SymbolTable symbols) { if (!term.hasSet()) { return Result.err( new Error.FormatError.DeserializationError("invalid Term kind, expected set")); @@ -927,8 +927,8 @@ public static Result deserializeV2( Error.FormatError.DeserializationLimitError.Reason.SET_TOO_LONG)); } - for (Schema.TermV2 l : s.getSetList()) { - var res = Term.deserializeEnumV2(l, limits, symbols); + for (Schema.Term l : s.getSetList()) { + var res = Term.deserializeEnum(l, limits, symbols); if (res.isErr()) { return Result.err(res.getErr()); } else { diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/expressions/Expression.java b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/expressions/Expression.java index e771db94..0c9ac7cf 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/expressions/Expression.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/expressions/Expression.java @@ -63,8 +63,8 @@ public Optional print(SymbolTable symbolTable) { } } - public Schema.ExpressionV2 serialize() { - Schema.ExpressionV2.Builder b = Schema.ExpressionV2.newBuilder(); + public Schema.Expression serialize() { + Schema.Expression.Builder b = Schema.Expression.newBuilder(); for (Op op : this.ops) { b.addOps(op.serialize()); @@ -73,14 +73,14 @@ public Schema.ExpressionV2 serialize() { return b.build(); } - public static Result deserializeV2( - Schema.ExpressionV2 e, DeserializationLimit.PerBlock limits, SymbolTable symbols) { + public static Result deserialize( + Schema.Expression e, DeserializationLimit.PerBlock limits, SymbolTable symbols) { ArrayList ops = new ArrayList<>(); // closures share the limit with the expression they are nested in int[] remainingOps = {limits.getMaxOpsPerExpression()}; for (Schema.Op op : e.getOpsList()) { - var res = Op.deserializeV2(op, limits, symbols, remainingOps); + var res = Op.deserialize(op, limits, symbols, remainingOps); if (res.isErr()) { return Result.err(res.getErr()); diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/expressions/Op.java b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/expressions/Op.java index 2f618544..9392e616 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/expressions/Op.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/expressions/Op.java @@ -36,7 +36,7 @@ public abstract void evaluate( public abstract Schema.Op serialize(); - public static Result deserializeV2( + public static Result deserialize( Schema.Op op, DeserializationLimit.PerBlock limits, SymbolTable symbols, int[] remainingOps) { if (remainingOps[0] <= 0) { return Result.err( @@ -46,10 +46,10 @@ public static Result deserializeV2( remainingOps[0]--; if (op.hasValue()) { - var res = Term.deserializeEnumV2(op.getValue(), limits, symbols); + var res = Term.deserializeEnum(op.getValue(), limits, symbols); return res.isOk() ? Result.ok(res.getOk()) : Result.err(res.getErr()); } else if (op.hasUnary()) { - return Op.Unary.deserializeV2(op.getUnary(), limits, symbols); + return Op.Unary.deserialize(op.getUnary(), limits, symbols); } else if (op.hasBinary()) { return Op.Binary.deserializeV1(op.getBinary(), limits, symbols); } else if (op.hasClosure()) { @@ -217,7 +217,7 @@ public Schema.Op serialize() { return b.build(); } - public static Result deserializeV2( + public static Result deserialize( Schema.OpUnary op, DeserializationLimit.PerBlock limits, SymbolTable symbols) { if (op.getKind() == Schema.OpUnary.Kind.Ffi && op.hasFfiName()) { var error = @@ -228,7 +228,7 @@ public static Result deserializeV2( } } - var res = deserializeV2(op); + var res = deserialize(op); var allowedOps = limits.getAllowedUnaryOps(); if (allowedOps != null && res.isOk() @@ -240,7 +240,7 @@ public static Result deserializeV2( return res; } - public static Result deserializeV2(Schema.OpUnary op) { + public static Result deserialize(Schema.OpUnary op) { switch (op.getKind()) { case Negate: return Result.ok(new Op.Unary(UnaryOp.Negate)); @@ -1033,7 +1033,7 @@ public Schema.Op serialize() { b1.setKind(Schema.OpBinary.Kind.All); break; case TryOr: - b1.setKind(Schema.OpBinary.Kind.Try); + b1.setKind(Schema.OpBinary.Kind.TryOr); break; default: } @@ -1124,7 +1124,7 @@ public static Result deserializeV1(Schema.OpBinary op) { return Result.ok(new Op.Binary(BinaryOp.Any)); case All: return Result.ok(new Op.Binary(BinaryOp.All)); - case Try: + case TryOr: return Result.ok(new Op.Binary(BinaryOp.TryOr)); default: return Result.err( @@ -1283,7 +1283,7 @@ public static Result deserializeV1( } for (Schema.Op op : closure.getOpsList()) { - var res = Op.deserializeV2(op, limits, symbols, remainingOps); + var res = Op.deserialize(op, limits, symbols, remainingOps); if (res.isErr()) { return Result.err(res.getErr()); } else { diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/token/Block.java b/biscuit-core/src/main/java/org/eclipse/biscuit/token/Block.java index 0e7f59a6..f3c37a4c 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/token/Block.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/token/Block.java @@ -204,15 +204,15 @@ public Schema.Block serialize() { } for (Fact fact : this.facts) { - b.addFactsV2(fact.serialize()); + b.addFacts(fact.serialize()); } for (Rule rule : this.rules) { - b.addRulesV2(rule.serialize()); + b.addRules(rule.serialize()); } for (Check check : this.checks) { - b.addChecksV2(check.serialize()); + b.addChecks(check.serialize()); } for (Scope scope : this.scopes) { @@ -264,17 +264,17 @@ public static Result deserialize( SerializedBiscuit.MIN_SCHEMA_VERSION, SerializedBiscuit.MAX_SCHEMA_VERSION, version)); } - if (b.getFactsV2Count() > limits.getMaxFacts()) { + if (b.getFactsCount() > limits.getMaxFacts()) { return Result.err( new Error.FormatError.DeserializationLimitError( Error.FormatError.DeserializationLimitError.Reason.TOO_MANY_FACTS)); } - if (b.getChecksV2Count() > limits.getMaxChecks()) { + if (b.getChecksCount() > limits.getMaxChecks()) { return Result.err( new Error.FormatError.DeserializationLimitError( Error.FormatError.DeserializationLimitError.Reason.TOO_MANY_CHECKS)); } - if (b.getRulesV2Count() > limits.getMaxRules()) { + if (b.getRulesCount() > limits.getMaxRules()) { return Result.err( new Error.FormatError.DeserializationLimitError( Error.FormatError.DeserializationLimitError.Reason.TOO_MANY_RULES)); @@ -322,8 +322,8 @@ public static Result deserialize( ArrayList rules = new ArrayList<>(); ArrayList checks = new ArrayList<>(); - for (Schema.FactV2 fact : b.getFactsV2List()) { - var res = Fact.deserializeV2(fact, limits, baseSymbols); + for (Schema.Fact fact : b.getFactsList()) { + var res = Fact.deserialize(fact, limits, baseSymbols); if (res.isErr()) { return Result.err(res.getErr()); } else { @@ -331,8 +331,8 @@ public static Result deserialize( } } - for (Schema.RuleV2 rule : b.getRulesV2List()) { - var res = Rule.deserializeV2(rule, limits, baseSymbols); + for (Schema.Rule rule : b.getRulesList()) { + var res = Rule.deserialize(rule, limits, baseSymbols); if (res.isErr()) { return Result.err(res.getErr()); } else { @@ -340,8 +340,8 @@ public static Result deserialize( } } - for (Schema.CheckV2 check : b.getChecksV2List()) { - var res = Check.deserializeV2(check, limits, baseSymbols); + for (Schema.Check check : b.getChecksList()) { + var res = Check.deserialize(check, limits, baseSymbols); if (res.isErr()) { return Result.err(res.getErr()); } else { diff --git a/biscuit/src/test/java/org/eclipse/biscuit/token/DeserializationLimitsTest.java b/biscuit/src/test/java/org/eclipse/biscuit/token/DeserializationLimitsTest.java index 9a38236b..bfaaee73 100644 --- a/biscuit/src/test/java/org/eclipse/biscuit/token/DeserializationLimitsTest.java +++ b/biscuit/src/test/java/org/eclipse/biscuit/token/DeserializationLimitsTest.java @@ -394,8 +394,8 @@ public void testTrustedScopeKindsAreComparedByTheirAlgorithm() { @Test public void testATermWithNoContentIsRejectedWithItsKindInTheMessage() { var result = - org.eclipse.biscuit.datalog.Term.deserializeEnumV2( - Schema.TermV2.getDefaultInstance(), + org.eclipse.biscuit.datalog.Term.deserializeEnum( + Schema.Term.getDefaultInstance(), PerBlock.allowEverything(), new org.eclipse.biscuit.datalog.SymbolTable()); From a79a96ce2fb87b33eb9a28b6504990e72c0ff4e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Corbi=C3=A8re?= Date: Mon, 23 Nov 2026 07:50:45 +0100 Subject: [PATCH 04/11] Integrate elapsed_steps and new samples serialization --- .../java/org/eclipse/biscuit/token/SamplesTest.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/biscuit/src/test/java/org/eclipse/biscuit/token/SamplesTest.java b/biscuit/src/test/java/org/eclipse/biscuit/token/SamplesTest.java index 59b22858..1df19a6c 100644 --- a/biscuit/src/test/java/org/eclipse/biscuit/token/SamplesTest.java +++ b/biscuit/src/test/java/org/eclipse/biscuit/token/SamplesTest.java @@ -241,6 +241,7 @@ DynamicTest processTestcase( assertEquals(world.rules, authorizerWorld.rules); assertEquals(world.checks, authorizerWorld.checks); assertEquals(world.policies, authorizerWorld.policies); + assertEquals(world.elapsedSteps, authorizerWorld.elapsedSteps); } res = Result.ok(authorizeResult); @@ -255,6 +256,7 @@ DynamicTest processTestcase( assertEquals(world.rules, authorizerWorld.rules); assertEquals(world.checks, authorizerWorld.checks); assertEquals(world.policies, authorizerWorld.policies); + assertEquals(world.elapsedSteps, authorizerWorld.elapsedSteps); } throw e; @@ -263,7 +265,7 @@ DynamicTest processTestcase( res = Result.err(e); } - if (expectedResult.has("Ok")) { + if ("ok".equals(expectedResult.get("type").textValue())) { if (res.isErr()) { System.out.println( "validation '" @@ -385,16 +387,19 @@ static class World { List rules; List checks; List policies; + long elapsedSteps; public World( @JsonProperty("facts") List facts, @JsonProperty("rules") List rules, @JsonProperty("checks") List checks, - @JsonProperty("policies") List policies) { + @JsonProperty("policies") List policies, + @JsonProperty("elapsed_steps") long elapsedSteps) { this.facts = facts; this.rules = rules.stream().sorted().collect(Collectors.toList()); this.checks = checks.stream().sorted().collect(Collectors.toList()); this.policies = policies; + this.elapsedSteps = elapsedSteps; } public World(Authorizer authorizer) { @@ -472,6 +477,8 @@ public String toString() { + checks + ",\npolicies=" + policies + + ",\nelapsed_steps=" + + elapsedSteps + '}'; } } From a3b31bd777f8fb47af9fd8e323dcadfd93b28782 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Corbi=C3=A8re?= Date: Tue, 24 Nov 2026 09:25:17 +0100 Subject: [PATCH 05/11] Reshape json --- .project | 28 ++++ .settings/org.eclipse.core.resources.prefs | 2 + .settings/org.eclipse.m2e.core.prefs | 4 + biscuit-core/.classpath | 63 +++++++++ biscuit-core/.project | 34 +++++ .../org.eclipse.core.resources.prefs | 4 + .../.settings/org.eclipse.jdt.apt.core.prefs | 2 + .../.settings/org.eclipse.jdt.core.prefs | 9 ++ .../.settings/org.eclipse.m2e.core.prefs | 4 + .../org/eclipse/biscuit/crypto/Token.java | 2 +- .../java/org/eclipse/biscuit/error/Error.java | 124 +++++++++++++----- .../eclipse/biscuit/error/FailedCheck.java | 4 +- .../org/eclipse/biscuit/error/LogicError.java | 26 ++-- .../token/format/SerializedBiscuit.java | 2 +- biscuit/.classpath | 57 ++++++++ biscuit/.project | 34 +++++ .../org.eclipse.core.resources.prefs | 6 + .../.settings/org.eclipse.jdt.apt.core.prefs | 2 + biscuit/.settings/org.eclipse.jdt.core.prefs | 9 ++ biscuit/.settings/org.eclipse.m2e.core.prefs | 4 + .../bouncycastle/Ed25519PublicKey.java | 2 +- .../bouncycastle/SECP256R1PublicKey.java | 2 +- .../eclipse/biscuit/token/SamplesTest.java | 35 ++--- 23 files changed, 393 insertions(+), 66 deletions(-) create mode 100644 .project create mode 100644 .settings/org.eclipse.core.resources.prefs create mode 100644 .settings/org.eclipse.m2e.core.prefs create mode 100644 biscuit-core/.classpath create mode 100644 biscuit-core/.project create mode 100644 biscuit-core/.settings/org.eclipse.core.resources.prefs create mode 100644 biscuit-core/.settings/org.eclipse.jdt.apt.core.prefs create mode 100644 biscuit-core/.settings/org.eclipse.jdt.core.prefs create mode 100644 biscuit-core/.settings/org.eclipse.m2e.core.prefs create mode 100644 biscuit/.classpath create mode 100644 biscuit/.project create mode 100644 biscuit/.settings/org.eclipse.core.resources.prefs create mode 100644 biscuit/.settings/org.eclipse.jdt.apt.core.prefs create mode 100644 biscuit/.settings/org.eclipse.jdt.core.prefs create mode 100644 biscuit/.settings/org.eclipse.m2e.core.prefs diff --git a/.project b/.project new file mode 100644 index 00000000..c0d28af8 --- /dev/null +++ b/.project @@ -0,0 +1,28 @@ + + + biscuit-java + + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.m2e.core.maven2Nature + + + + 1795780263878 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + + diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 00000000..99f26c02 --- /dev/null +++ b/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/=UTF-8 diff --git a/.settings/org.eclipse.m2e.core.prefs b/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 00000000..f897a7f1 --- /dev/null +++ b/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/biscuit-core/.classpath b/biscuit-core/.classpath new file mode 100644 index 00000000..ba52700e --- /dev/null +++ b/biscuit-core/.classpath @@ -0,0 +1,63 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/biscuit-core/.project b/biscuit-core/.project new file mode 100644 index 00000000..8e7e203f --- /dev/null +++ b/biscuit-core/.project @@ -0,0 +1,34 @@ + + + biscuit-core + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + + + 1795780263877 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + + diff --git a/biscuit-core/.settings/org.eclipse.core.resources.prefs b/biscuit-core/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 00000000..6bca6226 --- /dev/null +++ b/biscuit-core/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,4 @@ +eclipse.preferences.version=1 +encoding//src/main/java=UTF-8 +encoding//target/generated-sources/protobuf=UTF-8 +encoding/=UTF-8 diff --git a/biscuit-core/.settings/org.eclipse.jdt.apt.core.prefs b/biscuit-core/.settings/org.eclipse.jdt.apt.core.prefs new file mode 100644 index 00000000..d4313d4b --- /dev/null +++ b/biscuit-core/.settings/org.eclipse.jdt.apt.core.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.apt.aptEnabled=false diff --git a/biscuit-core/.settings/org.eclipse.jdt.core.prefs b/biscuit-core/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 00000000..8012c4ef --- /dev/null +++ b/biscuit-core/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,9 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=11 +org.eclipse.jdt.core.compiler.compliance=11 +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled +org.eclipse.jdt.core.compiler.problem.forbiddenReference=error +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore +org.eclipse.jdt.core.compiler.processAnnotations=disabled +org.eclipse.jdt.core.compiler.release=enabled +org.eclipse.jdt.core.compiler.source=11 diff --git a/biscuit-core/.settings/org.eclipse.m2e.core.prefs b/biscuit-core/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 00000000..f897a7f1 --- /dev/null +++ b/biscuit-core/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/crypto/Token.java b/biscuit-core/src/main/java/org/eclipse/biscuit/crypto/Token.java index ce08fd68..6484fbe4 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/crypto/Token.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/crypto/Token.java @@ -85,7 +85,7 @@ public Result verify(PublicKey root) } else { return Result.err( new Error.FormatError.Signature.InvalidSignature( - "signature error: Verification equation was not satisfied")); + "signature error")); } } } diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/error/Error.java b/biscuit-core/src/main/java/org/eclipse/biscuit/error/Error.java index f0c8b975..5df3e126 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/error/Error.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/error/Error.java @@ -34,19 +34,20 @@ public Optional> getFailedChecks() { public static final class InternalError extends Error { @Override public JsonNode toJson() { - return TextNode.valueOf("InternalError"); + return objectMapper.createObjectNode().put("type", "internal_error"); } } public abstract static class FormatError extends Error { private static JsonNode jsonWrapper(JsonNode e) { - return objectMapper.createObjectNode().set("Format", e); + return objectMapper.createObjectNode().put("type", "format").set("value", e); } public abstract static class Signature extends FormatError { private static JsonNode jsonWrapper(JsonNode e) { - return FormatError.jsonWrapper(objectMapper.createObjectNode().set("Signature", e)); + return FormatError.jsonWrapper( + objectMapper.createObjectNode().put("type", "signature").set("value", e)); } public static final class InvalidFormat extends Signature { @@ -62,7 +63,8 @@ public boolean equals(Object o) { @Override public JsonNode toJson() { - return Signature.jsonWrapper(TextNode.valueOf("InvalidFormat")); + return Signature.jsonWrapper( + objectMapper.createObjectNode().put("type", "invalid_format")); } @Override @@ -89,7 +91,10 @@ public boolean equals(Object o) { @Override public JsonNode toJson() { return Signature.jsonWrapper( - objectMapper.createObjectNode().put("InvalidSignature", this.err)); + objectMapper + .createObjectNode() + .put("type", "invalid_signature") + .put("value", this.err)); } @Override @@ -110,7 +115,8 @@ public boolean equals(Object o) { @Override public JsonNode toJson() { - return Signature.jsonWrapper(TextNode.valueOf("SealedSignature")); + return Signature.jsonWrapper( + objectMapper.createObjectNode().put("type", "sealed_signature")); } @Override @@ -130,7 +136,7 @@ public boolean equals(Object o) { @Override public JsonNode toJson() { - return Signature.jsonWrapper(TextNode.valueOf("EmptyKeys")); + return Signature.jsonWrapper(objectMapper.createObjectNode().put("type", "empty_keys")); } @Override @@ -150,7 +156,8 @@ public boolean equals(Object o) { @Override public JsonNode toJson() { - return Signature.jsonWrapper(TextNode.valueOf("UnknownPublicKey")); + return Signature.jsonWrapper( + objectMapper.createObjectNode().put("type", "unknown_public_key")); } @Override @@ -191,7 +198,10 @@ public String toString() { @Override public JsonNode toJson() { return FormatError.jsonWrapper( - objectMapper.createObjectNode().put("DeserializationError", this.err)); + objectMapper + .createObjectNode() + .put("type", "deserialization_error") + .put("value", this.err)); } } @@ -290,7 +300,10 @@ public String toString() { @Override public JsonNode toJson() { return FormatError.jsonWrapper( - objectMapper.createObjectNode().put("DeserializationLimitError", this.reason.name())); + objectMapper + .createObjectNode() + .put("type", "deserialization_limit_error") + .put("value", this.reason.name())); } } @@ -326,7 +339,10 @@ public String toString() { @Override public JsonNode toJson() { return FormatError.jsonWrapper( - objectMapper.createObjectNode().put("SerializationError", this.err)); + objectMapper + .createObjectNode() + .put("type", "serialization_error") + .put("value", this.err)); } } @@ -362,7 +378,10 @@ public String toString() { @Override public JsonNode toJson() { return FormatError.jsonWrapper( - objectMapper.createObjectNode().put("BlockDeserializationError", this.err)); + objectMapper + .createObjectNode() + .put("type", "block_deserialization_error") + .put("value", this.err)); } } @@ -402,7 +421,10 @@ public String toString() { @Override public JsonNode toJson() { return FormatError.jsonWrapper( - objectMapper.createObjectNode().put("BlockSignatureDeserializationError", this.err)); + objectMapper + .createObjectNode() + .put("type", "block_signature_deserialization_error") + .put("value", this.err)); } } @@ -438,7 +460,10 @@ public String toString() { @Override public JsonNode toJson() { return FormatError.jsonWrapper( - objectMapper.createObjectNode().put("BlockSerializationError", this.err)); + objectMapper + .createObjectNode() + .put("type", "block_serialization_error") + .put("value", this.err)); } } @@ -498,7 +523,8 @@ public JsonNode toJson() { .put("minimum", this.minimum) .put("maximum", this.maximum) .put("actual", this.actual); - return FormatError.jsonWrapper(objectMapper.createObjectNode().set("Version", child)); + return FormatError.jsonWrapper( + objectMapper.createObjectNode().put("type", "version").set("value", child)); } } @@ -536,7 +562,10 @@ public String toString() { @Override public JsonNode toJson() { return FormatError.jsonWrapper( - objectMapper.createObjectNode().set("InvalidSignatureSize", IntNode.valueOf(size))); + objectMapper + .createObjectNode() + .put("type", "invalid_signature_size") + .set("value", IntNode.valueOf(size))); } } @@ -574,7 +603,10 @@ public String toString() { @Override public JsonNode toJson() { return FormatError.jsonWrapper( - objectMapper.createObjectNode().set("InvalidKeySize", IntNode.valueOf(size))); + objectMapper + .createObjectNode() + .put("type", "invalid_key_size") + .set("value", IntNode.valueOf(size))); } } @@ -610,7 +642,10 @@ public String toString() { @Override public JsonNode toJson() { return FormatError.jsonWrapper( - objectMapper.createObjectNode().set("InvalidKey", TextNode.valueOf(this.err))); + objectMapper + .createObjectNode() + .put("type", "invalid_key") + .set("value", TextNode.valueOf(this.err))); } } } @@ -648,7 +683,10 @@ public String toString() { public JsonNode toJson() { ObjectNode child = objectMapper.createObjectNode().put("index", index); return FormatError.jsonWrapper( - objectMapper.createObjectNode().set("InvalidAuthorityIndex", child)); + objectMapper + .createObjectNode() + .put("type", "invalid_authority_index") + .set("value", child)); } } @@ -688,7 +726,7 @@ public JsonNode toJson() { ObjectNode child = objectMapper.createObjectNode().put("expected", expected).put("found", found); return FormatError.jsonWrapper( - objectMapper.createObjectNode().set("InvalidBlockIndex", child)); + objectMapper.createObjectNode().put("type", "invalid_block_index").set("value", child)); } } @@ -703,7 +741,7 @@ public boolean equals(Object o) { @Override public JsonNode toJson() { - return TextNode.valueOf("SymbolTableOverlap"); + return objectMapper.createObjectNode().put("type", "symbol_table_overlap"); } } @@ -718,7 +756,7 @@ public boolean equals(Object o) { @Override public JsonNode toJson() { - return TextNode.valueOf("MissingSymbols"); + return objectMapper.createObjectNode().put("type", "missing_symbols"); } } @@ -733,7 +771,7 @@ public boolean equals(Object o) { @Override public JsonNode toJson() { - return TextNode.valueOf("Sealed"); + return objectMapper.createObjectNode().put("type", "sealed"); } } @@ -773,7 +811,10 @@ public Optional> getFailedChecks() { @Override public JsonNode toJson() { - return objectMapper.createObjectNode().set("FailedLogic", error.toJson()); + return objectMapper + .createObjectNode() + .put("type", "failed_logic") + .set("value", error.toJson()); } } @@ -794,7 +835,10 @@ public boolean equals(Object o) { @Override public JsonNode toJson() { - return objectMapper.createObjectNode().set("Language", langError.toJson()); + return objectMapper + .createObjectNode() + .put("type", "language") + .set("value", langError.toJson()); } } @@ -809,7 +853,7 @@ public boolean equals(Object o) { @Override public JsonNode toJson() { - return TextNode.valueOf("TooManySteps"); + return objectMapper.createObjectNode().put("type", "too_many_steps"); } } @@ -824,7 +868,7 @@ public boolean equals(Object o) { @Override public JsonNode toJson() { - return TextNode.valueOf("TooManyFacts"); + return objectMapper.createObjectNode().put("type", "too_many_facts"); } } @@ -839,7 +883,7 @@ public boolean equals(Object o) { @Override public JsonNode toJson() { - return TextNode.valueOf("TooManyIterations"); + return objectMapper.createObjectNode().put("type", "too_many_iterations"); } } @@ -854,7 +898,7 @@ public boolean equals(Object o) { @Override public JsonNode toJson() { - return TextNode.valueOf("Timeout"); + return objectMapper.createObjectNode().put("type", "timeout"); } } @@ -897,9 +941,25 @@ public boolean equals(Object o) { return o != null && getClass() == o.getClass(); } + private String kind2String(Kind k) { + switch (k) { + case Execution: + return "execution"; + case Overflow: + return "overflow"; + case InvalidType: + return "invalid_type"; + case ShadowedVariable: + return "shadowed_variable"; + } + return ""; + } + @Override public JsonNode toJson() { - return objectMapper.createObjectNode().put("Execution", kind.toString()); + var kindObj = + objectMapper.createObjectNode().put("type", kind2String(kind)); + return objectMapper.createObjectNode().put("type", "execution").set("value", kindObj); } @Override @@ -919,7 +979,7 @@ public boolean equals(Object o) { @Override public JsonNode toJson() { - return TextNode.valueOf("InvalidType"); + return objectMapper.createObjectNode().put("type", "invalid_type"); } } @@ -934,7 +994,7 @@ public boolean equals(Object o) { @Override public JsonNode toJson() { - return TextNode.valueOf("Shadowing"); + return objectMapper.createObjectNode().put("type", "shadowing"); } } diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/error/FailedCheck.java b/biscuit-core/src/main/java/org/eclipse/biscuit/error/FailedCheck.java index e2775ef4..34c4eab5 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/error/FailedCheck.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/error/FailedCheck.java @@ -68,7 +68,7 @@ public JsonNode toJson() { child.set("block_id", LongNode.valueOf(this.blockId)); child.set("check_id", LongNode.valueOf(this.checkId)); child.set("rule", TextNode.valueOf(this.rule)); - return objectMapper.createObjectNode().set("Block", child); + return objectMapper.createObjectNode().put("type","block").set("value", child); } } @@ -108,7 +108,7 @@ public JsonNode toJson() { ObjectNode child = objectMapper.createObjectNode(); child.set("check_id", LongNode.valueOf(this.checkId)); child.set("rule", TextNode.valueOf(this.rule)); - return objectMapper.createObjectNode().set("Authorizer", child); + return objectMapper.createObjectNode().put("type","authorizer").set("value", child); } } diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/error/LogicError.java b/biscuit-core/src/main/java/org/eclipse/biscuit/error/LogicError.java index c876985a..35cabb3e 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/error/LogicError.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/error/LogicError.java @@ -54,7 +54,7 @@ public String toString() { @Override public JsonNode toJson() { - return TextNode.valueOf("InvalidAuthorityFact"); + return objectMapper.createObjectNode().put("type", "invalid_authority_fact"); } } @@ -89,8 +89,12 @@ public String toString() { @Override public JsonNode toJson() { - ObjectNode child = objectMapper.createObjectNode().put("error", this.err); - return objectMapper.createObjectNode().set("InvalidAmbientFact", child); + ObjectNode child = + objectMapper.createObjectNode().put("type", "error").put("value", this.err); + return objectMapper + .createObjectNode() + .put("type", "invalid_ambient_fact") + .set("value", child); } } @@ -128,7 +132,7 @@ public String toString() { @Override public JsonNode toJson() { ObjectNode child = objectMapper.createObjectNode().put("id", id).put("error", err); - return objectMapper.createObjectNode().set("InvalidBlockFact", child); + return objectMapper.createObjectNode().put("type", "invalid_block_fact").set("value", child); } } @@ -168,7 +172,7 @@ public JsonNode toJson() { ArrayNode child = objectMapper.createArrayNode(); child.add(id); child.add(err); - return objectMapper.createObjectNode().set("InvalidBlockRule", child); + return objectMapper.createObjectNode().put("type","invalid_block_rule").set("value", child); } } @@ -208,7 +212,7 @@ public JsonNode toJson() { ArrayNode child = objectMapper.createArrayNode(); child.add(id); child.add(err); - return objectMapper.createObjectNode().set("InvalidBlockScope", child); + return objectMapper.createObjectNode().put("type", "invalid_block_scope").set("value", child); } } @@ -265,7 +269,7 @@ public JsonNode toJson() { ObjectNode unauthorized = objectMapper.createObjectNode(); unauthorized.set("policy", policy.toJson()); unauthorized.set("checks", checks); - return objectMapper.createObjectNode().set("Unauthorized", unauthorized); + return objectMapper.createObjectNode().put("type", "unauthorized").set("value", unauthorized); } } @@ -318,7 +322,7 @@ public JsonNode toJson() { for (FailedCheck t : this.errors) { errors.add(t.toJson()); } - return objectMapper.createObjectNode().set("NoMatchingPolicy", errors); + return objectMapper.createObjectNode().put("type", "no_matching_policy").set("value", errors); } } @@ -328,7 +332,7 @@ public AuthorizerNotEmpty() {} @Override public JsonNode toJson() { - return TextNode.valueOf("AuthorizerNotEmpty"); + return objectMapper.createObjectNode().put("type", "authorizer_not_empty"); } @Override @@ -354,7 +358,7 @@ public String toString() { @Override public JsonNode toJson() { - return objectMapper.createObjectNode().put("Allow", nb); + return objectMapper.createObjectNode().put("type", "allow").put("value", nb); } } @@ -372,7 +376,7 @@ public String toString() { @Override public JsonNode toJson() { - return objectMapper.createObjectNode().put("Deny", nb); + return objectMapper.createObjectNode().put("type", "deny").put("value", nb); } } } diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/token/format/SerializedBiscuit.java b/biscuit-core/src/main/java/org/eclipse/biscuit/token/format/SerializedBiscuit.java index 29dd94e9..a2d05466 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/token/format/SerializedBiscuit.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/token/format/SerializedBiscuit.java @@ -435,7 +435,7 @@ public Result verify(org.eclipse.biscuit.crypto.PublicKey root) } else { return Result.err( new Error.FormatError.Signature.InvalidSignature( - "signature error: Verification equation was not satisfied")); + "signature error")); } } else { byte[] finalSignature = this.proof.getSignature().get(); diff --git a/biscuit/.classpath b/biscuit/.classpath new file mode 100644 index 00000000..8c458320 --- /dev/null +++ b/biscuit/.classpath @@ -0,0 +1,57 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/biscuit/.project b/biscuit/.project new file mode 100644 index 00000000..28979c73 --- /dev/null +++ b/biscuit/.project @@ -0,0 +1,34 @@ + + + biscuit + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + + + 1795780263876 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + + diff --git a/biscuit/.settings/org.eclipse.core.resources.prefs b/biscuit/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 00000000..29abf999 --- /dev/null +++ b/biscuit/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,6 @@ +eclipse.preferences.version=1 +encoding//src/main/java=UTF-8 +encoding//src/main/resources=UTF-8 +encoding//src/test/java=UTF-8 +encoding//src/test/resources=UTF-8 +encoding/=UTF-8 diff --git a/biscuit/.settings/org.eclipse.jdt.apt.core.prefs b/biscuit/.settings/org.eclipse.jdt.apt.core.prefs new file mode 100644 index 00000000..d4313d4b --- /dev/null +++ b/biscuit/.settings/org.eclipse.jdt.apt.core.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.apt.aptEnabled=false diff --git a/biscuit/.settings/org.eclipse.jdt.core.prefs b/biscuit/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 00000000..8012c4ef --- /dev/null +++ b/biscuit/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,9 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=11 +org.eclipse.jdt.core.compiler.compliance=11 +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled +org.eclipse.jdt.core.compiler.problem.forbiddenReference=error +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore +org.eclipse.jdt.core.compiler.processAnnotations=disabled +org.eclipse.jdt.core.compiler.release=enabled +org.eclipse.jdt.core.compiler.source=11 diff --git a/biscuit/.settings/org.eclipse.m2e.core.prefs b/biscuit/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 00000000..f897a7f1 --- /dev/null +++ b/biscuit/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/biscuit/src/main/java/org/eclipse/biscuit/bouncycastle/Ed25519PublicKey.java b/biscuit/src/main/java/org/eclipse/biscuit/bouncycastle/Ed25519PublicKey.java index 965489c3..f3d64af4 100644 --- a/biscuit/src/main/java/org/eclipse/biscuit/bouncycastle/Ed25519PublicKey.java +++ b/biscuit/src/main/java/org/eclipse/biscuit/bouncycastle/Ed25519PublicKey.java @@ -76,7 +76,7 @@ public Optional verify(byte[] data, byte[] signature) { if (!sgr.verifySignature(signature)) { return Optional.of( new Error.FormatError.Signature.InvalidSignature( - "signature error: Verification equation was not satisfied")); + "signature error")); } return Optional.empty(); diff --git a/biscuit/src/main/java/org/eclipse/biscuit/bouncycastle/SECP256R1PublicKey.java b/biscuit/src/main/java/org/eclipse/biscuit/bouncycastle/SECP256R1PublicKey.java index b11aa451..e3edda0e 100644 --- a/biscuit/src/main/java/org/eclipse/biscuit/bouncycastle/SECP256R1PublicKey.java +++ b/biscuit/src/main/java/org/eclipse/biscuit/bouncycastle/SECP256R1PublicKey.java @@ -117,7 +117,7 @@ public Optional verify(byte[] data, byte[] signature) { if (!signer.verifySignature(hash, sig[0], sig[1])) { return Optional.of( new Error.FormatError.Signature.InvalidSignature( - "signature error: Verification equation was not satisfied")); + "signature error")); } return Optional.empty(); diff --git a/biscuit/src/test/java/org/eclipse/biscuit/token/SamplesTest.java b/biscuit/src/test/java/org/eclipse/biscuit/token/SamplesTest.java index 1df19a6c..2b06502f 100644 --- a/biscuit/src/test/java/org/eclipse/biscuit/token/SamplesTest.java +++ b/biscuit/src/test/java/org/eclipse/biscuit/token/SamplesTest.java @@ -201,9 +201,9 @@ DynamicTest processTestcase( List revocationIds = token.revocationIdentifiers(); ArrayNode validationRevocationIds = (ArrayNode) validation.get("revocation_ids"); - assertEquals(revocationIds.size(), validationRevocationIds.size()); + assertEquals(revocationIds.size(), validationRevocationIds.size(),"revocation_ids size"); for (int i = 0; i < revocationIds.size(); i++) { - assertEquals(validationRevocationIds.get(i).asText(), revocationIds.get(i).toHex()); + assertEquals(validationRevocationIds.get(i).asText(), revocationIds.get(i).toHex(),"revocation_ids"); } // TODO Add check of the token @@ -237,11 +237,11 @@ DynamicTest processTestcase( new ObjectMapper().treeToValue(validation.get("world"), World.class); World authorizerWorld = new World(authorizer); - assertEquals(world.factMap(), authorizerWorld.factMap()); - assertEquals(world.rules, authorizerWorld.rules); - assertEquals(world.checks, authorizerWorld.checks); - assertEquals(world.policies, authorizerWorld.policies); - assertEquals(world.elapsedSteps, authorizerWorld.elapsedSteps); + assertEquals(world.factMap(), authorizerWorld.factMap(), "facts"); + assertEquals(world.rules, authorizerWorld.rules, "rules"); + assertEquals(world.checks, authorizerWorld.checks, "checks"); + assertEquals(world.policies, authorizerWorld.policies, "policies"); + assertEquals(world.elapsedSteps, authorizerWorld.elapsedSteps, "elapsed_steps"); } res = Result.ok(authorizeResult); @@ -252,11 +252,11 @@ DynamicTest processTestcase( new ObjectMapper().treeToValue(validation.get("world"), World.class); World authorizerWorld = new World(authorizer); - assertEquals(world.factMap(), authorizerWorld.factMap()); - assertEquals(world.rules, authorizerWorld.rules); - assertEquals(world.checks, authorizerWorld.checks); - assertEquals(world.policies, authorizerWorld.policies); - assertEquals(world.elapsedSteps, authorizerWorld.elapsedSteps); + assertEquals(world.factMap(), authorizerWorld.factMap(), "facts"); + assertEquals(world.rules, authorizerWorld.rules, "rules"); + assertEquals(world.checks, authorizerWorld.checks, "checks"); + assertEquals(world.policies, authorizerWorld.policies, "policies"); + assertEquals(world.elapsedSteps, authorizerWorld.elapsedSteps, "elapsed_steps"); } throw e; @@ -271,22 +271,22 @@ DynamicTest processTestcase( "validation '" + validationName + "' expected result Ok(" - + expectedResult.get("Ok").asLong() + + expectedResult.get("value").asLong() + "), got error"); throw res.getErr(); } else { - assertEquals(expectedResult.get("Ok").asLong(), res.getOk()); + assertEquals(expectedResult.get("value").asLong(), res.getOk()); } } else { if (res.isErr()) { - if (res.getErr() instanceof Error) { + if (res.getErr() instanceof Error && "err".equals(expectedResult.get("type").textValue())) { Error e = (Error) res.getErr(); System.out.println("validation '" + validationName + "' got error: " + e); // Serialize and deserialize the error to ensure the jackson node types match. var objectMapper = new ObjectMapper(); var result = objectMapper.readTree(objectMapper.writeValueAsString(e.toJson())); - assertEquals(expectedResult.get("Err"), result); + assertEquals(expectedResult.get("value"), result); } else { throw res.getErr(); } @@ -295,7 +295,7 @@ DynamicTest processTestcase( "validation '" + validationName + "' expected result error(" - + expectedResult.get("Err") + + expectedResult.get("value") + "), got success: " + res.getOk()); } @@ -453,6 +453,7 @@ public World(Authorizer authorizer) { .collect(Collectors.toList()); this.policies = authorizer.getPolicies().stream().map(p -> p.toString()).collect(Collectors.toList()); + this.elapsedSteps = authorizer.elapsedSteps(); Collections.sort(this.rules); Collections.sort(this.checks); } From e58de1068af00e46b3e15cf0a092c7106a15848c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Corbi=C3=A8re?= Date: Wed, 25 Nov 2026 09:22:36 +0100 Subject: [PATCH 06/11] Add missing UnknownSymbol error --- .../biscuit/datalog/expressions/Op.java | 2 +- .../java/org/eclipse/biscuit/error/Error.java | 39 +++++++++++++++++++ .../biscuit/token/DeserializationLimit.java | 9 +++-- 3 files changed, 45 insertions(+), 5 deletions(-) diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/expressions/Op.java b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/expressions/Op.java index 9392e616..597c42e6 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/expressions/Op.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/expressions/Op.java @@ -85,7 +85,7 @@ private static Error.FormatError checkExternalFunction( ? symbols.getSymbol((int) nameIndex) : Optional.empty(); if (name.isEmpty()) { - return new Error.FormatError.DeserializationError("unknown symbol " + nameIndex); + return new Error.FormatError.UnknownSymbol(nameIndex); } if (!allowedFunctions.contains(name.get())) { return new Error.FormatError.DeserializationLimitError( diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/error/Error.java b/biscuit-core/src/main/java/org/eclipse/biscuit/error/Error.java index 5df3e126..a0fa0350 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/error/Error.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/error/Error.java @@ -166,6 +166,45 @@ public String toString() { } } + public static final class UnknownSymbol extends FormatError { + private final long symbol; + + public UnknownSymbol(long symbol) { + this.symbol = symbol; + } + + public long getSymbol() { + return symbol; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + return symbol == ((UnknownSymbol) o).symbol; + } + + @Override + public int hashCode() { + return Objects.hash(symbol); + } + + @Override + public JsonNode toJson() { + return FormatError.jsonWrapper( + objectMapper.createObjectNode().put("type", "unknown_symbol").put("value", symbol)); + } + + @Override + public String toString() { + return "Err(Format(UnknownSymbol(" + symbol + ")))"; + } + } + public static final class DeserializationError extends FormatError { private final String err; diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/token/DeserializationLimit.java b/biscuit-core/src/main/java/org/eclipse/biscuit/token/DeserializationLimit.java index ac49f181..4d918818 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/token/DeserializationLimit.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/token/DeserializationLimit.java @@ -383,15 +383,16 @@ private static Error.FormatError checkLength( long symbolIndex, int maxLength, Error.FormatError.DeserializationLimitError.Reason reason) { - if (maxLength == Integer.MAX_VALUE) { - return null; - } + // a symbol must always be known when it is referenced, whatever the limits are Optional symbol = symbolIndex >= 0 && symbolIndex <= Integer.MAX_VALUE ? symbols.getSymbol((int) symbolIndex) : Optional.empty(); if (symbol.isEmpty()) { - return new Error.FormatError.DeserializationError("unknown symbol " + symbolIndex); + return new Error.FormatError.UnknownSymbol(symbolIndex); + } + if (maxLength == Integer.MAX_VALUE) { + return null; } if (symbol.get().getBytes(StandardCharsets.UTF_8).length > maxLength) { return new Error.FormatError.DeserializationLimitError(reason); From 5812f0d18ec3983d77200ab400ccafae19b7d526 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Corbi=C3=A8re?= Date: Wed, 25 Nov 2026 09:22:36 +0100 Subject: [PATCH 07/11] Load SymbolTable --- .../java/org/eclipse/biscuit/token/SamplesTest.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/biscuit/src/test/java/org/eclipse/biscuit/token/SamplesTest.java b/biscuit/src/test/java/org/eclipse/biscuit/token/SamplesTest.java index 2b06502f..ca77ebfd 100644 --- a/biscuit/src/test/java/org/eclipse/biscuit/token/SamplesTest.java +++ b/biscuit/src/test/java/org/eclipse/biscuit/token/SamplesTest.java @@ -193,8 +193,19 @@ DynamicTest processTestcase( org.eclipse.biscuit.token.Block block = token.blocks.get(i); SignedBlock signedBlock = token.serializedBiscuit.getBlocks().get(i); byte[] serBlock = block.toBytes().getOk(); + // symbols must be known when a block is deserialized: blocks without an external + // key share the token's symbol table, the others have their own + SymbolTable baseSymbols = + block.getExternalKey().isPresent() + ? new SymbolTable() + : new SymbolTable(token.symbolTable); org.eclipse.biscuit.token.Block deserBlock = - fromBytes(serBlock, block.getExternalKey()).getOk(); + fromBytes( + serBlock, + block.getExternalKey(), + DeserializationLimit.PerBlock.allowEverything(), + baseSymbols) + .getOk(); assertEquals(block.getVersion(), deserBlock.getVersion()); assertEquals(block.print(token.symbolTable), deserBlock.print(token.symbolTable)); } From 59420a51a12c506bc9979fa3274a1ffa6cdc405e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Corbi=C3=A8re?= Date: Wed, 25 Nov 2026 23:44:42 +0100 Subject: [PATCH 08/11] Closure Steps Steps count even if shadowed --- .../main/java/org/eclipse/biscuit/datalog/expressions/Op.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/expressions/Op.java b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/expressions/Op.java index 597c42e6..0f5ffc81 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/expressions/Op.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/expressions/Op.java @@ -1204,8 +1204,6 @@ Term call( TemporarySymbolTable temporarySymbolTable, StepBudget steps) throws Error { - steps.step(); - if (arity() != 1) { throw new Error.Execution("called closure with arity " + arity() + "with 1 argument"); } @@ -1213,6 +1211,7 @@ Term call( throw new Error.Execution( Error.Execution.Kind.ShadowedVariable, "closure parameter shadows variable"); } + steps.step(); Deque stack = new ArrayDeque<>(16); // Default value for (Op op : ops) { op.evaluate(stack, variables, temporarySymbolTable, steps); From 1d2608993583262e66873ceadf76b97a16699b4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Corbi=C3=A8re?= Date: Wed, 25 Nov 2026 23:44:42 +0100 Subject: [PATCH 09/11] Closure Steps part 2 Steps count even if shadowed --- .../main/java/org/eclipse/biscuit/datalog/expressions/Op.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/expressions/Op.java b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/expressions/Op.java index 0f5ffc81..e78f0da7 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/expressions/Op.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/expressions/Op.java @@ -1183,11 +1183,10 @@ int arity() { Term call( Map variables, TemporarySymbolTable temporarySymbolTable, StepBudget steps) throws Error { - steps.step(); - if (arity() != 0) { throw new Error.Execution("called closure with arity " + arity() + "with no arguments"); } + steps.step(); Deque stack = new ArrayDeque<>(16); // Default value for (Op op : ops) { op.evaluate(stack, variables, temporarySymbolTable, steps); From ba7f034df3d1727ae8078ea1f4fbccef1df4eff3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Corbi=C3=A8re?= Date: Wed, 25 Nov 2026 13:33:43 +0100 Subject: [PATCH 10/11] Switch to TreeMap and TreeSet to fix SampleTest --- .../org/eclipse/biscuit/crypto/Token.java | 4 +- .../eclipse/biscuit/datalog/Combinator.java | 4 +- .../org/eclipse/biscuit/datalog/Fact.java | 4 + .../org/eclipse/biscuit/datalog/FactSet.java | 56 +++--- .../org/eclipse/biscuit/datalog/MapKey.java | 28 ++- .../org/eclipse/biscuit/datalog/Origin.java | 41 +++- .../org/eclipse/biscuit/datalog/Pair.java | 14 ++ .../eclipse/biscuit/datalog/Predicate.java | 6 + .../org/eclipse/biscuit/datalog/Rule.java | 4 +- .../org/eclipse/biscuit/datalog/Term.java | 187 +++++++++++++++--- .../biscuit/datalog/expressions/Op.java | 29 +-- .../java/org/eclipse/biscuit/error/Error.java | 3 +- .../eclipse/biscuit/error/FailedCheck.java | 4 +- .../org/eclipse/biscuit/error/LogicError.java | 3 +- .../biscuit/token/DeserializationLimit.java | 30 +-- .../eclipse/biscuit/token/builder/MapKey.java | 22 +++ .../eclipse/biscuit/token/builder/Term.java | 147 +++++++++++++- .../eclipse/biscuit/token/builder/Utils.java | 44 ++++- .../biscuit/token/builder/parser/Parser.java | 12 +- .../token/format/SerializedBiscuit.java | 4 +- .../bouncycastle/Ed25519PublicKey.java | 4 +- .../bouncycastle/SECP256R1PublicKey.java | 4 +- .../eclipse/biscuit/builder/BuilderTest.java | 18 +- .../biscuit/builder/parser/ParserTest.java | 29 +-- .../biscuit/datalog/ExpressionTest.java | 26 +-- .../biscuit/datalog/TermComparatorTest.java | 154 +++++++++++++++ .../eclipse/biscuit/datalog/WorldTest.java | 54 +++-- .../token/DeserializationLimitsTest.java | 4 +- .../eclipse/biscuit/token/SamplesTest.java | 19 +- 29 files changed, 767 insertions(+), 191 deletions(-) create mode 100644 biscuit/src/test/java/org/eclipse/biscuit/datalog/TermComparatorTest.java diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/crypto/Token.java b/biscuit-core/src/main/java/org/eclipse/biscuit/crypto/Token.java index 6484fbe4..3b7236ea 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/crypto/Token.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/crypto/Token.java @@ -83,9 +83,7 @@ public Result verify(PublicKey root) if (this.next.getPublicKey().equals(currentKey)) { return Result.ok(null); } else { - return Result.err( - new Error.FormatError.Signature.InvalidSignature( - "signature error")); + return Result.err(new Error.FormatError.Signature.InvalidSignature("signature error")); } } } diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Combinator.java b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Combinator.java index 9c360540..74ae76d5 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Combinator.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Combinator.java @@ -5,13 +5,13 @@ package org.eclipse.biscuit.datalog; -import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.NoSuchElementException; import java.util.Optional; import java.util.Set; +import java.util.TreeSet; import org.eclipse.biscuit.error.Error; import org.eclipse.biscuit.error.Result; @@ -62,7 +62,7 @@ public Optional>, Error>> getNext() { // we should return a value, but only once. To prevent further // successful calls, we create a set of variables that cannot // possibly be completed, so the next call will fail - Set set = new HashSet<>(); + Set set = new TreeSet<>(); set.add((long) 0); this.variables = new MatchedVariables(set); diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Fact.java b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Fact.java index be5478fc..aabfd053 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Fact.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Fact.java @@ -6,6 +6,7 @@ package org.eclipse.biscuit.datalog; import biscuit.format.schema.Schema; +import java.util.Comparator; import java.util.List; import java.util.Objects; import org.eclipse.biscuit.error.Error; @@ -57,6 +58,9 @@ public Schema.Fact serialize() { return Schema.Fact.newBuilder().setPredicate(this.predicate.serialize()).build(); } + public static Comparator comparator = + Comparator.comparing(Fact::predicate, Predicate.comparator); + public static Result deserialize( Schema.Fact fact, DeserializationLimit.PerBlock limits, SymbolTable symbols) { var res = Predicate.deserialize(fact.getPredicate(), limits, symbols); diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/FactSet.java b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/FactSet.java index 6678e5f5..f1e858f6 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/FactSet.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/FactSet.java @@ -6,31 +6,36 @@ package org.eclipse.biscuit.datalog; import java.util.ArrayList; -import java.util.HashMap; -import java.util.HashSet; +import java.util.Comparator; import java.util.List; import java.util.Map; +import java.util.SortedMap; +import java.util.SortedSet; +import java.util.TreeMap; +import java.util.TreeSet; import java.util.stream.Stream; public final class FactSet { /** facts grouped by (predicate name, arity), then by origin */ - private final HashMap, HashMap>> facts; + private final SortedMap, SortedMap>> facts; + + private static final Comparator> KEY_COMPARATOR = Pair.naturalComparator(); public FactSet() { - facts = new HashMap<>(); + facts = new TreeMap<>(KEY_COMPARATOR); } - public FactSet(Origin o, HashSet factSet) { - facts = new HashMap<>(); + public FactSet(Origin o, SortedSet factSet) { + facts = new TreeMap<>(KEY_COMPARATOR); for (Fact fact : factSet) { add(o, fact); } } public Map> byOrigin() { - Map> origins = new HashMap<>(); - for (HashMap> group : facts.values()) { - for (Map.Entry> entry : group.entrySet()) { + Map> origins = new TreeMap<>(Origin.comparator); + for (SortedMap> group : facts.values()) { + for (Map.Entry> entry : group.entrySet()) { origins.computeIfAbsent(entry.getKey(), k -> new ArrayList<>()).addAll(entry.getValue()); } } @@ -43,14 +48,15 @@ private static Pair keyOf(Predicate predicate) { public void add(Origin origin, Fact fact) { Pair key = keyOf(fact.predicate()); - HashMap> group = facts.computeIfAbsent(key, k -> new HashMap<>()); - group.computeIfAbsent(origin, k -> new HashSet<>()).add(fact); + SortedMap> group = + facts.computeIfAbsent(key, k -> new TreeMap<>(Origin.comparator)); + group.computeIfAbsent(origin, k -> new TreeSet<>(Fact.comparator)).add(fact); } public int size() { int size = 0; - for (HashMap> group : facts.values()) { - for (HashSet h : group.values()) { + for (SortedMap> group : facts.values()) { + for (SortedSet h : group.values()) { size += h.size(); } } @@ -61,11 +67,11 @@ public int size() { public FactSet clone() { FactSet newFacts = new FactSet(); - for (Map.Entry, HashMap>> group : + for (Map.Entry, SortedMap>> group : this.facts.entrySet()) { - HashMap> newGroup = new HashMap<>(); - for (Map.Entry> entry : group.getValue().entrySet()) { - newGroup.put(entry.getKey(), new HashSet<>(entry.getValue())); + SortedMap> newGroup = new TreeMap<>(Origin.comparator); + for (Map.Entry> entry : group.getValue().entrySet()) { + newGroup.put(entry.getKey(), new TreeSet<>(entry.getValue())); } newFacts.facts.put(group.getKey(), newGroup); } @@ -74,19 +80,21 @@ public FactSet clone() { } public void merge(FactSet other) { - for (Map.Entry, HashMap>> group : + for (Map.Entry, SortedMap>> group : other.facts.entrySet()) { - HashMap> ownGroup = - facts.computeIfAbsent(group.getKey(), k -> new HashMap<>()); - for (Map.Entry> entry : group.getValue().entrySet()) { - ownGroup.computeIfAbsent(entry.getKey(), k -> new HashSet<>()).addAll(entry.getValue()); + SortedMap> ownGroup = + facts.computeIfAbsent(group.getKey(), k -> new TreeMap<>(Origin.comparator)); + for (Map.Entry> entry : group.getValue().entrySet()) { + ownGroup + .computeIfAbsent(entry.getKey(), k -> new TreeSet<>(Fact.comparator)) + .addAll(entry.getValue()); } } } /** trusted facts sharing the predicate's name and arity, the terms are not checked */ public Stream> stream(Predicate predicate, TrustedOrigins blockIds) { - HashMap> group = facts.get(keyOf(predicate)); + SortedMap> group = facts.get(keyOf(predicate)); if (group == null) { return Stream.empty(); } @@ -98,7 +106,7 @@ public Stream> stream(Predicate predicate, TrustedOrigins blo public Stream stream() { return facts.values().stream() .flatMap(group -> group.values().stream()) - .flatMap(HashSet::stream); + .flatMap(SortedSet::stream); } @Override diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/MapKey.java b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/MapKey.java index 86253fea..1fc75ee9 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/MapKey.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/MapKey.java @@ -1,6 +1,7 @@ package org.eclipse.biscuit.datalog; import biscuit.format.schema.Schema; +import java.util.Comparator; import org.eclipse.biscuit.error.Error; import org.eclipse.biscuit.error.Result; import org.eclipse.biscuit.token.DeserializationLimit; @@ -10,6 +11,8 @@ public abstract class MapKey extends Term { public abstract org.eclipse.biscuit.token.builder.MapKey toMapKey(SymbolTable symbolTable); + public abstract MapKeyType getMapKeyType(); + public static Result deserializeMapKeyEnum( Schema.MapKey mapKey, DeserializationLimit.PerBlock limits, SymbolTable symbols) { var allowedTypes = limits.getAllowedMapKeyTypes(); @@ -34,12 +37,31 @@ public static Result deserializeMapKeyEnum( } } - private static DeserializationLimit.MapKeyType mapKeyType(Schema.MapKey.ContentCase content) { + public static Comparator comparator = + new Comparator() { + @Override + public int compare(MapKey m1, MapKey m2) { + int c = m1.getMapKeyType().compareTo(m2.getMapKeyType()); + if (c != 0) { + return c; + } + switch (m1.getMapKeyType()) { + case INTEGER: + return Long.compare(((Term.Integer) m1).value(), ((Term.Integer) m2).value()); + case STRING: + return Long.compare(((Term.Str) m1).value(), ((Term.Str) m2).value()); + default: + throw new UnknownError("Unexpected MapKey " + m1 + m2); + } + } + }; + + private static MapKeyType mapKeyType(Schema.MapKey.ContentCase content) { switch (content) { case INTEGER: - return DeserializationLimit.MapKeyType.INTEGER; + return MapKeyType.INTEGER; case STRING: - return DeserializationLimit.MapKeyType.STRING; + return MapKeyType.STRING; case CONTENT_NOT_SET: return null; default: diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Origin.java b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Origin.java index c8fd89f8..679286b5 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Origin.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Origin.java @@ -7,24 +7,25 @@ import java.util.Collection; import java.util.Collections; -import java.util.HashSet; +import java.util.Comparator; import java.util.Objects; import java.util.Set; +import java.util.TreeSet; public final class Origin { - private final HashSet blockIds; + private final TreeSet blockIds; public Origin() { - this.blockIds = new HashSet<>(); + this.blockIds = new TreeSet<>(); } public Origin(Long i) { - this.blockIds = new HashSet<>(); + this.blockIds = new TreeSet<>(); this.blockIds.add(i); } public Origin(int i) { - this.blockIds = new HashSet<>(); + this.blockIds = new TreeSet<>(); this.blockIds.add((long) i); } @@ -75,6 +76,36 @@ public boolean equals(Object o) { return Objects.equals(blockIds, origin.blockIds); } + public static Comparator comparator = + new Comparator() { + @Override + public int compare(Origin o1, Origin o2) { + if (o1 == null && o2 == null) { + return 0; + } else if (o1 == null) { + return -1; + } else if (o2 == null) { + return 1; + } else { + var o1it = o1.blockIds.iterator(); + var o2it = o2.blockIds.iterator(); + var c = 0; + while (c == 0) { + if (o1it.hasNext() && o2it.hasNext()) { + c = Long.compare(o1it.next(), o2it.next()); + } else if (o1it.hasNext()) { + c = 1; + } else if (o2it.hasNext()) { + c = -1; + } else { + return c; + } + } + return c; + } + } + }; + @Override public int hashCode() { return blockIds != null ? blockIds.hashCode() : 0; diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Pair.java b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Pair.java index 26bb8741..3054458b 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Pair.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Pair.java @@ -5,6 +5,7 @@ package org.eclipse.biscuit.datalog; +import java.util.Comparator; import java.util.Objects; public final class Pair { @@ -37,4 +38,17 @@ public int hashCode() { public String toString() { return "(" + this._1 + ", " + this._2 + ")"; } + + /** Lexicographic order: by the first element with {@code ct1}, then by the second with {@code ct2}. */ + public static Comparator> comparator( + Comparator ct1, Comparator ct2) { + return Comparator.comparing((Pair p) -> p._1, ct1) + .thenComparing((Pair p) -> p._2, ct2); + } + + /** Lexicographic order using the natural order of both elements. */ + public static , U2 extends Comparable> + Comparator> naturalComparator() { + return comparator(Comparator.naturalOrder(), Comparator.naturalOrder()); + } } diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Predicate.java b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Predicate.java index 90d47fe4..8c393935 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Predicate.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Predicate.java @@ -7,6 +7,7 @@ import biscuit.format.schema.Schema; import java.util.ArrayList; +import java.util.Comparator; import java.util.List; import java.util.ListIterator; import java.util.Objects; @@ -14,11 +15,16 @@ import org.eclipse.biscuit.error.Error; import org.eclipse.biscuit.error.Result; import org.eclipse.biscuit.token.DeserializationLimit; +import org.eclipse.biscuit.token.builder.Utils; public final class Predicate { private final long name; private final List terms; + public static Comparator comparator = + Comparator.comparing(Predicate::name) + .thenComparing(Predicate::terms, Utils.listComparator(Term.comparator)); + public long name() { return this.name; } diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Rule.java b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Rule.java index bb4302a9..f3b5b50c 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Rule.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Rule.java @@ -7,13 +7,13 @@ import biscuit.format.schema.Schema; import java.util.ArrayList; -import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.Set; import java.util.Spliterator; import java.util.Spliterators; +import java.util.TreeSet; import java.util.stream.Collectors; import java.util.stream.Stream; import java.util.stream.StreamSupport; @@ -105,7 +105,7 @@ public Stream, Error>> apply( } private MatchedVariables variablesSet() { - final Set variablesSet = new HashSet<>(); + final Set variablesSet = new TreeSet<>(); for (final Predicate pred : this.body) { variablesSet.addAll( diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Term.java b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Term.java index c96da1e2..2df7c7e6 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Term.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Term.java @@ -10,16 +10,37 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; +import java.util.Comparator; import java.util.Deque; -import java.util.HashMap; -import java.util.HashSet; import java.util.List; +import java.util.SortedMap; +import java.util.SortedSet; +import java.util.TreeMap; +import java.util.TreeSet; import org.eclipse.biscuit.datalog.expressions.Op; import org.eclipse.biscuit.error.Error; import org.eclipse.biscuit.error.Result; import org.eclipse.biscuit.token.DeserializationLimit; +import org.eclipse.biscuit.token.builder.Utils; public abstract class Term extends Op { + public enum TermType { + VARIABLE, + INTEGER, + STRING, + DATE, + BYTES, + BOOL, + SET, + NULL, + ARRAY, + MAP + } + + public enum MapKeyType { + INTEGER, + STRING + } public abstract boolean match(Term other); @@ -27,6 +48,8 @@ public abstract class Term extends Op { public abstract String typeOf(); + public abstract TermType getTermType(); + public abstract org.eclipse.biscuit.token.builder.Term toTerm(SymbolTable symbolTable); @Override @@ -114,26 +137,83 @@ public static Result deserializeEnum( } } - private static DeserializationLimit.TermType termType(Schema.Term.ContentCase content) { + /** + * Total order on terms: first by {@link TermType} (in declaration order), then by value. Strings + * are compared by symbol index, as in {@link MapKey#comparator}. + */ + public static Comparator comparator = + new Comparator() { + @Override + public int compare(Term o1, Term o2) { + int c = o1.getTermType().compareTo(o2.getTermType()); + if (c != 0) { + return c; + } + switch (o1.getTermType()) { + case VARIABLE: + return Long.compare(((Variable) o1).value(), ((Variable) o2).value()); + case INTEGER: + return Long.compare(((Integer) o1).value(), ((Integer) o2).value()); + case STRING: + return Long.compare(((Str) o1).value(), ((Str) o2).value()); + case DATE: + return Long.compare(((Date) o1).value(), ((Date) o2).value()); + case BYTES: + return Arrays.compareUnsigned(((Bytes) o1).value(), ((Bytes) o2).value()); + case BOOL: + return Boolean.compare(((Bool) o1).value(), ((Bool) o2).value()); + case NULL: + return 0; + case SET: + return Utils.iteratorComparator(Term.comparator) + .compare(((Set) o1).value().iterator(), ((Set) o2).value().iterator()); + case ARRAY: + return Utils.iteratorComparator(Term.comparator) + .compare(((Array) o1).value().iterator(), ((Array) o2).value().iterator()); + case MAP: + return Utils.iteratorComparator(MAP_ENTRY_COMPARATOR) + .compare( + ((Map) o1).value().entrySet().iterator(), + ((Map) o2).value().entrySet().iterator()); + default: + throw new IllegalStateException("unhandled term type " + o1.getTermType()); + } + } + }; + + private static final Comparator> MAP_ENTRY_COMPARATOR = + new Comparator>() { + @Override + public int compare( + java.util.Map.Entry e1, java.util.Map.Entry e2) { + int c = MapKey.comparator.compare(e1.getKey(), e2.getKey()); + if (c != 0) { + return c; + } + return Term.comparator.compare(e1.getValue(), e2.getValue()); + } + }; + + private static TermType termType(Schema.Term.ContentCase content) { switch (content) { case INTEGER: - return DeserializationLimit.TermType.INTEGER; + return TermType.INTEGER; case STRING: - return DeserializationLimit.TermType.STRING; + return TermType.STRING; case DATE: - return DeserializationLimit.TermType.DATE; + return TermType.DATE; case BYTES: - return DeserializationLimit.TermType.BYTES; + return TermType.BYTES; case BOOL: - return DeserializationLimit.TermType.BOOL; + return TermType.BOOL; case SET: - return DeserializationLimit.TermType.SET; + return TermType.SET; case NULL: - return DeserializationLimit.TermType.NULL; + return TermType.NULL; case ARRAY: - return DeserializationLimit.TermType.ARRAY; + return TermType.ARRAY; case MAP: - return DeserializationLimit.TermType.MAP; + return TermType.MAP; case VARIABLE: case CONTENT_NOT_SET: // not values, so they have no type @@ -180,6 +260,11 @@ public String typeOf() { return "null"; } + @Override + public TermType getTermType() { + return TermType.NULL; + } + public static Result deserialize(Schema.Term term) { if (!term.hasNull()) { return Result.err( @@ -245,6 +330,11 @@ public String typeOf() { return "date"; } + @Override + public TermType getTermType() { + return TermType.DATE; + } + public static Result deserialize(Schema.Term term) { if (!term.hasDate()) { return Result.err( @@ -312,6 +402,16 @@ public String typeOf() { return "integer"; } + @Override + public TermType getTermType() { + return TermType.INTEGER; + } + + @Override + public MapKeyType getMapKeyType() { + return MapKeyType.INTEGER; + } + public Schema.MapKey serializeMapKey() { return Schema.MapKey.newBuilder().setInteger(this.value).build(); } @@ -394,6 +494,11 @@ public String typeOf() { return "bytes"; } + @Override + public TermType getTermType() { + return TermType.BYTES; + } + public static Result deserialize(Schema.Term term) { if (!term.hasBytes()) { return Result.err( @@ -456,6 +561,16 @@ public String typeOf() { return "string"; } + @Override + public TermType getTermType() { + return TermType.STRING; + } + + @Override + public MapKeyType getMapKeyType() { + return MapKeyType.STRING; + } + public Schema.MapKey serializeMapKey() { return Schema.MapKey.newBuilder().setString(this.value).build(); } @@ -536,6 +651,11 @@ public String typeOf() { throw new InternalError(); } + @Override + public TermType getTermType() { + return TermType.VARIABLE; + } + public static Result deserialize(Schema.Term term) { if (!term.hasVariable()) { return Result.err( @@ -604,6 +724,11 @@ public String typeOf() { return "bool"; } + @Override + public TermType getTermType() { + return TermType.BOOL; + } + public static Result deserialize(Schema.Term term) { if (!term.hasBool()) { return Result.err( @@ -677,6 +802,11 @@ public String typeOf() { return "array"; } + @Override + public TermType getTermType() { + return TermType.ARRAY; + } + public static Result deserialize( Schema.Term term, DeserializationLimit.PerBlock limits, SymbolTable symbols) { if (!term.hasArray()) { @@ -728,9 +858,9 @@ public org.eclipse.biscuit.token.builder.Term toTerm(SymbolTable symbolTable) { } public static final class Map extends Term { - private final HashMap value; + private final SortedMap value; - public HashMap value() { + public SortedMap value() { return this.value; } @@ -744,7 +874,7 @@ public boolean match(final Term other) { return false; } - public Map(final HashMap value) { + public Map(final SortedMap value) { this.value = value; } @@ -789,13 +919,18 @@ public String typeOf() { return "map"; } + @Override + public TermType getTermType() { + return TermType.MAP; + } + public static Result deserialize( Schema.Term term, DeserializationLimit.PerBlock limits, SymbolTable symbols) { if (!term.hasMap()) { return Result.err( new Error.FormatError.DeserializationError("invalid Term kind, expected map")); } - java.util.HashMap values = new HashMap<>(); + java.util.TreeMap values = new TreeMap<>(MapKey.comparator); Schema.Map s = term.getMap(); if (s.getEntriesCount() > limits.getMaxMapLength()) { @@ -830,8 +965,8 @@ public static Result deserialize( } public org.eclipse.biscuit.token.builder.Term toTerm(SymbolTable symbolTable) { - HashMap s = - new HashMap<>(); + TreeMap s = + new TreeMap<>(org.eclipse.biscuit.token.builder.MapKey.comparator); for (java.util.Map.Entry i : this.value.entrySet()) { s.put(i.getKey().toMapKey(symbolTable), i.getValue().toTerm(symbolTable)); @@ -842,9 +977,9 @@ public org.eclipse.biscuit.token.builder.Term toTerm(SymbolTable symbolTable) { } public static final class Set extends Term { - private final HashSet value; + private final SortedSet value; - public HashSet value() { + public SortedSet value() { return this.value; } @@ -858,7 +993,7 @@ public boolean match(final Term other) { return false; } - public Set(final HashSet value) { + public Set(final SortedSet value) { this.value = value; } @@ -912,13 +1047,18 @@ public String typeOf() { return "set"; } + @Override + public TermType getTermType() { + return TermType.SET; + } + public static Result deserialize( Schema.Term term, DeserializationLimit.PerBlock limits, SymbolTable symbols) { if (!term.hasSet()) { return Result.err( new Error.FormatError.DeserializationError("invalid Term kind, expected set")); } else { - java.util.HashSet values = new HashSet<>(); + java.util.SortedSet values = new TreeSet<>(Term.comparator); Schema.TermSet s = term.getSet(); if (s.getSetCount() > limits.getMaxSetLength()) { @@ -948,7 +1088,8 @@ public static Result deserialize( } public org.eclipse.biscuit.token.builder.Term toTerm(SymbolTable symbolTable) { - HashSet s = new HashSet<>(); + SortedSet s = + new TreeSet<>(org.eclipse.biscuit.token.builder.Term.comparator); for (Term i : this.value) { s.add(i.toTerm(symbolTable)); diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/expressions/Op.java b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/expressions/Op.java index e78f0da7..e1c87442 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/expressions/Op.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/datalog/expressions/Op.java @@ -10,13 +10,14 @@ import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Deque; -import java.util.HashMap; -import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.Optional; import java.util.Set; +import java.util.SortedMap; +import java.util.SortedSet; +import java.util.TreeSet; import org.eclipse.biscuit.datalog.*; import org.eclipse.biscuit.error.Error; import org.eclipse.biscuit.error.Result; @@ -448,7 +449,7 @@ public void evaluate( stack.push(new Term.Bool(array.contains(right))); } if (left instanceof Term.Map) { - HashMap map = ((Term.Map) left).value(); + SortedMap map = ((Term.Map) left).value(); if (right instanceof MapKey) { MapKey key = (MapKey) right; stack.push(new Term.Bool(map.containsKey(key))); @@ -633,9 +634,9 @@ public void evaluate( break; case Intersection: if (right instanceof Term.Set && left instanceof Term.Set) { - HashSet intersec = new HashSet<>(); - HashSet setRight = ((Term.Set) right).value(); - HashSet setLeft = ((Term.Set) left).value(); + SortedSet intersec = new TreeSet<>(Term.comparator); + SortedSet setRight = ((Term.Set) right).value(); + SortedSet setLeft = ((Term.Set) left).value(); for (Term locId : setRight) { if (setLeft.contains(locId)) { intersec.add(locId); @@ -646,9 +647,9 @@ public void evaluate( break; case Union: if (right instanceof Term.Set && left instanceof Term.Set) { - HashSet union = new HashSet<>(); - HashSet setRight = ((Term.Set) right).value(); - HashSet setLeft = ((Term.Set) left).value(); + SortedSet union = new TreeSet<>(Term.comparator); + SortedSet setRight = ((Term.Set) right).value(); + SortedSet setLeft = ((Term.Set) left).value(); union.addAll(setRight); union.addAll(setLeft); stack.push(new Term.Set(union)); @@ -688,7 +689,7 @@ public void evaluate( } if (right instanceof MapKey && left instanceof Term.Map) { MapKey key = (MapKey) right; - HashMap map = ((Term.Map) left).value(); + SortedMap map = ((Term.Map) left).value(); Term value = map.get(key); stack.push(Objects.requireNonNullElseGet(value, Term.Null::new)); } @@ -710,7 +711,7 @@ public void evaluate( } } } else if (left instanceof Term.Set) { - HashSet set = ((Term.Set) left).value(); + SortedSet set = ((Term.Set) left).value(); for (Term elem : set) { Term returnValue = closure.call(elem, variables, temporarySymbolTable, steps); if (!(returnValue instanceof Term.Bool)) { @@ -722,7 +723,7 @@ public void evaluate( } } } else if (left instanceof Term.Map) { - HashMap map = ((Term.Map) left).value(); + SortedMap map = ((Term.Map) left).value(); for (Map.Entry entry : map.entrySet()) { List params = new ArrayList<>(List.of(entry.getKey(), entry.getValue())); Term returnValue = @@ -760,7 +761,7 @@ public void evaluate( } } } else if (left instanceof Term.Set) { - HashSet set = ((Term.Set) left).value(); + SortedSet set = ((Term.Set) left).value(); for (Term elem : set) { Term returnValue = closure.call(elem, variables, temporarySymbolTable, steps); if (!(returnValue instanceof Term.Bool)) { @@ -772,7 +773,7 @@ public void evaluate( } } } else if (left instanceof Term.Map) { - HashMap map = ((Term.Map) left).value(); + SortedMap map = ((Term.Map) left).value(); for (Map.Entry entry : map.entrySet()) { ArrayList params = new ArrayList<>(List.of(entry.getKey(), entry.getValue())); Term returnValue = diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/error/Error.java b/biscuit-core/src/main/java/org/eclipse/biscuit/error/Error.java index a0fa0350..3b483944 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/error/Error.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/error/Error.java @@ -996,8 +996,7 @@ private String kind2String(Kind k) { @Override public JsonNode toJson() { - var kindObj = - objectMapper.createObjectNode().put("type", kind2String(kind)); + var kindObj = objectMapper.createObjectNode().put("type", kind2String(kind)); return objectMapper.createObjectNode().put("type", "execution").set("value", kindObj); } diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/error/FailedCheck.java b/biscuit-core/src/main/java/org/eclipse/biscuit/error/FailedCheck.java index 34c4eab5..e0cbff88 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/error/FailedCheck.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/error/FailedCheck.java @@ -68,7 +68,7 @@ public JsonNode toJson() { child.set("block_id", LongNode.valueOf(this.blockId)); child.set("check_id", LongNode.valueOf(this.checkId)); child.set("rule", TextNode.valueOf(this.rule)); - return objectMapper.createObjectNode().put("type","block").set("value", child); + return objectMapper.createObjectNode().put("type", "block").set("value", child); } } @@ -108,7 +108,7 @@ public JsonNode toJson() { ObjectNode child = objectMapper.createObjectNode(); child.set("check_id", LongNode.valueOf(this.checkId)); child.set("rule", TextNode.valueOf(this.rule)); - return objectMapper.createObjectNode().put("type","authorizer").set("value", child); + return objectMapper.createObjectNode().put("type", "authorizer").set("value", child); } } diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/error/LogicError.java b/biscuit-core/src/main/java/org/eclipse/biscuit/error/LogicError.java index 35cabb3e..4b026993 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/error/LogicError.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/error/LogicError.java @@ -9,7 +9,6 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; -import com.fasterxml.jackson.databind.node.TextNode; import java.util.List; import java.util.Objects; import java.util.Optional; @@ -172,7 +171,7 @@ public JsonNode toJson() { ArrayNode child = objectMapper.createArrayNode(); child.add(id); child.add(err); - return objectMapper.createObjectNode().put("type","invalid_block_rule").set("value", child); + return objectMapper.createObjectNode().put("type", "invalid_block_rule").set("value", child); } } diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/token/DeserializationLimit.java b/biscuit-core/src/main/java/org/eclipse/biscuit/token/DeserializationLimit.java index 4d918818..2bbffca9 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/token/DeserializationLimit.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/token/DeserializationLimit.java @@ -13,6 +13,7 @@ import java.util.Set; import org.eclipse.biscuit.datalog.Check; import org.eclipse.biscuit.datalog.SymbolTable; +import org.eclipse.biscuit.datalog.Term; import org.eclipse.biscuit.datalog.expressions.Op; import org.eclipse.biscuit.error.Error; @@ -233,23 +234,6 @@ public int hashCode() { return Arrays.hashCode(state()); } - public enum TermType { - INTEGER, - STRING, - DATE, - BYTES, - BOOL, - SET, - NULL, - ARRAY, - MAP - } - - public enum MapKeyType { - INTEGER, - STRING - } - public static final class TrustedScopeKind { private enum Kind { AUTHORITY, @@ -319,8 +303,8 @@ public static final class PerBlock { private Set allowedUnaryOps = Set.of(); private Set allowedUnaryExternalFunctions = Set.of(); private Set allowedBinaryExternalFunctions = Set.of(); - private Set allowedTermTypes = Set.of(); - private Set allowedMapKeyTypes = Set.of(); + private Set allowedTermTypes = Set.of(); + private Set allowedMapKeyTypes = Set.of(); private Set allowedTrustingScopes = Set.of(); private Set allowedCheckKinds = Set.of(); @@ -607,20 +591,20 @@ public PerBlock setAllowedBinaryExternalFunctions(Set names) { return this; } - public Set getAllowedTermTypes() { + public Set getAllowedTermTypes() { return allowedTermTypes; } - public PerBlock setAllowedTermTypes(Set types) { + public PerBlock setAllowedTermTypes(Set types) { this.allowedTermTypes = copy(types); return this; } - public Set getAllowedMapKeyTypes() { + public Set getAllowedMapKeyTypes() { return allowedMapKeyTypes; } - public PerBlock setAllowedMapKeyTypes(Set types) { + public PerBlock setAllowedMapKeyTypes(Set types) { this.allowedMapKeyTypes = copy(types); return this; } diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/token/builder/MapKey.java b/biscuit-core/src/main/java/org/eclipse/biscuit/token/builder/MapKey.java index 20079ce4..96ef132d 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/token/builder/MapKey.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/token/builder/MapKey.java @@ -1,7 +1,29 @@ package org.eclipse.biscuit.token.builder; +import java.util.Comparator; import org.eclipse.biscuit.datalog.SymbolTable; public abstract class MapKey extends Term { public abstract org.eclipse.biscuit.datalog.MapKey convertMapKey(SymbolTable symbolTable); + + public abstract org.eclipse.biscuit.datalog.Term.MapKeyType getMapKeyType(); + + public static Comparator comparator = + new Comparator() { + @Override + public int compare(MapKey m1, MapKey m2) { + int c = m1.getMapKeyType().compareTo(m2.getMapKeyType()); + if (c != 0) { + return c; + } + switch (m1.getMapKeyType()) { + case INTEGER: + return Long.compare(((Term.Integer) m1).getValue(), ((Term.Integer) m2).getValue()); + case STRING: + return ((Term.Str) m1).getValue().compareTo(((Term.Str) m2).getValue()); + default: + throw new UnknownError("Unexpected MapKey " + m1 + m2); + } + } + }; } diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/token/builder/Term.java b/biscuit-core/src/main/java/org/eclipse/biscuit/token/builder/Term.java index ea8a23f7..dccde6e0 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/token/builder/Term.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/token/builder/Term.java @@ -10,20 +10,86 @@ import java.time.format.DateTimeFormatter; import java.util.Arrays; import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; +import java.util.Comparator; import java.util.Objects; +import java.util.SortedMap; +import java.util.SortedSet; +import java.util.TreeMap; +import java.util.TreeSet; import java.util.stream.Collectors; import org.eclipse.biscuit.datalog.SymbolTable; +import org.eclipse.biscuit.datalog.Term.MapKeyType; import org.eclipse.biscuit.datalog.expressions.Op; public abstract class Term extends Expression { public abstract org.eclipse.biscuit.datalog.Term convert(SymbolTable symbolTable); + public abstract org.eclipse.biscuit.datalog.Term.TermType getTermType(); + public static Term convertFrom(org.eclipse.biscuit.datalog.Term id, SymbolTable symbols) { return id.toTerm(symbols); } + /** + * Total order on builder terms: first by {@link org.eclipse.biscuit.datalog.Term.TermType} (in + * declaration order), then by value. Mirrors {@link org.eclipse.biscuit.datalog.Term#comparator}, + * except that strings are compared by content rather than by symbol index. + */ + public static final Comparator comparator = + new Comparator<>() { + @Override + public int compare(Term o1, Term o2) { + int c = o1.getTermType().compareTo(o2.getTermType()); + if (c != 0) { + return c; + } + switch (o1.getTermType()) { + case VARIABLE: + return ((Variable) o1).value.compareTo(((Variable) o2).value); + case INTEGER: + return Long.compare(((Integer) o1).value, ((Integer) o2).value); + case STRING: + return ((Str) o1).value.compareTo(((Str) o2).value); + case DATE: + return Long.compare(((Date) o1).value, ((Date) o2).value); + case BYTES: + return Arrays.compareUnsigned(((Bytes) o1).value, ((Bytes) o2).value); + case BOOL: + return Boolean.compare(((Bool) o1).value, ((Bool) o2).value); + case NULL: + return 0; + case SET: + return Utils.iteratorComparator(Term.comparator) + .compare( + Utils.treeSetWithComparator(((Set) o1).value, Term.comparator).iterator(), + Utils.treeSetWithComparator(((Set) o2).value, Term.comparator).iterator()); + case ARRAY: + return Utils.iteratorComparator(Term.comparator) + .compare(((Array) o1).value.iterator(), ((Array) o2).value.iterator()); + case MAP: + return Utils.iteratorComparator(MAP_ENTRY_COMPARATOR) + .compare( + ((Map) o1).value.entrySet().iterator(), + ((Map) o2).value.entrySet().iterator()); + default: + throw new IllegalStateException("unhandled term type " + o1.getTermType()); + } + } + }; + + private static final Comparator> MAP_ENTRY_COMPARATOR = + new Comparator<>() { + @Override + public int compare( + java.util.Map.Entry e1, java.util.Map.Entry e2) { + int c = MapKey.comparator.compare(e1.getKey(), e2.getKey()); + if (c != 0) { + return c; + } + return Term.comparator.compare(e1.getValue(), e2.getValue()); + } + }; + public void toOpcodes(SymbolTable symbolTable, java.util.List ops) { ops.add(this.convert(symbolTable)); } @@ -46,6 +112,16 @@ public org.eclipse.biscuit.datalog.Term convert(SymbolTable symbolTable) { return new org.eclipse.biscuit.datalog.Term.Str(symbolTable.insert(this.value)); } + @Override + public MapKeyType getMapKeyType() { + return MapKeyType.STRING; + } + + @Override + public org.eclipse.biscuit.datalog.Term.TermType getTermType() { + return org.eclipse.biscuit.datalog.Term.TermType.STRING; + } + public org.eclipse.biscuit.datalog.MapKey convertMapKey(SymbolTable symbolTable) { return new org.eclipse.biscuit.datalog.Term.Str(symbolTable.insert(this.value)); } @@ -93,6 +169,11 @@ public String getValue() { return value; } + @Override + public org.eclipse.biscuit.datalog.Term.TermType getTermType() { + return org.eclipse.biscuit.datalog.Term.TermType.VARIABLE; + } + @Override public String toString() { return "$" + value; @@ -129,6 +210,11 @@ public long getValue() { return value; } + @Override + public org.eclipse.biscuit.datalog.Term.TermType getTermType() { + return org.eclipse.biscuit.datalog.Term.TermType.INTEGER; + } + @Override public org.eclipse.biscuit.datalog.Term convert(SymbolTable symbolTable) { return new org.eclipse.biscuit.datalog.Term.Integer(this.value); @@ -138,6 +224,11 @@ public org.eclipse.biscuit.datalog.MapKey convertMapKey(SymbolTable symbolTable) return new org.eclipse.biscuit.datalog.Term.Integer(this.value); } + @Override + public MapKeyType getMapKeyType() { + return MapKeyType.INTEGER; + } + @Override public String toString() { return String.valueOf(value); @@ -179,6 +270,11 @@ public byte[] getValue() { return Arrays.copyOf(value, value.length); } + @Override + public org.eclipse.biscuit.datalog.Term.TermType getTermType() { + return org.eclipse.biscuit.datalog.Term.TermType.BYTES; + } + @Override public String toString() { return "hex:" + Utils.byteArrayToHexString(value).toLowerCase(); @@ -211,6 +307,11 @@ public org.eclipse.biscuit.datalog.Term convert(SymbolTable symbolTable) { return new org.eclipse.biscuit.datalog.Term.Null(); } + @Override + public org.eclipse.biscuit.datalog.Term.TermType getTermType() { + return org.eclipse.biscuit.datalog.Term.TermType.NULL; + } + @Override public String toString() { return "null"; @@ -242,6 +343,11 @@ public org.eclipse.biscuit.datalog.Term convert(SymbolTable symbolTable) { return new org.eclipse.biscuit.datalog.Term.Date(this.value); } + @Override + public org.eclipse.biscuit.datalog.Term.TermType getTermType() { + return org.eclipse.biscuit.datalog.Term.TermType.DATE; + } + public long getValue() { return value; } @@ -286,6 +392,11 @@ public org.eclipse.biscuit.datalog.Term convert(SymbolTable symbolTable) { return new org.eclipse.biscuit.datalog.Term.Bool(this.value); } + @Override + public org.eclipse.biscuit.datalog.Term.TermType getTermType() { + return org.eclipse.biscuit.datalog.Term.TermType.BOOL; + } + public boolean getValue() { return value; } @@ -332,6 +443,11 @@ public org.eclipse.biscuit.datalog.Term convert(SymbolTable symbolTable) { value.stream().map(t -> t.convert(symbolTable)).collect(Collectors.toList())); } + @Override + public org.eclipse.biscuit.datalog.Term.TermType getTermType() { + return org.eclipse.biscuit.datalog.Term.TermType.ARRAY; + } + public java.util.List getValue() { return Collections.unmodifiableList(value); } @@ -362,16 +478,16 @@ public int hashCode() { } public static final class Map extends Term { - final HashMap value; + final SortedMap value; - public Map(HashMap value) { + public Map(SortedMap value) { this.value = value; } @Override public org.eclipse.biscuit.datalog.Term convert(SymbolTable symbolTable) { - HashMap s = - new HashMap<>(); + SortedMap s = + new TreeMap<>(org.eclipse.biscuit.datalog.MapKey.comparator); for (java.util.Map.Entry i : this.value.entrySet()) { s.put(i.getKey().convertMapKey(symbolTable), i.getValue().convert(symbolTable)); @@ -380,6 +496,11 @@ public org.eclipse.biscuit.datalog.Term convert(SymbolTable symbolTable) { return new org.eclipse.biscuit.datalog.Term.Map(s); } + @Override + public org.eclipse.biscuit.datalog.Term.TermType getTermType() { + return org.eclipse.biscuit.datalog.Term.TermType.MAP; + } + public java.util.Map getValue() { return Collections.unmodifiableMap(value); } @@ -388,7 +509,6 @@ public java.util.Map getValue() { public String toString() { return value.entrySet().stream() .map(entry -> entry.getKey() + ": " + entry.getValue()) - .sorted() .collect(Collectors.joining(", ", "{", "}")); } @@ -413,15 +533,17 @@ public int hashCode() { } public static final class Set extends Term { - final java.util.Set value; + final java.util.SortedSet value; - public Set(java.util.Set value) { + public Set(java.util.SortedSet value) { this.value = value; } @Override public org.eclipse.biscuit.datalog.Term convert(SymbolTable symbolTable) { - HashSet s = new HashSet<>(); + SortedSet s = + new TreeSet( + org.eclipse.biscuit.datalog.Term.comparator); for (Term t : this.value) { s.add(t.convert(symbolTable)); @@ -430,6 +552,11 @@ public org.eclipse.biscuit.datalog.Term convert(SymbolTable symbolTable) { return new org.eclipse.biscuit.datalog.Term.Set(s); } + @Override + public org.eclipse.biscuit.datalog.Term.TermType getTermType() { + return org.eclipse.biscuit.datalog.Term.TermType.SET; + } + public java.util.Set getValue() { return Collections.unmodifiableSet(value); } diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/token/builder/Utils.java b/biscuit-core/src/main/java/org/eclipse/biscuit/token/builder/Utils.java index 68db736d..aa321bf9 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/token/builder/Utils.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/token/builder/Utils.java @@ -8,9 +8,13 @@ import static org.eclipse.biscuit.datalog.Check.Kind.ONE; import java.util.ArrayList; +import java.util.Collection; +import java.util.Comparator; import java.util.Date; -import java.util.HashSet; +import java.util.Iterator; import java.util.List; +import java.util.SortedSet; +import java.util.TreeSet; import org.eclipse.biscuit.error.Error; public final class Utils { @@ -60,7 +64,7 @@ public static Term var(String name) { return new Term.Variable(name); } - public static Term set(HashSet s) { + public static Term set(SortedSet s) { return new Term.Set(s); } @@ -87,4 +91,40 @@ public static byte[] hexStringToByteArray(String hex) { } return data; } + + public static Comparator> listComparator(Comparator comparator) { + return (o1, o2) -> { + var c = Integer.compare(o1.size(), o2.size()); + if (c == 0) { + return iteratorComparator(comparator).compare(o1.iterator(), o2.iterator()); + } else { + return c; + } + }; + } + + public static Comparator> iteratorComparator(Comparator comparator) { + return (o1it, o2it) -> { + var c = 0; + while (c == 0) { + if (o1it.hasNext() && o2it.hasNext()) { + c = comparator.compare(o1it.next(), o2it.next()); + } else if (o1it.hasNext()) { + c = 1; + } else if (o2it.hasNext()) { + c = -1; + } else { + return c; + } + } + return c; + }; + } + + public static TreeSet treeSetWithComparator( + Collection c, Comparator comparator) { + var tS = new TreeSet(comparator); + tS.addAll(c); + return tS; + } } diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/token/builder/parser/Parser.java b/biscuit-core/src/main/java/org/eclipse/biscuit/token/builder/parser/Parser.java index e835f872..2fece79e 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/token/builder/parser/Parser.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/token/builder/parser/Parser.java @@ -9,10 +9,10 @@ import java.time.OffsetDateTime; import java.time.format.DateTimeParseException; import java.util.ArrayList; -import java.util.HashMap; -import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.TreeMap; +import java.util.TreeSet; import java.util.function.Function; import org.eclipse.biscuit.crypto.PublicKey; import org.eclipse.biscuit.datalog.Pair; @@ -71,7 +71,7 @@ public static Result>> datalogCompon return Result.ok(components); } - Map> errors = new HashMap<>(); + Map> errors = new TreeMap<>(); s = removeCommentsAndWhitespaces(s); String[] codeLines = s.split(";"); @@ -887,7 +887,7 @@ public static Result, Error> map(String s) { } s = s.substring(1); - HashMap v = new HashMap<>(); + TreeMap v = new TreeMap<>(MapKey.comparator); while (true) { s = space(s); @@ -950,13 +950,13 @@ public static Result, Error> set(String s) { s = space(s.substring(1)); if (s.charAt(0) == '}') { s = s.substring(1); - return Result.ok(new Pair<>(s, new Term.Set(new HashSet<>()))); + return Result.ok(new Pair<>(s, new Term.Set(new TreeSet<>(Term.comparator)))); } else { return Result.err(new Error(s, "closing brace not found")); } } - HashSet terms = new HashSet<>(); + TreeSet terms = new TreeSet<>(Term.comparator); while (true) { s = space(s); diff --git a/biscuit-core/src/main/java/org/eclipse/biscuit/token/format/SerializedBiscuit.java b/biscuit-core/src/main/java/org/eclipse/biscuit/token/format/SerializedBiscuit.java index a2d05466..1a833d1f 100644 --- a/biscuit-core/src/main/java/org/eclipse/biscuit/token/format/SerializedBiscuit.java +++ b/biscuit-core/src/main/java/org/eclipse/biscuit/token/format/SerializedBiscuit.java @@ -433,9 +433,7 @@ public Result verify(org.eclipse.biscuit.crypto.PublicKey root) if (this.proof.secretKey().getPublicKey().equals(currentKey)) { return Result.ok(null); } else { - return Result.err( - new Error.FormatError.Signature.InvalidSignature( - "signature error")); + return Result.err(new Error.FormatError.Signature.InvalidSignature("signature error")); } } else { byte[] finalSignature = this.proof.getSignature().get(); diff --git a/biscuit/src/main/java/org/eclipse/biscuit/bouncycastle/Ed25519PublicKey.java b/biscuit/src/main/java/org/eclipse/biscuit/bouncycastle/Ed25519PublicKey.java index f3d64af4..8d5a1ed4 100644 --- a/biscuit/src/main/java/org/eclipse/biscuit/bouncycastle/Ed25519PublicKey.java +++ b/biscuit/src/main/java/org/eclipse/biscuit/bouncycastle/Ed25519PublicKey.java @@ -74,9 +74,7 @@ public Optional verify(byte[] data, byte[] signature) { sgr.init(false, this.publicKey); sgr.update(data, 0, data.length); if (!sgr.verifySignature(signature)) { - return Optional.of( - new Error.FormatError.Signature.InvalidSignature( - "signature error")); + return Optional.of(new Error.FormatError.Signature.InvalidSignature("signature error")); } return Optional.empty(); diff --git a/biscuit/src/main/java/org/eclipse/biscuit/bouncycastle/SECP256R1PublicKey.java b/biscuit/src/main/java/org/eclipse/biscuit/bouncycastle/SECP256R1PublicKey.java index e3edda0e..1a48d81e 100644 --- a/biscuit/src/main/java/org/eclipse/biscuit/bouncycastle/SECP256R1PublicKey.java +++ b/biscuit/src/main/java/org/eclipse/biscuit/bouncycastle/SECP256R1PublicKey.java @@ -115,9 +115,7 @@ public Optional verify(byte[] data, byte[] signature) { } if (!signer.verifySignature(hash, sig[0], sig[1])) { - return Optional.of( - new Error.FormatError.Signature.InvalidSignature( - "signature error")); + return Optional.of(new Error.FormatError.Signature.InvalidSignature("signature error")); } return Optional.empty(); diff --git a/biscuit/src/test/java/org/eclipse/biscuit/builder/BuilderTest.java b/biscuit/src/test/java/org/eclipse/biscuit/builder/BuilderTest.java index 5d494cfa..091d6fab 100644 --- a/biscuit/src/test/java/org/eclipse/biscuit/builder/BuilderTest.java +++ b/biscuit/src/test/java/org/eclipse/biscuit/builder/BuilderTest.java @@ -17,9 +17,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Date; -import java.util.HashSet; import java.util.List; -import java.util.Set; import org.eclipse.biscuit.crypto.KeyPair; import org.eclipse.biscuit.datalog.SymbolTable; import org.eclipse.biscuit.error.Error; @@ -66,11 +64,12 @@ public void testBuild() throws Error.Language, Error.SymbolTableOverlap, Error.F Expression.OpCode.Contains, Utils.var("operation"), new Term.Set( - new HashSet<>( + Utils.treeSetWithComparator( Arrays.asList( Utils.str("create_topic"), Utils.str("get_topic"), - Utils.str("get_topics")))))))); + Utils.str("get_topics")), + Term.comparator)))))); authorityBuilder.addRule( Utils.constrainedRule( "right", @@ -93,7 +92,9 @@ public void testBuild() throws Error.Language, Error.SymbolTableOverlap, Error.F new Expression.Binary( Expression.OpCode.Contains, Utils.var("operation"), - new Term.Set(new HashSet<>(Arrays.asList(Utils.str("lookup")))))))); + new Term.Set( + Utils.treeSetWithComparator( + Arrays.asList(Utils.str("lookup")), Term.comparator)))))); org.eclipse.biscuit.token.Block authority = authorityBuilder.build(symbolTable); Biscuit rootBiscuit = Biscuit.make(rng, root, authority); @@ -121,14 +122,17 @@ public void testStringValueOfVariableTerm() { @Test public void testStringValueOfSetTerm() { String actual = - new Term.Set(Set.of(new Term.Str("a"), new Term.Str("b"), new Term.Integer((3)))) + new Term.Set( + Utils.treeSetWithComparator( + List.of(new Term.Str("a"), new Term.Str("b"), new Term.Integer((3))), + Term.comparator)) .toString(); assertTrue(actual.startsWith("{"), "starts with {"); assertTrue(actual.endsWith("}"), "ends with }"); assertTrue(actual.contains("\"a\""), "contains a"); assertTrue(actual.contains("\"b\""), "contains b"); assertTrue(actual.contains("3"), "contains 3"); - String empty = new Term.Set(new java.util.HashSet()).toString(); + String empty = new Term.Set(new java.util.TreeSet<>(Term.comparator)).toString(); assertEquals("{,}", empty); } diff --git a/biscuit/src/test/java/org/eclipse/biscuit/builder/parser/ParserTest.java b/biscuit/src/test/java/org/eclipse/biscuit/builder/parser/ParserTest.java index 1a97aec6..efd11b9b 100644 --- a/biscuit/src/test/java/org/eclipse/biscuit/builder/parser/ParserTest.java +++ b/biscuit/src/test/java/org/eclipse/biscuit/builder/parser/ParserTest.java @@ -12,10 +12,10 @@ import biscuit.format.schema.Schema; import java.util.ArrayList; import java.util.Arrays; -import java.util.HashMap; -import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.TreeMap; +import java.util.TreeSet; import org.eclipse.biscuit.crypto.PublicKey; import org.eclipse.biscuit.datalog.*; import org.eclipse.biscuit.datalog.expressions.Op; @@ -167,11 +167,13 @@ void expressionIntersectionAndContainsTest() { new Expression.Binary( Expression.OpCode.Intersection, Utils.set( - new HashSet<>( - Arrays.asList( - Utils.integer(1), Utils.integer(2), Utils.integer(3)))), + Utils.treeSetWithComparator( + Arrays.asList(Utils.integer(1), Utils.integer(2), Utils.integer(3)), + Term.comparator)), Utils.set( - new HashSet<>(Arrays.asList(Utils.integer(1), Utils.integer(2))))), + Utils.treeSetWithComparator( + Arrays.asList(Utils.integer(1), Utils.integer(2)), + Term.comparator))), Utils.integer(1)))), res); } @@ -191,11 +193,14 @@ void expressionIntersectionAndContainsAndLengthEqualsTest() { new Expression.Binary( Expression.OpCode.Intersection, Utils.set( - new HashSet<>( + Utils.treeSetWithComparator( Arrays.asList( - Utils.integer(1), Utils.integer(2), Utils.integer(3)))), + Utils.integer(1), Utils.integer(2), Utils.integer(3)), + Term.comparator)), Utils.set( - new HashSet<>(Arrays.asList(Utils.integer(1), Utils.integer(2)))))), + Utils.treeSetWithComparator( + Arrays.asList(Utils.integer(1), Utils.integer(2)), + Term.comparator)))), Utils.integer(2)))), res); } @@ -338,7 +343,7 @@ void testExpression() { var res5 = Parser.expression(" { \"abc\", \"def\" }.contains($operation) "); - HashSet s = new HashSet<>(); + TreeSet s = new TreeSet<>(Term.comparator); s.add(Utils.str("abc")); s.add(Utils.str("def")); @@ -380,7 +385,7 @@ void testParens() throws org.eclipse.biscuit.error.Error { new Op.Binary(Op.BinaryOp.Add)), ex.getOps()); - Map variables = new HashMap<>(); + Map variables = new TreeMap<>(); org.eclipse.biscuit.datalog.Term value = ex.evaluate( variables, @@ -419,7 +424,7 @@ void testParens() throws org.eclipse.biscuit.error.Error { new Op.Binary(Op.BinaryOp.Mul)), ex2.getOps()); - Map variables2 = new HashMap<>(); + Map variables2 = new TreeMap<>(); org.eclipse.biscuit.datalog.Term value2 = ex2.evaluate( variables2, diff --git a/biscuit/src/test/java/org/eclipse/biscuit/datalog/ExpressionTest.java b/biscuit/src/test/java/org/eclipse/biscuit/datalog/ExpressionTest.java index b3f00b5a..3a48ac65 100644 --- a/biscuit/src/test/java/org/eclipse/biscuit/datalog/ExpressionTest.java +++ b/biscuit/src/test/java/org/eclipse/biscuit/datalog/ExpressionTest.java @@ -10,11 +10,12 @@ import java.util.ArrayList; import java.util.Arrays; -import java.util.HashMap; -import java.util.HashSet; +import java.util.Map; +import java.util.TreeMap; import org.eclipse.biscuit.datalog.expressions.Expression; import org.eclipse.biscuit.datalog.expressions.Op; import org.eclipse.biscuit.error.Error; +import org.eclipse.biscuit.token.builder.Utils; import org.junit.jupiter.api.Test; public class ExpressionTest { @@ -37,7 +38,7 @@ public void testNegate() throws Error { assertEquals("!1 < $var", e.print(symbolTable).get()); - HashMap variables = new HashMap<>(); + Map variables = new TreeMap<>(); variables.put(SymbolTable.DEFAULT_SYMBOLS_OFFSET + 2L, new Term.Integer(0)); assertEquals( @@ -68,7 +69,7 @@ public void testAddsStr() throws Error { assertEquals( new Term.Str(SymbolTable.DEFAULT_SYMBOLS_OFFSET + 2), e.evaluate( - new HashMap<>(), + new TreeMap<>(), new TemporarySymbolTable(symbolTable), new StepCounter().budget(new RunLimits().getMaxSteps()))); } @@ -92,7 +93,7 @@ public void testContainsStr() throws Error { assertEquals( new Term.Bool(true), e.evaluate( - new HashMap<>(), + new TreeMap<>(), new TemporarySymbolTable(symbolTable), new StepCounter().budget(new RunLimits().getMaxSteps()))); } @@ -117,7 +118,7 @@ public void testNegativeContainsStr() throws Error { assertEquals( new Term.Bool(false), e.evaluate( - new HashMap<>(), + new TreeMap<>(), new TemporarySymbolTable(symbolTable), new StepCounter().budget(new RunLimits().getMaxSteps()))); } @@ -131,11 +132,14 @@ public void testIntersectionAndContains() throws Error { new ArrayList<>( Arrays.asList( new Term.Set( - new HashSet<>( + Utils.treeSetWithComparator( Arrays.asList( - new Term.Integer(1), new Term.Integer(2), new Term.Integer(3)))), + new Term.Integer(1), new Term.Integer(2), new Term.Integer(3)), + Term.comparator)), new Term.Set( - new HashSet<>(Arrays.asList(new Term.Integer(1), new Term.Integer(2)))), + Utils.treeSetWithComparator( + Arrays.asList(new Term.Integer(1), new Term.Integer(2)), + Term.comparator)), new Op.Binary(Op.BinaryOp.Intersection), new Term.Integer(1), new Op.Binary(Op.BinaryOp.Contains)))); @@ -145,7 +149,7 @@ public void testIntersectionAndContains() throws Error { assertEquals( new Term.Bool(true), e.evaluate( - new HashMap<>(), + new TreeMap<>(), new TemporarySymbolTable(symbolTable), new StepCounter().budget(new RunLimits().getMaxSteps()))); } @@ -153,7 +157,7 @@ public void testIntersectionAndContains() throws Error { private static Term evaluate(Op... ops) throws Error { return new Expression(new ArrayList<>(Arrays.asList(ops))) .evaluate( - new HashMap<>(), + new TreeMap<>(), new TemporarySymbolTable(new SymbolTable()), new StepCounter().budget(new RunLimits().getMaxSteps())); } diff --git a/biscuit/src/test/java/org/eclipse/biscuit/datalog/TermComparatorTest.java b/biscuit/src/test/java/org/eclipse/biscuit/datalog/TermComparatorTest.java new file mode 100644 index 00000000..1c86339d --- /dev/null +++ b/biscuit/src/test/java/org/eclipse/biscuit/datalog/TermComparatorTest.java @@ -0,0 +1,154 @@ +/* + * Copyright (c) 2019 Geoffroy Couprie and Contributors to the Eclipse Foundation. + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.eclipse.biscuit.datalog; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.List; +import java.util.TreeMap; +import java.util.TreeSet; +import org.eclipse.biscuit.datalog.Term.MapKeyType; +import org.eclipse.biscuit.datalog.Term.TermType; +import org.eclipse.biscuit.token.builder.Utils; +import org.junit.jupiter.api.Test; + +public class TermComparatorTest { + + private static org.eclipse.biscuit.datalog.Term.Map datalogMap(MapKey k, Term v) { + var m = new TreeMap(MapKey.comparator); + m.put(k, v); + return new Term.Map(m); + } + + @Test + public void datalogTermTypes() { + assertEquals(TermType.VARIABLE, new Term.Variable(0).getTermType()); + assertEquals(TermType.INTEGER, new Term.Integer(0).getTermType()); + assertEquals(TermType.STRING, new Term.Str(0).getTermType()); + assertEquals(TermType.DATE, new Term.Date(0).getTermType()); + assertEquals(TermType.BYTES, new Term.Bytes(new byte[0]).getTermType()); + assertEquals(TermType.BOOL, new Term.Bool(true).getTermType()); + assertEquals(TermType.NULL, new Term.Null().getTermType()); + assertEquals( + TermType.SET, + new Term.Set(Utils.treeSetWithComparator(List.of(), Term.comparator)).getTermType()); + assertEquals(TermType.ARRAY, new Term.Array(List.of()).getTermType()); + assertEquals(TermType.MAP, datalogMap(new Term.Integer(1), new Term.Null()).getTermType()); + assertEquals(MapKeyType.INTEGER, new Term.Integer(0).getMapKeyType()); + assertEquals(MapKeyType.STRING, new Term.Str(0).getMapKeyType()); + } + + @Test + public void datalogOrdering() { + // ascending order, each element strictly lower than the next + List ordered = + List.of( + new Term.Variable(5), + new Term.Integer(-1), + new Term.Integer(2), + new Term.Str(1), + new Term.Str(3), + new Term.Date(1), + new Term.Bytes(new byte[] {1}), + new Term.Bytes(new byte[] {1, 0}), + new Term.Bytes(new byte[] {(byte) 0xff}), + new Term.Bool(false), + new Term.Bool(true), + new Term.Set( + Utils.treeSetWithComparator(List.of(new Term.Integer(1)), Term.comparator)), + new Term.Set( + Utils.treeSetWithComparator( + List.of(new Term.Integer(1), new Term.Integer(2)), Term.comparator)), + new Term.Null(), + new Term.Array(List.of(new Term.Integer(1))), + new Term.Array(List.of(new Term.Integer(1), new Term.Integer(0))), + datalogMap(new Term.Integer(1), new Term.Integer(1)), + datalogMap(new Term.Integer(1), new Term.Integer(2)), + datalogMap(new Term.Str(0), new Term.Integer(0))); + for (int i = 0; i < ordered.size(); i++) { + for (int j = 0; j < ordered.size(); j++) { + assertEquals( + Integer.signum(Integer.compare(i, j)), + Integer.signum(Term.comparator.compare(ordered.get(i), ordered.get(j))), + ordered.get(i) + " vs " + ordered.get(j)); + } + } + } + + @Test + public void datalogMapKeyOrdering() { + assertTrue(MapKey.comparator.compare(new Term.Integer(100), new Term.Str(0)) < 0); + assertTrue(MapKey.comparator.compare(new Term.Str(0), new Term.Integer(100)) > 0); + assertTrue(MapKey.comparator.compare(new Term.Integer(1), new Term.Integer(2)) < 0); + assertTrue(MapKey.comparator.compare(new Term.Str(2), new Term.Str(1)) > 0); + assertEquals(0, MapKey.comparator.compare(new Term.Str(2), new Term.Str(2))); + } + + @Test + public void builderTermTypes() { + assertEquals( + TermType.VARIABLE, new org.eclipse.biscuit.token.builder.Term.Variable("a").getTermType()); + assertEquals( + TermType.INTEGER, new org.eclipse.biscuit.token.builder.Term.Integer(1).getTermType()); + assertEquals( + TermType.STRING, new org.eclipse.biscuit.token.builder.Term.Str("a").getTermType()); + assertEquals(TermType.DATE, new org.eclipse.biscuit.token.builder.Term.Date(1).getTermType()); + assertEquals( + TermType.BYTES, + new org.eclipse.biscuit.token.builder.Term.Bytes(new byte[0]).getTermType()); + assertEquals( + TermType.BOOL, new org.eclipse.biscuit.token.builder.Term.Bool(true).getTermType()); + assertEquals(TermType.NULL, new org.eclipse.biscuit.token.builder.Term.Null().getTermType()); + assertEquals( + TermType.SET, + new org.eclipse.biscuit.token.builder.Term.Set( + new TreeSet( + org.eclipse.biscuit.token.builder.Term.comparator)) + .getTermType()); + assertEquals( + TermType.ARRAY, new org.eclipse.biscuit.token.builder.Term.Array(List.of()).getTermType()); + assertEquals( + TermType.MAP, + new org.eclipse.biscuit.token.builder.Term.Map( + new TreeMap<>(org.eclipse.biscuit.token.builder.MapKey.comparator)) + .getTermType()); + } + + @Test + public void builderOrderingAndSetDeduplication() { + var b = org.eclipse.biscuit.token.builder.Term.comparator; + var i1 = new org.eclipse.biscuit.token.builder.Term.Integer(1); + var i2 = new org.eclipse.biscuit.token.builder.Term.Integer(2); + var sa = new org.eclipse.biscuit.token.builder.Term.Str("a"); + var sb = new org.eclipse.biscuit.token.builder.Term.Str("b"); + assertTrue(b.compare(i1, i2) < 0); + assertTrue(b.compare(i2, sa) < 0); + assertTrue(b.compare(sa, sb) < 0); + assertEquals(0, b.compare(sa, new org.eclipse.biscuit.token.builder.Term.Str("a"))); + + // sets are compared on their sorted content, whatever their iteration order + var s1 = + new org.eclipse.biscuit.token.builder.Term.Set( + Utils.treeSetWithComparator( + List.of(i2, i1), org.eclipse.biscuit.token.builder.Term.comparator)); + var s2 = + new org.eclipse.biscuit.token.builder.Term.Set( + Utils.treeSetWithComparator( + List.of(i1, i2), org.eclipse.biscuit.token.builder.Term.comparator)); + assertEquals(0, b.compare(s1, s2)); + + // converting a set with distinct elements keeps them all (comparator used to return 0) + var symbols = new SymbolTable(); + var converted = + (Term.Set) + new org.eclipse.biscuit.token.builder.Term.Set( + Utils.treeSetWithComparator( + List.of(i1, i2), org.eclipse.biscuit.token.builder.Term.comparator)) + .convert(symbols); + assertEquals(2, converted.value().size()); + } +} diff --git a/biscuit/src/test/java/org/eclipse/biscuit/datalog/WorldTest.java b/biscuit/src/test/java/org/eclipse/biscuit/datalog/WorldTest.java index 2d7eca00..e749bb15 100644 --- a/biscuit/src/test/java/org/eclipse/biscuit/datalog/WorldTest.java +++ b/biscuit/src/test/java/org/eclipse/biscuit/datalog/WorldTest.java @@ -12,17 +12,26 @@ import java.time.Instant; import java.util.ArrayList; -import java.util.HashSet; import java.util.Iterator; import java.util.List; +import java.util.TreeSet; import java.util.stream.Collectors; import org.eclipse.biscuit.datalog.expressions.Expression; import org.eclipse.biscuit.datalog.expressions.Op; import org.eclipse.biscuit.error.Error; +import org.eclipse.biscuit.token.builder.Utils; import org.junit.jupiter.api.Test; public class WorldTest { + private static TreeSet factSet(List facts) { + return Utils.treeSetWithComparator(facts, Fact.comparator); + } + + private static TreeSet termSet(List terms) { + return Utils.treeSetWithComparator(terms, Term.comparator); + } + @Test public void testFamily() throws Error { final World w = new World(); @@ -179,11 +188,12 @@ public void testFamily() throws Error { final FactSet expected = new FactSet( new Origin(0), - new HashSet<>( + Utils.treeSetWithComparator( List.of( new Fact(new Predicate(grandparent, List.of(a, c))), new Fact(new Predicate(grandparent, List.of(b, d))), - new Fact(new Predicate(grandparent, List.of(b, e)))))); + new Fact(new Predicate(grandparent, List.of(b, e)))), + Fact.comparator)); assertEquals(expected, res); w.addRule( @@ -297,11 +307,12 @@ public void testNumbers() throws Error { FactSet expected = new FactSet( new Origin(0), - new HashSet<>( + Utils.treeSetWithComparator( List.of( new Fact(new Predicate(join, List.of(abc, aaa))), new Fact(new Predicate(join, List.of(abc, bbb))), - new Fact(new Predicate(join, List.of(def, ccc)))))); + new Fact(new Predicate(join, List.of(def, ccc)))), + Fact.comparator)); assertEquals(expected, res); res = @@ -341,10 +352,11 @@ public void testNumbers() throws Error { expected = new FactSet( new Origin(0), - new HashSet<>( + Utils.treeSetWithComparator( List.of( new Fact(new Predicate(join, List.of(abc, aaa))), - new Fact(new Predicate(join, List.of(abc, bbb)))))); + new Fact(new Predicate(join, List.of(abc, bbb)))), + Fact.comparator)); assertEquals(expected, res); } @@ -416,8 +428,9 @@ public void testStr() throws Error { FactSet expected = new FactSet( new Origin(0), - new HashSet<>( - List.of(new Fact(new Predicate(suff, List.of(app_2, syms.add("test.fr"))))))); + Utils.treeSetWithComparator( + List.of(new Fact(new Predicate(suff, List.of(app_2, syms.add("test.fr"))))), + Fact.comparator)); assertEquals(expected, res); res = testSuffix(w, syms, suff, route, "example.com"); @@ -428,11 +441,12 @@ public void testStr() throws Error { expected = new FactSet( new Origin(0), - new HashSet<>( + Utils.treeSetWithComparator( List.of( new Fact(new Predicate(suff, List.of(app_0, syms.add("example.com")))), new Fact(new Predicate(suff, List.of(app_0, syms.add("www.example.com")))), - new Fact(new Predicate(suff, List.of(app_1, syms.add("mx.example.com"))))))); + new Fact(new Predicate(suff, List.of(app_1, syms.add("mx.example.com"))))), + Fact.comparator)); assertEquals(expected, res); } @@ -498,7 +512,7 @@ public void testDate() throws Error { FactSet expected = new FactSet( new Origin(0), - new HashSet<>( + factSet( List.of( new Fact( new Predicate(before, List.of(new Term.Date(t1.getEpochSecond()), abc)))))); @@ -539,7 +553,7 @@ public void testDate() throws Error { expected = new FactSet( new Origin(0), - new HashSet<>( + factSet( List.of( new Fact( new Predicate(after, List.of(new Term.Date(t3.getEpochSecond()), def)))))); @@ -583,7 +597,7 @@ public void testSet() throws Error { new ArrayList<>( List.of( new Term.Set( - new HashSet<>(List.of(new Term.Integer(0L), new Term.Integer(1L)))), + termSet(List.of(new Term.Integer(0L), new Term.Integer(1L)))), new Term.Variable(syms.insert("int")), new Op.Binary(Op.BinaryOp.Contains)))))); System.out.println("testing r1: " + syms.formatRule(r1)); @@ -595,8 +609,7 @@ public void testSet() throws Error { FactSet expected = new FactSet( new Origin(0), - new HashSet<>( - List.of(new Fact(new Predicate(int_set, List.of(abc, syms.add("test"))))))); + factSet(List.of(new Fact(new Predicate(int_set, List.of(abc, syms.add("test"))))))); assertEquals(expected, res); final long abcSymId = syms.insert("abc"); @@ -622,8 +635,7 @@ public void testSet() throws Error { new ArrayList<>( List.of( new Term.Set( - new HashSet<>( - List.of(new Term.Str(abcSymId), new Term.Str(ghiSymId)))), + termSet(List.of(new Term.Str(abcSymId), new Term.Str(ghiSymId)))), new Term.Variable(syms.insert("sym")), new Op.Binary(Op.BinaryOp.Contains), new Op.Unary(Op.UnaryOp.Negate)))))); @@ -637,7 +649,7 @@ public void testSet() throws Error { expected = new FactSet( new Origin(0), - new HashSet<>( + factSet( List.of( new Fact( new Predicate( @@ -663,7 +675,7 @@ public void testSet() throws Error { new Expression( new ArrayList<>( List.of( - new Term.Set(new HashSet<>(List.of(syms.add("test"), syms.add("aaa")))), + new Term.Set(termSet(List.of(syms.add("test"), syms.add("aaa")))), new Term.Variable(syms.insert("str")), new Op.Binary(Op.BinaryOp.Contains)))))); System.out.println("testing r3: " + syms.formatRule(r3)); @@ -675,7 +687,7 @@ public void testSet() throws Error { expected = new FactSet( new Origin(0), - new HashSet<>( + factSet( List.of( new Fact( new Predicate( diff --git a/biscuit/src/test/java/org/eclipse/biscuit/token/DeserializationLimitsTest.java b/biscuit/src/test/java/org/eclipse/biscuit/token/DeserializationLimitsTest.java index bfaaee73..50aa2b12 100644 --- a/biscuit/src/test/java/org/eclipse/biscuit/token/DeserializationLimitsTest.java +++ b/biscuit/src/test/java/org/eclipse/biscuit/token/DeserializationLimitsTest.java @@ -21,12 +21,12 @@ import org.eclipse.biscuit.crypto.KeyDelegate; import org.eclipse.biscuit.crypto.KeyPair; import org.eclipse.biscuit.datalog.Check; +import org.eclipse.biscuit.datalog.Term.MapKeyType; +import org.eclipse.biscuit.datalog.Term.TermType; import org.eclipse.biscuit.datalog.expressions.Op; import org.eclipse.biscuit.error.Error; import org.eclipse.biscuit.error.Error.FormatError.DeserializationLimitError.Reason; -import org.eclipse.biscuit.token.DeserializationLimit.MapKeyType; import org.eclipse.biscuit.token.DeserializationLimit.PerBlock; -import org.eclipse.biscuit.token.DeserializationLimit.TermType; import org.eclipse.biscuit.token.DeserializationLimit.TrustedScopeKind; import org.eclipse.biscuit.token.builder.Block; import org.junit.jupiter.api.BeforeEach; diff --git a/biscuit/src/test/java/org/eclipse/biscuit/token/SamplesTest.java b/biscuit/src/test/java/org/eclipse/biscuit/token/SamplesTest.java index ca77ebfd..b7fab555 100644 --- a/biscuit/src/test/java/org/eclipse/biscuit/token/SamplesTest.java +++ b/biscuit/src/test/java/org/eclipse/biscuit/token/SamplesTest.java @@ -26,12 +26,12 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; -import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.Optional; +import java.util.TreeMap; import java.util.stream.Collectors; import java.util.stream.Stream; import org.eclipse.biscuit.crypto.KeyPair; @@ -43,6 +43,7 @@ import org.eclipse.biscuit.error.Result; import org.eclipse.biscuit.token.builder.AuthorizerBuilder; import org.eclipse.biscuit.token.builder.Check; +import org.eclipse.biscuit.token.builder.Utils; import org.eclipse.biscuit.token.builder.parser.Parser; import org.eclipse.biscuit.token.format.SignedBlock; import org.junit.jupiter.api.DynamicTest; @@ -212,9 +213,13 @@ DynamicTest processTestcase( List revocationIds = token.revocationIdentifiers(); ArrayNode validationRevocationIds = (ArrayNode) validation.get("revocation_ids"); - assertEquals(revocationIds.size(), validationRevocationIds.size(),"revocation_ids size"); + assertEquals( + revocationIds.size(), validationRevocationIds.size(), "revocation_ids size"); for (int i = 0; i < revocationIds.size(); i++) { - assertEquals(validationRevocationIds.get(i).asText(), revocationIds.get(i).toHex(),"revocation_ids"); + assertEquals( + validationRevocationIds.get(i).asText(), + revocationIds.get(i).toHex(), + "revocation_ids"); } // TODO Add check of the token @@ -290,7 +295,8 @@ DynamicTest processTestcase( } } else { if (res.isErr()) { - if (res.getErr() instanceof Error && "err".equals(expectedResult.get("type").textValue())) { + if (res.getErr() instanceof Error + && "err".equals(expectedResult.get("type").textValue())) { Error e = (Error) res.getErr(); System.out.println("validation '" + validationName + "' got error: " + e); @@ -469,8 +475,9 @@ public World(Authorizer authorizer) { Collections.sort(this.checks); } - public HashMap, List> factMap() { - HashMap, List> worldFacts = new HashMap<>(); + public Map, List> factMap() { + Map, List> worldFacts = + new TreeMap<>(Utils.listComparator(Long::compareTo)); for (FactSet f : this.facts) { worldFacts.put(f.origin, f.facts); } From 670cf708493fe46a11fde701f974f204a981a744 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Corbi=C3=A8re?= Date: Fri, 27 Nov 2026 13:02:27 +0100 Subject: [PATCH 11/11] Edit samples.json to match the FFI behavior --- biscuit/src/test/resources/samples/samples.json | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/biscuit/src/test/resources/samples/samples.json b/biscuit/src/test/resources/samples/samples.json index e0011114..a768d00d 100644 --- a/biscuit/src/test/resources/samples/samples.json +++ b/biscuit/src/test/resources/samples/samples.json @@ -3248,8 +3248,14 @@ "elapsed_steps": 3 }, "result": { - "type": "ok", - "value": 0 + "type": "err", + "value": { + "type": "format", + "value": { + "type": "deserialization_error", + "value": "invalid unary operation" + } + } }, "authorizer_code": "allow if true;\n", "revocation_ids": [