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