This is an automated email from the ASF dual-hosted git repository. coheigea pushed a commit to branch CAMEL-13402 in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/CAMEL-13402 by this push: new bfe15d2 Adding more CoAP TLS tests bfe15d2 is described below commit bfe15d2986cb05ce5f5d6aacfecc6f504d5127f9 Author: Colm O hEigeartaigh <cohei...@apache.org> AuthorDate: Fri Apr 19 13:04:09 2019 +0100 Adding more CoAP TLS tests --- .../apache/camel/coap/CoAPComponentTLSTest.java | 142 +++++++++++++++++++-- .../camel-coap/src/test/resources/selfsigned.jks | Bin 0 -> 701 bytes .../camel-coap/src/test/resources/truststore2.jks | Bin 0 -> 717 bytes 3 files changed, 132 insertions(+), 10 deletions(-) diff --git a/components/camel-coap/src/test/java/org/apache/camel/coap/CoAPComponentTLSTest.java b/components/camel-coap/src/test/java/org/apache/camel/coap/CoAPComponentTLSTest.java index dfd5664..ef02c4c 100644 --- a/components/camel-coap/src/test/java/org/apache/camel/coap/CoAPComponentTLSTest.java +++ b/components/camel-coap/src/test/java/org/apache/camel/coap/CoAPComponentTLSTest.java @@ -17,8 +17,8 @@ package org.apache.camel.coap; import org.apache.camel.Exchange; -import org.apache.camel.Produce; -import org.apache.camel.ProducerTemplate; +import org.apache.camel.Message; +import org.apache.camel.Processor; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.mock.MockEndpoint; import org.apache.camel.support.jsse.KeyStoreParameters; @@ -29,20 +29,77 @@ import org.eclipse.californium.core.coap.MediaTypeRegistry; import org.junit.Test; public class CoAPComponentTLSTest extends CamelTestSupport { - + protected static final int PORT = AvailablePortFinder.getNextAvailable(); + protected static final int PORT2 = AvailablePortFinder.getNextAvailable(); + protected static final int PORT3 = AvailablePortFinder.getNextAvailable(); + protected static final int PORT4 = AvailablePortFinder.getNextAvailable(); + + @Test + public void testSuccessfulCall() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedMinimumMessageCount(1); + mock.expectedBodiesReceived("Hello Camel CoAP"); + mock.expectedHeaderReceived(Exchange.CONTENT_TYPE, MediaTypeRegistry.toString(MediaTypeRegistry.APPLICATION_OCTET_STREAM)); + mock.expectedHeaderReceived(CoAPConstants.COAP_RESPONSE_CODE, CoAP.ResponseCode.CONTENT.toString()); + sendBodyAndHeader("direct:start", "Camel CoAP", CoAPConstants.COAP_METHOD, "POST"); + assertMockEndpointsSatisfied(); + } - @Produce("direct:start") - protected ProducerTemplate sender; + @Test + public void testNoTruststore() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedMessageCount(0); + sendBodyAndHeader("direct:notruststore", "Camel CoAP", CoAPConstants.COAP_METHOD, "POST"); + assertMockEndpointsSatisfied(); + } + + @Test + public void testTrustValidationFailed() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedMessageCount(0); + sendBodyAndHeader("direct:failedtrust", "Camel CoAP", CoAPConstants.COAP_METHOD, "POST"); + assertMockEndpointsSatisfied(); + } + + @Test + public void testSelfSigned() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedMinimumMessageCount(1); + mock.expectedBodiesReceived("Hello Camel CoAP"); + mock.expectedHeaderReceived(Exchange.CONTENT_TYPE, MediaTypeRegistry.toString(MediaTypeRegistry.APPLICATION_OCTET_STREAM)); + mock.expectedHeaderReceived(CoAPConstants.COAP_RESPONSE_CODE, CoAP.ResponseCode.CONTENT.toString()); + sendBodyAndHeader("direct:selfsigned", "Camel CoAP", CoAPConstants.COAP_METHOD, "POST"); + assertMockEndpointsSatisfied(); + } + + @Test + public void testClientAuthentication() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedMinimumMessageCount(1); + mock.expectedBodiesReceived("Hello Camel CoAP"); + mock.expectedHeaderReceived(Exchange.CONTENT_TYPE, MediaTypeRegistry.toString(MediaTypeRegistry.APPLICATION_OCTET_STREAM)); + mock.expectedHeaderReceived(CoAPConstants.COAP_RESPONSE_CODE, CoAP.ResponseCode.CONTENT.toString()); + sendBodyAndHeader("direct:clientauth", "Camel CoAP", CoAPConstants.COAP_METHOD, "POST"); + assertMockEndpointsSatisfied(); + } + + @Test + public void testFailedClientAuthentication() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedMessageCount(0); + sendBodyAndHeader("direct:failedclientauth", "Camel CoAP", CoAPConstants.COAP_METHOD, "POST"); + assertMockEndpointsSatisfied(); + } @Test - public void testTLS() throws Exception { + public void testCipherSuites() throws Exception { MockEndpoint mock = getMockEndpoint("mock:result"); mock.expectedMinimumMessageCount(1); mock.expectedBodiesReceived("Hello Camel CoAP"); mock.expectedHeaderReceived(Exchange.CONTENT_TYPE, MediaTypeRegistry.toString(MediaTypeRegistry.APPLICATION_OCTET_STREAM)); mock.expectedHeaderReceived(CoAPConstants.COAP_RESPONSE_CODE, CoAP.ResponseCode.CONTENT.toString()); - sender.sendBodyAndHeader("Camel CoAP", CoAPConstants.COAP_METHOD, "POST"); + sendBodyAndHeader("direct:ciphersuites", "Camel CoAP", CoAPConstants.COAP_METHOD, "POST"); assertMockEndpointsSatisfied(); } @@ -51,14 +108,29 @@ public class CoAPComponentTLSTest extends CamelTestSupport { KeyStoreParameters keystoreParameters = new KeyStoreParameters(); keystoreParameters.setResource("service.jks"); keystoreParameters.setPassword("security"); - + + KeyStoreParameters keystoreParameters2 = new KeyStoreParameters(); + keystoreParameters2.setResource("selfsigned.jks"); + keystoreParameters2.setPassword("security"); + + KeyStoreParameters keystoreParameters3 = new KeyStoreParameters(); + keystoreParameters3.setResource("client.jks"); + keystoreParameters3.setPassword("security"); + KeyStoreParameters truststoreParameters = new KeyStoreParameters(); truststoreParameters.setResource("truststore.jks"); truststoreParameters.setPassword("storepass"); - + + KeyStoreParameters truststoreParameters2 = new KeyStoreParameters(); + truststoreParameters2.setResource("truststore2.jks"); + truststoreParameters2.setPassword("storepass"); + context.getRegistry().bind("keyParams", keystoreParameters); + context.getRegistry().bind("keyParams2", keystoreParameters2); + context.getRegistry().bind("keyParams3", keystoreParameters3); context.getRegistry().bind("trustParams", truststoreParameters); - + context.getRegistry().bind("trustParams2", truststoreParameters2); + return new RouteBuilder() { @Override public void configure() throws Exception { @@ -66,10 +138,60 @@ public class CoAPComponentTLSTest extends CamelTestSupport { + "keyStoreParameters=#keyParams", PORT) .transform(body().prepend("Hello ")); + fromF("coaps://localhost:%d/TestResource?alias=selfsigned&password=security&" + + "keyStoreParameters=#keyParams2", PORT2) + .transform(body().prepend("Hello ")); + + fromF("coaps://localhost:%d/TestResource?alias=service&password=security&" + + "trustStoreParameters=#trustParams&" + + "keyStoreParameters=#keyParams&clientAuthentication=REQUIRE", PORT3) + .transform(body().prepend("Hello ")); + + fromF("coaps://localhost:%d/TestResource?alias=service&password=security&" + + "keyStoreParameters=#keyParams&cipherSuites=TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8", PORT4) + .transform(body().prepend("Hello ")); + from("direct:start") .toF("coaps://localhost:%d/TestResource?trustStoreParameters=#trustParams", PORT) .to("mock:result"); + + from("direct:notruststore") + .toF("coaps://localhost:%d/TestResource", PORT) + .to("mock:result"); + + from("direct:failedtrust") + .toF("coaps://localhost:%d/TestResource?trustStoreParameters=#trustParams2", PORT) + .to("mock:result"); + + from("direct:selfsigned") + .toF("coaps://localhost:%d/TestResource?trustStoreParameters=#keyParams2", PORT2) + .to("mock:result"); + + from("direct:clientauth") + .toF("coaps://localhost:%d/TestResource?trustStoreParameters=#trustParams&" + + "keyStoreParameters=#keyParams3&alias=client&password=security", PORT3) + .to("mock:result"); + + from("direct:failedclientauth") + .toF("coaps://localhost:%d/TestResource?trustStoreParameters=#trustParams&" + + "keyStoreParameters=#keyParams2&alias=selfsigned&password=security", PORT3) + .to("mock:result"); + + from("direct:ciphersuites") + .toF("coaps://localhost:%d/TestResource?trustStoreParameters=#trustParams&" + + "cipherSuites=TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8", PORT4) + .to("mock:result"); } }; } + + protected void sendBodyAndHeader(String endpointUri, final Object body, String headerName, String headerValue) { + template.send(endpointUri, new Processor() { + public void process(Exchange exchange) { + Message in = exchange.getIn(); + in.setBody(body); + in.setHeader(headerName, headerValue); + } + }); + } } diff --git a/components/camel-coap/src/test/resources/selfsigned.jks b/components/camel-coap/src/test/resources/selfsigned.jks new file mode 100644 index 0000000..ee745ff Binary files /dev/null and b/components/camel-coap/src/test/resources/selfsigned.jks differ diff --git a/components/camel-coap/src/test/resources/truststore2.jks b/components/camel-coap/src/test/resources/truststore2.jks new file mode 100644 index 0000000..94249d6 Binary files /dev/null and b/components/camel-coap/src/test/resources/truststore2.jks differ