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