From dbe73239ae1f8cf0d3211fde37640d0fc8995a94 Mon Sep 17 00:00:00 2001 From: Aleksandr Kolosov Date: Mon, 19 Aug 2024 11:20:05 +0300 Subject: [PATCH] Address comments --- .../com/mbed/coap/packet/CoapResponse.java | 8 ++-- .../mbedtls/DtlsSessionExpirationFilter.java | 40 ++++++++++++++++++ .../DtlsSessionExpirationFilterTest.java | 42 +++++++++++++++++++ .../transport/mbedtls/MbedtlsNettyTest.java | 20 +++++++-- 4 files changed, 102 insertions(+), 8 deletions(-) create mode 100644 coap-mbedtls/src/main/java/org/opencoap/transport/mbedtls/DtlsSessionExpirationFilter.java create mode 100644 coap-mbedtls/src/test/java/org/opencoap/transport/mbedtls/DtlsSessionExpirationFilterTest.java diff --git a/coap-core/src/main/java/com/mbed/coap/packet/CoapResponse.java b/coap-core/src/main/java/com/mbed/coap/packet/CoapResponse.java index 91314ed6..46640132 100644 --- a/coap-core/src/main/java/com/mbed/coap/packet/CoapResponse.java +++ b/coap-core/src/main/java/com/mbed/coap/packet/CoapResponse.java @@ -117,10 +117,6 @@ public T getTransContext(TransportContext.Key key) { return transContext.get(key); } - public T getTransContext(TransportContext.Key key, T defaultValue) { - return transContext.getOrDefault(key, defaultValue); - } - @Override public boolean equals(Object o) { if (this == o) { @@ -165,6 +161,10 @@ public CoapResponse withOptions(Consumer optionsFunc) { return new CoapResponse(code, payload, optionsBuilder.build(), transContext); } + public CoapResponse withContext(TransportContext otherTransContext) { + return new CoapResponse(code, payload, options, transContext.with(otherTransContext)); + } + public static class Builder { private final Code code; private final CoapOptionsBuilder options = CoapOptionsBuilder.options(); diff --git a/coap-mbedtls/src/main/java/org/opencoap/transport/mbedtls/DtlsSessionExpirationFilter.java b/coap-mbedtls/src/main/java/org/opencoap/transport/mbedtls/DtlsSessionExpirationFilter.java new file mode 100644 index 00000000..80bcd2d2 --- /dev/null +++ b/coap-mbedtls/src/main/java/org/opencoap/transport/mbedtls/DtlsSessionExpirationFilter.java @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2022-2024 java-coap contributors (https://github.com/open-coap/java-coap) + * SPDX-License-Identifier: Apache-2.0 + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.opencoap.transport.mbedtls; + +import static com.mbed.coap.transport.TransportContext.NON_CONFIRMABLE; +import static java.util.concurrent.CompletableFuture.completedFuture; +import static org.opencoap.transport.mbedtls.DtlsTransportContext.DTLS_SESSION_EXPIRATION_HINT; +import com.mbed.coap.packet.CoapRequest; +import com.mbed.coap.packet.CoapResponse; +import com.mbed.coap.transport.TransportContext; +import com.mbed.coap.utils.Filter; +import com.mbed.coap.utils.Service; +import java.util.concurrent.CompletableFuture; + +public class DtlsSessionExpirationFilter implements Filter.SimpleFilter { + + @Override + public CompletableFuture apply(CoapRequest request, Service service) { + if (!request.getTransContext(NON_CONFIRMABLE)) { + return CoapResponse.badRequest().toFuture(); + } + + return service + .apply(request) + .thenCompose(resp -> completedFuture(resp.withContext(TransportContext.of(DTLS_SESSION_EXPIRATION_HINT, true)))); + } +} diff --git a/coap-mbedtls/src/test/java/org/opencoap/transport/mbedtls/DtlsSessionExpirationFilterTest.java b/coap-mbedtls/src/test/java/org/opencoap/transport/mbedtls/DtlsSessionExpirationFilterTest.java new file mode 100644 index 00000000..7b1c637e --- /dev/null +++ b/coap-mbedtls/src/test/java/org/opencoap/transport/mbedtls/DtlsSessionExpirationFilterTest.java @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2022-2024 java-coap contributors (https://github.com/open-coap/java-coap) + * SPDX-License-Identifier: Apache-2.0 + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.opencoap.transport.mbedtls; + +import static com.mbed.coap.packet.CoapResponse.of; +import static java.util.concurrent.CompletableFuture.completedFuture; +import static org.junit.jupiter.api.Assertions.*; +import com.mbed.coap.packet.CoapRequest; +import com.mbed.coap.packet.CoapResponse; +import com.mbed.coap.packet.Code; +import com.mbed.coap.transport.TransportContext; +import com.mbed.coap.utils.Service; +import org.junit.jupiter.api.Test; + +class DtlsSessionExpirationFilterTest { + private final Service service = (new DtlsSessionExpirationFilter()).then(coapRequest -> completedFuture(of(Code.C201_CREATED))); + + @Test + void shouldReturnBadRequestWhenRequestIsConfirmable() { + assertEquals(of(Code.C400_BAD_REQUEST), service.apply(CoapRequest.get("/test").build()).join()); + } + + @Test + void shouldReturnResponseWithExpirationHint() { + CoapResponse resp = service.apply(CoapRequest.get("/test").context(TransportContext.of(TransportContext.NON_CONFIRMABLE, true)).build()).join(); + assertEquals(Code.C201_CREATED, resp.getCode()); + assertTrue(resp.getTransContext().get(DtlsTransportContext.DTLS_SESSION_EXPIRATION_HINT)); + } +} diff --git a/coap-mbedtls/src/test/java/org/opencoap/transport/mbedtls/MbedtlsNettyTest.java b/coap-mbedtls/src/test/java/org/opencoap/transport/mbedtls/MbedtlsNettyTest.java index a68d4179..0f888ac2 100644 --- a/coap-mbedtls/src/test/java/org/opencoap/transport/mbedtls/MbedtlsNettyTest.java +++ b/coap-mbedtls/src/test/java/org/opencoap/transport/mbedtls/MbedtlsNettyTest.java @@ -16,8 +16,10 @@ package org.opencoap.transport.mbedtls; import static com.mbed.coap.packet.CoapRequest.get; +import static com.mbed.coap.packet.CoapRequest.post; import static com.mbed.coap.packet.CoapResponse.badRequest; import static com.mbed.coap.packet.CoapResponse.ok; +import static com.mbed.coap.packet.Code.C201_CREATED; import static com.mbed.coap.packet.Opaque.of; import static com.mbed.coap.utils.Assertions.assertEquals; import static com.mbed.coap.utils.Networks.localhost; @@ -54,6 +56,8 @@ import java.io.IOException; import java.net.InetSocketAddress; import java.nio.ByteBuffer; +import java.util.HashMap; +import java.util.Map; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; @@ -108,7 +112,15 @@ void beforeAll() throws IOException { } }) - .get("/ctx-hint", __ -> ok("OK!").context(TransportContext.of(DTLS_SESSION_EXPIRATION_HINT, true)).toFuture()) + .post("/dtls-ctx", req -> { + String key = req.options().getUriQueryMap().get("key"); + HashMap authCtx = new HashMap<>(req.getTransContext(DTLS_AUTHENTICATION)); + authCtx.put(key, req.getPayload().toUtf8String()); + return CoapResponse.coapResponse(C201_CREATED) + .payload(authCtx.get(key)) + .addContext(DTLS_AUTHENTICATION, authCtx) + .toFuture(); + }) ) .build().start(); srvAddress = localhost(server.getLocalSocketAddress().getPort()); @@ -206,12 +218,12 @@ public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) }); // when client sends a request - coapClient.sendSync(get("/ctx-hint")); + coapClient.sendSync(post("/dtls-ctx").query("key", "foo").payload("bar")); - // then server should see the DTLS context on the response message + // then server should see updated DTLS context on the response message DtlsSessionContext respCtx = coapResponseDtlsContextPromise.get(1, TimeUnit.SECONDS); + assertEquals("bar", respCtx.getAuthenticationContext().get("foo")); assertEquals("dev01", respCtx.getAuthenticationContext().get("dev-id")); - assertTrue(respCtx.getSessionExpirationHint()); coapClient.close(); serverTransport.getChannel().pipeline().remove("test-handler");