use of com.quorum.tessera.encryption.PublicKey in project tessera by ConsenSys.
the class PrivacyHelperTest method validPayloadMandatoryRecipients.
@Test
public void validPayloadMandatoryRecipients() {
TxHash txHash = mock(TxHash.class);
EncodedPayload payload = mock(EncodedPayload.class);
when(payload.getPrivacyMode()).thenReturn(PrivacyMode.MANDATORY_RECIPIENTS);
PublicKey mandatory1 = mock(PublicKey.class);
PublicKey mandatory2 = mock(PublicKey.class);
when(payload.getMandatoryRecipients()).thenReturn(Set.of(mandatory1, mandatory2));
EncodedPayload affectedPayload1 = mock(EncodedPayload.class);
when(affectedPayload1.getPrivacyMode()).thenReturn(PrivacyMode.MANDATORY_RECIPIENTS);
when(affectedPayload1.getMandatoryRecipients()).thenReturn(Set.of(mandatory1));
AffectedTransaction affectedTransaction1 = mock(AffectedTransaction.class);
when(affectedTransaction1.getPayload()).thenReturn(affectedPayload1);
EncodedPayload affectedPayload2 = mock(EncodedPayload.class);
when(affectedPayload2.getPrivacyMode()).thenReturn(PrivacyMode.MANDATORY_RECIPIENTS);
when(affectedPayload2.getMandatoryRecipients()).thenReturn(Set.of(mandatory2));
AffectedTransaction affectedTransaction2 = mock(AffectedTransaction.class);
when(affectedTransaction2.getPayload()).thenReturn(affectedPayload2);
boolean result = privacyHelper.validatePayload(txHash, payload, List.of(affectedTransaction1, affectedTransaction2));
assertThat(result).isTrue();
}
use of com.quorum.tessera.encryption.PublicKey in project tessera by ConsenSys.
the class PrivacyHelperTest method testValidateSendMandatoryRecipientsMismatched.
@Test
public void testValidateSendMandatoryRecipientsMismatched() {
PublicKey recipient1 = mock(PublicKey.class);
PublicKey recipient2 = mock(PublicKey.class);
final EncodedPayload encodedPayload = mock(EncodedPayload.class);
when(encodedPayload.getPrivacyMode()).thenReturn(PrivacyMode.MANDATORY_RECIPIENTS);
when(encodedPayload.getRecipientKeys()).thenReturn(List.of(recipient1, recipient2));
when(encodedPayload.getMandatoryRecipients()).thenReturn(Set.of(mock(PublicKey.class)));
final AffectedTransaction affectedTransaction = mock(AffectedTransaction.class);
when(affectedTransaction.getPayload()).thenReturn(encodedPayload);
final TxHash hash = TxHash.from("someHash".getBytes());
when(affectedTransaction.getHash()).thenReturn(hash);
assertThatExceptionOfType(PrivacyViolationException.class).isThrownBy(() -> privacyHelper.validateSendRequest(PrivacyMode.MANDATORY_RECIPIENTS, List.of(recipient1, recipient2), singletonList(affectedTransaction), Set.of(recipient1))).withMessageContaining("Privacy metadata mismatched");
}
use of com.quorum.tessera.encryption.PublicKey in project tessera by ConsenSys.
the class PrivacyHelperTest method validatePayloadPsvFakeSender.
@Test
public void validatePayloadPsvFakeSender() {
final PublicKey recipient1 = PublicKey.from("Recipient1".getBytes());
final PublicKey recipient2 = PublicKey.from("Recipient2".getBytes());
final PublicKey fakeSender = PublicKey.from("someone".getBytes());
final TxHash txHash = TxHash.from("someHash".getBytes());
EncodedPayload payload = mock(EncodedPayload.class);
when(payload.getPrivacyMode()).thenReturn(PrivacyMode.PRIVATE_STATE_VALIDATION);
when(payload.getSenderKey()).thenReturn(fakeSender);
Map<TxHash, SecurityHash> affected = new HashMap<>();
affected.put(TxHash.from("Hash1".getBytes()), SecurityHash.from("secHash1".getBytes()));
affected.put(TxHash.from("Hash2".getBytes()), SecurityHash.from("secHash2".getBytes()));
when(payload.getAffectedContractTransactions()).thenReturn(affected);
EncodedPayload affectedPayload1 = mock(EncodedPayload.class);
when(affectedPayload1.getPrivacyMode()).thenReturn(PrivacyMode.PRIVATE_STATE_VALIDATION);
when(affectedPayload1.getRecipientKeys()).thenReturn(List.of(recipient1, fakeSender));
AffectedTransaction affectedTransaction1 = mock(AffectedTransaction.class);
when(affectedTransaction1.getPayload()).thenReturn(affectedPayload1);
when(affectedTransaction1.getHash()).thenReturn(TxHash.from("hash1".getBytes()));
EncodedPayload affectedPayload2 = mock(EncodedPayload.class);
when(affectedPayload2.getPrivacyMode()).thenReturn(PrivacyMode.PRIVATE_STATE_VALIDATION);
when(affectedPayload2.getRecipientKeys()).thenReturn(List.of(recipient1, recipient2));
AffectedTransaction affectedTransaction2 = mock(AffectedTransaction.class);
when(affectedTransaction2.getPayload()).thenReturn(affectedPayload2);
when(affectedTransaction2.getHash()).thenReturn(TxHash.from("hash2".getBytes()));
boolean result = privacyHelper.validatePayload(txHash, payload, List.of(affectedTransaction1, affectedTransaction2));
assertThat(result).isFalse();
}
use of com.quorum.tessera.encryption.PublicKey in project tessera by ConsenSys.
the class PrivacyHelperTest method testValidSendMandatoryRecipients.
@Test
public void testValidSendMandatoryRecipients() {
PublicKey recipient1 = mock(PublicKey.class);
PublicKey recipient2 = mock(PublicKey.class);
final EncodedPayload encodedPayload = mock(EncodedPayload.class);
when(encodedPayload.getPrivacyMode()).thenReturn(PrivacyMode.MANDATORY_RECIPIENTS);
when(encodedPayload.getRecipientKeys()).thenReturn(List.of(recipient1));
when(encodedPayload.getMandatoryRecipients()).thenReturn(Set.of(recipient1));
final AffectedTransaction affectedTransaction = mock(AffectedTransaction.class);
when(affectedTransaction.getPayload()).thenReturn(encodedPayload);
final TxHash hash = TxHash.from("someHash".getBytes());
when(affectedTransaction.getHash()).thenReturn(hash);
boolean valid = privacyHelper.validateSendRequest(PrivacyMode.MANDATORY_RECIPIENTS, List.of(recipient1, recipient2), singletonList(affectedTransaction), Set.of(recipient1, recipient2));
assertThat(valid).isTrue();
}
use of com.quorum.tessera.encryption.PublicKey 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());
}
Aggregations