Jakub Czapiga | 9f13cb7 | 2021-02-25 13:13:20 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | |
| 3 | #include <tests/test.h> |
| 4 | #include <string.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <types.h> |
| 7 | #include <ip_checksum.h> |
| 8 | |
| 9 | static const uint8_t test_data_simple[] = { |
Jakub Czapiga | c08b6a7 | 2022-01-10 13:36:47 +0000 | [diff] [blame] | 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}; |
Jakub Czapiga | 9f13cb7 | 2021-02-25 13:13:20 +0100 | [diff] [blame] | 13 | static const size_t test_data_simple_sz = ARRAY_SIZE(test_data_simple); |
| 14 | static const unsigned long 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 unsigned long test_data_zeros_checksum = 0xFFFF; |
| 19 | |
| 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 | |
| 27 | static void test_compute_ip_checksum_zero_length(void **state) |
| 28 | { |
| 29 | unsigned long res = compute_ip_checksum(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 | |
| 35 | static void test_compute_ip_checksum_zero_buffer(void **state) |
| 36 | { |
| 37 | unsigned long res = compute_ip_checksum(test_data_zeros, test_data_zeros_sz); |
| 38 | assert_int_equal(test_data_zeros_checksum, res); |
| 39 | } |
| 40 | |
| 41 | static void test_compute_ip_checksum_simple_data(void **state) |
| 42 | { |
| 43 | unsigned long res; |
| 44 | unsigned long 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 = compute_ip_checksum(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 = compute_ip_checksum(helper_buffer, helper_buffer_size); |
| 61 | assert_int_equal(0, check_res); |
| 62 | |
| 63 | free(helper_buffer); |
| 64 | } |
| 65 | |
| 66 | static void test_add_ip_checksums_empty_values(void **state) |
| 67 | { |
| 68 | unsigned long res; |
| 69 | |
| 70 | res = add_ip_checksums(0, 0xFFFF, 0xFFFF); |
| 71 | assert_int_equal(0xFFFF, res); |
| 72 | |
| 73 | res = add_ip_checksums(1, 0xFFFF, 0xFFFF); |
| 74 | assert_int_equal(0xFFFF, res); |
| 75 | } |
| 76 | |
| 77 | static void test_add_ip_checksums(void **state) |
| 78 | { |
| 79 | unsigned long res_1 = compute_ip_checksum(test_data_simple, test_data_simple_sz / 2); |
| 80 | unsigned long res_2 = compute_ip_checksum(test_data_simple + test_data_simple_sz / 2, |
Jakub Czapiga | c08b6a7 | 2022-01-10 13:36:47 +0000 | [diff] [blame] | 81 | test_data_simple_sz / 2); |
Jakub Czapiga | 9f13cb7 | 2021-02-25 13:13:20 +0100 | [diff] [blame] | 82 | unsigned long res_sum = add_ip_checksums(test_data_simple_sz / 2, res_1, res_2); |
| 83 | |
| 84 | assert_int_equal(test_data_simple_checksum, res_sum); |
| 85 | } |
| 86 | |
| 87 | int main(void) |
| 88 | { |
| 89 | const struct CMUnitTest tests[] = { |
| 90 | cmocka_unit_test(test_compute_ip_checksum_zero_length), |
| 91 | cmocka_unit_test(test_compute_ip_checksum_zero_buffer), |
| 92 | cmocka_unit_test(test_compute_ip_checksum_simple_data), |
| 93 | |
| 94 | cmocka_unit_test(test_add_ip_checksums_empty_values), |
| 95 | cmocka_unit_test(test_add_ip_checksums), |
| 96 | }; |
| 97 | |
Jakub Czapiga | 7c6081e | 2021-08-25 16:27:35 +0200 | [diff] [blame] | 98 | return cb_run_group_tests(tests, setup_test_group, NULL); |
Jakub Czapiga | 9f13cb7 | 2021-02-25 13:13:20 +0100 | [diff] [blame] | 99 | } |