LCOV - code coverage report
Current view: top level - tests - simpletlv.c (source / functions) Coverage Total Hit
Test: Code coverage Lines: 100.0 % 266 266
Test Date: 2026-11-03 08:45:29 Functions: 100.0 % 13 13
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 50.0 % 236 118

             Branch data     Line data    Source code
       1                 :             : #include <glib.h>
       2                 :             : #include <string.h>
       3                 :             : #include "simpletlv.h"
       4                 :             : 
       5                 :             : static GMainLoop *loop;
       6                 :             : 
       7                 :             : /* Test that length estimations are sane */
       8                 :           1 : static void test_length_simple(void)
       9                 :             : {
      10                 :             :     size_t length = 0;
      11                 :           1 :     unsigned char simple_value[] = "\x10\x11";
      12                 :           1 :     unsigned char long_value[256] = "Long data value";
      13                 :             :     static struct simpletlv_member simple[1] = {
      14                 :             :       {0x25, 2, {/*.value = simple_value*/}, SIMPLETLV_TYPE_LEAF}
      15                 :             :     };
      16                 :           1 :     simple[0].value.value = simple_value;
      17                 :             : 
      18                 :             :     /* Simple short value to TLV */
      19                 :           1 :     length = simpletlv_get_length(simple, 1, SIMPLETLV_BOTH);
      20         [ -  + ]:           1 :     g_assert_cmpint(length, ==, 4);
      21                 :             : 
      22                 :             :     /* Simple short value to TL */
      23                 :           1 :     length = simpletlv_get_length(simple, 1, SIMPLETLV_TL);
      24         [ -  + ]:           1 :     g_assert_cmpint(length, ==, 2);
      25                 :             : 
      26                 :             :     /* Simple short value to V */
      27                 :           1 :     length = simpletlv_get_length(simple, 1, SIMPLETLV_VALUE);
      28         [ -  + ]:           1 :     g_assert_cmpint(length, ==, 2);
      29                 :             : 
      30                 :             : 
      31                 :             :     /* Prepare long value */
      32                 :           1 :     simple[0].value.value = long_value;
      33                 :           1 :     simple[0].length = 256;
      34                 :             : 
      35                 :             : 
      36                 :             :     /* Simple long value to TLV */
      37                 :           1 :     length = simpletlv_get_length(simple, 1, SIMPLETLV_BOTH);
      38         [ -  + ]:           1 :     g_assert_cmpint(length, ==, 260);
      39                 :             : 
      40                 :             :     /* Simple long value to TL */
      41                 :           1 :     length = simpletlv_get_length(simple, 1, SIMPLETLV_TL);
      42         [ -  + ]:           1 :     g_assert_cmpint(length, ==, 4);
      43                 :             : 
      44                 :             :     /* Simple long value to V */
      45                 :           1 :     length = simpletlv_get_length(simple, 1, SIMPLETLV_VALUE);
      46         [ -  + ]:           1 :     g_assert_cmpint(length, ==, 256);
      47                 :           1 : }
      48                 :             : 
      49                 :           1 : static void test_length_nested(void)
      50                 :             : {
      51                 :             :     int length = 0;
      52                 :           1 :     unsigned char simple_value[] = "\x12\x14";
      53                 :             :     static struct simpletlv_member simple[1] = {
      54                 :             :       {0x25, 2, {/*.value = simple_value*/}, SIMPLETLV_TYPE_LEAF}
      55                 :             :     };
      56                 :             :     static struct simpletlv_member nested[1] = {
      57                 :             :       {0x72, 1, {/*.child = simple*/}, SIMPLETLV_TYPE_COMPOUND}
      58                 :             :     };
      59                 :           1 :     simple[0].value.value = simple_value;
      60                 :           1 :     nested[0].value.child = simple;
      61                 :             : 
      62                 :             :     /* Simple short value to TLV */
      63                 :           1 :     length = simpletlv_get_length(nested, 1, SIMPLETLV_BOTH);
      64         [ -  + ]:           1 :     g_assert_cmpint(length, ==, 6);
      65                 :             : 
      66                 :             :     /* Nested structures do not support splitting TL and V buffers ?? */
      67                 :             :     /* Simple short value to TL */
      68                 :           1 :     length = simpletlv_get_length(nested, 1, SIMPLETLV_TL);
      69         [ -  + ]:           1 :     g_assert_cmpint(length, ==, -1);
      70                 :             : 
      71                 :             :     /* Simple short value to V */
      72                 :           1 :     length = simpletlv_get_length(nested, 1, SIMPLETLV_VALUE);
      73         [ -  + ]:           1 :     g_assert_cmpint(length, ==, -1);
      74                 :           1 : }
      75                 :             : 
      76                 :           1 : static void test_length_skipped(void)
      77                 :             : {
      78                 :             :     size_t length = 0;
      79                 :           1 :     unsigned char simple_value[] = "\x12\x14";
      80                 :           1 :     unsigned char simple_value2[] = "\x16\x18";
      81                 :             :     static struct simpletlv_member simple[2] = {
      82                 :             :       {0x25, 2, {/*.value = simple_value*/}, SIMPLETLV_TYPE_LEAF},
      83                 :             :       {0x30, 2, {/*.value = simple_value2*/}, SIMPLETLV_TYPE_NONE}
      84                 :             :     };
      85                 :           1 :     simple[0].value.value = simple_value;
      86                 :           1 :     simple[1].value.value = simple_value2;
      87                 :             : 
      88                 :             :     /* Simple short value to TLV */
      89                 :           1 :     length = simpletlv_get_length(simple, 2, SIMPLETLV_BOTH);
      90         [ -  + ]:           1 :     g_assert_cmpint(length, ==, 4);
      91                 :             : 
      92                 :             :     /* Simple short value to TL */
      93                 :           1 :     length = simpletlv_get_length(simple, 2, SIMPLETLV_TL);
      94         [ -  + ]:           1 :     g_assert_cmpint(length, ==, 2);
      95                 :             : 
      96                 :             :     /* Simple short value to V */
      97                 :           1 :     length = simpletlv_get_length(simple, 2, SIMPLETLV_VALUE);
      98         [ -  + ]:           1 :     g_assert_cmpint(length, ==, 2);
      99                 :           1 : }
     100                 :             : 
     101                 :             : /* Test that we can encode zero-length values into Simple TLV */
     102                 :           1 : static void test_encode_zero(void)
     103                 :             : {
     104                 :           1 :     unsigned char *result = NULL, *p = NULL;
     105                 :             :     int result_len = 0;
     106                 :           1 :     unsigned char empty_value[] = "";
     107                 :           1 :     unsigned char empty_encoded[] = "\x25\x00";
     108                 :             :     static struct simpletlv_member simple[1] = {
     109                 :             :       {0x25, 0, {/*.value = empty_value*/}, SIMPLETLV_TYPE_LEAF}
     110                 :             :     };
     111                 :           1 :     simple[0].value.value = empty_value;
     112                 :             : 
     113                 :             :     /* Encode simple short TLV with automatic allocation */
     114                 :             :     result = NULL;
     115                 :           1 :     result_len = simpletlv_encode(simple, 1, &result, 0, NULL);
     116   [ -  +  -  +  :           1 :     g_assert_cmpmem(result, result_len, empty_encoded, 2);
                   -  + ]
     117                 :           1 :     g_free(result);
     118                 :             : 
     119                 :             :     /* Encode simple short TLV with pre-allocated buffer (long enough) */
     120                 :           1 :     result = g_malloc(10);
     121                 :           1 :     result_len = simpletlv_encode(simple, 1, &result, 10, NULL);
     122   [ -  +  -  +  :           1 :     g_assert_cmpmem(result, result_len, empty_encoded, 2);
                   -  + ]
     123                 :           1 :     g_free(result);
     124                 :             : 
     125                 :             :     /* Encode simple short TLV with pre-allocated buffer (too short) */
     126                 :           1 :     result = g_malloc(1);
     127                 :           1 :     result_len = simpletlv_encode(simple, 1, &result, 1, NULL);
     128         [ -  + ]:           1 :     g_assert_cmpint(result_len, ==, -1);
     129                 :           1 :     g_free(result);
     130                 :             : 
     131                 :             :     /* Encode only TL part */
     132                 :           1 :     result = NULL;
     133                 :           1 :     result_len = simpletlv_encode_tl(simple, 1, &result, 0, NULL);
     134   [ -  +  -  +  :           1 :     g_assert_cmpmem(result, result_len, "\x25\x00", 2);
                   -  + ]
     135                 :           1 :     g_free(result);
     136                 :             : 
     137                 :             :     /* Encode only VALUE part (equals to the value itself) */
     138                 :           1 :     result = NULL;
     139                 :           1 :     result_len = simpletlv_encode_val(simple, 1, &result, 0, NULL);
     140   [ -  +  -  + ]:           1 :     g_assert_cmpmem(result, result_len, empty_value, 0);
     141                 :           1 :     g_free(result);
     142                 :             : 
     143                 :             :     /* Encode only empty VALUE part with pre-allocated buffer (long enough) */
     144                 :           1 :     result = g_malloc(10);
     145                 :           1 :     result_len = simpletlv_encode_val(simple, 1, &result, 10, &p);
     146   [ -  +  -  + ]:           1 :     g_assert_cmpmem(result, result_len, empty_value, 0);
     147                 :           1 :     g_free(result);
     148                 :           1 : }
     149                 :             : 
     150                 :             : /* Test that we can encode zero-length values with NULL value pointer */
     151                 :           1 : static void test_encode_zero_null(void)
     152                 :             : {
     153                 :           1 :     unsigned char *result = NULL, *p = NULL;
     154                 :             :     int result_len = 0;
     155                 :           1 :     unsigned char empty_encoded[] = "\x25\x00";
     156                 :           1 :     unsigned char value[] = "\x10\x11";
     157                 :           1 :     unsigned char mixed_encoded[] = "\x25\x00\x26\x02\x10\x11";
     158                 :             :     static struct simpletlv_member simple[1] = {
     159                 :             :       {0x25, 0, {/*.value = NULL*/}, SIMPLETLV_TYPE_LEAF}
     160                 :             :     };
     161                 :             :     static struct simpletlv_member mixed[2] = {
     162                 :             :       {0x25, 0, {/*.value = NULL*/}, SIMPLETLV_TYPE_LEAF},
     163                 :             :       {0x26, 2, {/*.value = value*/}, SIMPLETLV_TYPE_LEAF}
     164                 :             :     };
     165                 :           1 :     simple[0].value.value = NULL;
     166                 :           1 :     mixed[0].value.value = NULL;
     167                 :           1 :     mixed[1].value.value = value;
     168                 :             : 
     169                 :             :     /* Encode simple short TLV with automatic allocation */
     170                 :             :     result = NULL;
     171                 :           1 :     result_len = simpletlv_encode(simple, 1, &result, 0, NULL);
     172   [ -  +  -  +  :           1 :     g_assert_cmpmem(result, result_len, empty_encoded, 2);
                   -  + ]
     173                 :           1 :     g_free(result);
     174                 :             : 
     175                 :             :     /* Encode simple short TLV with pre-allocated buffer (long enough) */
     176                 :           1 :     result = g_malloc(10);
     177                 :           1 :     result_len = simpletlv_encode(simple, 1, &result, 10, NULL);
     178   [ -  +  -  +  :           1 :     g_assert_cmpmem(result, result_len, empty_encoded, 2);
                   -  + ]
     179                 :           1 :     g_free(result);
     180                 :             : 
     181                 :             :     /* Encode the empty leaf followed by a non-empty one */
     182                 :           1 :     result = NULL;
     183                 :           1 :     result_len = simpletlv_encode(mixed, 2, &result, 0, NULL);
     184   [ -  +  -  +  :           1 :     g_assert_cmpmem(result, result_len, mixed_encoded, 6);
                   -  + ]
     185                 :           1 :     g_free(result);
     186                 :             : 
     187                 :             :     /* Encode only VALUE part of the empty and non-empty leaves */
     188                 :           1 :     result = g_malloc(10);
     189                 :           1 :     result_len = simpletlv_encode_val(mixed, 2, &result, 10, &p);
     190   [ -  +  -  +  :           1 :     g_assert_cmpmem(result, result_len, value, 2);
                   -  + ]
     191                 :           1 :     g_free(result);
     192                 :           1 : }
     193                 :             : 
     194                 :             : /* Test that NULL value with non-zero length is rejected */
     195                 :           1 : static void test_encode_null_value(void)
     196                 :             : {
     197                 :           1 :     unsigned char *result = NULL;
     198                 :             :     int result_len = 0;
     199                 :             :     static struct simpletlv_member simple[1] = {
     200                 :             :       {0x25, 2, {/*.value = NULL*/}, SIMPLETLV_TYPE_LEAF}
     201                 :             :     };
     202                 :           1 :     simple[0].value.value = NULL;
     203                 :             : 
     204                 :             :     /* Encode with automatic allocation */
     205                 :             :     result = NULL;
     206                 :           1 :     result_len = simpletlv_encode(simple, 1, &result, 0, NULL);
     207         [ -  + ]:           1 :     g_assert_cmpint(result_len, ==, -1);
     208         [ -  + ]:           1 :     g_assert_null(result);
     209                 :             : 
     210                 :             :     /* Encode with pre-allocated buffer (long enough) */
     211                 :           1 :     result = g_malloc(10);
     212                 :           1 :     result_len = simpletlv_encode(simple, 1, &result, 10, NULL);
     213         [ -  + ]:           1 :     g_assert_cmpint(result_len, ==, -1);
     214                 :           1 :     g_free(result);
     215                 :             : 
     216                 :             :     /* Encode only VALUE part */
     217                 :           1 :     result = NULL;
     218                 :           1 :     result_len = simpletlv_encode_val(simple, 1, &result, 0, NULL);
     219         [ -  + ]:           1 :     g_assert_cmpint(result_len, ==, -1);
     220         [ -  + ]:           1 :     g_assert_null(result);
     221                 :           1 : }
     222                 :             : 
     223                 :             : /* Test that we can encode arbitrary data into Simple TLV */
     224                 :           1 : static void test_encode_simple(void)
     225                 :             : {
     226                 :           1 :     unsigned char *result = NULL;
     227                 :             :     int result_len = 0;
     228                 :           1 :     unsigned char simple_value[] = "\x10\x11";
     229                 :           1 :     unsigned char simple_encoded[] = "\x25\x02\x10\x11";
     230                 :           1 :     unsigned char long_value[256] = "Long data value";
     231                 :           1 :     unsigned char long_encoded[261] = "\x25\xFF\x00\x01Long data value";
     232                 :             :     static struct simpletlv_member simple[1] = {
     233                 :             :       {0x25, 2, {/*.value = simple_value*/}, SIMPLETLV_TYPE_LEAF}
     234                 :             :     };
     235                 :           1 :     simple[0].value.value = simple_value;
     236                 :             : 
     237                 :             :     /* Encode simple short TLV with automatic allocation */
     238                 :             :     result = NULL;
     239                 :           1 :     result_len = simpletlv_encode(simple, 1, &result, 0, NULL);
     240   [ -  +  -  +  :           1 :     g_assert_cmpmem(result, result_len, simple_encoded, 4);
                   -  + ]
     241                 :           1 :     g_free(result);
     242                 :             : 
     243                 :             :     /* Encode simple short TLV with pre-allocated buffer (long enough) */
     244                 :           1 :     result = g_malloc(10);
     245                 :           1 :     result_len = simpletlv_encode(simple, 1, &result, 10, NULL);
     246   [ -  +  -  +  :           1 :     g_assert_cmpmem(result, result_len, simple_encoded, 4);
                   -  + ]
     247                 :           1 :     g_free(result);
     248                 :             : 
     249                 :             :     /* Encode simple short TLV with pre-allocated buffer (too short) */
     250                 :           1 :     result = g_malloc(2);
     251                 :           1 :     result_len = simpletlv_encode(simple, 1, &result, 2, NULL);
     252         [ -  + ]:           1 :     g_assert_cmpint(result_len, ==, -1);
     253                 :           1 :     g_free(result);
     254                 :             : 
     255                 :             :     /* Encode only TL part */
     256                 :           1 :     result = NULL;
     257                 :           1 :     result_len = simpletlv_encode_tl(simple, 1, &result, 0, NULL);
     258   [ -  +  -  +  :           1 :     g_assert_cmpmem(result, result_len, "\x25\x02", 2);
                   -  + ]
     259                 :           1 :     g_free(result);
     260                 :             : 
     261                 :             :     /* Encode only VALUE part (equals to the value itself) */
     262                 :           1 :     result = NULL;
     263                 :           1 :     result_len = simpletlv_encode_val(simple, 1, &result, 0, NULL);
     264   [ -  +  -  +  :           1 :     g_assert_cmpmem(result, result_len, simple_value, 2);
                   -  + ]
     265                 :           1 :     g_free(result);
     266                 :             : 
     267                 :             : 
     268                 :             :     /* Prepare long value */
     269                 :           1 :     memset(long_value+15, '\x00', 256-15);
     270                 :           1 :     memset(long_encoded+19, '\x00', 256-15);
     271                 :           1 :     simple[0].value.value = long_value;
     272                 :           1 :     simple[0].length = 256;
     273                 :             : 
     274                 :             : 
     275                 :             :     /* Encode simple long TLV with automatic allocation */
     276                 :           1 :     result = NULL;
     277                 :           1 :     result_len = simpletlv_encode(simple, 1, &result, 0, NULL);
     278   [ -  +  -  +  :           1 :     g_assert_cmpmem(result, result_len, long_encoded, 260);
                   -  + ]
     279                 :           1 :     g_free(result);
     280                 :             : 
     281                 :             :     /* Encode simple long TLV with pre-allocated buffer (long enough) */
     282                 :           1 :     result = g_malloc(300);
     283                 :           1 :     result_len = simpletlv_encode(simple, 1, &result, 300, NULL);
     284   [ -  +  -  +  :           1 :     g_assert_cmpmem(result, result_len, long_encoded, 260);
                   -  + ]
     285                 :           1 :     g_free(result);
     286                 :             : 
     287                 :           1 : }
     288                 :             : 
     289                 :             : /* Test that we can encode nested data into Simple TLV */
     290                 :           1 : static void test_encode_nested(void)
     291                 :             : {
     292                 :           1 :     unsigned char *result = NULL;
     293                 :             :     int result_len = 0;
     294                 :           1 :     unsigned char simple_value[] = "\x12\x14";
     295                 :           1 :     unsigned char encoded[] = "\x72\x04\x25\x02\x12\x14";
     296                 :             :     static struct simpletlv_member simple[1] = {
     297                 :             :       {0x25, 2, {/*.value = simple_value*/}, SIMPLETLV_TYPE_LEAF}
     298                 :             :     };
     299                 :             :     static struct simpletlv_member nested[1] = {
     300                 :             :       {0x72, 1, {/*.child = simple*/}, SIMPLETLV_TYPE_COMPOUND}
     301                 :             :     };
     302                 :           1 :     simple[0].value.value = simple_value;
     303                 :           1 :     nested[0].value.child = simple;
     304                 :             : 
     305                 :             :     /* Encode nested TLV with automatic allocation */
     306                 :             :     result = NULL;
     307                 :           1 :     result_len = simpletlv_encode(nested, 1, &result, 0, NULL);
     308   [ -  +  -  +  :           1 :     g_assert_cmpmem(result, result_len, encoded, 6);
                   -  + ]
     309                 :           1 :     g_free(result);
     310                 :             : 
     311                 :             :     /* Encode nested TLV with pre-allocated buffer (long enough) */
     312                 :           1 :     result = g_malloc(10);
     313                 :           1 :     result_len = simpletlv_encode(nested, 1, &result, 10, NULL);
     314   [ -  +  -  +  :           1 :     g_assert_cmpmem(result, result_len, encoded, 6);
                   -  + ]
     315                 :           1 :     g_free(result);
     316                 :             : 
     317                 :             :     /* Encode simple short TLV with pre-allocated buffer (too short) */
     318                 :           1 :     result = g_malloc(4);
     319                 :           1 :     result_len = simpletlv_encode(nested, 1, &result, 4, NULL);
     320         [ -  + ]:           1 :     g_assert_cmpint(result_len, ==, -1);
     321                 :           1 :     g_free(result);
     322                 :             : 
     323                 :             :     /* Encode only TL part */
     324                 :           1 :     result = NULL;
     325                 :           1 :     result_len = simpletlv_encode_tl(nested, 1, &result, 0, NULL);
     326         [ -  + ]:           1 :     g_assert_cmpint(result_len, ==, -1);
     327                 :             : 
     328                 :             :     /* Encode only VALUE part (equals to the value itself) */
     329                 :           1 :     result = NULL;
     330                 :           1 :     result_len = simpletlv_encode_val(nested, 1, &result, 0, NULL);
     331         [ -  + ]:           1 :     g_assert_cmpint(result_len, ==, -1);
     332                 :           1 : }
     333                 :             : 
     334                 :           1 : static void test_encode_skipped(void)
     335                 :             : {
     336                 :           1 :     unsigned char *result = NULL;
     337                 :             :     size_t result_len = 0;
     338                 :           1 :     unsigned char simple_value[] = "\x12\x14";
     339                 :           1 :     unsigned char simple_value2[] = "\x16\x18";
     340                 :             :     static struct simpletlv_member simple[2] = {
     341                 :             :       {0x25, 2, {/*.value = simple_value*/}, SIMPLETLV_TYPE_LEAF},
     342                 :             :       {0x30, 2, {/*.value = simple_value2*/}, SIMPLETLV_TYPE_NONE}
     343                 :             :     };
     344                 :           1 :     unsigned char encoded[] = "\x25\x02\x12\x14";
     345                 :             : 
     346                 :           1 :     simple[0].value.value = simple_value;
     347                 :           1 :     simple[1].value.value = simple_value2;
     348                 :             : 
     349                 :             :     /* Simple short value to TLV */
     350                 :             :     result = NULL;
     351                 :           1 :     result_len = simpletlv_encode(simple, 2, &result, 0, NULL);
     352   [ -  +  -  +  :           1 :     g_assert_cmpmem(result, result_len, encoded, 4);
                   -  + ]
     353                 :           1 :     g_free(result);
     354                 :             : 
     355                 :             :     /* Simple short value to TL */
     356                 :           1 :     result = NULL;
     357                 :           1 :     result_len = simpletlv_encode_tl(simple, 2, &result, 0, NULL);
     358   [ -  +  -  +  :           1 :     g_assert_cmpmem(result, result_len, "\x25\x02", 2);
                   -  + ]
     359                 :           1 :     g_free(result);
     360                 :             : 
     361                 :             :     /* Simple short value to V */
     362                 :           1 :     result = NULL;
     363                 :           1 :     result_len = simpletlv_encode_val(simple, 2, &result, 0, NULL);
     364   [ -  +  -  +  :           1 :     g_assert_cmpmem(result, result_len, "\x12\x14", 2);
                   -  + ]
     365                 :           1 :     g_free(result);
     366                 :           1 : }
     367                 :             : 
     368                 :           1 : static void test_clone_simple(void)
     369                 :             : {
     370                 :           1 :     unsigned char *result = NULL;
     371                 :             :     size_t result_len = 0;
     372                 :           1 :     unsigned char simple_value[] = "\x14\x18";
     373                 :           1 :     unsigned char simple_value2[] = "\x64\x24\x44";
     374                 :             :     static struct simpletlv_member simple[2] = {
     375                 :             :       {0x13, 2, {/*.value = simple_value*/}, SIMPLETLV_TYPE_LEAF},
     376                 :             :       {0xDD, 3, {/*.value = simple_value2*/}, SIMPLETLV_TYPE_LEAF}
     377                 :             :     };
     378                 :           1 :     unsigned char encoded[] = "\x13\x02\x14\x18\xDD\x03\x64\x24\x44";
     379                 :             :     struct simpletlv_member *clone;
     380                 :             : 
     381                 :           1 :     simple[0].value.value = simple_value;
     382                 :           1 :     simple[1].value.value = simple_value2;
     383                 :             : 
     384                 :           1 :     clone = simpletlv_clone(simple, 2);
     385         [ -  + ]:           1 :     g_assert_nonnull(clone);
     386                 :             : 
     387                 :           1 :     result = NULL;
     388                 :           1 :     result_len = simpletlv_encode(clone, 2, &result, 0, NULL);
     389   [ -  +  -  +  :           1 :     g_assert_cmpmem(result, result_len, encoded, 9);
                   -  + ]
     390                 :           1 :     g_free(result);
     391                 :           1 :     simpletlv_free(clone, 2);
     392                 :           1 : }
     393                 :             : 
     394                 :           1 : static void test_parse_simple(void)
     395                 :             : {
     396                 :           1 :     unsigned char data[] = "\x13\x02\x14\x18\xDD\x03\x64\x24\x44";
     397                 :           1 :     size_t data_len = 9, tlv_len = 0;
     398                 :             :     struct simpletlv_member *tlv;
     399                 :             : 
     400                 :           1 :     tlv = simpletlv_parse(data, data_len, &tlv_len);
     401         [ -  + ]:           1 :     g_assert_cmpint(tlv_len, ==, 2);
     402                 :             : 
     403         [ -  + ]:           1 :     g_assert_cmpint(tlv[0].tag, ==, 0x13);
     404         [ -  + ]:           1 :     g_assert_cmpint(tlv[0].length, ==, 0x02);
     405         [ -  + ]:           1 :     g_assert_cmpint(tlv[0].type, ==, SIMPLETLV_TYPE_LEAF);
     406   [ -  +  -  +  :           1 :     g_assert_cmpmem(tlv[0].value.value, tlv[0].length, "\x14\x18", 2);
                   -  + ]
     407                 :             : 
     408         [ -  + ]:           1 :     g_assert_cmpint(tlv[1].tag, ==, 0xDD);
     409         [ -  + ]:           1 :     g_assert_cmpint(tlv[1].length, ==, 0x03);
     410         [ -  + ]:           1 :     g_assert_cmpint(tlv[1].type, ==, SIMPLETLV_TYPE_LEAF);
     411   [ -  +  -  +  :           1 :     g_assert_cmpmem(tlv[1].value.value, tlv[1].length, "\x64\x24\x44", 3);
                   -  + ]
     412                 :             : 
     413                 :           1 :     simpletlv_free(tlv, tlv_len);
     414                 :           1 : }
     415                 :             : 
     416                 :             : /* If there is some gargabe in the end of buffer, we would like to
     417                 :             :  * return at least what was properly parsed, rahter than to fail
     418                 :             :  * hard.
     419                 :             :  * Also makes sure we do not leak memory or crash on bad data.
     420                 :             :  *
     421                 :             :  * This is an issue for OpenSC at this moment, which fails to encode
     422                 :             :  * last TLV into the compound buffer for data objects.
     423                 :             :  */
     424                 :           1 : static void test_parse_last_bad(void)
     425                 :             : {
     426                 :             :     size_t data_len = 9;
     427                 :           1 :     unsigned char data[] = "\x13\x02\x14\x18\x28\x13\x64\x24\x44";
     428                 :             :     /*                    this length is oveflow -^ */
     429                 :             :     size_t data2_len = 7;
     430                 :           1 :     unsigned char data2[] = "\x13\x02\x14\x18\x28\xFF\xFF";
     431                 :             :     /*           this length is missing last byte -^ */
     432                 :             :     size_t data3_len = 5;
     433                 :           1 :     unsigned char data3[] = "\x13\x02\x14\x18\x12";
     434                 :             :     /*         this length is missed completely -^ */
     435                 :           1 :     size_t tlv_len = 0;
     436                 :             :     struct simpletlv_member *tlv;
     437                 :             : 
     438                 :             :     /* Test the overflow length in the last member */
     439                 :           1 :     tlv = simpletlv_parse(data, data_len, &tlv_len);
     440         [ -  + ]:           1 :     g_assert_cmpint(tlv_len, ==, 1);
     441                 :             : 
     442         [ -  + ]:           1 :     g_assert_cmpint(tlv[0].tag, ==, 0x13);
     443         [ -  + ]:           1 :     g_assert_cmpint(tlv[0].length, ==, 0x02);
     444         [ -  + ]:           1 :     g_assert_cmpint(tlv[0].type, ==, SIMPLETLV_TYPE_LEAF);
     445   [ -  +  -  +  :           1 :     g_assert_cmpmem(tlv[0].value.value, tlv[0].length, "\x14\x18", 2);
                   -  + ]
     446                 :             : 
     447                 :           1 :     simpletlv_free(tlv, tlv_len);
     448                 :             : 
     449                 :             :     /* Test the truncated length item in last member */
     450                 :           1 :     tlv = simpletlv_parse(data2, data2_len, &tlv_len);
     451         [ -  + ]:           1 :     g_assert_cmpint(tlv_len, ==, 1);
     452                 :             : 
     453         [ -  + ]:           1 :     g_assert_cmpint(tlv[0].tag, ==, 0x13);
     454         [ -  + ]:           1 :     g_assert_cmpint(tlv[0].length, ==, 0x02);
     455         [ -  + ]:           1 :     g_assert_cmpint(tlv[0].type, ==, SIMPLETLV_TYPE_LEAF);
     456   [ -  +  -  +  :           1 :     g_assert_cmpmem(tlv[0].value.value, tlv[0].length, "\x14\x18", 2);
                   -  + ]
     457                 :             : 
     458                 :           1 :     simpletlv_free(tlv, tlv_len);
     459                 :             : 
     460                 :             :     /* Test the missing length item in last member */
     461                 :           1 :     tlv = simpletlv_parse(data3, data3_len, &tlv_len);
     462         [ -  + ]:           1 :     g_assert_cmpint(tlv_len, ==, 1);
     463                 :             : 
     464         [ -  + ]:           1 :     g_assert_cmpint(tlv[0].tag, ==, 0x13);
     465         [ -  + ]:           1 :     g_assert_cmpint(tlv[0].length, ==, 0x02);
     466         [ -  + ]:           1 :     g_assert_cmpint(tlv[0].type, ==, SIMPLETLV_TYPE_LEAF);
     467   [ -  +  -  +  :           1 :     g_assert_cmpmem(tlv[0].value.value, tlv[0].length, "\x14\x18", 2);
                   -  + ]
     468                 :             : 
     469                 :           1 :     simpletlv_free(tlv, tlv_len);
     470                 :           1 : }
     471                 :             : 
     472                 :           1 : int main(int argc, char *argv[])
     473                 :             : {
     474                 :             :     int ret;
     475                 :             : 
     476                 :           1 :     g_test_init(&argc, &argv, NULL);
     477                 :             : 
     478                 :           1 :     loop = g_main_loop_new(NULL, TRUE);
     479                 :             : 
     480                 :           1 :     g_test_add_func("/simpletlv/length/simple", test_length_simple);
     481                 :           1 :     g_test_add_func("/simpletlv/length/nested", test_length_nested);
     482                 :           1 :     g_test_add_func("/simpletlv/length/skipped", test_length_skipped);
     483                 :           1 :     g_test_add_func("/simpletlv/encode/zero", test_encode_zero);
     484                 :           1 :     g_test_add_func("/simpletlv/encode/zero_null", test_encode_zero_null);
     485                 :           1 :     g_test_add_func("/simpletlv/encode/null_value", test_encode_null_value);
     486                 :           1 :     g_test_add_func("/simpletlv/encode/simple", test_encode_simple);
     487                 :           1 :     g_test_add_func("/simpletlv/encode/nested", test_encode_nested);
     488                 :           1 :     g_test_add_func("/simpletlv/encode/skipped", test_encode_skipped);
     489                 :           1 :     g_test_add_func("/simpletlv/parse/simple", test_parse_simple);
     490                 :           1 :     g_test_add_func("/simpletlv/parse/last_bad", test_parse_last_bad);
     491                 :           1 :     g_test_add_func("/simpletlv/clone/simple", test_clone_simple);
     492                 :             : 
     493                 :           1 :     ret = g_test_run();
     494                 :             : 
     495                 :           1 :     g_main_loop_unref(loop);
     496                 :             : 
     497                 :             :     return ret;
     498                 :             : }
     499                 :             : 
     500                 :             : /* vim: set ts=4 sw=4 tw=0 noet expandtab: */
        

Generated by: LCOV version 2.0-1