Search in sources :

Example 1 with Party

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);
}
Also used : Entity(jakarta.ws.rs.client.Entity) Invocation(jakarta.ws.rs.client.Invocation) Party(com.quorum.tessera.partyinfo.model.Party) PrivacyMetadata(com.quorum.tessera.enclave.PrivacyMetadata) PublicKey(com.quorum.tessera.encryption.PublicKey) Recipient(com.quorum.tessera.partyinfo.model.Recipient) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) PartyInfo(com.quorum.tessera.partyinfo.model.PartyInfo) Response(jakarta.ws.rs.core.Response) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) NodeInfo(com.quorum.tessera.partyinfo.node.NodeInfo) JsonObject(jakarta.json.JsonObject) WebTarget(jakarta.ws.rs.client.WebTarget) Test(org.junit.Test)

Example 2 with Party

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();
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) java.util(java.util) NodeInfoUtil(com.quorum.tessera.partyinfo.model.NodeInfoUtil) LoggerFactory(org.slf4j.LoggerFactory) Party(com.quorum.tessera.partyinfo.model.Party) GetPartyInfoResponse(com.quorum.tessera.p2p.model.GetPartyInfoResponse) Content(io.swagger.v3.oas.annotations.media.Content) Discovery(com.quorum.tessera.discovery.Discovery) Operation(io.swagger.v3.oas.annotations.Operation) Response(jakarta.ws.rs.core.Response) NodeInfo(com.quorum.tessera.partyinfo.node.NodeInfo) Objects.requireNonNull(java.util.Objects.requireNonNull) RequestBody(io.swagger.v3.oas.annotations.parameters.RequestBody) ApiResponse(io.swagger.v3.oas.annotations.responses.ApiResponse) Constants(com.quorum.tessera.shared.Constants) Schema(io.swagger.v3.oas.annotations.media.Schema) PartyInfoParser(com.quorum.tessera.p2p.partyinfo.PartyInfoParser) Client(jakarta.ws.rs.client.Client) Logger(org.slf4j.Logger) Collections.emptySet(java.util.Collections.emptySet) Collections.emptyList(java.util.Collections.emptyList) Predicate(java.util.function.Predicate) com.quorum.tessera.enclave(com.quorum.tessera.enclave) JsonArrayBuilder(jakarta.json.JsonArrayBuilder) NodeUri(com.quorum.tessera.discovery.NodeUri) jakarta.ws.rs(jakarta.ws.rs) Collectors(java.util.stream.Collectors) PartyStore(com.quorum.tessera.p2p.partyinfo.PartyStore) Json(jakarta.json.Json) Entity(jakarta.ws.rs.client.Entity) Parameter(io.swagger.v3.oas.annotations.Parameter) ArraySchema(io.swagger.v3.oas.annotations.media.ArraySchema) PartyInfo(com.quorum.tessera.partyinfo.model.PartyInfo) MediaType(jakarta.ws.rs.core.MediaType) Tag(io.swagger.v3.oas.annotations.tags.Tag) Recipient(com.quorum.tessera.partyinfo.model.Recipient) NodeInfo(com.quorum.tessera.partyinfo.node.NodeInfo) JsonArrayBuilder(jakarta.json.JsonArrayBuilder) Operation(io.swagger.v3.oas.annotations.Operation) ApiResponse(io.swagger.v3.oas.annotations.responses.ApiResponse)

Example 3 with Party

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

Example 4 with Party

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

Example 5 with Party

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

Aggregations

Party (com.quorum.tessera.partyinfo.model.Party)17 Test (org.junit.Test)13 PartyInfo (com.quorum.tessera.partyinfo.model.PartyInfo)7 Recipient (com.quorum.tessera.partyinfo.model.Recipient)5 NodeInfo (com.quorum.tessera.partyinfo.node.NodeInfo)5 PublicKey (com.quorum.tessera.encryption.PublicKey)4 Discovery (com.quorum.tessera.discovery.Discovery)3 PartyInfoParser (com.quorum.tessera.p2p.partyinfo.PartyInfoParser)3 Entity (jakarta.ws.rs.client.Entity)3 Response (jakarta.ws.rs.core.Response)3 NodeUri (com.quorum.tessera.discovery.NodeUri)2 com.quorum.tessera.enclave (com.quorum.tessera.enclave)2 GetPartyInfoResponse (com.quorum.tessera.p2p.model.GetPartyInfoResponse)2 PartyStore (com.quorum.tessera.p2p.partyinfo.PartyStore)2 NodeInfoUtil (com.quorum.tessera.partyinfo.model.NodeInfoUtil)2 Constants (com.quorum.tessera.shared.Constants)2 Operation (io.swagger.v3.oas.annotations.Operation)2 Parameter (io.swagger.v3.oas.annotations.Parameter)2 ArraySchema (io.swagger.v3.oas.annotations.media.ArraySchema)2 Content (io.swagger.v3.oas.annotations.media.Content)2