Skip to content
This repository has been archived by the owner on Dec 3, 2023. It is now read-only.

fix: verify correctness of map -> list equality #174

Merged
merged 3 commits into from Mar 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 13 additions & 3 deletions google-cloud-core/src/main/java/com/google/cloud/Policy.java
Expand Up @@ -260,7 +260,7 @@ public final Builder addIdentity(Role role, Identity first, Identity... others)
Binding binding = bindingsList.get(i);
if (binding.getRole().equals(role.getValue())) {
Binding.Builder bindingBuilder = binding.toBuilder();
ImmutableList.Builder<String> membersBuilder = ImmutableList.builder();
ImmutableSet.Builder<String> membersBuilder = ImmutableSet.builder();
membersBuilder.addAll(binding.getMembers());
membersBuilder.add(first.strValue());
for (Identity identity : others) {
Expand All @@ -273,7 +273,7 @@ public final Builder addIdentity(Role role, Identity first, Identity... others)
}
// Binding does not yet exist.
Binding.Builder bindingBuilder = Binding.newBuilder().setRole(role.getValue());
ImmutableList.Builder<String> membersBuilder = ImmutableList.builder();
ImmutableSet.Builder<String> membersBuilder = ImmutableSet.builder();
membersBuilder.add(first.strValue());
for (Identity identity : others) {
membersBuilder.add(identity.strValue());
Expand Down Expand Up @@ -432,9 +432,19 @@ public boolean equals(Object obj) {
return false;
}
Policy other = (Policy) obj;
if (!bindingsList.equals(other.getBindingsList())) {
if (bindingsList == null && other.getBindingsList() == null) {
return true;
}
if ((bindingsList == null && other.getBindingsList() != null)
|| bindingsList != null && other.getBindingsList() == null
|| bindingsList.size() != other.getBindingsList().size()) {
return false;
}
for (Binding binding : bindingsList) {
if (!other.getBindingsList().contains(binding)) {
return false;
}
}
return Objects.equals(etag, other.getEtag()) && version == other.getVersion();
}

Expand Down
30 changes: 30 additions & 0 deletions google-cloud-core/src/test/java/com/google/cloud/PolicyTest.java
Expand Up @@ -104,6 +104,36 @@ public void testBuilder() {
assertEquals(0, policy.getVersion());
}

@Test
public void testPolicyOrderShouldNotMatter() {
Role role1 = Role.of("role1");
Identity identity1 = Identity.user("user1@example.com");
Role role2 = Role.of("role2");
Identity identity2 = Identity.user("user2@example.com");
Policy policy1 =
Policy.newBuilder().addIdentity(role1, identity1).addIdentity(role2, identity2).build();
Policy policy2 =
Policy.newBuilder().addIdentity(role2, identity2).addIdentity(role1, identity1).build();
assertEquals(policy1, policy2);
}

@Test
public void testPolicyMultipleAddIdentitiesShouldNotMatter() {
Role role1 = Role.of("role1");
Identity identity1 = Identity.user("user1@example.com");
Role role2 = Role.of("role2");
Identity identity2 = Identity.user("user2@example.com");
Policy policy1 =
Policy.newBuilder()
.addIdentity(role1, identity1)
.addIdentity(role2, identity2)
.addIdentity(role2, identity2)
.build();
Policy policy2 =
Policy.newBuilder().addIdentity(role2, identity2).addIdentity(role1, identity1).build();
assertEquals(policy1, policy2);
}

@Test
public void testIllegalPolicies() {
try {
Expand Down