Home | History | Annotate | Line # | Download | only in test
      1      1.1  christos /*
      2  1.1.1.4  christos  * Copyright 2015-2021 The OpenSSL Project Authors. All Rights Reserved.
      3      1.1  christos  *
      4  1.1.1.5  christos  * Licensed under the Apache License 2.0 (the "License").  You may not use
      5      1.1  christos  * this file except in compliance with the License.  You can obtain a copy
      6      1.1  christos  * in the file LICENSE in the source distribution or at
      7      1.1  christos  * https://www.openssl.org/source/license.html
      8      1.1  christos  */
      9      1.1  christos 
     10  1.1.1.5  christos #include "internal/packet.h"
     11  1.1.1.2  christos #include "testutil.h"
     12      1.1  christos 
     13      1.1  christos #define BUF_LEN 255
     14      1.1  christos 
     15  1.1.1.5  christos static unsigned char smbuf[BUF_LEN + 1];
     16  1.1.1.2  christos 
     17  1.1.1.2  christos static int test_PACKET_remaining(void)
     18      1.1  christos {
     19      1.1  christos     PACKET pkt;
     20      1.1  christos 
     21  1.1.1.5  christos     if (!TEST_true(PACKET_buf_init(&pkt, smbuf, BUF_LEN))
     22  1.1.1.2  christos             || !TEST_size_t_eq(PACKET_remaining(&pkt), BUF_LEN)
     23  1.1.1.2  christos             || !TEST_true(PACKET_forward(&pkt, BUF_LEN - 1))
     24  1.1.1.2  christos             || !TEST_size_t_eq(PACKET_remaining(&pkt), 1)
     25  1.1.1.2  christos             || !TEST_true(PACKET_forward(&pkt, 1))
     26  1.1.1.2  christos             || !TEST_size_t_eq(PACKET_remaining(&pkt), 0))
     27      1.1  christos         return 0;
     28      1.1  christos 
     29      1.1  christos     return 1;
     30      1.1  christos }
     31      1.1  christos 
     32  1.1.1.2  christos static int test_PACKET_end(void)
     33      1.1  christos {
     34      1.1  christos     PACKET pkt;
     35      1.1  christos 
     36  1.1.1.5  christos     if (!TEST_true(PACKET_buf_init(&pkt, smbuf, BUF_LEN))
     37  1.1.1.2  christos             || !TEST_size_t_eq(PACKET_remaining(&pkt), BUF_LEN)
     38  1.1.1.2  christos             || !TEST_ptr_eq(PACKET_end(&pkt), smbuf + BUF_LEN)
     39  1.1.1.2  christos             || !TEST_true(PACKET_forward(&pkt, BUF_LEN - 1))
     40  1.1.1.2  christos             || !TEST_ptr_eq(PACKET_end(&pkt), smbuf + BUF_LEN)
     41  1.1.1.2  christos             || !TEST_true(PACKET_forward(&pkt, 1))
     42  1.1.1.2  christos             || !TEST_ptr_eq(PACKET_end(&pkt), smbuf + BUF_LEN))
     43      1.1  christos         return 0;
     44      1.1  christos 
     45      1.1  christos     return 1;
     46      1.1  christos }
     47      1.1  christos 
     48  1.1.1.2  christos static int test_PACKET_get_1(void)
     49      1.1  christos {
     50  1.1.1.2  christos     unsigned int i = 0;
     51      1.1  christos     PACKET pkt;
     52      1.1  christos 
     53  1.1.1.2  christos     if (!TEST_true(PACKET_buf_init(&pkt, smbuf, BUF_LEN))
     54  1.1.1.2  christos             || !TEST_true(PACKET_get_1(&pkt, &i))
     55  1.1.1.2  christos             || !TEST_uint_eq(i, 0x02)
     56  1.1.1.2  christos             || !TEST_true(PACKET_forward(&pkt, BUF_LEN - 2))
     57  1.1.1.2  christos             || !TEST_true(PACKET_get_1(&pkt, &i))
     58  1.1.1.2  christos             || !TEST_uint_eq(i, 0xfe)
     59  1.1.1.2  christos             || !TEST_false(PACKET_get_1(&pkt, &i)))
     60      1.1  christos         return 0;
     61      1.1  christos 
     62      1.1  christos     return 1;
     63      1.1  christos }
     64      1.1  christos 
     65  1.1.1.2  christos static int test_PACKET_get_4(void)
     66      1.1  christos {
     67  1.1.1.2  christos     unsigned long i = 0;
     68      1.1  christos     PACKET pkt;
     69      1.1  christos 
     70  1.1.1.2  christos     if (!TEST_true(PACKET_buf_init(&pkt, smbuf, BUF_LEN))
     71  1.1.1.2  christos             || !TEST_true(PACKET_get_4(&pkt, &i))
     72  1.1.1.2  christos             || !TEST_ulong_eq(i, 0x08060402UL)
     73  1.1.1.2  christos             || !TEST_true(PACKET_forward(&pkt, BUF_LEN - 8))
     74  1.1.1.2  christos             || !TEST_true(PACKET_get_4(&pkt, &i))
     75  1.1.1.2  christos             || !TEST_ulong_eq(i, 0xfefcfaf8UL)
     76  1.1.1.2  christos             || !TEST_false(PACKET_get_4(&pkt, &i)))
     77      1.1  christos         return 0;
     78      1.1  christos 
     79      1.1  christos     return 1;
     80      1.1  christos }
     81      1.1  christos 
     82  1.1.1.2  christos static int test_PACKET_get_net_2(void)
     83      1.1  christos {
     84  1.1.1.2  christos     unsigned int i = 0;
     85      1.1  christos     PACKET pkt;
     86      1.1  christos 
     87  1.1.1.2  christos     if (!TEST_true(PACKET_buf_init(&pkt, smbuf, BUF_LEN))
     88  1.1.1.2  christos             || !TEST_true(PACKET_get_net_2(&pkt, &i))
     89  1.1.1.2  christos             || !TEST_uint_eq(i, 0x0204)
     90  1.1.1.2  christos             || !TEST_true(PACKET_forward(&pkt, BUF_LEN - 4))
     91  1.1.1.2  christos             || !TEST_true(PACKET_get_net_2(&pkt, &i))
     92  1.1.1.2  christos             || !TEST_uint_eq(i, 0xfcfe)
     93  1.1.1.2  christos             || !TEST_false(PACKET_get_net_2(&pkt, &i)))
     94      1.1  christos         return 0;
     95      1.1  christos 
     96      1.1  christos     return 1;
     97      1.1  christos }
     98      1.1  christos 
     99  1.1.1.2  christos static int test_PACKET_get_net_3(void)
    100      1.1  christos {
    101  1.1.1.2  christos     unsigned long i = 0;
    102      1.1  christos     PACKET pkt;
    103      1.1  christos 
    104  1.1.1.2  christos     if (!TEST_true(PACKET_buf_init(&pkt, smbuf, BUF_LEN))
    105  1.1.1.2  christos             || !TEST_true(PACKET_get_net_3(&pkt, &i))
    106  1.1.1.2  christos             || !TEST_ulong_eq(i, 0x020406UL)
    107  1.1.1.2  christos             || !TEST_true(PACKET_forward(&pkt, BUF_LEN - 6))
    108  1.1.1.2  christos             || !TEST_true(PACKET_get_net_3(&pkt, &i))
    109  1.1.1.2  christos             || !TEST_ulong_eq(i, 0xfafcfeUL)
    110  1.1.1.2  christos             || !TEST_false(PACKET_get_net_3(&pkt, &i)))
    111      1.1  christos         return 0;
    112      1.1  christos 
    113      1.1  christos     return 1;
    114      1.1  christos }
    115      1.1  christos 
    116  1.1.1.2  christos static int test_PACKET_get_net_4(void)
    117      1.1  christos {
    118  1.1.1.2  christos     unsigned long i = 0;
    119      1.1  christos     PACKET pkt;
    120      1.1  christos 
    121  1.1.1.2  christos     if (!TEST_true(PACKET_buf_init(&pkt, smbuf, BUF_LEN))
    122  1.1.1.2  christos             || !TEST_true(PACKET_get_net_4(&pkt, &i))
    123  1.1.1.2  christos             || !TEST_ulong_eq(i, 0x02040608UL)
    124  1.1.1.2  christos             || !TEST_true(PACKET_forward(&pkt, BUF_LEN - 8))
    125  1.1.1.2  christos             || !TEST_true(PACKET_get_net_4(&pkt, &i))
    126  1.1.1.2  christos             || !TEST_ulong_eq(i, 0xf8fafcfeUL)
    127  1.1.1.2  christos             || !TEST_false(PACKET_get_net_4(&pkt, &i)))
    128      1.1  christos         return 0;
    129      1.1  christos 
    130      1.1  christos     return 1;
    131      1.1  christos }
    132      1.1  christos 
    133  1.1.1.2  christos static int test_PACKET_get_sub_packet(void)
    134      1.1  christos {
    135      1.1  christos     PACKET pkt, subpkt;
    136  1.1.1.2  christos     unsigned long i = 0;
    137      1.1  christos 
    138  1.1.1.2  christos     if (!TEST_true(PACKET_buf_init(&pkt, smbuf, BUF_LEN))
    139  1.1.1.2  christos             || !TEST_true(PACKET_get_sub_packet(&pkt, &subpkt, 4))
    140  1.1.1.2  christos             || !TEST_true(PACKET_get_net_4(&subpkt, &i))
    141  1.1.1.2  christos             || !TEST_ulong_eq(i, 0x02040608UL)
    142  1.1.1.2  christos             || !TEST_size_t_eq(PACKET_remaining(&subpkt), 0)
    143  1.1.1.2  christos             || !TEST_true(PACKET_forward(&pkt, BUF_LEN - 8))
    144  1.1.1.2  christos             || !TEST_true(PACKET_get_sub_packet(&pkt, &subpkt, 4))
    145  1.1.1.2  christos             || !TEST_true(PACKET_get_net_4(&subpkt, &i))
    146  1.1.1.2  christos             || !TEST_ulong_eq(i, 0xf8fafcfeUL)
    147  1.1.1.2  christos             || !TEST_size_t_eq(PACKET_remaining(&subpkt), 0)
    148  1.1.1.2  christos             || !TEST_false(PACKET_get_sub_packet(&pkt, &subpkt, 4)))
    149      1.1  christos         return 0;
    150      1.1  christos 
    151      1.1  christos     return 1;
    152      1.1  christos }
    153      1.1  christos 
    154  1.1.1.2  christos static int test_PACKET_get_bytes(void)
    155      1.1  christos {
    156  1.1.1.2  christos     const unsigned char *bytes = NULL;
    157      1.1  christos     PACKET pkt;
    158      1.1  christos 
    159  1.1.1.2  christos     if (!TEST_true(PACKET_buf_init(&pkt, smbuf, BUF_LEN))
    160  1.1.1.2  christos             || !TEST_true(PACKET_get_bytes(&pkt, &bytes, 4))
    161  1.1.1.2  christos             || !TEST_uchar_eq(bytes[0], 2)
    162  1.1.1.2  christos             || !TEST_uchar_eq(bytes[1], 4)
    163  1.1.1.2  christos             || !TEST_uchar_eq(bytes[2], 6)
    164  1.1.1.2  christos             || !TEST_uchar_eq(bytes[3], 8)
    165  1.1.1.2  christos             || !TEST_size_t_eq(PACKET_remaining(&pkt), BUF_LEN -4)
    166  1.1.1.2  christos             || !TEST_true(PACKET_forward(&pkt, BUF_LEN - 8))
    167  1.1.1.2  christos             || !TEST_true(PACKET_get_bytes(&pkt, &bytes, 4))
    168  1.1.1.2  christos             || !TEST_uchar_eq(bytes[0], 0xf8)
    169  1.1.1.2  christos             || !TEST_uchar_eq(bytes[1], 0xfa)
    170  1.1.1.2  christos             || !TEST_uchar_eq(bytes[2], 0xfc)
    171  1.1.1.2  christos             || !TEST_uchar_eq(bytes[3], 0xfe)
    172  1.1.1.2  christos             || !TEST_false(PACKET_remaining(&pkt)))
    173      1.1  christos         return 0;
    174      1.1  christos 
    175      1.1  christos     return 1;
    176      1.1  christos }
    177      1.1  christos 
    178  1.1.1.2  christos static int test_PACKET_copy_bytes(void)
    179      1.1  christos {
    180      1.1  christos     unsigned char bytes[4];
    181      1.1  christos     PACKET pkt;
    182      1.1  christos 
    183  1.1.1.2  christos     if (!TEST_true(PACKET_buf_init(&pkt, smbuf, BUF_LEN))
    184  1.1.1.2  christos             || !TEST_true(PACKET_copy_bytes(&pkt, bytes, 4))
    185  1.1.1.2  christos             || !TEST_char_eq(bytes[0], 2)
    186  1.1.1.2  christos             || !TEST_char_eq(bytes[1], 4)
    187  1.1.1.2  christos             || !TEST_char_eq(bytes[2], 6)
    188  1.1.1.2  christos             || !TEST_char_eq(bytes[3], 8)
    189  1.1.1.2  christos             || !TEST_size_t_eq(PACKET_remaining(&pkt), BUF_LEN - 4)
    190  1.1.1.2  christos             || !TEST_true(PACKET_forward(&pkt, BUF_LEN - 8))
    191  1.1.1.2  christos             || !TEST_true(PACKET_copy_bytes(&pkt, bytes, 4))
    192  1.1.1.2  christos             || !TEST_uchar_eq(bytes[0], 0xf8)
    193  1.1.1.2  christos             || !TEST_uchar_eq(bytes[1], 0xfa)
    194  1.1.1.2  christos             || !TEST_uchar_eq(bytes[2], 0xfc)
    195  1.1.1.2  christos             || !TEST_uchar_eq(bytes[3], 0xfe)
    196  1.1.1.2  christos             || !TEST_false(PACKET_remaining(&pkt)))
    197      1.1  christos         return 0;
    198      1.1  christos 
    199      1.1  christos     return 1;
    200      1.1  christos }
    201      1.1  christos 
    202  1.1.1.2  christos static int test_PACKET_copy_all(void)
    203      1.1  christos {
    204      1.1  christos     unsigned char tmp[BUF_LEN];
    205      1.1  christos     PACKET pkt;
    206      1.1  christos     size_t len;
    207      1.1  christos 
    208  1.1.1.2  christos     if (!TEST_true(PACKET_buf_init(&pkt, smbuf, BUF_LEN))
    209  1.1.1.2  christos             || !TEST_true(PACKET_copy_all(&pkt, tmp, BUF_LEN, &len))
    210  1.1.1.2  christos             || !TEST_size_t_eq(len, BUF_LEN)
    211  1.1.1.2  christos             || !TEST_mem_eq(smbuf, BUF_LEN, tmp, BUF_LEN)
    212  1.1.1.2  christos             || !TEST_size_t_eq(PACKET_remaining(&pkt), BUF_LEN)
    213  1.1.1.2  christos             || !TEST_false(PACKET_copy_all(&pkt, tmp, BUF_LEN - 1, &len)))
    214      1.1  christos         return 0;
    215      1.1  christos 
    216      1.1  christos     return 1;
    217      1.1  christos }
    218      1.1  christos 
    219  1.1.1.2  christos static int test_PACKET_memdup(void)
    220      1.1  christos {
    221      1.1  christos     unsigned char *data = NULL;
    222      1.1  christos     size_t len;
    223      1.1  christos     PACKET pkt;
    224  1.1.1.2  christos     int result = 0;
    225      1.1  christos 
    226  1.1.1.2  christos     if (!TEST_true(PACKET_buf_init(&pkt, smbuf, BUF_LEN))
    227  1.1.1.2  christos             || !TEST_true(PACKET_memdup(&pkt, &data, &len))
    228  1.1.1.2  christos             || !TEST_size_t_eq(len, BUF_LEN)
    229  1.1.1.2  christos             || !TEST_mem_eq(data, len, PACKET_data(&pkt), len)
    230  1.1.1.2  christos             || !TEST_true(PACKET_forward(&pkt, 10))
    231  1.1.1.2  christos             || !TEST_true(PACKET_memdup(&pkt, &data, &len))
    232  1.1.1.2  christos             || !TEST_size_t_eq(len, BUF_LEN - 10)
    233  1.1.1.2  christos             || !TEST_mem_eq(data, len, PACKET_data(&pkt), len))
    234  1.1.1.2  christos         goto end;
    235  1.1.1.2  christos     result = 1;
    236  1.1.1.2  christos end:
    237      1.1  christos     OPENSSL_free(data);
    238  1.1.1.2  christos     return result;
    239      1.1  christos }
    240      1.1  christos 
    241  1.1.1.2  christos static int test_PACKET_strndup(void)
    242      1.1  christos {
    243  1.1.1.2  christos     char buf1[10], buf2[10];
    244      1.1  christos     char *data = NULL;
    245      1.1  christos     PACKET pkt;
    246  1.1.1.2  christos     int result = 0;
    247      1.1  christos 
    248  1.1.1.2  christos     memset(buf1, 'x', 10);
    249      1.1  christos     memset(buf2, 'y', 10);
    250      1.1  christos     buf2[5] = '\0';
    251      1.1  christos 
    252  1.1.1.2  christos     if (!TEST_true(PACKET_buf_init(&pkt, (unsigned char*)buf1, 10))
    253  1.1.1.2  christos             || !TEST_true(PACKET_strndup(&pkt, &data))
    254  1.1.1.2  christos             || !TEST_size_t_eq(strlen(data), 10)
    255  1.1.1.2  christos             || !TEST_strn_eq(data, buf1, 10)
    256  1.1.1.2  christos             || !TEST_true(PACKET_buf_init(&pkt, (unsigned char*)buf2, 10))
    257  1.1.1.2  christos             || !TEST_true(PACKET_strndup(&pkt, &data))
    258  1.1.1.2  christos             || !TEST_size_t_eq(strlen(data), 5)
    259  1.1.1.2  christos             || !TEST_str_eq(data, buf2))
    260  1.1.1.2  christos         goto end;
    261      1.1  christos 
    262  1.1.1.2  christos     result = 1;
    263  1.1.1.2  christos end:
    264      1.1  christos     OPENSSL_free(data);
    265  1.1.1.2  christos     return result;
    266      1.1  christos }
    267      1.1  christos 
    268  1.1.1.2  christos static int test_PACKET_contains_zero_byte(void)
    269      1.1  christos {
    270  1.1.1.2  christos     char buf1[10], buf2[10];
    271      1.1  christos     PACKET pkt;
    272      1.1  christos 
    273  1.1.1.2  christos     memset(buf1, 'x', 10);
    274      1.1  christos     memset(buf2, 'y', 10);
    275      1.1  christos     buf2[5] = '\0';
    276      1.1  christos 
    277  1.1.1.2  christos     if (!TEST_true(PACKET_buf_init(&pkt, (unsigned char*)buf1, 10))
    278  1.1.1.2  christos             || !TEST_false(PACKET_contains_zero_byte(&pkt))
    279  1.1.1.2  christos             || !TEST_true(PACKET_buf_init(&pkt, (unsigned char*)buf2, 10))
    280  1.1.1.2  christos             || !TEST_true(PACKET_contains_zero_byte(&pkt)))
    281      1.1  christos         return 0;
    282      1.1  christos 
    283      1.1  christos     return 1;
    284      1.1  christos }
    285      1.1  christos 
    286  1.1.1.2  christos static int test_PACKET_forward(void)
    287      1.1  christos {
    288  1.1.1.2  christos     const unsigned char *byte = NULL;
    289      1.1  christos     PACKET pkt;
    290      1.1  christos 
    291  1.1.1.2  christos     if (!TEST_true(PACKET_buf_init(&pkt, smbuf, BUF_LEN))
    292  1.1.1.2  christos             || !TEST_true(PACKET_forward(&pkt, 1))
    293  1.1.1.2  christos             || !TEST_true(PACKET_get_bytes(&pkt, &byte, 1))
    294  1.1.1.2  christos             || !TEST_uchar_eq(byte[0], 4)
    295  1.1.1.2  christos             || !TEST_true(PACKET_forward(&pkt, BUF_LEN - 3))
    296  1.1.1.2  christos             || !TEST_true(PACKET_get_bytes(&pkt, &byte, 1))
    297  1.1.1.2  christos             || !TEST_uchar_eq(byte[0], 0xfe))
    298      1.1  christos         return 0;
    299      1.1  christos 
    300      1.1  christos     return 1;
    301      1.1  christos }
    302      1.1  christos 
    303  1.1.1.2  christos static int test_PACKET_buf_init(void)
    304      1.1  christos {
    305  1.1.1.4  christos     unsigned char buf1[BUF_LEN] = { 0 };
    306      1.1  christos     PACKET pkt;
    307      1.1  christos 
    308      1.1  christos     /* Also tests PACKET_remaining() */
    309  1.1.1.2  christos     if (!TEST_true(PACKET_buf_init(&pkt, buf1, 4))
    310  1.1.1.2  christos             || !TEST_size_t_eq(PACKET_remaining(&pkt), 4)
    311  1.1.1.2  christos             || !TEST_true(PACKET_buf_init(&pkt, buf1, BUF_LEN))
    312  1.1.1.2  christos             || !TEST_size_t_eq(PACKET_remaining(&pkt), BUF_LEN)
    313  1.1.1.2  christos             || !TEST_false(PACKET_buf_init(&pkt, buf1, -1)))
    314      1.1  christos         return 0;
    315      1.1  christos 
    316      1.1  christos     return 1;
    317      1.1  christos }
    318      1.1  christos 
    319  1.1.1.2  christos static int test_PACKET_null_init(void)
    320      1.1  christos {
    321      1.1  christos     PACKET pkt;
    322      1.1  christos 
    323      1.1  christos     PACKET_null_init(&pkt);
    324  1.1.1.2  christos     if (!TEST_size_t_eq(PACKET_remaining(&pkt), 0)
    325  1.1.1.2  christos             || !TEST_false(PACKET_forward(&pkt, 1)))
    326      1.1  christos         return 0;
    327      1.1  christos 
    328      1.1  christos     return 1;
    329      1.1  christos }
    330      1.1  christos 
    331  1.1.1.2  christos static int test_PACKET_equal(void)
    332      1.1  christos {
    333      1.1  christos     PACKET pkt;
    334      1.1  christos 
    335  1.1.1.2  christos     if (!TEST_true(PACKET_buf_init(&pkt, smbuf, 4))
    336  1.1.1.2  christos             || !TEST_true(PACKET_equal(&pkt, smbuf, 4))
    337  1.1.1.2  christos             || !TEST_false(PACKET_equal(&pkt, smbuf + 1, 4))
    338  1.1.1.2  christos             || !TEST_true(PACKET_buf_init(&pkt, smbuf, BUF_LEN))
    339  1.1.1.2  christos             || !TEST_true(PACKET_equal(&pkt, smbuf, BUF_LEN))
    340  1.1.1.2  christos             || !TEST_false(PACKET_equal(&pkt, smbuf, BUF_LEN - 1))
    341  1.1.1.2  christos             || !TEST_false(PACKET_equal(&pkt, smbuf, BUF_LEN + 1))
    342  1.1.1.2  christos             || !TEST_false(PACKET_equal(&pkt, smbuf, 0)))
    343      1.1  christos         return 0;
    344      1.1  christos 
    345      1.1  christos     return 1;
    346      1.1  christos }
    347      1.1  christos 
    348  1.1.1.2  christos static int test_PACKET_get_length_prefixed_1(void)
    349      1.1  christos {
    350  1.1.1.2  christos     unsigned char buf1[BUF_LEN];
    351      1.1  christos     const size_t len = 16;
    352      1.1  christos     unsigned int i;
    353  1.1.1.5  christos     PACKET pkt, short_pkt, subpkt;
    354      1.1  christos 
    355  1.1.1.5  christos     memset(&subpkt, 0, sizeof(subpkt));
    356  1.1.1.2  christos     buf1[0] = (unsigned char)len;
    357  1.1.1.2  christos     for (i = 1; i < BUF_LEN; i++)
    358  1.1.1.2  christos         buf1[i] = (i * 2) & 0xff;
    359      1.1  christos 
    360  1.1.1.2  christos     if (!TEST_true(PACKET_buf_init(&pkt, buf1, BUF_LEN))
    361  1.1.1.2  christos             || !TEST_true(PACKET_buf_init(&short_pkt, buf1, len))
    362  1.1.1.2  christos             || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &subpkt))
    363  1.1.1.2  christos             || !TEST_size_t_eq(PACKET_remaining(&subpkt), len)
    364  1.1.1.2  christos             || !TEST_true(PACKET_get_net_2(&subpkt, &i))
    365  1.1.1.2  christos             || !TEST_uint_eq(i, 0x0204)
    366  1.1.1.2  christos             || !TEST_false(PACKET_get_length_prefixed_1(&short_pkt, &subpkt))
    367  1.1.1.2  christos             || !TEST_size_t_eq(PACKET_remaining(&short_pkt), len))
    368      1.1  christos         return 0;
    369      1.1  christos 
    370      1.1  christos     return 1;
    371      1.1  christos }
    372      1.1  christos 
    373  1.1.1.2  christos static int test_PACKET_get_length_prefixed_2(void)
    374      1.1  christos {
    375  1.1.1.2  christos     unsigned char buf1[1024];
    376      1.1  christos     const size_t len = 516;  /* 0x0204 */
    377      1.1  christos     unsigned int i;
    378  1.1.1.5  christos     PACKET pkt, short_pkt, subpkt;
    379      1.1  christos 
    380  1.1.1.5  christos     memset(&subpkt, 0, sizeof(subpkt));
    381  1.1.1.2  christos     for (i = 1; i <= 1024; i++)
    382  1.1.1.2  christos         buf1[i - 1] = (i * 2) & 0xff;
    383      1.1  christos 
    384  1.1.1.2  christos     if (!TEST_true(PACKET_buf_init(&pkt, buf1, 1024))
    385  1.1.1.2  christos             || !TEST_true(PACKET_buf_init(&short_pkt, buf1, len))
    386  1.1.1.2  christos             || !TEST_true(PACKET_get_length_prefixed_2(&pkt, &subpkt))
    387  1.1.1.2  christos             || !TEST_size_t_eq(PACKET_remaining(&subpkt), len)
    388  1.1.1.2  christos             || !TEST_true(PACKET_get_net_2(&subpkt, &i))
    389  1.1.1.2  christos             || !TEST_uint_eq(i, 0x0608)
    390  1.1.1.2  christos             || !TEST_false(PACKET_get_length_prefixed_2(&short_pkt, &subpkt))
    391  1.1.1.2  christos             || !TEST_size_t_eq(PACKET_remaining(&short_pkt), len))
    392      1.1  christos         return 0;
    393      1.1  christos 
    394      1.1  christos     return 1;
    395      1.1  christos }
    396      1.1  christos 
    397  1.1.1.2  christos static int test_PACKET_get_length_prefixed_3(void)
    398      1.1  christos {
    399  1.1.1.2  christos     unsigned char buf1[1024];
    400      1.1  christos     const size_t len = 516;  /* 0x000204 */
    401      1.1  christos     unsigned int i;
    402  1.1.1.5  christos     PACKET pkt, short_pkt, subpkt;
    403      1.1  christos 
    404  1.1.1.5  christos     memset(&subpkt, 0, sizeof(subpkt));
    405  1.1.1.2  christos     for (i = 0; i < 1024; i++)
    406  1.1.1.2  christos         buf1[i] = (i * 2) & 0xff;
    407      1.1  christos 
    408  1.1.1.2  christos     if (!TEST_true(PACKET_buf_init(&pkt, buf1, 1024))
    409  1.1.1.2  christos             || !TEST_true(PACKET_buf_init(&short_pkt, buf1, len))
    410  1.1.1.2  christos             || !TEST_true(PACKET_get_length_prefixed_3(&pkt, &subpkt))
    411  1.1.1.2  christos             || !TEST_size_t_eq(PACKET_remaining(&subpkt), len)
    412  1.1.1.2  christos             || !TEST_true(PACKET_get_net_2(&subpkt, &i))
    413  1.1.1.2  christos             || !TEST_uint_eq(i, 0x0608)
    414  1.1.1.2  christos             || !TEST_false(PACKET_get_length_prefixed_3(&short_pkt, &subpkt))
    415  1.1.1.2  christos             || !TEST_size_t_eq(PACKET_remaining(&short_pkt), len))
    416      1.1  christos         return 0;
    417      1.1  christos 
    418      1.1  christos     return 1;
    419      1.1  christos }
    420      1.1  christos 
    421  1.1.1.2  christos static int test_PACKET_as_length_prefixed_1(void)
    422      1.1  christos {
    423  1.1.1.2  christos     unsigned char buf1[BUF_LEN];
    424      1.1  christos     const size_t len = 16;
    425      1.1  christos     unsigned int i;
    426  1.1.1.5  christos     PACKET pkt, exact_pkt, subpkt;
    427      1.1  christos 
    428  1.1.1.5  christos     memset(&subpkt, 0, sizeof(subpkt));
    429  1.1.1.2  christos     buf1[0] = (unsigned char)len;
    430  1.1.1.2  christos     for (i = 1; i < BUF_LEN; i++)
    431  1.1.1.2  christos         buf1[i] = (i * 2) & 0xff;
    432      1.1  christos 
    433  1.1.1.2  christos     if (!TEST_true(PACKET_buf_init(&pkt, buf1, BUF_LEN))
    434  1.1.1.2  christos             || !TEST_true(PACKET_buf_init(&exact_pkt, buf1, len + 1))
    435  1.1.1.2  christos             || !TEST_false(PACKET_as_length_prefixed_1(&pkt, &subpkt))
    436  1.1.1.2  christos             || !TEST_size_t_eq(PACKET_remaining(&pkt), BUF_LEN)
    437  1.1.1.2  christos             || !TEST_true(PACKET_as_length_prefixed_1(&exact_pkt, &subpkt))
    438  1.1.1.2  christos             || !TEST_size_t_eq(PACKET_remaining(&exact_pkt), 0)
    439  1.1.1.2  christos             || !TEST_size_t_eq(PACKET_remaining(&subpkt), len))
    440      1.1  christos         return 0;
    441      1.1  christos 
    442      1.1  christos     return 1;
    443      1.1  christos }
    444      1.1  christos 
    445  1.1.1.2  christos static int test_PACKET_as_length_prefixed_2(void)
    446      1.1  christos {
    447      1.1  christos     unsigned char buf[1024];
    448      1.1  christos     const size_t len = 516;  /* 0x0204 */
    449      1.1  christos     unsigned int i;
    450  1.1.1.5  christos     PACKET pkt, exact_pkt, subpkt;
    451      1.1  christos 
    452  1.1.1.5  christos     memset(&subpkt, 0, sizeof(subpkt));
    453  1.1.1.2  christos     for (i = 1; i <= 1024; i++)
    454      1.1  christos         buf[i-1] = (i * 2) & 0xff;
    455      1.1  christos 
    456  1.1.1.2  christos     if (!TEST_true(PACKET_buf_init(&pkt, buf, 1024))
    457  1.1.1.2  christos             || !TEST_true(PACKET_buf_init(&exact_pkt, buf, len + 2))
    458  1.1.1.2  christos             || !TEST_false(PACKET_as_length_prefixed_2(&pkt, &subpkt))
    459  1.1.1.2  christos             || !TEST_size_t_eq(PACKET_remaining(&pkt), 1024)
    460  1.1.1.2  christos             || !TEST_true(PACKET_as_length_prefixed_2(&exact_pkt, &subpkt))
    461  1.1.1.2  christos             || !TEST_size_t_eq(PACKET_remaining(&exact_pkt), 0)
    462  1.1.1.2  christos             || !TEST_size_t_eq(PACKET_remaining(&subpkt), len))
    463      1.1  christos         return 0;
    464      1.1  christos 
    465      1.1  christos     return 1;
    466      1.1  christos }
    467      1.1  christos 
    468  1.1.1.2  christos int setup_tests(void)
    469      1.1  christos {
    470      1.1  christos     unsigned int i;
    471      1.1  christos 
    472  1.1.1.2  christos     for (i = 1; i <= BUF_LEN; i++)
    473  1.1.1.2  christos         smbuf[i - 1] = (i * 2) & 0xff;
    474      1.1  christos 
    475  1.1.1.2  christos     ADD_TEST(test_PACKET_buf_init);
    476  1.1.1.2  christos     ADD_TEST(test_PACKET_null_init);
    477  1.1.1.2  christos     ADD_TEST(test_PACKET_remaining);
    478  1.1.1.2  christos     ADD_TEST(test_PACKET_end);
    479  1.1.1.2  christos     ADD_TEST(test_PACKET_equal);
    480  1.1.1.2  christos     ADD_TEST(test_PACKET_get_1);
    481  1.1.1.2  christos     ADD_TEST(test_PACKET_get_4);
    482  1.1.1.2  christos     ADD_TEST(test_PACKET_get_net_2);
    483  1.1.1.2  christos     ADD_TEST(test_PACKET_get_net_3);
    484  1.1.1.2  christos     ADD_TEST(test_PACKET_get_net_4);
    485  1.1.1.2  christos     ADD_TEST(test_PACKET_get_sub_packet);
    486  1.1.1.2  christos     ADD_TEST(test_PACKET_get_bytes);
    487  1.1.1.2  christos     ADD_TEST(test_PACKET_copy_bytes);
    488  1.1.1.2  christos     ADD_TEST(test_PACKET_copy_all);
    489  1.1.1.2  christos     ADD_TEST(test_PACKET_memdup);
    490  1.1.1.2  christos     ADD_TEST(test_PACKET_strndup);
    491  1.1.1.2  christos     ADD_TEST(test_PACKET_contains_zero_byte);
    492  1.1.1.2  christos     ADD_TEST(test_PACKET_forward);
    493  1.1.1.2  christos     ADD_TEST(test_PACKET_get_length_prefixed_1);
    494  1.1.1.2  christos     ADD_TEST(test_PACKET_get_length_prefixed_2);
    495  1.1.1.2  christos     ADD_TEST(test_PACKET_get_length_prefixed_3);
    496  1.1.1.2  christos     ADD_TEST(test_PACKET_as_length_prefixed_1);
    497  1.1.1.2  christos     ADD_TEST(test_PACKET_as_length_prefixed_2);
    498  1.1.1.2  christos     return 1;
    499      1.1  christos }
    500