Search in sources :

Example 66 with Timeout

use of io.vertx.junit5.Timeout in project hono by eclipse.

the class CredentialsApiTests method testGetCredentialsWithDeviceAutoProvisioningUsingDeviceIdTemplate.

/**
 * Verifies when no credentials are found and if the properties related to auto-provisioning of devices are
 * enabled, the device id template is configured in the corresponding tenant's CA entry and the client context
 * contains a serialized X.509 certificate then a device is auto-provisioned.
 * (i.e A device is registered and it's corresponding credentials are stored).
 * <p>
 * Also verify that the device is auto-provisioned with a device id generated in accordance
 * with the configured device id template.
 * <p>
 * It also verifies that an auto-provisioning event is successfully sent and the device registration
 * is updated accordingly.
 *
 * @param ctx The vert.x test context.
 * @throws CertificateException if the self signed certificate cannot be created.
 * @throws FileNotFoundException if the self signed certificate cannot be read.
 */
@Timeout(value = 5, timeUnit = TimeUnit.SECONDS)
@Test
public void testGetCredentialsWithDeviceAutoProvisioningUsingDeviceIdTemplate(final VertxTestContext ctx) throws CertificateException, FileNotFoundException {
    // GIVEN a tenant's trusted CA entry with auto-provisioning enabled
    // while device has not been registered and no credentials are stored yet
    final X509Certificate cert = createCertificate();
    final var tenant = Tenants.createTenantForTrustAnchor(cert);
    tenant.getTrustedCertificateAuthorities().get(0).setAutoProvisioningEnabled(true).setAutoProvisioningDeviceIdTemplate("test-device-{{subject-dn}}");
    final String expectedDeviceId = "test-device-" + cert.getSubjectX500Principal().getName(X500Principal.RFC2253);
    testAutoProvisioningSucceeds(ctx, tenant, cert, false, expectedDeviceId);
}
Also used : X509Certificate(java.security.cert.X509Certificate) Test(org.junit.jupiter.api.Test) Timeout(io.vertx.junit5.Timeout)

Example 67 with Timeout

use of io.vertx.junit5.Timeout in project hono by eclipse.

the class CredentialsApiTests method testGetCredentialsWithAutoProvisioning.

/**
 * Verifies that if no credentials are found and the client context in the Get request contains a serialized X.509
 * certificate, the credentials and device are created (i.e., automatic provisioning is performed).
 * <p>
 * It also verifies that an auto-provisioning event is successfully sent and the device registration
 * is updated accordingly.
 *
 * @param ctx The vert.x test context.
 * @throws CertificateException if the self signed certificate cannot be created.
 * @throws FileNotFoundException if the self signed certificate cannot be read.
 */
@Timeout(value = 5, timeUnit = TimeUnit.SECONDS)
@Test
public void testGetCredentialsWithAutoProvisioning(final VertxTestContext ctx) throws CertificateException, FileNotFoundException {
    // GIVEN a tenant with auto-provisioning enabled and a client context that contains a client certificate
    // while device has not been registered and no credentials are stored yet
    final X509Certificate cert = createCertificate();
    final var tenant = Tenants.createTenantForTrustAnchor(cert);
    tenant.getTrustedCertificateAuthorities().get(0).setAutoProvisioningEnabled(true);
    testAutoProvisioningSucceeds(ctx, tenant, cert, false, null);
}
Also used : X509Certificate(java.security.cert.X509Certificate) Test(org.junit.jupiter.api.Test) Timeout(io.vertx.junit5.Timeout)

Example 68 with Timeout

use of io.vertx.junit5.Timeout in project hono by eclipse.

the class CredentialsApiTests method testGetCredentialsWithGatewayAutoProvisioningUsingDeviceIdTemplate.

/**
 * Verifies when no credentials are found and if the properties related to auto-provisioning of gateways are
 * enabled, device id template is configured in the corresponding tenant's CA entry and the client context
 * contains a serialized X.509 certificate then a gateway is auto-provisioned.
 * (i.e A gateway is registered and it's corresponding credentials are stored).
 * <p>
 * Also verify that the gateway is auto-provisioned with a device id generated in accordance
 * with the configured device id template.
 * <p>
 * It also verifies that an auto-provisioning event is sent successfully sent and the device registration's
 * property {@value RegistryManagementConstants#FIELD_AUTO_PROVISIONING_NOTIFICATION_SENT} is updated to
 * {@code true}.
 *
 * @param ctx The vert.x test context.
 * @throws CertificateException if the self signed certificate cannot be created.
 * @throws FileNotFoundException if the self signed certificate cannot be read.
 */
@Timeout(value = 5, timeUnit = TimeUnit.SECONDS)
@Test
public void testGetCredentialsWithGatewayAutoProvisioningUsingDeviceIdTemplate(final VertxTestContext ctx) throws CertificateException, FileNotFoundException {
    // GIVEN a tenant's trusted CA entry with auto-provisioning and auto-provision as gateway enabled
    // while gateway has not been registered and no credentials are stored yet
    final X509Certificate cert = createCertificate();
    final var tenant = Tenants.createTenantForTrustAnchor(cert);
    tenant.getTrustedCertificateAuthorities().get(0).setAutoProvisioningEnabled(true).setAutoProvisioningAsGatewayEnabled(true).setAutoProvisioningDeviceIdTemplate("test-device-{{subject-cn}}");
    final String expectedDeviceId = "test-device-" + AuthenticationConstants.getCommonName(cert.getSubjectX500Principal().getName(X500Principal.RFC2253));
    testAutoProvisioningSucceeds(ctx, tenant, cert, true, expectedDeviceId);
}
Also used : X509Certificate(java.security.cert.X509Certificate) Test(org.junit.jupiter.api.Test) Timeout(io.vertx.junit5.Timeout)

Example 69 with Timeout

use of io.vertx.junit5.Timeout in project hono by eclipse.

the class CredentialsApiTests method testGetCredentialsFailsForDisabledCredentials.

/**
 * Verifies that the service fails when the credentials set is disabled.
 *
 * @param ctx The vert.x test context.
 */
@Timeout(value = 5, timeUnit = TimeUnit.SECONDS)
@Test
public void testGetCredentialsFailsForDisabledCredentials(final VertxTestContext ctx) {
    final String deviceId = getHelper().getRandomDeviceId(tenantId);
    final String authId = UUID.randomUUID().toString();
    final CommonCredential credential = getRandomHashedPasswordCredential(authId);
    credential.setEnabled(false);
    getHelper().registry.registerDevice(tenantId, deviceId).compose(ok -> getHelper().registry.addCredentials(tenantId, deviceId, Collections.singleton(credential))).compose(ok -> getClient().get(tenantId, CredentialsConstants.SECRETS_TYPE_HASHED_PASSWORD, authId, spanContext)).onComplete(ctx.failing(t -> {
        ctx.verify(() -> assertErrorCode(t, HttpURLConnection.HTTP_NOT_FOUND));
        ctx.completeNow();
    }));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) X509Certificate(java.security.cert.X509Certificate) VertxTestContext(io.vertx.junit5.VertxTestContext) BeforeEach(org.junit.jupiter.api.BeforeEach) X500Principal(javax.security.auth.x500.X500Principal) DownstreamMessage(org.eclipse.hono.application.client.DownstreamMessage) CertificateFactory(java.security.cert.CertificateFactory) LoggerFactory(org.slf4j.LoggerFactory) Credentials(org.eclipse.hono.service.management.credentials.Credentials) SelfSignedCertificate(io.vertx.core.net.SelfSignedCertificate) OptionalInt(java.util.OptionalInt) Tenant(org.eclipse.hono.service.management.tenant.Tenant) Timeout(io.vertx.junit5.Timeout) IntegrationTestSupport(org.eclipse.hono.tests.IntegrationTestSupport) CredentialsClient(org.eclipse.hono.client.registry.CredentialsClient) JsonObject(io.vertx.core.json.JsonObject) Tenants(org.eclipse.hono.tests.Tenants) RegistryManagementConstants(org.eclipse.hono.util.RegistryManagementConstants) Device(org.eclipse.hono.service.management.device.Device) Logger(org.slf4j.Logger) Truth.assertWithMessage(com.google.common.truth.Truth.assertWithMessage) Vertx(io.vertx.core.Vertx) FileInputStream(java.io.FileInputStream) CertificateException(java.security.cert.CertificateException) UUID(java.util.UUID) Truth.assertThat(com.google.common.truth.Truth.assertThat) Instant(java.time.Instant) DeviceStatus(org.eclipse.hono.service.management.device.DeviceStatus) MessageHelper(org.eclipse.hono.util.MessageHelper) EventConstants(org.eclipse.hono.util.EventConstants) FileNotFoundException(java.io.FileNotFoundException) CredentialsConstants(org.eclipse.hono.util.CredentialsConstants) AuthenticationConstants(org.eclipse.hono.util.AuthenticationConstants) SpanContext(io.opentracing.SpanContext) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) List(java.util.List) CommonCredential(org.eclipse.hono.service.management.credentials.CommonCredential) Checkpoint(io.vertx.junit5.Checkpoint) NoopSpan(io.opentracing.noop.NoopSpan) Collections(java.util.Collections) CredentialsObject(org.eclipse.hono.util.CredentialsObject) PasswordCredential(org.eclipse.hono.service.management.credentials.PasswordCredential) CertificateEncodingException(java.security.cert.CertificateEncodingException) CommonCredential(org.eclipse.hono.service.management.credentials.CommonCredential) Test(org.junit.jupiter.api.Test) Timeout(io.vertx.junit5.Timeout)

Example 70 with Timeout

use of io.vertx.junit5.Timeout in project hono by eclipse.

the class CredentialsApiTests method testGetCredentialsByClientContext.

/**
 * Verifies that the service returns credentials for a given type, authentication ID and matching client context.
 *
 * @param ctx The vert.x test context.
 */
@Timeout(value = 5, timeUnit = TimeUnit.SECONDS)
@Test
public void testGetCredentialsByClientContext(final VertxTestContext ctx) {
    final String deviceId = getHelper().getRandomDeviceId(tenantId);
    final String authId = UUID.randomUUID().toString();
    final CommonCredential credentials = getRandomHashedPasswordCredential(authId).putExtension("client-id", "gateway-one");
    final JsonObject clientContext = new JsonObject().put("client-id", "gateway-one");
    getHelper().registry.registerDevice(tenantId, deviceId).compose(httpResponse -> getHelper().registry.addCredentials(tenantId, deviceId, List.of(credentials))).compose(ok -> getClient().get(tenantId, CredentialsConstants.SECRETS_TYPE_HASHED_PASSWORD, authId, clientContext, spanContext)).onComplete(ctx.succeeding(result -> {
        ctx.verify(() -> {
            assertStandardProperties(result, deviceId, authId, CredentialsConstants.SECRETS_TYPE_HASHED_PASSWORD, 2);
        });
        ctx.completeNow();
    }));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) X509Certificate(java.security.cert.X509Certificate) VertxTestContext(io.vertx.junit5.VertxTestContext) BeforeEach(org.junit.jupiter.api.BeforeEach) X500Principal(javax.security.auth.x500.X500Principal) DownstreamMessage(org.eclipse.hono.application.client.DownstreamMessage) CertificateFactory(java.security.cert.CertificateFactory) LoggerFactory(org.slf4j.LoggerFactory) Credentials(org.eclipse.hono.service.management.credentials.Credentials) SelfSignedCertificate(io.vertx.core.net.SelfSignedCertificate) OptionalInt(java.util.OptionalInt) Tenant(org.eclipse.hono.service.management.tenant.Tenant) Timeout(io.vertx.junit5.Timeout) IntegrationTestSupport(org.eclipse.hono.tests.IntegrationTestSupport) CredentialsClient(org.eclipse.hono.client.registry.CredentialsClient) JsonObject(io.vertx.core.json.JsonObject) Tenants(org.eclipse.hono.tests.Tenants) RegistryManagementConstants(org.eclipse.hono.util.RegistryManagementConstants) Device(org.eclipse.hono.service.management.device.Device) Logger(org.slf4j.Logger) Truth.assertWithMessage(com.google.common.truth.Truth.assertWithMessage) Vertx(io.vertx.core.Vertx) FileInputStream(java.io.FileInputStream) CertificateException(java.security.cert.CertificateException) UUID(java.util.UUID) Truth.assertThat(com.google.common.truth.Truth.assertThat) Instant(java.time.Instant) DeviceStatus(org.eclipse.hono.service.management.device.DeviceStatus) MessageHelper(org.eclipse.hono.util.MessageHelper) EventConstants(org.eclipse.hono.util.EventConstants) FileNotFoundException(java.io.FileNotFoundException) CredentialsConstants(org.eclipse.hono.util.CredentialsConstants) AuthenticationConstants(org.eclipse.hono.util.AuthenticationConstants) SpanContext(io.opentracing.SpanContext) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) List(java.util.List) CommonCredential(org.eclipse.hono.service.management.credentials.CommonCredential) Checkpoint(io.vertx.junit5.Checkpoint) NoopSpan(io.opentracing.noop.NoopSpan) Collections(java.util.Collections) CredentialsObject(org.eclipse.hono.util.CredentialsObject) PasswordCredential(org.eclipse.hono.service.management.credentials.PasswordCredential) CertificateEncodingException(java.security.cert.CertificateEncodingException) CommonCredential(org.eclipse.hono.service.management.credentials.CommonCredential) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.jupiter.api.Test) Timeout(io.vertx.junit5.Timeout)

Aggregations

Timeout (io.vertx.junit5.Timeout)73 VertxTestContext (io.vertx.junit5.VertxTestContext)73 Test (org.junit.jupiter.api.Test)71 TimeUnit (java.util.concurrent.TimeUnit)67 Truth.assertThat (com.google.common.truth.Truth.assertThat)65 Future (io.vertx.core.Future)54 HttpURLConnection (java.net.HttpURLConnection)52 IntegrationTestSupport (org.eclipse.hono.tests.IntegrationTestSupport)49 Handler (io.vertx.core.Handler)47 Buffer (io.vertx.core.buffer.Buffer)46 BeforeEach (org.junit.jupiter.api.BeforeEach)46 Promise (io.vertx.core.Promise)45 Vertx (io.vertx.core.Vertx)37 JsonObject (io.vertx.core.json.JsonObject)37 VertxExtension (io.vertx.junit5.VertxExtension)37 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)37 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)37 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)37 Tenant (org.eclipse.hono.service.management.tenant.Tenant)36 MethodSource (org.junit.jupiter.params.provider.MethodSource)35