3 * Copyright (C) 2012 Olaf Bergmann <bergmann@tzi.org>
5 * This file is part of the CoAP library libcoap. Please see
6 * README for terms of use.
13 void t_parse_uri1(void)
15 char teststr[] = "coap://[::1]/.well-known/core";
20 result = coap_split_uri((unsigned char *) teststr, strlen(teststr), &uri);
23 CU_ASSERT(uri.host.length == 3);
24 CU_ASSERT_NSTRING_EQUAL(uri.host.s, "::1", 3);
26 CU_ASSERT(uri.port == COAP_DEFAULT_PORT);
28 CU_ASSERT(uri.path.length == 16);
29 CU_ASSERT_NSTRING_EQUAL(uri.path.s, ".well-known/core", 16);
31 CU_ASSERT(uri.query.length == 0);
32 CU_ASSERT(uri.query.s == NULL);
36 CU_FAIL("uri parser error");
40 void t_parse_uri2(void)
42 char teststr[] = "coap://[::1]:8000/.well-known/core";
46 result = coap_split_uri((unsigned char *) teststr, strlen(teststr), &uri);
49 CU_ASSERT(uri.host.length == 3);
50 CU_ASSERT_NSTRING_EQUAL(uri.host.s, "::1", 3);
52 CU_ASSERT(uri.port == 8000);
54 CU_ASSERT(uri.path.length == 16);
55 CU_ASSERT_NSTRING_EQUAL(uri.path.s, ".well-known/core", 16);
57 CU_ASSERT(uri.query.length == 0);
58 CU_ASSERT(uri.query.s == NULL);
62 CU_FAIL("uri parser error");
66 void t_parse_uri3(void)
68 char teststr[] = "coap://localhost/?foo&bla=fasel";
72 result = coap_split_uri((unsigned char *) teststr, strlen(teststr), &uri);
75 CU_ASSERT(uri.host.length == 9);
76 CU_ASSERT_NSTRING_EQUAL(uri.host.s, "localhost", 9);
78 CU_ASSERT(uri.port == COAP_DEFAULT_PORT);
80 CU_ASSERT(uri.path.length == 0);
82 CU_ASSERT(uri.query.length == 13);
83 CU_ASSERT_NSTRING_EQUAL(uri.query.s, "foo&bla=fasel", 13);
87 CU_FAIL("uri parser error");
91 void t_parse_uri4(void)
93 char teststr[] = "coap://:100000";
97 result = coap_split_uri((unsigned char *) teststr, strlen(teststr), &uri);
98 CU_ASSERT(result < 0);
101 void t_parse_uri5(void)
103 char teststr[] = "coap://foo:100000";
107 result = coap_split_uri((unsigned char *) teststr, strlen(teststr), &uri);
110 CU_ASSERT(uri.host.length == 3);
111 CU_ASSERT_NSTRING_EQUAL(uri.host.s, "foo", 3);
113 CU_ASSERT(uri.path.length == 0);
114 CU_ASSERT(uri.path.s == NULL);
116 CU_ASSERT(uri.query.length == 0);
117 CU_ASSERT(uri.query.s == NULL);
119 CU_FAIL("invalid port not detected");
123 CU_PASS("detected invalid port");
127 void t_parse_uri6(void)
129 char teststr[] = "coap://134.102.218.2/.well-known/core";
133 result = coap_split_uri((unsigned char *) teststr, strlen(teststr), &uri);
136 CU_ASSERT(uri.host.length == 13);
137 CU_ASSERT_NSTRING_EQUAL(uri.host.s, "134.102.218.2", 13);
139 CU_ASSERT(uri.port == COAP_DEFAULT_PORT);
141 CU_ASSERT(uri.path.length == 16);
142 CU_ASSERT_NSTRING_EQUAL(uri.path.s, ".well-known/core", 16);
144 CU_ASSERT(uri.query.length == 0);
145 CU_ASSERT(uri.query.s == NULL);
149 CU_FAIL("uri parser error");
153 void t_parse_uri7(void)
155 char teststr[] = "coap://foo.bar:5683/some_resource/with/multiple/segments";
158 unsigned char buf[40];
159 size_t buflen = sizeof(buf);
161 /* The list of path segments to check against. Each segment is
162 preceded by a dummy option indicating that holds the (dummy)
163 delta value 0 and the actual segment length. */
164 const unsigned char checkbuf[] =
165 { 0x0d, 0x00, 's', 'o', 'm', 'e', '_', 'r', 'e', 's', 'o', 'u', 'r', 'c', 'e', 0x04, 'w', 'i',
166 't', 'h', 0x08, 'm', 'u', 'l', 't', 'i', 'p', 'l', 'e', 0x08, 's', 'e', 'g', 'm', 'e',
169 result = coap_split_uri((unsigned char *) teststr, strlen(teststr), &uri);
172 CU_ASSERT(uri.host.length == 7);
173 CU_ASSERT_NSTRING_EQUAL(uri.host.s, "foo.bar", 7);
175 CU_ASSERT(uri.port == 5683);
177 CU_ASSERT(uri.path.length == 36);
178 CU_ASSERT_NSTRING_EQUAL(uri.path.s, "some_resource/with/multiple/segments", 36);
180 CU_ASSERT(uri.query.length == 0);
181 CU_ASSERT(uri.query.s == NULL);
183 /* check path segments */
184 result = coap_split_path(uri.path.s, uri.path.length, buf, &buflen);
185 CU_ASSERT(result == 4);
186 CU_ASSERT(buflen == sizeof(checkbuf));
187 CU_ASSERT_NSTRING_EQUAL(buf, checkbuf, buflen);
191 CU_FAIL("uri parser error");
195 void t_parse_uri8(void)
197 char teststr[] = "http://example.com/%7E%AB%13";
201 result = coap_split_uri((unsigned char *) teststr, strlen(teststr), &uri);
204 CU_PASS("detected non-coap URI");
208 CU_FAIL("non-coap URI not recognized");
212 void t_parse_uri9(void)
214 char teststr[] = "http://example.com/%x";
218 result = coap_split_uri((unsigned char *) teststr, strlen(teststr), &uri);
221 CU_PASS("detected non-coap URI");
225 CU_FAIL("non-coap URI not recognized");
229 void t_parse_uri10(void)
231 char teststr[] = "/absolute/path";
235 result = coap_split_uri((unsigned char *) teststr, strlen(teststr), &uri);
238 CU_ASSERT(uri.host.length == 0);
239 CU_ASSERT(uri.host.s == NULL);
241 CU_ASSERT(uri.port == COAP_DEFAULT_PORT);
243 CU_ASSERT(uri.path.length == 13);
244 CU_ASSERT_NSTRING_EQUAL(uri.path.s, "absolute/path", 13);
246 CU_ASSERT(uri.query.length == 0);
247 CU_ASSERT(uri.query.s == NULL);
251 CU_FAIL("uri parser error");
255 void t_parse_uri11(void)
257 char teststr[] = "coap://xn--18j4d.example/%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF";
260 unsigned char buf[40];
261 size_t buflen = sizeof(buf);
263 /* The list of path segments to check against. Each segment is
264 preceded by a dummy option indicating that holds the (dummy)
265 delta value 0 and the actual segment length. */
266 const unsigned char checkbuf[] =
267 { 0x0d, 0x02, 0xE3, 0x81, 0x93, 0xE3, 0x82, 0x93, 0xE3, 0x81, 0xAB, 0xE3, 0x81, 0xA1, 0xE3,
270 result = coap_split_uri((unsigned char *) teststr, strlen(teststr), &uri);
273 CU_ASSERT(uri.host.length == 17);
274 CU_ASSERT_NSTRING_EQUAL(uri.host.s, "xn--18j4d.example", 17);
276 CU_ASSERT(uri.port == COAP_DEFAULT_PORT);
278 CU_ASSERT(uri.path.length == 45);
279 CU_ASSERT_NSTRING_EQUAL(uri.path.s, "%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF", 45);
281 CU_ASSERT(uri.query.length == 0);
282 CU_ASSERT(uri.query.s == NULL);
284 /* check path segments */
285 result = coap_split_path(uri.path.s, uri.path.length, buf, &buflen);
286 CU_ASSERT(result == 1);
287 CU_ASSERT(buflen == sizeof(checkbuf));
288 CU_ASSERT_NSTRING_EQUAL(buf, checkbuf, buflen);
292 CU_FAIL("uri parser error");
296 void t_parse_uri12(void)
298 char teststr[] = "coap://198.51.100.1:61616//%2F//?%2F%2F&?%26";
301 unsigned char buf[40];
302 size_t buflen = sizeof(buf);
304 /* The list of path segments to check against. Each segment is
305 preceded by a dummy option indicating that holds the (dummy)
306 delta value 0 and the actual segment length. */
307 const unsigned char uricheckbuf[] =
308 { 0x00, 0x01, 0x2f, 0x00, 0x00 };
309 const unsigned char querycheckbuf[] =
310 { 0x02, 0x2f, 0x2f, 0x02, 0x3f, 0x26 };
312 result = coap_split_uri((unsigned char *) teststr, strlen(teststr), &uri);
315 CU_ASSERT(uri.host.length == 12);
316 CU_ASSERT_NSTRING_EQUAL(uri.host.s, "198.51.100.1", 12);
318 CU_ASSERT(uri.port == 61616);
320 CU_ASSERT(uri.path.length == 6);
321 CU_ASSERT_NSTRING_EQUAL(uri.path.s, "/%2F//", 6);
323 CU_ASSERT(uri.query.length == 11);
324 CU_ASSERT_NSTRING_EQUAL(uri.query.s, "%2F%2F&?%26", 11);
326 /* check path segments */
327 result = coap_split_path(uri.path.s, uri.path.length, buf, &buflen);
328 CU_ASSERT(result == 4);
329 CU_ASSERT(buflen == sizeof(uricheckbuf));
330 CU_ASSERT_NSTRING_EQUAL(buf, uricheckbuf, buflen);
332 /* check query segments */
333 buflen = sizeof(buf);
334 result = coap_split_query(uri.query.s, uri.query.length, buf, &buflen);
335 CU_ASSERT(result == 2);
336 CU_ASSERT(buflen == sizeof(querycheckbuf));
337 CU_ASSERT_NSTRING_EQUAL(buf, querycheckbuf, buflen);
341 CU_FAIL("uri parser error");
345 void t_parse_uri13(void)
347 char teststr[] __attribute__ ((aligned (8))) =
348 { 0x00, 0x00, 0x00, 0x00, 0x80, 0x03, 'f', 'o', 'o', 0x3b, '.', 'w', 'e', 'l', 'l', '-', 'k',
349 'n', 'o', 'w', 'n', 0x04, 'c', 'o', 'r', 'e' };
352 { .max_size = sizeof(teststr), .hdr = (coap_hdr_t *) teststr, .length = sizeof(teststr) };
356 coap_hash_request_uri(&pdu, key);
358 CU_ASSERT(sizeof(key) == sizeof(COAP_DEFAULT_WKC_HASHKEY) - 1);
359 CU_ASSERT_NSTRING_EQUAL(key, COAP_DEFAULT_WKC_HASHKEY, sizeof(key));
362 CU_pSuite t_init_uri_tests(void)
366 suite = CU_add_suite("uri parser", NULL, NULL);
369 fprintf(stderr, "W: cannot add uri parser test suite (%s)\n", CU_get_error_msg());
374 #define URI_TEST(s,t) \
375 if (!CU_ADD_TEST(s,t)) { \
376 fprintf(stderr, "W: cannot add uri parser test (%s)\n", \
377 CU_get_error_msg()); \
380 URI_TEST(suite, t_parse_uri1);
381 URI_TEST(suite, t_parse_uri2);
382 URI_TEST(suite, t_parse_uri3);
383 URI_TEST(suite, t_parse_uri4);
384 URI_TEST(suite, t_parse_uri5);
385 URI_TEST(suite, t_parse_uri6);
386 URI_TEST(suite, t_parse_uri7);
387 URI_TEST(suite, t_parse_uri8);
388 URI_TEST(suite, t_parse_uri9);
389 URI_TEST(suite, t_parse_uri10);
390 URI_TEST(suite, t_parse_uri11);
391 URI_TEST(suite, t_parse_uri12);
392 URI_TEST(suite, t_parse_uri13);