xref: /aosp_15_r20/external/coreboot/tests/commonlib/bsd/ipchksum-test.c (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #include <commonlib/bsd/ipchksum.h>
4 #include <tests/test.h>
5 #include <string.h>
6 #include <stdlib.h>
7 #include <types.h>
8 
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;
15 
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;
19 
setup_test_group(void ** state)20 static int setup_test_group(void **state)
21 {
22 	memset(test_data_zeros, 0, test_data_zeros_sz);
23 
24 	return 0;
25 }
26 
test_ipchksum_zero_length(void ** state)27 static void test_ipchksum_zero_length(void **state)
28 {
29 	uint16_t res = ipchksum(test_data_simple, 0);
30 
31 	/* Expect checksum to be in initial state as there are were no data provided. */
32 	assert_int_equal(0xFFFF, res);
33 }
34 
test_ipchksum_zero_buffer(void ** state)35 static void test_ipchksum_zero_buffer(void **state)
36 {
37 	uint16_t res = ipchksum(test_data_zeros, test_data_zeros_sz);
38 	assert_int_equal(test_data_zeros_checksum, res);
39 }
40 
test_ipchksum_simple_data(void ** state)41 static void test_ipchksum_simple_data(void **state)
42 {
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);
47 
48 	/* Self test */
49 	assert_non_null(helper_buffer);
50 
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);
54 
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);
62 
63 	free(helper_buffer);
64 }
65 
test_ipchksum_80kff(void ** state)66 static void test_ipchksum_80kff(void **state)
67 {
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);
72 
73 	memset(buffer, 0xff, buffer_sz);
74 	assert_int_equal(ipchksum(buffer, buffer_sz), 0);
75 
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);
79 
80 	free(buffer);
81 }
82 
test_ipchksum_add_empty_values(void ** state)83 static void test_ipchksum_add_empty_values(void **state)
84 {
85 	uint16_t res;
86 
87 	res = ipchksum_add(0, 0xFFFF, 0xFFFF);
88 	assert_int_equal(0xFFFF, res);
89 
90 	res = ipchksum_add(1, 0xFFFF, 0xFFFF);
91 	assert_int_equal(0xFFFF, res);
92 }
93 
test_ipchksum_add(void ** state)94 static void test_ipchksum_add(void **state)
95 {
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);
100 
101 	assert_int_equal(0xb62e, res_1);
102 	assert_int_equal(0x8c38, res_2);
103 	assert_int_equal(test_data_simple_checksum, res_sum);
104 
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);
109 
110 	assert_int_equal(0x2198, res_1);
111 	assert_int_equal(0xcf20, res_2);
112 	assert_int_equal(test_data_simple_checksum, res_sum);
113 
114 }
115 
main(void)116 int main(void)
117 {
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),
123 
124 		cmocka_unit_test(test_ipchksum_add_empty_values),
125 		cmocka_unit_test(test_ipchksum_add),
126 	};
127 
128 	return cb_run_group_tests(tests, setup_test_group, NULL);
129 }
130