1c87c5fbaSopenharmony_ci/* libcoap unit tests
2c87c5fbaSopenharmony_ci *
3c87c5fbaSopenharmony_ci * Copyright (C) 2013,2015,2022-2023 Olaf Bergmann <bergmann@tzi.org>
4c87c5fbaSopenharmony_ci *
5c87c5fbaSopenharmony_ci * SPDX-License-Identifier: BSD-2-Clause
6c87c5fbaSopenharmony_ci *
7c87c5fbaSopenharmony_ci * This file is part of the CoAP library libcoap. Please see
8c87c5fbaSopenharmony_ci * README for terms of use.
9c87c5fbaSopenharmony_ci */
10c87c5fbaSopenharmony_ci
11c87c5fbaSopenharmony_ci#include "test_common.h"
12c87c5fbaSopenharmony_ci#include "test_error_response.h"
13c87c5fbaSopenharmony_ci
14c87c5fbaSopenharmony_ci#include <assert.h>
15c87c5fbaSopenharmony_ci#include <stdio.h>
16c87c5fbaSopenharmony_ci#include <stdlib.h>
17c87c5fbaSopenharmony_ci#include <string.h>
18c87c5fbaSopenharmony_ci
19c87c5fbaSopenharmony_cistatic coap_pdu_t *pdu;          /* Holds the request PDU for most tests */
20c87c5fbaSopenharmony_cistatic coap_opt_filter_t opts;   /* option filter used for generating responses */
21c87c5fbaSopenharmony_ci
22c87c5fbaSopenharmony_ci/************************************************************************
23c87c5fbaSopenharmony_ci ** PDU decoder
24c87c5fbaSopenharmony_ci ************************************************************************/
25c87c5fbaSopenharmony_ci
26c87c5fbaSopenharmony_ci/* FIXME: handle COAP_ERROR_PHRASE_LENGTH == 0 */
27c87c5fbaSopenharmony_ci
28c87c5fbaSopenharmony_cistatic void
29c87c5fbaSopenharmony_cit_error_response1(void) {
30c87c5fbaSopenharmony_ci  uint8_t teststr[] = {
31c87c5fbaSopenharmony_ci    0x60, 0x80, 0x12, 0x34, 0xff, 'B', 'a', 'd',
32c87c5fbaSopenharmony_ci    ' ', 'R', 'e', 'q', 'u', 'e', 's', 't'
33c87c5fbaSopenharmony_ci  };
34c87c5fbaSopenharmony_ci  coap_pdu_t *response;
35c87c5fbaSopenharmony_ci
36c87c5fbaSopenharmony_ci  coap_pdu_clear(pdu, pdu->max_size);
37c87c5fbaSopenharmony_ci  pdu->type = COAP_MESSAGE_CON;
38c87c5fbaSopenharmony_ci  pdu->mid = 0x1234;
39c87c5fbaSopenharmony_ci
40c87c5fbaSopenharmony_ci  /* result = coap_add_token(pdu, 5, (unsigned char *)"token"); */
41c87c5fbaSopenharmony_ci  coap_option_filter_clear(&opts);
42c87c5fbaSopenharmony_ci  response = coap_new_error_response(pdu, COAP_RESPONSE_CODE(400), &opts);
43c87c5fbaSopenharmony_ci
44c87c5fbaSopenharmony_ci  CU_ASSERT_PTR_NOT_NULL(response);
45c87c5fbaSopenharmony_ci
46c87c5fbaSopenharmony_ci  CU_ASSERT(response->used_size == sizeof(teststr) - 4);
47c87c5fbaSopenharmony_ci  CU_ASSERT(response->type == COAP_MESSAGE_ACK);
48c87c5fbaSopenharmony_ci  CU_ASSERT(response->e_token_length == 0);
49c87c5fbaSopenharmony_ci  CU_ASSERT(response->code == 0x80);
50c87c5fbaSopenharmony_ci  CU_ASSERT(response->mid == 0x1234);
51c87c5fbaSopenharmony_ci  CU_ASSERT(coap_pdu_encode_header(response, COAP_PROTO_UDP) == 4);
52c87c5fbaSopenharmony_ci  CU_ASSERT(memcmp(response->token - 4, teststr, sizeof(teststr)) == 0);
53c87c5fbaSopenharmony_ci  coap_delete_pdu(response);
54c87c5fbaSopenharmony_ci}
55c87c5fbaSopenharmony_ci
56c87c5fbaSopenharmony_cistatic void
57c87c5fbaSopenharmony_cit_error_response2(void) {
58c87c5fbaSopenharmony_ci  uint8_t teststr[] = {
59c87c5fbaSopenharmony_ci    0x55, 0x84, 0x12, 0x34, 't', 'o', 'k', 'e',
60c87c5fbaSopenharmony_ci    'n', 0xff, 'N', 'o', 't', ' ', 'F', 'o',
61c87c5fbaSopenharmony_ci    'u', 'n', 'd'
62c87c5fbaSopenharmony_ci  };
63c87c5fbaSopenharmony_ci  coap_pdu_t *response;
64c87c5fbaSopenharmony_ci
65c87c5fbaSopenharmony_ci  coap_pdu_clear(pdu, pdu->max_size);
66c87c5fbaSopenharmony_ci  pdu->type = COAP_MESSAGE_NON;
67c87c5fbaSopenharmony_ci  pdu->mid = 0x1234;
68c87c5fbaSopenharmony_ci  coap_add_token(pdu, 5, (const uint8_t *)"token");
69c87c5fbaSopenharmony_ci  coap_add_option(pdu, COAP_OPTION_URI_HOST, 4, (const uint8_t *)"time");
70c87c5fbaSopenharmony_ci
71c87c5fbaSopenharmony_ci  coap_option_filter_clear(&opts);
72c87c5fbaSopenharmony_ci  response = coap_new_error_response(pdu, COAP_RESPONSE_CODE(404), &opts);
73c87c5fbaSopenharmony_ci
74c87c5fbaSopenharmony_ci  CU_ASSERT_PTR_NOT_NULL(response);
75c87c5fbaSopenharmony_ci
76c87c5fbaSopenharmony_ci  CU_ASSERT(response->used_size == sizeof(teststr) - 4);
77c87c5fbaSopenharmony_ci  CU_ASSERT(response->type == COAP_MESSAGE_NON);
78c87c5fbaSopenharmony_ci  CU_ASSERT(response->e_token_length == 5);
79c87c5fbaSopenharmony_ci  CU_ASSERT(response->code == 0x84);
80c87c5fbaSopenharmony_ci  CU_ASSERT(coap_pdu_encode_header(response, COAP_PROTO_UDP) == 4);
81c87c5fbaSopenharmony_ci  CU_ASSERT(memcmp(response->token - 4, teststr, sizeof(teststr)) == 0);
82c87c5fbaSopenharmony_ci  coap_delete_pdu(response);
83c87c5fbaSopenharmony_ci}
84c87c5fbaSopenharmony_ci
85c87c5fbaSopenharmony_cistatic void
86c87c5fbaSopenharmony_cit_error_response3(void) {
87c87c5fbaSopenharmony_ci  const uint8_t code = COAP_RESPONSE_CODE(402);
88c87c5fbaSopenharmony_ci  uint8_t teststr[] = {
89c87c5fbaSopenharmony_ci    0x65, code, 0x00, 0x00, 't', 'o', 'k', 'e',
90c87c5fbaSopenharmony_ci    'n', 0xd0, 0x0c, 0xff, 'B', 'a', 'd', ' ', 'O',
91c87c5fbaSopenharmony_ci    'p', 't', 'i', 'o', 'n'
92c87c5fbaSopenharmony_ci  };
93c87c5fbaSopenharmony_ci  coap_pdu_t *response;
94c87c5fbaSopenharmony_ci
95c87c5fbaSopenharmony_ci  coap_pdu_clear(pdu, pdu->max_size);
96c87c5fbaSopenharmony_ci  pdu->type = COAP_MESSAGE_CON;
97c87c5fbaSopenharmony_ci  coap_add_token(pdu, 5, (const uint8_t *)"token");
98c87c5fbaSopenharmony_ci  /* coap_add_option(pdu, COAP_OPTION_URI_HOST, 4, (const uint8_t *)"time"); */
99c87c5fbaSopenharmony_ci
100c87c5fbaSopenharmony_ci  /* unknown critical option 25 */
101c87c5fbaSopenharmony_ci  coap_add_option(pdu, 25, 0, NULL);
102c87c5fbaSopenharmony_ci
103c87c5fbaSopenharmony_ci  coap_option_filter_clear(&opts);
104c87c5fbaSopenharmony_ci  coap_option_filter_set(&opts, 25);
105c87c5fbaSopenharmony_ci  response = coap_new_error_response(pdu, code, &opts);
106c87c5fbaSopenharmony_ci
107c87c5fbaSopenharmony_ci  CU_ASSERT_PTR_NOT_NULL(response);
108c87c5fbaSopenharmony_ci
109c87c5fbaSopenharmony_ci  CU_ASSERT(response->used_size == sizeof(teststr) - 4);
110c87c5fbaSopenharmony_ci  CU_ASSERT(response->type == COAP_MESSAGE_ACK);
111c87c5fbaSopenharmony_ci  CU_ASSERT(response->e_token_length == 5);
112c87c5fbaSopenharmony_ci  CU_ASSERT(response->code == code);
113c87c5fbaSopenharmony_ci  CU_ASSERT(coap_pdu_encode_header(response, COAP_PROTO_UDP) == 4);
114c87c5fbaSopenharmony_ci  CU_ASSERT(memcmp(response->token - 4, teststr, sizeof(teststr)) == 0);
115c87c5fbaSopenharmony_ci  coap_delete_pdu(response);
116c87c5fbaSopenharmony_ci}
117c87c5fbaSopenharmony_ci
118c87c5fbaSopenharmony_cistatic void
119c87c5fbaSopenharmony_cit_error_response4(void) {
120c87c5fbaSopenharmony_ci  const uint8_t code = COAP_RESPONSE_CODE(402);
121c87c5fbaSopenharmony_ci  unsigned char optval[] = {
122c87c5fbaSopenharmony_ci    0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
123c87c5fbaSopenharmony_ci    0x08, 0x09, 0x0a, 0x0b
124c87c5fbaSopenharmony_ci  };
125c87c5fbaSopenharmony_ci  uint8_t teststr[] = {
126c87c5fbaSopenharmony_ci    0x65, code, 0x00, 0x00,  't',  'o',  'k',  'e',
127c87c5fbaSopenharmony_ci    'n', 0xdc, 0x0c, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
128c87c5fbaSopenharmony_ci    0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0xff,  'B',
129c87c5fbaSopenharmony_ci    'a',  'd',  ' ',  'O',  'p',  't',  'i',  'o',
130c87c5fbaSopenharmony_ci    'n'
131c87c5fbaSopenharmony_ci  };
132c87c5fbaSopenharmony_ci  coap_pdu_t *response;
133c87c5fbaSopenharmony_ci
134c87c5fbaSopenharmony_ci  coap_pdu_clear(pdu, pdu->max_size);
135c87c5fbaSopenharmony_ci  pdu->type = COAP_MESSAGE_CON;
136c87c5fbaSopenharmony_ci  coap_add_token(pdu, 5, (const uint8_t *)"token");
137c87c5fbaSopenharmony_ci  /* coap_add_option(pdu, COAP_OPTION_URI_HOST, 4, (const uint8_t *)"time"); */
138c87c5fbaSopenharmony_ci
139c87c5fbaSopenharmony_ci  /* unknown critical option 25 */
140c87c5fbaSopenharmony_ci  coap_add_option(pdu, 25, sizeof(optval), optval);
141c87c5fbaSopenharmony_ci
142c87c5fbaSopenharmony_ci  coap_option_filter_clear(&opts);
143c87c5fbaSopenharmony_ci  coap_option_filter_set(&opts, 25);
144c87c5fbaSopenharmony_ci  response = coap_new_error_response(pdu, code, &opts);
145c87c5fbaSopenharmony_ci
146c87c5fbaSopenharmony_ci  CU_ASSERT_PTR_NOT_NULL(response);
147c87c5fbaSopenharmony_ci
148c87c5fbaSopenharmony_ci  CU_ASSERT(response->used_size == sizeof(teststr) - 4);
149c87c5fbaSopenharmony_ci  CU_ASSERT(response->type == COAP_MESSAGE_ACK);
150c87c5fbaSopenharmony_ci  CU_ASSERT(response->e_token_length == 5);
151c87c5fbaSopenharmony_ci  CU_ASSERT(response->code == code);
152c87c5fbaSopenharmony_ci  CU_ASSERT(coap_pdu_encode_header(response, COAP_PROTO_UDP) == 4);
153c87c5fbaSopenharmony_ci  CU_ASSERT(memcmp(response->token - 4, teststr, sizeof(teststr)) == 0);
154c87c5fbaSopenharmony_ci  coap_delete_pdu(response);
155c87c5fbaSopenharmony_ci}
156c87c5fbaSopenharmony_ci
157c87c5fbaSopenharmony_cistatic void
158c87c5fbaSopenharmony_cit_error_response5(void) {
159c87c5fbaSopenharmony_ci  const uint8_t code = COAP_RESPONSE_CODE(402);
160c87c5fbaSopenharmony_ci  unsigned char optval[] = {
161c87c5fbaSopenharmony_ci    0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
162c87c5fbaSopenharmony_ci    0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
163c87c5fbaSopenharmony_ci    0x10, 0x11, 0x12
164c87c5fbaSopenharmony_ci  };
165c87c5fbaSopenharmony_ci  uint8_t teststr[] = {
166c87c5fbaSopenharmony_ci    0x65, code, 0x00, 0x00,  't',  'o',  'k',  'e',
167c87c5fbaSopenharmony_ci    'n', 0xdd, 0x0c, 0x06, 0x00, 0x01, 0x02, 0x03, 0x04,
168c87c5fbaSopenharmony_ci    0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c,
169c87c5fbaSopenharmony_ci    0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0xff,  'B',
170c87c5fbaSopenharmony_ci    'a',  'd',  ' ',  'O',  'p',  't',  'i',  'o',
171c87c5fbaSopenharmony_ci    'n'
172c87c5fbaSopenharmony_ci  };
173c87c5fbaSopenharmony_ci  coap_pdu_t *response;
174c87c5fbaSopenharmony_ci
175c87c5fbaSopenharmony_ci  coap_pdu_clear(pdu, pdu->max_size);
176c87c5fbaSopenharmony_ci  pdu->type = COAP_MESSAGE_CON;
177c87c5fbaSopenharmony_ci  coap_add_token(pdu, 5, (const uint8_t *)"token");
178c87c5fbaSopenharmony_ci  /* coap_add_option(pdu, COAP_OPTION_URI_HOST, 4, (const uint8_t *)"time"); */
179c87c5fbaSopenharmony_ci
180c87c5fbaSopenharmony_ci  /* unknown critical option 25 */
181c87c5fbaSopenharmony_ci  coap_add_option(pdu, 25, sizeof(optval), optval);
182c87c5fbaSopenharmony_ci
183c87c5fbaSopenharmony_ci  coap_option_filter_clear(&opts);
184c87c5fbaSopenharmony_ci  coap_option_filter_set(&opts, 25);
185c87c5fbaSopenharmony_ci  response = coap_new_error_response(pdu, code, &opts);
186c87c5fbaSopenharmony_ci
187c87c5fbaSopenharmony_ci  CU_ASSERT_PTR_NOT_NULL(response);
188c87c5fbaSopenharmony_ci
189c87c5fbaSopenharmony_ci  CU_ASSERT(response->used_size == sizeof(teststr) - 4);
190c87c5fbaSopenharmony_ci  CU_ASSERT(response->type == COAP_MESSAGE_ACK);
191c87c5fbaSopenharmony_ci  CU_ASSERT(response->e_token_length == 5);
192c87c5fbaSopenharmony_ci  CU_ASSERT(response->code == code);
193c87c5fbaSopenharmony_ci  CU_ASSERT(coap_pdu_encode_header(response, COAP_PROTO_UDP) == 4);
194c87c5fbaSopenharmony_ci  CU_ASSERT(memcmp(response->token - 4, teststr, sizeof(teststr)) == 0);
195c87c5fbaSopenharmony_ci  coap_delete_pdu(response);
196c87c5fbaSopenharmony_ci}
197c87c5fbaSopenharmony_ci
198c87c5fbaSopenharmony_cistatic void
199c87c5fbaSopenharmony_cit_error_response6(void) {
200c87c5fbaSopenharmony_ci  const uint8_t code = COAP_RESPONSE_CODE(402);
201c87c5fbaSopenharmony_ci  unsigned char optval[] = {
202c87c5fbaSopenharmony_ci    0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
203c87c5fbaSopenharmony_ci    0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
204c87c5fbaSopenharmony_ci    0x10, 0x11, 0x12
205c87c5fbaSopenharmony_ci  };
206c87c5fbaSopenharmony_ci  uint8_t teststr[] = {
207c87c5fbaSopenharmony_ci    0x65, code, 0x00, 0x00,  't',  'o',  'k',  'e',
208c87c5fbaSopenharmony_ci    'n', 0xdd, 0x0a, 0x06, 0x00, 0x01, 0x02, 0x03,
209c87c5fbaSopenharmony_ci    0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
210c87c5fbaSopenharmony_ci    0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0xff,
211c87c5fbaSopenharmony_ci    'B',  'a',  'd',  ' ',  'O',  'p',  't',  'i',
212c87c5fbaSopenharmony_ci    'o',  'n'
213c87c5fbaSopenharmony_ci  };
214c87c5fbaSopenharmony_ci  coap_pdu_t *response;
215c87c5fbaSopenharmony_ci
216c87c5fbaSopenharmony_ci  coap_pdu_clear(pdu, pdu->max_size);
217c87c5fbaSopenharmony_ci  pdu->type = COAP_MESSAGE_CON;
218c87c5fbaSopenharmony_ci  coap_add_token(pdu, 5, (const uint8_t *)"token");
219c87c5fbaSopenharmony_ci  /* coap_add_option(pdu, COAP_OPTION_URI_HOST, 4, (const uint8_t *)"time"); */
220c87c5fbaSopenharmony_ci
221c87c5fbaSopenharmony_ci  /* unknown critical option 23 */
222c87c5fbaSopenharmony_ci  coap_add_option(pdu, 23, sizeof(optval), optval);
223c87c5fbaSopenharmony_ci
224c87c5fbaSopenharmony_ci  coap_option_filter_clear(&opts);
225c87c5fbaSopenharmony_ci  coap_option_filter_set(&opts, 23);
226c87c5fbaSopenharmony_ci  response = coap_new_error_response(pdu, code, &opts);
227c87c5fbaSopenharmony_ci
228c87c5fbaSopenharmony_ci  CU_ASSERT_PTR_NOT_NULL(response);
229c87c5fbaSopenharmony_ci
230c87c5fbaSopenharmony_ci  CU_ASSERT(response->used_size == sizeof(teststr) - 4);
231c87c5fbaSopenharmony_ci  CU_ASSERT(response->type == COAP_MESSAGE_ACK);
232c87c5fbaSopenharmony_ci  CU_ASSERT(response->e_token_length == 5);
233c87c5fbaSopenharmony_ci  CU_ASSERT(response->code == code);
234c87c5fbaSopenharmony_ci  CU_ASSERT(coap_pdu_encode_header(response, COAP_PROTO_UDP) == 4);
235c87c5fbaSopenharmony_ci  CU_ASSERT(memcmp(response->token - 4, teststr, sizeof(teststr)) == 0);
236c87c5fbaSopenharmony_ci  coap_delete_pdu(response);
237c87c5fbaSopenharmony_ci}
238c87c5fbaSopenharmony_ci
239c87c5fbaSopenharmony_cistatic void
240c87c5fbaSopenharmony_cit_error_response7(void) {
241c87c5fbaSopenharmony_ci  const uint8_t code = COAP_RESPONSE_CODE(402);
242c87c5fbaSopenharmony_ci  unsigned char optval[] = {
243c87c5fbaSopenharmony_ci    0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
244c87c5fbaSopenharmony_ci    0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
245c87c5fbaSopenharmony_ci    0x10, 0x11, 0x12
246c87c5fbaSopenharmony_ci  };
247c87c5fbaSopenharmony_ci  uint8_t teststr[] = {
248c87c5fbaSopenharmony_ci    0x65, code, 0x00, 0x00,  't',  'o',  'k',  'e',
249c87c5fbaSopenharmony_ci    'n', 0xdd, 0x0a, 0x06, 0x00, 0x01, 0x02, 0x03,
250c87c5fbaSopenharmony_ci    0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
251c87c5fbaSopenharmony_ci    0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0xff,
252c87c5fbaSopenharmony_ci    'B',  'a',  'd',  ' ',  'O',  'p',  't',  'i',
253c87c5fbaSopenharmony_ci    'o',  'n'
254c87c5fbaSopenharmony_ci  };
255c87c5fbaSopenharmony_ci  coap_pdu_t *response;
256c87c5fbaSopenharmony_ci
257c87c5fbaSopenharmony_ci  coap_pdu_clear(pdu, pdu->max_size);
258c87c5fbaSopenharmony_ci  pdu->type = COAP_MESSAGE_CON;
259c87c5fbaSopenharmony_ci  coap_add_token(pdu, 5, (const uint8_t *)"token");
260c87c5fbaSopenharmony_ci  /* known option 11 */
261c87c5fbaSopenharmony_ci  coap_add_option(pdu, 11, 4, (const uint8_t *)"time");
262c87c5fbaSopenharmony_ci
263c87c5fbaSopenharmony_ci  /* unknown critical option 23 */
264c87c5fbaSopenharmony_ci  coap_add_option(pdu, 23, sizeof(optval), optval);
265c87c5fbaSopenharmony_ci
266c87c5fbaSopenharmony_ci  coap_option_filter_clear(&opts);
267c87c5fbaSopenharmony_ci  coap_option_filter_set(&opts, 23);
268c87c5fbaSopenharmony_ci  response = coap_new_error_response(pdu, code, &opts);
269c87c5fbaSopenharmony_ci
270c87c5fbaSopenharmony_ci  CU_ASSERT_PTR_NOT_NULL(response);
271c87c5fbaSopenharmony_ci
272c87c5fbaSopenharmony_ci  CU_ASSERT(response->used_size == sizeof(teststr) - 4);
273c87c5fbaSopenharmony_ci  CU_ASSERT(response->type == COAP_MESSAGE_ACK);
274c87c5fbaSopenharmony_ci  CU_ASSERT(response->e_token_length == 5);
275c87c5fbaSopenharmony_ci  CU_ASSERT(response->code == code);
276c87c5fbaSopenharmony_ci  CU_ASSERT(coap_pdu_encode_header(response, COAP_PROTO_UDP) == 4);
277c87c5fbaSopenharmony_ci  CU_ASSERT(memcmp(response->token - 4, teststr, sizeof(teststr)) == 0);
278c87c5fbaSopenharmony_ci  coap_delete_pdu(response);
279c87c5fbaSopenharmony_ci}
280c87c5fbaSopenharmony_ci
281c87c5fbaSopenharmony_cistatic void
282c87c5fbaSopenharmony_cit_error_response8(void) {
283c87c5fbaSopenharmony_ci  const uint8_t code = COAP_RESPONSE_CODE(503);
284c87c5fbaSopenharmony_ci  uint8_t teststr[] = {
285c87c5fbaSopenharmony_ci    0x65, code, 0x00, 0x00,  't',  'o',  'k',  'e',
286c87c5fbaSopenharmony_ci    'n', 0xe0, 0x02, 0xdc, 0xd0, 0x00, 0xff,  'S',
287c87c5fbaSopenharmony_ci    'e',  'r',  'v',  'i',  'c',  'e',  ' ',  'U',
288c87c5fbaSopenharmony_ci    'n',  'a',  'v',  'a',  'i',  'l',  'a',  'b',
289c87c5fbaSopenharmony_ci    'l',  'e'
290c87c5fbaSopenharmony_ci  };
291c87c5fbaSopenharmony_ci  coap_pdu_t *response;
292c87c5fbaSopenharmony_ci
293c87c5fbaSopenharmony_ci  coap_pdu_clear(pdu, pdu->max_size);
294c87c5fbaSopenharmony_ci  pdu->type = COAP_MESSAGE_CON;
295c87c5fbaSopenharmony_ci  coap_add_token(pdu, 5, (const uint8_t *)"token");
296c87c5fbaSopenharmony_ci  /* known option 1000 */
297c87c5fbaSopenharmony_ci  coap_add_option(pdu, 1000, 0, NULL);
298c87c5fbaSopenharmony_ci
299c87c5fbaSopenharmony_ci  /* unknown options 1001 and 1014 */
300c87c5fbaSopenharmony_ci  coap_add_option(pdu, 1001, 0, NULL);
301c87c5fbaSopenharmony_ci  coap_add_option(pdu, 1014, 0, NULL);
302c87c5fbaSopenharmony_ci
303c87c5fbaSopenharmony_ci  /* known option 2000 */
304c87c5fbaSopenharmony_ci  coap_add_option(pdu, 2000, 0, NULL);
305c87c5fbaSopenharmony_ci
306c87c5fbaSopenharmony_ci  coap_option_filter_clear(&opts);
307c87c5fbaSopenharmony_ci  coap_option_filter_set(&opts, 1001);
308c87c5fbaSopenharmony_ci  coap_option_filter_set(&opts, 1014);
309c87c5fbaSopenharmony_ci  response = coap_new_error_response(pdu, code, &opts);
310c87c5fbaSopenharmony_ci
311c87c5fbaSopenharmony_ci  CU_ASSERT_PTR_NOT_NULL(response);
312c87c5fbaSopenharmony_ci
313c87c5fbaSopenharmony_ci  CU_ASSERT(response->used_size == sizeof(teststr) - 4);
314c87c5fbaSopenharmony_ci  CU_ASSERT(response->type == COAP_MESSAGE_ACK);
315c87c5fbaSopenharmony_ci  CU_ASSERT(response->e_token_length == 5);
316c87c5fbaSopenharmony_ci  CU_ASSERT(response->code == code);
317c87c5fbaSopenharmony_ci  CU_ASSERT(coap_pdu_encode_header(response, COAP_PROTO_UDP) == 4);
318c87c5fbaSopenharmony_ci  CU_ASSERT(memcmp(response->token - 4, teststr, sizeof(teststr)) == 0);
319c87c5fbaSopenharmony_ci  coap_delete_pdu(response);
320c87c5fbaSopenharmony_ci}
321c87c5fbaSopenharmony_ci
322c87c5fbaSopenharmony_cistatic int
323c87c5fbaSopenharmony_cit_error_response_tests_create(void) {
324c87c5fbaSopenharmony_ci  pdu = coap_pdu_init(0, 0, 0, COAP_DEFAULT_MTU);
325c87c5fbaSopenharmony_ci
326c87c5fbaSopenharmony_ci  return pdu == NULL;
327c87c5fbaSopenharmony_ci}
328c87c5fbaSopenharmony_ci
329c87c5fbaSopenharmony_cistatic int
330c87c5fbaSopenharmony_cit_error_response_tests_remove(void) {
331c87c5fbaSopenharmony_ci  coap_delete_pdu(pdu);
332c87c5fbaSopenharmony_ci  return 0;
333c87c5fbaSopenharmony_ci}
334c87c5fbaSopenharmony_ci
335c87c5fbaSopenharmony_ciCU_pSuite
336c87c5fbaSopenharmony_cit_init_error_response_tests(void) {
337c87c5fbaSopenharmony_ci  CU_pSuite suite[1];
338c87c5fbaSopenharmony_ci
339c87c5fbaSopenharmony_ci  suite[0] = CU_add_suite("error response generator",
340c87c5fbaSopenharmony_ci                          t_error_response_tests_create,
341c87c5fbaSopenharmony_ci                          t_error_response_tests_remove);
342c87c5fbaSopenharmony_ci  if (!suite[0]) {                        /* signal error */
343c87c5fbaSopenharmony_ci    fprintf(stderr, "W: cannot add error response generator test suite (%s)\n",
344c87c5fbaSopenharmony_ci            CU_get_error_msg());
345c87c5fbaSopenharmony_ci
346c87c5fbaSopenharmony_ci    return NULL;
347c87c5fbaSopenharmony_ci  }
348c87c5fbaSopenharmony_ci
349c87c5fbaSopenharmony_ci#define ERROR_RESPONSE_TEST(s,t)                                        \
350c87c5fbaSopenharmony_ci  if (!CU_ADD_TEST(s,t)) {                                                \
351c87c5fbaSopenharmony_ci    fprintf(stderr, "W: cannot add error response generator test (%s)\n", \
352c87c5fbaSopenharmony_ci            CU_get_error_msg());                                        \
353c87c5fbaSopenharmony_ci  }
354c87c5fbaSopenharmony_ci
355c87c5fbaSopenharmony_ci  ERROR_RESPONSE_TEST(suite[0], t_error_response1);
356c87c5fbaSopenharmony_ci  ERROR_RESPONSE_TEST(suite[0], t_error_response2);
357c87c5fbaSopenharmony_ci  ERROR_RESPONSE_TEST(suite[0], t_error_response3);
358c87c5fbaSopenharmony_ci  ERROR_RESPONSE_TEST(suite[0], t_error_response4);
359c87c5fbaSopenharmony_ci  ERROR_RESPONSE_TEST(suite[0], t_error_response5);
360c87c5fbaSopenharmony_ci  ERROR_RESPONSE_TEST(suite[0], t_error_response6);
361c87c5fbaSopenharmony_ci  ERROR_RESPONSE_TEST(suite[0], t_error_response7);
362c87c5fbaSopenharmony_ci  ERROR_RESPONSE_TEST(suite[0], t_error_response8);
363c87c5fbaSopenharmony_ci
364c87c5fbaSopenharmony_ci  return suite[0];
365c87c5fbaSopenharmony_ci}
366