Skip to content

Commit 6b7cb9c

Browse files
committed
Test: apply the google rules
1 parent 7b4e803 commit 6b7cb9c

File tree

11 files changed

+223
-195
lines changed

11 files changed

+223
-195
lines changed

src/test/java/org/biscuitsec/biscuit/builder/BuilderTest.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package org.biscuitsec.biscuit.builder;
22

3-
import static org.junit.jupiter.api.Assertions.*;
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
5+
import static org.junit.jupiter.api.Assertions.assertNotNull;
6+
import static org.junit.jupiter.api.Assertions.assertTrue;
47

58
import biscuit.format.schema.Schema;
69
import java.nio.charset.StandardCharsets;
@@ -28,11 +31,11 @@ public void testBuild() throws Error.Language, Error.SymbolTableOverlap, Error.F
2831
KeyPair root = KeyPair.generate(Schema.PublicKey.Algorithm.Ed25519, rng);
2932
SymbolTable symbols = Biscuit.defaultSymbolTable();
3033

31-
Block authority_builder = new Block();
32-
authority_builder.addFact(
34+
Block authorityBuilder = new Block();
35+
authorityBuilder.addFact(
3336
Utils.fact("revocation_id", Arrays.asList(Utils.date(Date.from(Instant.now())))));
34-
authority_builder.addFact(Utils.fact("right", Arrays.asList(Utils.str("admin"))));
35-
authority_builder.addRule(
37+
authorityBuilder.addFact(Utils.fact("right", Arrays.asList(Utils.str("admin"))));
38+
authorityBuilder.addRule(
3639
Utils.constrainedRule(
3740
"right",
3841
Arrays.asList(
@@ -59,7 +62,7 @@ public void testBuild() throws Error.Language, Error.SymbolTableOverlap, Error.F
5962
Utils.str("create_topic"),
6063
Utils.str("get_topic"),
6164
Utils.str("get_topics")))))))));
62-
authority_builder.addRule(
65+
authorityBuilder.addRule(
6366
Utils.constrainedRule(
6467
"right",
6568
Arrays.asList(
@@ -84,7 +87,7 @@ public void testBuild() throws Error.Language, Error.SymbolTableOverlap, Error.F
8487
new Expression.Value(
8588
new Term.Set(new HashSet<>(Arrays.asList(Utils.str("lookup")))))))));
8689

87-
org.biscuitsec.biscuit.token.Block authority = authority_builder.build(symbols);
90+
org.biscuitsec.biscuit.token.Block authority = authorityBuilder.build(symbols);
8891
Biscuit rootBiscuit = Biscuit.make(rng, root, authority);
8992

9093
System.out.println(rootBiscuit.print());
@@ -93,22 +96,22 @@ public void testBuild() throws Error.Language, Error.SymbolTableOverlap, Error.F
9396
}
9497

9598
@Test
96-
public void testStringValueOfAStringTerm() {
99+
public void testStringValueOfStringTerm() {
97100
assertEquals("\"hello\"", new Term.Str("hello").toString());
98101
}
99102

100103
@Test
101-
public void testStringValueOfAnIntegerTerm() {
104+
public void testStringValueOfIntegerTerm() {
102105
assertEquals("123", new Term.Integer(123).toString());
103106
}
104107

105108
@Test
106-
public void testStringValueOfAVariableTerm() {
109+
public void testStringValueOfVariableTerm() {
107110
assertEquals("$hello", new Term.Variable("hello").toString());
108111
}
109112

110113
@Test
111-
public void testStringValueOfASetTerm() {
114+
public void testStringValueOfSetTerm() {
112115
String actual =
113116
new Term.Set(Set.of(new Term.Str("a"), new Term.Str("b"), new Term.Integer((3))))
114117
.toString();
@@ -120,7 +123,7 @@ public void testStringValueOfASetTerm() {
120123
}
121124

122125
@Test
123-
public void testStringValueOfAByteArrayTermIsJustTheArrayReferenceNotTheContents() {
126+
public void testStringValueOfByteArrayTermIsJustTheArrayReferenceNotTheContents() {
124127
String string = new Term.Bytes("Hello".getBytes(StandardCharsets.UTF_8)).toString();
125128
assertTrue(string.startsWith("hex:"), "starts with hex prefix");
126129
}

src/test/java/org/biscuitsec/biscuit/builder/parser/ParserTest.java

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,34 @@
11
package org.biscuitsec.biscuit.builder.parser;
22

33
import static org.biscuitsec.biscuit.datalog.Check.Kind.One;
4-
import static org.junit.jupiter.api.Assertions.*;
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
56

67
import biscuit.format.schema.Schema;
78
import io.vavr.Tuple2;
89
import io.vavr.control.Either;
9-
import java.util.*;
10+
import java.util.ArrayList;
11+
import java.util.Arrays;
12+
import java.util.HashMap;
13+
import java.util.HashSet;
14+
import java.util.List;
15+
import java.util.Map;
1016
import org.biscuitsec.biscuit.crypto.PublicKey;
1117
import org.biscuitsec.biscuit.datalog.SymbolTable;
1218
import org.biscuitsec.biscuit.datalog.TemporarySymbolTable;
1319
import org.biscuitsec.biscuit.datalog.expressions.Op;
1420
import org.biscuitsec.biscuit.token.Biscuit;
15-
import org.biscuitsec.biscuit.token.builder.*;
21+
import org.biscuitsec.biscuit.token.builder.Block;
22+
import org.biscuitsec.biscuit.token.builder.Check;
23+
import org.biscuitsec.biscuit.token.builder.Expression;
24+
import org.biscuitsec.biscuit.token.builder.Fact;
25+
import org.biscuitsec.biscuit.token.builder.Predicate;
26+
import org.biscuitsec.biscuit.token.builder.Rule;
27+
import org.biscuitsec.biscuit.token.builder.Scope;
28+
import org.biscuitsec.biscuit.token.builder.Term;
29+
import org.biscuitsec.biscuit.token.builder.Utils;
1630
import org.biscuitsec.biscuit.token.builder.parser.Error;
1731
import org.biscuitsec.biscuit.token.builder.parser.Parser;
18-
import org.junit.jupiter.api.Assertions;
1932
import org.junit.jupiter.api.Test;
2033

2134
class ParserTest {
@@ -75,7 +88,8 @@ void testFact() throws org.biscuitsec.biscuit.error.Error.Language {
7588
assertEquals(
7689
Either.right(
7790
new Tuple2<>(
78-
"", Utils.fact("n1:right", Arrays.asList(Utils.string("file1"), Utils.str("read"))))),
91+
"",
92+
Utils.fact("n1:right", Arrays.asList(Utils.string("file1"), Utils.str("read"))))),
7993
res4);
8094
}
8195

@@ -227,30 +241,30 @@ void ruleWithFreeExpressionVariables() {
227241
Either.left(
228242
new Error(
229243
" resource($0), operation(\"read\"), $test",
230-
"rule head or expressions contains variables that are not used in predicates of the rule's body: [test]")),
244+
"rule head or expressions contains variables that are not used in predicates of the"
245+
+ " rule's body: [test]")),
231246
res);
232247
}
233248

234249
@Test
235250
void testRuleWithScope() {
236251
Either<Error, Tuple2<String, Rule>> res =
237252
Parser.rule(
238-
"valid_date(\"file1\") <- resource(\"file1\") trusting ed25519/6e9e6d5a75cf0c0e87ec1256b4dfed0ca3ba452912d213fcc70f8516583db9db, authority ");
239-
assertEquals(
240-
Either.right(
241-
new Tuple2<>(
242-
"",
243-
new Rule(
244-
new Predicate("valid_date", List.of(Utils.string("file1"))),
245-
Arrays.asList(Utils.pred("resource", List.of(Utils.string("file1")))),
246-
new ArrayList<>(),
247-
Arrays.asList(
248-
Scope.publicKey(
249-
new PublicKey(
250-
Schema.PublicKey.Algorithm.Ed25519,
251-
"6e9e6d5a75cf0c0e87ec1256b4dfed0ca3ba452912d213fcc70f8516583db9db")),
252-
Scope.authority())))),
253-
res);
253+
"valid_date(\"file1\") <- resource(\"file1\") trusting"
254+
+ " ed25519/6e9e6d5a75cf0c0e87ec1256b4dfed0ca3ba452912d213fcc70f8516583db9db,"
255+
+ " authority ");
256+
Rule refRule =
257+
new Rule(
258+
new Predicate("valid_date", List.of(Utils.string("file1"))),
259+
List.of(Utils.pred("resource", List.of(Utils.string("file1")))),
260+
new ArrayList<>(),
261+
Arrays.asList(
262+
Scope.publicKey(
263+
new PublicKey(
264+
Schema.PublicKey.Algorithm.Ed25519,
265+
"6e9e6d5a75cf0c0e87ec1256b4dfed0ca3ba452912d213fcc70f8516583db9db")),
266+
Scope.authority()));
267+
assertEquals(Either.right(new Tuple2<>("", refRule)), res);
254268
}
255269

256270
@Test
@@ -313,7 +327,7 @@ void testExpression() {
313327

314328
SymbolTable s3 = new SymbolTable();
315329
long test = s3.insert("test");
316-
Assertions.assertEquals(
330+
assertEquals(
317331
Arrays.asList(
318332
new Op.Value(new org.biscuitsec.biscuit.datalog.Term.Integer(1)),
319333
new Op.Value(new org.biscuitsec.biscuit.datalog.Term.Variable(test)),
Lines changed: 21 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.biscuitsec.biscuit.crypto;
22

3-
import static biscuit.format.schema.Schema.PublicKey.Algorithm.*;
43
import static io.vavr.API.Right;
54
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
65
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
@@ -22,26 +21,33 @@ public class SignatureTest {
2221

2322
@Test
2423
public void testSerialize() {
25-
testSerialize(Ed25519, 32);
26-
testSerialize(
27-
SECP256R1, 33); // compressed - 0x02 or 0x03 prefix byte, 32 bytes for X coordinate
24+
prTestSerialize(Schema.PublicKey.Algorithm.Ed25519, 32);
25+
prTestSerialize(
26+
// compressed - 0x02 or 0x03 prefix byte, 32 bytes for X coordinate
27+
Schema.PublicKey.Algorithm.SECP256R1, 33);
2828
}
2929

3030
@Test
3131
public void testThreeMessages()
3232
throws NoSuchAlgorithmException, SignatureException, InvalidKeyException {
33-
testThreeMessages(Ed25519);
34-
testThreeMessages(SECP256R1);
33+
prTestThreeMessages(Schema.PublicKey.Algorithm.Ed25519);
34+
prTestThreeMessages(Schema.PublicKey.Algorithm.SECP256R1);
3535
}
3636

37-
/*
3837
@Test
39-
public void testChangeMessages() throws NoSuchAlgorithmException, SignatureException, InvalidKeyException {
40-
testChangeMessages(Ed25519);
41-
testChangeMessages(SECP256R1);
42-
}*/
38+
public void testSerializeBiscuit() throws Error {
39+
var root = KeyPair.generate(Schema.PublicKey.Algorithm.SECP256R1);
40+
var biscuit =
41+
Biscuit.builder(root)
42+
.addAuthorityFact("user(\"1234\")")
43+
.addAuthorityCheck("check if operation(\"read\")")
44+
.build();
45+
var serialized = biscuit.serialize();
46+
var unverified = Biscuit.fromBytes(serialized);
47+
assertDoesNotThrow(() -> unverified.verify(root.getPublicKey()));
48+
}
4349

44-
private static void testSerialize(
50+
private static void prTestSerialize(
4551
Schema.PublicKey.Algorithm algorithm, int expectedPublicKeyLength) {
4652
byte[] seed = {1, 2, 3, 4};
4753
SecureRandom rng = new SecureRandom(seed);
@@ -52,8 +58,8 @@ private static void testSerialize(
5258
byte[] serializedSecretKey = keypair.toBytes();
5359
byte[] serializedPublicKey = pubkey.toBytes();
5460

55-
KeyPair deserializedSecretKey = KeyPair.generate(algorithm, serializedSecretKey);
56-
PublicKey deserializedPublicKey = new PublicKey(algorithm, serializedPublicKey);
61+
final KeyPair deserializedSecretKey = KeyPair.generate(algorithm, serializedSecretKey);
62+
final PublicKey deserializedPublicKey = new PublicKey(algorithm, serializedPublicKey);
5763

5864
assertEquals(32, serializedSecretKey.length);
5965
assertEquals(expectedPublicKeyLength, serializedPublicKey.length);
@@ -67,32 +73,7 @@ private static void testSerialize(
6773
assertEquals(pubkey.toHex(), deserializedPublicKey.toHex());
6874
}
6975

70-
/*
71-
private static void testChangeMessages(Schema.PublicKey.Algorithm algorithm) throws NoSuchAlgorithmException, SignatureException, InvalidKeyException {
72-
byte[] seed = {0, 0, 0, 0};
73-
SecureRandom rng = new SecureRandom(seed);
74-
75-
String message1 = "hello";
76-
KeyPair root = KeyPair.generate(algorithm, rng);
77-
KeyPair keypair2 = KeyPair.generate(algorithm, rng);
78-
Token token1 = new Token(root, message1.getBytes(), keypair2);
79-
assertEquals(Right(null), token1.verify(new PublicKey(algorithm, root.getPublicKey().getKey())));
80-
81-
String message2 = "world";
82-
KeyPair keypair3 = KeyPair.generate(algorithm, rng);
83-
Token token2 = token1.append(keypair3, message2.getBytes());
84-
token2.blocks.set(1, "you".getBytes());
85-
assertEquals(Left(new Error.FormatError.Signature.InvalidSignature("signature error: Verification equation was not satisfied")),
86-
token2.verify(new PublicKey(algorithm, root.getPublicKey().getKey())));
87-
88-
String message3 = "!!";
89-
KeyPair keypair4 = KeyPair.generate(algorithm, rng);
90-
Token token3 = token2.append(keypair4, message3.getBytes());
91-
assertEquals(Left(new Error.FormatError.Signature.InvalidSignature("signature error: Verification equation was not satisfied")),
92-
token3.verify(new PublicKey(algorithm, root.getPublicKey().getKey())));
93-
}*/
94-
95-
private static void testThreeMessages(Schema.PublicKey.Algorithm algorithm)
76+
private static void prTestThreeMessages(Schema.PublicKey.Algorithm algorithm)
9677
throws NoSuchAlgorithmException, SignatureException, InvalidKeyException {
9778
byte[] seed = {0, 0, 0, 0};
9879
SecureRandom rng = new SecureRandom(seed);
@@ -118,17 +99,4 @@ private static void testThreeMessages(Schema.PublicKey.Algorithm algorithm)
11899
Token token3 = token2.append(keypair4, message3.getBytes());
119100
assertEquals(Right(null), token3.verify(root.getPublicKey()));
120101
}
121-
122-
@Test
123-
public void testSerializeBiscuit() throws Error {
124-
var root = KeyPair.generate(SECP256R1);
125-
var biscuit =
126-
Biscuit.builder(root)
127-
.addAuthorityFact("user(\"1234\")")
128-
.addAuthorityCheck("check if operation(\"read\")")
129-
.build();
130-
var serialized = biscuit.serialize();
131-
var unverified = Biscuit.fromBytes(serialized);
132-
assertDoesNotThrow(() -> unverified.verify(root.getPublicKey()));
133-
}
134102
}

src/test/java/org/biscuitsec/biscuit/datalog/WorldTest.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
import static org.junit.jupiter.api.Assertions.assertTrue;
55

66
import java.time.Instant;
7-
import java.util.*;
7+
import java.util.ArrayList;
8+
import java.util.Arrays;
9+
import java.util.HashSet;
10+
import java.util.Iterator;
811
import java.util.stream.Collectors;
912
import org.biscuitsec.biscuit.datalog.expressions.Expression;
1013
import org.biscuitsec.biscuit.datalog.expressions.Op;
@@ -51,12 +54,12 @@ public void testFamily() throws Error {
5154
new ArrayList<>());
5255

5356
System.out.println("testing r1: " + syms.formatRule(r1));
54-
FactSet query_rule_result = w.queryRule(r1, (long) 0, new TrustedOrigins(0), syms);
57+
FactSet queryRuleResult = w.queryRule(r1, (long) 0, new TrustedOrigins(0), syms);
5558
System.out.println(
5659
"grandparents query_rules: ["
5760
+ String.join(
5861
", ",
59-
query_rule_result.stream()
62+
queryRuleResult.stream()
6063
.map((f) -> syms.formatFact(f))
6164
.collect(Collectors.toList()))
6265
+ "]");
@@ -593,7 +596,7 @@ public void testSet() throws Error {
593596
new Term.Set(
594597
new HashSet<>(
595598
Arrays.asList(
596-
new Term.Integer(0l), new Term.Integer(1l))))),
599+
new Term.Integer(0L), new Term.Integer(1L))))),
597600
new Op.Value(new Term.Variable(syms.insert("int"))),
598601
new Op.Binary(Op.BinaryOp.Contains))))));
599602
System.out.println("testing r1: " + syms.formatRule(r1));
@@ -610,8 +613,8 @@ public void testSet() throws Error {
610613
new Fact(new Predicate(int_set, Arrays.asList(abc, syms.add("test")))))));
611614
assertEquals(expected, res);
612615

613-
final long abc_sym_id = syms.insert("abc");
614-
final long ghi_sym_id = syms.insert("ghi");
616+
final long abcSymId = syms.insert("abc");
617+
final long ghiSymId = syms.insert("ghi");
615618

616619
final Rule r2 =
617620
new Rule(
@@ -636,7 +639,7 @@ public void testSet() throws Error {
636639
new Term.Set(
637640
new HashSet<>(
638641
Arrays.asList(
639-
new Term.Str(abc_sym_id), new Term.Str(ghi_sym_id))))),
642+
new Term.Str(abcSymId), new Term.Str(ghiSymId))))),
640643
new Op.Value(new Term.Variable(syms.insert("sym"))),
641644
new Op.Binary(Op.BinaryOp.Contains),
642645
new Op.Unary(Op.UnaryOp.Negate))))));

src/test/java/org/biscuitsec/biscuit/token/AuthorizerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public void testAuthorizerPolicy() throws Parser {
4141
}
4242

4343
@Test
44-
public void testPuttingSomeFactsInABiscuitAndGettingThemBackOutAgain() throws Exception {
44+
public void testPuttingSomeFactsInBiscuitAndGettingThemBackOutAgain() throws Exception {
4545

4646
KeyPair keypair = KeyPair.generate(Schema.PublicKey.Algorithm.Ed25519, new SecureRandom());
4747

0 commit comments

Comments
 (0)