xref: /freebsd/sbin/ping/tests/in_cksum_test.c (revision eda14cbc264d6969b02f2b1994cef11148e914f1)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (C) 2019 Jan Sucan <jansucan@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 
34 #include <atf-c.h>
35 
36 #include "../utils.h"
37 
38 /*
39  * Test cases.
40  */
41 
42 ATF_TC_WITHOUT_HEAD(aligned_even_length_big_endian);
43 ATF_TC_BODY(aligned_even_length_big_endian, tc)
44 {
45 	u_char data[] __aligned(sizeof(u_short)) =
46 		{0x12, 0x34, 0x56, 0x78};
47 	u_short sum;
48 
49 	sum = in_cksum(data, nitems(data));
50 	ATF_REQUIRE(sum == 0x5397);
51 }
52 
53 ATF_TC_WITHOUT_HEAD(aligned_odd_length_big_endian);
54 ATF_TC_BODY(aligned_odd_length_big_endian, tc)
55 {
56 	u_char data[] __aligned(sizeof(u_short)) =
57 		{0x12, 0x34, 0x56, 0x78, 0x9a};
58 	u_short sum;
59 
60 	sum = in_cksum(data, nitems(data));
61 	ATF_REQUIRE(sum == 0x52fd);
62 }
63 
64 ATF_TC_WITHOUT_HEAD(aligned_even_length_little_endian);
65 ATF_TC_BODY(aligned_even_length_little_endian, tc)
66 {
67 	u_char data[] __aligned(sizeof(u_short)) =
68 		{0x34, 0x12, 0x78, 0x56};
69 	u_short sum;
70 
71 	sum = in_cksum(data, nitems(data));
72 	ATF_REQUIRE_MSG(sum == 0x9753, "%d", sum);
73 }
74 
75 ATF_TC_WITHOUT_HEAD(aligned_odd_length_little_endian);
76 ATF_TC_BODY(aligned_odd_length_little_endian, tc)
77 {
78 	u_char data[] __aligned(sizeof(u_short)) =
79 		{0x34, 0x12, 0x78, 0x56, 0x00, 0x9a};
80 	u_short sum;
81 
82 	sum = in_cksum(data, nitems(data));
83 	ATF_REQUIRE(sum == 0xfd52);
84 }
85 
86 ATF_TC_WITHOUT_HEAD(unaligned_even_length_big_endian);
87 ATF_TC_BODY(unaligned_even_length_big_endian, tc)
88 {
89 	u_char data[] __aligned(sizeof(u_short)) =
90 		{0x00, 0x12, 0x34, 0x56, 0x78};
91 	u_short sum;
92 
93 	sum = in_cksum(data + 1, nitems(data) - 1);
94 	ATF_REQUIRE(sum == 0x5397);
95 }
96 
97 ATF_TC_WITHOUT_HEAD(unaligned_odd_length_big_endian);
98 ATF_TC_BODY(unaligned_odd_length_big_endian, tc)
99 {
100 	u_char data[] __aligned(sizeof(u_short)) =
101 		{0x00, 0x12, 0x34, 0x56, 0x78, 0x9a};
102 	u_short sum;
103 
104 	sum = in_cksum(data + 1, nitems(data) - 1);
105 	ATF_REQUIRE(sum == 0x52fd);
106 }
107 
108 ATF_TC_WITHOUT_HEAD(unaligned_even_length_little_endian);
109 ATF_TC_BODY(unaligned_even_length_little_endian, tc)
110 {
111 	u_char data[] __aligned(sizeof(u_short)) =
112 		{0x00, 0x34, 0x12, 0x78, 0x56};
113 	u_short sum;
114 
115 	sum = in_cksum(data + 1, nitems(data) - 1);
116 	ATF_REQUIRE_MSG(sum == 0x9753, "%d", sum);
117 }
118 
119 ATF_TC_WITHOUT_HEAD(unaligned_odd_length_little_endian);
120 ATF_TC_BODY(unaligned_odd_length_little_endian, tc)
121 {
122 	u_char data[] __aligned(sizeof(u_short)) =
123 		{0x00, 0x34, 0x12, 0x78, 0x56, 0x00, 0x9a};
124 	u_short sum;
125 
126 	sum = in_cksum(data + 1, nitems(data) - 1);
127 	ATF_REQUIRE(sum == 0xfd52);
128 }
129 
130 /*
131  * Main.
132  */
133 
134 ATF_TP_ADD_TCS(tp)
135 {
136 	ATF_TP_ADD_TC(tp, aligned_even_length_big_endian);
137 	ATF_TP_ADD_TC(tp, aligned_odd_length_big_endian);
138 	ATF_TP_ADD_TC(tp, aligned_even_length_little_endian);
139 	ATF_TP_ADD_TC(tp, aligned_odd_length_little_endian);
140 	ATF_TP_ADD_TC(tp, unaligned_even_length_big_endian);
141 	ATF_TP_ADD_TC(tp, unaligned_odd_length_big_endian);
142 	ATF_TP_ADD_TC(tp, unaligned_even_length_little_endian);
143 	ATF_TP_ADD_TC(tp, unaligned_odd_length_little_endian);
144 
145 	return (atf_no_error());
146 }
147