xref: /freebsd/sbin/ping/tests/in_cksum_test.c (revision c66ec88fed842fbaad62c30d510644ceb7bd2d71)
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);
43 ATF_TC_BODY(aligned_even_length, 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 	u_char *c_sum = (u_char *)&sum;
51 	ATF_REQUIRE(c_sum[0] == 0x97 && c_sum[1] == 0x53);
52 }
53 
54 ATF_TC_WITHOUT_HEAD(aligned_odd_length);
55 ATF_TC_BODY(aligned_odd_length, tc)
56 {
57 	u_char data[] __aligned(sizeof(u_short)) =
58 		{0x12, 0x34, 0x56, 0x78, 0x9a};
59 	u_short sum;
60 
61 	sum = in_cksum(data, nitems(data));
62 	u_char *c_sum = (u_char *)&sum;
63 	ATF_REQUIRE(c_sum[0] == 0xfd && c_sum[1] == 0x52);
64 }
65 
66 ATF_TC_WITHOUT_HEAD(unaligned_even_length);
67 ATF_TC_BODY(unaligned_even_length, tc)
68 {
69 	u_char data[] __aligned(sizeof(u_short)) =
70 		{0x00, 0x12, 0x34, 0x56, 0x78};
71 	u_short sum;
72 
73 	sum = in_cksum(data + 1, nitems(data) - 1);
74 	u_char *c_sum = (u_char *)&sum;
75 	ATF_REQUIRE(c_sum[0] == 0x97 && c_sum[1] == 0x53);
76 }
77 
78 ATF_TC_WITHOUT_HEAD(unaligned_odd_length);
79 ATF_TC_BODY(unaligned_odd_length, tc)
80 {
81 	u_char data[] __aligned(sizeof(u_short)) =
82 		{0x00, 0x12, 0x34, 0x56, 0x78, 0x9a};
83 	u_short sum;
84 
85 	sum = in_cksum(data + 1, nitems(data) - 1);
86 	u_char *c_sum = (u_char *)&sum;
87 	ATF_REQUIRE(c_sum[0] == 0xfd && c_sum[1] == 0x52);
88 }
89 
90 /*
91  * Main.
92  */
93 
94 ATF_TP_ADD_TCS(tp)
95 {
96 	ATF_TP_ADD_TC(tp, aligned_even_length);
97 	ATF_TP_ADD_TC(tp, aligned_odd_length);
98 	ATF_TP_ADD_TC(tp, unaligned_even_length);
99 	ATF_TP_ADD_TC(tp, unaligned_odd_length);
100 
101 	return (atf_no_error());
102 }
103