use of com.quorum.tessera.enclave.PrivacyGroup in project tessera by ConsenSys.
the class PrivacyGroupManagerImplTest method testCreateLegacyPrivacyGroup.
@Test
public void testCreateLegacyPrivacyGroup() {
final List<PublicKey> members = List.of(mock(PublicKey.class), mock(PublicKey.class));
when(privacyGroupUtil.generateId(anyList())).thenReturn("generatedId".getBytes());
when(privacyGroupUtil.generateLookupId(anyList())).thenReturn("lookup".getBytes());
when(privacyGroupUtil.encode(any())).thenReturn("encoded".getBytes());
when(privacyGroupDAO.retrieve("generatedId".getBytes())).thenReturn(Optional.empty());
final PrivacyGroup privacyGroup = privacyGroupManager.createLegacyPrivacyGroup(localKey, members);
// Verify entity being saved has the correct values
ArgumentCaptor<PrivacyGroupEntity> argCaptor = ArgumentCaptor.forClass(PrivacyGroupEntity.class);
verify(privacyGroupDAO).retrieveOrSave(argCaptor.capture());
PrivacyGroupEntity savedEntity = argCaptor.getValue();
assertThat(savedEntity).isNotNull();
assertThat(savedEntity.getId()).isEqualTo("generatedId".getBytes());
assertThat(savedEntity.getLookupId()).isEqualTo("lookup".getBytes());
assertThat(savedEntity.getData()).isEqualTo("encoded".getBytes());
// Verify generated privacy group has the correct values
assertThat(privacyGroup).isNotNull();
assertThat(privacyGroup.getId().getBytes()).isEqualTo("generatedId".getBytes());
assertThat(privacyGroup.getName()).isEqualTo("legacy");
assertThat(privacyGroup.getDescription()).isEqualTo("Privacy groups to support the creation of groups by privateFor and privateFrom");
assertThat(privacyGroup.getMembers()).containsAll(members).contains(localKey);
assertThat(privacyGroup.getType()).isEqualTo(PrivacyGroup.Type.LEGACY);
assertThat(privacyGroup.getState()).isEqualTo(PrivacyGroup.State.ACTIVE);
verify(privacyGroupDAO).retrieveOrSave(any());
}
use of com.quorum.tessera.enclave.PrivacyGroup in project tessera by ConsenSys.
the class ResidentGroupHandlerImplTest method noGroupConfigButExistedInDb.
@Test
public void noGroupConfigButExistedInDb() {
PublicKey localKey1 = mock(PublicKey.class);
PublicKey localKey2 = mock(PublicKey.class);
when(privacyGroupManager.getManagedKeys()).thenReturn(Set.of(localKey1, localKey2));
PrivacyGroup rg = mock(PrivacyGroup.class);
when(rg.getMembers()).thenReturn(List.of(localKey1, localKey2));
when(rg.getId()).thenReturn(PrivacyGroup.Id.fromBytes("id".getBytes()));
when(privacyGroupManager.findPrivacyGroupByType(eq(PrivacyGroup.Type.RESIDENT))).thenReturn(List.of(rg));
residentGroupHandler.onCreate(mock(Config.class));
verify(privacyGroupManager).getManagedKeys();
verify(privacyGroupManager).findPrivacyGroupByType(PrivacyGroup.Type.RESIDENT);
}
use of com.quorum.tessera.enclave.PrivacyGroup in project tessera by ConsenSys.
the class ResidentGroupHandlerImplTest method keyExistedInADifferentGroupInDb.
@Test
public void keyExistedInADifferentGroupInDb() {
ResidentGroup rg1 = new ResidentGroup();
rg1.setMembers(List.of(PublicKey.from("m1".getBytes()).encodeToBase64()));
rg1.setName("rg1");
ResidentGroup rg2 = new ResidentGroup();
rg2.setMembers(List.of(PublicKey.from("m2".getBytes()).encodeToBase64()));
rg2.setName("rg2");
when(privacyGroupManager.getManagedKeys()).thenReturn(Set.of(PublicKey.from("m1".getBytes()), PublicKey.from("m2".getBytes())));
Config config = mock(Config.class);
when(config.getResidentGroups()).thenReturn(List.of(rg1, rg2));
final PrivacyGroup existedGroup = mock(PrivacyGroup.class);
when(existedGroup.getMembers()).thenReturn(List.of(PublicKey.from("m2".getBytes())));
when(existedGroup.getId()).thenReturn(PrivacyGroup.Id.fromBytes("otherGroup".getBytes()));
when(privacyGroupManager.findPrivacyGroupByType(eq(PrivacyGroup.Type.RESIDENT))).thenReturn(List.of(existedGroup));
assertThatThrownBy(() -> residentGroupHandler.onCreate(config)).isInstanceOf(PrivacyViolationException.class).hasMessageContaining("Key cannot belong to more than one resident group");
verify(privacyGroupManager).findPrivacyGroupByType(eq(PrivacyGroup.Type.RESIDENT));
verify(privacyGroupManager).getManagedKeys();
}
use of com.quorum.tessera.enclave.PrivacyGroup in project tessera by ConsenSys.
the class ResidentGroupHandlerImplTest method keysCanNotBeMovedOutOfAGroup.
@Test
public void keysCanNotBeMovedOutOfAGroup() {
final PrivacyGroup existedGroup = mock(PrivacyGroup.class);
when(existedGroup.getMembers()).thenReturn(List.of(PublicKey.from("m1".getBytes())));
when(existedGroup.getId()).thenReturn(PrivacyGroup.Id.fromBytes("rg1".getBytes()));
when(privacyGroupManager.findPrivacyGroupByType(eq(PrivacyGroup.Type.RESIDENT))).thenReturn(List.of(existedGroup));
ResidentGroup rg2 = new ResidentGroup();
rg2.setMembers(List.of(PublicKey.from("m1".getBytes()).encodeToBase64(), PublicKey.from("m2".getBytes()).encodeToBase64()));
rg2.setName("rg2");
Config config = mock(Config.class);
when(config.getResidentGroups()).thenReturn(List.of(rg2));
when(privacyGroupManager.getManagedKeys()).thenReturn(Set.of(PublicKey.from("m1".getBytes()), PublicKey.from("m2".getBytes())));
assertThatThrownBy(() -> residentGroupHandler.onCreate(config)).isInstanceOf(PrivacyViolationException.class).hasMessageContaining("Key cannot belong to more than one resident group");
verify(privacyGroupManager).findPrivacyGroupByType(eq(PrivacyGroup.Type.RESIDENT));
verify(privacyGroupManager).getManagedKeys();
}
use of com.quorum.tessera.enclave.PrivacyGroup in project tessera by ConsenSys.
the class PrivacyGroupManagerImpl method createLegacyPrivacyGroup.
@Override
public PrivacyGroup createLegacyPrivacyGroup(PublicKey from, List<PublicKey> recipients) {
final List<PublicKey> members = new ArrayList<>();
members.add(from);
members.addAll(recipients);
final String name = "legacy";
final String description = "Privacy groups to support the creation of groups by privateFor and privateFrom";
final byte[] groupIdBytes = privacyGroupUtil.generateId(members);
final PrivacyGroup created = PrivacyGroup.Builder.create().withPrivacyGroupId(groupIdBytes).withName(name).withDescription(description).withMembers(members).withType(PrivacyGroup.Type.LEGACY).withState(PrivacyGroup.State.ACTIVE).build();
final byte[] lookupId = privacyGroupUtil.generateLookupId(members);
final byte[] encodedData = privacyGroupUtil.encode(created);
privacyGroupDAO.retrieveOrSave(new PrivacyGroupEntity(groupIdBytes, lookupId, encodedData));
return created;
}
Aggregations