use of org.eclipse.hono.util.Adapter in project hono by eclipse.
the class MqttConnectionIT method testConnectFailsForDisabledTenant.
/**
* Verifies that the adapter rejects connection attempts from devices that belong to a disabled tenant.
*
* @param ctx The test context
*/
@Test
public void testConnectFailsForDisabledTenant(final VertxTestContext ctx) {
// Given a disabled tenant for which the MQTT adapter is enabled
final Tenant tenant = new Tenant();
tenant.setEnabled(false);
helper.registry.addDeviceForTenant(tenantId, tenant, deviceId, password).compose(ok -> connectToAdapter(IntegrationTestSupport.getUsername(deviceId, tenantId), password)).onComplete(ctx.failing(t -> {
// THEN the connection is refused with a NOT_AUTHORIZED code
ctx.verify(() -> {
assertThat(t).isInstanceOf(MqttConnectionException.class);
assertThat(((MqttConnectionException) t).code()).isEqualTo(MqttConnectReturnCode.CONNECTION_REFUSED_NOT_AUTHORIZED);
});
ctx.completeNow();
}));
}
use of org.eclipse.hono.util.Adapter 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();
}));
}
use of org.eclipse.hono.util.Adapter 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();
}));
}
use of org.eclipse.hono.util.Adapter in project hono by eclipse.
the class AbstractVertxBasedMqttProtocolAdapterTest method testMessageLimitExceededForATelemetryMessage.
/**
* Verifies that a telemetry message is rejected due to the limit exceeded.
*
* @param ctx The vert.x test context.
*/
@Test
public void testMessageLimitExceededForATelemetryMessage(final VertxTestContext ctx) {
// GIVEN an adapter
givenAnAdapter(properties);
givenATelemetrySenderForAnyTenant();
when(resourceLimitChecks.isMessageLimitReached(any(TenantObject.class), anyLong(), any(SpanContext.class))).thenReturn(Future.succeededFuture(Boolean.TRUE));
// WHEN a device of "my-tenant" publishes a telemetry message
final MqttEndpoint client = mockEndpoint();
final MqttPublishMessage msg = mock(MqttPublishMessage.class);
when(msg.topicName()).thenReturn("t/my-tenant/the-device");
when(msg.qosLevel()).thenReturn(MqttQoS.AT_LEAST_ONCE);
adapter.uploadTelemetryMessage(newMqttContext(msg, client, span), "my-tenant", "the-device", Buffer.buffer("test")).onComplete(ctx.failing(t -> {
ctx.verify(() -> {
// THEN the message has not been sent downstream
assertNoTelemetryMessageHasBeenSentDownstream();
// because the message limit is exceeded
assertThat(((ClientErrorException) t).getErrorCode()).isEqualTo(HttpUtils.HTTP_TOO_MANY_REQUESTS);
// and the published message has not been acknowledged
verify(client, never()).publishAcknowledge(anyInt());
// and the message has been reported as unprocessable
verify(metrics).reportTelemetry(any(MetricsTags.EndpointType.class), anyString(), any(), eq(MetricsTags.ProcessingOutcome.UNPROCESSABLE), any(MetricsTags.QoS.class), anyInt(), any());
});
ctx.completeNow();
}));
}
use of org.eclipse.hono.util.Adapter in project hono by eclipse.
the class AbstractVertxBasedMqttProtocolAdapterTest method testMessageLimitExceededForAnEventMessage.
/**
* Verifies that an event message is rejected due to the limit exceeded.
*
* @param ctx The vert.x test context.
*/
@Test
public void testMessageLimitExceededForAnEventMessage(final VertxTestContext ctx) {
// GIVEN an adapter
givenAnAdapter(properties);
givenAnEventSenderForAnyTenant();
when(resourceLimitChecks.isMessageLimitReached(any(TenantObject.class), anyLong(), any(SpanContext.class))).thenReturn(Future.succeededFuture(Boolean.TRUE));
// WHEN a device of "my-tenant" publishes an event message
final MqttEndpoint client = mockEndpoint();
final MqttPublishMessage msg = mock(MqttPublishMessage.class);
when(msg.topicName()).thenReturn("e/my-tenant/the-device");
when(msg.qosLevel()).thenReturn(MqttQoS.AT_LEAST_ONCE);
adapter.uploadEventMessage(newMqttContext(msg, client, span), "my-tenant", "the-device", Buffer.buffer("test")).onComplete(ctx.failing(t -> {
ctx.verify(() -> {
// THEN the message has not been sent downstream
assertNoEventHasBeenSentDownstream();
// because the message limit is exceeded
assertThat(((ClientErrorException) t).getErrorCode()).isEqualTo(HttpUtils.HTTP_TOO_MANY_REQUESTS);
// and the event has not been acknowledged
verify(client, never()).publishAcknowledge(anyInt());
// and the message has been reported as unprocessable
verify(metrics).reportTelemetry(any(MetricsTags.EndpointType.class), anyString(), any(), eq(MetricsTags.ProcessingOutcome.UNPROCESSABLE), any(MetricsTags.QoS.class), anyInt(), any());
});
ctx.completeNow();
}));
}
Aggregations