Blame view

RIOT/tests/unittests/tests-gcoap/tests-gcoap.c 9.41 KB
a752c7ab   elopes   add first test an...
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
  /*
   * Copyright (c) 2016 Ken Bannister. All rights reserved.
   *
   * This file is subject to the terms and conditions of the GNU Lesser
   * General Public License v2.1. See the file LICENSE in the top level
   * directory for more details.
   */
  
  /**
   * @{
   *
   * @file
   */
  #include <errno.h>
  #include <stdint.h>
  #include <stdlib.h>
  
  #include "embUnit.h"
  
  #include "net/gcoap.h"
  
  #include "unittests-constants.h"
  #include "tests-gcoap.h"
  
  /*
   * A test set of dummy resources. The resource handlers are set to NULL.
   */
  static const coap_resource_t ressources[] = {
      { "/test/info/all", (COAP_GET), NULL },
      { "/sensor/temp", (COAP_GET), NULL },
      { "/act/switch", (COAP_GET | COAP_POST), NULL }
  };
  
  static const coap_resource_t resources_second[] = {
      { "/second/part", (COAP_GET), NULL },
  };
  
  static gcoap_listener_t listener = {
      .resources     = (coap_resource_t *)&ressources[0],
      .resources_len = (sizeof(ressources) / sizeof(ressources[0])),
      .next          = NULL
  };
  
  static gcoap_listener_t listener_second = {
      .resources     = (coap_resource_t *)&resources_second[0],
      .resources_len = (sizeof(resources_second) / sizeof(resources_second[0])),
      .next          = NULL
  };
  
  static const char *resource_list_str = "</test/info/all>,</sensor/temp>,</act/switch>,</second/part>";
  
  /*
   * Client GET request success case. Test request generation.
   * Request /time resource from libcoap example
   * Includes token of length GCOAP_TOKENLEN.
   */
  static void test_gcoap__client_get_req(void)
  {
      uint8_t buf[GCOAP_PDU_BUF_SIZE];
      coap_pkt_t pdu;
      size_t len;
      char path[] = "/time";
  
      /* Create expected pdu_data, with token length from GCOAP_TOKENLEN. */
      size_t hdr_fixed_len = 4;
      uint8_t hdr_fixed[]  = { 0x52, 0x01, 0xe6, 0x02 };
      size_t options_len   = 5;
      uint8_t options[]    = { 0xb4, 0x74, 0x69, 0x6d, 0x65 };
  
      uint8_t pdu_data[hdr_fixed_len + GCOAP_TOKENLEN + options_len];
  
      memcpy(&pdu_data[0], &hdr_fixed[0], hdr_fixed_len);
  #if GCOAP_TOKENLEN
      /* actual value is random */
      memset(&pdu_data[hdr_fixed_len], 0x9b, GCOAP_TOKENLEN);
  #endif
      memcpy(&pdu_data[hdr_fixed_len + GCOAP_TOKENLEN], &options[0], options_len);
  
      len = gcoap_request(&pdu, &buf[0], GCOAP_PDU_BUF_SIZE, COAP_METHOD_GET,
                                                             &path[0]);
  
      TEST_ASSERT_EQUAL_INT(COAP_METHOD_GET, coap_get_code(&pdu));
      TEST_ASSERT_EQUAL_INT(GCOAP_TOKENLEN, coap_get_token_len(&pdu));
      TEST_ASSERT_EQUAL_INT(hdr_fixed_len + GCOAP_TOKENLEN, coap_get_total_hdr_len(&pdu));
      TEST_ASSERT_EQUAL_INT(COAP_TYPE_NON, coap_get_type(&pdu));
      TEST_ASSERT_EQUAL_STRING(&path[0], (char *)&pdu.url[0]);
      TEST_ASSERT_EQUAL_INT(0, pdu.payload_len);
      TEST_ASSERT_EQUAL_INT(sizeof(pdu_data), len);
  }
  
  /*
   * Client GET response success case. Test parsing response.
   * Response for /time resource from libcoap example
   * Includes 2-byte token
   */
  static void test_gcoap__client_get_resp(void)
  {
      uint8_t buf[GCOAP_PDU_BUF_SIZE];
      coap_pkt_t pdu;
      int res;
      size_t hdr_fixed_len = 4;
      char exp_payload[]   = "Oct 22 10:46:48";
      size_t exp_tokenlen  = 2;
  
      uint8_t pdu_data[] = {
          0x52, 0x45, 0xe6, 0x02, 0x9b, 0xce, 0xc0, 0x21,
          0x01, 0xff, 0x4f, 0x63, 0x74, 0x20, 0x32, 0x32,
          0x20, 0x31, 0x30, 0x3a, 0x34, 0x36, 0x3a, 0x34,
          0x38
      };
      memcpy(buf, pdu_data, sizeof(pdu_data));
  
      res = coap_parse(&pdu, &buf[0], sizeof(pdu_data));
  
      TEST_ASSERT_EQUAL_INT(0, res);
      TEST_ASSERT_EQUAL_INT(COAP_CLASS_SUCCESS, coap_get_code_class(&pdu));
      TEST_ASSERT_EQUAL_INT(exp_tokenlen, coap_get_token_len(&pdu));
      TEST_ASSERT_EQUAL_INT(hdr_fixed_len + exp_tokenlen, coap_get_total_hdr_len(&pdu));
      TEST_ASSERT_EQUAL_INT(COAP_TYPE_NON, coap_get_type(&pdu));
      TEST_ASSERT_EQUAL_INT(strlen(exp_payload), pdu.payload_len);
  
      for (size_t i = 0; i < strlen(exp_payload); i++) {
          TEST_ASSERT_EQUAL_INT(exp_payload[i], pdu.payload[i]);
      }
  }
  
  /*
   * Helper for server_get tests below.
   * Request from libcoap example for gcoap_cli /cli/stats resource
   * Include 2-byte token and Uri-Host option.
   */
  static int _read_cli_stats_req(coap_pkt_t *pdu, uint8_t *buf)
  {
      uint8_t pdu_data[] = {
          0x52, 0x01, 0x20, 0xb6, 0x35, 0x61, 0x3d, 0x10,
          0x66, 0x65, 0x38, 0x30, 0x3a, 0x3a, 0x38, 0x63,
          0x32, 0x3a, 0x31, 0x33, 0x66, 0x66, 0x3a, 0x66,
          0x65, 0x63, 0x30, 0x3a, 0x35, 0x65, 0x31, 0x32,
          0x25, 0x74, 0x61, 0x70, 0x30, 0x83, 0x63, 0x6c,
          0x69, 0x05, 0x73, 0x74, 0x61, 0x74, 0x73
      };
      memcpy(buf, pdu_data, sizeof(pdu_data));
  
      return coap_parse(pdu, buf, sizeof(pdu_data));
  }
  
  /* Server GET request success case. Validate request example. */
  static void test_gcoap__server_get_req(void)
  {
      uint8_t buf[GCOAP_PDU_BUF_SIZE];
      coap_pkt_t pdu;
  
      int res = _read_cli_stats_req(&pdu, &buf[0]);
  
      TEST_ASSERT_EQUAL_INT(0, res);
      TEST_ASSERT_EQUAL_INT(COAP_METHOD_GET, coap_get_code(&pdu));
      TEST_ASSERT_EQUAL_INT(2, coap_get_token_len(&pdu));
      TEST_ASSERT_EQUAL_INT(4 + 2, coap_get_total_hdr_len(&pdu));
      TEST_ASSERT_EQUAL_INT(COAP_TYPE_NON, coap_get_type(&pdu));
      TEST_ASSERT_EQUAL_INT(0, pdu.payload_len);
      TEST_ASSERT_EQUAL_STRING("/cli/stats", (char *) &pdu.url[0]);
  }
  
  /*
   * Server GET response success case. Test writing response.
   * Response for libcoap example for gcoap_cli /cli/stats resource
   */
  static void test_gcoap__server_get_resp(void)
  {
      uint8_t buf[GCOAP_PDU_BUF_SIZE];
      coap_pkt_t pdu;
  
      /* read request */
      _read_cli_stats_req(&pdu, &buf[0]);
  
      /* generate response */
      gcoap_resp_init(&pdu, &buf[0], sizeof(buf), COAP_CODE_CONTENT);
      char resp_payload[]  = "2";
      memcpy(&pdu.payload[0], &resp_payload[0], strlen(resp_payload));
      ssize_t res = gcoap_finish(&pdu, strlen(resp_payload), COAP_FORMAT_TEXT);
  
      uint8_t resp_data[] = {
          0x52, 0x45, 0x20, 0xb6, 0x35, 0x61, 0xc0, 0xff,
          0x32
      };
  
      TEST_ASSERT_EQUAL_INT(COAP_CLASS_SUCCESS, coap_get_code_class(&pdu));
      TEST_ASSERT_EQUAL_INT(2, coap_get_token_len(&pdu));
      TEST_ASSERT_EQUAL_INT(4 + 2, coap_get_total_hdr_len(&pdu));
      TEST_ASSERT_EQUAL_INT(COAP_TYPE_NON, coap_get_type(&pdu));
      TEST_ASSERT_EQUAL_INT(strlen(resp_payload), pdu.payload_len);
      TEST_ASSERT_EQUAL_INT(sizeof(resp_data), res);
  
      for (size_t i = 0; i < strlen(resp_payload); i++) {
          TEST_ASSERT_EQUAL_INT(resp_payload[i], pdu.payload[i]);
      }
  }
  
  /*
   * Helper for server_con_* tests below.
   * Confirmable request from libcoap example for gcoap_cli /cli/stats resource.
   * Include 2-byte token.
   */
  static int _read_cli_stats_req_con(coap_pkt_t *pdu, uint8_t *buf)
  {
      uint8_t pdu_data[] = {
          0x42, 0x01, 0x8e, 0x03, 0x35, 0x61, 0xb3, 0x63,
          0x6c, 0x69, 0x05, 0x73, 0x74, 0x61, 0x74, 0x73
      };
      memcpy(buf, pdu_data, sizeof(pdu_data));
  
      return coap_parse(pdu, buf, sizeof(pdu_data));
  }
  
  /* Server CON GET request success case. Validate request is confirmable. */
  static void test_gcoap__server_con_req(void)
  {
      uint8_t buf[GCOAP_PDU_BUF_SIZE];
      coap_pkt_t pdu;
  
      int res = _read_cli_stats_req_con(&pdu, &buf[0]);
  
      TEST_ASSERT_EQUAL_INT(0, res);
      TEST_ASSERT_EQUAL_INT(COAP_METHOD_GET, coap_get_code(&pdu));
      TEST_ASSERT_EQUAL_INT(COAP_TYPE_CON, coap_get_type(&pdu));
  }
  
  /*
   * Server CON GET response success case. Test response is ACK.
   * Response for libcoap example for gcoap_cli /cli/stats resource
   */
  static void test_gcoap__server_con_resp(void)
  {
      uint8_t buf[GCOAP_PDU_BUF_SIZE];
      coap_pkt_t pdu;
  
      /* read request */
      _read_cli_stats_req_con(&pdu, &buf[0]);
  
      /* generate response */
      gcoap_resp_init(&pdu, &buf[0], sizeof(buf), COAP_CODE_CONTENT);
      char resp_payload[]  = "2";
      memcpy(&pdu.payload[0], &resp_payload[0], strlen(resp_payload));
      ssize_t res = gcoap_finish(&pdu, strlen(resp_payload), COAP_FORMAT_TEXT);
  
      uint8_t resp_data[] = {
          0x62, 0x45, 0x8e, 0x03, 0x35, 0x61, 0xc0, 0xff,
          0x30
      };
  
      TEST_ASSERT_EQUAL_INT(COAP_CLASS_SUCCESS, coap_get_code_class(&pdu));
      TEST_ASSERT_EQUAL_INT(COAP_TYPE_ACK, coap_get_type(&pdu));
      TEST_ASSERT_EQUAL_INT(sizeof(resp_data), res);
  }
  
  /*
   * Test the export of configured resources as CoRE link format string
   */
  static void test_gcoap__server_get_resource_list(void)
  {
      char res[128];
      int size = 0;
  
      gcoap_register_listener(&listener);
      gcoap_register_listener(&listener_second);
  
      size = gcoap_get_resource_list(NULL, 0, COAP_CT_LINK_FORMAT);
      TEST_ASSERT_EQUAL_INT(strlen(resource_list_str), size);
  
      res[0] = 'A';
      size = gcoap_get_resource_list(res, 0, COAP_CT_LINK_FORMAT);
      TEST_ASSERT_EQUAL_INT(0, size);
      TEST_ASSERT_EQUAL_INT((int)'A', (int)res[0]);
  
      size = gcoap_get_resource_list(res, 127, COAP_CT_LINK_FORMAT);
      res[size] = '\0';
      TEST_ASSERT_EQUAL_INT(strlen(resource_list_str), size);
      TEST_ASSERT_EQUAL_STRING(resource_list_str, (char *)res);
  }
  
  Test *tests_gcoap_tests(void)
  {
      EMB_UNIT_TESTFIXTURES(fixtures) {
          new_TestFixture(test_gcoap__client_get_req),
          new_TestFixture(test_gcoap__client_get_resp),
          new_TestFixture(test_gcoap__server_get_req),
          new_TestFixture(test_gcoap__server_get_resp),
          new_TestFixture(test_gcoap__server_con_req),
          new_TestFixture(test_gcoap__server_con_resp),
          new_TestFixture(test_gcoap__server_get_resource_list)
      };
  
      EMB_UNIT_TESTCALLER(gcoap_tests, NULL, NULL, fixtures);
  
      return (Test *)&gcoap_tests;
  }
  
  void tests_gcoap(void)
  {
      TESTS_RUN(tests_gcoap_tests());
  }
  /** @} */