use of com.quorum.tessera.enclave.PrivacyGroup in project tessera by ConsenSys.
the class PrivacyGroupManagerImplTest method testStoreUpdatedPrivacyGroup.
@Test
public void testStoreUpdatedPrivacyGroup() {
final PrivacyGroup mockPrivacyGroup = mock(PrivacyGroup.class);
when(mockPrivacyGroup.getId()).thenReturn(PrivacyGroup.Id.fromBytes("id".getBytes()));
when(mockPrivacyGroup.getState()).thenReturn(PrivacyGroup.State.DELETED);
final byte[] encoded = "encoded".getBytes();
when(privacyGroupUtil.decode(encoded)).thenReturn(mockPrivacyGroup);
when(privacyGroupUtil.generateLookupId(anyList())).thenReturn("lookup".getBytes());
PrivacyGroupEntity existing = new PrivacyGroupEntity("id".getBytes(), "lookup".getBytes(), "old".getBytes());
when(privacyGroupDAO.retrieve("id".getBytes())).thenReturn(Optional.of(existing));
privacyGroupManager.storePrivacyGroup(encoded);
ArgumentCaptor<PrivacyGroupEntity> argCaptor = ArgumentCaptor.forClass(PrivacyGroupEntity.class);
verify(privacyGroupDAO).retrieve("id".getBytes());
verify(privacyGroupDAO).update(argCaptor.capture());
final PrivacyGroupEntity saved = argCaptor.getValue();
assertThat(saved).isNotNull();
assertThat(saved.getId()).isEqualTo("id".getBytes());
assertThat(saved.getLookupId()).isEqualTo("lookup".getBytes());
assertThat(saved.getData()).isEqualTo("encoded".getBytes());
}
use of com.quorum.tessera.enclave.PrivacyGroup in project tessera by ConsenSys.
the class ResidentGroupHandlerImplTest method homelessKeysNotAllowedConsideringBothConfigAndDb.
@Test
public void homelessKeysNotAllowedConsideringBothConfigAndDb() {
ResidentGroup rg1 = new ResidentGroup();
rg1.setMembers(List.of(PublicKey.from("m1".getBytes()).encodeToBase64()));
rg1.setName("rg1");
when(privacyGroupManager.getManagedKeys()).thenReturn(Set.of(PublicKey.from("m1".getBytes()), PublicKey.from("m2".getBytes()), PublicKey.from("m3".getBytes())));
Config config = mock(Config.class);
when(config.getResidentGroups()).thenReturn(List.of(rg1));
final PrivacyGroup pg = PrivacyGroup.Builder.buildResidentGroup("private", "", List.of(PublicKey.from("m2".getBytes())));
when(privacyGroupManager.findPrivacyGroupByType(eq(PrivacyGroup.Type.RESIDENT))).thenReturn(List.of(pg));
assertThatThrownBy(() -> residentGroupHandler.onCreate(config)).isInstanceOf(PrivacyViolationException.class).hasMessageContaining("PublicKey[bTM=] must belong to a 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 keyGetsAddedToAnExistingGroup.
@Test
public void keyGetsAddedToAnExistingGroup() {
ResidentGroup rg1 = new ResidentGroup();
rg1.setMembers(List.of(PublicKey.from("m1".getBytes()).encodeToBase64(), PublicKey.from("m2".getBytes()).encodeToBase64()));
rg1.setName("rg1");
Config config = mock(Config.class);
when(config.getResidentGroups()).thenReturn(List.of(rg1));
when(privacyGroupManager.getManagedKeys()).thenReturn(Set.of(PublicKey.from("m1".getBytes()), PublicKey.from("m2".getBytes())));
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));
residentGroupHandler.onCreate(config);
verify(privacyGroupManager).findPrivacyGroupByType(eq(PrivacyGroup.Type.RESIDENT));
verify(privacyGroupManager).getManagedKeys();
verify(privacyGroupManager).saveResidentGroup(eq("rg1"), any(), memberCaptor.capture());
assertThat(memberCaptor.getValue()).containsExactlyInAnyOrder(PublicKey.from("m1".getBytes()), PublicKey.from("m2".getBytes()));
}
use of com.quorum.tessera.enclave.PrivacyGroup in project tessera by ConsenSys.
the class PrivacyGroupManagerImpl method storePrivacyGroup.
@Override
public void storePrivacyGroup(byte[] encodedData) {
final PrivacyGroup privacyGroup = privacyGroupUtil.decode(encodedData);
if (privacyGroup.getState() == PrivacyGroup.State.DELETED) {
privacyGroupDAO.retrieve(privacyGroup.getId().getBytes()).ifPresent(et -> {
et.setData(encodedData);
privacyGroupDAO.update(et);
});
return;
}
final byte[] id = privacyGroup.getId().getBytes();
final byte[] lookupId = privacyGroupUtil.generateLookupId(privacyGroup.getMembers());
final PrivacyGroupEntity newEntity = new PrivacyGroupEntity(id, lookupId, encodedData);
privacyGroupDAO.save(newEntity);
}
use of com.quorum.tessera.enclave.PrivacyGroup in project tessera by ConsenSys.
the class PrivacyGroupManagerImpl method createPrivacyGroup.
@Override
public PrivacyGroup createPrivacyGroup(String name, String description, PublicKey from, List<PublicKey> members, byte[] seed) {
final Set<PublicKey> localKeys = enclave.getPublicKeys();
if (!members.contains(from)) {
throw new PrivacyViolationException("The list of members in a privacy group should include self");
}
final byte[] groupIdBytes = privacyGroupUtil.generateId(members, seed);
final PrivacyGroup created = PrivacyGroup.Builder.create().withPrivacyGroupId(groupIdBytes).withName(name).withDescription(description).withMembers(members).withSeed(seed).withType(PrivacyGroup.Type.PANTHEON).withState(PrivacyGroup.State.ACTIVE).build();
final byte[] lookupId = privacyGroupUtil.generateLookupId(members);
final byte[] encodedData = privacyGroupUtil.encode(created);
final List<PublicKey> forwardingMembers = members.stream().filter(Predicate.not(localKeys::contains)).collect(Collectors.toList());
privacyGroupDAO.save(new PrivacyGroupEntity(groupIdBytes, lookupId, encodedData), () -> {
publisher.publishPrivacyGroup(encodedData, forwardingMembers);
return null;
});
return created;
}
Aggregations