Search in sources :

Example 1 with ProtocolSelectorFactory

use of io.netty.handler.ssl.JdkApplicationProtocolNegotiator.ProtocolSelectorFactory in project netty by netty.

the class JdkSslEngineTest method testAlpnNoCompatibleProtocolsClientHandshakeFailure.

@Test
public void testAlpnNoCompatibleProtocolsClientHandshakeFailure() throws Exception {
    try {
        // initialization error.
        if (!JdkAlpnSslEngine.isAvailable()) {
            throw tlsExtensionNotFound(Protocol.ALPN);
        }
        SelfSignedCertificate ssc = new SelfSignedCertificate();
        JdkApplicationProtocolNegotiator clientApn = new JdkAlpnApplicationProtocolNegotiator(true, true, PREFERRED_APPLICATION_LEVEL_PROTOCOL);
        JdkApplicationProtocolNegotiator serverApn = new JdkAlpnApplicationProtocolNegotiator(new ProtocolSelectorFactory() {

            @Override
            public ProtocolSelector newSelector(SSLEngine engine, Set<String> supportedProtocols) {
                return new ProtocolSelector() {

                    @Override
                    public void unsupported() {
                    }

                    @Override
                    public String select(List<String> protocols) {
                        return APPLICATION_LEVEL_PROTOCOL_NOT_COMPATIBLE;
                    }
                };
            }
        }, JdkBaseApplicationProtocolNegotiator.FAIL_SELECTION_LISTENER_FACTORY, APPLICATION_LEVEL_PROTOCOL_NOT_COMPATIBLE);
        SslContext serverSslCtx = new JdkSslServerContext(ssc.certificate(), ssc.privateKey(), null, null, IdentityCipherSuiteFilter.INSTANCE, serverApn, 0, 0);
        SslContext clientSslCtx = new JdkSslClientContext(null, InsecureTrustManagerFactory.INSTANCE, null, IdentityCipherSuiteFilter.INSTANCE, clientApn, 0, 0);
        setupHandlers(serverSslCtx, clientSslCtx);
        assertTrue(clientLatch.await(2, TimeUnit.SECONDS));
        assertTrue(clientException instanceof SSLHandshakeException);
    } catch (SkipTestException e) {
        // ALPN availability is dependent on the java version. If ALPN is not available because of
        // java version incompatibility don't fail the test, but instead just skip the test
        assumeNoException(e);
    }
}
Also used : SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) SSLEngine(javax.net.ssl.SSLEngine) ProtocolSelectorFactory(io.netty.handler.ssl.JdkApplicationProtocolNegotiator.ProtocolSelectorFactory) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) ProtocolSelector(io.netty.handler.ssl.JdkApplicationProtocolNegotiator.ProtocolSelector) Test(org.junit.Test)

Aggregations

ProtocolSelector (io.netty.handler.ssl.JdkApplicationProtocolNegotiator.ProtocolSelector)1 ProtocolSelectorFactory (io.netty.handler.ssl.JdkApplicationProtocolNegotiator.ProtocolSelectorFactory)1 SelfSignedCertificate (io.netty.handler.ssl.util.SelfSignedCertificate)1 SSLEngine (javax.net.ssl.SSLEngine)1 SSLHandshakeException (javax.net.ssl.SSLHandshakeException)1 Test (org.junit.Test)1