Skip to content

Commit

Permalink
Updating test to not generate duplicate euids
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronjeline committed Oct 17, 2023
1 parent 828529b commit f5267b7
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,43 +18,61 @@

import com.cedarpolicy.model.slice.Entity;
import com.cedarpolicy.serializer.JsonEUID;
import com.cedarpolicy.value.EntityUID;
import com.cedarpolicy.value.Value;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import net.jqwik.api.Arbitraries;

/** Generate random actions for testing. */
public final class ActionGen {
/** Generate a random Action Group. */
public static List<Entity> getEntities() {
List<Entity> actions = new ArrayList<>();
String actionType = "Action";
String actionId = Utils.strings();
String actionEUID = actionType+"::\"" + actionId + "\"";
Map<String, Value> actionAttributes = new HashMap<>();
Set<JsonEUID> actionParents = new HashSet<>();
Entity e = new Entity(new JsonEUID(actionType, actionId), actionAttributes, actionParents);
actions.add(e);
int count = Arbitraries.integers().between(10, 100).sample();

for (int i = 0; i < count; i++) {
actionId = Utils.strings();
actionEUID = "Action::\"" + actionId + "\"";
if (!e.getEUID().toString().equals(actionEUID)) {
e.parentsEUIDs.add(new JsonEUID(actionType, actionId));
public final class EntityGen {

private List<String> ids;
private String type;

public EntityGen(String type) {
this.type = type;
ids = new ArrayList<>();
}

private JsonEUID arbitraryEntityId() {
// Generate Id's until we find one not in the generated set
String id;
while (true) {
id = Utils.strings();
if (isUnique(id)) {
break;
}
actionAttributes = new HashMap<>();
actionParents = new HashSet<>();
actions.add(new Entity(new JsonEUID(actionType, actionId), actionAttributes, actionParents));
}
return actions;
this.ids.add(id);
return new JsonEUID(type, id);
}

private boolean isUnique(String id) {
return !ids.contains(id);
}

private ActionGen() {
throw new IllegalStateException("Utility class");
// Return an arbitrary action w/ no attributes or parents
public Entity arbitraryEntity() {
return new Entity(arbitraryEntityId(), new HashMap<>(), new HashSet<>());
}

public List<Entity> arbitraryEntities() {
List<Entity> actions = new ArrayList<>();
actions.add(arbitraryEntity());

var count = Arbitraries.integers().between(10, 100).sample();

for (int i = 0; i < count; i++ ) {
actions.add(arbitraryEntity());
}


return actions;
}
}
42 changes: 19 additions & 23 deletions CedarJava/src/test/java/com/cedarpolicy/pbt/ParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,41 +182,34 @@ void testActionIn(@ForAll String ids) {
/*
* Generate a random principal
*/
String principalType = "User";
String principalId = Utils.strings();
String principal = principalType+"::\"" + principalId + "\"";
Map<String, Value> principalAttributes = new HashMap<>();
Set<JsonEUID> principalParents = new HashSet<>();
entities.add(new Entity(new JsonEUID(principalType, principalId), principalAttributes, principalParents));
var principal = new EntityGen("User").arbitraryEntity();
entities.add(principal);

/*
* Generate a random Action
*/
List<Entity> actions = ActionGen.getEntities();
var gen = new EntityGen("Action");
List<Entity> actions = gen.arbitraryEntities();
entities.addAll(actions);
String action = actions.get(0).getEUID().toString();
var action = actions.get(0);
/*
* Generate a random Resource
*/
String resourceType = "Resource";
String resourceId = Utils.strings();
String resource = resourceType+"::\"" + resourceId + "\"";
Map<String, Value> resourceAttributes = new HashMap<>();
Set<JsonEUID> resourceParents = new HashSet<>();
entities.add(new Entity(new JsonEUID(resourceType, resourceId), resourceAttributes, resourceParents));
var resource = new EntityGen("resource").arbitraryEntity();
entities.add(resource);
/*
* Generate a universal permit policy
*/
String p =
"permit(\n"
+ "principal=="
+ principal
+ principal.getEUID()
+ ",\n"
+ "action=="
+ action
+ action.getEUID()
+ ",\n"
+ "resource=="
+ resource
+ resource.getEUID()
+ "\n"
+ ");";
Policy policy = new Policy(p, ids);
Expand All @@ -226,24 +219,27 @@ void testActionIn(@ForAll String ids) {
Map<String, Value> currentContext = new HashMap<>();
AuthorizationRequest request =
new AuthorizationRequest(
principal, action, resource, currentContext);
principal.getEUID().toString(),
action.getEUID().toString(),
resource.getEUID().toString(),
currentContext);
AuthorizationEngine authEngine = new BasicAuthorizationEngine();
AuthorizationResponse response =
Assertions.assertDoesNotThrow(() -> authEngine.isAuthorized(request, slice));

Assertions.assertTrue(response.isAllowed());
String actionList =
"[" + actions.stream().map(x -> x.getEUID().toString()).collect(Collectors.joining(",")) + "]";
"[" + actions.stream().map(a -> a.getEUID().toString()).collect(Collectors.joining(",")) + "]";
String p2 =
"permit(\n"
+ "principal=="
+ principal
+ principal.getEUID()
+ ",\n"
+ "action in"
+ actionList
+ ",\n"
+ "resource=="
+ resource
+ resource.getEUID()
+ "\n"
+ ");";

Expand All @@ -253,10 +249,10 @@ void testActionIn(@ForAll String ids) {
Slice slice2 = new BasicSlice(policies, entities);
Map<String, Value> currentContext2 = new HashMap<>();
int index = Arbitraries.integers().between(0, actions.size() - 1).sample();
action = actions.get(index).getEUID().toString();
action = actions.get(index);
AuthorizationRequest request2 =
new AuthorizationRequest(
principal, action, resource, currentContext2);
principal.getEUID().toString(), action.getEUID().toString(), resource.getEUID().toString(), currentContext2);
AuthorizationResponse response2 =
Assertions.assertDoesNotThrow(() -> authEngine.isAuthorized(request2, slice2));
Assertions.assertTrue(response2.isAllowed());
Expand Down

0 comments on commit f5267b7

Please sign in to comment.