Skip to content

Commit

Permalink
Address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
akolosov-n committed Aug 19, 2024
1 parent 75dcae5 commit dbe7323
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,6 @@ public <T> T getTransContext(TransportContext.Key<T> key) {
return transContext.get(key);
}

public <T> T getTransContext(TransportContext.Key<T> key, T defaultValue) {
return transContext.getOrDefault(key, defaultValue);
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down Expand Up @@ -165,6 +161,10 @@ public CoapResponse withOptions(Consumer<CoapOptionsBuilder> optionsFunc) {
return new CoapResponse(code, payload, optionsBuilder.build(), transContext);
}

public CoapResponse withContext(TransportContext otherTransContext) {
return new CoapResponse(code, payload, options, transContext.with(otherTransContext));

Check warning on line 165 in coap-core/src/main/java/com/mbed/coap/packet/CoapResponse.java

View check run for this annotation

Codecov / codecov/patch

coap-core/src/main/java/com/mbed/coap/packet/CoapResponse.java#L165

Added line #L165 was not covered by tests
}

public static class Builder {
private final Code code;
private final CoapOptionsBuilder options = CoapOptionsBuilder.options();
Expand Down
Original file line number Diff line number Diff line change
@@ -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<CoapRequest, CoapResponse> {

@Override
public CompletableFuture<CoapResponse> apply(CoapRequest request, Service<CoapRequest, CoapResponse> 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))));
}
}
Original file line number Diff line number Diff line change
@@ -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<CoapRequest, CoapResponse> 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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, String> 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());
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit dbe7323

Please sign in to comment.