use of org.eclipse.hono.util.ResourceIdentifier in project hono by eclipse.
the class HonoMessagingMessageFilterTest method testVerifyDetectsMissingContentType.
@Test
public void testVerifyDetectsMissingContentType() {
// GIVEN a valid telemetry message without content type
final Message msg = givenAMessageHavingProperties(MY_DEVICE, MY_TENANT, null, new byte[] { 0x00 });
// WHEN receiving the message via a link with matching tenant
final ResourceIdentifier linkTarget = getResourceIdentifier(MY_TENANT);
// THEN message validation fails
assertFalse(HonoMessagingMessageFilter.verify(linkTarget, msg));
}
use of org.eclipse.hono.util.ResourceIdentifier in project hono by eclipse.
the class HonoMessagingMessageFilterTest method assertMessageAnnotationsContainProperties.
private void assertMessageAnnotationsContainProperties(final Message msg, final String tenantId, final String deviceId) {
assertNotNull(msg.getMessageAnnotations());
assertThat(msg.getMessageAnnotations().getValue().get(Symbol.valueOf(MessageHelper.APP_PROPERTY_TENANT_ID)), is(tenantId));
assertThat(msg.getMessageAnnotations().getValue().get(Symbol.valueOf(MessageHelper.APP_PROPERTY_DEVICE_ID)), is(deviceId));
final ResourceIdentifier expectedResourceIdentifier = getResourceIdentifier(MY_TENANT, MY_DEVICE);
assertThat(msg.getMessageAnnotations().getValue().get(Symbol.valueOf(APP_PROPERTY_RESOURCE)), is(expectedResourceIdentifier.toString()));
}
use of org.eclipse.hono.util.ResourceIdentifier in project hono by eclipse.
the class HonoMessagingMessageFilterTest method testVerifyDetectsMissingRegistrationAssertion.
/**
* Verifies that the filter rejects messages lacking a registration assertion
* property.
*/
@Test
public void testVerifyDetectsMissingRegistrationAssertion() {
// GIVEN a valid telemetry message without registration assertion
final Message msg = givenAMessageHavingProperties(MY_DEVICE, MY_TENANT, null, CONTENT_TYPE_OCTET_STREAM, new byte[] { 0x00 });
// WHEN receiving the message via a link with matching tenant
final ResourceIdentifier linkTarget = getResourceIdentifier(MY_TENANT);
// THEN message validation fails
assertFalse(HonoMessagingMessageFilter.verify(linkTarget, msg));
}
use of org.eclipse.hono.util.ResourceIdentifier in project hono by eclipse.
the class SenderFactoryImplTest method testNewSenderIsClosedOnRemoteDetachOrClose.
@SuppressWarnings({ "unchecked", "rawtypes" })
private void testNewSenderIsClosedOnRemoteDetachOrClose(final TestContext ctx, final BiConsumer<ProtonSender, ArgumentCaptor<Handler>> handlerCaptor) {
// GIVEN a sender created by the factory
final Async senderCreation = ctx.async();
final ProtonSender sender = mock(ProtonSender.class);
when(sender.open()).then(answer -> {
senderCreation.complete();
return sender;
});
final ProtonConnection con = mock(ProtonConnection.class);
final ProtonSession session = mock(ProtonSession.class);
when(session.createSender(anyString())).thenReturn(sender);
final ResourceIdentifier address = ResourceIdentifier.from(TelemetryConstants.TELEMETRY_ENDPOINT, Constants.DEFAULT_TENANT, null);
final Handler<String> closeHook = mock(Handler.class);
final SenderFactoryImpl factory = new SenderFactoryImpl();
final ArgumentCaptor<Handler> captor = ArgumentCaptor.forClass(Handler.class);
factory.newSender(con, session, address, ProtonQoS.AT_LEAST_ONCE, drain -> {
}, closeHook);
handlerCaptor.accept(sender, captor);
// WHEN the peer detaches from the sender
captor.getValue().handle(Future.succeededFuture(sender));
// THEN the sender gets closed
verify(sender).close();
// and the close hook is called
verify(closeHook).handle(any());
}
use of org.eclipse.hono.util.ResourceIdentifier in project hono by eclipse.
the class SimpleAuthenticationServer method handleSenderOpen.
/**
* Handles a request from a client to establish a link for receiving messages from this server.
*
* @param con the connection to the client.
* @param sender the sender created for the link.
*/
@Override
protected void handleSenderOpen(final ProtonConnection con, final ProtonSender sender) {
final Source remoteSource = sender.getRemoteSource();
LOG.debug("client [{}] wants to open a link for receiving messages [address: {}]", con.getRemoteContainer(), remoteSource);
try {
final ResourceIdentifier targetResource = getResourceIdentifier(remoteSource.getAddress());
final AmqpEndpoint endpoint = getEndpoint(targetResource);
if (endpoint == null) {
LOG.debug("no endpoint registered for node [{}]", targetResource);
con.setCondition(ProtonHelper.condition(AmqpError.NOT_FOUND, "no such node")).close();
} else {
HonoUser user = Constants.getClientPrincipal(con);
if (Constants.SUBJECT_ANONYMOUS.equals(user.getName())) {
con.setCondition(ProtonHelper.condition(AmqpError.UNAUTHORIZED_ACCESS, "client must authenticate using SASL")).close();
} else {
Constants.copyProperties(con, sender);
sender.setSource(sender.getRemoteSource());
endpoint.onLinkAttach(con, sender, targetResource);
vertx.setTimer(5000, closeCon -> {
if (!con.isDisconnected()) {
LOG.debug("connection with client [{}] timed out after 5 seconds, closing connection", con.getRemoteContainer());
con.setCondition(ProtonHelper.condition("hono: inactivity", "client must retrieve token within 5 secs after opening connection")).close();
}
});
}
}
} catch (final IllegalArgumentException e) {
LOG.debug("client has provided invalid resource identifier as source address", e);
con.setCondition(ProtonHelper.condition(AmqpError.INVALID_FIELD, "malformed source address")).close();
}
}
Aggregations