use of com.quorum.tessera.partyinfo.model.Recipient in project tessera by ConsenSys.
the class PartyInfoResourceTest method partyInfoExceptionIfValidationFailsWith400.
@Test
public void partyInfoExceptionIfValidationFailsWith400() {
final int validateResponseCode = 400;
final String validateResponseMsg = null;
String url = "http://www.bogus.com";
PublicKey myKey = PublicKey.from("myKey".getBytes());
PublicKey recipientKey = PublicKey.from("recipientKey".getBytes());
String message = "I love sparrows";
byte[] payload = message.getBytes();
Recipient recipient = Recipient.of(recipientKey, url);
Set<Recipient> recipientList = Collections.singleton(recipient);
PartyInfo partyInfo = new PartyInfo(url, recipientList, Collections.emptySet());
when(partyInfoParser.from(payload)).thenReturn(partyInfo);
when(enclave.defaultPublicKey()).thenReturn(myKey);
when(partyInfoParser.to(partyInfo)).thenReturn(payload);
EncodedPayload encodedPayload = mock(EncodedPayload.class);
when(enclave.encryptPayload(any(byte[].class), any(PublicKey.class), anyList(), any(PrivacyMetadata.class))).thenReturn(encodedPayload);
when(payloadEncoder.encode(encodedPayload)).thenReturn(payload);
WebTarget webTarget = mock(WebTarget.class);
when(restClient.target(url)).thenReturn(webTarget);
when(webTarget.path(anyString())).thenReturn(webTarget);
Invocation.Builder invocationBuilder = mock(Invocation.Builder.class);
when(webTarget.request()).thenReturn(invocationBuilder);
Response response = mock(Response.class);
when(response.getStatus()).thenReturn(validateResponseCode);
doAnswer((invocation) -> validateResponseMsg).when(response).readEntity(String.class);
when(invocationBuilder.post(any(Entity.class))).thenReturn(response);
try {
partyInfoResource.partyInfo(payload, List.of("v1", "v2"));
failBecauseExceptionWasNotThrown(SecurityException.class);
} catch (SecurityException ex) {
verify(partyInfoParser).from(payload);
verify(enclave).defaultPublicKey();
verify(enclave).encryptPayload(any(byte[].class), any(PublicKey.class), anyList(), any(PrivacyMetadata.class));
verify(payloadEncoder).encode(encodedPayload);
verify(restClient).target(url);
}
}
use of com.quorum.tessera.partyinfo.model.Recipient in project tessera by ConsenSys.
the class PartyInfoResourceTest method partyInfo.
@Test
public void partyInfo() {
String url = "http://www.bogus.com";
PublicKey myKey = PublicKey.from("myKey".getBytes());
PublicKey recipientKey = PublicKey.from("recipientKey".getBytes());
String message = "I love sparrows";
byte[] payload = message.getBytes();
Recipient recipient = Recipient.of(recipientKey, url);
Set<Recipient> recipientList = Collections.singleton(recipient);
PartyInfo partyInfo = new PartyInfo(url, recipientList, Collections.emptySet());
when(partyInfoParser.from(payload)).thenReturn(partyInfo);
when(enclave.defaultPublicKey()).thenReturn(myKey);
when(partyInfoParser.to(partyInfo)).thenReturn(payload);
EncodedPayload encodedPayload = mock(EncodedPayload.class);
List<String> uuidList = new ArrayList<>();
doAnswer((invocation) -> {
byte[] d = invocation.getArgument(0);
uuidList.add(new String(d));
return encodedPayload;
}).when(enclave).encryptPayload(any(byte[].class), any(PublicKey.class), anyList(), any(PrivacyMetadata.class));
when(payloadEncoder.encode(encodedPayload)).thenReturn(payload);
WebTarget webTarget = mock(WebTarget.class);
when(restClient.target(url)).thenReturn(webTarget);
when(webTarget.path(anyString())).thenReturn(webTarget);
Invocation.Builder invocationBuilder = mock(Invocation.Builder.class);
when(webTarget.request()).thenReturn(invocationBuilder);
Response response = mock(Response.class);
when(response.getStatus()).thenReturn(200);
doAnswer((invocation) -> uuidList.get(0)).when(response).readEntity(String.class);
when(invocationBuilder.post(any(Entity.class))).thenReturn(response);
Response result = partyInfoResource.partyInfo(payload, List.of("v1,v2"));
assertThat(result.getStatus()).isEqualTo(200);
verify(partyInfoParser).from(payload);
verify(enclave).defaultPublicKey();
verify(enclave).encryptPayload(any(byte[].class), any(PublicKey.class), anyList(), any(PrivacyMetadata.class));
verify(payloadEncoder).encode(encodedPayload);
verify(restClient).target(url);
ArgumentCaptor<NodeInfo> argCaptor = ArgumentCaptor.forClass(NodeInfo.class);
verify(discovery).onUpdate(argCaptor.capture());
final NodeInfo nodeInfo = argCaptor.getValue();
assertThat(nodeInfo).isNotNull();
assertThat(nodeInfo.getUrl()).isEqualTo(url);
assertThat(nodeInfo.supportedApiVersions()).containsExactlyInAnyOrder("v1", "v2");
}
use of com.quorum.tessera.partyinfo.model.Recipient in project tessera by ConsenSys.
the class PartyInfoResourceTest method partyInfoExceptionIfValidationFailsWith200.
@Test
public void partyInfoExceptionIfValidationFailsWith200() {
final int validateResponseCode = 200;
final String validateResponseMsg = "BADRESPONSE";
String url = "http://www.bogus.com";
PublicKey myKey = PublicKey.from("myKey".getBytes());
PublicKey recipientKey = PublicKey.from("recipientKey".getBytes());
String message = "I love sparrows";
byte[] payload = message.getBytes();
Recipient recipient = Recipient.of(recipientKey, url);
Set<Recipient> recipientList = Collections.singleton(recipient);
PartyInfo partyInfo = new PartyInfo(url, recipientList, Collections.emptySet());
when(partyInfoParser.from(payload)).thenReturn(partyInfo);
when(enclave.defaultPublicKey()).thenReturn(myKey);
when(partyInfoParser.to(partyInfo)).thenReturn(payload);
EncodedPayload encodedPayload = mock(EncodedPayload.class);
when(enclave.encryptPayload(any(byte[].class), any(PublicKey.class), anyList(), any(PrivacyMetadata.class))).thenReturn(encodedPayload);
when(payloadEncoder.encode(encodedPayload)).thenReturn(payload);
WebTarget webTarget = mock(WebTarget.class);
when(restClient.target(url)).thenReturn(webTarget);
when(webTarget.path(anyString())).thenReturn(webTarget);
Invocation.Builder invocationBuilder = mock(Invocation.Builder.class);
when(webTarget.request()).thenReturn(invocationBuilder);
Response response = mock(Response.class);
when(response.getStatus()).thenReturn(validateResponseCode);
doAnswer((invocation) -> validateResponseMsg).when(response).readEntity(String.class);
when(invocationBuilder.post(any(Entity.class))).thenReturn(response);
try {
partyInfoResource.partyInfo(payload, Collections.emptyList());
failBecauseExceptionWasNotThrown(SecurityException.class);
} catch (SecurityException ex) {
verify(partyInfoParser).from(payload);
verify(enclave).defaultPublicKey();
verify(enclave).encryptPayload(any(byte[].class), any(PublicKey.class), anyList(), any(PrivacyMetadata.class));
verify(payloadEncoder).encode(encodedPayload);
verify(restClient).target(url);
}
}
use of com.quorum.tessera.partyinfo.model.Recipient in project tessera by ConsenSys.
the class PeerToPeerIT method happyCase.
/*
* Send a valid party info from B to A
*/
@Test
public void happyCase() {
Party partyB = partyHelper.findByAlias(NodeAlias.B);
ServerConfig serverContext = Optional.of(partyB.getConfig()).map(Config::getP2PServerConfig).get();
Client client = clientFactory.buildFrom(serverContext);
PublicKey partyBKey = Optional.of(partyB).map(Party::getPublicKey).map(Base64.getDecoder()::decode).map(PublicKey::from).get();
String partyBServerAddress = partyB.getConfig().getP2PServerConfig().getServerAddress();
Recipient recipient = Recipient.of(partyBKey, partyBServerAddress);
PartyInfo partyInfo = new PartyInfo(partyBServerAddress, Collections.singleton(recipient), Collections.emptySet());
PartyInfoParser partyInfoParser = PartyInfoParser.create();
byte[] data = partyInfoParser.to(partyInfo);
StreamingOutput output = out -> out.write(data);
Response response = client.target(partyA.getP2PUri()).path("partyinfo").request().post(Entity.entity(output, MediaType.APPLICATION_OCTET_STREAM));
assertThat(response.getStatus()).isEqualTo(200);
}
use of com.quorum.tessera.partyinfo.model.Recipient in project tessera by ConsenSys.
the class PeerToPeerIT method benevolentNodeBecomesPossessedAndTriesToSendInvalidUrlAndKeyCombo.
@Test
public void benevolentNodeBecomesPossessedAndTriesToSendInvalidUrlAndKeyCombo() throws Exception {
Party partyB = partyHelper.findByAlias(NodeAlias.B);
ServerConfig serverConfig = partyB.getConfig().getP2PServerConfig();
PublicKey publicKey = Optional.of(partyB).map(Party::getPublicKey).map(Base64.getDecoder()::decode).map(PublicKey::from).get();
Recipient itself = Recipient.of(publicKey, serverConfig.getServerUri().toString());
String validKeyFromOtherNode = partyHelper.findByAlias(NodeAlias.C).getPublicKey();
PublicKey validButIncorrectKey = Optional.of(validKeyFromOtherNode).map(Base64.getDecoder()::decode).map(PublicKey::from).get();
String workingUrlFromSomeOtherNode = partyHelper.findByAlias(NodeAlias.D).getConfig().getP2PServerConfig().getServerAddress();
Recipient badRecipient = Recipient.of(validButIncorrectKey, workingUrlFromSomeOtherNode);
Set<Recipient> recipients = Stream.of(itself, badRecipient).collect(Collectors.toSet());
assertThat(recipients).containsExactlyInAnyOrder(itself, badRecipient);
PartyInfo partyInfo = new PartyInfo(serverConfig.getServerUri().toString(), recipients, Collections.emptySet());
Client client = new ClientFactory().buildFrom(serverConfig);
PartyInfoParser partyInfoParser = PartyInfoParser.create();
byte[] data = partyInfoParser.to(partyInfo);
StreamingOutput output = out -> out.write(data);
Response response = client.target(partyA.getP2PUri()).path("partyinfo").request().post(Entity.entity(output, MediaType.APPLICATION_OCTET_STREAM));
assertThat(response.getStatus()).isEqualTo(200);
}
Aggregations