1 /*
2 Copyright (c) 2022 Eriptic Technologies. See the COPYRIGHT
3 file at the top-level directory of this distribution.
4
5 Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 option. This file may not be copied, modified, or distributed
9 except according to those terms.
10 */
11
12 #include <zephyr/zephyr.h>
13 #include <zephyr/ztest.h>
14 #include <string.h>
15
16 #include "oscore/option.h"
17 #include "common/byte_array.h"
18
19 #define GET_ARRAY_SIZE(_array) (sizeof(_array) / sizeof(_array[0]))
20
uri_path_create_and_expect(struct o_coap_option * options,uint32_t options_size,uint8_t * uri_path,uint32_t * uri_path_size,enum err expected_result)21 static void uri_path_create_and_expect(struct o_coap_option *options, uint32_t options_size, uint8_t * uri_path, uint32_t * uri_path_size, enum err expected_result)
22 {
23 PRINTF("uri_path_create; expected result = %d\n", expected_result);
24 enum err result = uri_path_create(options, options_size, uri_path, uri_path_size);
25 zassert_equal(expected_result, result, "unexpected result: %d", result);
26 }
27
uri_path_create_and_compare(struct o_coap_option * options,uint32_t options_size,uint8_t * uri_path,uint32_t * uri_path_size,enum err expected_result,uint8_t * expected_uri_path,uint32_t expected_uri_path_size)28 static void uri_path_create_and_compare(struct o_coap_option *options, uint32_t options_size, uint8_t * uri_path, uint32_t * uri_path_size, enum err expected_result, uint8_t * expected_uri_path, uint32_t expected_uri_path_size)
29 {
30 uri_path_create_and_expect(options, options_size, uri_path, uri_path_size, expected_result);
31 zassert_equal(expected_uri_path_size, *uri_path_size, "unexpected output size: %d", *uri_path_size);
32 zassert_mem_equal(expected_uri_path, uri_path, expected_uri_path_size, "");
33 *uri_path_size = OSCORE_MAX_URI_PATH_LEN; //restore valid buffer size (cleanup for next calls).
34 }
35
get_observe_value_and_compare(struct o_coap_option * options,uint8_t options_cnt,struct byte_array * output,bool expected_result,struct byte_array * expected_output)36 static void get_observe_value_and_compare(struct o_coap_option *options, uint8_t options_cnt, struct byte_array * output, bool expected_result, struct byte_array * expected_output)
37 {
38 PRINTF("get_observe_value; expected result = %d\n", expected_result);
39 bool result = get_observe_value(options, options_cnt, output);
40 zassert_equal(expected_result, result, "unexpected result: %d", result);
41
42 if (NULL != expected_output)
43 {
44 zassert_equal(true, array_equals(output, expected_output), "");
45 }
46 }
47
t400_is_class_e(void)48 void t400_is_class_e(void)
49 {
50 enum o_num not_e_opt_nums[] = { URI_HOST, URI_PORT, OSCORE, PROXY_URI,
51 PROXY_SCHEME };
52
53 uint32_t len = sizeof(not_e_opt_nums) / sizeof(not_e_opt_nums[0]);
54 for (uint32_t i = 0; i < len; i++) {
55 zassert_equal(is_class_e(not_e_opt_nums[i]), false, "");
56 }
57 }
58
t401_cache_echo_val(void)59 void t401_cache_echo_val(void)
60 {
61 struct byte_array empty = BYTE_ARRAY_INIT(NULL, 0);
62 enum err r;
63
64 struct o_coap_option options[] = {
65 { .delta = 2, .len = 0, .value = NULL, .option_number = 2 },
66 { .delta = 2, .len = 0, .value = NULL, .option_number = 4 },
67 { .delta = 3, .len = 0, .value = NULL, .option_number = 7 },
68 { .delta = 5, .len = 0, .value = NULL, .option_number = ECHO }
69 };
70
71 /*successful caching*/
72 r = cache_echo_val(&empty, (struct o_coap_option *)&options, 4);
73 zassert_equal(r, ok, "Error in cache_echo_val. r: %d", r);
74
75 /*unsuccessful caching */
76 r = cache_echo_val(&empty, (struct o_coap_option *)&options, 3);
77 zassert_equal(r, no_echo_option, "Error in cache_echo_val. r: %d", r);
78 }
79
t402_echo_val_is_fresh(void)80 void t402_echo_val_is_fresh(void)
81 {
82 enum err r;
83
84 uint8_t cache_val_buf[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
85 0x06, 0x07, 0x08, 0x09, 0x10, 0x11 };
86
87 struct byte_array cache_val =
88 BYTE_ARRAY_INIT(cache_val_buf, sizeof(cache_val_buf));
89
90 uint8_t decrypted_payload_buf[] = { 0x01 }; /*code only*/
91 struct byte_array decrypted_payload = BYTE_ARRAY_INIT(
92 decrypted_payload_buf, sizeof(decrypted_payload_buf));
93
94 /*test no ECHO option*/
95 r = echo_val_is_fresh(&cache_val, &decrypted_payload);
96 zassert_equal(r, no_echo_option, "Error in echo_val_is_fresh. r: %d",
97 r);
98
99 /*test ECHO option mismatch*/
100 uint8_t decrypted_payload_buf_mismatch[] = {
101 0x81, 0xDC, 0xEF, 0x00, 0x01, 0x02, 0x03, 0x04,
102 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x12
103 }; /*wrong last byte of the ECHO option*/
104 struct byte_array decrypted_payload_mismatch =
105 BYTE_ARRAY_INIT(decrypted_payload_buf_mismatch,
106 sizeof(decrypted_payload_buf_mismatch));
107
108 r = echo_val_is_fresh(&cache_val, &decrypted_payload_mismatch);
109 zassert_equal(r, echo_val_mismatch, "Error in echo_val_is_fresh. r: %d",
110 r);
111 }
112
t403_uri_path_create(void)113 void t403_uri_path_create(void)
114 {
115 struct o_coap_option default_options[] = {
116 { .option_number = IF_NONE_MATCH },
117 { .option_number = URI_PATH, .value = "path", .len = 4 },
118 { .option_number = URI_PATH, .value = "to", .len = 2 },
119 { .option_number = OSCORE },
120 { .option_number = URI_PATH, .value = "rsc", .len = 3 },
121 };
122 uint32_t default_size = GET_ARRAY_SIZE(default_options);
123 uint8_t expected_default_path[] = "path/to/rsc";
124
125 uint8_t output_buffer[OSCORE_MAX_URI_PATH_LEN];
126 uint32_t output_buffer_size = sizeof(output_buffer);
127
128 /* Test null pointers. */
129 uri_path_create_and_expect(NULL, default_size, output_buffer, &output_buffer_size, wrong_parameter);
130 uri_path_create_and_expect(default_options, default_size, NULL, &output_buffer_size, wrong_parameter);
131 uri_path_create_and_expect(default_options, default_size, output_buffer, NULL, wrong_parameter);
132
133 /* Test too small output buffer. */
134 uint32_t wrong_output_size = 2; //should fail while adding first element
135 uri_path_create_and_expect(default_options, default_size, output_buffer, &wrong_output_size, buffer_to_small);
136 wrong_output_size = 4; //should fail while adding '/' after first element
137 uri_path_create_and_expect(default_options, default_size, output_buffer, &wrong_output_size, buffer_to_small);
138 wrong_output_size = 10; //should fail while adding last element
139 uri_path_create_and_expect(default_options, default_size, output_buffer, &wrong_output_size, buffer_to_small);
140
141 /* Wrong option should fail. */
142 struct o_coap_option wrong_option[] = {
143 { .option_number = MAX_AGE },
144 { .option_number = URI_PATH, .value = "path", .len = 4 },
145 { .option_number = URI_PATH, .value = NULL, .len = 2 },
146 { .option_number = OSCORE },
147 { .option_number = URI_PATH, .value = "rsc", .len = 3 },
148 };
149 uint32_t wrong_size = GET_ARRAY_SIZE(wrong_option);
150 uri_path_create_and_expect(wrong_option, wrong_size, output_buffer, &output_buffer_size, oscore_wrong_uri_path);
151
152 /* Valid data should pass. */
153 uri_path_create_and_compare(default_options, default_size, output_buffer, &output_buffer_size, ok, expected_default_path, strlen(expected_default_path));
154
155 /* Empty option should pass. */
156 struct o_coap_option empty_option[] = {
157 { .option_number = MAX_AGE },
158 { .option_number = URI_PATH, .value = "path", .len = 4 },
159 { .option_number = URI_PATH, .value = NULL, .len = 0 },
160 { .option_number = OSCORE },
161 { .option_number = URI_PATH, .value = "rsc", .len = 3 },
162 };
163 uint32_t empty_size = GET_ARRAY_SIZE(empty_option);
164 uint8_t expected_empty_path[] = "path//rsc";
165 uri_path_create_and_compare(empty_option, empty_size, output_buffer, &output_buffer_size, ok, expected_empty_path, strlen(expected_empty_path));
166
167 /* No URI-Path option should pass. */
168 struct o_coap_option no_options[] = {
169 { .option_number = MAX_AGE },
170 { .option_number = OSCORE },
171 };
172 uint32_t no_options_size = GET_ARRAY_SIZE(no_options);
173 uint8_t expected_no_options_path[] = "/";
174 uri_path_create_and_compare(no_options, no_options_size, output_buffer, &output_buffer_size, ok, expected_no_options_path, strlen(expected_no_options_path));
175 }
176
t404_get_observe_value(void)177 void t404_get_observe_value(void)
178 {
179 struct o_coap_option options_default[] = {
180 { .option_number = IF_NONE_MATCH },
181 { .option_number = OSCORE },
182 { .option_number = OBSERVE, .value = "\x00", .len = 1 },
183 };
184 struct o_coap_option options_long_observe[] = {
185 { .option_number = IF_NONE_MATCH },
186 { .option_number = OSCORE },
187 { .option_number = OBSERVE, .value = "\x00\x01\x02\x04", .len = 4 },
188 };
189 struct o_coap_option options_empty_observe[] = {
190 { .option_number = IF_NONE_MATCH },
191 { .option_number = OSCORE },
192 { .option_number = OBSERVE },
193 };
194 struct o_coap_option options_no_observe[] = {
195 { .option_number = IF_NONE_MATCH },
196 { .option_number = OSCORE },
197 };
198
199 /* Test null pointers. */
200 struct byte_array output = BYTE_ARRAY_INIT(NULL, 0);
201 get_observe_value_and_compare(NULL, 0, &output, false, NULL);
202 get_observe_value_and_compare(options_default, 0, NULL, false, NULL);
203
204 /* Test different valid values of the OBSERVE option. */
205 struct byte_array expected_default = BYTE_ARRAY_INIT("\x00", 1);
206 struct byte_array expected_long_observe = BYTE_ARRAY_INIT("\x00\x01\x02\x04", 4);
207 struct byte_array expected_empty_observe = BYTE_ARRAY_INIT(NULL, 0);
208 get_observe_value_and_compare(options_default, GET_ARRAY_SIZE(options_default), &output, true, &expected_default);
209 get_observe_value_and_compare(options_long_observe, GET_ARRAY_SIZE(options_long_observe), &output, true, &expected_long_observe);
210 get_observe_value_and_compare(options_empty_observe, GET_ARRAY_SIZE(options_empty_observe), &output, true, &expected_empty_observe);
211
212 /* Test non-existing OBSERVE option. */
213 get_observe_value_and_compare(options_no_observe, GET_ARRAY_SIZE(options_no_observe), &output, false, NULL);
214 }
215