summaryrefslogtreecommitdiff
path: root/tests/commonlib/bsd/ipchksum-test.c
diff options
context:
space:
mode:
authorJulius Werner <jwerner@chromium.org>2024-01-30 17:12:30 -0800
committerJulius Werner <jwerner@chromium.org>2024-02-02 22:48:37 +0000
commitd385113cc4d5ccaf1558c1984d63251c5c82b123 (patch)
treece6fc2a8aabd81b46a9dc8a839d249fbd4d5b452 /tests/commonlib/bsd/ipchksum-test.c
parentde37109767b6b415778f34cbac196c8418f7e371 (diff)
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 <jwerner@chromium.org> Reviewed-on: https://review.coreboot.org/c/coreboot/+/80252 Tested-by: build bot (Jenkins) <no-reply@coreboot.org> Reviewed-by: Arthur Heymans <arthur@aheymans.xyz> Reviewed-by: Jakub Czapiga <czapiga@google.com>
Diffstat (limited to 'tests/commonlib/bsd/ipchksum-test.c')
-rw-r--r--tests/commonlib/bsd/ipchksum-test.c30
1 files changed, 30 insertions, 0 deletions
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),