From 3fa89f578052f31dab700d558ddd55a789d5ce1c Mon Sep 17 00:00:00 2001 From: Warmek Date: Fri, 24 Feb 2023 16:19:38 +0100 Subject: [PATCH 01/14] Added class to encapsulate CipherSuite --- .../server/bootstrap/BootstrapConfig.java | 64 +++++++++++++++---- .../server/bootstrap/BootstrapUtil.java | 3 +- 2 files changed, 55 insertions(+), 12 deletions(-) diff --git a/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapConfig.java b/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapConfig.java index 1297373432..859e6fd951 100644 --- a/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapConfig.java +++ b/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapConfig.java @@ -96,12 +96,24 @@ public class BootstrapConfig { */ public Map oscore = new HashMap<>(); - /** Server Configuration (object 1) as defined in LWM2M 1.0.x TS. */ + @Override + public String toString() { + return String.format("BootstrapConfig [servers=%s, security=%s, acls=%s, oscore=%s]", servers, security, acls, + oscore); + } + + /** + * Server Configuration (object 1) as defined in LWM2M 1.0.x TS. + */ public static class ServerConfig { - /** Used as link to associate server Object Instance. */ + /** + * Used as link to associate server Object Instance. + */ public int shortId; - /** Specify the lifetime of the registration in seconds (see Section 5.3 Registration). */ + /** + * Specify the lifetime of the registration in seconds (see Section 5.3 Registration). + */ public int lifetime = 86400; /** * The default value the LwM2M Client should use for the Minimum Period of an Observation in the absence of this @@ -291,7 +303,6 @@ public static class ServerSecurity { /** * The Object ID of the OSCORE Object Instance that holds the OSCORE configuration to be used by the LWM2M * Client to the LWM2M Server associated with this Security object. - * */ public Integer oscoreSecurityMode; @@ -341,7 +352,7 @@ public static class ServerSecurity { *

* Since Security v1.1 */ - public ULong cipherSuite = null; + public CipherSuiteId cipherSuite = null; @Override public String toString() { @@ -362,9 +373,13 @@ public String toString() { */ public static class ACLConfig { - /** The Object ID of the Object Instance for which ACL are applied. */ + /** + * The Object ID of the Object Instance for which ACL are applied. + */ public int objectId; - /** The Object instance ID of the Object Instance for which ACL are applied. */ + /** + * The Object instance ID of the Object Instance for which ACL are applied. + */ public int objectInstanceId; /** @@ -465,9 +480,36 @@ public String toString() { } } - @Override - public String toString() { - return String.format("BootstrapConfig [servers=%s, security=%s, acls=%s, oscore=%s]", servers, security, acls, - oscore); + public class CipherSuiteId { + + private final byte firstByte; + private final byte secondByte; + + public CipherSuiteId(byte firstByte, byte secondByte) { + this.firstByte = firstByte; + this.secondByte = secondByte; + } + + /** + * The IANA TLS ciphersuite registry is maintained at + * https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml. As an example, the + * TLS_PSK_WITH_AES_128_CCM_8 ciphersuite is represented with the following string "0xC0,0xA8" + */ + + public CipherSuiteId(ULong valueFromSecurityObject) { + String binaryString = Long.toBinaryString(valueFromSecurityObject.longValue()); + this.firstByte = (byte) Integer.parseInt(binaryString.substring(0, 8), 2); + this.secondByte = (byte) Integer.parseInt(binaryString.substring(9, 17), 2); + + } + + /** + * As an example, the TLS_PSK_WITH_AES_128_CCM_8 ciphersuite is represented with the following string + * "0xC0,0xA8". To form an integer value the two values are concatenated. In this example, the value is 0xc0a8 + * or 49320. + */ + public ULong getValueForSecurityObject() { + return ULong.valueOf(Byte.toUnsignedInt(firstByte) * 256 + Byte.toUnsignedInt(secondByte)); + } } } diff --git a/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapUtil.java b/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapUtil.java index 8848ab65a1..7008370536 100644 --- a/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapUtil.java +++ b/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapUtil.java @@ -80,7 +80,8 @@ public static LwM2mObjectInstance toSecurityInstance(int instanceId, ServerSecur if (securityConfig.certificateUsage != null) resources.add(LwM2mSingleResource.newUnsignedIntegerResource(15, securityConfig.certificateUsage.code)); if (securityConfig.cipherSuite != null) - resources.add(LwM2mSingleResource.newUnsignedIntegerResource(16, securityConfig.cipherSuite)); + resources.add(LwM2mSingleResource.newUnsignedIntegerResource(16, + securityConfig.cipherSuite.getValueForSecurityObject())); if (securityConfig.oscoreSecurityMode != null) { resources.add(LwM2mSingleResource.newObjectLinkResource(17, new ObjectLink(21, securityConfig.oscoreSecurityMode))); From f5050fab6bd5c64012b3e0f40eff8e142a6b9526 Mon Sep 17 00:00:00 2001 From: Warmek Date: Fri, 24 Feb 2023 17:50:51 +0100 Subject: [PATCH 02/14] Added class to encapsulate CipherSuiteIds --- .../leshan/core/node/LwM2mSingleResource.java | 10 ++- .../server/bootstrap/BootstrapConfig.java | 75 +++++++++++++------ .../server/bootstrap/BootstrapUtil.java | 3 +- 3 files changed, 59 insertions(+), 29 deletions(-) diff --git a/leshan-core/src/main/java/org/eclipse/leshan/core/node/LwM2mSingleResource.java b/leshan-core/src/main/java/org/eclipse/leshan/core/node/LwM2mSingleResource.java index ee6dd93c31..6bffcc5a55 100644 --- a/leshan-core/src/main/java/org/eclipse/leshan/core/node/LwM2mSingleResource.java +++ b/leshan-core/src/main/java/org/eclipse/leshan/core/node/LwM2mSingleResource.java @@ -181,6 +181,10 @@ public static LwM2mSingleResource newUnsignedIntegerResource(int id, Long value) throw new LwM2mNodeException("Invalid value : positive value expected for UNSIGNED_INTEGER"); } + public static LwM2mSingleResource newULongArray(int id, ULong[] value) { + return new LwM2mSingleResource(id, value, Type.OPAQUE); + } + /** * {@inheritDoc} */ @@ -206,7 +210,7 @@ public Object getValue() { } /** - * @exception NoSuchElementException use {@link #getValue()} instead. + * @throws NoSuchElementException use {@link #getValue()} instead. */ @Override public Object getValue(int id) { @@ -214,7 +218,7 @@ public Object getValue(int id) { } /** - * @exception NoSuchElementException use {@link #getValue()} instead. + * @throws NoSuchElementException use {@link #getValue()} instead. */ @Override public LwM2mResourceInstance getInstance(int id) { @@ -222,7 +226,7 @@ public LwM2mResourceInstance getInstance(int id) { } /** - * @exception NoSuchElementException use {@link #getValue()} instead. + * @throws NoSuchElementException use {@link #getValue()} instead. */ @Override public Map getInstances() { diff --git a/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapConfig.java b/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapConfig.java index 859e6fd951..5df3f14898 100644 --- a/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapConfig.java +++ b/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapConfig.java @@ -352,7 +352,7 @@ public static class ServerSecurity { *

* Since Security v1.1 */ - public CipherSuiteId cipherSuite = null; + public CipherSuiteIds cipherSuite = null; @Override public String toString() { @@ -480,36 +480,63 @@ public String toString() { } } - public class CipherSuiteId { + public class CipherSuiteIds { + private CipherSuiteId[] values; - private final byte firstByte; - private final byte secondByte; - - public CipherSuiteId(byte firstByte, byte secondByte) { - this.firstByte = firstByte; - this.secondByte = secondByte; + public CipherSuiteIds(byte[] firstBytes, byte[] secondBytes) { + values = new CipherSuiteId[firstBytes.length]; + for (int i = 0; i < firstBytes.length; i++) { + values[i] = new CipherSuiteId(firstBytes[i], secondBytes[i]); + } } - /** - * The IANA TLS ciphersuite registry is maintained at - * https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml. As an example, the - * TLS_PSK_WITH_AES_128_CCM_8 ciphersuite is represented with the following string "0xC0,0xA8" - */ + public CipherSuiteIds(ULong[] valuesFromSecurityObject) { + values = new CipherSuiteId[valuesFromSecurityObject.length]; + for (int i = 0; i < valuesFromSecurityObject.length; i++) { + values[i] = new CipherSuiteId(valuesFromSecurityObject[i]); + } + } - public CipherSuiteId(ULong valueFromSecurityObject) { - String binaryString = Long.toBinaryString(valueFromSecurityObject.longValue()); - this.firstByte = (byte) Integer.parseInt(binaryString.substring(0, 8), 2); - this.secondByte = (byte) Integer.parseInt(binaryString.substring(9, 17), 2); + public ULong[] getULongArray() { + ULong[] out = new ULong[values.length]; + for (int i = 0; i < values.length; i++) { + out[i] = values[i].getValueForSecurityObject(); + } + return out; } - /** - * As an example, the TLS_PSK_WITH_AES_128_CCM_8 ciphersuite is represented with the following string - * "0xC0,0xA8". To form an integer value the two values are concatenated. In this example, the value is 0xc0a8 - * or 49320. - */ - public ULong getValueForSecurityObject() { - return ULong.valueOf(Byte.toUnsignedInt(firstByte) * 256 + Byte.toUnsignedInt(secondByte)); + private class CipherSuiteId { + + private final byte firstByte; + private final byte secondByte; + + public CipherSuiteId(byte firstByte, byte secondByte) { + this.firstByte = firstByte; + this.secondByte = secondByte; + } + + /** + * The IANA TLS ciphersuite registry is maintained at + * https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml. As an example, the + * TLS_PSK_WITH_AES_128_CCM_8 ciphersuite is represented with the following string "0xC0,0xA8" + */ + + public CipherSuiteId(ULong valueFromSecurityObject) { + String binaryString = Long.toBinaryString(valueFromSecurityObject.longValue()); + this.firstByte = (byte) Integer.parseInt(binaryString.substring(0, 8), 2); + this.secondByte = (byte) Integer.parseInt(binaryString.substring(9, 17), 2); + + } + + /** + * As an example, the TLS_PSK_WITH_AES_128_CCM_8 ciphersuite is represented with the following string + * "0xC0,0xA8". To form an integer value the two values are concatenated. In this example, the value is + * 0xc0a8 or 49320. + */ + public ULong getValueForSecurityObject() { + return ULong.valueOf(Byte.toUnsignedInt(firstByte) * 256 + Byte.toUnsignedInt(secondByte)); + } } } } diff --git a/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapUtil.java b/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapUtil.java index 7008370536..1928e5c8fe 100644 --- a/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapUtil.java +++ b/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapUtil.java @@ -80,8 +80,7 @@ public static LwM2mObjectInstance toSecurityInstance(int instanceId, ServerSecur if (securityConfig.certificateUsage != null) resources.add(LwM2mSingleResource.newUnsignedIntegerResource(15, securityConfig.certificateUsage.code)); if (securityConfig.cipherSuite != null) - resources.add(LwM2mSingleResource.newUnsignedIntegerResource(16, - securityConfig.cipherSuite.getValueForSecurityObject())); + resources.add(LwM2mSingleResource.newULongArray(16, securityConfig.cipherSuite.getULongArray())); if (securityConfig.oscoreSecurityMode != null) { resources.add(LwM2mSingleResource.newObjectLinkResource(17, new ObjectLink(21, securityConfig.oscoreSecurityMode))); From d6da61a5c3d51c4c9a7db76c7d8502c562457d70 Mon Sep 17 00:00:00 2001 From: Warmek Date: Fri, 24 Feb 2023 19:32:30 +0100 Subject: [PATCH 03/14] Moved from CipherSuiteIds to List --- .../server/bootstrap/BootstrapConfig.java | 75 ++++++------------- .../server/bootstrap/BootstrapUtil.java | 10 ++- 2 files changed, 32 insertions(+), 53 deletions(-) diff --git a/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapConfig.java b/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapConfig.java index 5df3f14898..b0c8b72a1a 100644 --- a/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapConfig.java +++ b/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapConfig.java @@ -352,7 +352,7 @@ public static class ServerSecurity { *

* Since Security v1.1 */ - public CipherSuiteIds cipherSuite = null; + public List cipherSuite = null; @Override public String toString() { @@ -480,63 +480,36 @@ public String toString() { } } - public class CipherSuiteIds { - private CipherSuiteId[] values; + public class CipherSuiteId { - public CipherSuiteIds(byte[] firstBytes, byte[] secondBytes) { - values = new CipherSuiteId[firstBytes.length]; - for (int i = 0; i < firstBytes.length; i++) { - values[i] = new CipherSuiteId(firstBytes[i], secondBytes[i]); - } - } + private final byte firstByte; + private final byte secondByte; - public CipherSuiteIds(ULong[] valuesFromSecurityObject) { - values = new CipherSuiteId[valuesFromSecurityObject.length]; - for (int i = 0; i < valuesFromSecurityObject.length; i++) { - values[i] = new CipherSuiteId(valuesFromSecurityObject[i]); - } + public CipherSuiteId(byte firstByte, byte secondByte) { + this.firstByte = firstByte; + this.secondByte = secondByte; } - public ULong[] getULongArray() { - ULong[] out = new ULong[values.length]; - for (int i = 0; i < values.length; i++) { - out[i] = values[i].getValueForSecurityObject(); - } + /** + * The IANA TLS ciphersuite registry is maintained at + * https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml. As an example, the + * TLS_PSK_WITH_AES_128_CCM_8 ciphersuite is represented with the following string "0xC0,0xA8" + */ + + public CipherSuiteId(ULong valueFromSecurityObject) { + String binaryString = Long.toBinaryString(valueFromSecurityObject.longValue()); + this.firstByte = (byte) Integer.parseInt(binaryString.substring(0, 8), 2); + this.secondByte = (byte) Integer.parseInt(binaryString.substring(9, 17), 2); - return out; } - private class CipherSuiteId { - - private final byte firstByte; - private final byte secondByte; - - public CipherSuiteId(byte firstByte, byte secondByte) { - this.firstByte = firstByte; - this.secondByte = secondByte; - } - - /** - * The IANA TLS ciphersuite registry is maintained at - * https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml. As an example, the - * TLS_PSK_WITH_AES_128_CCM_8 ciphersuite is represented with the following string "0xC0,0xA8" - */ - - public CipherSuiteId(ULong valueFromSecurityObject) { - String binaryString = Long.toBinaryString(valueFromSecurityObject.longValue()); - this.firstByte = (byte) Integer.parseInt(binaryString.substring(0, 8), 2); - this.secondByte = (byte) Integer.parseInt(binaryString.substring(9, 17), 2); - - } - - /** - * As an example, the TLS_PSK_WITH_AES_128_CCM_8 ciphersuite is represented with the following string - * "0xC0,0xA8". To form an integer value the two values are concatenated. In this example, the value is - * 0xc0a8 or 49320. - */ - public ULong getValueForSecurityObject() { - return ULong.valueOf(Byte.toUnsignedInt(firstByte) * 256 + Byte.toUnsignedInt(secondByte)); - } + /** + * As an example, the TLS_PSK_WITH_AES_128_CCM_8 ciphersuite is represented with the following string + * "0xC0,0xA8". To form an integer value the two values are concatenated. In this example, the value is 0xc0a8 + * or 49320. + */ + public ULong getValueForSecurityObject() { + return ULong.valueOf(Byte.toUnsignedInt(firstByte) * 256 + Byte.toUnsignedInt(secondByte)); } } } diff --git a/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapUtil.java b/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapUtil.java index 1928e5c8fe..1e82a6a02d 100644 --- a/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapUtil.java +++ b/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapUtil.java @@ -36,6 +36,7 @@ import org.eclipse.leshan.core.request.BootstrapWriteRequest; import org.eclipse.leshan.core.request.ContentFormat; import org.eclipse.leshan.core.response.LwM2mResponse; +import org.eclipse.leshan.core.util.datatype.ULong; import org.eclipse.leshan.server.bootstrap.BootstrapConfig.ACLConfig; import org.eclipse.leshan.server.bootstrap.BootstrapConfig.OscoreObject; import org.eclipse.leshan.server.bootstrap.BootstrapConfig.ServerConfig; @@ -79,8 +80,13 @@ public static LwM2mObjectInstance toSecurityInstance(int instanceId, ServerSecur resources.add(LwM2mSingleResource.newStringResource(14, securityConfig.sni)); if (securityConfig.certificateUsage != null) resources.add(LwM2mSingleResource.newUnsignedIntegerResource(15, securityConfig.certificateUsage.code)); - if (securityConfig.cipherSuite != null) - resources.add(LwM2mSingleResource.newULongArray(16, securityConfig.cipherSuite.getULongArray())); + if (securityConfig.cipherSuite != null) { + List ciperSuiteULong = new ArrayList<>(); + for (BootstrapConfig.CipherSuiteId cipherSuiteId : securityConfig.cipherSuite) { + ciperSuiteULong.add(cipherSuiteId.getValueForSecurityObject()); + } + resources.add(LwM2mSingleResource.newULongArray(16, (ULong[]) ciperSuiteULong.toArray())); + } if (securityConfig.oscoreSecurityMode != null) { resources.add(LwM2mSingleResource.newObjectLinkResource(17, new ObjectLink(21, securityConfig.oscoreSecurityMode))); From e030e0bcdb70fb657162d640db224af97583d216 Mon Sep 17 00:00:00 2001 From: Warmek Date: Mon, 27 Feb 2023 14:33:25 +0100 Subject: [PATCH 04/14] Moved from CipherSuiteIds to MultipleResource UNSIGNED_INTEGER --- .../eclipse/leshan/server/bootstrap/BootstrapUtil.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapUtil.java b/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapUtil.java index 1e82a6a02d..ce8b7354af 100644 --- a/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapUtil.java +++ b/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapUtil.java @@ -18,7 +18,9 @@ import java.util.ArrayList; import java.util.Collection; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.Map.Entry; import java.util.TreeMap; @@ -81,11 +83,12 @@ public static LwM2mObjectInstance toSecurityInstance(int instanceId, ServerSecur if (securityConfig.certificateUsage != null) resources.add(LwM2mSingleResource.newUnsignedIntegerResource(15, securityConfig.certificateUsage.code)); if (securityConfig.cipherSuite != null) { - List ciperSuiteULong = new ArrayList<>(); + Map ciperSuiteULong = new HashMap<>(); + int i = 0; for (BootstrapConfig.CipherSuiteId cipherSuiteId : securityConfig.cipherSuite) { - ciperSuiteULong.add(cipherSuiteId.getValueForSecurityObject()); + ciperSuiteULong.put(i++, cipherSuiteId.getValueForSecurityObject()); } - resources.add(LwM2mSingleResource.newULongArray(16, (ULong[]) ciperSuiteULong.toArray())); + resources.add(LwM2mMultipleResource.newUnsignedIntegerResource(16, ciperSuiteULong)); } if (securityConfig.oscoreSecurityMode != null) { resources.add(LwM2mSingleResource.newObjectLinkResource(17, From d0861f804693083e9503733b7366aa379569cd81 Mon Sep 17 00:00:00 2001 From: Warmek Date: Mon, 27 Feb 2023 17:58:36 +0100 Subject: [PATCH 05/14] Fixed commands for pull request --- .../leshan/core/node/LwM2mSingleResource.java | 36 +++++-------------- .../server/bootstrap/BootstrapConfig.java | 29 +++++---------- 2 files changed, 17 insertions(+), 48 deletions(-) diff --git a/leshan-core/src/main/java/org/eclipse/leshan/core/node/LwM2mSingleResource.java b/leshan-core/src/main/java/org/eclipse/leshan/core/node/LwM2mSingleResource.java index 6bffcc5a55..5120ed7f7f 100644 --- a/leshan-core/src/main/java/org/eclipse/leshan/core/node/LwM2mSingleResource.java +++ b/leshan-core/src/main/java/org/eclipse/leshan/core/node/LwM2mSingleResource.java @@ -25,9 +25,7 @@ import org.eclipse.leshan.core.util.datatype.LwM2mValueUtil; import org.eclipse.leshan.core.util.datatype.ULong; -/** - * A resource with a single value. - */ +/** A resource with a single value. */ public class LwM2mSingleResource implements LwM2mResource { private final int id; @@ -181,61 +179,43 @@ public static LwM2mSingleResource newUnsignedIntegerResource(int id, Long value) throw new LwM2mNodeException("Invalid value : positive value expected for UNSIGNED_INTEGER"); } - public static LwM2mSingleResource newULongArray(int id, ULong[] value) { - return new LwM2mSingleResource(id, value, Type.OPAQUE); - } - - /** - * {@inheritDoc} - */ + /** {@inheritDoc} */ @Override public int getId() { return id; } - /** - * {@inheritDoc} - */ + /** {@inheritDoc} */ @Override public Type getType() { return type; } - /** - * {@inheritDoc} - */ + /** {@inheritDoc} */ @Override public Object getValue() { return value; } - /** - * @throws NoSuchElementException use {@link #getValue()} instead. - */ + /** @exception NoSuchElementException use {@link #getValue()} instead. */ @Override public Object getValue(int id) { throw new NoSuchElementException("There is no 'values' on single resources, use getValue() instead."); } - /** - * @throws NoSuchElementException use {@link #getValue()} instead. - */ + /** @exception NoSuchElementException use {@link #getValue()} instead. */ @Override public LwM2mResourceInstance getInstance(int id) { throw new NoSuchElementException("There is no 'instance' on single resources, use getValue() instead."); } - /** - * @throws NoSuchElementException use {@link #getValue()} instead. - */ + /** @exception NoSuchElementException use {@link #getValue()} instead. */ @Override public Map getInstances() { throw new NoSuchElementException("There is no 'instances' on single resources, use getValue() instead."); } - /** - * {@inheritDoc} - */ + /** * {@inheritDoc} */ @Override public boolean isMultiInstances() { return false; diff --git a/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapConfig.java b/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapConfig.java index b0c8b72a1a..c5036ff512 100644 --- a/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapConfig.java +++ b/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapConfig.java @@ -102,18 +102,12 @@ public String toString() { oscore); } - /** - * Server Configuration (object 1) as defined in LWM2M 1.0.x TS. - */ + /** Server Configuration (object 1) as defined in LWM2M 1.0.x TS. */ public static class ServerConfig { - /** - * Used as link to associate server Object Instance. - */ + /** Used as link to associate server Object Instance. */ public int shortId; - /** - * Specify the lifetime of the registration in seconds (see Section 5.3 Registration). - */ + /** Specify the lifetime of the registration in seconds (see Section 5.3 Registration). */ public int lifetime = 86400; /** * The default value the LwM2M Client should use for the Minimum Period of an Observation in the absence of this @@ -373,13 +367,9 @@ public String toString() { */ public static class ACLConfig { - /** - * The Object ID of the Object Instance for which ACL are applied. - */ + /** The Object ID of the Object Instance for which ACL are applied. */ public int objectId; - /** - * The Object instance ID of the Object Instance for which ACL are applied. - */ + /** The Object instance ID of the Object Instance for which ACL are applied. */ public int objectInstanceId; /** @@ -485,16 +475,15 @@ public class CipherSuiteId { private final byte firstByte; private final byte secondByte; - public CipherSuiteId(byte firstByte, byte secondByte) { - this.firstByte = firstByte; - this.secondByte = secondByte; - } - /** * The IANA TLS ciphersuite registry is maintained at * https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml. As an example, the * TLS_PSK_WITH_AES_128_CCM_8 ciphersuite is represented with the following string "0xC0,0xA8" */ + public CipherSuiteId(byte firstByte, byte secondByte) { + this.firstByte = firstByte; + this.secondByte = secondByte; + } public CipherSuiteId(ULong valueFromSecurityObject) { String binaryString = Long.toBinaryString(valueFromSecurityObject.longValue()); From 12c96fce9cf2ba241d5b667d25dd84ad50c9aebb Mon Sep 17 00:00:00 2001 From: Bartosz Stolarczyk Date: Mon, 27 Feb 2023 19:31:30 +0100 Subject: [PATCH 06/14] Reverted unintended changes to LwM2mSingleResource.java and BootstrapConfig.java --- .../leshan/core/node/LwM2mSingleResource.java | 32 ++++++++++++++----- .../server/bootstrap/BootstrapConfig.java | 12 +++---- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/leshan-core/src/main/java/org/eclipse/leshan/core/node/LwM2mSingleResource.java b/leshan-core/src/main/java/org/eclipse/leshan/core/node/LwM2mSingleResource.java index 5120ed7f7f..ee6dd93c31 100644 --- a/leshan-core/src/main/java/org/eclipse/leshan/core/node/LwM2mSingleResource.java +++ b/leshan-core/src/main/java/org/eclipse/leshan/core/node/LwM2mSingleResource.java @@ -25,7 +25,9 @@ import org.eclipse.leshan.core.util.datatype.LwM2mValueUtil; import org.eclipse.leshan.core.util.datatype.ULong; -/** A resource with a single value. */ +/** + * A resource with a single value. + */ public class LwM2mSingleResource implements LwM2mResource { private final int id; @@ -179,43 +181,57 @@ public static LwM2mSingleResource newUnsignedIntegerResource(int id, Long value) throw new LwM2mNodeException("Invalid value : positive value expected for UNSIGNED_INTEGER"); } - /** {@inheritDoc} */ + /** + * {@inheritDoc} + */ @Override public int getId() { return id; } - /** {@inheritDoc} */ + /** + * {@inheritDoc} + */ @Override public Type getType() { return type; } - /** {@inheritDoc} */ + /** + * {@inheritDoc} + */ @Override public Object getValue() { return value; } - /** @exception NoSuchElementException use {@link #getValue()} instead. */ + /** + * @exception NoSuchElementException use {@link #getValue()} instead. + */ @Override public Object getValue(int id) { throw new NoSuchElementException("There is no 'values' on single resources, use getValue() instead."); } - /** @exception NoSuchElementException use {@link #getValue()} instead. */ + /** + * @exception NoSuchElementException use {@link #getValue()} instead. + */ @Override public LwM2mResourceInstance getInstance(int id) { throw new NoSuchElementException("There is no 'instance' on single resources, use getValue() instead."); } - /** @exception NoSuchElementException use {@link #getValue()} instead. */ + /** + * @exception NoSuchElementException use {@link #getValue()} instead. + */ @Override public Map getInstances() { throw new NoSuchElementException("There is no 'instances' on single resources, use getValue() instead."); } - /** * {@inheritDoc} */ + /** + * {@inheritDoc} + */ @Override public boolean isMultiInstances() { return false; diff --git a/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapConfig.java b/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapConfig.java index c5036ff512..40cd0b95be 100644 --- a/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapConfig.java +++ b/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapConfig.java @@ -96,12 +96,6 @@ public class BootstrapConfig { */ public Map oscore = new HashMap<>(); - @Override - public String toString() { - return String.format("BootstrapConfig [servers=%s, security=%s, acls=%s, oscore=%s]", servers, security, acls, - oscore); - } - /** Server Configuration (object 1) as defined in LWM2M 1.0.x TS. */ public static class ServerConfig { @@ -501,4 +495,10 @@ public ULong getValueForSecurityObject() { return ULong.valueOf(Byte.toUnsignedInt(firstByte) * 256 + Byte.toUnsignedInt(secondByte)); } } + + @Override + public String toString() { + return String.format("BootstrapConfig [servers=%s, security=%s, acls=%s, oscore=%s]", servers, security, acls, + oscore); + } } From 6f4aeb1a4f02b3b4a772d19438f7046c5114a2b7 Mon Sep 17 00:00:00 2001 From: Bartosz Stolarczyk Date: Tue, 28 Feb 2023 11:56:24 +0100 Subject: [PATCH 07/14] Created javadoc for CipherSuiteId class --- .../server/bootstrap/BootstrapConfig.java | 40 +++++++++++++------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapConfig.java b/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapConfig.java index 40cd0b95be..91f2cf78a0 100644 --- a/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapConfig.java +++ b/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapConfig.java @@ -96,12 +96,18 @@ public class BootstrapConfig { */ public Map oscore = new HashMap<>(); - /** Server Configuration (object 1) as defined in LWM2M 1.0.x TS. */ + /** + * Server Configuration (object 1) as defined in LWM2M 1.0.x TS. + */ public static class ServerConfig { - /** Used as link to associate server Object Instance. */ + /** + * Used as link to associate server Object Instance. + */ public int shortId; - /** Specify the lifetime of the registration in seconds (see Section 5.3 Registration). */ + /** + * Specify the lifetime of the registration in seconds (see Section 5.3 Registration). + */ public int lifetime = 86400; /** * The default value the LwM2M Client should use for the Minimum Period of an Observation in the absence of this @@ -361,9 +367,13 @@ public String toString() { */ public static class ACLConfig { - /** The Object ID of the Object Instance for which ACL are applied. */ + /** + * The Object ID of the Object Instance for which ACL are applied. + */ public int objectId; - /** The Object instance ID of the Object Instance for which ACL are applied. */ + /** + * The Object instance ID of the Object Instance for which ACL are applied. + */ public int objectInstanceId; /** @@ -470,26 +480,32 @@ public class CipherSuiteId { private final byte secondByte; /** - * The IANA TLS ciphersuite registry is maintained at - * https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml. As an example, the - * TLS_PSK_WITH_AES_128_CCM_8 ciphersuite is represented with the following string "0xC0,0xA8" + * Ciphersuite is created with 2 bytes. + * + * @param firstByte first byte of ciphersuite (for example 0xC0) + * @param secondByte second byte of ciphersuite (for example 0xA8) */ public CipherSuiteId(byte firstByte, byte secondByte) { this.firstByte = firstByte; this.secondByte = secondByte; } + /** + * Integer is split into 2 bytes for example 49320 (0xc0a8 in hex) will be split into "0xC0,0xA8". + * + * @param valueFromSecurityObject Integer representing ciphersuite id + */ public CipherSuiteId(ULong valueFromSecurityObject) { String binaryString = Long.toBinaryString(valueFromSecurityObject.longValue()); this.firstByte = (byte) Integer.parseInt(binaryString.substring(0, 8), 2); this.secondByte = (byte) Integer.parseInt(binaryString.substring(9, 17), 2); - } /** - * As an example, the TLS_PSK_WITH_AES_128_CCM_8 ciphersuite is represented with the following string - * "0xC0,0xA8". To form an integer value the two values are concatenated. In this example, the value is 0xc0a8 - * or 49320. + * Two bytes of ciphersuite id are concatenated into integer value. As an example bytes "0xC0,0xA8" will be + * concatenated into 0xc0a8 which in decimal notation is 49320. + * + * @return Integer number concatenated from 2 bytes. */ public ULong getValueForSecurityObject() { return ULong.valueOf(Byte.toUnsignedInt(firstByte) * 256 + Byte.toUnsignedInt(secondByte)); From db597850ee3298c7b8f476fed671d3144b9f930f Mon Sep 17 00:00:00 2001 From: Bartosz Stolarczyk Date: Tue, 28 Feb 2023 16:11:06 +0100 Subject: [PATCH 08/14] Added toString and fixed javadoc for CipherSuiteId --- .../leshan/server/bootstrap/BootstrapConfig.java | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapConfig.java b/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapConfig.java index 91f2cf78a0..162499e2ae 100644 --- a/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapConfig.java +++ b/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapConfig.java @@ -480,7 +480,9 @@ public class CipherSuiteId { private final byte secondByte; /** - * Ciphersuite is created with 2 bytes. + * Ciphersuite is created with 2 bytes. Possible values are described + * in the + * registry. * * @param firstByte first byte of ciphersuite (for example 0xC0) * @param secondByte second byte of ciphersuite (for example 0xA8) @@ -492,6 +494,7 @@ public CipherSuiteId(byte firstByte, byte secondByte) { /** * Integer is split into 2 bytes for example 49320 (0xc0a8 in hex) will be split into "0xC0,0xA8". + * This format is used by Security Object, resource 16. * * @param valueFromSecurityObject Integer representing ciphersuite id */ @@ -510,6 +513,15 @@ public CipherSuiteId(ULong valueFromSecurityObject) { public ULong getValueForSecurityObject() { return ULong.valueOf(Byte.toUnsignedInt(firstByte) * 256 + Byte.toUnsignedInt(secondByte)); } + + + /** + * @return String representing hex value of concatenated two bytes. + */ + @Override + public String toString() { + return Integer.toHexString(getValueForSecurityObject().intValue()); + } } @Override From 52df0987cb8ff2f2fd89c9cfd5a636d425bcf4fd Mon Sep 17 00:00:00 2001 From: Bartosz Stolarczyk Date: Tue, 28 Feb 2023 16:28:25 +0100 Subject: [PATCH 09/14] Fixed one line comments --- .../server/bootstrap/BootstrapConfig.java | 36 +++++-------------- 1 file changed, 9 insertions(+), 27 deletions(-) diff --git a/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapConfig.java b/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapConfig.java index 162499e2ae..e1fb637532 100644 --- a/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapConfig.java +++ b/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapConfig.java @@ -71,43 +71,29 @@ public class BootstrapConfig { */ public ContentFormat contentFormat = null; - /** - * List of LWM2M path to delete. - */ + /** List of LWM2M path to delete. */ public List toDelete = new ArrayList<>(); - /** - * Map indexed by Server Instance Id. Key is the Server Instance to write. - */ + /** Map indexed by Server Instance Id. Key is the Server Instance to write. */ public Map servers = new HashMap<>(); - /** - * Map indexed by Security Instance Id. Key is the Server Instance to write. - */ + /** Map indexed by Security Instance Id. Key is the Server Instance to write. */ public Map security = new HashMap<>(); - /** - * Map indexed by ACL Instance Id. Key is the ACL Instance to write. - */ + /** Map indexed by ACL Instance Id. Key is the ACL Instance to write. */ public Map acls = new HashMap<>(); - /** - * Map indexed by OSCORE Object Instance Id. Key is the OSCORE Object Instance to write. - */ + /** Map indexed by OSCORE Object Instance Id. Key is the OSCORE Object Instance to write. */ public Map oscore = new HashMap<>(); - /** - * Server Configuration (object 1) as defined in LWM2M 1.0.x TS. - */ + /** Server Configuration (object 1) as defined in LWM2M 1.0.x TS. */ public static class ServerConfig { /** * Used as link to associate server Object Instance. */ public int shortId; - /** - * Specify the lifetime of the registration in seconds (see Section 5.3 Registration). - */ + /** Specify the lifetime of the registration in seconds (see Section 5.3 Registration). */ public int lifetime = 86400; /** * The default value the LwM2M Client should use for the Minimum Period of an Observation in the absence of this @@ -367,13 +353,9 @@ public String toString() { */ public static class ACLConfig { - /** - * The Object ID of the Object Instance for which ACL are applied. - */ + /** The Object ID of the Object Instance for which ACL are applied. */ public int objectId; - /** - * The Object instance ID of the Object Instance for which ACL are applied. - */ + /** The Object instance ID of the Object Instance for which ACL are applied. */ public int objectInstanceId; /** From 4f936eb23bd10eea4a83fe1cc2bc9311be5fa1d9 Mon Sep 17 00:00:00 2001 From: Bartosz Stolarczyk Date: Tue, 28 Feb 2023 18:15:49 +0100 Subject: [PATCH 10/14] Changed handling of bytes in CipherSuiteId to use bitwise operators --- .../eclipse/leshan/server/bootstrap/BootstrapConfig.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapConfig.java b/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapConfig.java index e1fb637532..a3f0807f83 100644 --- a/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapConfig.java +++ b/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapConfig.java @@ -481,9 +481,8 @@ public CipherSuiteId(byte firstByte, byte secondByte) { * @param valueFromSecurityObject Integer representing ciphersuite id */ public CipherSuiteId(ULong valueFromSecurityObject) { - String binaryString = Long.toBinaryString(valueFromSecurityObject.longValue()); - this.firstByte = (byte) Integer.parseInt(binaryString.substring(0, 8), 2); - this.secondByte = (byte) Integer.parseInt(binaryString.substring(9, 17), 2); + this.firstByte = (byte) ((valueFromSecurityObject.intValue() >> 8) & 0xFF); + this.secondByte = (byte) ((valueFromSecurityObject.intValue()) & 0xFF); } /** @@ -493,7 +492,7 @@ public CipherSuiteId(ULong valueFromSecurityObject) { * @return Integer number concatenated from 2 bytes. */ public ULong getValueForSecurityObject() { - return ULong.valueOf(Byte.toUnsignedInt(firstByte) * 256 + Byte.toUnsignedInt(secondByte)); + return ULong.valueOf(((int) firstByte << 8) | secondByte); } @@ -502,7 +501,7 @@ public ULong getValueForSecurityObject() { */ @Override public String toString() { - return Integer.toHexString(getValueForSecurityObject().intValue()); + return String.format("%x,%x", firstByte, secondByte); } } From 6da0e16d91c41ab895c52c872c287ba53f3b6cb3 Mon Sep 17 00:00:00 2001 From: Bartosz Stolarczyk Date: Tue, 28 Feb 2023 18:56:13 +0100 Subject: [PATCH 11/14] Formatted BootstrapConfig.java --- .../leshan/server/bootstrap/BootstrapConfig.java | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapConfig.java b/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapConfig.java index a3f0807f83..4ce04f1513 100644 --- a/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapConfig.java +++ b/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapConfig.java @@ -462,9 +462,8 @@ public class CipherSuiteId { private final byte secondByte; /** - * Ciphersuite is created with 2 bytes. Possible values are described - * in the - * registry. + * Ciphersuite is created with 2 bytes. Possible values are described in the + * registry. * * @param firstByte first byte of ciphersuite (for example 0xC0) * @param secondByte second byte of ciphersuite (for example 0xA8) @@ -475,8 +474,8 @@ public CipherSuiteId(byte firstByte, byte secondByte) { } /** - * Integer is split into 2 bytes for example 49320 (0xc0a8 in hex) will be split into "0xC0,0xA8". - * This format is used by Security Object, resource 16. + * Integer is split into 2 bytes for example 49320 (0xc0a8 in hex) will be split into "0xC0,0xA8". This format + * is used by Security Object, resource 16. * * @param valueFromSecurityObject Integer representing ciphersuite id */ @@ -495,7 +494,6 @@ public ULong getValueForSecurityObject() { return ULong.valueOf(((int) firstByte << 8) | secondByte); } - /** * @return String representing hex value of concatenated two bytes. */ From 124903946323474531764417367a882e1303d287 Mon Sep 17 00:00:00 2001 From: Bartosz Stolarczyk Date: Tue, 28 Feb 2023 20:42:12 +0100 Subject: [PATCH 12/14] Reformatted BootstrapConfig.java --- .../org/eclipse/leshan/server/bootstrap/BootstrapConfig.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapConfig.java b/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapConfig.java index 4ce04f1513..fb80f9cc2a 100644 --- a/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapConfig.java +++ b/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapConfig.java @@ -465,7 +465,7 @@ public class CipherSuiteId { * Ciphersuite is created with 2 bytes. Possible values are described in the * registry. * - * @param firstByte first byte of ciphersuite (for example 0xC0) + * @param firstByte first byte of ciphersuite (for example 0xC0) * @param secondByte second byte of ciphersuite (for example 0xA8) */ public CipherSuiteId(byte firstByte, byte secondByte) { From edb1744eac6cb69d20db8fdd45f2fa3475830801 Mon Sep 17 00:00:00 2001 From: Bartosz Stolarczyk Date: Wed, 1 Mar 2023 10:24:38 +0100 Subject: [PATCH 13/14] Reformatted BootstrapConfig.java --- .../org/eclipse/leshan/server/bootstrap/BootstrapConfig.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapConfig.java b/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapConfig.java index fb80f9cc2a..4ce04f1513 100644 --- a/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapConfig.java +++ b/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapConfig.java @@ -465,7 +465,7 @@ public class CipherSuiteId { * Ciphersuite is created with 2 bytes. Possible values are described in the * registry. * - * @param firstByte first byte of ciphersuite (for example 0xC0) + * @param firstByte first byte of ciphersuite (for example 0xC0) * @param secondByte second byte of ciphersuite (for example 0xA8) */ public CipherSuiteId(byte firstByte, byte secondByte) { From ca39a0e56c88ddc1ee4df789ec4d8bf0f8d6d9cb Mon Sep 17 00:00:00 2001 From: Bartosz Stolarczyk Date: Thu, 2 Mar 2023 15:42:56 +0100 Subject: [PATCH 14/14] Created tests for CipherSuiteId --- .../server/bootstrap/BootstrapConfig.java | 6 +- .../server/bootstrap/BootstrapConfigTest.java | 76 +++++++++++++++++++ 2 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 leshan-server-core/src/test/java/org/eclipse/leshan/server/bootstrap/BootstrapConfigTest.java diff --git a/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapConfig.java b/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapConfig.java index 4ce04f1513..3323e5f8b5 100644 --- a/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapConfig.java +++ b/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/BootstrapConfig.java @@ -456,7 +456,7 @@ public String toString() { } } - public class CipherSuiteId { + public static class CipherSuiteId { private final byte firstByte; private final byte secondByte; @@ -480,6 +480,8 @@ public CipherSuiteId(byte firstByte, byte secondByte) { * @param valueFromSecurityObject Integer representing ciphersuite id */ public CipherSuiteId(ULong valueFromSecurityObject) { + if (valueFromSecurityObject.intValue() < 0 || valueFromSecurityObject.intValue() > 65535) + throw new IllegalArgumentException("ULong value have to be between <0, 65535>"); this.firstByte = (byte) ((valueFromSecurityObject.intValue() >> 8) & 0xFF); this.secondByte = (byte) ((valueFromSecurityObject.intValue()) & 0xFF); } @@ -491,7 +493,7 @@ public CipherSuiteId(ULong valueFromSecurityObject) { * @return Integer number concatenated from 2 bytes. */ public ULong getValueForSecurityObject() { - return ULong.valueOf(((int) firstByte << 8) | secondByte); + return ULong.valueOf((Byte.toUnsignedInt(firstByte) << 8) | Byte.toUnsignedInt(secondByte)); } /** diff --git a/leshan-server-core/src/test/java/org/eclipse/leshan/server/bootstrap/BootstrapConfigTest.java b/leshan-server-core/src/test/java/org/eclipse/leshan/server/bootstrap/BootstrapConfigTest.java new file mode 100644 index 0000000000..1556ad814f --- /dev/null +++ b/leshan-server-core/src/test/java/org/eclipse/leshan/server/bootstrap/BootstrapConfigTest.java @@ -0,0 +1,76 @@ +/******************************************************************************* + * Copyright (c) 2016 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: + * Bartosz Stolarczyk + * Orange Polska S.A. - initial API and implementation + *******************************************************************************/ +package org.eclipse.leshan.server.bootstrap; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.eclipse.leshan.core.util.Hex; +import org.eclipse.leshan.core.util.datatype.ULong; +import org.junit.jupiter.api.Test; + +class BootstrapConfigTest { + + @Test + public void CipherSuiteId_encode_from_two_bytes() { + // Create 2 bytes + byte[] decoded = Hex.decodeHex("C0A8".toCharArray()); + Byte firstByte = decoded[0]; + Byte secoundByte = decoded[1]; + + // Create CipherSuiteId with two bytes + BootstrapConfig.CipherSuiteId cipherSuiteId = new BootstrapConfig.CipherSuiteId(firstByte, secoundByte); + + // Assert if bytes were correctly phrased + assertEquals("c0,a8", cipherSuiteId.toString()); + } + + @Test + public void CipherSuiteId_encode_from_ULong() { + // Create CipherSuiteId with ULong + BootstrapConfig.CipherSuiteId cipherSuiteId = new BootstrapConfig.CipherSuiteId(ULong.valueOf(49320)); + + // Assert if ULong was correctly phrased + assertEquals("c0,a8", cipherSuiteId.toString()); + } + + @Test + public void getValueForSecurityObject() { + // Create example ULong + ULong testValue = ULong.valueOf(49320); + + // Create cipherSuiteId from ULong + BootstrapConfig.CipherSuiteId cipherSuiteId = new BootstrapConfig.CipherSuiteId(testValue); + + // Check if getValueForSecurityObject() returns input ULong + assertEquals(testValue, cipherSuiteId.getValueForSecurityObject()); + } + + @Test + public void is_error_thrown_for_too_big_values() { + // Create ULong with value bigger than 65535 + ULong testValue = ULong.valueOf(65536); + + // Try to create CipherSuiteId with ULong outside of range + try { + new BootstrapConfig.CipherSuiteId(testValue); + assert (false); + } catch (IllegalArgumentException e) { + // If IllegalArgumentException is caught pass the test + assert (true); + } + } +}