Skip to content

Commit

Permalink
test rules and checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Geal committed Jan 8, 2024
1 parent 0870f92 commit 9932d30
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 52 deletions.
12 changes: 9 additions & 3 deletions src/main/java/org/biscuitsec/biscuit/token/Authorizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -640,13 +640,17 @@ public RuleSet rules() {

public List<Tuple2<Long, List<Check>>> checks() {
List<Tuple2<Long, List<Check>>> allChecks = new ArrayList<>();
allChecks.add(new Tuple2(null, this.checks));
if(!this.checks.isEmpty()) {
allChecks.add(new Tuple2(Long.MAX_VALUE, this.checks));
}

List<Check> authorityChecks = new ArrayList<>();
for(org.biscuitsec.biscuit.datalog.Check check: this.token.authority.checks) {
authorityChecks.add(Check.convert_from(check, this.token.symbols));
}
allChecks.add(new Tuple2((long)0, authorityChecks));
if(!authorityChecks.isEmpty()) {
allChecks.add(new Tuple2((long) 0, authorityChecks));
}

long count = 1;
for(Block block: this.token.blocks) {
Expand All @@ -662,7 +666,9 @@ public List<Tuple2<Long, List<Check>>> checks() {
blockChecks.add(Check.convert_from(check, token.symbols));
}
}
allChecks.add(new Tuple2(count, blockChecks));
if(!blockChecks.isEmpty()) {
allChecks.add(new Tuple2(count, blockChecks));
}
count += 1;
}

Expand Down
101 changes: 52 additions & 49 deletions src/test/java/org/biscuitsec/biscuit/token/SamplesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,61 +88,29 @@ DynamicTest process_testcase(final TestCase testCase, final PublicKey publicKey,

if(validation.has("world") && !validation.get("world").isJsonNull()) {
World world = new Gson().fromJson(validation.get("world").getAsJsonObject(), World.class);
System.out.println("will fix origin");
world.fixOrigin();
System.out.println("fixed origin");

World authorizerWorld = new World(authorizer);

System.out.println("validation world"+world);
System.out.println("authorizer world"+authorizerWorld);

assertEquals(world.factMap(), authorizerWorld.factMap());
assertEquals(world.rules, authorizerWorld.rules);
assertEquals(world.checks, authorizerWorld.checks);
assertEquals(world.policies, authorizerWorld.policies);


assertEquals(world.rules.size(), authorizerWorld.rules.size());
for (int i = 0; i < world.rules.size(); i++) {
assertEquals(world.rules.get(i), authorizerWorld.rules.get(i));
}
/*assertEquals(world.checks.size(), authorizerWorld.checks.size());
for (int i = 0; i < world.checks.size(); i++) {
assertEquals(world.checks.get(i), authorizerWorld.checks.get(i));
}
assertEquals(world.policies.size(), authorizerWorld.policies.size());
for (int i = 0; i < world.policies.size(); i++) {
assertEquals(world.policies.get(i), authorizerWorld.policies.get(i));
}*/
}

return authorizeResult;
} catch (Exception e) {

if(validation.has("world") && !validation.get("world").isJsonNull()) {
World world = new Gson().fromJson(validation.get("world").getAsJsonObject(), World.class);
World authorizerWorld = new World(authorizer);
System.out.println("will fix origin");
world.fixOrigin();
System.out.println("fixed origin");


System.out.println(world.facts);
System.out.println(authorizerWorld.facts);
World authorizerWorld = new World(authorizer);
assertEquals(world.factMap(), authorizerWorld.factMap());

System.out.println(world.rules);
System.out.println(authorizerWorld.rules);

assertEquals(world.rules.size(), authorizerWorld.rules.size());
for (int i = 0; i < world.rules.size(); i++) {
assertEquals(world.rules.get(i), authorizerWorld.rules.get(i));
}
/*assertEquals(world.checks.size(), authorizerWorld.checks.size());
for (int i = 0; i < world.checks.size(); i++) {
assertEquals(world.checks.get(i), authorizerWorld.checks.get(i));
}
assertEquals(world.policies.size(), authorizerWorld.policies.size());
for (int i = 0; i < world.policies.size(); i++) {
assertEquals(world.policies.get(i), authorizerWorld.policies.get(i));
}*/
assertEquals(world.rules, authorizerWorld.rules);
assertEquals(world.checks, authorizerWorld.checks);
assertEquals(world.policies, authorizerWorld.policies);
}

throw e;
Expand Down Expand Up @@ -336,12 +304,22 @@ public World(Authorizer authorizer) {

this.checks = checksets;
this.policies = authorizer.policies().stream().map(p -> p.toString()).collect(Collectors.toList());
Collections.sort(this.rules);
Collections.sort(this.checks);
}

public void fixOrigin() {
for(FactSet f: this.facts) {
f.fixOrigin();
}
for(RuleSet r: this.rules) {
r.fixOrigin();
}
Collections.sort(this.rules);
for(CheckSet c: this.checks) {
c.fixOrigin();
}
Collections.sort(this.checks);
}

public HashMap<List<Long>, List<String>> factMap() {
Expand Down Expand Up @@ -369,23 +347,18 @@ class FactSet {
List<String> facts;

public FactSet(List<Long> origin, List<String> facts) {
System.out.println("creating new factset with origin "+origin);
this.origin = origin;
this.facts = facts;
}

// JSON cannot represent Long.MAX_VALUE so it is stored as null, fix the origin list
public void fixOrigin() {
System.out.println("fixing origins: "+this.origin);
for(int i = 0; i < this.origin.size(); i++) {
if (this.origin.get(i) == null) {
this.origin.set(i, Long.MAX_VALUE);
}
}
Collections.sort(this.origin);

System.out.println("->: "+this.origin);

}

@Override
Expand Down Expand Up @@ -424,6 +397,12 @@ public RuleSet(Long origin, List<String> rules) {
this.rules = rules;
}

public void fixOrigin() {
if (this.origin == null || this.origin == -1) {
this.origin = Long.MAX_VALUE;
}
}

@Override
public int compareTo(RuleSet ruleSet) {
// we only compare origin to sort the list of rulesets
Expand Down Expand Up @@ -464,7 +443,7 @@ public String toString() {
}
}

class CheckSet implements Comparable<RuleSet> {
class CheckSet implements Comparable<CheckSet> {
Long origin;
List<String> checks;

Expand All @@ -478,19 +457,43 @@ public CheckSet(List<String> checks) {
this.checks = checks;
}

public void fixOrigin() {
if (this.origin == null || this.origin == -1) {
this.origin = Long.MAX_VALUE;
}
}

@Override
public int compareTo(RuleSet ruleSet) {
public int compareTo(CheckSet checkSet) {
// we only compare origin to sort the list of checksets
// there's only one of each origin so we don't need to compare the list of rules
if(this.origin == null) {
return -1;
} else if (ruleSet.origin == null) {
} else if (checkSet.origin == null) {
return 1;
} else {
return this.origin.compareTo(ruleSet.origin);
return this.origin.compareTo(checkSet.origin);
}
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

CheckSet checkSet = (CheckSet) o;

if (!Objects.equals(origin, checkSet.origin)) return false;
return Objects.equals(checks, checkSet.checks);
}

@Override
public int hashCode() {
int result = origin != null ? origin.hashCode() : 0;
result = 31 * result + (checks != null ? checks.hashCode() : 0);
return result;
}

@Override
public String toString() {
return "CheckSet{" +
Expand Down

0 comments on commit 9932d30

Please sign in to comment.