DataParser { public final Message parseMessage(final RawData raw) { if (raw == null) { throw new NullPointerException("raw-data must not be null!"); } Message message = parseMessage(raw.getBytes()); message.setSourceContext(raw.getEndpointContext()); message.setNanoTimestamp(raw.getReceiveNanoTimestamp()); return message; }  final Message parseMessage(final RawData raw); final Message parseMessage(final byte[] msg); static void parseOptionsAndPayload(DatagramReader reader, Message message);  }
@Test public void testParseMessageDetectsMalformedToken() { byte[] malformedGetRequest = new byte[] { 0b01001000, 0b00000001, 0x00, 0x10, 0x24, 0x01, 0x02, 0x03 }; try { parser.parseMessage(malformedGetRequest); fail("Parser should have detected malformed options"); } catch (CoAPMessageFormatException e) { assertFalse(tcp); assertEquals(0b00000001, e.getCode()); assertEquals(true, e.isConfirmable()); } catch (MessageFormatException e) { assertTrue(tcp); } }
@Test public void testParseMessageDetectsMissingPayload() { byte[] malformedGetRequest = new byte[] { 0b01000000, 0b00000001, 0x00, 0x10, (byte) 0xFF }; try { parser.parseMessage(malformedGetRequest); fail("Parser should have detected missing payload"); } catch (CoAPMessageFormatException e) { assertEquals(0b00000001, e.getCode()); assertEquals(true, e.isConfirmable()); } }
@Test public void testResponseParsing() { Response response = new Response(ResponseCode.CONTENT); response.setDestinationContext(ENDPOINT_CONTEXT); response.setType(Type.NON); response.setMID(expectedMid); response.setToken(new byte[] { 22, -1, 0, 78, 100, 22 }); response.getOptions().addETag(new byte[] { 1, 0, 0, 0, 0, 1 }) .addLocationPath("/one/two/three/four/five/six/seven/eight/nine/ten") .addOption(new Option(57453, "Arbitrary".hashCode())).addOption(new Option(19205, "Arbitrary1")) .addOption(new Option(19205, "Arbitrary2")).addOption(new Option(19205, "Arbitrary3")); RawData rawData = serializer.serializeResponse(response); Response result = (Response) parser.parseMessage(rawData); assertEquals(response.getMID(), result.getMID()); assertEquals(response.getToken(), result.getToken()); assertEquals(response.getOptions().asSortedList(), result.getOptions().asSortedList()); }
@Test public void testUTF8Encoding() { Response response = new Response(ResponseCode.CONTENT); response.setDestinationContext(ENDPOINT_CONTEXT); response.setType(Type.NON); response.setMID(expectedMid); response.setToken(Token.EMPTY); response.getOptions().addLocationPath("ᚠᛇᚻ᛫ᛒᛦᚦ᛫ᚠᚱᚩᚠᚢᚱ᛫ᚠᛁᚱᚪ᛫ᚷᛖᚻᚹᛦᛚᚳᚢᛗ").addLocationPath("γλώσσα") .addLocationPath("пустынных").addLocationQuery("ვეპხის=யாமறிந்த").addLocationQuery("⠊⠀⠉⠁⠝=⠑⠁⠞⠀⠛⠇⠁⠎⠎"); response.setPayload("⠊⠀⠉⠁⠝⠀⠑⠁⠞⠀⠛⠇⠁⠎⠎⠀⠁⠝⠙⠀⠊⠞⠀⠙⠕⠑⠎⠝⠞⠀⠓⠥⠗⠞⠀⠍⠑"); RawData rawData = serializer.serializeResponse(response); Response result = (Response) parser.parseMessage(rawData); assertEquals("ᚠᛇᚻ᛫ᛒᛦᚦ᛫ᚠᚱᚩᚠᚢᚱ᛫ᚠᛁᚱᚪ᛫ᚷᛖᚻᚹᛦᛚᚳᚢᛗ/γλώσσα/пустынных", response.getOptions().getLocationPathString()); assertEquals("ვეპხის=யாமறிந்த&⠊⠀⠉⠁⠝=⠑⠁⠞⠀⠛⠇⠁⠎⠎", response.getOptions().getLocationQueryString()); assertEquals("⠊⠀⠉⠁⠝⠀⠑⠁⠞⠀⠛⠇⠁⠎⠎⠀⠁⠝⠙⠀⠊⠞⠀⠙⠕⠑⠎⠝⠞⠀⠓⠥⠗⠞⠀⠍⠑", result.getPayloadString()); assertEquals(response.getMID(), result.getMID()); }
@Test public void testRequestParsing() { Request request = new Request(Code.POST); request.setDestinationContext(ENDPOINT_CONTEXT); request.setType(Type.NON); request.setMID(expectedMid); request.setToken(new byte[] { 11, 82, -91, 77, 3 }); request.getOptions().addIfMatch(new byte[] { 34, -17 }).addIfMatch(new byte[] { 88, 12, -2, -99, 5 }) .setContentFormat(40).setAccept(40); RawData rawData = serializer.serializeRequest(request); Request result = (Request) parser.parseMessage(rawData); assertEquals(request.getMID(), result.getMID()); assertEquals(request.getToken(), result.getToken()); assertEquals(request.getOptions().asSortedList(), result.getOptions().asSortedList()); }
@Test public void testParseMessageDetectsIllegalCodeClass() { byte[] malformedRequest = new byte[] { 0b01000000, 0b00100001, 0x00, 0x10 }; try { parser.parseMessage(malformedRequest); fail("Parser should have detected that message is not a request"); } catch (CoAPMessageFormatException e) { assertEquals(0b00100001, e.getCode()); assertEquals(true, e.isConfirmable()); } }
@Test public void testParseMessageDetectsIllegalCode() { byte code = 0b00001000; byte[] malformedRequest = new byte[] { 0b01000000, code, 0x00, 0x10 }; try { parser.parseMessage(malformedRequest); fail("Parser should have detected that message is not a request"); } catch (CoAPMessageFormatException e) { assertEquals(code, e.getCode()); assertEquals(true, e.isConfirmable()); } }
@Test public void testParseMessageDetectsMalformedOption() { byte[] malformedGetRequest = new byte[] { 0b01000000, 0b00000001, 0x00, 0x10, 0x24, 0x01, 0x02, 0x03 }; try { parser.parseMessage(malformedGetRequest); fail("Parser should have detected malformed options"); } catch (CoAPMessageFormatException e) { assertEquals(0b00000001, e.getCode()); assertEquals(true, e.isConfirmable()); } }