Search in sources :

Example 71 with TestContext

use of io.vertx.ext.unit.TestContext in project vertx-proton by vert-x3.

the class ProtonServerImplTest method testCustomAuthenticatorFailsAuthentication.

@Test(timeout = 20000)
public void testCustomAuthenticatorFailsAuthentication(TestContext context) {
    Async connectedAsync = context.async();
    ProtonServer.create(vertx).saslAuthenticatorFactory(new TestPlainAuthenticatorFactory()).connectHandler(protonConnection -> {
        context.fail("Handler should not be called for connection that failed authentication");
    }).listen(server -> ProtonClient.create(vertx).connect("localhost", server.result().actualPort(), BAD_USER, PASSWD, protonConnectionAsyncResult -> {
        context.assertFalse(protonConnectionAsyncResult.succeeded());
        connectedAsync.complete();
    }));
    connectedAsync.awaitSuccess();
}
Also used : TestContext(io.vertx.ext.unit.TestContext) ProtonConnection(io.vertx.proton.ProtonConnection) Async(io.vertx.ext.unit.Async) ProtonSaslAuthenticator(io.vertx.proton.sasl.ProtonSaslAuthenticator) Vertx(io.vertx.core.Vertx) RunWith(org.junit.runner.RunWith) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ProtonClient(io.vertx.proton.ProtonClient) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Sasl(org.apache.qpid.proton.engine.Sasl) SaslOutcome(org.apache.qpid.proton.engine.Sasl.SaslOutcome) Context(io.vertx.core.Context) ProtonServer(io.vertx.proton.ProtonServer) StandardCharsets(java.nio.charset.StandardCharsets) Transport(org.apache.qpid.proton.engine.Transport) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) After(org.junit.After) Handler(io.vertx.core.Handler) ProtonSaslAuthenticatorFactory(io.vertx.proton.sasl.ProtonSaslAuthenticatorFactory) NetSocket(io.vertx.core.net.NetSocket) Before(org.junit.Before) Async(io.vertx.ext.unit.Async) Test(org.junit.Test)

Example 72 with TestContext

use of io.vertx.ext.unit.TestContext in project vertx-proton by vert-x3.

the class ProtonServerImplTest method doTestAsyncServerAuthenticatorTestImpl.

private void doTestAsyncServerAuthenticatorTestImpl(TestContext context, boolean passAuthentication) {
    Async connectAsync = context.async();
    AtomicBoolean connectedServer = new AtomicBoolean();
    final long delay = 750;
    TestAsyncAuthenticator testAsyncAuthenticator = new TestAsyncAuthenticator(delay, passAuthentication);
    TestAsyncAuthenticatorFactory authenticatorFactory = new TestAsyncAuthenticatorFactory(testAsyncAuthenticator);
    ProtonServer.create(vertx).saslAuthenticatorFactory(authenticatorFactory).connectHandler(protonConnection -> {
        connectedServer.set(true);
    }).listen(server -> {
        final long startTime = System.currentTimeMillis();
        ProtonClient.create(vertx).connect("localhost", server.result().actualPort(), GOOD_USER, PASSWD, conResult -> {
            // Verify the process took expected time from auth delay.
            long actual = System.currentTimeMillis() - startTime;
            context.assertTrue(actual >= delay, "Connect completed before expected time delay elapsed! " + actual);
            if (passAuthentication) {
                context.assertTrue(conResult.succeeded(), "Expected connect to succeed");
                conResult.result().disconnect();
            } else {
                context.assertFalse(conResult.succeeded(), "Expected connect to fail");
            }
            connectAsync.complete();
        });
    });
    connectAsync.awaitSuccess();
    if (passAuthentication) {
        context.assertTrue(connectedServer.get(), "Server handler should have been called");
    } else {
        context.assertFalse(connectedServer.get(), "Server handler should not have been called");
    }
    context.assertEquals(1, authenticatorFactory.getCreateCount(), "unexpected authenticator creation count");
}
Also used : TestContext(io.vertx.ext.unit.TestContext) ProtonConnection(io.vertx.proton.ProtonConnection) Async(io.vertx.ext.unit.Async) ProtonSaslAuthenticator(io.vertx.proton.sasl.ProtonSaslAuthenticator) Vertx(io.vertx.core.Vertx) RunWith(org.junit.runner.RunWith) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ProtonClient(io.vertx.proton.ProtonClient) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Sasl(org.apache.qpid.proton.engine.Sasl) SaslOutcome(org.apache.qpid.proton.engine.Sasl.SaslOutcome) Context(io.vertx.core.Context) ProtonServer(io.vertx.proton.ProtonServer) StandardCharsets(java.nio.charset.StandardCharsets) Transport(org.apache.qpid.proton.engine.Transport) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) After(org.junit.After) Handler(io.vertx.core.Handler) ProtonSaslAuthenticatorFactory(io.vertx.proton.sasl.ProtonSaslAuthenticatorFactory) NetSocket(io.vertx.core.net.NetSocket) Before(org.junit.Before) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Async(io.vertx.ext.unit.Async)

Example 73 with TestContext

use of io.vertx.ext.unit.TestContext in project vertx-proton by vert-x3.

the class ProtonClientSaslTest method doConnectWithGivenCredentialsTestImpl.

private void doConnectWithGivenCredentialsTestImpl(TestContext context, ProtonClientOptions options, String username, String password, boolean expectConnectToSucceed) {
    Async async = context.async();
    // Connect the client and open the connection to verify it works
    ProtonClient client = ProtonClient.create(vertx);
    client.connect(options, "localhost", getBrokerAmqpConnectorPort(), username, password, res -> {
        if (expectConnectToSucceed) {
            // Expect connect to succeed
            context.assertTrue(res.succeeded());
            ProtonConnection connection = res.result();
            connection.openHandler(connRes -> {
                context.assertTrue(connRes.succeeded());
                LOG.trace("Client connection open");
                async.complete();
            }).open();
        } else {
            // Expect connect to fail
            context.assertFalse(res.succeeded());
            LOG.trace("Connect failed");
            async.complete();
        }
    });
    async.awaitSuccess();
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) ProtonSaslAnonymousImpl(io.vertx.proton.sasl.impl.ProtonSaslAnonymousImpl) After(org.junit.After) RunWith(org.junit.runner.RunWith) Vertx(io.vertx.core.Vertx) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Logger(io.vertx.core.logging.Logger) LoggerFactory(io.vertx.core.logging.LoggerFactory) Before(org.junit.Before) Async(io.vertx.ext.unit.Async)

Example 74 with TestContext

use of io.vertx.ext.unit.TestContext in project vertx-proton by vert-x3.

the class ProtonClientSslTest method doHostnameVerificationTestImpl.

private void doHostnameVerificationTestImpl(TestContext context, boolean verifyHost) throws Exception {
    Async async = context.async();
    // Create a server that accept a connection and expects a client connection+session+receiver
    ProtonServerOptions serverOptions = new ProtonServerOptions();
    serverOptions.setSsl(true);
    PfxOptions serverPfxOptions = new PfxOptions().setPath(WRONG_HOST_KEYSTORE).setPassword(PASSWORD);
    serverOptions.setPfxKeyCertOptions(serverPfxOptions);
    protonServer = createServer(serverOptions, this::handleClientConnectionSessionReceiverOpen);
    // Connect the client and open a receiver to verify the connection works
    ProtonClientOptions clientOptions = new ProtonClientOptions();
    clientOptions.setSsl(true);
    PfxOptions clientPfxOptions = new PfxOptions().setPath(TRUSTSTORE).setPassword(PASSWORD);
    clientOptions.setPfxTrustOptions(clientPfxOptions);
    // Verify/update the hostname verification settings
    context.assertEquals(VERIFY_HTTPS, clientOptions.getHostnameVerificationAlgorithm(), "expected host verification to be on by default");
    if (!verifyHost) {
        clientOptions.setHostnameVerificationAlgorithm(NO_VERIFY);
    }
    ProtonClient client = ProtonClient.create(vertx);
    client.connect(clientOptions, "localhost", protonServer.actualPort(), res -> {
        if (verifyHost) {
            // Expect connect to fail as server cert hostname doesn't match.
            context.assertFalse(res.succeeded(), "expected connect to fail");
            LOG.trace("Connect failed");
            async.complete();
        } else {
            // Expect connect to succeed as verification is disabled
            context.assertTrue(res.succeeded(), "expected connect to succeed");
            LOG.trace("Connect succeeded");
            ProtonConnection connection = res.result();
            connection.open();
            ProtonReceiver receiver = connection.createReceiver("some-address");
            receiver.openHandler(recvResult -> {
                context.assertTrue(recvResult.succeeded());
                LOG.trace("Client receiver open");
                async.complete();
            }).open();
        }
    });
    async.awaitSuccess();
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) RunWith(org.junit.runner.RunWith) Vertx(io.vertx.core.Vertx) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) LoggerFactory(io.vertx.core.logging.LoggerFactory) PfxOptions(io.vertx.core.net.PfxOptions) ExecutionException(java.util.concurrent.ExecutionException) After(org.junit.After) ClientAuth(io.vertx.core.http.ClientAuth) AsyncResult(io.vertx.core.AsyncResult) Handler(io.vertx.core.Handler) Logger(io.vertx.core.logging.Logger) Before(org.junit.Before) Async(io.vertx.ext.unit.Async) PfxOptions(io.vertx.core.net.PfxOptions)

Example 75 with TestContext

use of io.vertx.ext.unit.TestContext in project hono by eclipse.

the class AbstractVertxBasedHttpProtocolAdapterTest method testStartUsesClientProvidedHttpServer.

/**
 * Verifies that a client provided HTTP server is started instead of creating and starting a new http server.
 *
 * @param ctx The helper to use for running async tests on vertx.
 */
@SuppressWarnings("unchecked")
@Test
public void testStartUsesClientProvidedHttpServer(final TestContext ctx) {
    // GIVEN an adapter with a client provided HTTP server
    final HttpServer server = getHttpServer(false);
    final AbstractVertxBasedHttpProtocolAdapter<HttpProtocolAdapterProperties> adapter = getAdapter(server, null);
    adapter.setCredentialsAuthProvider(credentialsAuthProvider);
    // WHEN starting the adapter
    final Async startup = ctx.async();
    Future<Void> startupTracker = Future.future();
    startupTracker.setHandler(ctx.asyncAssertSuccess(s -> {
        startup.complete();
    }));
    adapter.start(startupTracker);
    // THEN the client provided HTTP server has been configured and started
    startup.await();
    verify(server).requestHandler(any(Handler.class));
    verify(server).listen(any(Handler.class));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) HttpServerRequest(io.vertx.core.http.HttpServerRequest) ArgumentMatchers(org.mockito.ArgumentMatchers) ProtonDelivery(io.vertx.proton.ProtonDelivery) TenantConstants(org.eclipse.hono.util.TenantConstants) HttpServer(io.vertx.core.http.HttpServer) RunWith(org.junit.runner.RunWith) Router(io.vertx.ext.web.Router) ClientErrorException(org.eclipse.hono.client.ClientErrorException) RoutingContext(io.vertx.ext.web.RoutingContext) TenantClient(org.eclipse.hono.client.TenantClient) MessageSender(org.eclipse.hono.client.MessageSender) Timeout(org.junit.rules.Timeout) Message(org.apache.qpid.proton.message.Message) RegistrationClient(org.eclipse.hono.client.RegistrationClient) JsonObject(io.vertx.core.json.JsonObject) AsyncResult(io.vertx.core.AsyncResult) HonoClient(org.eclipse.hono.client.HonoClient) Before(org.junit.Before) RegistrationConstants(org.eclipse.hono.util.RegistrationConstants) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Future(io.vertx.core.Future) TenantObject(org.eclipse.hono.util.TenantObject) Mockito(org.mockito.Mockito) HonoClientBasedAuthProvider(org.eclipse.hono.service.auth.device.HonoClientBasedAuthProvider) Rule(org.junit.Rule) Buffer(io.vertx.core.buffer.Buffer) HttpServerResponse(io.vertx.core.http.HttpServerResponse) Handler(io.vertx.core.Handler) Async(io.vertx.ext.unit.Async) HttpServer(io.vertx.core.http.HttpServer) Handler(io.vertx.core.Handler) Test(org.junit.Test)

Aggregations

TestContext (io.vertx.ext.unit.TestContext)148 Test (org.junit.Test)147 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)141 RunWith (org.junit.runner.RunWith)141 Async (io.vertx.ext.unit.Async)123 Future (io.vertx.core.Future)121 Handler (io.vertx.core.Handler)112 Vertx (io.vertx.core.Vertx)103 HttpURLConnection (java.net.HttpURLConnection)100 Before (org.junit.Before)97 Timeout (org.junit.rules.Timeout)95 JsonObject (io.vertx.core.json.JsonObject)91 Rule (org.junit.Rule)83 Mockito (org.mockito.Mockito)74 Constants (org.eclipse.hono.util.Constants)68 Assert.assertThat (org.junit.Assert.assertThat)62 Context (io.vertx.core.Context)57 CoreMatchers.is (org.hamcrest.CoreMatchers.is)54 AsyncResult (io.vertx.core.AsyncResult)53 Buffer (io.vertx.core.buffer.Buffer)52