Search in sources :

Example 46 with Device

use of org.eclipse.hono.auth.Device in project hono by eclipse.

the class CredentialsApiAuthProviderTest method testValidateFailsIfCredentialsAreDisabled.

/**
 * Verifies that credentials validation fails if the credentials on record are disabled.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testValidateFailsIfCredentialsAreDisabled(final VertxTestContext ctx) {
    // WHEN trying to authenticate a disabled device
    final AbstractDeviceCredentials creds = getDeviceCredentials("type", "tenant", "identity");
    final CredentialsObject credentialsOnRecord = getCredentialsObject("type", "identity", "device", false).addSecret(CredentialsObject.emptySecret(Instant.now().minusSeconds(120), null));
    when(credentialsClient.get(eq("tenant"), eq("type"), eq("identity"), any(JsonObject.class), any())).thenReturn(Future.succeededFuture(credentialsOnRecord));
    provider.authenticate(creds, null, ctx.failing(t -> {
        // THEN authentication fails with a 401 client error
        ctx.verify(() -> assertThat(((ClientErrorException) t).getErrorCode()).isEqualTo(HttpURLConnection.HTTP_UNAUTHORIZED));
        ctx.completeNow();
    }));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) HttpURLConnection(java.net.HttpURLConnection) VertxTestContext(io.vertx.junit5.VertxTestContext) BeforeEach(org.junit.jupiter.api.BeforeEach) Tracer(io.opentracing.Tracer) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) NoopTracerFactory(io.opentracing.noop.NoopTracerFactory) ServerErrorException(org.eclipse.hono.client.ServerErrorException) Mockito.when(org.mockito.Mockito.when) ClientErrorException(org.eclipse.hono.client.ClientErrorException) Truth.assertThat(com.google.common.truth.Truth.assertThat) Instant(java.time.Instant) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) Device(org.eclipse.hono.auth.Device) Timeout(io.vertx.junit5.Timeout) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) CredentialsClient(org.eclipse.hono.client.registry.CredentialsClient) JsonObject(io.vertx.core.json.JsonObject) CredentialsObject(org.eclipse.hono.util.CredentialsObject) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) CredentialsObject(org.eclipse.hono.util.CredentialsObject) JsonObject(io.vertx.core.json.JsonObject) ClientErrorException(org.eclipse.hono.client.ClientErrorException) Test(org.junit.jupiter.api.Test)

Example 47 with Device

use of org.eclipse.hono.auth.Device in project hono by eclipse.

the class AbstractVertxBasedMqttProtocolAdapterTest method testDeviceConnectionIsClosedOnDeviceDeletedNotification.

/**
 * Verifies that the adapter closes the connection to an authenticated device when a notification
 * about the deletion of registration data of that device has been received.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testDeviceConnectionIsClosedOnDeviceDeletedNotification(final VertxTestContext ctx) {
    final Device device = new Device("tenant", "deviceId");
    testDeviceConnectionIsClosedOnDeviceOrTenantChangeNotification(ctx, device, new DeviceChangeNotification(LifecycleChange.DELETE, "tenant", "deviceId", Instant.now(), true));
}
Also used : Device(org.eclipse.hono.auth.Device) DeviceChangeNotification(org.eclipse.hono.notification.deviceregistry.DeviceChangeNotification) Test(org.junit.jupiter.api.Test)

Example 48 with Device

use of org.eclipse.hono.auth.Device in project hono by eclipse.

the class AbstractVertxBasedMqttProtocolAdapterTest method testDeviceConnectionIsClosedOnTenantDeletedNotification.

/**
 * Verifies that the adapter closes the connection to an authenticated device when a notification
 * about the deletion of the tenant of that device has been received.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testDeviceConnectionIsClosedOnTenantDeletedNotification(final VertxTestContext ctx) {
    final Device device = new Device("tenant", "deviceId");
    testDeviceConnectionIsClosedOnDeviceOrTenantChangeNotification(ctx, device, new TenantChangeNotification(LifecycleChange.DELETE, "tenant", Instant.now(), true));
}
Also used : Device(org.eclipse.hono.auth.Device) TenantChangeNotification(org.eclipse.hono.notification.deviceregistry.TenantChangeNotification) Test(org.junit.jupiter.api.Test)

Example 49 with Device

use of org.eclipse.hono.auth.Device in project hono by eclipse.

the class AbstractVertxBasedMqttProtocolAdapterTest method testUploadMessageSupportsShortAndLongEndpointNames.

/**
 * Verifies that the adapter will accept uploading messages to standard as well as shortened topic names.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testUploadMessageSupportsShortAndLongEndpointNames(final VertxTestContext ctx) {
    // GIVEN an adapter with downstream telemetry & event consumers
    givenAnAdapter(properties);
    givenATelemetrySenderForAnyTenant();
    givenAnEventSenderForAnyTenant();
    // WHEN a device publishes events and telemetry messages
    final MqttEndpoint endpoint = mockEndpoint();
    when(endpoint.isConnected()).thenReturn(Boolean.TRUE);
    final Buffer payload = Buffer.buffer("some payload");
    final MqttPublishMessage messageFromDevice = mock(MqttPublishMessage.class);
    when(messageFromDevice.qosLevel()).thenReturn(MqttQoS.AT_LEAST_ONCE);
    when(messageFromDevice.messageId()).thenReturn(5555555);
    when(messageFromDevice.payload()).thenReturn(payload);
    ResourceIdentifier resourceId = ResourceIdentifier.from("telemetry", "my-tenant", "4712");
    when(messageFromDevice.topicName()).thenReturn(resourceId.toString());
    adapter.uploadMessage(newMqttContext(messageFromDevice, endpoint, span), resourceId, messageFromDevice).onFailure(ctx::failNow);
    resourceId = ResourceIdentifier.from("event", "my-tenant", "4712");
    when(messageFromDevice.topicName()).thenReturn(resourceId.toString());
    adapter.uploadMessage(newMqttContext(messageFromDevice, endpoint, span), resourceId, messageFromDevice).onFailure(ctx::failNow);
    resourceId = ResourceIdentifier.from("t", "my-tenant", "4712");
    when(messageFromDevice.topicName()).thenReturn(resourceId.toString());
    adapter.uploadMessage(newMqttContext(messageFromDevice, endpoint, span), resourceId, messageFromDevice).onFailure(ctx::failNow);
    resourceId = ResourceIdentifier.from("e", "my-tenant", "4712");
    when(messageFromDevice.topicName()).thenReturn(resourceId.toString());
    adapter.uploadMessage(newMqttContext(messageFromDevice, endpoint, span), resourceId, messageFromDevice).onFailure(ctx::failNow);
    resourceId = ResourceIdentifier.from("unknown", "my-tenant", "4712");
    when(messageFromDevice.topicName()).thenReturn(resourceId.toString());
    adapter.uploadMessage(newMqttContext(messageFromDevice, endpoint, span), resourceId, messageFromDevice).onComplete(ctx.failing(t -> {
        ctx.verify(() -> assertThat(t).isInstanceOf(ClientErrorException.class));
    }));
    ctx.completeNow();
}
Also used : Buffer(io.vertx.core.buffer.Buffer) HttpURLConnection(java.net.HttpURLConnection) BeforeEach(org.junit.jupiter.api.BeforeEach) LifecycleChange(org.eclipse.hono.notification.deviceregistry.LifecycleChange) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) DeviceChangeNotification(org.eclipse.hono.notification.deviceregistry.DeviceChangeNotification) MqttEndpoint(io.vertx.mqtt.MqttEndpoint) MqttPublishMessage(io.vertx.mqtt.messages.MqttPublishMessage) Context(io.vertx.core.Context) Timeout(io.vertx.junit5.Timeout) AfterAll(org.junit.jupiter.api.AfterAll) EndpointType(org.eclipse.hono.service.metric.MetricsTags.EndpointType) MessagingType(org.eclipse.hono.util.MessagingType) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) BeforeAll(org.junit.jupiter.api.BeforeAll) Mockito.doAnswer(org.mockito.Mockito.doAnswer) TracingMockSupport(org.eclipse.hono.test.TracingMockSupport) ResourceIdentifier(org.eclipse.hono.util.ResourceIdentifier) AllDevicesOfTenantDeletedNotification(org.eclipse.hono.notification.deviceregistry.AllDevicesOfTenantDeletedNotification) AuthHandler(org.eclipse.hono.adapter.auth.device.AuthHandler) MetricsTags(org.eclipse.hono.service.metric.MetricsTags) RegistrationAssertion(org.eclipse.hono.util.RegistrationAssertion) Instant(java.time.Instant) MessageHelper(org.eclipse.hono.util.MessageHelper) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) Device(org.eclipse.hono.auth.Device) Test(org.junit.jupiter.api.Test) List(java.util.List) Buffer(io.vertx.core.buffer.Buffer) CommandConsumer(org.eclipse.hono.client.command.CommandConsumer) VertxMockSupport(org.eclipse.hono.test.VertxMockSupport) Span(io.opentracing.Span) NotificationEventBusSupport(org.eclipse.hono.notification.NotificationEventBusSupport) QoS(org.eclipse.hono.util.QoS) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) AbstractNotification(org.eclipse.hono.notification.AbstractNotification) VertxTestContext(io.vertx.junit5.VertxTestContext) MqttQoS(io.netty.handler.codec.mqtt.MqttQoS) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) MqttConnectReturnCode(io.netty.handler.codec.mqtt.MqttConnectReturnCode) ClientErrorException(org.eclipse.hono.client.ClientErrorException) OptionalInt(java.util.OptionalInt) CommandResponseSender(org.eclipse.hono.client.command.CommandResponseSender) Commands(org.eclipse.hono.client.command.Commands) Constants(org.eclipse.hono.util.Constants) ArrayList(java.util.ArrayList) DeviceUser(org.eclipse.hono.service.auth.DeviceUser) MqttServer(io.vertx.mqtt.MqttServer) ProtocolAdapterTestSupport(org.eclipse.hono.adapter.test.ProtocolAdapterTestSupport) SSLSession(javax.net.ssl.SSLSession) ArgumentCaptor(org.mockito.ArgumentCaptor) MqttTopicSubscription(io.vertx.mqtt.MqttTopicSubscription) BiConsumer(java.util.function.BiConsumer) HttpUtils(org.eclipse.hono.service.http.HttpUtils) AsyncResult(io.vertx.core.AsyncResult) CommandConstants(org.eclipse.hono.util.CommandConstants) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) TenantChangeNotification(org.eclipse.hono.notification.deviceregistry.TenantChangeNotification) Promise(io.vertx.core.Promise) MqttSubscribeMessage(io.vertx.mqtt.messages.MqttSubscribeMessage) Vertx(io.vertx.core.Vertx) ServerErrorException(org.eclipse.hono.client.ServerErrorException) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Truth.assertThat(com.google.common.truth.Truth.assertThat) Mockito.verify(org.mockito.Mockito.verify) CommandResponse(org.eclipse.hono.client.command.CommandResponse) TenantObject(org.eclipse.hono.util.TenantObject) SpanContext(io.opentracing.SpanContext) TimeUnit(java.util.concurrent.TimeUnit) Mockito.never(org.mockito.Mockito.never) Adapter(org.eclipse.hono.util.Adapter) ConnectionAttemptOutcome(org.eclipse.hono.service.metric.MetricsTags.ConnectionAttemptOutcome) MqttAuth(io.vertx.mqtt.MqttAuth) ResourceLimitChecks(org.eclipse.hono.adapter.resourcelimits.ResourceLimitChecks) Handler(io.vertx.core.Handler) Collections(java.util.Collections) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ResourceIdentifier(org.eclipse.hono.util.ResourceIdentifier) MqttEndpoint(io.vertx.mqtt.MqttEndpoint) MqttPublishMessage(io.vertx.mqtt.messages.MqttPublishMessage) Test(org.junit.jupiter.api.Test)

Example 50 with Device

use of org.eclipse.hono.auth.Device in project hono by eclipse.

the class AbstractVertxBasedMqttProtocolAdapterTest method testDeviceConnectionIsClosedOnDeviceDisabledNotification.

/**
 * Verifies that the adapter closes the connection to an authenticated device when a notification
 * has been received that the device has been disabled.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testDeviceConnectionIsClosedOnDeviceDisabledNotification(final VertxTestContext ctx) {
    final Device device = new Device("tenant", "deviceId");
    testDeviceConnectionIsClosedOnDeviceOrTenantChangeNotification(ctx, device, new DeviceChangeNotification(LifecycleChange.UPDATE, "tenant", "deviceId", Instant.now(), false));
}
Also used : Device(org.eclipse.hono.auth.Device) DeviceChangeNotification(org.eclipse.hono.notification.deviceregistry.DeviceChangeNotification) Test(org.junit.jupiter.api.Test)

Aggregations

Device (org.eclipse.hono.auth.Device)115 HttpURLConnection (java.net.HttpURLConnection)74 Test (org.junit.jupiter.api.Test)72 Future (io.vertx.core.Future)69 ClientErrorException (org.eclipse.hono.client.ClientErrorException)67 Buffer (io.vertx.core.buffer.Buffer)66 Handler (io.vertx.core.Handler)63 TenantObject (org.eclipse.hono.util.TenantObject)63 Promise (io.vertx.core.Promise)59 Constants (org.eclipse.hono.util.Constants)58 Span (io.opentracing.Span)55 RegistrationAssertion (org.eclipse.hono.util.RegistrationAssertion)55 SpanContext (io.opentracing.SpanContext)53 VertxTestContext (io.vertx.junit5.VertxTestContext)52 VertxExtension (io.vertx.junit5.VertxExtension)51 MessageHelper (org.eclipse.hono.util.MessageHelper)51 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)51 Mockito.when (org.mockito.Mockito.when)51 Truth.assertThat (com.google.common.truth.Truth.assertThat)50 ResourceIdentifier (org.eclipse.hono.util.ResourceIdentifier)47