From 5567ea36137edae1a53f3e0f4de586416d1a7193 Mon Sep 17 00:00:00 2001 From: Olaf Bergmann Date: Fri, 10 Jun 2016 13:43:38 +0200 Subject: [PATCH] test_uri.c: additional unit tests for option encoding These tests cover the Uri-Query encoding bug. --- tests/test_uri.c | 87 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/tests/test_uri.c b/tests/test_uri.c index d6f21447..004cbd6a 100644 --- a/tests/test_uri.c +++ b/tests/test_uri.c @@ -339,6 +339,89 @@ t_parse_uri13(void) { CU_ASSERT_NSTRING_EQUAL(key, COAP_DEFAULT_WKC_HASHKEY, sizeof(key)); } +static void +t_parse_uri14(void) { + char teststr[] = + "longerthan13lessthan270=0123456789012345678901234567890123456789"; + int result; + + /* buf is large enough to hold sizeof(teststr) - 1 bytes content and + * 2 bytes for the option header. */ + unsigned char buf[sizeof(teststr) + 1]; + size_t buflen = sizeof(buf); + + result = coap_split_query((unsigned char *)teststr, strlen(teststr), + buf, &buflen); + if (result >= 0) { + CU_ASSERT(buf[0] == 0x0d); + CU_ASSERT(buf[1] == strlen(teststr) - 13); + + CU_ASSERT_NSTRING_EQUAL(buf+2, teststr, strlen(teststr)); + } else { + CU_FAIL("uri parser error"); + } +} + +static void +t_parse_uri15(void) { + char teststr[] = + "longerthan13lessthan270=0123456789012345678901234567890123456789"; + int result; + + /* buf is too small to hold sizeof(teststr) - 1 bytes content and 2 + * bytes for the option header. */ + unsigned char buf[sizeof(teststr) - 1]; + size_t buflen = sizeof(buf); + + result = coap_split_query((unsigned char *)teststr, strlen(teststr), + buf, &buflen); + CU_ASSERT(result == 0); +} + +static void +t_parse_uri16(void) { + char teststr[] = + "longerthan13lessthan270=0123456789012345678901234567890123456789"; + int result; + + /* buf is too small to hold the option header. */ + unsigned char buf[1]; + size_t buflen = sizeof(buf); + + result = coap_split_query((unsigned char *)teststr, strlen(teststr), + buf, &buflen); + CU_ASSERT(result == 0); +} + +static void +t_parse_uri17(void) { + char teststr[] = + "thisislongerthan269=" + "01234567890123456789012345678901234567890123456789" + "01234567890123456789012345678901234567890123456789" + "01234567890123456789012345678901234567890123456789" + "01234567890123456789012345678901234567890123456789" + "01234567890123456789012345678901234567890123456789"; + int result; + + /* buf is large enough to hold sizeof(teststr) - 1 bytes content and + * 3 bytes for the option header. */ + unsigned char buf[sizeof(teststr) + 2]; + size_t buflen = sizeof(buf); + + result = coap_split_query((unsigned char *)teststr, strlen(teststr), + buf, &buflen); + if (result >= 0) { + CU_ASSERT(buf[0] == 0x0e); + CU_ASSERT(buf[1] == (((strlen(teststr) - 269) >> 8) & 0xff)); + CU_ASSERT(buf[2] == ((strlen(teststr) - 269) & 0xff)); + + CU_ASSERT_NSTRING_EQUAL(buf+3, teststr, strlen(teststr)); + } else { + CU_FAIL("uri parser error"); + } +} + CU_pSuite t_init_uri_tests(void) { CU_pSuite suite; @@ -370,6 +453,10 @@ t_init_uri_tests(void) { URI_TEST(suite, t_parse_uri11); URI_TEST(suite, t_parse_uri12); URI_TEST(suite, t_parse_uri13); + URI_TEST(suite, t_parse_uri14); + URI_TEST(suite, t_parse_uri15); + URI_TEST(suite, t_parse_uri16); + URI_TEST(suite, t_parse_uri17); return suite; }