Skip to content

Commit

Permalink
#416 : raise CodecException if 2 Tlv resource instance with the same id
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Nov 9, 2017
1 parent 69881c0 commit ba3a103
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,12 @@ private static Map<Integer, Object> parseTlvValues(Tlv[] tlvs, Type expectedType
throw new CodecException("Expected TLV of type RESOURCE_INSTANCE but was %s for path %s",
tlvChild.getType().name(), path);

values.put(tlvChild.getIdentifier(), parseTlvValue(tlvChild.getValue(), expectedType, path));
Object resourceInstance = parseTlvValue(tlvChild.getValue(), expectedType, path);
Object previousResourceInstance = values.put(tlvChild.getIdentifier(), resourceInstance);
if (previousResourceInstance != null) {
throw new CodecException("2 RESOURCE_INSTANCE (%s,%s) with the same identifier %d for path %s",
previousResourceInstance, resourceInstance, tlvChild.getIdentifier(), path);
}
}
return values;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.eclipse.leshan.core.node.LwM2mPath;
import org.eclipse.leshan.core.node.LwM2mResource;
import org.eclipse.leshan.core.node.LwM2mSingleResource;
import org.eclipse.leshan.core.node.ObjectLink;
import org.eclipse.leshan.core.node.TimestampedLwM2mNode;
import org.eclipse.leshan.core.request.ContentFormat;
import org.eclipse.leshan.tlv.Tlv;
Expand Down Expand Up @@ -260,6 +261,16 @@ public void tlv_empty_multi_resource() {
assertTrue(resource.getValues().isEmpty());
}

@Test(expected = CodecException.class)
public void tlv_invalid_multi_resource_2_instance_with_the_same_id() {
Tlv resInstance1 = new Tlv(TlvType.RESOURCE_INSTANCE, null, TlvEncoder.encodeObjlnk(new ObjectLink(100, 1)), 0);
Tlv resInstance2 = new Tlv(TlvType.RESOURCE_INSTANCE, null, TlvEncoder.encodeObjlnk(new ObjectLink(101, 2)), 0);
Tlv multiResource = new Tlv(TlvType.MULTIPLE_RESOURCE, new Tlv[] { resInstance1, resInstance2 }, null, 22);
byte[] content = TlvEncoder.encode(new Tlv[] { multiResource }).array();

decoder.decode(content, ContentFormat.TLV, new LwM2mPath(3, 0, 22), model);
}

@Test
public void json_device_object_instance0() throws CodecException {
// json content for instance 0 of device object
Expand Down

0 comments on commit ba3a103

Please sign in to comment.