Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added OPAQUE text encoding, using base64 #676

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ bin/
.idea/
*.iml

# Visual Studio Code files #
.vscode/

# Package Files #
*.jar
*.war
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.eclipse.leshan.core.node.LwM2mSingleResource;
import org.eclipse.leshan.core.node.ObjectLink;
import org.eclipse.leshan.core.node.codec.CodecException;
import org.eclipse.leshan.util.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -96,7 +97,10 @@ private static Object parseTextValue(String value, Type type, LwM2mPath path) th
path);
}
case OPAQUE:
// not specified
if (!Base64.isBase64(value)) {
throw new CodecException("Invalid value for opaque resource [%s], base64 expected", path);
}
return Base64.decodeBase64(value);
default:
throw new CodecException("Could not handle %s value with TEXT encoder for resource %s", type, path);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.eclipse.leshan.core.node.ObjectLink;
import org.eclipse.leshan.core.node.codec.CodecException;
import org.eclipse.leshan.core.node.codec.LwM2mValueConverter;
import org.eclipse.leshan.util.Base64;
import org.eclipse.leshan.util.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -100,6 +101,10 @@ public void visit(LwM2mResource resource) {
ObjectLink objlnk = (ObjectLink) val;
strValue = String.valueOf(objlnk.getObjectId() + ":" + objlnk.getObjectInstanceId());
break;
case OPAQUE:
byte[] binaryValue = (byte[]) val;
strValue = Base64.encodeBase64String(binaryValue);
break;
default:
throw new CodecException("Cannot encode %s in text format for %s", val, path);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,27 @@ public void text_battery_resource() throws CodecException {
assertEquals(100, ((Number) resource.getValue()).intValue());
}

@Test
public void text_decode_opaque_from_base64_string() throws CodecException {
// Using Firmware Update/Package
LwM2mSingleResource resource = (LwM2mSingleResource) decoder.decode("AQIDBAU=".getBytes(StandardCharsets.UTF_8),
ContentFormat.TEXT, new LwM2mPath(5, 0, 0), model);

byte[] expectedValue = new byte[] { 0x1, 0x2, 0x3, 0x4, 0x5 };

assertEquals(0, resource.getId());
assertFalse(resource.isMultiInstances());
assertEquals(Type.OPAQUE, resource.getType());
assertArrayEquals(expectedValue, ((byte[]) resource.getValue()));
}

@Test(expected = CodecException.class)
public void text_decode_should_throw_an_exception_for_invalid_base64() throws CodecException {
// Using Firmware Update/Package
decoder.decode("!,-INVALID$_'".getBytes(StandardCharsets.UTF_8),
ContentFormat.TEXT, new LwM2mPath(5, 0, 0), model);
}

@Test
public void tlv_manufacturer_resource() throws CodecException {
String value = "MyManufacturer";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ public void text_encode_multiple_instances() {
model);
}

@Test
public void text_encode_opaque_as_base64_string() {
byte[] opaqueValue = new byte[] { 0x1, 0x2, 0x3, 0x4, 0x5 };
byte[] encoded = encoder.encode(LwM2mSingleResource.newBinaryResource(0, opaqueValue), ContentFormat.TEXT,
new LwM2mPath("/5/0/0"), model);

Assert.assertEquals("AQIDBAU=", new String(encoded, StandardCharsets.UTF_8));
}

// tlv content for instance 0 of device object (encoded as an array of resource TLVs)
// Example from LWM2M spec §4.3.1
private final static byte[] ENCODED_DEVICE_WITHOUT_INSTANCE = Hex.decodeHex(
Expand Down