blob: 18e7ff0f841b623ebe207d93a50fd63293fce110 [file] [log] [blame]
Jakub Czapiga9f13cb72021-02-25 13:13:20 +01001/* 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
9static const uint8_t test_data_simple[] = {
10 0x64, 0x3b, 0x33, 0x17, 0x34, 0x74, 0x62, 0x30,
11 0x75, 0x73, 0xf3, 0x11, 0x30, 0x2c, 0x34, 0x35,
12 0x6d, 0x39, 0x69, 0x32, 0x23, 0x24, 0x76, 0x71,
13 0x77, 0x30, 0x39, 0x75, 0x76, 0x35, 0x71, 0x32,
14 0x40, 0x46, 0x34, 0x34, 0xBB, 0x03, 0x66, 0x52
15};
16static const size_t test_data_simple_sz = ARRAY_SIZE(test_data_simple);
17static const unsigned long test_data_simple_checksum = 0x4267;
18
19static uint8_t test_data_zeros[1024];
20static const size_t test_data_zeros_sz = ARRAY_SIZE(test_data_zeros);
21static const unsigned long test_data_zeros_checksum = 0xFFFF;
22
23static int setup_test_group(void **state)
24{
25 memset(test_data_zeros, 0, test_data_zeros_sz);
26
27 return 0;
28}
29
30static void test_compute_ip_checksum_zero_length(void **state)
31{
32 unsigned long res = compute_ip_checksum(test_data_simple, 0);
33
34 /* Expect checksum to be in initial state as there are were no data provided. */
35 assert_int_equal(0xFFFF, res);
36}
37
38static void test_compute_ip_checksum_zero_buffer(void **state)
39{
40 unsigned long res = compute_ip_checksum(test_data_zeros, test_data_zeros_sz);
41 assert_int_equal(test_data_zeros_checksum, res);
42}
43
44static void test_compute_ip_checksum_simple_data(void **state)
45{
46 unsigned long res;
47 unsigned long check_res;
48 const size_t helper_buffer_size = sizeof(uint8_t) * (test_data_simple_sz + 2);
49 char *helper_buffer = malloc(helper_buffer_size);
50
51 /* Self test */
52 assert_non_null(helper_buffer);
53
54 /* Expect function to generate the same checksum as stored in */
55 res = compute_ip_checksum(test_data_simple, test_data_simple_sz);
56 assert_int_equal(test_data_simple_checksum, res);
57
58 /* Copy test data and checksum to new buffer. Expect computed checksum to be zero,
59 as it proves the data and the checksum are correct. */
60 memcpy(helper_buffer, test_data_simple, test_data_simple_sz);
61 helper_buffer[helper_buffer_size - 2] = res & 0xFF;
62 helper_buffer[helper_buffer_size - 1] = (res >> 8) & 0xFF;
63 check_res = compute_ip_checksum(helper_buffer, helper_buffer_size);
64 assert_int_equal(0, check_res);
65
66 free(helper_buffer);
67}
68
69static void test_add_ip_checksums_empty_values(void **state)
70{
71 unsigned long res;
72
73 res = add_ip_checksums(0, 0xFFFF, 0xFFFF);
74 assert_int_equal(0xFFFF, res);
75
76 res = add_ip_checksums(1, 0xFFFF, 0xFFFF);
77 assert_int_equal(0xFFFF, res);
78}
79
80static void test_add_ip_checksums(void **state)
81{
82 unsigned long res_1 = compute_ip_checksum(test_data_simple, test_data_simple_sz / 2);
83 unsigned long res_2 = compute_ip_checksum(test_data_simple + test_data_simple_sz / 2,
84 test_data_simple_sz / 2);
85 unsigned long res_sum = add_ip_checksums(test_data_simple_sz / 2, res_1, res_2);
86
87 assert_int_equal(test_data_simple_checksum, res_sum);
88}
89
90int main(void)
91{
92 const struct CMUnitTest tests[] = {
93 cmocka_unit_test(test_compute_ip_checksum_zero_length),
94 cmocka_unit_test(test_compute_ip_checksum_zero_buffer),
95 cmocka_unit_test(test_compute_ip_checksum_simple_data),
96
97 cmocka_unit_test(test_add_ip_checksums_empty_values),
98 cmocka_unit_test(test_add_ip_checksums),
99 };
100
Jakub Czapiga7c6081e2021-08-25 16:27:35 +0200101 return cb_run_group_tests(tests, setup_test_group, NULL);
Jakub Czapiga9f13cb72021-02-25 13:13:20 +0100102}