PduV2Factory implements PduFactory { public AggregationResponse readAggregationResponse(KSIRequestContext context, ServiceCredentials credentials, TLVElement input) throws KSIException { return this.aggregatorPduV2Factory.readAggregationResponse(context, credentials, input); } PduV2Factory();  PduV2Factory(AggregatorPduV2Factory aggregatorPduV2Factory, ExtenderPduV2Factory extenderPduV2Factory);  PduV2Factory(AggregatorPduV2Factory aggregatorPduV2Factory);  PduV2Factory(ExtenderPduV2Factory extenderPduV2Factory); AggregationRequest createAggregationRequest(KSIRequestContext context, ServiceCredentials credentials, DataHash imprint, Long level); AggregationResponse readAggregationResponse(KSIRequestContext context, ServiceCredentials credentials, TLVElement input); AggregationRequest createAggregatorConfigurationRequest(KSIRequestContext context, ServiceCredentials credentials); AggregatorConfiguration readAggregatorConfigurationResponse(KSIRequestContext context, ServiceCredentials credentials, TLVElement input); ExtensionRequest createExtensionRequest(KSIRequestContext context, ServiceCredentials credentials, Date aggregationTime, Date publicationTime); ExtensionResponse readExtensionResponse(KSIRequestContext context, ServiceCredentials credentials, TLVElement input); ExtensionRequest createExtensionConfigurationRequest(KSIRequestContext context, ServiceCredentials credentials); ExtenderConfiguration readExtenderConfigurationResponse(ServiceCredentials credentials, TLVElement input);  }
@Test(expectedExceptions = InvalidMessageAuthenticationCodeException.class, expectedExceptionsMessageRegExp = "Invalid MAC code. Expected.*") public void testAggregationResponseContainsInvalidMac_ThrowsInvalidMessageAuthenticationCodeException() throws Exception { pduFactory.readAggregationResponse(requestContext, CREDENTIALS, loadTlv("pdu/aggregation/aggregation-response-v2-invalid-mac.tlv")); }
@Test(expectedExceptions = KSIProtocolException.class, expectedExceptionsMessageRegExp = "Aggregation response payload with requestId 1 wasn't found") public void testAggregationResponseContainsInvalidRequestId_ThrowsKSIProtocolException() throws Exception { pduFactory.readAggregationResponse(new KSIRequestContext(1L, 42L, 42L), CREDENTIALS, loadTlv("pdu/aggregation/aggregation-response-v2.tlv")); }
@Test(expectedExceptions = KSIProtocolException.class, expectedExceptionsMessageRegExp = "Invalid response message. Response message must contain at least one payload element") public void testAggregationResponseDoesNotContainResponseTlvTag_ThrowsKSIProtocolException() throws Exception { pduFactory.readAggregationResponse(requestContext, CREDENTIALS, loadTlv("pdu/aggregation/aggregation-response-v2-missing-payload.tlv")); }
@Test(expectedExceptions = KSIProtocolException.class, expectedExceptionsMessageRegExp = "Error was returned by server. Error status is .* Error message from server: 'The request could not be authenticated'") public void testAggregationResponseContains03ErrorMessage_ThrowsKSIProtocolException() throws Exception { pduFactory.readAggregationResponse(requestContext, CREDENTIALS, loadTlv("pdu/aggregation/aggregation-response-v2-invalid-login-key.tlv")); }
@Test(expectedExceptions = KSIProtocolException.class, expectedExceptionsMessageRegExp = ".*:Error was returned by server. Error status is .* Error message from server: .*") public void testAggregationResponseContainsErrorMessageInside02Element_ThrowsKSIProtocolException() throws Exception { pduFactory.readAggregationResponse(new KSIRequestContext(8530358545345979581L, 42L, 42L), CREDENTIALS, loadTlv("pdu/aggregation/aggregation-response-v2-with-error.tlv")); }
@Test(expectedExceptions = KSIProtocolException.class, expectedExceptionsMessageRegExp = "Received unknown response to PDU v2 aggregation request.") public void testReadV2AggregationResponse() throws Exception { pduFactory.readAggregationResponse(requestContext, CREDENTIALS, loadTlv("aggregation-203-error.tlv")); }
@Test(expectedExceptions = KSIProtocolException.class, expectedExceptionsMessageRegExp = "Error was returned by server. Error status is 0x101. Error message from server: 'this-error-should-be-thrown'") public void testReadV2AggregationResponseWithErrorPayloadAndMac_ThrowsKSIProtocolException() throws Exception { pduFactory.readAggregationResponse(requestContext, CREDENTIALS, loadTlv("pdu/aggregation/aggregation-response-v2-multi-payload-with-error-payload.tlv")); }
@Test(expectedExceptions = TLVParserException.class, expectedExceptionsMessageRegExp = "Unknown critical TLV element with tag=.* encountered") public void testReadV2AggregationResponseWithUnknownCriticalElement_ThrowsTLVParserException() throws Exception { pduFactory.readAggregationResponse(requestContext, CREDENTIALS, loadTlv("pdu/aggregation/aggregation-response-v2-multi-payload-with-critical-unknown-element.tlv")); }
@Test(expectedExceptions = InvalidMessageAuthenticationCodeException.class, expectedExceptionsMessageRegExp = "Invalid MAC code. Expected.*") public void testReadV2AggregationResponseWithEditedFlagAndUnchangedMac_ThrowsInvalidMessageAuthenticationCodeException() throws Exception { pduFactory.readAggregationResponse(requestContext, CREDENTIALS, loadTlv("pdu/aggregation/aggregation-response-v2-changed-flag-but-unchanged-hmac.tlv")); }
@Test(expectedExceptions = TLVParserException.class, expectedExceptionsMessageRegExp = "Invalid PDU header element. Expected element 0x01, got 0x.*") public void testReadV2AggregationResponseHeaderNotFirst_ThrowsInvalidMessageAuthenticationCodeException() throws Exception { pduFactory.readAggregationResponse(requestContext, CREDENTIALS, loadTlv("pdu/aggregation/aggregation-response-v2-header-not-first.tlv")); }
@Test(expectedExceptions = KSIException.class, expectedExceptionsMessageRegExp = "HMAC algorithm mismatch. Expected SHA-512, received SHA-256") public void testReadV2AggregationResponseHMACAlgorithm_ThrowsKSIException() throws Exception { ServiceCredentials extensionCredentials = new KSIServiceCredentials("anon", "anon".getBytes("UTF-8"), HashAlgorithm.SHA2_512); pduFactory.readAggregationResponse(requestContext, extensionCredentials, loadTlv("pdu/aggregation/aggregation-response-v2.tlv")); }
@Test public void testAggregationResponseParsingWithMultipleResponses() throws Exception { AggregationResponse response = pduFactory.readAggregationResponse(requestContext, CREDENTIALS, loadTlv("pdu/aggregation/aggregator-response-v2-multiple-mixed-and-duplicate-payloads.tlv")); Assert.assertNotNull(response); }
@Test public void testAggregationResponseParsingWithoutConfUpdate() throws Exception { ConfigurationListener<AggregatorConfiguration> aggregatorConfigurationListener = mock(ConfigurationListener.class); PduV2Factory pduFactoryWithConfListener = new PduV2Factory(new AggregatorPduV2Factory(aggregatorConfigurationListener)); KSIRequestContext context = new KSIRequestContext(123456L, 42L, 42L); AggregationResponse response = pduFactoryWithConfListener.readAggregationResponse(context, CREDENTIALS, loadTlv("pdu/aggregation/aggregation-response-v2.tlv")); Assert.assertNotNull(response); verify(aggregatorConfigurationListener, times(0)).updated(any(AggregatorConfiguration.class)); verify(aggregatorConfigurationListener, times(0)).updateFailed(any(Throwable.class)); }
@Test public void testAggregationResponseParsingWithConfigurationUpdate() throws Exception { ArgumentCaptor<AggregatorConfiguration> argumentCaptor = ArgumentCaptor.forClass(AggregatorConfiguration.class); ConfigurationListener<AggregatorConfiguration> aggregatorConfigurationListener = mock(ConfigurationListener.class); PduV2Factory pduFactoryWithConfHandler = new PduV2Factory(new AggregatorPduV2Factory(aggregatorConfigurationListener)); AggregationResponse response = pduFactoryWithConfHandler.readAggregationResponse(requestContext, CREDENTIALS, loadTlv("pdu/aggregation/aggregator-response-v2-multiple-mixed-and-duplicate-payloads.tlv")); Assert.assertNotNull(response); verify(aggregatorConfigurationListener, times(1)).updated(argumentCaptor.capture()); verify(aggregatorConfigurationListener, times(0)).updateFailed(any(Throwable.class)); AggregatorConfiguration configuration = argumentCaptor.getValue(); Assert.assertEquals((long) configuration.getMaximumLevel(), 19L); Assert.assertEquals((long) configuration.getAggregationPeriod(), 12288L); Assert.assertEquals((long) configuration.getMaximumRequests(), 17L); Assert.assertEquals(configuration.getAggregationAlgorithm(), HashAlgorithm.SHA2_384); Assert.assertEquals(configuration.getParents().size(), 3); }
@Test public void testAggregationResponseParsingWithBrokenConfigurationUpdate() throws Exception { ArgumentCaptor<Throwable> argumentCaptor = ArgumentCaptor.forClass(Throwable.class); ConfigurationListener<AggregatorConfiguration> aggregatorConfigurationListener = mock(ConfigurationListener.class); PduV2Factory pduFactoryWithConfHandler = new PduV2Factory(new AggregatorPduV2Factory(aggregatorConfigurationListener)); AggregationResponse response = pduFactoryWithConfHandler.readAggregationResponse(requestContext, CREDENTIALS, loadTlv("pdu/aggregation/aggregation-response-v2-with-broken-conf.tlv")); Assert.assertNotNull(response); verify(aggregatorConfigurationListener, times(0)).updated(any(AggregatorConfiguration.class)); verify(aggregatorConfigurationListener, times(1)).updateFailed(argumentCaptor.capture()); Assert.assertEquals(argumentCaptor.getValue().getMessage(), "Unknown critical TLV element with tag=0x5 encountered"); }