Skip to content

Commit

Permalink
Add ReserverProxy to be able to test client IP address changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Jul 3, 2023
1 parent 2066961 commit c8606bc
Show file tree
Hide file tree
Showing 5 changed files with 342 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ public static URI createUri(String uri) {
}
}

public static URI replaceAddress(URI originalUri, InetSocketAddress newAddress) {
try {
return new URI(originalUri.getScheme(), null, newAddress.getHostString(), newAddress.getPort(), null, null,
null);
} catch (URISyntaxException e) {
throw new IllegalStateException(e);
}
}

public static InetSocketAddress getSocketAddr(URI uri) {
return new InetSocketAddress(uri.getHost(), uri.getPort());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*******************************************************************************
* Copyright (c) 2023 Sierra Wireless and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v20.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.html.
*
* Contributors:
* Sierra Wireless - initial API and implementation
*******************************************************************************/
package org.eclipse.leshan.integration.tests.util;

import java.net.InetSocketAddress;
import java.net.URI;

import org.eclipse.leshan.core.endpoint.Protocol;

public class LeshanProxyBuilder {

public static ReverseProxy givenReverseProxyFor(LeshanTestServer server, Protocol protocol) {
URI serverEndpointUri = server.getEndpoint(protocol).getURI();
return new ReverseProxy(new InetSocketAddress("localhost", 0),
new InetSocketAddress(serverEndpointUri.getHost(), serverEndpointUri.getPort()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.timeout;

import java.net.InetSocketAddress;
import java.net.URI;
import java.security.cert.Certificate;
import java.util.List;
import java.util.Map;
Expand All @@ -43,11 +45,13 @@
import org.eclipse.leshan.client.send.DataSender;
import org.eclipse.leshan.client.servers.LwM2mServer;
import org.eclipse.leshan.client.util.LinkFormatHelper;
import org.eclipse.leshan.core.endpoint.EndpointUriUtil;
import org.eclipse.leshan.core.link.LinkSerializer;
import org.eclipse.leshan.core.link.lwm2m.attributes.LwM2mAttributeParser;
import org.eclipse.leshan.core.node.codec.LwM2mDecoder;
import org.eclipse.leshan.core.node.codec.LwM2mEncoder;
import org.eclipse.leshan.server.bootstrap.LeshanBootstrapServer;
import org.eclipse.leshan.server.endpoint.LwM2mServerEndpoint;
import org.mockito.ArgumentCaptor;
import org.mockito.InOrder;

Expand All @@ -57,20 +61,23 @@ public class LeshanTestClient extends LeshanClient {

private final String endpointName;
private final InOrder inOrder;
private final ReverseProxy proxy;

public LeshanTestClient(String endpoint, List<? extends LwM2mObjectEnabler> objectEnablers,
List<DataSender> dataSenders, List<Certificate> trustStore, RegistrationEngineFactory engineFactory,
BootstrapConsistencyChecker checker, Map<String, String> additionalAttributes,
Map<String, String> bsAdditionalAttributes, LwM2mEncoder encoder, LwM2mDecoder decoder,
ScheduledExecutorService sharedExecutor, LinkSerializer linkSerializer, LinkFormatHelper linkFormatHelper,
LwM2mAttributeParser attributeParser, LwM2mClientEndpointsProvider endpointsProvider) {
LwM2mAttributeParser attributeParser, LwM2mClientEndpointsProvider endpointsProvider, ReverseProxy proxy) {
super(endpoint, objectEnablers, dataSenders, trustStore, engineFactory, checker, additionalAttributes,
bsAdditionalAttributes, encoder, decoder, sharedExecutor, linkSerializer, linkFormatHelper,
attributeParser, endpointsProvider);

// Store some internal attribute
this.endpointName = endpoint;

this.proxy = proxy;

// Add Mock Listener
clientObserver = mock(LwM2mClientObserver.class);
addObserver(clientObserver);
Expand Down Expand Up @@ -100,31 +107,20 @@ public void waitForRegistrationTo(LeshanTestServer server) {

public void waitForRegistrationTo(LeshanTestServer server, long timeout, TimeUnit unit) {
inOrder.verify(clientObserver, timeout(unit.toMillis(timeout)).times(1)).onRegistrationStarted(assertArg( //
s -> {
assertThat(server.getEndpoints()) //
.filteredOn(ep -> ep.getURI().toString().equals(s.getUri())) //
.hasSize(1);

}), //
s -> assertThat(isServerIdentifiedByUri(server, s.getUri()))), //
isNotNull());
inOrder.verify(clientObserver, timeout(unit.toMillis(timeout)).times(1)).onRegistrationSuccess(assertArg( //
s -> assertThat(server.getEndpoints()) //
.filteredOn(ep -> ep.getURI().toString().equals(s.getUri())) //
.hasSize(1)), //
s -> assertThat(isServerIdentifiedByUri(server, s.getUri()))), //
isNotNull(), isNotNull());
inOrder.verifyNoMoreInteractions();
}

public void waitForUpdateTo(LeshanTestServer server, long timeout, TimeUnit unit) {
inOrder.verify(clientObserver, timeout(unit.toMillis(timeout)).times(1)).onUpdateStarted(assertArg( //
s -> assertThat(server.getEndpoints()) //
.filteredOn(ep -> ep.getURI().toString().equals(s.getUri())) //
.hasSize(1)), //
s -> assertThat(isServerIdentifiedByUri(server, s.getUri()))), //
notNull());
inOrder.verify(clientObserver, timeout(unit.toMillis(timeout)).times(1)).onUpdateSuccess(assertArg( //
s -> assertThat(server.getEndpoints()) //
.filteredOn(ep -> ep.getURI().toString().equals(s.getUri())) //
.hasSize(1)), //
s -> assertThat(isServerIdentifiedByUri(server, s.getUri()))), //
notNull());
inOrder.verifyNoMoreInteractions();
}
Expand All @@ -135,17 +131,10 @@ public void waitForDeregistrationTo(LeshanTestServer server) {

public void waitForDeregistrationTo(LeshanTestServer server, long timeout, TimeUnit unit) {
inOrder.verify(clientObserver, timeout(unit.toMillis(timeout)).times(1)).onDeregistrationStarted(assertArg( //
s -> {
assertThat(server.getEndpoints()) //
.filteredOn(ep -> ep.getURI().toString().equals(s.getUri())) //
.hasSize(1);

}), //
s -> assertThat(isServerIdentifiedByUri(server, s.getUri()))), //
isNotNull());
inOrder.verify(clientObserver, timeout(unit.toMillis(timeout)).times(1)).onDeregistrationSuccess(assertArg( //
s -> assertThat(server.getEndpoints()) //
.filteredOn(ep -> ep.getURI().toString().equals(s.getUri())) //
.hasSize(1)), //
s -> assertThat(isServerIdentifiedByUri(server, s.getUri()))), //
isNotNull());
inOrder.verifyNoMoreInteractions();
}
Expand All @@ -156,15 +145,11 @@ public void waitForUpdateTimeoutTo(LeshanTestServer server) {

public void waitForUpdateTimeoutTo(LeshanTestServer server, long timeout, TimeUnit unit) {
inOrder.verify(clientObserver, timeout(unit.toMillis(timeout)).times(1)).onUpdateStarted(assertArg( //
s -> assertThat(server.getEndpoints()) //
.filteredOn(ep -> ep.getURI().toString().equals(s.getUri())) //
.hasSize(1)), //
s -> assertThat(isServerIdentifiedByUri(server, s.getUri()))), //
notNull());

inOrder.verify(clientObserver, timeout(unit.toMillis(timeout)).times(1)).onUpdateTimeout(assertArg( //
s -> assertThat(server.getEndpoints()) //
.filteredOn(ep -> ep.getURI().toString().equals(s.getUri())) //
.hasSize(1)), //
s -> assertThat(isServerIdentifiedByUri(server, s.getUri()))), //
notNull());
// if client update timeout, it will retry again then try a register so all events can not be consume by inOrder
// ...
Expand All @@ -176,14 +161,10 @@ public void waitForUpdateFailureTo(LeshanTestServer server) {

public void waitForUpdateFailureTo(LeshanTestServer server, long timeout, TimeUnit unit) {
inOrder.verify(clientObserver, timeout(unit.toMillis(timeout)).times(1)).onUpdateStarted(assertArg( //
s -> assertThat(server.getEndpoints()) //
.filteredOn(ep -> ep.getURI().toString().equals(s.getUri())) //
.hasSize(1)), //
s -> assertThat(isServerIdentifiedByUri(server, s.getUri()))), //
notNull());
inOrder.verify(clientObserver, timeout(unit.toMillis(timeout)).times(1)).onUpdateFailure(assertArg( //
s -> assertThat(server.getEndpoints()) //
.filteredOn(ep -> ep.getURI().toString().equals(s.getUri())) //
.hasSize(1)), //
s -> assertThat(isServerIdentifiedByUri(server, s.getUri()))), //
notNull(), //
notNull(), // TODO we should be able to check response code
any(), //
Expand Down Expand Up @@ -225,4 +206,22 @@ public Exception waitForBootstrapFailure(LeshanBootstrapServer server, long time
c.capture());
return c.getValue();
}

private boolean isServerIdentifiedByUri(LeshanTestServer server, String expectedUri) {
for (LwM2mServerEndpoint endpoint : server.getEndpoints()) {
URI endpointURI = endpoint.getURI();
InetSocketAddress endpointAddr = EndpointUriUtil.getSocketAddr(endpointURI);
if (proxy != null && endpointAddr.equals(proxy.getServerAddress())) {
URI proxyUri = EndpointUriUtil.replaceAddress(endpointURI, proxy.getClientSideProxyAddress());
if (proxyUri.toString().equals(expectedUri)) {
return true;
}
} else {
if (endpointURI.toString().equals(expectedUri)) {
return true;
}
}
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import org.eclipse.leshan.client.util.LinkFormatHelper;
import org.eclipse.leshan.core.CertificateUsage;
import org.eclipse.leshan.core.LwM2mId;
import org.eclipse.leshan.core.endpoint.EndpointUriUtil;
import org.eclipse.leshan.core.endpoint.Protocol;
import org.eclipse.leshan.core.link.LinkSerializer;
import org.eclipse.leshan.core.link.lwm2m.attributes.LwM2mAttributeParser;
Expand Down Expand Up @@ -88,6 +89,7 @@ public class LeshanTestClientBuilder extends LeshanClientBuilder {
private Protocol protocolToUse;
private LeshanServer server;
private LeshanBootstrapServer bootstrapServer;
private ReverseProxy proxy;

private Integer bootstrapServerId;

Expand Down Expand Up @@ -128,8 +130,8 @@ public LeshanTestClient build() {
try {
// connect to LWM2M Server
if (server != null) {
LwM2mServerEndpoint endpoint = server.getEndpoint(protocolToUse);
URI uri = endpoint.getURI();
URI uri = getServerUri();

int serverID = 12345;

if (pskIdentity != null && pskKey != null) {
Expand Down Expand Up @@ -234,7 +236,7 @@ protected LeshanTestClient createLeshanClient(String endpoint, List<? extends Lw

return new LeshanTestClient(endpointName, objectEnablers, dataSenders, trustStore, engineFactory, checker,
additionalAttributes, bsAdditionalAttributes, encoder, decoder, sharedExecutor, linkSerializer,
linkFormatHelper, attributeParser, endpointsProvider);
linkFormatHelper, attributeParser, endpointsProvider, proxy);
}

public static LeshanTestClientBuilder givenClientUsing(Protocol protocol) {
Expand Down Expand Up @@ -425,4 +427,20 @@ public ExecuteResponse execute(LwM2mServer server, int resourceid, Arguments arg
}
}
}

public LeshanTestClientBuilder behind(ReverseProxy proxy) {
this.proxy = proxy;
return this;
}

private URI getServerUri() {
LwM2mServerEndpoint endpoint = server.getEndpoint(protocolToUse);
URI serverUri = endpoint.getURI();
if (proxy != null) {
// if server is behind a proxy we use its URI
return EndpointUriUtil.replaceAddress(serverUri, proxy.getClientSideProxyAddress());
} else {
return serverUri;
}
}
}
Loading

0 comments on commit c8606bc

Please sign in to comment.