LCOV - code coverage report
Current view: top level - test/oscore_unit_tests - unit_test_coap2oscore.c (source / functions) Coverage Total Hit
Test: lcov.info Lines: 100.0 % 189 189
Test Date: 2026-03-12 12:01:18 Functions: 100.0 % 9 9
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 100.0 % 4 4

             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                 :             : #include <zephyr/kernel.h>
      12                 :             : #include <zephyr/ztest.h>
      13                 :             : 
      14                 :             : #include "common/unit_test.h"
      15                 :             : #include "common/oscore_edhoc_error.h"
      16                 :             : #include "common/print_util.h"
      17                 :             : #include "common/byte_array.h"
      18                 :             : 
      19                 :             : #include "oscore/oscore_coap.h"
      20                 :             : #include "oscore/option.h"
      21                 :             : 
      22                 :             : /* Use this function for debugging to print an array of options*/
      23                 :           6 : static void print_options(struct o_coap_option *opt, uint8_t opt_cnt)
      24                 :             : {
      25                 :           6 :         uint8_t i;
      26         [ +  + ]:          26 :         for (i = 0; i < opt_cnt; i++) {
      27                 :          20 :                 PRINTF("option_number: %d\n", opt[i].option_number);
      28                 :          20 :                 PRINT_ARRAY("value", opt[i].value, opt[i].len);
      29                 :          20 :                 PRINTF("delta: %d\n\n", opt[i].delta);
      30                 :             :         }
      31                 :           6 : }
      32                 :             : 
      33                 :           6 : static void assert_options(struct o_coap_option *opt,
      34                 :             :                            struct o_coap_option *opt_expected, uint8_t opt_cnt,
      35                 :             :                            uint8_t opt_cnt_expected)
      36                 :             : {
      37                 :           6 :         zassert_equal(opt_cnt, opt_cnt_expected, "wrong option count");
      38                 :             : 
      39         [ +  + ]:          26 :         for (uint8_t i = 0; i < opt_cnt; i++) {
      40                 :          20 :                 zassert_equal(opt[i].delta, opt_expected[i].delta,
      41                 :             :                               "wrong delta");
      42                 :          20 :                 zassert_equal(opt[i].len, opt_expected[i].len, "wrong length");
      43                 :          20 :                 zassert_equal(opt[i].option_number,
      44                 :             :                               opt_expected[i].option_number, "option_number");
      45                 :             : 
      46                 :          20 :                 zassert_mem_equal__(opt[i].value, opt_expected[i].value,
      47                 :             :                                     opt[i].len, "wrong value");
      48                 :             :         }
      49                 :           6 : }
      50                 :             : 
      51                 :             : /**
      52                 :             :  * @brief   Tests the function inner_outer_option_split without options
      53                 :             :  *          with that require special processing.
      54                 :             :  */
      55                 :           1 : void t100_inner_outer_option_split__no_special_options(void)
      56                 :             : {
      57                 :           1 :         enum err r;
      58                 :             : 
      59                 :           1 :         struct o_coap_header header = {
      60                 :             :                 .ver = 1,
      61                 :             :                 .type = TYPE_CON,
      62                 :             :                 .TKL = 0,
      63                 :             :                 .code = CODE_REQ_POST,
      64                 :             :                 .MID = 0x0,
      65                 :             :         };
      66                 :             : 
      67                 :           1 :         struct o_coap_packet coap_pkt = {
      68                 :             :                 .header = header,
      69                 :             :                 .token = NULL,
      70                 :             :                 .options_cnt = 6,
      71                 :             :                 .options = {
      72                 :             :                 /*If-Match (opt num 1, E)*/
      73                 :             :                 { .delta = 1,
      74                 :             :                                .len = 0,
      75                 :             :                                .value = NULL,
      76                 :             :                                .option_number = IF_MATCH },
      77                 :             :                 /*Etag (opt num 4, E)*/
      78                 :             :                             { .delta = 3,
      79                 :             :                                .len = 0,
      80                 :             :                                .value = NULL,
      81                 :             :                                .option_number = ETAG },
      82                 :             :                 /*Content-Format (opt num 12, E)*/
      83                 :             :                             { .delta = 8,
      84                 :             :                                .len = 0,
      85                 :             :                                .value = NULL,
      86                 :             :                                .option_number = CONTENT_FORMAT },
      87                 :             :                 /*Proxy-Uri (opt num 35, U)*/
      88                 :             :                             { .delta = 23,
      89                 :             :                                .len = 0,
      90                 :             :                                .value = NULL,
      91                 :             :                                .option_number = PROXY_URI },
      92                 :             :                                 /*Custom option 1 (opt num 0xFE00, E)*/
      93                 :             :                             { .delta = 64989,
      94                 :             :                                .len = 0,
      95                 :             :                                .value = NULL,
      96                 :             :                                .option_number = 0xFE00 },
      97                 :             :                                 /*Custom option 1 (opt num 0xFE10, E)*/
      98                 :             :                             { .delta = 16,
      99                 :             :                                .len = 0,
     100                 :             :                                .value = NULL,
     101                 :             :                                .option_number = 0xFE10 }
     102                 :             :                         },
     103                 :             :                 .payload.len = 0,
     104                 :             :                 .payload.ptr = NULL,
     105                 :             :         };
     106                 :             : 
     107                 :           1 :         struct o_coap_option inner_options[6];
     108                 :           1 :         struct o_coap_option outer_options[6];
     109                 :           1 :         memset(inner_options, 0, sizeof(inner_options));
     110                 :           1 :         memset(outer_options, 0, sizeof(outer_options));
     111                 :           1 :         uint16_t inner_options_len = 0;
     112                 :           1 :         uint8_t inner_options_cnt = 0;
     113                 :           1 :         uint8_t outer_options_cnt = 0;
     114                 :           1 :         uint8_t expected_inner_options_cnt;
     115                 :           1 :         uint8_t expected_outer_options_cnt;
     116                 :             : 
     117                 :           1 :         struct o_coap_option expected_inner_options[] = {
     118                 :             :                 /*If-Match (opt num 1, E)*/
     119                 :             :                 { .delta = 1,
     120                 :             :                   .len = 0,
     121                 :             :                   .value = NULL,
     122                 :             :                   .option_number = IF_MATCH },
     123                 :             :                 /*Etag (opt num 4, E)*/
     124                 :             :                 { .delta = 3, .len = 0, .value = NULL, .option_number = ETAG },
     125                 :             :                 /*Content-Format (opt num 12, E)*/
     126                 :             :                 { .delta = 8,
     127                 :             :                   .len = 0,
     128                 :             :                   .value = NULL,
     129                 :             :                   .option_number = CONTENT_FORMAT },
     130                 :             :                 /*Custom option 1 (opt num 0xFE00, E)*/
     131                 :             :                 { .delta = 65012,
     132                 :             :                         .len = 0,
     133                 :             :                         .value = NULL,
     134                 :             :                         .option_number = 0xFE00 },
     135                 :             :                 /*Custom option 1 (opt num 0xFE10, E)*/
     136                 :             :                 { .delta = 16,
     137                 :             :                         .len = 0,
     138                 :             :                         .value = NULL,
     139                 :             :                         .option_number = 0xFE10 }
     140                 :             :         };
     141                 :           1 :         expected_inner_options_cnt = sizeof(expected_inner_options) /
     142                 :             :                                      sizeof(expected_inner_options[0]);
     143                 :             : 
     144                 :           1 :         struct o_coap_option expected_outer_options[] = {
     145                 :             :                 /*Proxy-Uri (opt num 35, U)*/
     146                 :             :                 { .delta = 35,
     147                 :             :                   .len = 0,
     148                 :             :                   .value = NULL,
     149                 :             :                   .option_number = PROXY_URI }
     150                 :             :         };
     151                 :           1 :         expected_outer_options_cnt = sizeof(expected_outer_options) /
     152                 :             :                                      sizeof(expected_outer_options[0]);
     153                 :             : 
     154                 :           1 :         r = inner_outer_option_split(&coap_pkt, inner_options,
     155                 :             :                                      &inner_options_cnt, &inner_options_len,
     156                 :             :                                      outer_options, &outer_options_cnt);
     157                 :             : 
     158                 :           1 :         PRINT_MSG("\ninner options\n");
     159                 :           1 :         print_options(inner_options, inner_options_cnt);
     160                 :           1 :         PRINT_MSG("\nouter options\n");
     161                 :           1 :         print_options(outer_options, outer_options_cnt);
     162                 :             : 
     163                 :           1 :         zassert_equal(r, ok, "Error in inner_outer_option_split. r: %d", r);
     164                 :             : 
     165                 :           1 :         assert_options(inner_options, expected_inner_options, inner_options_cnt,
     166                 :             :                        expected_inner_options_cnt);
     167                 :             : 
     168                 :           1 :         assert_options(outer_options, expected_outer_options, outer_options_cnt,
     169                 :             :                        expected_outer_options_cnt);
     170                 :           1 : }
     171                 :             : 
     172                 :             : /**
     173                 :             :  * @brief   Tests the function inner_outer_option_split with Observe option
     174                 :             :  *          indicating a notification. This function tests the behavior of
     175                 :             :  *          the server preparing a response
     176                 :             :  */
     177                 :           1 : void t101_inner_outer_option_split__with_observe_notification(void)
     178                 :             : {
     179                 :           1 :         enum err r;
     180                 :             : 
     181                 :           1 :         struct o_coap_header header = {
     182                 :             :                 .ver = 1,
     183                 :             :                 .type = TYPE_ACK,
     184                 :             :                 .TKL = 0,
     185                 :             :                 .code = CODE_RESP_CONTENT,
     186                 :             :                 .MID = 0x0,
     187                 :             :         };
     188                 :             : 
     189                 :             :         /*The Observe option value is a sequence number in notifications*/
     190                 :           1 :         uint8_t observe_val[] = { 0x12 };
     191                 :             : 
     192                 :           1 :         struct o_coap_packet coap_pkt = {
     193                 :             :                 .header = header,
     194                 :             :                 .token = NULL,
     195                 :             :                 .options_cnt = 7,
     196                 :             :                 .options = {
     197                 :             :                 /*If-Match (opt num 1, E)*/
     198                 :             :                 { .delta = 1,
     199                 :             :                                .len = 0,
     200                 :             :                                .value = NULL,
     201                 :             :                                .option_number = IF_MATCH },
     202                 :             :                 /*Etag (opt num 4, E)*/
     203                 :             :                             { .delta = 3,
     204                 :             :                                .len = 0,
     205                 :             :                                .value = NULL,
     206                 :             :                                .option_number = ETAG },
     207                 :             :                 /*Observe (opt num 6, EU)*/
     208                 :             :                             { .delta = 2,
     209                 :             :                                .len = sizeof(observe_val),
     210                 :             :                                .value = observe_val,
     211                 :             :                                .option_number = OBSERVE },
     212                 :             :                 /*Content-Format (opt num 12, E)*/
     213                 :             :                             { .delta = 6,
     214                 :             :                                .len = 0,
     215                 :             :                                .value = NULL,
     216                 :             :                                .option_number = CONTENT_FORMAT } ,
     217                 :             :                 /*Proxy-Uri (opt num 35, U)*/
     218                 :             :                             { .delta = 23,
     219                 :             :                                .len = 0,
     220                 :             :                                .value = NULL,
     221                 :             :                                .option_number = PROXY_URI },
     222                 :             :                                 /*Custom option 1 (opt num 0xFE00, E)*/
     223                 :             :                                 { .delta = 64989,
     224                 :             :                                         .len = 0,
     225                 :             :                                         .value = NULL,
     226                 :             :                                         .option_number = 0xFE00 },
     227                 :             :                                 /*Custom option 1 (opt num 0xFE10, E)*/
     228                 :             :                                 { .delta = 16,
     229                 :             :                                         .len = 0,
     230                 :             :                                         .value = NULL,
     231                 :             :                                         .option_number = 0xFE10 }
     232                 :             :                    },
     233                 :             :                 .payload.len = 0,
     234                 :             :                 .payload.ptr = NULL,
     235                 :             :         };
     236                 :             : 
     237                 :           1 :         struct o_coap_option inner_options[7];
     238                 :           1 :         struct o_coap_option outer_options[7];
     239                 :           1 :         memset(inner_options, 0, sizeof(inner_options));
     240                 :           1 :         memset(outer_options, 0, sizeof(outer_options));
     241                 :           1 :         uint16_t inner_options_len = 0;
     242                 :           1 :         uint8_t inner_options_cnt = 0;
     243                 :           1 :         uint8_t outer_options_cnt = 0;
     244                 :             : 
     245                 :           1 :         struct o_coap_option expected_inner_options[] = {
     246                 :             :                 /*If-Match (opt num 1, E)*/
     247                 :             :                 { .delta = 1,
     248                 :             :                   .len = 0,
     249                 :             :                   .value = NULL,
     250                 :             :                   .option_number = IF_MATCH },
     251                 :             :                 /*Etag (opt num 4, E)*/
     252                 :             :                 { .delta = 3, .len = 0, .value = NULL, .option_number = ETAG },
     253                 :             :                 /*Observe(opt num 6): The inner observe option shall have
     254                 :             :         no value, see 4.1.3.5.2 in RFC8613*/
     255                 :             :                 { .delta = 2,
     256                 :             :                   .len = 0,
     257                 :             :                   .value = NULL,
     258                 :             :                   .option_number = OBSERVE },
     259                 :             :                 /*Content-Format (opt num 12, E)*/
     260                 :             :                 { .delta = 6,
     261                 :             :                   .len = 0,
     262                 :             :                   .value = NULL,
     263                 :             :                   .option_number = CONTENT_FORMAT },
     264                 :             :                 /*Custom option 1 (opt num 0xFE00, E)*/
     265                 :             :                 { .delta = 65012,
     266                 :             :                         .len = 0,
     267                 :             :                         .value = NULL,
     268                 :             :                         .option_number = 0xFE00 },
     269                 :             :                 /*Custom option 1 (opt num 0xFE10, E)*/
     270                 :             :                 { .delta = 16,
     271                 :             :                         .len = 0,
     272                 :             :                         .value = NULL,
     273                 :             :                         .option_number = 0xFE10 }
     274                 :             : 
     275                 :             :         };
     276                 :           1 :         uint8_t expected_inner_options_cnt =
     277                 :             :                 sizeof(expected_inner_options) / sizeof(struct o_coap_option);
     278                 :             : 
     279                 :           1 :         struct o_coap_option expected_outer_options[2];
     280                 :           1 :         memset(expected_outer_options, 0, sizeof(expected_outer_options));
     281                 :             :         /*Observe(opt num 6): The outer observe option may have
     282                 :             :         a value as in the original coap packet, see 4.1.3.5.2 in RFC8613*/
     283                 :           1 :         expected_outer_options[0].delta = 6;
     284                 :           1 :         expected_outer_options[0].len = sizeof(observe_val);
     285                 :           1 :         expected_outer_options[0].value = observe_val;
     286                 :           1 :         expected_outer_options[0].option_number = OBSERVE;
     287                 :             : 
     288                 :             :         /*Proxy-Uri (opt num 35, U)*/
     289                 :           1 :         expected_outer_options[1].delta = 29;
     290                 :           1 :         expected_outer_options[1].len = 0;
     291                 :           1 :         expected_outer_options[1].value = NULL;
     292                 :           1 :         expected_outer_options[1].option_number = PROXY_URI;
     293                 :             : 
     294                 :           1 :         uint8_t expected_outer_options_cnt =
     295                 :             :                 sizeof(expected_outer_options) / sizeof(struct o_coap_option);
     296                 :             : 
     297                 :           1 :         r = inner_outer_option_split(&coap_pkt, inner_options,
     298                 :             :                                      &inner_options_cnt, &inner_options_len,
     299                 :             :                                      outer_options, &outer_options_cnt);
     300                 :             : 
     301                 :           1 :         PRINT_MSG("\ninner options\n");
     302                 :           1 :         print_options(inner_options, inner_options_cnt);
     303                 :           1 :         PRINT_MSG("\nouter options\n");
     304                 :           1 :         print_options(outer_options, outer_options_cnt);
     305                 :             : 
     306                 :           1 :         zassert_equal(r, ok, "Error in inner_outer_option_split. r: %d", r);
     307                 :             : 
     308                 :           1 :         assert_options(inner_options, expected_inner_options, inner_options_cnt,
     309                 :             :                        expected_inner_options_cnt);
     310                 :             : 
     311                 :           1 :         assert_options(outer_options, expected_outer_options, outer_options_cnt,
     312                 :             :                        expected_outer_options_cnt);
     313                 :           1 : }
     314                 :             : 
     315                 :             : /**
     316                 :             :  * @brief   Tests the function inner_outer_option_split with Observe option
     317                 :             :  *          indicating a registration. This function tests the behavior of
     318                 :             :  *          the client preparing a request
     319                 :             :  */
     320                 :           1 : void t102_inner_outer_option_split__with_observe_registration(void)
     321                 :             : {
     322                 :           1 :         enum err r;
     323                 :             : 
     324                 :           1 :         struct o_coap_header header = {
     325                 :             :                 .ver = 1,
     326                 :             :                 .type = TYPE_CON,
     327                 :             :                 .TKL = 0,
     328                 :             :                 .code = CODE_REQ_POST,
     329                 :             :                 .MID = 0x0,
     330                 :             :         };
     331                 :             : 
     332                 :             :         /*The Observe option value is 0x00 when indicating a registration*/
     333                 :           1 :         uint8_t observe_val[] = { 0x00 };
     334                 :             : 
     335                 :           1 :         struct o_coap_packet coap_pkt = {
     336                 :             :                 .header = header,
     337                 :             :                 .token = NULL,
     338                 :             :                 .options_cnt = 5,
     339                 :             :                 .options = {
     340                 :             :                 /*If-Match (opt num 1, E)*/
     341                 :             :                 { .delta = 1,
     342                 :             :                                .len = 0,
     343                 :             :                                .value = NULL,
     344                 :             :                                .option_number = IF_MATCH },
     345                 :             :                 /*Etag (opt num 4, E)*/
     346                 :             :                             { .delta = 3,
     347                 :             :                                .len = 0,
     348                 :             :                                .value = NULL,
     349                 :             :                                .option_number = ETAG },
     350                 :             :                 /*Observe (opt num 6, EU)*/
     351                 :             :                             { .delta = 2,
     352                 :             :                                .len = sizeof(observe_val),
     353                 :             :                                .value = observe_val,
     354                 :             :                                .option_number =  OBSERVE},
     355                 :             :                 /*Content-Format (opt num 12, E)*/
     356                 :             :                             { .delta = 6,
     357                 :             :                                .len = 0,
     358                 :             :                                .value = NULL,
     359                 :             :                                .option_number = CONTENT_FORMAT } ,
     360                 :             :                 /*Proxy-Uri (opt num 35, U)*/
     361                 :             :                             { .delta = 23,
     362                 :             :                                .len = 0,
     363                 :             :                                .value = NULL,
     364                 :             :                                .option_number = PROXY_URI }
     365                 :             :                    },
     366                 :             :                 .payload.len = 0,
     367                 :             :                 .payload.ptr = NULL,
     368                 :             :         };
     369                 :             : 
     370                 :           1 :         struct o_coap_option inner_options[5];
     371                 :           1 :         struct o_coap_option outer_options[5];
     372                 :           1 :         memset(inner_options, 0, sizeof(inner_options));
     373                 :           1 :         memset(outer_options, 0, sizeof(outer_options));
     374                 :           1 :         uint16_t inner_options_len = 0;
     375                 :           1 :         uint8_t inner_options_cnt = 0;
     376                 :           1 :         uint8_t outer_options_cnt = 0;
     377                 :             : 
     378                 :           1 :         struct o_coap_option expected_inner_options[4];
     379                 :           1 :         memset(expected_inner_options, 0, sizeof(expected_inner_options));
     380                 :             : 
     381                 :             :         /*If-Match (opt num 1, E)*/
     382                 :           1 :         expected_inner_options[0].delta = 1;
     383                 :           1 :         expected_inner_options[0].len = 0;
     384                 :           1 :         expected_inner_options[0].value = NULL;
     385                 :           1 :         expected_inner_options[0].option_number = IF_MATCH;
     386                 :             :         /*Etag (opt num 4, E)*/
     387                 :           1 :         expected_inner_options[1].delta = 3;
     388                 :           1 :         expected_inner_options[1].len = 0;
     389                 :           1 :         expected_inner_options[1].value = NULL;
     390                 :           1 :         expected_inner_options[1].option_number = ETAG;
     391                 :             :         /*Observe(opt num 6): The inner observe option shall have
     392                 :             :         the value contained in the original coap packet, see 4.1.3.5.1 in RFC8613*/
     393                 :           1 :         expected_inner_options[2].delta = 2;
     394                 :           1 :         expected_inner_options[2].len = sizeof(observe_val);
     395                 :           1 :         expected_inner_options[2].value = observe_val;
     396                 :           1 :         expected_inner_options[2].option_number = OBSERVE;
     397                 :             :         /*Content-Format (opt num 12, E)*/
     398                 :           1 :         expected_inner_options[3].delta = 6;
     399                 :           1 :         expected_inner_options[3].len = 0;
     400                 :           1 :         expected_inner_options[3].value = NULL;
     401                 :           1 :         expected_inner_options[3].option_number = CONTENT_FORMAT;
     402                 :             : 
     403                 :           1 :         struct o_coap_option expected_outer_options[2];
     404                 :           1 :         memset(expected_outer_options, 0, sizeof(expected_outer_options));
     405                 :             : 
     406                 :             :         /*Observe(opt num 6): The outer observe option must have
     407                 :             :         a value as in the original coap packet, see 4.1.3.5.1 in RFC8613*/
     408                 :           1 :         expected_outer_options[0].delta = 6;
     409                 :           1 :         expected_outer_options[0].len = sizeof(observe_val);
     410                 :           1 :         expected_outer_options[0].value = observe_val;
     411                 :           1 :         expected_outer_options[0].option_number = OBSERVE;
     412                 :             : 
     413                 :             :         /*Proxy-Uri (opt num 35, U)*/
     414                 :           1 :         expected_outer_options[1].delta = 29;
     415                 :           1 :         expected_outer_options[1].len = 0;
     416                 :           1 :         expected_outer_options[1].value = NULL;
     417                 :           1 :         expected_outer_options[1].option_number = PROXY_URI;
     418                 :             : 
     419                 :           1 :         r = inner_outer_option_split(&coap_pkt, inner_options,
     420                 :             :                                      &inner_options_cnt, &inner_options_len,
     421                 :             :                                      outer_options, &outer_options_cnt);
     422                 :           1 :         zassert_equal(r, ok, "Error in inner_outer_option_split. r: %d", r);
     423                 :             : 
     424                 :           1 :         PRINT_MSG("\ninner options\n");
     425                 :           1 :         print_options(inner_options, inner_options_cnt);
     426                 :           1 :         PRINT_MSG("\nouter options\n");
     427                 :           1 :         print_options(outer_options, outer_options_cnt);
     428                 :             : 
     429                 :           1 :         uint8_t expected_inner_options_cnt =
     430                 :             :                 sizeof(expected_inner_options) / sizeof(struct o_coap_option);
     431                 :           1 :         uint8_t expected_outer_options_cnt =
     432                 :             :                 sizeof(expected_outer_options) / sizeof(struct o_coap_option);
     433                 :           1 :         assert_options(inner_options, expected_inner_options, inner_options_cnt,
     434                 :             :                        expected_inner_options_cnt);
     435                 :             : 
     436                 :           1 :         assert_options(outer_options, expected_outer_options, outer_options_cnt,
     437                 :             :                        expected_outer_options_cnt);
     438                 :           1 : }
     439                 :             : 
     440                 :             : /**
     441                 :             :  * @brief   Tests oscore_pkg_generate with an observe option.
     442                 :             :  *          The observe option indicates registration, which is a
     443                 :             :  *                      request message.
     444                 :             :  *
     445                 :             :  */
     446                 :           1 : void t103_oscore_pkg_generate__request_with_observe_registration(void)
     447                 :             : {
     448                 :           1 :         enum err r;
     449                 :             : 
     450                 :           1 :         struct o_coap_header header = {
     451                 :             :                 .ver = 1,
     452                 :             :                 .type = TYPE_CON,
     453                 :             :                 .TKL = 0,
     454                 :             :                 .code = CODE_REQ_POST,
     455                 :             :                 .MID = 0x0,
     456                 :             :         };
     457                 :             : 
     458                 :             :         /*The Observe option value is 0x00 when indicating a registration*/
     459                 :           1 :         uint8_t observe_val[] = { 0x00 };
     460                 :             : 
     461                 :           1 :         struct o_coap_packet coap_pkt = {
     462                 :             :                 .header = header,
     463                 :             :                 .token = NULL,
     464                 :             :                 .options_cnt = 5,
     465                 :             :                 .options = {
     466                 :             :                 /*If-Match (opt num 1, E)*/
     467                 :             :                 { .delta = 1,
     468                 :             :                                .len = 0,
     469                 :             :                                .value = NULL,
     470                 :             :                                .option_number = IF_MATCH },
     471                 :             :                 /*Etag (opt num 4, E)*/
     472                 :             :                             { .delta = 3,
     473                 :             :                                .len = 0,
     474                 :             :                                .value = NULL,
     475                 :             :                                .option_number = ETAG },
     476                 :             :                 /*Observe (opt num 6, EU)*/
     477                 :             :                             { .delta = 2,
     478                 :             :                                .len = sizeof(observe_val),
     479                 :             :                                .value = observe_val,
     480                 :             :                                .option_number = OBSERVE },
     481                 :             :                 /*Content-Format (opt num 12, E)*/
     482                 :             :                             { .delta = 6,
     483                 :             :                                .len = 0,
     484                 :             :                                .value = NULL,
     485                 :             :                                .option_number = CONTENT_FORMAT } ,
     486                 :             :                 /*Proxy-Uri (opt num 35, U)*/
     487                 :             :                             { .delta = 23,
     488                 :             :                                .len = 0,
     489                 :             :                                .value = NULL,
     490                 :             :                                .option_number = PROXY_URI }
     491                 :             :                    },
     492                 :             :                 .payload.len = 0,
     493                 :             :                 .payload.ptr = NULL,
     494                 :             :         };
     495                 :             : 
     496                 :           1 :         struct o_coap_option u_options[] = {
     497                 :             :                 /*Observe(opt num 6): The outer observe option must have
     498                 :             :         a value as in the original coap packet, see 4.1.3.5.1 in RFC8613*/
     499                 :             :                 { .delta = 6,
     500                 :             :                   .len = sizeof(observe_val),
     501                 :             :                   .value = observe_val,
     502                 :             :                   .option_number = OBSERVE },
     503                 :             :                 /*Proxy-Uri (opt num 35, U)*/
     504                 :             :                 { .delta = 29,
     505                 :             :                   .len = 0,
     506                 :             :                   .value = NULL,
     507                 :             :                   .option_number = PROXY_URI }
     508                 :             :         };
     509                 :             : 
     510                 :           1 :         struct oscore_option oscore_option = {
     511                 :             :                 .delta = 0, .len = 0, .value = NULL, .option_number = OSCORE
     512                 :             :         };
     513                 :             : 
     514                 :           1 :         struct o_coap_packet oscore_pkt;
     515                 :           1 :         memset(&oscore_pkt, 0, sizeof(oscore_pkt));
     516                 :             : 
     517                 :           1 :         struct o_coap_header expected_oscore_header = {
     518                 :             :                 .ver = 1,
     519                 :             :                 .type = TYPE_CON,
     520                 :             :                 .TKL = 0,
     521                 :             :                 .code = CODE_REQ_FETCH,
     522                 :             :                 .MID = 0x0,
     523                 :             :         };
     524                 :           1 :         struct o_coap_packet expected_oscore_pkt = {
     525                 :             :                 .header = expected_oscore_header,
     526                 :             :                 .token = NULL,
     527                 :             :                 .options_cnt = 3,
     528                 :             :                 .options = { { .delta = 6,
     529                 :             :                                .len = sizeof(observe_val),
     530                 :             :                                .value = observe_val,
     531                 :             :                                .option_number = OBSERVE },
     532                 :             :                              { .delta = 3,
     533                 :             :                                .len = 0,
     534                 :             :                                .value = NULL,
     535                 :             :                                .option_number = OSCORE },
     536                 :             :                              { .delta = 26,
     537                 :             :                                .len = 0,
     538                 :             :                                .value = NULL,
     539                 :             :                                .option_number = PROXY_URI } },
     540                 :             :                 .payload.len = 0,
     541                 :             :                 .payload.ptr = NULL,
     542                 :             :         };
     543                 :             : 
     544                 :           1 :         struct byte_array no_ciphertext = { .ptr = NULL, .len = 0 };
     545                 :           1 :         r = oscore_pkg_generate(&coap_pkt, &oscore_pkt, u_options, 2,
     546                 :             :                                 &no_ciphertext, &oscore_option);
     547                 :             : 
     548                 :           1 :         zassert_equal(r, ok, "Error in oscore_pkg_generate. r: %d", r);
     549                 :             : 
     550                 :             :         // PRINTF("coap_pkt code: %02X\n", coap_pkt.header.code);
     551                 :             :         // PRINTF("oscore_pkt code: %02X\n", oscore_pkt.header.code);
     552                 :             : 
     553                 :             :         // PRINT_ARRAY("oscore_pkt", &oscore_pkt, sizeof(oscore_pkt));
     554                 :             :         // PRINT_ARRAY("oscore_pkt options", &oscore_pkt.options,
     555                 :             :         //          sizeof(oscore_pkt.options));
     556                 :             :         // PRINT_ARRAY("expected_oscore_pkt", &expected_oscore_pkt,
     557                 :             :         //          sizeof(expected_oscore_pkt));
     558                 :             :         // PRINT_ARRAY("expected_oscore_pkt options", &expected_oscore_pkt.options,
     559                 :             :         //          sizeof(expected_oscore_pkt.options));
     560                 :             : 
     561                 :           1 :         zassert_mem_equal__(&oscore_pkt, &expected_oscore_pkt,
     562                 :             :                             sizeof(oscore_pkt), "oscore_pkt incorrect");
     563                 :           1 : }
     564                 :             : 
     565                 :             : /**
     566                 :             :  * @brief   Tests oscore_pkg_generate with an observe option.
     567                 :             :  *          The observe option indicates notification.
     568                 :             :  *                      The message is a response.
     569                 :             :  *
     570                 :             :  */
     571                 :           1 : void t104_oscore_pkg_generate__request_with_observe_notification(void)
     572                 :             : {
     573                 :           1 :         enum err r;
     574                 :             : 
     575                 :           1 :         struct o_coap_header header = {
     576                 :             :                 .ver = 1,
     577                 :             :                 .type = TYPE_ACK,
     578                 :             :                 .TKL = 0,
     579                 :             :                 .code = CODE_RESP_CONTENT,
     580                 :             :                 .MID = 0x0,
     581                 :             :         };
     582                 :             : 
     583                 :             :         /*The Observe option value is 0x00 when indicating a registration*/
     584                 :           1 :         uint8_t observe_val[] = { 0xde, 0xad, 0xbe, 0xaf };
     585                 :             : 
     586                 :           1 :         struct o_coap_packet coap_pkt = {
     587                 :             :                 .header = header,
     588                 :             :                 .token = NULL,
     589                 :             :                 .options_cnt = 1,
     590                 :             :                 .options = {
     591                 :             :                 /*Observe (opt num 6, EU)*/
     592                 :             :                             { .delta = 6,
     593                 :             :                                .len = sizeof(observe_val),
     594                 :             :                                .value = observe_val,
     595                 :             :                                .option_number = OBSERVE },
     596                 :             :                    },
     597                 :             :                 .payload.len = 0,
     598                 :             :                 .payload.ptr = NULL,
     599                 :             :         };
     600                 :             : 
     601                 :           1 :         struct o_coap_option u_options[] = {
     602                 :             :                 /*Observe(opt num 6): The outer observe option may have
     603                 :             :         a value as in the original coap packet, see 4.1.3.5.1 in RFC8613*/
     604                 :             :                 { .delta = 6,
     605                 :             :                   .len = sizeof(observe_val),
     606                 :             :                   .value = observe_val,
     607                 :             :                   .option_number = OBSERVE }
     608                 :             :         };
     609                 :           1 :         uint8_t u_options_len =
     610                 :             :                 sizeof(u_options) / sizeof(struct o_coap_option);
     611                 :             : 
     612                 :           1 :         struct oscore_option oscore_option = {
     613                 :             :                 .delta = 0, .len = 0, .value = NULL, .option_number = OSCORE
     614                 :             :         };
     615                 :             : 
     616                 :           1 :         struct o_coap_packet oscore_pkt;
     617                 :           1 :         memset(&oscore_pkt, 0, sizeof(oscore_pkt));
     618                 :             : 
     619                 :           1 :         struct o_coap_header expected_oscore_header = {
     620                 :             :                 .ver = 1,
     621                 :             :                 .type = TYPE_ACK,
     622                 :             :                 .TKL = 0,
     623                 :             :                 .code = CODE_RESP_CONTENT,
     624                 :             :                 .MID = 0x0,
     625                 :             :         };
     626                 :           1 :         struct o_coap_packet expected_oscore_pkt = {
     627                 :             :                 .header = expected_oscore_header,
     628                 :             :                 .token = NULL,
     629                 :             :                 .options_cnt = 2,
     630                 :             :                 .options = { { .delta = 6,
     631                 :             :                                .len = sizeof(observe_val),
     632                 :             :                                .value = observe_val,
     633                 :             :                                .option_number = OBSERVE },
     634                 :             :                              { .delta = 3,
     635                 :             :                                .len = 0,
     636                 :             :                                .value = NULL,
     637                 :             :                                .option_number = OSCORE } },
     638                 :             :                 .payload.len = 0,
     639                 :             :                 .payload.ptr = NULL,
     640                 :             :         };
     641                 :           1 :         struct byte_array no_ciphertext = { .ptr = NULL, .len = 0 };
     642                 :           1 :         r = oscore_pkg_generate(&coap_pkt, &oscore_pkt, u_options,
     643                 :             :                                 u_options_len, &no_ciphertext, &oscore_option);
     644                 :             : 
     645                 :           1 :         zassert_equal(r, ok, "Error in oscore_pkg_generate. r: %d", r);
     646                 :             : 
     647                 :             :         // PRINTF("coap_pkt code: %02X\n", coap_pkt.header.code);
     648                 :             :         // PRINTF("oscore_pkt code: %02X\n", oscore_pkt.header.code);
     649                 :             : 
     650                 :             :         // PRINT_ARRAY("oscore_pkt", &oscore_pkt, sizeof(oscore_pkt));
     651                 :             :         // PRINT_ARRAY("oscore_pkt options", &oscore_pkt.options,
     652                 :             :         //          sizeof(oscore_pkt.options));
     653                 :             :         // PRINT_ARRAY("expected_oscore_pkt", &expected_oscore_pkt,
     654                 :             :         //          sizeof(expected_oscore_pkt));
     655                 :             :         // PRINT_ARRAY("expected_oscore_pkt options", &expected_oscore_pkt.options,
     656                 :             :         //          sizeof(expected_oscore_pkt.options));
     657                 :             : 
     658                 :           1 :         zassert_mem_equal__(&oscore_pkt, &expected_oscore_pkt,
     659                 :             :                             sizeof(oscore_pkt), "oscore_pkt incorrect");
     660                 :           1 : }
     661                 :             : 
     662                 :             : /**
     663                 :             :  * @brief   Tests the function inner_outer_option_split with too many options
     664                 :             :  */
     665                 :           1 : void t105_inner_outer_option_split__too_many_options(void)
     666                 :             : {
     667                 :           1 :         enum err r;
     668                 :             : 
     669                 :           1 :         struct o_coap_header header = {
     670                 :             :                 .ver = 1,
     671                 :             :                 .type = TYPE_CON,
     672                 :             :                 .TKL = 0,
     673                 :             :                 .code = CODE_REQ_POST,
     674                 :             :                 .MID = 0x0,
     675                 :             :         };
     676                 :             : 
     677                 :           1 :         struct o_coap_packet coap_pkt = {
     678                 :             :                 .header = header,
     679                 :             :                 .token = NULL,
     680                 :             :                 .options_cnt = 21,
     681                 :             :                 .payload.len = 0,
     682                 :             :                 .payload.ptr = NULL,
     683                 :             :         };
     684                 :             : 
     685                 :           1 :         struct o_coap_option inner_options[5];
     686                 :           1 :         struct o_coap_option outer_options[5];
     687                 :           1 :         memset(inner_options, 0, sizeof(inner_options));
     688                 :           1 :         memset(outer_options, 0, sizeof(outer_options));
     689                 :           1 :         uint16_t inner_options_len = 0;
     690                 :           1 :         uint8_t inner_options_cnt = 0;
     691                 :           1 :         uint8_t outer_options_cnt = 0;
     692                 :             : 
     693                 :           1 :         r = inner_outer_option_split(&coap_pkt, inner_options,
     694                 :             :                                      &inner_options_cnt, &inner_options_len,
     695                 :             :                                      outer_options, &outer_options_cnt);
     696                 :           1 :         zassert_equal(r, too_many_options,
     697                 :             :                       "Error in inner_outer_option_split. r: %d", r);
     698                 :           1 : }
     699                 :             : 
     700                 :             : /**
     701                 :             :  * @brief create an OSCORE option without a PIV
     702                 :             : */
     703                 :           1 : void t106_oscore_option_generate_no_piv(void)
     704                 :             : {
     705                 :           1 :         struct oscore_option oscore_option;
     706                 :             : 
     707                 :           1 :         uint8_t val[] = { 0b11000, 1, 1, 1 };
     708                 :           1 :         struct oscore_option expected = {
     709                 :             :                 .delta = OSCORE,
     710                 :             :                 .len = sizeof(val),
     711                 :             :                 .value = val,
     712                 :             :                 .option_number = OSCORE,
     713                 :             :         };
     714                 :             : 
     715                 :           1 :         uint8_t kid_buf[] = { 1 };
     716                 :           1 :         uint8_t kid_context_buf[] = { 1 };
     717                 :             : 
     718                 :           1 :         struct byte_array piv = BYTE_ARRAY_INIT(NULL, 0);
     719                 :           1 :         struct byte_array kid = BYTE_ARRAY_INIT(kid_buf, sizeof(kid_buf));
     720                 :           1 :         struct byte_array kid_context =
     721                 :             :                 BYTE_ARRAY_INIT(kid_context_buf, sizeof(kid_context_buf));
     722                 :             : 
     723                 :           1 :         enum err r = oscore_option_generate(&piv, &kid, &kid_context,
     724                 :             :                                             &oscore_option);
     725                 :             : 
     726                 :           1 :         zassert_equal(r, ok, "Error in oscore_option_generate. r: %d", r);
     727                 :           1 :         zassert_equal(oscore_option.len, expected.len,
     728                 :             :                       "wrong oscore option len");
     729                 :           1 :         zassert_equal(oscore_option.option_number, expected.option_number,
     730                 :             :                       "wrong oscore option_number");
     731                 :           1 :         zassert_mem_equal__(oscore_option.value, expected.value,
     732                 :             :                             oscore_option.len, "wrong oscore option value");
     733                 :           1 :         ;
     734                 :           1 : }
        

Generated by: LCOV version 2.0-1