Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update sample tests #102

Merged
merged 13 commits into from
Jun 15, 2024
47 changes: 46 additions & 1 deletion src/main/java/org/biscuitsec/biscuit/token/Block.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import biscuit.format.schema.Schema;
import org.biscuitsec.biscuit.crypto.PublicKey;
import org.biscuitsec.biscuit.datalog.expressions.Expression;
import org.biscuitsec.biscuit.datalog.expressions.Op;
import org.biscuitsec.biscuit.error.Error;
import org.biscuitsec.biscuit.datalog.*;
import org.biscuitsec.biscuit.token.format.SerializedBiscuit;
Expand All @@ -13,6 +15,7 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static io.vavr.API.Left;
import static io.vavr.API.Right;
Expand Down Expand Up @@ -155,10 +158,52 @@ public Schema.Block serialize() {
b.addPublicKeys(pk.serialize());
}

b.setVersion(SerializedBiscuit.MAX_SCHEMA_VERSION);
b.setVersion(getSchemaVersion());
return b.build();
}

int getSchemaVersion() {
boolean containsScopes = !this.scopes.isEmpty();
boolean containsCheckAll = false;
boolean containsV4 = false;

for (Rule r: this.rules) {
containsScopes |= !r.scopes().isEmpty();
for(Expression e: r.expressions()) {
containsV4 |= containsV4Op(e);
}
}
for(Check c: this.checks) {
containsCheckAll |= c.kind() == Check.Kind.All;

for (Rule q: c.queries()) {
containsScopes |= !q.scopes().isEmpty();
for(Expression e: q.expressions()) {
containsV4 |= containsV4Op(e);
}
}
}

if(containsScopes || containsCheckAll || containsV4) {
return SerializedBiscuit.MAX_SCHEMA_VERSION;
} else {
return SerializedBiscuit.MIN_SCHEMA_VERSION;
}
}

boolean containsV4Op(Expression e) {
for (Op op: e.getOps()) {
if (op instanceof Op.Binary) {
Op.BinaryOp o = ((Op.Binary) op).getOp();
if (o == Op.BinaryOp.BitwiseAnd || o == Op.BinaryOp.BitwiseOr || o == Op.BinaryOp.BitwiseXor || o == Op.BinaryOp.NotEqual) {
return true;
}
}
}

return false;
}

/**
* Deserializes a block from its Protobuf representation
*
Expand Down
135 changes: 135 additions & 0 deletions src/main/java/org/biscuitsec/biscuit/token/builder/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,102 @@
import java.util.function.Function;

public class Parser {
public static Either<Map<Integer, List<Error>>, Block> datalog(long index, SymbolTable baseSymbols, String s) {
return datalog(index, baseSymbols, null, s);
}

/**
* Takes a datalog string with <code>\n</code> as datalog line separator. It tries to parse
* each line using fact, rule, check and scope sequentially.
*
* If one succeeds it returns Right(Block)
* else it returns a Map[lineNumber, List[Error]]
*
* @param index block index
* @param baseSymbols symbols table
* @param blockSymbols block's custom symbols table (added to baseSymbols)
* @param s datalog string to parse
* @return Either<Map<Integer, List<Error>>, Block>
*/
public static Either<Map<Integer, List<Error>>, Block> datalog(long index, SymbolTable baseSymbols, SymbolTable blockSymbols, String s) {
Block blockBuilder = new Block(index, baseSymbols);

// empty block code
if (s.isEmpty()) {
return Either.right(blockBuilder);
}

if (blockSymbols != null) {
blockSymbols.symbols.forEach(blockBuilder::addSymbol);
}

Map<Integer, List<Error>> errors = new HashMap<>();

s = removeCommentsAndWhitespaces(s);
String[] codeLines = s.split(";");

Stream.of(codeLines)
.zipWithIndex()
.forEach(indexedLine -> {
String code = indexedLine._1.strip();

if (!code.isEmpty()) {
int lineNumber = indexedLine._2;
List<Error> lineErrors = new ArrayList<>();

boolean parsed = false;
parsed = rule(code).fold(e -> {
lineErrors.add(e);
return false;
}, r -> {
blockBuilder.add_rule(r._2);
return true;
});

if (!parsed) {
parsed = scope(code).fold(e -> {
lineErrors.add(e);
return false;
}, r -> {
blockBuilder.add_scope(r._2);
return true;
});
}

if (!parsed) {
parsed = fact(code).fold(e -> {
lineErrors.add(e);
return false;
}, r -> {
blockBuilder.add_fact(r._2);
return true;
});
}

if (!parsed) {
parsed = check(code).fold(e -> {
lineErrors.add(e);
return false;
}, r -> {
blockBuilder.add_check(r._2);
return true;
});
}

if (!parsed) {
lineErrors.forEach(System.out::println);
errors.put(lineNumber, lineErrors);
}
}
});

if (!errors.isEmpty()) {
return Either.left(errors);
}

return Either.right(blockBuilder);
}

public static Either<Error, Tuple2<String, Fact>> fact(String s) {
Either<Error, Tuple2<String, Predicate>> res = fact_predicate(s);
if (res.isLeft()) {
Expand Down Expand Up @@ -671,4 +767,43 @@ public static Tuple2<String, String> take_while(String s, Function<Character, Bo

return new Tuple2<>(s.substring(0, index), s.substring(index));
}

public static String removeCommentsAndWhitespaces(String s) {
s = removeComments(s);
s = s.replace("\n", "").replace("\\\"", "\"").strip();
return s;
}

public static String removeComments(String str) {
StringBuilder result = new StringBuilder();
String remaining = str;

while (!remaining.isEmpty()) {
remaining = space(remaining); // Skip leading whitespace
if (remaining.startsWith("/*")) {
// Find the end of the multiline comment
remaining = remaining.substring(2); // Skip "/*"
String finalRemaining = remaining;
Tuple2<String, String> split = take_while(remaining, c -> !finalRemaining.startsWith("*/"));
remaining = split._2.length() > 2 ? split._2.substring(2) : ""; // Skip "*/"
} else if (remaining.startsWith("//")) {
// Find the end of the single-line comment
remaining = remaining.substring(2); // Skip "//"
Tuple2<String, String> split = take_while(remaining, c -> c != '\n' && c != '\r');
remaining = split._2;
if (!remaining.isEmpty()) {
result.append(remaining.charAt(0)); // Preserve line break
remaining = remaining.substring(1);
}
} else {
// Take non-comment text until the next comment or end of string
String finalRemaining = remaining;
Tuple2<String, String> split = take_while(remaining, c -> !finalRemaining.startsWith("/*") && !finalRemaining.startsWith("//"));
result.append(split._1);
remaining = split._2;
}
}

return result.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -398,4 +398,95 @@ void testParens() throws org.biscuitsec.biscuit.error.Error.Execution {
assertEquals(new org.biscuitsec.biscuit.datalog.Term.Integer(9), value2);
assertEquals("(1 + 2) * 3", ex2.print(s2).get());
}

@Test
void testDatalogSucceeds() throws org.biscuitsec.biscuit.error.Error.Parser {
SymbolTable symbols = Biscuit.default_symbol_table();

String l1 = "fact1(1, 2)";
String l2 = "fact2(\"2\")";
String l3 = "rule1(2) <- fact2(\"2\")";
String l4 = "check if rule1(2)";
String toParse = String.join(";", Arrays.asList(l1, l2, l3, l4));

Either<Map<Integer, List<Error>>, Block> output = Parser.datalog(1, symbols, toParse);
assertTrue(output.isRight());

Block validBlock = new Block(1, symbols);
validBlock.add_fact(l1);
validBlock.add_fact(l2);
validBlock.add_rule(l3);
validBlock.add_check(l4);

output.forEach(block ->
assertArrayEquals(block.build().to_bytes().get(), validBlock.build().to_bytes().get())
);
}

@Test
void testDatalogSucceedsArrays() throws org.biscuitsec.biscuit.error.Error.Parser {
SymbolTable symbols = Biscuit.default_symbol_table();

String l1 = "check if [2, 3].union([2])";
String toParse = String.join(";", List.of(l1));

Either<Map<Integer, List<Error>>, Block> output = Parser.datalog(1, symbols, toParse);
assertTrue(output.isRight());

Block validBlock = new Block(1, symbols);
validBlock.add_check(l1);

output.forEach(block ->
assertArrayEquals(block.build().to_bytes().get(), validBlock.build().to_bytes().get())
);
}

@Test
void testDatalogSucceedsArraysContains() throws org.biscuitsec.biscuit.error.Error.Parser {
SymbolTable symbols = Biscuit.default_symbol_table();

String l1 = "check if [2019-12-04T09:46:41Z, 2020-12-04T09:46:41Z].contains(2020-12-04T09:46:41Z)";
String toParse = String.join(";", List.of(l1));

Either<Map<Integer, List<Error>>, Block> output = Parser.datalog(1, symbols, toParse);
assertTrue(output.isRight());

Block validBlock = new Block(1, symbols);
validBlock.add_check(l1);

output.forEach(block ->
assertArrayEquals(block.build().to_bytes().get(), validBlock.build().to_bytes().get())
);
}

@Test
void testDatalogFailed() {
SymbolTable symbols = Biscuit.default_symbol_table();

String l1 = "fact(1)";
String l2 = "check fact(1)"; // typo missing "if"
String toParse = String.join(";", Arrays.asList(l1, l2));

Either<Map<Integer, List<Error>>, Block> output = Parser.datalog(1, symbols, toParse);
assertTrue(output.isLeft());
}

@Test
void testDatalogRemoveComment() throws org.biscuitsec.biscuit.error.Error.Parser {
SymbolTable symbols = Biscuit.default_symbol_table();

String l0 = "// test comment";
String l1 = "fact1(1, 2);";
String l2 = "fact2(\"2\");";
String l3 = "rule1(2) <- fact2(\"2\");";
String l4 = "// another comment";
String l5 = "/* test multiline";
String l6 = "comment */ check if rule1(2);";
String l7 = " /* another multiline";
String l8 = "comment */";
String toParse = String.join("", Arrays.asList(l0, l1, l2, l3, l4, l5, l6, l7, l8));

Either<Map<Integer, List<Error>>, Block> output = Parser.datalog(1, symbols, toParse);
assertTrue(output.isRight());
}
}
Loading
Loading