xref: /freebsd/sbin/ping/tests/in_cksum_test.c (revision edf8578117e8844e02c0121147f45e4609b30680)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 #include <sys/param.h>
31 
32 #include <atf-c.h>
33 
34 #include "../utils.h"
35 
36 /*
37  * Test cases.
38  */
39 
40 ATF_TC_WITHOUT_HEAD(aligned_even_length);
41 ATF_TC_BODY(aligned_even_length, tc)
42 {
43 	u_char data[] __aligned(sizeof(u_short)) =
44 		{0x12, 0x34, 0x56, 0x78};
45 	u_short sum;
46 
47 	sum = in_cksum(data, nitems(data));
48 	u_char *c_sum = (u_char *)&sum;
49 	ATF_REQUIRE(c_sum[0] == 0x97 && c_sum[1] == 0x53);
50 }
51 
52 ATF_TC_WITHOUT_HEAD(aligned_odd_length);
53 ATF_TC_BODY(aligned_odd_length, tc)
54 {
55 	u_char data[] __aligned(sizeof(u_short)) =
56 		{0x12, 0x34, 0x56, 0x78, 0x9a};
57 	u_short sum;
58 
59 	sum = in_cksum(data, nitems(data));
60 	u_char *c_sum = (u_char *)&sum;
61 	ATF_REQUIRE(c_sum[0] == 0xfd && c_sum[1] == 0x52);
62 }
63 
64 ATF_TC_WITHOUT_HEAD(unaligned_even_length);
65 ATF_TC_BODY(unaligned_even_length, tc)
66 {
67 	u_char data[] __aligned(sizeof(u_short)) =
68 		{0x00, 0x12, 0x34, 0x56, 0x78};
69 	u_short sum;
70 
71 	sum = in_cksum(data + 1, nitems(data) - 1);
72 	u_char *c_sum = (u_char *)&sum;
73 	ATF_REQUIRE(c_sum[0] == 0x97 && c_sum[1] == 0x53);
74 }
75 
76 ATF_TC_WITHOUT_HEAD(unaligned_odd_length);
77 ATF_TC_BODY(unaligned_odd_length, tc)
78 {
79 	u_char data[] __aligned(sizeof(u_short)) =
80 		{0x00, 0x12, 0x34, 0x56, 0x78, 0x9a};
81 	u_short sum;
82 
83 	sum = in_cksum(data + 1, nitems(data) - 1);
84 	u_char *c_sum = (u_char *)&sum;
85 	ATF_REQUIRE(c_sum[0] == 0xfd && c_sum[1] == 0x52);
86 }
87 
88 /*
89  * Main.
90  */
91 
92 ATF_TP_ADD_TCS(tp)
93 {
94 	ATF_TP_ADD_TC(tp, aligned_even_length);
95 	ATF_TP_ADD_TC(tp, aligned_odd_length);
96 	ATF_TP_ADD_TC(tp, unaligned_even_length);
97 	ATF_TP_ADD_TC(tp, unaligned_odd_length);
98 
99 	return (atf_no_error());
100 }
101