use of io.vertx.core.Promise in project hono by eclipse.
the class CoapTestBase method testUploadMessageFailsForDisabledGateway.
/**
* Verifies that the CoAP adapter rejects messages from a disabled gateway
* for an enabled device with a 403.
*
* @param ctx The test context
*/
@Test
@Timeout(value = 10, timeUnit = TimeUnit.SECONDS)
public void testUploadMessageFailsForDisabledGateway(final VertxTestContext ctx) {
// GIVEN a device that is connected via a disabled gateway
final Tenant tenant = new Tenant();
final String gatewayId = helper.getRandomDeviceId(tenantId);
final Device gatewayData = new Device();
gatewayData.setEnabled(false);
final Device deviceData = new Device();
deviceData.setVia(Collections.singletonList(gatewayId));
helper.registry.addPskDeviceForTenant(tenantId, tenant, gatewayId, gatewayData, SECRET).compose(ok -> helper.registry.registerDevice(tenantId, deviceId, deviceData)).compose(ok -> {
// WHEN the gateway tries to upload a message for the device
final Promise<OptionSet> result = Promise.promise();
final CoapClient client = getCoapsClient(gatewayId, tenantId, SECRET);
// THEN a FORBIDDEN response code is returned
client.advanced(getHandler(result, ResponseCode.FORBIDDEN), createCoapsRequest(Code.PUT, getPutResource(tenantId, deviceId), 0));
return result.future();
}).onComplete(ctx.succeedingThenComplete());
}
use of io.vertx.core.Promise 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 io.vertx.core.Promise in project hono by eclipse.
the class CoapTestBase method testAutoProvisioningViaGateway.
/**
* Verifies that an edge device is auto-provisioned if it connects via a gateway equipped with the corresponding
* authority.
*
* @param ctx The test context.
* @throws InterruptedException if the test fails.
*/
@Test
public void testAutoProvisioningViaGateway(final VertxTestContext ctx) throws InterruptedException {
final Tenant tenant = new Tenant();
final String gatewayId = helper.getRandomDeviceId(tenantId);
final Device gateway = new Device().setAuthorities(Collections.singleton(RegistryManagementConstants.AUTHORITY_AUTO_PROVISIONING_ENABLED));
final String edgeDeviceId = helper.getRandomDeviceId(tenantId);
final Promise<Void> provisioningNotificationReceived = Promise.promise();
helper.createAutoProvisioningMessageConsumers(ctx, provisioningNotificationReceived, tenantId, edgeDeviceId).compose(ok -> helper.registry.addPskDeviceForTenant(tenantId, tenant, gatewayId, gateway, SECRET)).compose(ok -> {
final Promise<OptionSet> result = Promise.promise();
final Request request = createCoapsRequest(Code.PUT, getPutResource(tenantId, edgeDeviceId), 0);
final CoapClient client = getCoapsClient(gatewayId, tenantId, SECRET);
client.advanced(getHandler(result), request);
return result.future();
}).compose(ok -> provisioningNotificationReceived.future()).compose(ok -> helper.registry.getRegistrationInfo(tenantId, edgeDeviceId)).onComplete(ctx.succeeding(registrationResult -> {
ctx.verify(() -> {
final var info = registrationResult.bodyAsJsonObject();
IntegrationTestSupport.assertDeviceStatusProperties(info.getJsonObject(RegistryManagementConstants.FIELD_STATUS), true);
});
ctx.completeNow();
}));
}
use of io.vertx.core.Promise 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 io.vertx.core.Promise 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();
}));
}
Aggregations