LCOV - code coverage report
Current view: top level - test/oscore_unit_tests - unit_test_option.c (source / functions) Hit Total Coverage
Test: lcov.info Lines: 28 90 31.1 %
Date: 2024-09-16 20:15:30 Functions: 3 8 37.5 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 2 4 50.0 %

           Branch data     Line data    Source code
       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/kernel.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                 :            : 
      21                 :          0 : 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                 :          0 :         PRINTF("uri_path_create; expected result = %d\n", expected_result);
      24                 :          0 :         enum err result = uri_path_create(options, options_size, uri_path, uri_path_size);
      25                 :          0 :         zassert_equal(expected_result, result, "unexpected result: %d", result);
      26                 :          0 : }
      27                 :            : 
      28                 :          0 : 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                 :          0 :         uri_path_create_and_expect(options, options_size, uri_path, uri_path_size, expected_result);
      31                 :          0 :         zassert_equal(expected_uri_path_size, *uri_path_size, "unexpected output size: %d", *uri_path_size);
      32                 :          0 :         zassert_mem_equal(expected_uri_path, uri_path, expected_uri_path_size, "");
      33                 :          0 :         *uri_path_size = OSCORE_MAX_URI_PATH_LEN; //restore valid buffer size (cleanup for next calls).
      34                 :          0 : }
      35                 :            : 
      36                 :          0 : 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                 :          0 :         PRINTF("get_observe_value; expected result = %d\n", expected_result);
      39                 :          0 :         bool result = get_observe_value(options, options_cnt, output);
      40                 :          0 :         zassert_equal(expected_result, result, "unexpected result: %d", result);
      41                 :            : 
      42         [ #  # ]:          0 :         if (NULL != expected_output)
      43                 :            :         {
      44                 :          0 :                 zassert_equal(true, array_equals(output, expected_output), "");
      45                 :            :         }
      46                 :          0 : }
      47                 :            : 
      48                 :          1 : void t400_is_class_e(void)
      49                 :            : {
      50                 :          1 :         enum o_num not_e_opt_nums[] = { URI_HOST, URI_PORT, OSCORE, PROXY_URI,
      51                 :            :                                         PROXY_SCHEME };
      52                 :            : 
      53                 :          1 :         uint32_t len = sizeof(not_e_opt_nums) / sizeof(not_e_opt_nums[0]);
      54         [ +  + ]:          6 :         for (uint32_t i = 0; i < len; i++) {
      55                 :          5 :                 zassert_equal(is_class_e(not_e_opt_nums[i]), false, "");
      56                 :            :         }
      57                 :          1 : }
      58                 :            : 
      59                 :          1 : void t401_cache_echo_val(void)
      60                 :            : {
      61                 :          1 :         struct byte_array empty = BYTE_ARRAY_INIT(NULL, 0);
      62                 :          1 :         enum err r;
      63                 :            : 
      64                 :          1 :         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                 :          1 :         r = cache_echo_val(&empty, (struct o_coap_option *)&options, 4);
      73                 :          1 :         zassert_equal(r, ok, "Error in cache_echo_val. r: %d", r);
      74                 :            : 
      75                 :            :         /*unsuccessful caching */
      76                 :          1 :         r = cache_echo_val(&empty, (struct o_coap_option *)&options, 3);
      77                 :          1 :         zassert_equal(r, no_echo_option, "Error in cache_echo_val. r: %d", r);
      78                 :          1 : }
      79                 :            : 
      80                 :          1 : void t402_echo_val_is_fresh(void)
      81                 :            : {
      82                 :          1 :         enum err r;
      83                 :            : 
      84                 :          1 :         uint8_t cache_val_buf[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
      85                 :            :                                     0x06, 0x07, 0x08, 0x09, 0x10, 0x11 };
      86                 :            : 
      87                 :          1 :         struct byte_array cache_val =
      88                 :            :                 BYTE_ARRAY_INIT(cache_val_buf, sizeof(cache_val_buf));
      89                 :            : 
      90                 :          1 :         uint8_t decrypted_payload_buf[] = { 0x01 }; /*code only*/
      91                 :          1 :         struct byte_array decrypted_payload = BYTE_ARRAY_INIT(
      92                 :            :                 decrypted_payload_buf, sizeof(decrypted_payload_buf));
      93                 :            : 
      94                 :            :         /*test no ECHO option*/
      95                 :          1 :         r = echo_val_is_fresh(&cache_val, &decrypted_payload);
      96                 :          1 :         zassert_equal(r, no_echo_option, "Error in echo_val_is_fresh. r: %d",
      97                 :            :                       r);
      98                 :            : 
      99                 :            :         /*test ECHO option mismatch*/
     100                 :          1 :         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                 :          1 :         struct byte_array decrypted_payload_mismatch =
     105                 :            :                 BYTE_ARRAY_INIT(decrypted_payload_buf_mismatch,
     106                 :            :                                 sizeof(decrypted_payload_buf_mismatch));
     107                 :            : 
     108                 :          1 :         r = echo_val_is_fresh(&cache_val, &decrypted_payload_mismatch);
     109                 :          1 :         zassert_equal(r, echo_val_mismatch, "Error in echo_val_is_fresh. r: %d",
     110                 :            :                       r);
     111                 :          1 : }
     112                 :            : 
     113                 :          0 : void t403_uri_path_create(void)
     114                 :            : {
     115                 :          0 :         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                 :          0 :         uint32_t default_size = GET_ARRAY_SIZE(default_options);
     123                 :          0 :         uint8_t expected_default_path[] = "path/to/rsc";
     124                 :            :         
     125                 :          0 :         uint8_t output_buffer[OSCORE_MAX_URI_PATH_LEN];
     126                 :          0 :         uint32_t output_buffer_size = sizeof(output_buffer);
     127                 :            : 
     128                 :            :         /* Test null pointers. */
     129                 :          0 :         uri_path_create_and_expect(NULL, default_size, output_buffer, &output_buffer_size, wrong_parameter);
     130                 :          0 :         uri_path_create_and_expect(default_options, default_size, NULL, &output_buffer_size, wrong_parameter);
     131                 :          0 :         uri_path_create_and_expect(default_options, default_size, output_buffer, NULL, wrong_parameter);
     132                 :            : 
     133                 :            :         /* Test too small output buffer. */
     134                 :          0 :         uint32_t wrong_output_size = 2; //should fail while adding first element
     135                 :          0 :         uri_path_create_and_expect(default_options, default_size, output_buffer, &wrong_output_size, buffer_to_small);
     136                 :          0 :         wrong_output_size = 4; //should fail while adding '/' after first element
     137                 :          0 :         uri_path_create_and_expect(default_options, default_size, output_buffer, &wrong_output_size, buffer_to_small);
     138                 :          0 :         wrong_output_size = 10; //should fail while adding last element
     139                 :          0 :         uri_path_create_and_expect(default_options, default_size, output_buffer, &wrong_output_size, buffer_to_small);
     140                 :            : 
     141                 :            :         /* Wrong option should fail. */
     142                 :          0 :         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                 :          0 :         uint32_t wrong_size = GET_ARRAY_SIZE(wrong_option);
     150                 :          0 :         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                 :          0 :         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                 :          0 :         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                 :          0 :         uint32_t empty_size = GET_ARRAY_SIZE(empty_option);
     164                 :          0 :         uint8_t expected_empty_path[] = "path//rsc";
     165                 :          0 :         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                 :          0 :         struct o_coap_option no_options[] = {   
     169                 :            :                 { .option_number = MAX_AGE },
     170                 :            :                 { .option_number = OSCORE },
     171                 :            :         };
     172                 :          0 :         uint32_t no_options_size = GET_ARRAY_SIZE(no_options);
     173                 :          0 :         uint8_t expected_no_options_path[] = "/";
     174                 :          0 :         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                 :          0 : }
     176                 :            : 
     177                 :          0 : void t404_get_observe_value(void)
     178                 :            : {
     179                 :          0 :         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                 :          0 :         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                 :          0 :         struct o_coap_option options_empty_observe[] = {
     190                 :            :                 { .option_number = IF_NONE_MATCH },
     191                 :            :                 { .option_number = OSCORE },
     192                 :            :                 { .option_number = OBSERVE },
     193                 :            :         };
     194                 :          0 :         struct o_coap_option options_no_observe[] = {
     195                 :            :                 { .option_number = IF_NONE_MATCH },
     196                 :            :                 { .option_number = OSCORE },
     197                 :            :         };
     198                 :            : 
     199                 :            :         /* Test null pointers. */
     200                 :          0 :         struct byte_array output = BYTE_ARRAY_INIT(NULL, 0);
     201                 :          0 :         get_observe_value_and_compare(NULL, 0, &output, false, NULL);
     202                 :          0 :         get_observe_value_and_compare(options_default, 0, NULL, false, NULL);
     203                 :            : 
     204                 :            :         /* Test different valid values of the OBSERVE option. */
     205                 :          0 :         struct byte_array expected_default = BYTE_ARRAY_INIT("\x00", 1);
     206                 :          0 :         struct byte_array expected_long_observe = BYTE_ARRAY_INIT("\x00\x01\x02\x04", 4);
     207                 :          0 :         struct byte_array expected_empty_observe = BYTE_ARRAY_INIT(NULL, 0);
     208                 :          0 :         get_observe_value_and_compare(options_default, GET_ARRAY_SIZE(options_default), &output, true, &expected_default);
     209                 :          0 :         get_observe_value_and_compare(options_long_observe, GET_ARRAY_SIZE(options_long_observe), &output, true, &expected_long_observe);
     210                 :          0 :         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                 :          0 :         get_observe_value_and_compare(options_no_observe, GET_ARRAY_SIZE(options_no_observe), &output, false, NULL);
     214                 :          0 : }

Generated by: LCOV version 1.14