Search in sources :

Example 11 with DefaultTestCerts

use of io.servicetalk.test.resources.DefaultTestCerts in project servicetalk by apple.

the class SslProvidersTest method setUp.

private void setUp(SslProvider serverSslProvider, SslProvider clientSslProvider, int payloadLength) throws Exception {
    payloadBody = randomString(payloadLength);
    serverContext = HttpServers.forAddress(localAddress(0)).sslConfig(new ServerSslConfigBuilder(DefaultTestCerts::loadServerPem, DefaultTestCerts::loadServerKey).provider(serverSslProvider).build()).listenBlockingAndAwait((ctx, request, responseFactory) -> {
        assertThat(ctx.sslSession(), is(notNullValue()));
        assertThat(request.path(), is("/path"));
        assertThat(request.headers().get(CONTENT_TYPE), is(TEXT_PLAIN_UTF_8));
        assertThat(request.payloadBody(textSerializerUtf8()), is("request-payload-body-" + payloadBody));
        return responseFactory.ok().payloadBody("response-payload-body-" + payloadBody, textSerializerUtf8());
    });
    client = HttpClients.forSingleAddress(serverHostAndPort(serverContext)).ioExecutor(NettyIoExecutors.createIoExecutor("client-io")).sslConfig(new ClientSslConfigBuilder(DefaultTestCerts::loadServerCAPem).peerHost(serverPemHostname()).provider(clientSslProvider).build()).buildBlocking();
}
Also used : ServerSslConfigBuilder(io.servicetalk.transport.api.ServerSslConfigBuilder) CONTENT_TYPE(io.servicetalk.http.api.HttpHeaderNames.CONTENT_TYPE) OPENSSL(io.servicetalk.transport.api.SslProvider.OPENSSL) SslProvider(io.servicetalk.transport.api.SslProvider) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) HttpSerializers.textSerializerUtf8(io.servicetalk.http.api.HttpSerializers.textSerializerUtf8) AddressUtils.serverHostAndPort(io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort) DefaultTestCerts(io.servicetalk.test.resources.DefaultTestCerts) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) JDK(io.servicetalk.transport.api.SslProvider.JDK) MethodSource(org.junit.jupiter.params.provider.MethodSource) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) NettyIoExecutors(io.servicetalk.transport.netty.NettyIoExecutors) AddressUtils.localAddress(io.servicetalk.transport.netty.internal.AddressUtils.localAddress) ServerContext(io.servicetalk.transport.api.ServerContext) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) HttpResponse(io.servicetalk.http.api.HttpResponse) Arguments(org.junit.jupiter.params.provider.Arguments) OK(io.servicetalk.http.api.HttpResponseStatus.OK) DefaultTestCerts.serverPemHostname(io.servicetalk.test.resources.DefaultTestCerts.serverPemHostname) AfterEach(org.junit.jupiter.api.AfterEach) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Stream(java.util.stream.Stream) TEXT_PLAIN_UTF_8(io.servicetalk.http.api.HttpHeaderValues.TEXT_PLAIN_UTF_8) ClientSslConfigBuilder(io.servicetalk.transport.api.ClientSslConfigBuilder) Matchers.is(org.hamcrest.Matchers.is) DefaultTestCerts(io.servicetalk.test.resources.DefaultTestCerts) ClientSslConfigBuilder(io.servicetalk.transport.api.ClientSslConfigBuilder) ServerSslConfigBuilder(io.servicetalk.transport.api.ServerSslConfigBuilder)

Example 12 with DefaultTestCerts

use of io.servicetalk.test.resources.DefaultTestCerts in project servicetalk by apple.

the class SslAndNonSslConnectionsTest method beforeClass.

@BeforeAll
static void beforeClass() throws Exception {
    final HttpHeaders httpHeaders = DefaultHttpHeadersFactory.INSTANCE.newHeaders().set(CONTENT_LENGTH, ZERO);
    // Configure HTTP server
    when(STREAMING_HTTP_SERVICE.handle(any(), any(), any())).thenAnswer((Answer<Single<StreamingHttpResponse>>) invocation -> {
        StreamingHttpResponseFactory factory = invocation.getArgument(2);
        StreamingHttpResponse resp = factory.ok();
        resp.headers().set(httpHeaders);
        return succeeded(resp);
    });
    when(STREAMING_HTTP_SERVICE.closeAsync()).thenReturn(completed());
    when(STREAMING_HTTP_SERVICE.closeAsyncGracefully()).thenReturn(completed());
    serverCtx = HttpServers.forAddress(localAddress(0)).executionStrategy(offloadNever()).listenStreamingAndAwait(STREAMING_HTTP_SERVICE);
    final String serverHostHeader = hostHeader(serverHostAndPort(serverCtx));
    requestTarget = "http://" + serverHostHeader + "/";
    // Configure HTTPS server
    when(SECURE_STREAMING_HTTP_SERVICE.handle(any(), any(), any())).thenAnswer(invocation -> {
        StreamingHttpResponseFactory factory = invocation.getArgument(2);
        StreamingHttpResponse resp = factory.ok();
        resp.headers().set(httpHeaders);
        return succeeded(resp);
    });
    when(SECURE_STREAMING_HTTP_SERVICE.closeAsync()).thenReturn(completed());
    when(SECURE_STREAMING_HTTP_SERVICE.closeAsyncGracefully()).thenReturn(completed());
    secureServerCtx = HttpServers.forAddress(localAddress(0)).sslConfig(new ServerSslConfigBuilder(DefaultTestCerts::loadServerPem, DefaultTestCerts::loadServerKey).build()).executionStrategy(offloadNever()).listenStreamingAndAwait(SECURE_STREAMING_HTTP_SERVICE);
    final String secureServerHostHeader = hostHeader(serverHostAndPort(secureServerCtx));
    secureRequestTarget = "https://" + secureServerHostHeader + "/";
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) ServerSslConfigBuilder(io.servicetalk.transport.api.ServerSslConfigBuilder) HttpHeaders(io.servicetalk.http.api.HttpHeaders) AfterAll(org.junit.jupiter.api.AfterAll) InetAddress(java.net.InetAddress) Answer(org.mockito.stubbing.Answer) ZERO(io.servicetalk.http.api.HttpHeaderValues.ZERO) BeforeAll(org.junit.jupiter.api.BeforeAll) Single.succeeded(io.servicetalk.concurrent.api.Single.succeeded) AddressUtils.hostHeader(io.servicetalk.transport.netty.internal.AddressUtils.hostHeader) AddressUtils.serverHostAndPort(io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort) DefaultTestCerts(io.servicetalk.test.resources.DefaultTestCerts) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Nullable(javax.annotation.Nullable) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) AddressUtils.localAddress(io.servicetalk.transport.netty.internal.AddressUtils.localAddress) ServerContext(io.servicetalk.transport.api.ServerContext) Mockito.clearInvocations(org.mockito.Mockito.clearInvocations) ClosedChannelException(java.nio.channels.ClosedChannelException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) Single(io.servicetalk.concurrent.api.Single) HttpResponse(io.servicetalk.http.api.HttpResponse) Mockito.times(org.mockito.Mockito.times) CertificateException(java.security.cert.CertificateException) Mockito.when(org.mockito.Mockito.when) CONTENT_LENGTH(io.servicetalk.http.api.HttpHeaderNames.CONTENT_LENGTH) OK(io.servicetalk.http.api.HttpResponseStatus.OK) DefaultTestCerts.serverPemHostname(io.servicetalk.test.resources.DefaultTestCerts.serverPemHostname) Mockito.verify(org.mockito.Mockito.verify) DefaultHttpHeadersFactory(io.servicetalk.http.api.DefaultHttpHeadersFactory) Test(org.junit.jupiter.api.Test) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) AfterEach(org.junit.jupiter.api.AfterEach) Mockito.never(org.mockito.Mockito.never) StreamingHttpService(io.servicetalk.http.api.StreamingHttpService) Completable.completed(io.servicetalk.concurrent.api.Completable.completed) StreamingHttpResponseFactory(io.servicetalk.http.api.StreamingHttpResponseFactory) ClientSslConfigBuilder(io.servicetalk.transport.api.ClientSslConfigBuilder) Matchers.is(org.hamcrest.Matchers.is) HostAndPort(io.servicetalk.transport.api.HostAndPort) HttpExecutionStrategies.offloadNever(io.servicetalk.http.api.HttpExecutionStrategies.offloadNever) Mockito.mock(org.mockito.Mockito.mock) HttpHeaders(io.servicetalk.http.api.HttpHeaders) Single(io.servicetalk.concurrent.api.Single) StreamingHttpResponseFactory(io.servicetalk.http.api.StreamingHttpResponseFactory) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) ServerSslConfigBuilder(io.servicetalk.transport.api.ServerSslConfigBuilder) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 13 with DefaultTestCerts

use of io.servicetalk.test.resources.DefaultTestCerts in project servicetalk by apple.

the class TcpFastOpenTest method requestSucceedsEvenIfTcpFastOpenNotEnabledOrSupported.

@ParameterizedTest(name = "{displayName} [{index}] protocols={0}, secure={1}, serverListenOptions={2}, clientOptions={3}")
@MethodSource("sslProviders")
void requestSucceedsEvenIfTcpFastOpenNotEnabledOrSupported(final Collection<HttpProtocol> protocols, final boolean secure, @SuppressWarnings("rawtypes") final Map<SocketOption, Object> serverListenOptions, @SuppressWarnings("rawtypes") final Map<SocketOption, Object> clientOptions) throws Exception {
    assumeTcpFastOpen(clientOptions);
    HttpServerBuilder serverBuilder = HttpServers.forAddress(localAddress(0)).protocols(toConfigs(protocols));
    if (secure) {
        serverBuilder.sslConfig(new ServerSslConfigBuilder(DefaultTestCerts::loadServerPem, DefaultTestCerts::loadServerKey).build());
    }
    for (@SuppressWarnings("rawtypes") Entry<SocketOption, Object> entry : serverListenOptions.entrySet()) {
        @SuppressWarnings("unchecked") SocketOption<Object> option = entry.getKey();
        serverBuilder.listenSocketOption(option, entry.getValue());
    }
    try (ServerContext serverContext = serverBuilder.listenBlockingAndAwait((ctx, request, responseFactory) -> responseFactory.ok());
        BlockingHttpClient client = newClient(serverContext, protocols, secure, clientOptions)) {
        assertEquals(HttpResponseStatus.OK, client.request(client.get("/")).status());
    }
}
Also used : SocketOption(java.net.SocketOption) ServerContext(io.servicetalk.transport.api.ServerContext) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) HttpServerBuilder(io.servicetalk.http.api.HttpServerBuilder) DefaultTestCerts(io.servicetalk.test.resources.DefaultTestCerts) ServerSslConfigBuilder(io.servicetalk.transport.api.ServerSslConfigBuilder) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 14 with DefaultTestCerts

use of io.servicetalk.test.resources.DefaultTestCerts in project servicetalk by apple.

the class SecurityHandshakeObserverTest method verifyHandshakeObserved.

private void verifyHandshakeObserved(Function<SocketAddress, HttpServerBuilder> serverBuilderFactory, Function<HostAndPort, SingleAddressHttpClientBuilder<HostAndPort, InetSocketAddress>> clientBuilderFactory) throws Exception {
    try (ServerContext serverContext = serverBuilderFactory.apply(localAddress(0)).ioExecutor(SERVER_CTX.ioExecutor()).executor(SERVER_CTX.executor()).executionStrategy(defaultStrategy()).sslConfig(new ServerSslConfigBuilder(DefaultTestCerts::loadServerPem, DefaultTestCerts::loadServerKey).build()).transportObserver(serverTransportObserver).listenStreamingAndAwait(new TestServiceStreaming());
        BlockingHttpClient client = clientBuilderFactory.apply(serverHostAndPort(serverContext)).ioExecutor(CLIENT_CTX.ioExecutor()).executor(CLIENT_CTX.executor()).executionStrategy(defaultStrategy()).sslConfig(new ClientSslConfigBuilder(DefaultTestCerts::loadServerCAPem).peerHost(serverPemHostname()).build()).appendConnectionFactoryFilter(new TransportObserverConnectionFactoryFilter<>(clientTransportObserver)).buildBlocking()) {
        String content = "payload_body";
        HttpResponse response = client.request(client.post(SVC_ECHO).payloadBody(content, textSerializerUtf8()));
        assertThat(response.status(), is(OK));
        assertThat(response.payloadBody(textSerializerUtf8()), equalTo(content));
        verify(clientConnectionObserver).onSecurityHandshake();
        verify(clientSecurityHandshakeObserver).handshakeComplete(any(SSLSession.class));
        verify(serverConnectionObserver).onSecurityHandshake();
        verify(serverSecurityHandshakeObserver).handshakeComplete(any(SSLSession.class));
    }
}
Also used : ServerContext(io.servicetalk.transport.api.ServerContext) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) TransportObserverConnectionFactoryFilter(io.servicetalk.client.api.TransportObserverConnectionFactoryFilter) SSLSession(javax.net.ssl.SSLSession) HttpResponse(io.servicetalk.http.api.HttpResponse) DefaultTestCerts(io.servicetalk.test.resources.DefaultTestCerts) ClientSslConfigBuilder(io.servicetalk.transport.api.ClientSslConfigBuilder) ServerSslConfigBuilder(io.servicetalk.transport.api.ServerSslConfigBuilder)

Example 15 with DefaultTestCerts

use of io.servicetalk.test.resources.DefaultTestCerts in project servicetalk by apple.

the class SniTest method noSniClientDefaultServerFallbackSuccess.

@ParameterizedTest(name = "protocols={0}, alpn={1}")
@MethodSource("protocolsAndAlpn")
void noSniClientDefaultServerFallbackSuccess(List<HttpProtocol> protocols, boolean useALPN) throws Exception {
    try (ServerContext serverContext = HttpServers.forAddress(localAddress(0)).protocols(protocolConfigs(protocols)).sslConfig(trustedServerConfig(alpnIds(protocols, useALPN)), singletonMap("no_match" + SNI_HOSTNAME, untrustedServerConfig())).listenBlockingAndAwait(newSslVerifyService());
        BlockingHttpClient client = HttpClients.forSingleAddress(getLoopbackAddress().getHostName(), serverHostAndPort(serverContext).port()).protocols(protocolConfigs(protocols)).sslConfig(configureAlpn(new ClientSslConfigBuilder(DefaultTestCerts::loadServerCAPem).peerHost(serverPemHostname()), protocols, useALPN).build()).inferSniHostname(false).buildBlocking()) {
        HttpRequest request = client.get("/");
        ReservedBlockingHttpConnection reserved = client.reserveConnection(request);
        assertNotNull(reserved.connectionContext().sslSession());
        assertEquals(HttpResponseStatus.OK, reserved.request(request).status());
        reserved.release();
    }
}
Also used : HttpRequest(io.servicetalk.http.api.HttpRequest) ServerContext(io.servicetalk.transport.api.ServerContext) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) DefaultTestCerts(io.servicetalk.test.resources.DefaultTestCerts) ClientSslConfigBuilder(io.servicetalk.transport.api.ClientSslConfigBuilder) ReservedBlockingHttpConnection(io.servicetalk.http.api.ReservedBlockingHttpConnection) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

DefaultTestCerts (io.servicetalk.test.resources.DefaultTestCerts)21 ClientSslConfigBuilder (io.servicetalk.transport.api.ClientSslConfigBuilder)20 ServerContext (io.servicetalk.transport.api.ServerContext)15 ServerSslConfigBuilder (io.servicetalk.transport.api.ServerSslConfigBuilder)14 BlockingHttpClient (io.servicetalk.http.api.BlockingHttpClient)12 HostAndPort (io.servicetalk.transport.api.HostAndPort)9 AddressUtils.serverHostAndPort (io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort)9 HttpResponse (io.servicetalk.http.api.HttpResponse)8 DefaultTestCerts.serverPemHostname (io.servicetalk.test.resources.DefaultTestCerts.serverPemHostname)8 AddressUtils.localAddress (io.servicetalk.transport.netty.internal.AddressUtils.localAddress)8 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)8 Matchers.is (org.hamcrest.Matchers.is)8 Nullable (javax.annotation.Nullable)6 Matchers.instanceOf (org.hamcrest.Matchers.instanceOf)6 AfterEach (org.junit.jupiter.api.AfterEach)6 Assertions.assertThrows (org.junit.jupiter.api.Assertions.assertThrows)6 Test (org.junit.jupiter.api.Test)6 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)6 Single (io.servicetalk.concurrent.api.Single)5 OK (io.servicetalk.http.api.HttpResponseStatus.OK)5