use of org.eclipse.hono.util.Adapter in project hono by eclipse.
the class CoapTestBase method testUploadMessageFailsForDisabledDevice.
/**
* Verifies that the CoAP adapter rejects messages from a disabled device.
*
* @param ctx The test context
*/
@Test
@Timeout(value = 10, timeUnit = TimeUnit.SECONDS)
public void testUploadMessageFailsForDisabledDevice(final VertxTestContext ctx) {
// GIVEN a disabled device
final Tenant tenant = new Tenant();
final Device deviceData = new Device();
deviceData.setEnabled(false);
helper.registry.addPskDeviceForTenant(tenantId, tenant, deviceId, deviceData, SECRET).compose(ok -> {
// WHEN the device tries to upload a message
final CoapClient client = getCoapsClient(deviceId, tenantId, SECRET);
final Promise<OptionSet> result = Promise.promise();
// THEN a NOT_FOUND response code is returned
client.advanced(getHandler(result, ResponseCode.NOT_FOUND), 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 testUploadFailsForNonMatchingTrustAnchor.
/**
* Verifies that the adapter fails to authenticate a device if the device's client certificate's signature cannot be
* validated using the trust anchor that is registered for the tenant that the device belongs to.
*
* @param ctx The vert.x test context.
* @throws GeneralSecurityException if the tenant's trust anchor cannot be generated
*/
@Test
@Timeout(timeUnit = TimeUnit.SECONDS, value = 20)
public void testUploadFailsForNonMatchingTrustAnchor(final VertxTestContext ctx) throws GeneralSecurityException {
final var keyLoader = KeyLoader.fromFiles(vertx, PATH_DEVICE_KEY, PATH_DEVICE_CERT);
// GIVEN a tenant configured with a trust anchor
final KeyPair keyPair = helper.newEcKeyPair();
final var clientCert = (X509Certificate) keyLoader.getCertificateChain()[0];
final Tenant tenant = Tenants.createTenantForTrustAnchor(clientCert.getIssuerX500Principal().getName(X500Principal.RFC2253), keyPair.getPublic().getEncoded(), keyPair.getPublic().getAlgorithm());
helper.registry.addDeviceForTenant(tenantId, tenant, deviceId, clientCert).compose(ok -> {
final CoapClient client = getCoapsClient(keyLoader);
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 HttpTestBase method testUploadFailsForNonMatchingTrustAnchor.
/**
* Verifies that the adapter fails to authenticate a device if the device's client certificate's signature cannot be
* validated using the trust anchor that is registered for the tenant that the device belongs to.
*
* @param ctx The vert.x test context.
* @throws GeneralSecurityException if the tenant's trust anchor cannot be generated
*/
@Test
@Timeout(timeUnit = TimeUnit.SECONDS, value = 20)
public void testUploadFailsForNonMatchingTrustAnchor(final VertxTestContext ctx) throws GeneralSecurityException {
final KeyPair keyPair = helper.newEcKeyPair();
// GIVEN a tenant configured with a trust anchor
helper.getCertificate(deviceCert.certificatePath()).compose(cert -> {
final Tenant tenant = Tenants.createTenantForTrustAnchor(cert.getIssuerX500Principal().getName(X500Principal.RFC2253), keyPair.getPublic().getEncoded(), keyPair.getPublic().getAlgorithm());
return helper.registry.addDeviceForTenant(tenantId, tenant, deviceId, cert);
}).compose(ok -> {
final MultiMap requestHeaders = MultiMap.caseInsensitiveMultiMap().add(HttpHeaders.CONTENT_TYPE, "text/plain").add(HttpHeaders.ORIGIN, ORIGIN_URI);
return httpClientWithClientCert.create(getEndpointUri(), Buffer.buffer("hello"), requestHeaders, ResponsePredicate.status(HttpURLConnection.HTTP_UNAUTHORIZED));
}).onComplete(ctx.succeedingThenComplete());
}
use of org.eclipse.hono.util.Adapter in project hono by eclipse.
the class HttpTestBase method testUploadMessagesWithTtdThatDoNotReplyWithCommand.
/**
* Verifies that the HTTP adapter returns empty responses when sending consecutive requests
* for uploading telemetry data or events with a TTD but no command is pending for the device.
*
* @param ctx The test context.
* @throws InterruptedException if the test fails.
*/
@Test
public void testUploadMessagesWithTtdThatDoNotReplyWithCommand(final VertxTestContext ctx) throws InterruptedException {
final VertxTestContext setup = new VertxTestContext();
final Tenant tenant = new Tenant();
final MultiMap requestHeaders = MultiMap.caseInsensitiveMultiMap().add(HttpHeaders.CONTENT_TYPE, "text/plain").add(HttpHeaders.AUTHORIZATION, authorization).add(HttpHeaders.ORIGIN, ORIGIN_URI).add(Constants.HEADER_TIME_TILL_DISCONNECT, "2");
helper.registry.addDeviceForTenant(tenantId, tenant, deviceId, PWD).onComplete(setup.succeedingThenComplete());
assertThat(setup.awaitCompletion(5, TimeUnit.SECONDS)).isTrue();
if (setup.failed()) {
ctx.failNow(setup.causeOfFailure());
return;
}
testUploadMessages(ctx, tenantId, msg -> {
// do NOT send a command, but let the HTTP adapter's timer expire
logger.trace("received message");
return msg.getTimeUntilDisconnectNotification().map(notification -> {
ctx.verify(() -> {
assertThat(notification.getTtd()).isEqualTo(2);
assertThat(notification.getTenantId()).isEqualTo(tenantId);
assertThat(notification.getDeviceId()).isEqualTo(deviceId);
});
return Future.<Void>succeededFuture();
}).orElseGet(() -> Future.<Void>succeededFuture());
}, count -> {
return httpClient.create(getEndpointUri(), Buffer.buffer("hello " + count), requestHeaders, ResponsePredicate.status(HttpURLConnection.HTTP_ACCEPTED)).map(responseHeaders -> {
ctx.verify(() -> {
// assert that the response does not contain a command nor a request ID nor a payload
assertThat(responseHeaders.getHeader(Constants.HEADER_COMMAND)).isNull();
assertThat(responseHeaders.getHeader(Constants.HEADER_COMMAND_REQUEST_ID)).isNull();
assertThat(responseHeaders.getHeader(HttpHeaders.CONTENT_LENGTH.toString())).isEqualTo("0");
});
return responseHeaders;
});
}, 5, null);
}
use of org.eclipse.hono.util.Adapter in project hono by eclipse.
the class HttpTestBase method testUploadMessageFailsForDisabledTenant.
/**
* Verifies that the HTTP adapter rejects messages from a device that belongs to a tenant for which the HTTP adapter
* has been disabled with a 403.
*
* @param ctx The test context
*/
@Test
@Timeout(timeUnit = TimeUnit.SECONDS, value = 20)
public void testUploadMessageFailsForDisabledTenant(final VertxTestContext ctx) {
// GIVEN a tenant for which the HTTP adapter is disabled
final Tenant tenant = new Tenant();
tenant.addAdapterConfig(new Adapter(Constants.PROTOCOL_ADAPTER_TYPE_HTTP).setEnabled(false));
helper.registry.addDeviceForTenant(tenantId, tenant, deviceId, PWD).compose(ok -> {
// WHEN a device that belongs to the tenant uploads a message
final MultiMap requestHeaders = MultiMap.caseInsensitiveMultiMap().add(HttpHeaders.CONTENT_TYPE, "text/plain").add(HttpHeaders.AUTHORIZATION, authorization);
return httpClient.create(getEndpointUri(), Buffer.buffer("hello"), requestHeaders, ResponsePredicate.status(HttpURLConnection.HTTP_FORBIDDEN));
}).onComplete(ctx.succeedingThenComplete());
}
Aggregations