Imported Upstream version 0.9.1
[platform/upstream/iotivity.git] / resource / csdk / connectivity / lib / libcoap-4.1.1 / tests / test_uri.c
1 /* libcoap unit tests
2  *
3  * Copyright (C) 2012 Olaf Bergmann <bergmann@tzi.org>
4  *
5  * This file is part of the CoAP library libcoap. Please see
6  * README for terms of use.
7  */
8
9 #include <stdio.h>
10 #include <coap.h>
11 #include "test_uri.h"
12
13 void t_parse_uri1(void)
14 {
15     char teststr[] = "coap://[::1]/.well-known/core";
16
17     int result;
18     coap_uri_t uri;
19
20     result = coap_split_uri((unsigned char *) teststr, strlen(teststr), &uri);
21     if (result == 0)
22     {
23         CU_ASSERT(uri.host.length == 3);
24         CU_ASSERT_NSTRING_EQUAL(uri.host.s, "::1", 3);
25
26         CU_ASSERT(uri.port == COAP_DEFAULT_PORT);
27
28         CU_ASSERT(uri.path.length == 16);
29         CU_ASSERT_NSTRING_EQUAL(uri.path.s, ".well-known/core", 16);
30
31         CU_ASSERT(uri.query.length == 0);
32         CU_ASSERT(uri.query.s == NULL);
33     }
34     else
35     {
36         CU_FAIL("uri parser error");
37     }
38 }
39
40 void t_parse_uri2(void)
41 {
42     char teststr[] = "coap://[::1]:8000/.well-known/core";
43     int result;
44     coap_uri_t uri;
45
46     result = coap_split_uri((unsigned char *) teststr, strlen(teststr), &uri);
47     if (result == 0)
48     {
49         CU_ASSERT(uri.host.length == 3);
50         CU_ASSERT_NSTRING_EQUAL(uri.host.s, "::1", 3);
51
52         CU_ASSERT(uri.port == 8000);
53
54         CU_ASSERT(uri.path.length == 16);
55         CU_ASSERT_NSTRING_EQUAL(uri.path.s, ".well-known/core", 16);
56
57         CU_ASSERT(uri.query.length == 0);
58         CU_ASSERT(uri.query.s == NULL);
59     }
60     else
61     {
62         CU_FAIL("uri parser error");
63     }
64 }
65
66 void t_parse_uri3(void)
67 {
68     char teststr[] = "coap://localhost/?foo&bla=fasel";
69     int result;
70     coap_uri_t uri;
71
72     result = coap_split_uri((unsigned char *) teststr, strlen(teststr), &uri);
73     if (result == 0)
74     {
75         CU_ASSERT(uri.host.length == 9);
76         CU_ASSERT_NSTRING_EQUAL(uri.host.s, "localhost", 9);
77
78         CU_ASSERT(uri.port == COAP_DEFAULT_PORT);
79
80         CU_ASSERT(uri.path.length == 0);
81
82         CU_ASSERT(uri.query.length == 13);
83         CU_ASSERT_NSTRING_EQUAL(uri.query.s, "foo&bla=fasel", 13);
84     }
85     else
86     {
87         CU_FAIL("uri parser error");
88     }
89 }
90
91 void t_parse_uri4(void)
92 {
93     char teststr[] = "coap://:100000";
94     int result;
95     coap_uri_t uri;
96
97     result = coap_split_uri((unsigned char *) teststr, strlen(teststr), &uri);
98     CU_ASSERT(result < 0);
99 }
100
101 void t_parse_uri5(void)
102 {
103     char teststr[] = "coap://foo:100000";
104     int result;
105     coap_uri_t uri;
106
107     result = coap_split_uri((unsigned char *) teststr, strlen(teststr), &uri);
108     if (result == 0)
109     {
110         CU_ASSERT(uri.host.length == 3);
111         CU_ASSERT_NSTRING_EQUAL(uri.host.s, "foo", 3);
112
113         CU_ASSERT(uri.path.length == 0);
114         CU_ASSERT(uri.path.s == NULL);
115
116         CU_ASSERT(uri.query.length == 0);
117         CU_ASSERT(uri.query.s == NULL);
118
119         CU_FAIL("invalid port not detected");
120     }
121     else
122     {
123         CU_PASS("detected invalid port");
124     }
125 }
126
127 void t_parse_uri6(void)
128 {
129     char teststr[] = "coap://134.102.218.2/.well-known/core";
130     int result;
131     coap_uri_t uri;
132
133     result = coap_split_uri((unsigned char *) teststr, strlen(teststr), &uri);
134     if (result == 0)
135     {
136         CU_ASSERT(uri.host.length == 13);
137         CU_ASSERT_NSTRING_EQUAL(uri.host.s, "134.102.218.2", 13);
138
139         CU_ASSERT(uri.port == COAP_DEFAULT_PORT);
140
141         CU_ASSERT(uri.path.length == 16);
142         CU_ASSERT_NSTRING_EQUAL(uri.path.s, ".well-known/core", 16);
143
144         CU_ASSERT(uri.query.length == 0);
145         CU_ASSERT(uri.query.s == NULL);
146     }
147     else
148     {
149         CU_FAIL("uri parser error");
150     }
151 }
152
153 void t_parse_uri7(void)
154 {
155     char teststr[] = "coap://foo.bar:5683/some_resource/with/multiple/segments";
156     int result;
157     coap_uri_t uri;
158     unsigned char buf[40];
159     size_t buflen = sizeof(buf);
160
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',
167             'n', 't', 's' };
168
169     result = coap_split_uri((unsigned char *) teststr, strlen(teststr), &uri);
170     if (result == 0)
171     {
172         CU_ASSERT(uri.host.length == 7);
173         CU_ASSERT_NSTRING_EQUAL(uri.host.s, "foo.bar", 7);
174
175         CU_ASSERT(uri.port == 5683);
176
177         CU_ASSERT(uri.path.length == 36);
178         CU_ASSERT_NSTRING_EQUAL(uri.path.s, "some_resource/with/multiple/segments", 36);
179
180         CU_ASSERT(uri.query.length == 0);
181         CU_ASSERT(uri.query.s == NULL);
182
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);
188     }
189     else
190     {
191         CU_FAIL("uri parser error");
192     }
193 }
194
195 void t_parse_uri8(void)
196 {
197     char teststr[] = "http://example.com/%7E%AB%13";
198     int result;
199     coap_uri_t uri;
200
201     result = coap_split_uri((unsigned char *) teststr, strlen(teststr), &uri);
202     if (result < 0)
203     {
204         CU_PASS("detected non-coap URI");
205     }
206     else
207     {
208         CU_FAIL("non-coap URI not recognized");
209     }
210 }
211
212 void t_parse_uri9(void)
213 {
214     char teststr[] = "http://example.com/%x";
215     int result;
216     coap_uri_t uri;
217
218     result = coap_split_uri((unsigned char *) teststr, strlen(teststr), &uri);
219     if (result < 0)
220     {
221         CU_PASS("detected non-coap URI");
222     }
223     else
224     {
225         CU_FAIL("non-coap URI not recognized");
226     }
227 }
228
229 void t_parse_uri10(void)
230 {
231     char teststr[] = "/absolute/path";
232     int result;
233     coap_uri_t uri;
234
235     result = coap_split_uri((unsigned char *) teststr, strlen(teststr), &uri);
236     if (result == 0)
237     {
238         CU_ASSERT(uri.host.length == 0);
239         CU_ASSERT(uri.host.s == NULL);
240
241         CU_ASSERT(uri.port == COAP_DEFAULT_PORT);
242
243         CU_ASSERT(uri.path.length == 13);
244         CU_ASSERT_NSTRING_EQUAL(uri.path.s, "absolute/path", 13);
245
246         CU_ASSERT(uri.query.length == 0);
247         CU_ASSERT(uri.query.s == NULL);
248     }
249     else
250     {
251         CU_FAIL("uri parser error");
252     }
253 }
254
255 void t_parse_uri11(void)
256 {
257     char teststr[] = "coap://xn--18j4d.example/%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF";
258     int result;
259     coap_uri_t uri;
260     unsigned char buf[40];
261     size_t buflen = sizeof(buf);
262
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,
268             0x81, 0xAF };
269
270     result = coap_split_uri((unsigned char *) teststr, strlen(teststr), &uri);
271     if (result == 0)
272     {
273         CU_ASSERT(uri.host.length == 17);
274         CU_ASSERT_NSTRING_EQUAL(uri.host.s, "xn--18j4d.example", 17);
275
276         CU_ASSERT(uri.port == COAP_DEFAULT_PORT);
277
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);
280
281         CU_ASSERT(uri.query.length == 0);
282         CU_ASSERT(uri.query.s == NULL);
283
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);
289     }
290     else
291     {
292         CU_FAIL("uri parser error");
293     }
294 }
295
296 void t_parse_uri12(void)
297 {
298     char teststr[] = "coap://198.51.100.1:61616//%2F//?%2F%2F&?%26";
299     int result;
300     coap_uri_t uri;
301     unsigned char buf[40];
302     size_t buflen = sizeof(buf);
303
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 };
311
312     result = coap_split_uri((unsigned char *) teststr, strlen(teststr), &uri);
313     if (result == 0)
314     {
315         CU_ASSERT(uri.host.length == 12);
316         CU_ASSERT_NSTRING_EQUAL(uri.host.s, "198.51.100.1", 12);
317
318         CU_ASSERT(uri.port == 61616);
319
320         CU_ASSERT(uri.path.length == 6);
321         CU_ASSERT_NSTRING_EQUAL(uri.path.s, "/%2F//", 6);
322
323         CU_ASSERT(uri.query.length == 11);
324         CU_ASSERT_NSTRING_EQUAL(uri.query.s, "%2F%2F&?%26", 11);
325
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);
331
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);
338     }
339     else
340     {
341         CU_FAIL("uri parser error");
342     }
343 }
344
345 void t_parse_uri13(void)
346 {
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' };
350
351     coap_pdu_t pdu =
352     { .max_size = sizeof(teststr), .hdr = (coap_hdr_t *) teststr, .length = sizeof(teststr) };
353
354     coap_key_t key;
355
356     coap_hash_request_uri(&pdu, key);
357
358     CU_ASSERT(sizeof(key) == sizeof(COAP_DEFAULT_WKC_HASHKEY) - 1);
359     CU_ASSERT_NSTRING_EQUAL(key, COAP_DEFAULT_WKC_HASHKEY, sizeof(key));
360 }
361
362 CU_pSuite t_init_uri_tests(void)
363 {
364     CU_pSuite suite;
365
366     suite = CU_add_suite("uri parser", NULL, NULL);
367     if (!suite)
368     { /* signal error */
369         fprintf(stderr, "W: cannot add uri parser test suite (%s)\n", CU_get_error_msg());
370
371         return NULL;
372     }
373
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());                      \
378   }
379
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);
393
394     return suite;
395 }
396