From d385113cc4d5ccaf1558c1984d63251c5c82b123 Mon Sep 17 00:00:00 2001 From: Julius Werner Date: Tue, 30 Jan 2024 17:12:30 -0800 Subject: [PATCH] tests: Add some more ipchksum() test cases This patch adds a few more test cases for the IP checksum algorithm to catch more possible corner cases (large data with more than 64K carries, unaligned data, checksum addition with offset, etc.). Change-Id: I39b4d3f1bb833894985649872329eec88a02a22c Signed-off-by: Julius Werner Reviewed-on: https://review.coreboot.org/c/coreboot/+/80252 Tested-by: build bot (Jenkins) Reviewed-by: Arthur Heymans Reviewed-by: Jakub Czapiga --- tests/commonlib/bsd/ipchksum-test.c | 30 +++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tests/commonlib/bsd/ipchksum-test.c b/tests/commonlib/bsd/ipchksum-test.c index 2aed94c0cf..3231e21376 100644 --- a/tests/commonlib/bsd/ipchksum-test.c +++ b/tests/commonlib/bsd/ipchksum-test.c @@ -63,6 +63,23 @@ static void test_ipchksum_simple_data(void **state) free(helper_buffer); } +static void test_ipchksum_80kff(void **state) +{ + /* 64K is an important boundary since naive 32-bit sum implementations that accumulate + carries may run over after that point. */ + size_t buffer_sz = 80 * 1024; + char *buffer = malloc(buffer_sz); + + memset(buffer, 0xff, buffer_sz); + assert_int_equal(ipchksum(buffer, buffer_sz), 0); + + /* Make things a bit more interesting... */ + memcpy(buffer + 0x6789, test_data_simple, test_data_simple_sz); + assert_int_equal(ipchksum(buffer, buffer_sz), 0x6742); + + free(buffer); +} + static void test_ipchksum_add_empty_values(void **state) { uint16_t res; @@ -81,7 +98,19 @@ static void test_ipchksum_add(void **state) test_data_simple_sz / 2); uint16_t res_sum = ipchksum_add(test_data_simple_sz / 2, res_1, res_2); + assert_int_equal(0xb62e, res_1); + assert_int_equal(0x8c38, res_2); assert_int_equal(test_data_simple_checksum, res_sum); + + /* Test some unaligned sums */ + res_1 = ipchksum(test_data_simple, 17); + res_2 = ipchksum(test_data_simple + 17, test_data_simple_sz - 17); + res_sum = ipchksum_add(17, res_1, res_2); + + assert_int_equal(0x2198, res_1); + assert_int_equal(0xcf20, res_2); + assert_int_equal(test_data_simple_checksum, res_sum); + } int main(void) @@ -90,6 +119,7 @@ int main(void) cmocka_unit_test(test_ipchksum_zero_length), cmocka_unit_test(test_ipchksum_zero_buffer), cmocka_unit_test(test_ipchksum_simple_data), + cmocka_unit_test(test_ipchksum_80kff), cmocka_unit_test(test_ipchksum_add_empty_values), cmocka_unit_test(test_ipchksum_add),