Skip to content

Commit

Permalink
Add some integration tests about "bootstrap delete"
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Apr 3, 2019
1 parent ea2e70b commit 323a1bf
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@
import org.eclipse.leshan.client.object.Device;
import org.eclipse.leshan.client.object.Security;
import org.eclipse.leshan.client.resource.DummyInstanceEnabler;
import org.eclipse.leshan.client.resource.LwM2mObjectEnabler;
import org.eclipse.leshan.client.resource.ObjectsInitializer;
import org.eclipse.leshan.client.resource.SimpleInstanceEnabler;
import org.eclipse.leshan.core.request.Identity;
import org.eclipse.leshan.server.bootstrap.BootstrapConfig;
import org.eclipse.leshan.server.bootstrap.BootstrapConfig.ACLConfig;
Expand Down Expand Up @@ -122,25 +120,26 @@ public void createBootstrapServer(BootstrapSecurityStore securityStore) {
createBootstrapServer(securityStore, null);
}

@Override
public void createClient() {
public Security withoutSecurity() {
// Create Security Object (with bootstrap server only)
String bsUrl = "coap://" + bootstrapServer.getUnsecuredAddress().getHostString() + ":"
+ bootstrapServer.getUnsecuredAddress().getPort();
Security security = new Security(bsUrl, true, 3, new byte[0], new byte[0], new byte[0], 12345);
return new Security(bsUrl, true, 3, new byte[0], new byte[0], new byte[0], 12345);
}

createClient(security);
@Override
public void createClient() {
createClient(withoutSecurity(), null);
}

public void createPSKClient(String pskIdentity, byte[] pskKey) {

// Create Security Object (with bootstrap server only)
String bsUrl = "coaps://" + bootstrapServer.getSecuredAddress().getHostString() + ":"
+ bootstrapServer.getSecuredAddress().getPort();
byte[] pskId = pskIdentity.getBytes(StandardCharsets.UTF_8);
Security security = Security.pskBootstrap(bsUrl, pskId, pskKey);

createClient(security);
createClient(security, null);
}

@Override
Expand All @@ -150,23 +149,26 @@ public void createRPKClient() {
Security security = Security.rpkBootstrap(bsUrl, clientPublicKey.getEncoded(), clientPrivateKey.getEncoded(),
bootstrapServerPublicKey.getEncoded());

createClient(security);
createClient(security, null);
}

private void createClient(Security security) {
ObjectsInitializer initializer = new ObjectsInitializer();
public void createClient(Security security, ObjectsInitializer initializer) {
if (initializer == null) {
initializer = new ObjectsInitializer();
}

// Initialize LWM2M Object Tree
initializer.setInstancesForObject(LwM2mId.SECURITY, security);
initializer.setInstancesForObject(LwM2mId.DEVICE,
new Device("Eclipse Leshan", IntegrationTestHelper.MODEL_NUMBER, "12345", "U"));
initializer.setClassForObject(LwM2mId.ACCESS_CONTROL, SimpleInstanceEnabler.class);
initializer.setClassForObject(LwM2mId.SERVER, DummyInstanceEnabler.class);
List<LwM2mObjectEnabler> objects = initializer.createAll();
createClient(initializer);
}

public void createClient(ObjectsInitializer initializer) {
// Create Leshan Client
LeshanClientBuilder builder = new LeshanClientBuilder(getCurrentEndpoint());
builder.setObjects(objects);
builder.setObjects(initializer.createAll());
client = builder.build();
setupClientMonitoring();
}
Expand Down Expand Up @@ -262,6 +264,28 @@ public BootstrapConfig getBootstrap(String endpoint, Identity deviceIdentity) {
};
}

public BootstrapStore deleteSecurityStore(Integer... objectToDelete) {
String[] pathToDelete = new String[objectToDelete.length];
for (int i = 0; i < pathToDelete.length; i++) {
pathToDelete[i] = "/" + objectToDelete[i];

}
return deleteSecurityStore(pathToDelete);
}

public BootstrapStore deleteSecurityStore(final String... pathToDelete) {
return new BootstrapStore() {

@Override
public BootstrapConfig getBootstrap(String endpoint, Identity deviceIdentity) {

BootstrapConfig bsConfig = new BootstrapConfig();
bsConfig.toDelete = Arrays.asList(pathToDelete);
return bsConfig;
}
};
}

public BootstrapStore unsecuredWithAclBootstrapStore() {
return new BootstrapStore() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@
import static org.hamcrest.CoreMatchers.hasItems;
import static org.junit.Assert.*;

import org.eclipse.leshan.LwM2mId;
import org.eclipse.leshan.SecurityMode;
import org.eclipse.leshan.client.request.ServerIdentity;
import org.eclipse.leshan.client.resource.ObjectsInitializer;
import org.eclipse.leshan.client.resource.SimpleInstanceEnabler;
import org.eclipse.leshan.core.node.LwM2mObject;
import org.eclipse.leshan.core.node.LwM2mObjectInstance;
import org.eclipse.leshan.core.request.ReadRequest;
Expand Down Expand Up @@ -70,6 +73,39 @@ public void bootstrap() {
helper.assertClientRegisterered();
}

@Test
public void bootstrapDeleteSecurity() {
// Create DM Server without security & start it
helper.createServer();
helper.server.start();

// Create and start bootstrap server
helper.createBootstrapServer(null,
helper.deleteSecurityStore(LwM2mId.ACCESS_CONTROL, LwM2mId.CONNECTIVITY_STATISTICS));
helper.bootstrapServer.start();

// Create Client and check it is not already registered
ObjectsInitializer initializer = new ObjectsInitializer();
initializer.setInstancesForObject(LwM2mId.ACCESS_CONTROL, new SimpleInstanceEnabler());
initializer.setInstancesForObject(LwM2mId.CONNECTIVITY_STATISTICS, new SimpleInstanceEnabler());
helper.createClient(helper.withoutSecurity(), initializer);
helper.assertClientNotRegisterered();

// Start it and wait for bootstrap finished
helper.client.start();
helper.waitForBootstrapFinishedAtClientSide(1);

// ensure instances are deleted
ReadResponse response = helper.client.getObjectEnablers().get(LwM2mId.ACCESS_CONTROL)
.read(ServerIdentity.SYSTEM, new ReadRequest(LwM2mId.ACCESS_CONTROL));
assertTrue("ACL instance is not deleted", ((LwM2mObject) response.getContent()).getInstances().isEmpty());

response = helper.client.getObjectEnablers().get(LwM2mId.CONNECTIVITY_STATISTICS).read(ServerIdentity.SYSTEM,
new ReadRequest(LwM2mId.CONNECTIVITY_STATISTICS));
assertTrue("Connectvity instance is not deleted",
((LwM2mObject) response.getContent()).getInstances().isEmpty());
}

@Test
public void bootstrapWithAcl() {
// Create DM Server without security & start it
Expand All @@ -81,7 +117,9 @@ public void bootstrapWithAcl() {
helper.bootstrapServer.start();

// Create Client and check it is not already registered
helper.createClient();
ObjectsInitializer initializer = new ObjectsInitializer();
initializer.setInstancesForObject(LwM2mId.ACCESS_CONTROL, new SimpleInstanceEnabler());
helper.createClient(helper.withoutSecurity(), initializer);
helper.assertClientNotRegisterered();

// Start it and wait for registration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,14 @@ public void waitForUpdateAtClientSide(long timeInSeconds) {
}
}

public void waitForBootstrapFinishedAtClientSide(long timeInSeconds) {
try {
assertTrue(clientObserver.waitForBootstrap(timeInSeconds, TimeUnit.SECONDS));
} catch (InterruptedException | TimeoutException e) {
throw new RuntimeException(e);
}
}

public void ensureNoUpdate(long timeInSeconds) {
try {
registrationListener.waitForUpdate(timeInSeconds, TimeUnit.SECONDS);
Expand Down

0 comments on commit 323a1bf

Please sign in to comment.