Skip to content

Commit 14354cf

Browse files
committed
add test for #41
1 parent 2ef037f commit 14354cf

File tree

6 files changed

+108
-42
lines changed

6 files changed

+108
-42
lines changed

inc/common/unit_test.h

+2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ enum err oscore_option_generate(struct byte_array *piv, struct byte_array *kid,
5252
enum err derive(struct common_context *cc, struct byte_array *id,
5353
enum derive_type type, struct byte_array *out);
5454

55+
void set_observe_val(struct o_coap_option *options, uint8_t options_cnt,
56+
struct byte_array *piv);
5557
#else
5658
#define STATIC static
5759
#endif

src/oscore/oscore2coap.c

+34-25
Original file line numberDiff line numberDiff line change
@@ -191,15 +191,43 @@ options_reorder(struct o_coap_option *U_options, uint8_t U_options_cnt,
191191
return ok;
192192
}
193193

194+
/**
195+
* @brief Set the value of the inner observe option to the three least
196+
* significant bytes of the partial IV, see RFC8613 4.1.3.5.2
197+
*
198+
* @param options array of all inner options
199+
* @param options_cnt number of options
200+
* @param piv PIV
201+
*/
202+
STATIC void set_observe_val(struct o_coap_option *options,
203+
uint8_t options_cnt, struct byte_array *piv)
204+
{
205+
uint16_t len;
206+
if (piv->len > 3) {
207+
len = 3;
208+
} else {
209+
len = (uint16_t)piv->len;
210+
}
211+
212+
for (uint8_t i = 0; i < options_cnt; i++) {
213+
if (options[i].option_number == OBSERVE) {
214+
options[i].value = piv->ptr;
215+
options[i].len = len;
216+
}
217+
}
218+
}
219+
194220
/**
195221
* @brief Generate CoAP packet from OSCORE packet
196222
* @param decrypted_payload: decrypted OSCORE payload, which contains code, E-options and original unprotected CoAP payload
197223
* @param oscore_pkt: input OSCORE packet
224+
* @param piv: PIV to be used only in the case of observe notification
198225
* @param out: pointer to output CoAP packet
199226
* @return
200227
*/
201228
static inline enum err o_coap_pkg_generate(struct byte_array *decrypted_payload,
202229
struct o_coap_packet *oscore_pkt,
230+
struct byte_array *piv,
203231
struct o_coap_packet *out)
204232
{
205233
uint8_t code = 0;
@@ -212,6 +240,10 @@ static inline enum err o_coap_pkg_generate(struct byte_array *decrypted_payload,
212240
&E_options_cnt,
213241
&unprotected_o_coap_payload));
214242

243+
/*set inner observe option value to the three least significant bytes
244+
of the PIV*/
245+
set_observe_val(&E_options, E_options_cnt, piv);
246+
215247
/* Copy each items from OSCORE packet to CoAP packet */
216248
/* Header */
217249
out->header.ver = oscore_pkt->header.ver;
@@ -286,24 +318,6 @@ decrypt_wrapper(struct byte_array *ciphertext, struct byte_array *plaintext,
286318
return ok;
287319
}
288320

289-
static inline void set_observe_val(struct o_coap_option *options,
290-
uint8_t options_cnt, struct byte_array *piv)
291-
{
292-
uint16_t len;
293-
if (piv->len > 3) {
294-
len = 3;
295-
} else {
296-
len = (uint16_t)piv->len;
297-
}
298-
299-
for (uint8_t i = 0; i < options_cnt; i++) {
300-
if (options[i].option_number == OBSERVE) {
301-
options[i].value = piv->ptr;
302-
options[i].len = len;
303-
}
304-
}
305-
}
306-
307321
enum err oscore2coap(uint8_t *buf_in, uint32_t buf_in_len, uint8_t *buf_out,
308322
uint32_t *buf_out_len, struct context *c)
309323
{
@@ -419,12 +433,6 @@ enum err oscore2coap(uint8_t *buf_in, uint32_t buf_in_len, uint8_t *buf_out,
419433
&c->rc.notification_num,
420434
&c->rc.notification_num_initialized,
421435
&oscore_option.piv));
422-
423-
/*set outer observe option value to the three
424-
least significant bytes of the PIV*/
425-
set_observe_val(oscore_packet.options,
426-
oscore_packet.options_cnt,
427-
&oscore_option.piv);
428436
} else {
429437
/*Notification without PIV received -- Currently not supported*/
430438
return not_supported_feature; //LCOV_EXCL_LINE
@@ -445,7 +453,8 @@ enum err oscore2coap(uint8_t *buf_in, uint32_t buf_in_len, uint8_t *buf_out,
445453

446454
/* Generate corresponding CoAP packet */
447455
struct o_coap_packet o_coap_packet;
448-
TRY(o_coap_pkg_generate(&plaintext, &oscore_packet, &o_coap_packet));
456+
TRY(o_coap_pkg_generate(&plaintext, &oscore_packet, &oscore_option.piv,
457+
&o_coap_packet));
449458

450459
/*Convert to byte string*/
451460
return coap_serialize(&o_coap_packet, buf_out, buf_out_len);

test/main.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ void test_main(void)
133133
ztest_unit_test(test_initiator_responder_interaction2));
134134

135135
/* OSCORE test-vector tests */
136+
// ztest_test_suite(oscore_tests, ztest_unit_test(t304_set_observe_val));
137+
136138
ztest_test_suite(
137139
oscore_tests,
138140
ztest_unit_test(t1_oscore_client_request_response),
@@ -165,12 +167,12 @@ void test_main(void)
165167
ztest_unit_test(t301_oscore_option_parser_wrong_n),
166168
ztest_unit_test(t302_oscore_option_parser_no_kid),
167169
ztest_unit_test(t303_options_reorder),
170+
ztest_unit_test(t304_set_observe_val),
168171
ztest_unit_test(t400_is_class_e),
169172
ztest_unit_test(t401_cache_echo_val),
170173
ztest_unit_test(t402_echo_val_is_fresh),
171174
ztest_unit_test(t500_oscore_context_init_corner_cases),
172-
ztest_unit_test(t501_piv2ssn),
173-
ztest_unit_test(t502_ssn2piv),
175+
ztest_unit_test(t501_piv2ssn), ztest_unit_test(t502_ssn2piv),
174176
ztest_unit_test(t503_derive_corner_case),
175177
ztest_unit_test(t504_context_freshness),
176178
ztest_unit_test(t600_server_replay_init_test),

test/oscore_integration_tests/oscore_integration_tests.c

+15-15
Original file line numberDiff line numberDiff line change
@@ -368,14 +368,14 @@ void t8_oscore_server_response_simple_ack(void)
368368
* @brief This function test the behavior of a server and a client in a typical
369369
* observe exchange as depicted:
370370
*
371-
* client server
372-
* --------- ---------
373-
* | |
371+
* client server
372+
* --------- ---------
373+
* | |
374374
* |------registration------------>|
375-
* | |
375+
* | |
376376
* |<-----notification1------------|
377-
* rejected msg | x---replayed notification1----|
378-
* | |
377+
* rejected msg | x---replayed notification1----|
378+
* | |
379379
*
380380
* See as well Appendix A.1. in RFC7641
381381
*/
@@ -579,7 +579,7 @@ void t9_oscore_client_server_observe(void)
579579

580580
zassert_equal(r, ok, "Error in oscore2coap!");
581581
uint8_t EXPECTED_CONVERTED_COAP[] = {
582-
0x61, 0x45, 0x00, 0x00, 0x4A, 0x60
582+
0x61, 0x45, 0x00, 0x00, 0x4A, 0x61, 0x00
583583
};
584584
zassert_mem_equal__(&ser_conv_coap_pkt, EXPECTED_CONVERTED_COAP,
585585
sizeof(EXPECTED_CONVERTED_COAP),
@@ -600,23 +600,23 @@ void t9_oscore_client_server_observe(void)
600600
* @brief This function test the behavior of a server and a client after
601601
* reboot of the server and the rejection of a replayed request
602602
*
603-
* client server
604-
* --------- ---------
605-
* | |
603+
* client server
604+
* --------- ---------
605+
* | |
606606
* 1) |------request----------------------------->|
607607
* |<-----response with ECHO option (ECHO1)----|
608-
* | |
608+
* | |
609609
* 2) |------new request without ECHO opt-------->|
610610
* |<-----response with new ECHO option (ECHO2)|
611-
* | |
611+
* | |
612612
* 3) |------request with old ECHO option-(ECHO1)>|
613613
* |<-----response with new ECHO option (ECHO3)|
614-
* | |
614+
* | |
615615
* 4) |------request with ECHO option-(ECHO3)---->|
616616
* |<-----response-----------------------------|
617-
* | |
617+
* | |
618618
* 5) |------request----------------------------->|
619-
* |------replayed request----------X | rejected message
619+
* |------replayed request----------X | rejected message
620620
*
621621
* See as RFC8613 Appendix B1.2 and RFC9175
622622
*

test/oscore_tests.h

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ void t300_oscore_option_parser_no_piv(void);
4040
void t301_oscore_option_parser_wrong_n(void);
4141
void t302_oscore_option_parser_no_kid(void);
4242
void t303_options_reorder(void);
43+
void t304_set_observe_val(void);
4344

4445
void t400_is_class_e(void);
4546
void t401_cache_echo_val(void);

test/oscore_unit_tests/unit_test_oscore2coap.c

+52
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,55 @@ void t303_options_reorder(void)
186186
"wrong option_number");
187187
}
188188
}
189+
190+
void t304_set_observe_val(void)
191+
{
192+
uint32_t i;
193+
194+
//test not having a observe option in the list of options
195+
// -> nothing is happening
196+
struct o_coap_option options[] = {
197+
{ .delta = 2, .len = 0, .value = NULL, .option_number = 2 },
198+
{ .delta = 2, .len = 0, .value = NULL, .option_number = 4 },
199+
};
200+
uint8_t PIV1[] = { 0x00 };
201+
202+
struct byte_array piv1 = BYTE_ARRAY_INIT(PIV1, sizeof(PIV1));
203+
204+
set_observe_val(options, sizeof(options) / sizeof(options[0]), &piv1);
205+
206+
for (i = 0; i < sizeof(options) / sizeof(options[0]); i++) {
207+
zassert_equal(options[i].delta, 2, "wrong delta");
208+
zassert_equal(options[i].len, 0, "wrong len");
209+
zassert_equal(options[i].value, NULL, "wrong value");
210+
zassert_equal(options[i].option_number, 2 + i * 2,
211+
"wrong option_number");
212+
}
213+
214+
//test having a single observe option and piv of 2 byte
215+
uint8_t PIV2[] = { 0xaa, 0xaa };
216+
struct byte_array piv2 = BYTE_ARRAY_INIT(PIV2, sizeof(PIV2));
217+
struct o_coap_option option2 = {
218+
.delta = 6, .len = 0, .value = NULL, .option_number = OBSERVE
219+
};
220+
221+
set_observe_val(&option2, 1, &piv2);
222+
zassert_equal(option2.delta, 6, "wrong delta");
223+
zassert_equal(option2.len, 2, "wrong len");
224+
zassert_equal(option2.option_number, 6, "wrong option_number");
225+
zassert_mem_equal__(option2.value, piv2.ptr, piv2.len, "wrong value");
226+
227+
//test having a single observe option and piv of 4 byte
228+
uint8_t PIV3[] = { 0xaa, 0xaa, 0xaa, 0xaa };
229+
struct byte_array piv3 = BYTE_ARRAY_INIT(PIV3, sizeof(PIV3));
230+
struct o_coap_option option3 = {
231+
.delta = 6, .len = 0, .value = NULL, .option_number = OBSERVE
232+
};
233+
234+
set_observe_val(&option3, 1, &piv3);
235+
zassert_equal(option3.delta, 6, "wrong delta");
236+
zassert_equal(option3.len, 3, "wrong len");
237+
zassert_equal(option3.option_number, 6, "wrong option_number");
238+
zassert_mem_equal__(option3.value, piv3.ptr, option3.len,
239+
"wrong value");
240+
}

0 commit comments

Comments
 (0)