From 9d228feff0e95f8de89ad1c84ed1421b5a0a4118 Mon Sep 17 00:00:00 2001 From: Jon Shallow Date: Thu, 15 May 2025 16:10:32 +0100 Subject: [PATCH] testdriver.c: unit tests failures do not error on exit Unfortunately tests/testdriver always exited with 0, even if there was a test suite error. This meant that CI tests were not picking up any issues. tests/testdriver now exits with a value of 1 if there is a test suite issue. Errors corrected in/for the test suite. --- src/coap_net.c | 12 ++++++++++++ tests/test_error_response.c | 27 ++++++++++----------------- tests/testdriver.c | 5 ++++- 3 files changed, 26 insertions(+), 18 deletions(-) diff --git a/src/coap_net.c b/src/coap_net.c index e3ec6529..bf1d2d0d 100644 --- a/src/coap_net.c +++ b/src/coap_net.c @@ -3092,7 +3092,19 @@ coap_new_error_response(const coap_pdu_t *request, coap_pdu_code_t code, first = 0; } coap_add_data(response, (size_t)strlen(buf), (const uint8_t *)buf); + } else if (opts && opts->mask) { + coap_opt_t *option; + + /* copy all options */ + coap_option_iterator_init(request, &opt_iter, opts); + while ((option = coap_option_next(&opt_iter))) { + coap_add_option_internal(response, opt_iter.number, + coap_opt_length(option), + coap_opt_value(option)); + } #if COAP_ERROR_PHRASE_LENGTH > 0 + if (phrase) + coap_add_data(response, (size_t)strlen(phrase), (const uint8_t *)phrase); } else { /* note that diagnostic messages do not need a Content-Format option. */ if (phrase) diff --git a/tests/test_error_response.c b/tests/test_error_response.c index ab36e449..59fdb9c2 100644 --- a/tests/test_error_response.c +++ b/tests/test_error_response.c @@ -91,8 +91,8 @@ t_error_response3(void) { const uint8_t code = COAP_RESPONSE_CODE(402); uint8_t teststr[] = { 0x65, code, 0x00, 0x00, 't', 'o', 'k', 'e', - 'n', 0xd0, 0x0c, 0xff, 'B', 'a', 'd', ' ', 'O', - 'p', 't', 'i', 'o', 'n' + 'n', 0xff, 'B', 'a', 'd', ' ', 'O', + 'p', 't', 'i', 'o', 'n', ' ', '2', '5' }; coap_pdu_t *response; @@ -128,10 +128,9 @@ t_error_response4(void) { }; uint8_t teststr[] = { 0x65, code, 0x00, 0x00, 't', 'o', 'k', 'e', - 'n', 0xdc, 0x0c, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, - 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0xff, 'B', + 'n', 0xff, 'B', 'a', 'd', ' ', 'O', 'p', 't', 'i', 'o', - 'n' + 'n', ' ', '2', '5' }; coap_pdu_t *response; @@ -168,11 +167,9 @@ t_error_response5(void) { }; uint8_t teststr[] = { 0x65, code, 0x00, 0x00, 't', 'o', 'k', 'e', - 'n', 0xdd, 0x0c, 0x06, 0x00, 0x01, 0x02, 0x03, 0x04, - 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, - 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0xff, 'B', + 'n', 0xff, 'B', 'a', 'd', ' ', 'O', 'p', 't', 'i', 'o', - 'n' + 'n', ' ', '2', '5' }; coap_pdu_t *response; @@ -209,11 +206,9 @@ t_error_response6(void) { }; uint8_t teststr[] = { 0x65, code, 0x00, 0x00, 't', 'o', 'k', 'e', - 'n', 0xdd, 0x0a, 0x06, 0x00, 0x01, 0x02, 0x03, - 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, - 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0xff, + 'n', 0xff, 'B', 'a', 'd', ' ', 'O', 'p', 't', 'i', - 'o', 'n' + 'o', 'n', ' ', '2', '3' }; coap_pdu_t *response; @@ -250,11 +245,9 @@ t_error_response7(void) { }; uint8_t teststr[] = { 0x65, code, 0x00, 0x00, 't', 'o', 'k', 'e', - 'n', 0xdd, 0x0a, 0x06, 0x00, 0x01, 0x02, 0x03, - 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, - 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0xff, + 'n', 0xff, 'B', 'a', 'd', ' ', 'O', 'p', 't', 'i', - 'o', 'n' + 'o', 'n', ' ', '2', '3' }; coap_pdu_t *response; diff --git a/tests/testdriver.c b/tests/testdriver.c index 03511780..24b5b4a4 100644 --- a/tests/testdriver.c +++ b/tests/testdriver.c @@ -31,6 +31,7 @@ int main(int argc COAP_UNUSED, char **argv COAP_UNUSED) { CU_ErrorCode result; CU_BasicRunMode run_mode = CU_BRM_VERBOSE; + unsigned int failed; if (CU_initialize_registry() != CUE_SUCCESS) { fprintf(stderr, "E: test framework initialization failed\n"); @@ -57,9 +58,11 @@ main(int argc COAP_UNUSED, char **argv COAP_UNUSED) { CU_basic_set_mode(run_mode); result = CU_basic_run_tests(); - + failed = CU_get_number_of_failures(); CU_cleanup_registry(); coap_cleanup(); + if (failed) + exit(1); return result; }