use of com.quorum.tessera.partyinfo.model.Party in project tessera by ConsenSys.
the class PartyInfoResourceTest method partyInfoValidationEncryptsUniqueDataForEachKey.
@Test
public void partyInfoValidationEncryptsUniqueDataForEachKey() {
String url = "http://bogus";
Set<Party> parties = Collections.emptySet();
Set<Recipient> recipients = new HashSet<>();
recipients.add(Recipient.of(mock(PublicKey.class), url));
recipients.add(Recipient.of(mock(PublicKey.class), url));
PartyInfo partyInfo = new PartyInfo(url, recipients, parties);
byte[] payload = new byte[] {};
when(partyInfoParser.from(payload)).thenReturn(partyInfo);
when(enclave.defaultPublicKey()).thenReturn(PublicKey.from("defaultKey".getBytes()));
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(any(EncodedPayload.class))).thenReturn("somedata".getBytes());
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(invocationBuilder.post(any(Entity.class))).thenReturn(response);
when(response.getStatus()).thenReturn(200);
when(response.getEntity()).thenReturn("");
doAnswer(new Answer() {
private int i = 0;
public Object answer(InvocationOnMock invocation) {
String result = uuidList.get(i);
i++;
return result;
}
}).when(response).readEntity(String.class);
// the test
partyInfoResource.partyInfo(payload, null);
ArgumentCaptor<byte[]> uuidCaptor = ArgumentCaptor.forClass(byte[].class);
verify(enclave, times(2)).encryptPayload(uuidCaptor.capture(), any(PublicKey.class), anyList(), any(PrivacyMetadata.class));
List<byte[]> capturedUUIDs = uuidCaptor.getAllValues();
assertThat(capturedUUIDs).hasSize(2);
assertThat(capturedUUIDs.get(0)).isNotEqualTo(capturedUUIDs.get(1));
// other verifications
verify(discovery).onUpdate(any(NodeInfo.class));
verify(partyInfoParser).from(payload);
verify(enclave).defaultPublicKey();
verify(payloadEncoder, times(2)).encode(encodedPayload);
verify(restClient, times(2)).target(url);
}
use of com.quorum.tessera.partyinfo.model.Party in project tessera by ConsenSys.
the class PartyInfoResource method getPartyInfo.
@Operation(summary = "/partyinfo", description = "fetch network/peer information")
@ApiResponse(responseCode = "200", description = "server's partyinfo data", content = @Content(schema = @Schema(implementation = GetPartyInfoResponse.class)))
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getPartyInfo() {
final NodeInfo current = this.discovery.getCurrent();
final JsonArrayBuilder peersBuilder = Json.createArrayBuilder();
partyStore.getParties().stream().map(party -> Json.createObjectBuilder().add("url", party.toString()).build()).forEach(peersBuilder::add);
final JsonArrayBuilder recipientBuilder = Json.createArrayBuilder();
current.getRecipients().stream().map(recipient -> Json.createObjectBuilder().add("key", recipient.getKey().encodeToBase64()).add("url", recipient.getUrl()).build()).forEach(recipientBuilder::add);
final String output = Json.createObjectBuilder().add("url", current.getUrl()).add("peers", peersBuilder.build()).add("keys", recipientBuilder.build()).build().toString();
LOGGER.debug("Sending json {} from {}", output, current);
return Response.status(Response.Status.OK).entity(output).build();
}
use of com.quorum.tessera.partyinfo.model.Party in project tessera by ConsenSys.
the class ResendPartyStoreTest method failedRequestDoesntMakePartyAvailableForUseIfAboveThreeshold.
@Test
public void failedRequestDoesntMakePartyAvailableForUseIfAboveThreeshold() {
final int presetAttempts = 25;
final Party party = new Party("badurl.com");
final SyncableParty failedReq = new SyncableParty(party, presetAttempts);
this.resendPartyStore.incrementFailedAttempt(failedReq);
final Optional<SyncableParty> partyOne = resendPartyStore.getNextParty();
assertThat(partyOne).isNotPresent();
}
use of com.quorum.tessera.partyinfo.model.Party in project tessera by ConsenSys.
the class ResendPartyStoreTest method oldPeersAreNotServed.
@Test
public void oldPeersAreNotServed() {
final List<Party> peers = singletonList(new Party("newurl1.com"));
this.resendPartyStore.addUnseenParties(peers);
final Optional<SyncableParty> partyOne = resendPartyStore.getNextParty();
assertThat(partyOne).isPresent();
assertThat(partyOne.get().getAttempts()).isEqualTo(0);
assertThat(partyOne.get().getParty()).isEqualTo(new Party("newurl1.com"));
final Optional<SyncableParty> partyTwo = resendPartyStore.getNextParty();
assertThat(partyTwo).isNotPresent();
this.resendPartyStore.addUnseenParties(peers);
final Optional<SyncableParty> partyThree = resendPartyStore.getNextParty();
assertThat(partyThree).isNotPresent();
}
use of com.quorum.tessera.partyinfo.model.Party in project tessera by ConsenSys.
the class SyncPollerTest method localUrlIsExcludedFromPoll.
@Test
public void localUrlIsExcludedFromPoll() {
final String targetUrl = "localurl.com";
final String syncableUrl = "syncable.com";
final com.quorum.tessera.partyinfo.node.Recipient localKey = com.quorum.tessera.partyinfo.node.Recipient.of(mock(PublicKey.class), targetUrl);
final com.quorum.tessera.partyinfo.node.Recipient anotherKey = com.quorum.tessera.partyinfo.node.Recipient.of(mock(PublicKey.class), syncableUrl);
final Party syncableParty = new Party(syncableUrl);
final Set<Recipient> recipients = Set.of(localKey, anotherKey);
NodeInfo nodeInfo = NodeInfo.Builder.create().withUrl("localurl.com").withRecipients(recipients).build();
// partyStore.store(URI.create(syncableUrl));
when(partyInfoService.getCurrent()).thenReturn(nodeInfo);
doReturn(Optional.empty()).when(resendPartyStore).getNextParty();
syncPoller.run();
verify(resendPartyStore, times(1)).addUnseenParties(Set.of(syncableParty));
verify(resendPartyStore, times(1)).getNextParty();
}
Aggregations