PduV2Factory implements PduFactory { public ExtensionResponse readExtensionResponse(KSIRequestContext context, ServiceCredentials credentials, TLVElement input) throws KSIException { return this.extenderPduV2Factory.readExtensionResponse(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 = KSIException.class, expectedExceptionsMessageRegExp = "HMAC algorithm mismatch. Expected SHA-512, received SHA-256") public void testExtensionResponseInvalidHMACAlgorithm_ThrowsKSIException() throws Exception { ServiceCredentials extensionCredentials = new KSIServiceCredentials("anon", "anon".getBytes("UTF-8"), HashAlgorithm.SHA2_512); pduFactory.readExtensionResponse(extensionContext, extensionCredentials, loadTlv("pdu/extension/extension-response-v2.tlv")); }
@Test(expectedExceptions = InvalidMessageAuthenticationCodeException.class, expectedExceptionsMessageRegExp = "Invalid MAC code. Expected.*") public void testExtensionResponseInvalidHMAC_ThrowsInvalidMessageAuthenticationCodeException() throws Exception { pduFactory.readExtensionResponse(extensionContext, CREDENTIALS, loadTlv("pdu/extension/extension-response-v2-invalid-mac.tlv")); }
@Test(expectedExceptions = KSIProtocolException.class, expectedExceptionsMessageRegExp = "Extension response payload with requestId 5546551786909961666 wasn't found") public void testExtensionRequestIdsMismatch() throws Exception { pduFactory.readExtensionResponse(extensionContext, CREDENTIALS, loadTlv("pdu/extension/extension-response-v2.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 testExtensionResponseContains03ErrorMessage_ThrowsKSIProtocolException() throws Exception { pduFactory.readExtensionResponse(requestContext, CREDENTIALS, loadTlv("pdu/extension/extension-response-v2-invalid-login-key.tlv")); }
@Test(expectedExceptions = KSIProtocolException.class, expectedExceptionsMessageRegExp = ".*:Error was returned by server. Error status is .* Error message from server: 'The request contained invalid payload'") public void testExtensionResponseContains02ErrorMessage_ThrowsKSIProtocolException() throws Exception { pduFactory.readExtensionResponse(new KSIRequestContext(98765L, 42L, 42L), CREDENTIALS, loadTlv("pdu/extension/extension-response-v2-with-error.tlv")); }
@Test(expectedExceptions = KSIProtocolException.class, expectedExceptionsMessageRegExp = "Invalid KSI response. Missing MAC.") public void testExtensionResponseWithoutMac_ThrowsKSIProtocolException() throws Exception { pduFactory.readExtensionResponse(extensionContext, CREDENTIALS, loadTlv("pdu/extension/extension-response-v2-missing-mac.tlv")); }
@Test(expectedExceptions = KSIProtocolException.class, expectedExceptionsMessageRegExp = "Received unknown response to PDU v2 extension request.") public void testReadV2ExtensionResponse() throws Exception { pduFactory.readExtensionResponse(extensionContext, CREDENTIALS, loadTlv("pdu/extension/extension-response-v1-ok-request-id-4321.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 testExtensionResponseWithErrorPayloadAndMac_ThrowsKSIProtocolException() throws Exception { pduFactory.readExtensionResponse(extensionContext, CREDENTIALS, loadTlv("pdu/extension/extension-response-v2-multi-payload-with-error-payload.tlv")); }
@Test(expectedExceptions = TLVParserException.class, expectedExceptionsMessageRegExp = "Unknown critical TLV element with tag=.* encountered") public void testReadV2ExtensionResponseWithUnknownCriticalElement_ThrowsTLVParserException() throws Exception { pduFactory.readExtensionResponse(extensionContext, CREDENTIALS, loadTlv("pdu/extension/extension-response-v2-with-unknown-critical-element.tlv")); }
@Test(expectedExceptions = InvalidMessageAuthenticationCodeException.class, expectedExceptionsMessageRegExp = "Invalid MAC code. Expected.*") public void testReadV2ExtensionResponseWithEditedFlagAndUnchangedMac_ThrowsInvalidMessageAuthenticationCodeException() throws Exception { pduFactory.readExtensionResponse(extensionContext, CREDENTIALS, loadTlv("pdu/extension/extension-response-v2-with-changed-flag-but-unchanged-mac.tlv")); }
@Test(expectedExceptions = TLVParserException.class, expectedExceptionsMessageRegExp = "Invalid PDU header element. Expected element 0x01, got 0x.*") public void testReadV2ExtensionResponseHeaderNotFirst_ThrowsInvalidMessageAuthenticationCodeException() throws Exception { pduFactory.readExtensionResponse(extensionContext, CREDENTIALS, loadTlv("pdu/extension/extension-response-v2-header-not-first.tlv")); }
@Test public void testReadV2ExtensionResponseContainingUnknownNonCriticalElement() throws Exception { ExtensionResponse response = pduFactory.readExtensionResponse(new KSIRequestContext(8396215651691691389L, 42L, 42L), new KSIServiceCredentials("anon", "anon".getBytes("UTF-8"), HashAlgorithm.SHA2_512), loadTlv("pdu/extension/extension-response-v2-unknown-non-critical-element.tlv")); Assert.assertNotNull(response); Assert.assertNotNull(response.getCalendarHashChain()); }
@Test public void testReadV2ExtensionResponseContainingDifferentPayloads() throws Exception { ExtensionResponse response = pduFactory.readExtensionResponse(new KSIRequestContext(8396215651691691389L, 42L, 42L), new KSIServiceCredentials("anon", "anon".getBytes("UTF-8")), loadTlv("pdu/extension/extension-response-v2-multiple-mixed-and-duplicate-payloads.tlv")); Assert.assertNotNull(response); Assert.assertNotNull(response.getCalendarHashChain()); }
@Test public void testExtensionResponseParsingWithoutConfUpdate() throws Exception { ConfigurationListener<ExtenderConfiguration> extenderConfigurationListener = mock(ConfigurationListener.class); PduV2Factory pduFactoryWithConfHandler = new PduV2Factory(new ExtenderPduV2Factory(extenderConfigurationListener)); KSIRequestContext context = new KSIRequestContext(4846851148188931472L, 42L, 42L); ExtensionResponse response = pduFactoryWithConfHandler.readExtensionResponse(context, CREDENTIALS, loadTlv("pdu/extension/extension-response-v2.tlv")); Assert.assertNotNull(response); verify(extenderConfigurationListener, times(0)).updated(any(ExtenderConfiguration.class)); verify(extenderConfigurationListener, times(0)).updateFailed(any(Throwable.class)); }
@Test public void testExtensionResponseParsingWithConfigurationUpdate() throws Exception { ArgumentCaptor<ExtenderConfiguration> argumentCaptor = ArgumentCaptor.forClass(ExtenderConfiguration.class); ConfigurationListener<ExtenderConfiguration> extenderConfigurationListener = mock(ConfigurationListener.class); PduV2Factory pduFactoryWithConfHandler = new PduV2Factory(new ExtenderPduV2Factory(extenderConfigurationListener)); KSIRequestContext context = new KSIRequestContext(8396215651691691389L, 42L, 42L); ExtensionResponse response = pduFactoryWithConfHandler.readExtensionResponse(context, CREDENTIALS, loadTlv("pdu/extension/extension-response-v2-multiple-mixed-and-duplicate-payloads.tlv")); verify(extenderConfigurationListener, times(1)).updated(argumentCaptor.capture()); verify(extenderConfigurationListener, times(0)).updateFailed(any(Throwable.class)); ExtenderConfiguration configuration = argumentCaptor.getValue(); Assert.assertEquals((long) configuration.getMaximumRequests(), 4L); Assert.assertEquals(configuration.getCalendarFirstTime(), new Date(5557150000L)); Assert.assertEquals(configuration.getCalendarLastTime(), new Date(1422630579000L)); for (String parent : configuration.getParents()){ Assert.assertTrue(parent.contains(".url")); } Assert.assertNotNull(response); }
@Test public void testExtensionResponseParsingWithBrokenConfigurationUpdate() throws Exception { ArgumentCaptor<Throwable> argumentCaptor = ArgumentCaptor.forClass(Throwable.class); ConfigurationListener<ExtenderConfiguration> extenderConfigurationListener = mock(ConfigurationListener.class); PduV2Factory pduFactoryWithConfHandler = new PduV2Factory(new ExtenderPduV2Factory(extenderConfigurationListener)); KSIRequestContext context = new KSIRequestContext(8396215651691691389L, 42L, 42L); ExtensionResponse response = pduFactoryWithConfHandler.readExtensionResponse(context, CREDENTIALS, loadTlv("pdu/extension/extension-response-v2-with-broken-conf.tlv")); Assert.assertNotNull(response); verify(extenderConfigurationListener, times(0)).updated(any(ExtenderConfiguration.class)); verify(extenderConfigurationListener, times(1)).updateFailed(argumentCaptor.capture()); Assert.assertEquals(argumentCaptor.getValue().getMessage(), "Unknown critical TLV element with tag=0x5 encountered"); }