|
|
@ -20,26 +20,20 @@ import org.eclipse.californium.elements.config.Configuration; |
|
|
import org.eclipse.californium.scandium.DTLSConnector; |
|
|
import org.eclipse.californium.scandium.DTLSConnector; |
|
|
import org.eclipse.californium.scandium.dtls.Connection; |
|
|
import org.eclipse.californium.scandium.dtls.Connection; |
|
|
import org.eclipse.californium.scandium.dtls.ConnectionId; |
|
|
import org.eclipse.californium.scandium.dtls.ConnectionId; |
|
|
import org.eclipse.californium.scandium.dtls.DTLSSession; |
|
|
|
|
|
import org.eclipse.californium.scandium.dtls.InMemoryReadWriteLockConnectionStore; |
|
|
import org.eclipse.californium.scandium.dtls.InMemoryReadWriteLockConnectionStore; |
|
|
import org.eclipse.californium.scandium.dtls.ResumptionSupportingConnectionStore; |
|
|
import org.eclipse.californium.scandium.dtls.ResumptionSupportingConnectionStore; |
|
|
import org.eclipse.leshan.client.californium.endpoint.CaliforniumClientEndpoint; |
|
|
import org.eclipse.leshan.client.californium.endpoint.CaliforniumClientEndpoint; |
|
|
import org.eclipse.leshan.client.californium.endpoint.CaliforniumClientEndpointsProvider; |
|
|
import org.eclipse.leshan.client.californium.endpoint.CaliforniumClientEndpointsProvider; |
|
|
import org.eclipse.leshan.client.endpoint.LwM2mClientEndpoint; |
|
|
|
|
|
import org.eclipse.leshan.client.servers.LwM2mServer; |
|
|
import org.eclipse.leshan.client.servers.LwM2mServer; |
|
|
import org.eclipse.leshan.core.peer.IpPeer; |
|
|
import org.eclipse.leshan.core.peer.IpPeer; |
|
|
import org.eclipse.leshan.core.peer.LwM2mPeer; |
|
|
|
|
|
import org.junit.Assert; |
|
|
import org.junit.Assert; |
|
|
import org.thingsboard.server.common.data.Device; |
|
|
import org.thingsboard.server.common.data.Device; |
|
|
import org.thingsboard.server.common.data.DeviceProfile; |
|
|
import org.thingsboard.server.common.data.DeviceProfile; |
|
|
import org.thingsboard.server.dao.service.DaoSqlTest; |
|
|
import org.thingsboard.server.dao.service.DaoSqlTest; |
|
|
import org.thingsboard.server.transport.lwm2m.client.Lwm2mServer; |
|
|
|
|
|
import org.thingsboard.server.transport.lwm2m.security.AbstractSecurityLwM2MIntegrationTest; |
|
|
import org.thingsboard.server.transport.lwm2m.security.AbstractSecurityLwM2MIntegrationTest; |
|
|
|
|
|
|
|
|
import java.lang.reflect.Field; |
|
|
import java.lang.reflect.Field; |
|
|
import java.net.InetSocketAddress; |
|
|
import java.net.InetSocketAddress; |
|
|
import java.util.Collection; |
|
|
|
|
|
import java.util.Collections; |
|
|
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
|
|
|
|
|
import static org.awaitility.Awaitility.await; |
|
|
import static org.awaitility.Awaitility.await; |
|
|
@ -54,13 +48,13 @@ public abstract class AbstractSecurityLwM2MIntegrationDtlsCidLengthTest extends |
|
|
|
|
|
|
|
|
protected String awaitAlias; |
|
|
protected String awaitAlias; |
|
|
|
|
|
|
|
|
protected void testNoSecDtlsCidLength(Integer dtlsCidLength, Integer serverDtlsCidLength) throws Exception { |
|
|
protected void testNoSecDtlsCidLength(Integer clientDtlsCidLength, Integer serverDtlsCidLength) throws Exception { |
|
|
initDeviceCredentialsNoSek(); |
|
|
initDeviceCredentialsNoSek(); |
|
|
basicTestConnectionDtlsCidLength(dtlsCidLength, serverDtlsCidLength); |
|
|
basicTestConnectionDtlsCidLength(clientDtlsCidLength, serverDtlsCidLength); |
|
|
} |
|
|
} |
|
|
protected void testPskDtlsCidLength(Integer dtlsCidLength, Integer serverDtlsCidLength) throws Exception { |
|
|
protected void testPskDtlsCidLength(Integer clientDtlsCidLength, Integer serverDtlsCidLength) throws Exception { |
|
|
initDeviceCredentialsPsk(); |
|
|
initDeviceCredentialsPsk(); |
|
|
basicTestConnectionDtlsCidLength(dtlsCidLength, serverDtlsCidLength); |
|
|
basicTestConnectionDtlsCidLength(clientDtlsCidLength, serverDtlsCidLength); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
protected void basicTestConnectionDtlsCidLength(Integer clientDtlsCidLength, |
|
|
protected void basicTestConnectionDtlsCidLength(Integer clientDtlsCidLength, |
|
|
@ -84,31 +78,38 @@ public abstract class AbstractSecurityLwM2MIntegrationDtlsCidLengthTest extends |
|
|
Assert.assertTrue(lwM2MTestClient.getClientDtlsCid().isEmpty()); |
|
|
Assert.assertTrue(lwM2MTestClient.getClientDtlsCid().isEmpty()); |
|
|
} else { |
|
|
} else { |
|
|
Assert.assertEquals(2L, lwM2MTestClient.getClientDtlsCid().size()); |
|
|
Assert.assertEquals(2L, lwM2MTestClient.getClientDtlsCid().size()); |
|
|
Assert.assertTrue(lwM2MTestClient.getClientDtlsCid().keySet().contains(ON_READ_CONNECTION_ID)); |
|
|
Assert.assertTrue(lwM2MTestClient.getClientDtlsCid().containsKey(ON_READ_CONNECTION_ID)); |
|
|
if (serverDtlsCidLength == null) { |
|
|
Assert.assertTrue(lwM2MTestClient.getClientDtlsCid().containsKey(ON_WRITE_CONNECTION_ID)); |
|
|
|
|
|
|
|
|
|
|
|
LwM2mServer lwM2mServer = lwM2MTestClient.getLeshanClient().getRegisteredServers().entrySet().stream().findFirst().get().getValue(); |
|
|
|
|
|
CaliforniumClientEndpoint lwM2mClientEndpoint = (CaliforniumClientEndpoint) lwM2MTestClient.getLeshanClient().getEndpoint(lwM2mServer); |
|
|
|
|
|
Connection connection = getConnection(lwM2mClientEndpoint, lwM2mServer); |
|
|
|
|
|
ConnectionId clientCid = connection.getConnectionId(); |
|
|
|
|
|
ConnectionId readCid = connection.getEstablishedDtlsContext().getReadConnectionId(); |
|
|
|
|
|
ConnectionId serverCid = connection.getEstablishedDtlsContext().getWriteConnectionId(); |
|
|
|
|
|
if (serverDtlsCidLength == null || clientDtlsCidLength == null) { |
|
|
|
|
|
// cid is not used
|
|
|
Assert.assertNull(lwM2MTestClient.getClientDtlsCid().get(ON_WRITE_CONNECTION_ID)); |
|
|
Assert.assertNull(lwM2MTestClient.getClientDtlsCid().get(ON_WRITE_CONNECTION_ID)); |
|
|
Assert.assertNull(lwM2MTestClient.getClientDtlsCid().get(ON_READ_CONNECTION_ID)); |
|
|
Assert.assertNull(lwM2MTestClient.getClientDtlsCid().get(ON_READ_CONNECTION_ID)); |
|
|
|
|
|
Assert.assertNull(readCid); |
|
|
|
|
|
Assert.assertNull(serverCid); |
|
|
} else { |
|
|
} else { |
|
|
|
|
|
Assert.assertEquals(serverDtlsCidLength, lwM2MTestClient.getClientDtlsCid().get(ON_WRITE_CONNECTION_ID)); |
|
|
Assert.assertEquals(clientDtlsCidLength, lwM2MTestClient.getClientDtlsCid().get(ON_READ_CONNECTION_ID)); |
|
|
Assert.assertEquals(clientDtlsCidLength, lwM2MTestClient.getClientDtlsCid().get(ON_READ_CONNECTION_ID)); |
|
|
if (clientDtlsCidLength == null) { |
|
|
// cid used
|
|
|
Assert.assertNull(lwM2MTestClient.getClientDtlsCid().get(ON_READ_CONNECTION_ID)); |
|
|
Assert.assertNotNull(clientCid); |
|
|
} else { |
|
|
Assert.assertNotNull(readCid); |
|
|
Integer expectedWrite = Math.max(clientDtlsCidLength, serverDtlsCidLength); |
|
|
if (clientDtlsCidLength > 0) { |
|
|
// Assert.assertEquals(expectedWrite, lwM2MTestClient.getClientDtlsCid().get(ON_WRITE_CONNECTION_ID));
|
|
|
Assert.assertEquals(clientCid, readCid); |
|
|
Assert.assertEquals(serverDtlsCidLength, lwM2MTestClient.getClientDtlsCid().get(ON_WRITE_CONNECTION_ID)); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
Assert.assertNotNull(serverCid); |
|
|
|
|
|
int actualServerCidLength = serverCid.getBytes().length; |
|
|
|
|
|
int expectedServerCidLength = serverDtlsCidLength; |
|
|
|
|
|
Assert.assertEquals(expectedServerCidLength, actualServerCidLength); |
|
|
} |
|
|
} |
|
|
LwM2mServer lwM2mServer = lwM2MTestClient.getLeshanClient().getRegisteredServers().entrySet().stream().findFirst().get().getValue(); |
|
|
|
|
|
CaliforniumClientEndpoint lwM2mClientEndpoint = (CaliforniumClientEndpoint) lwM2MTestClient.getLeshanClient().getEndpoint(lwM2mServer); |
|
|
if (clientCid != null) { |
|
|
DTLSConnector connector = (DTLSConnector) lwM2mClientEndpoint.getCoapEndpoint().getConnector(); |
|
|
int actualClientCidLength = clientCid.getBytes().length; |
|
|
Field field = DTLSConnector.class.getDeclaredField("connectionStore"); |
|
|
|
|
|
field.setAccessible(true); |
|
|
|
|
|
ResumptionSupportingConnectionStore connectionStore = (InMemoryReadWriteLockConnectionStore) field.get(connector); |
|
|
|
|
|
InetSocketAddress serverAddr = ((IpPeer) lwM2mServer.getTransportData()).getSocketAddress(); |
|
|
|
|
|
Connection connection = connectionStore.get(serverAddr); |
|
|
|
|
|
ConnectionId cid = connection.getConnectionId(); |
|
|
|
|
|
if (cid != null) { |
|
|
|
|
|
int actualClientCidLength = cid.getBytes().length; |
|
|
|
|
|
int expectedClientCidLength; |
|
|
int expectedClientCidLength; |
|
|
if (clientDtlsCidLength == null || clientDtlsCidLength == 0) { |
|
|
if (clientDtlsCidLength == null || clientDtlsCidLength == 0) { |
|
|
expectedClientCidLength = 3; |
|
|
expectedClientCidLength = 3; |
|
|
@ -119,4 +120,13 @@ public abstract class AbstractSecurityLwM2MIntegrationDtlsCidLengthTest extends |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static Connection getConnection(CaliforniumClientEndpoint lwM2mClientEndpoint, LwM2mServer lwM2mServer) throws NoSuchFieldException, IllegalAccessException { |
|
|
|
|
|
DTLSConnector connector = (DTLSConnector) lwM2mClientEndpoint.getCoapEndpoint().getConnector(); |
|
|
|
|
|
Field field = DTLSConnector.class.getDeclaredField("connectionStore"); |
|
|
|
|
|
field.setAccessible(true); |
|
|
|
|
|
ResumptionSupportingConnectionStore connectionStore = (InMemoryReadWriteLockConnectionStore) field.get(connector); |
|
|
|
|
|
InetSocketAddress serverAddr = ((IpPeer) lwM2mServer.getTransportData()).getSocketAddress(); |
|
|
|
|
|
return connectionStore.get(serverAddr); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|