drivers/wifi: Remove unnecessary data structure copy
[coreboot2.git] / tests / commonlib / bsd / ipchksum-test.c
blob3231e21376ef3414b3d611d73a6cec4868b32432
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <commonlib/bsd/ipchksum.h>
4 #include <tests/test.h>
5 #include <string.h>
6 #include <stdlib.h>
7 #include <types.h>
9 static const uint8_t test_data_simple[] = {
10 0x64, 0x3b, 0x33, 0x17, 0x34, 0x74, 0x62, 0x30, 0x75, 0x73, 0xf3, 0x11, 0x30, 0x2c,
11 0x34, 0x35, 0x6d, 0x39, 0x69, 0x32, 0x23, 0x24, 0x76, 0x71, 0x77, 0x30, 0x39, 0x75,
12 0x76, 0x35, 0x71, 0x32, 0x40, 0x46, 0x34, 0x34, 0xBB, 0x03, 0x66, 0x52};
13 static const size_t test_data_simple_sz = ARRAY_SIZE(test_data_simple);
14 static const uint16_t test_data_simple_checksum = 0x4267;
16 static uint8_t test_data_zeros[1024];
17 static const size_t test_data_zeros_sz = ARRAY_SIZE(test_data_zeros);
18 static const uint16_t test_data_zeros_checksum = 0xFFFF;
20 static int setup_test_group(void **state)
22 memset(test_data_zeros, 0, test_data_zeros_sz);
24 return 0;
27 static void test_ipchksum_zero_length(void **state)
29 uint16_t res = ipchksum(test_data_simple, 0);
31 /* Expect checksum to be in initial state as there are were no data provided. */
32 assert_int_equal(0xFFFF, res);
35 static void test_ipchksum_zero_buffer(void **state)
37 uint16_t res = ipchksum(test_data_zeros, test_data_zeros_sz);
38 assert_int_equal(test_data_zeros_checksum, res);
41 static void test_ipchksum_simple_data(void **state)
43 uint16_t res;
44 uint16_t check_res;
45 const size_t helper_buffer_size = sizeof(uint8_t) * (test_data_simple_sz + 2);
46 char *helper_buffer = malloc(helper_buffer_size);
48 /* Self test */
49 assert_non_null(helper_buffer);
51 /* Expect function to generate the same checksum as stored in */
52 res = ipchksum(test_data_simple, test_data_simple_sz);
53 assert_int_equal(test_data_simple_checksum, res);
55 /* Copy test data and checksum to new buffer. Expect computed checksum to be zero,
56 as it proves the data and the checksum are correct. */
57 memcpy(helper_buffer, test_data_simple, test_data_simple_sz);
58 helper_buffer[helper_buffer_size - 2] = res & 0xFF;
59 helper_buffer[helper_buffer_size - 1] = (res >> 8) & 0xFF;
60 check_res = ipchksum(helper_buffer, helper_buffer_size);
61 assert_int_equal(0, check_res);
63 free(helper_buffer);
66 static void test_ipchksum_80kff(void **state)
68 /* 64K is an important boundary since naive 32-bit sum implementations that accumulate
69 carries may run over after that point. */
70 size_t buffer_sz = 80 * 1024;
71 char *buffer = malloc(buffer_sz);
73 memset(buffer, 0xff, buffer_sz);
74 assert_int_equal(ipchksum(buffer, buffer_sz), 0);
76 /* Make things a bit more interesting... */
77 memcpy(buffer + 0x6789, test_data_simple, test_data_simple_sz);
78 assert_int_equal(ipchksum(buffer, buffer_sz), 0x6742);
80 free(buffer);
83 static void test_ipchksum_add_empty_values(void **state)
85 uint16_t res;
87 res = ipchksum_add(0, 0xFFFF, 0xFFFF);
88 assert_int_equal(0xFFFF, res);
90 res = ipchksum_add(1, 0xFFFF, 0xFFFF);
91 assert_int_equal(0xFFFF, res);
94 static void test_ipchksum_add(void **state)
96 uint16_t res_1 = ipchksum(test_data_simple, test_data_simple_sz / 2);
97 uint16_t res_2 = ipchksum(test_data_simple + test_data_simple_sz / 2,
98 test_data_simple_sz / 2);
99 uint16_t res_sum = ipchksum_add(test_data_simple_sz / 2, res_1, res_2);
101 assert_int_equal(0xb62e, res_1);
102 assert_int_equal(0x8c38, res_2);
103 assert_int_equal(test_data_simple_checksum, res_sum);
105 /* Test some unaligned sums */
106 res_1 = ipchksum(test_data_simple, 17);
107 res_2 = ipchksum(test_data_simple + 17, test_data_simple_sz - 17);
108 res_sum = ipchksum_add(17, res_1, res_2);
110 assert_int_equal(0x2198, res_1);
111 assert_int_equal(0xcf20, res_2);
112 assert_int_equal(test_data_simple_checksum, res_sum);
116 int main(void)
118 const struct CMUnitTest tests[] = {
119 cmocka_unit_test(test_ipchksum_zero_length),
120 cmocka_unit_test(test_ipchksum_zero_buffer),
121 cmocka_unit_test(test_ipchksum_simple_data),
122 cmocka_unit_test(test_ipchksum_80kff),
124 cmocka_unit_test(test_ipchksum_add_empty_values),
125 cmocka_unit_test(test_ipchksum_add),
128 return cb_run_group_tests(tests, setup_test_group, NULL);