Search in sources :

Example 86 with PublicKey

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();
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) Test(org.junit.Test)

Example 87 with PublicKey

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");
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) Test(org.junit.Test)

Example 88 with PublicKey

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();
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) Test(org.junit.Test)

Example 89 with PublicKey

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();
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) Test(org.junit.Test)

Example 90 with PublicKey

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());
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) PrivacyGroupEntity(com.quorum.tessera.data.PrivacyGroupEntity) PrivacyGroup(com.quorum.tessera.enclave.PrivacyGroup) Test(org.junit.Test)

Aggregations

PublicKey (com.quorum.tessera.encryption.PublicKey)281 Test (org.junit.Test)213 Response (jakarta.ws.rs.core.Response)59 MessageHash (com.quorum.tessera.data.MessageHash)57 EncodedPayload (com.quorum.tessera.enclave.EncodedPayload)48 Collectors (java.util.stream.Collectors)32 PrivacyGroup (com.quorum.tessera.enclave.PrivacyGroup)28 NodeInfo (com.quorum.tessera.partyinfo.node.NodeInfo)25 java.util (java.util)23 SendResponse (com.quorum.tessera.api.SendResponse)21 Nonce (com.quorum.tessera.encryption.Nonce)20 Recipient (com.quorum.tessera.partyinfo.node.Recipient)20 Operation (io.swagger.v3.oas.annotations.Operation)20 ApiResponse (io.swagger.v3.oas.annotations.responses.ApiResponse)20 Stream (java.util.stream.Stream)19 ReceiveResponse (com.quorum.tessera.transaction.ReceiveResponse)18 EncryptedTransaction (com.quorum.tessera.data.EncryptedTransaction)17 PrivacyMode (com.quorum.tessera.enclave.PrivacyMode)17 URI (java.net.URI)17 SendRequest (com.quorum.tessera.api.SendRequest)15