use of org.eclipse.hono.util.Adapter in project hono by eclipse.
the class CoapTestBase method testUploadMessageFailsForDisabledTenant.
/**
* Verifies that the CoAP adapter rejects messages from a device that belongs to a tenant for which the CoAP adapter
* has been disabled.
*
* @param ctx The test context
*/
@Test
@Timeout(value = 10, timeUnit = TimeUnit.SECONDS)
public void testUploadMessageFailsForDisabledTenant(final VertxTestContext ctx) {
// GIVEN a tenant for which the CoAP adapter is disabled
final Tenant tenant = new Tenant();
tenant.addAdapterConfig(new Adapter(Constants.PROTOCOL_ADAPTER_TYPE_COAP).setEnabled(false));
helper.registry.addPskDeviceForTenant(tenantId, tenant, deviceId, SECRET).compose(ok -> {
// WHEN a device that belongs to the tenant uploads a message
final CoapClient client = getCoapsClient(deviceId, tenantId, SECRET);
final Promise<OptionSet> result = Promise.promise();
// THEN a FORBIDDEN response code is returned
client.advanced(getHandler(result, ResponseCode.FORBIDDEN), createCoapsRequest(Code.POST, getPostResource(), 0));
return result.future();
}).onComplete(ctx.succeedingThenComplete());
}
use of org.eclipse.hono.util.Adapter in project hono by eclipse.
the class CoapTestBase method testUploadMessagesViaGateway.
/**
* Verifies that a number of messages uploaded to the CoAP adapter via a gateway
* using TLS_PSK can be successfully consumed via the AMQP Messaging Network.
*
* @param ctx The test context.
* @throws InterruptedException if the test fails.
*/
@Test
public void testUploadMessagesViaGateway(final VertxTestContext ctx) throws InterruptedException {
// GIVEN a device that is connected via two gateways
final Tenant tenant = new Tenant();
final String gatewayOneId = helper.getRandomDeviceId(tenantId);
final String gatewayTwoId = helper.getRandomDeviceId(tenantId);
final Device deviceData = new Device();
deviceData.setVia(Arrays.asList(gatewayOneId, gatewayTwoId));
final VertxTestContext setup = new VertxTestContext();
helper.registry.addPskDeviceForTenant(tenantId, tenant, gatewayOneId, SECRET).compose(ok -> helper.registry.addPskDeviceToTenant(tenantId, gatewayTwoId, SECRET)).compose(ok -> helper.registry.registerDevice(tenantId, deviceId, deviceData)).onComplete(setup.succeedingThenComplete());
ctx.verify(() -> assertThat(setup.awaitCompletion(5, TimeUnit.SECONDS)).isTrue());
final CoapClient gatewayOne = getCoapsClient(gatewayOneId, tenantId, SECRET);
final CoapClient gatewayTwo = getCoapsClient(gatewayTwoId, tenantId, SECRET);
testUploadMessages(ctx, tenantId, () -> warmUp(gatewayOne, createCoapsRequest(Code.PUT, getPutResource(tenantId, deviceId), 0)), count -> {
final CoapClient client = (count.intValue() & 1) == 0 ? gatewayOne : gatewayTwo;
final Promise<OptionSet> result = Promise.promise();
final Request request = createCoapsRequest(Code.PUT, getPutResource(tenantId, deviceId), count);
client.advanced(getHandler(result), request);
return result.future();
});
}
use of org.eclipse.hono.util.Adapter in project hono by eclipse.
the class CoapTestBase method testUploadFailsForNonMatchingSharedKey.
/**
* Verifies that the adapter fails to authenticate a device if the shared key registered
* for the device does not match the key used by the device in the DTLS handshake.
*
* @param ctx The vert.x test context.
*/
@Test
@Timeout(value = 10, timeUnit = TimeUnit.SECONDS)
public void testUploadFailsForNonMatchingSharedKey(final VertxTestContext ctx) {
final Tenant tenant = new Tenant();
// GIVEN a device for which PSK credentials have been registered
helper.registry.addPskDeviceForTenant(tenantId, tenant, deviceId, "NOT" + SECRET).compose(ok -> {
// WHEN a device tries to upload data and authenticate using the PSK
// identity for which the server has a different shared secret on record
final CoapClient client = getCoapsClient(deviceId, tenantId, SECRET);
final Promise<OptionSet> result = Promise.promise();
client.advanced(getHandler(result), createCoapsRequest(Code.POST, getPostResource(), 0));
return result.future();
}).onComplete(ctx.failing(t -> {
// THEN the request fails because the DTLS handshake cannot be completed
assertStatus(ctx, HttpURLConnection.HTTP_UNAVAILABLE, t);
ctx.completeNow();
}));
}
use of org.eclipse.hono.util.Adapter in project hono by eclipse.
the class AmqpConnectionIT method testConnectFailsForDeletedDevices.
/**
* Verifies that the adapter rejects connection attempts from devices for which
* credentials exist but for which no registration assertion can be retrieved.
*
* @param ctx The test context
*/
@Test
public void testConnectFailsForDeletedDevices(final VertxTestContext ctx) {
final String tenantId = helper.getRandomTenantId();
final String deviceId = helper.getRandomDeviceId(tenantId);
final String password = "secret";
final Tenant tenant = new Tenant();
helper.registry.addDeviceForTenant(tenantId, tenant, deviceId, password).compose(device -> helper.registry.deregisterDevice(tenantId, deviceId)).compose(ok -> connectToAdapter(IntegrationTestSupport.getUsername(deviceId, tenantId), password)).onComplete(ctx.failing(t -> {
ctx.verify(() -> assertThat(t).isInstanceOf(AuthenticationException.class));
ctx.completeNow();
}));
}
use of org.eclipse.hono.util.Adapter in project hono by eclipse.
the class AmqpConnectionIT method testConnectSucceedsForRegisteredDevice.
/**
* Verifies that the adapter opens a connection to registered devices with credentials.
*
* @param tlsVersion The TLS protocol version to use for connecting to the adapter.
* @param ctx The test context
*/
@ParameterizedTest(name = IntegrationTestSupport.PARAMETERIZED_TEST_NAME_PATTERN)
@ValueSource(strings = { IntegrationTestSupport.TLS_VERSION_1_2, IntegrationTestSupport.TLS_VERSION_1_3 })
public void testConnectSucceedsForRegisteredDevice(final String tlsVersion, final VertxTestContext ctx) {
final String tenantId = helper.getRandomTenantId();
final String deviceId = helper.getRandomDeviceId(tenantId);
final String password = "secret";
final Tenant tenant = new Tenant();
helper.registry.addDeviceForTenant(tenantId, tenant, deviceId, password).compose(ok -> connectToAdapter(tlsVersion, null, IntegrationTestSupport.getUsername(deviceId, tenantId), password)).onComplete(ctx.succeeding(con -> {
ctx.verify(() -> assertThat(con.isDisconnected()).isFalse());
ctx.completeNow();
}));
}
Aggregations