Skip to content

Commit

Permalink
compare the authorizer world after execution
Browse files Browse the repository at this point in the history
  • Loading branch information
Geal committed Jan 5, 2024
1 parent a65249a commit b5f61be
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 4 deletions.
32 changes: 30 additions & 2 deletions src/main/java/org/biscuitsec/biscuit/token/Authorizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import io.vavr.control.Either;
import io.vavr.control.Option;
import org.biscuitsec.biscuit.datalog.Scope;
import org.biscuitsec.biscuit.token.builder.Check;
import org.biscuitsec.biscuit.token.builder.Fact;
import org.biscuitsec.biscuit.token.builder.Term;
import org.biscuitsec.biscuit.token.builder.parser.Parser;

Expand Down Expand Up @@ -592,7 +594,6 @@ public Long authorize(RunLimits limits) throws Error {
}

public String print_world() {
//FIXME
StringBuilder facts = new StringBuilder();
for(Map.Entry<Origin, HashSet<org.biscuitsec.biscuit.datalog.Fact>> entry: this.world.facts().facts().entrySet()) {
facts.append("\n\t\t"+entry.getKey()+":");
Expand Down Expand Up @@ -633,4 +634,31 @@ public String print_world() {
String.join(",\n\t\t", checks) +
"\n\t]\n}";
}
}

public List<Fact> facts() {
return this.world.facts().stream()
.map((f) -> org.biscuitsec.biscuit.token.builder.Fact.convert_from(f, this.symbols))
.collect(Collectors.toList());
}

public List<org.biscuitsec.biscuit.token.builder.Rule> rules() {
return this.world.rules().stream()
.map((r) -> org.biscuitsec.biscuit.token.builder.Rule.convert_from(r, this.symbols))
.collect(Collectors.toList());
}

public List<Check> checks() {
List<Check> checks = new ArrayList<>(this.checks);
for(List<org.biscuitsec.biscuit.datalog.Check> blockChecks: this.token_checks) {
for(org.biscuitsec.biscuit.datalog.Check check: blockChecks) {
checks.add(Check.convert_from(check, this.symbols));
}
}

return checks;
}

public List<Policy> policies() {
return this.policies;
}
}
67 changes: 65 additions & 2 deletions src/test/java/org/biscuitsec/biscuit/token/SamplesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.time.Duration;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -49,7 +51,6 @@ DynamicTest process_testcase(final TestCase testCase, final PublicKey publicKey,
String validationName = validationEntry.getKey();
JsonObject validation = validationEntry.getValue().getAsJsonObject();

World world = new Gson().fromJson(validation, World.class);
JsonObject expected_result = validation.getAsJsonObject("result");
String[] authorizer_facts = validation.getAsJsonPrimitive("authorizer_code").getAsString().split(";");
Either<Throwable, Long> res = Try.of(() -> {
Expand Down Expand Up @@ -83,7 +84,52 @@ DynamicTest process_testcase(final TestCase testCase, final PublicKey publicKey,
}
authorizer.allow(); // TODO manage the policies
System.out.println(authorizer.print_world());
return authorizer.authorize(runLimits);
try {
Long authorizeResult = authorizer.authorize(runLimits);

if(validation.has("world")) {
World world = new Gson().fromJson(validation.get("world").getAsJsonObject(), World.class);
World authorizerWorld = new World(
authorizer.facts().stream().map(f -> f.toString()).collect(Collectors.toList()),
authorizer.rules().stream().map(r -> r.toString()).collect(Collectors.toList()),
authorizer.checks().stream().map(c -> c.toString()).collect(Collectors.toList()),
authorizer.policies().stream().map(p -> p.toString()).collect(Collectors.toList())
);
Collections.sort(authorizerWorld.facts);
Collections.sort(authorizerWorld.rules);
Collections.sort(authorizerWorld.checks);
Collections.sort(authorizerWorld.policies);

assertEquals(world.facts.size(), authorizerWorld.facts.size());
for (int i = 0; i < world.facts.size(); i++) {
assertEquals(world.facts.get(i), authorizerWorld.facts.get(i));
}
}

return authorizeResult;
} catch (Exception e) {

if(validation.has("world")) {
World world = new Gson().fromJson(validation.get("world").getAsJsonObject(), World.class);
World authorizerWorld = new World(
authorizer.facts().stream().map(f -> f.toString()).collect(Collectors.toList()),
authorizer.rules().stream().map(r -> r.toString()).collect(Collectors.toList()),
authorizer.checks().stream().map(c -> c.toString()).collect(Collectors.toList()),
authorizer.policies().stream().map(p -> p.toString()).collect(Collectors.toList())
);
Collections.sort(authorizerWorld.facts);
Collections.sort(authorizerWorld.rules);
Collections.sort(authorizerWorld.checks);
Collections.sort(authorizerWorld.policies);

assertEquals(world.facts.size(), authorizerWorld.facts.size());
for (int i = 0; i < world.facts.size(); i++) {
assertEquals(world.facts.get(i), authorizerWorld.facts.get(i));
}
}

throw e;
}
}).toEither();
if (res.isLeft()) {
if(res.getLeft() instanceof Error) {
Expand Down Expand Up @@ -212,5 +258,22 @@ class World {
List<String> rules;
List<String> checks;
List<String> policies;

public World(List<String> facts, List<String> rules, List<String> checks, List<String> policies) {
this.facts = facts;
this.rules = rules;
this.checks = checks;
this.policies = policies;
}

@Override
public String toString() {
return "World{" +
"facts=" + facts +
", rules=" + rules +
", checks=" + checks +
", policies=" + policies +
'}';
}
}
}

0 comments on commit b5f61be

Please sign in to comment.