Search in sources :

Example 6 with MqttConnectionException

use of io.vertx.mqtt.MqttConnectionException in project hono by eclipse.

the class MqttConnectionIT method testConnectFailsForWrongCredentials.

/**
 * Verifies that the adapter rejects connection attempts from devices
 * using wrong credentials.
 *
 * @param ctx The test context
 */
@Test
public void testConnectFailsForWrongCredentials(final VertxTestContext ctx) {
    // GIVEN a registered device
    final Tenant tenant = new Tenant();
    helper.registry.addDeviceForTenant(tenantId, tenant, deviceId, password).compose(ok -> connectToAdapter(IntegrationTestSupport.getUsername(deviceId, tenantId), "wrong password")).onComplete(ctx.failing(t -> {
        // THEN the connection is refused
        ctx.verify(() -> {
            assertThat(t).isInstanceOf(MqttConnectionException.class);
            assertThat(((MqttConnectionException) t).code()).isEqualTo(MqttConnectReturnCode.CONNECTION_REFUSED_BAD_USER_NAME_OR_PASSWORD);
        });
        ctx.completeNow();
    }));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) VertxTestContext(io.vertx.junit5.VertxTestContext) BeforeEach(org.junit.jupiter.api.BeforeEach) X500Principal(javax.security.auth.x500.X500Principal) MqttConnectReturnCode(io.netty.handler.codec.mqtt.MqttConnectReturnCode) SelfSignedCertificate(io.vertx.core.net.SelfSignedCertificate) X509CertificateCredential(org.eclipse.hono.service.management.credentials.X509CertificateCredential) Supplier(java.util.function.Supplier) Constants(org.eclipse.hono.util.Constants) Tenant(org.eclipse.hono.service.management.tenant.Tenant) Timeout(io.vertx.junit5.Timeout) IntegrationTestSupport(org.eclipse.hono.tests.IntegrationTestSupport) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Assumptions.assumeTrue(org.junit.jupiter.api.Assumptions.assumeTrue) JsonObject(io.vertx.core.json.JsonObject) MqttConnectionException(io.vertx.mqtt.MqttConnectionException) Tenants(org.eclipse.hono.tests.Tenants) RegistryManagementConstants(org.eclipse.hono.util.RegistryManagementConstants) ValueSource(org.junit.jupiter.params.provider.ValueSource) Device(org.eclipse.hono.service.management.device.Device) Promise(io.vertx.core.Promise) X509CertificateSecret(org.eclipse.hono.service.management.credentials.X509CertificateSecret) RegistrationConstants(org.eclipse.hono.util.RegistrationConstants) UUID(java.util.UUID) Truth.assertThat(com.google.common.truth.Truth.assertThat) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) List(java.util.List) Adapter(org.eclipse.hono.util.Adapter) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Collections(java.util.Collections) PasswordCredential(org.eclipse.hono.service.management.credentials.PasswordCredential) Tenant(org.eclipse.hono.service.management.tenant.Tenant) MqttConnectionException(io.vertx.mqtt.MqttConnectionException) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 7 with MqttConnectionException

use of io.vertx.mqtt.MqttConnectionException in project hono by eclipse.

the class MqttConnectionIT method testConnectX509FailsForUnknownSubjectDN.

/**
 * Verifies that the adapter rejects connection attempts from devices using a client certificate with an unknown
 * subject DN.
 *
 * @param ctx The test context
 */
@Test
public void testConnectX509FailsForUnknownSubjectDN(final VertxTestContext ctx) {
    // GIVEN a registered device
    helper.getCertificate(deviceCert.certificatePath()).compose(cert -> {
        final var tenant = Tenants.createTenantForTrustAnchor(cert);
        return helper.registry.addTenant(tenantId, tenant);
    }).compose(ok -> helper.registry.registerDevice(tenantId, deviceId)).compose(ok -> {
        final String authId = new X500Principal("CN=4711").getName(X500Principal.RFC2253);
        final var credential = X509CertificateCredential.fromSubjectDn(authId, List.of(new X509CertificateSecret()));
        return helper.registry.addCredentials(tenantId, deviceId, Collections.singleton(credential));
    }).compose(ok -> connectToAdapter(deviceCert)).onComplete(ctx.failing(t -> {
        // THEN the connection is refused
        ctx.verify(() -> {
            assertThat(t).isInstanceOf(MqttConnectionException.class);
            assertThat(((MqttConnectionException) t).code()).isEqualTo(MqttConnectReturnCode.CONNECTION_REFUSED_BAD_USER_NAME_OR_PASSWORD);
        });
        ctx.completeNow();
    }));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) VertxTestContext(io.vertx.junit5.VertxTestContext) BeforeEach(org.junit.jupiter.api.BeforeEach) X500Principal(javax.security.auth.x500.X500Principal) MqttConnectReturnCode(io.netty.handler.codec.mqtt.MqttConnectReturnCode) SelfSignedCertificate(io.vertx.core.net.SelfSignedCertificate) X509CertificateCredential(org.eclipse.hono.service.management.credentials.X509CertificateCredential) Supplier(java.util.function.Supplier) Constants(org.eclipse.hono.util.Constants) Tenant(org.eclipse.hono.service.management.tenant.Tenant) Timeout(io.vertx.junit5.Timeout) IntegrationTestSupport(org.eclipse.hono.tests.IntegrationTestSupport) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Assumptions.assumeTrue(org.junit.jupiter.api.Assumptions.assumeTrue) JsonObject(io.vertx.core.json.JsonObject) MqttConnectionException(io.vertx.mqtt.MqttConnectionException) Tenants(org.eclipse.hono.tests.Tenants) RegistryManagementConstants(org.eclipse.hono.util.RegistryManagementConstants) ValueSource(org.junit.jupiter.params.provider.ValueSource) Device(org.eclipse.hono.service.management.device.Device) Promise(io.vertx.core.Promise) X509CertificateSecret(org.eclipse.hono.service.management.credentials.X509CertificateSecret) RegistrationConstants(org.eclipse.hono.util.RegistrationConstants) UUID(java.util.UUID) Truth.assertThat(com.google.common.truth.Truth.assertThat) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) List(java.util.List) Adapter(org.eclipse.hono.util.Adapter) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Collections(java.util.Collections) PasswordCredential(org.eclipse.hono.service.management.credentials.PasswordCredential) MqttConnectionException(io.vertx.mqtt.MqttConnectionException) X500Principal(javax.security.auth.x500.X500Principal) X509CertificateSecret(org.eclipse.hono.service.management.credentials.X509CertificateSecret) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

Truth.assertThat (com.google.common.truth.Truth.assertThat)7 MqttConnectReturnCode (io.netty.handler.codec.mqtt.MqttConnectReturnCode)7 Future (io.vertx.core.Future)7 Promise (io.vertx.core.Promise)7 JsonObject (io.vertx.core.json.JsonObject)7 SelfSignedCertificate (io.vertx.core.net.SelfSignedCertificate)7 Timeout (io.vertx.junit5.Timeout)7 VertxExtension (io.vertx.junit5.VertxExtension)7 VertxTestContext (io.vertx.junit5.VertxTestContext)7 MqttConnectionException (io.vertx.mqtt.MqttConnectionException)7 HttpURLConnection (java.net.HttpURLConnection)7 Collections (java.util.Collections)7 List (java.util.List)7 UUID (java.util.UUID)7 TimeUnit (java.util.concurrent.TimeUnit)7 Supplier (java.util.function.Supplier)7 X500Principal (javax.security.auth.x500.X500Principal)7 PasswordCredential (org.eclipse.hono.service.management.credentials.PasswordCredential)7 X509CertificateCredential (org.eclipse.hono.service.management.credentials.X509CertificateCredential)7 X509CertificateSecret (org.eclipse.hono.service.management.credentials.X509CertificateSecret)7