test_uri.c: additional unit tests for option encoding

These tests cover the Uri-Query encoding bug.
This commit is contained in:
Olaf Bergmann committed 2016-06-10 13:43:38 +02:00
1 parent fa69aeec79
commit 5567ea3613
1 file changed
+87
+87
View File
@@ -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;
}