forked from obgm/libcoap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_wellknown.c
281 lines (229 loc) · 8.12 KB
/
test_wellknown.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/* libcoap unit tests
*
* Copyright (C) 2013--2024 Olaf Bergmann <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* This file is part of the CoAP library libcoap. Please see
* README for terms of use.
*/
#include "test_common.h"
#include "test_wellknown.h"
#if COAP_SERVER_SUPPORT
#if COAP_CLIENT_SUPPORT
#include <assert.h>
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TEST_PDU_SIZE 120
#define TEST_URI_LEN 4
#define ReturnIf_CU_ASSERT_PTR_NOT_NULL(value) \
CU_ASSERT_PTR_NOT_NULL(value); \
if ((void*)value == NULL) return;
static coap_context_t *ctx; /* Holds the coap context for most tests */
static coap_pdu_t *pdu; /* Holds the parsed PDU for most tests */
static coap_session_t *session; /* Holds a reference-counted session object
* that is passed to coap_wellknown_response(). */
static void
t_wellknown1(void) {
coap_print_status_t result;
coap_resource_t *r;
unsigned char buf[40];
size_t buflen, offset, ofs;
char teststr[] = { /* </>;title="some attribute";ct=0 (31 chars) */
'<', '/', '>', ';', 't', 'i', 't', 'l',
'e', '=', '"', 's', 'o', 'm', 'e', ' ',
'a', 't', 't', 'r', 'i', 'b', 'u', 't',
'e', '"', ';', 'c', 't', '=', '0'
};
r = coap_resource_init(NULL, 0);
coap_add_attr(r, coap_make_str_const("ct"), coap_make_str_const("0"), 0);
coap_add_attr(r, coap_make_str_const("title"), coap_make_str_const("\"some attribute\""), 0);
coap_add_resource(ctx, r);
for (offset = 0; offset < sizeof(teststr); offset++) {
ofs = offset;
buflen = sizeof(buf);
result = coap_print_link(r, buf, &buflen, &ofs);
CU_ASSERT(result == sizeof(teststr) - offset);
CU_ASSERT(buflen == sizeof(teststr));
CU_ASSERT(memcmp(buf, teststr + offset, sizeof(teststr) - offset) == 0);
}
/* offset points behind teststr */
ofs = offset;
buflen = sizeof(buf);
result = coap_print_link(r, buf, &buflen, &ofs);
CU_ASSERT(result == 0);
CU_ASSERT(buflen == sizeof(teststr));
/* offset exceeds buffer */
buflen = sizeof(buf);
ofs = buflen;
result = coap_print_link(r, buf, &buflen, &ofs);
CU_ASSERT(result == 0);
CU_ASSERT(buflen == sizeof(teststr));
}
static void
t_wellknown2(void) {
coap_print_status_t result;
coap_resource_t *r;
unsigned char buf[10]; /* smaller than teststr */
size_t buflen, offset, ofs;
char teststr[] = { /* ,</abcd>;if="one";obs (21 chars) */
'<', '/', 'a', 'b', 'c', 'd', '>', ';',
'i', 'f', '=', '"', 'o', 'n', 'e', '"',
';', 'o', 'b', 's'
};
r = coap_resource_init(coap_make_str_const("abcd"), 0);
coap_resource_set_get_observable(r, 1);
coap_add_attr(r, coap_make_str_const("if"), coap_make_str_const("\"one\""), 0);
coap_add_resource(ctx, r);
for (offset = 0; offset < sizeof(teststr) - sizeof(buf); offset++) {
ofs = offset;
buflen = sizeof(buf);
result = coap_print_link(r, buf, &buflen, &ofs);
CU_ASSERT(result == (COAP_PRINT_STATUS_TRUNC | sizeof(buf)));
CU_ASSERT(buflen == sizeof(teststr));
CU_ASSERT(ofs == 0);
CU_ASSERT(memcmp(buf, teststr + offset, sizeof(buf)) == 0);
}
/* from here on, the resource description fits into buf */
for (; offset < sizeof(teststr); offset++) {
ofs = offset;
buflen = sizeof(buf);
result = coap_print_link(r, buf, &buflen, &ofs);
CU_ASSERT(result == sizeof(teststr) - offset);
CU_ASSERT(buflen == sizeof(teststr));
CU_ASSERT(ofs == 0);
CU_ASSERT(memcmp(buf, teststr + offset,
COAP_PRINT_OUTPUT_LENGTH(result)) == 0);
}
/* offset exceeds buffer */
buflen = sizeof(buf);
ofs = offset;
result = coap_print_link(r, buf, &buflen, &ofs);
CU_ASSERT(result == 0);
CU_ASSERT(buflen == sizeof(teststr));
CU_ASSERT(ofs == offset - sizeof(teststr));
}
static void
t_wellknown3(void) {
coap_print_status_t result;
int j;
coap_resource_t *r;
static char uris[2 * 1024];
unsigned char *uribuf = (unsigned char *)uris;
unsigned char buf[40];
size_t buflen = sizeof(buf);
size_t offset;
const uint16_t num_resources = (sizeof(uris) / TEST_URI_LEN) - 1;
/* ,</0000> (TEST_URI_LEN + 4 chars) */
for (j = 0; j < num_resources; j++) {
int len = snprintf((char *)uribuf, TEST_URI_LEN + 1,
"%0*d", TEST_URI_LEN, j);
coap_str_const_t uri_path = {.length = len, .s = uribuf};
r = coap_resource_init(&uri_path, 0);
coap_add_resource(ctx, r);
uribuf += TEST_URI_LEN;
}
/* the following test assumes that the first two resources from
* t_wellknown1() and t_wellknown2() need more than buflen
* characters. Otherwise, CU_ASSERT(result > 0) will fail.
*/
offset = num_resources * (TEST_URI_LEN + 4);
result = coap_print_wellknown(ctx, buf, &buflen, offset, NULL);
CU_ASSERT((result & COAP_PRINT_STATUS_ERROR) == 0);
CU_ASSERT(COAP_PRINT_OUTPUT_LENGTH(result) > 0);
}
static void
t_wellknown4(void) {
coap_print_status_t result;
coap_string_t *query;
unsigned char buf[40];
size_t buflen = sizeof(buf);
char teststr[] = { /* ,</abcd>;if="one";obs (21 chars) */
'<', '/', 'a', 'b', 'c', 'd', '>', ';',
'i', 'f', '=', '"', 'o', 'n', 'e', '"',
';', 'o', 'b', 's'
};
/* Check for the resource added in t_wellknown2 */
query = coap_new_string(sizeof("if=one")-1);
ReturnIf_CU_ASSERT_PTR_NOT_NULL(query);
memcpy(query->s, "if=one", sizeof("if=one")-1);
result = coap_print_wellknown(ctx, buf, &buflen, 0, query);
CU_ASSERT((result & COAP_PRINT_STATUS_ERROR) == 0);
CU_ASSERT(COAP_PRINT_OUTPUT_LENGTH(result) == sizeof(teststr));
CU_ASSERT(buflen == sizeof(teststr));
CU_ASSERT(memcmp(buf, teststr, buflen) == 0);
coap_delete_string(query);
}
static int
t_wkc_tests_create(void) {
coap_address_t addr;
coap_address_init(&addr);
addr.size = sizeof(struct sockaddr_in6);
addr.addr.sin6.sin6_family = AF_INET6;
addr.addr.sin6.sin6_addr = in6addr_any;
addr.addr.sin6.sin6_port = htons(COAP_DEFAULT_PORT);
ctx = coap_new_context(&addr);
addr.addr.sin6.sin6_addr = in6addr_loopback;
session = coap_new_client_session(ctx, NULL, &addr, COAP_PROTO_UDP);
pdu = coap_pdu_init(0, 0, 0, TEST_PDU_SIZE);
#if 0
/* add resources to coap context */
if (ctx && pdu) {
coap_resource_t *r;
static char _buf[2 * 1024];
unsigned char *buf = (unsigned char *)_buf;
int i;
/* </>;title="some attribute";ct=0 (31 chars) */
r = coap_resource_init(NULL, 0, 0);
coap_add_attr(r, coap_make_str_const("ct"), coap_make_str_const("0"), 0);
coap_add_attr(r, coap_make_str_const("title"), coap_make_str_const("\"some attribute\""), 0);
coap_add_resource(ctx, r);
/* ,</abcd>;if="one";obs (21 chars) */
r = coap_resource_init((const uint8_t *)"abcd", 4, 0);
r->observable = 1;
coap_add_attr(r, coap_make_str_const("if"), coap_make_str_const("\"one\""), 0);
coap_add_resource(ctx, r);
/* ,</0000> (TEST_URI_LEN + 4 chars) */
for (i = 0; i < sizeof(_buf) / (TEST_URI_LEN + 4); i++) {
int len = snprintf((char *)buf, TEST_URI_LEN + 1,
"%0*d", TEST_URI_LEN, i);
r = coap_resource_init(buf, len, 0);
coap_add_resource(ctx, r);
buf += TEST_URI_LEN + 1;
}
}
#endif
return ctx == NULL || pdu == NULL;
}
static int
t_wkc_tests_remove(void) {
coap_delete_pdu(pdu);
coap_free_context(ctx);
return 0;
}
CU_pSuite
t_init_wellknown_tests(void) {
CU_pSuite suite;
suite = CU_add_suite(".well-known/core", t_wkc_tests_create, t_wkc_tests_remove);
if (!suite) { /* signal error */
fprintf(stderr, "W: cannot add .well-known/core test suite (%s)\n",
CU_get_error_msg());
return NULL;
}
#define WKC_TEST(s,t) \
if (!CU_ADD_TEST(s,t)) { \
fprintf(stderr, "W: cannot add .well-known/core test (%s)\n", \
CU_get_error_msg()); \
}
WKC_TEST(suite, t_wellknown1);
WKC_TEST(suite, t_wellknown2);
WKC_TEST(suite, t_wellknown3);
WKC_TEST(suite, t_wellknown4);
return suite;
}
#endif /* COAP_CLIENT_SUPPORT */
#endif /* COAP_SERVER_SUPPORT */