1c43633c6SAlan Somers /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3c43633c6SAlan Somers *
4c43633c6SAlan Somers * Copyright (C) 2019 Jan Sucan <jansucan@FreeBSD.org>
5c43633c6SAlan Somers * All rights reserved.
6c43633c6SAlan Somers *
7c43633c6SAlan Somers * Redistribution and use in source and binary forms, with or without
8c43633c6SAlan Somers * modification, are permitted provided that the following conditions
9c43633c6SAlan Somers * are met:
10c43633c6SAlan Somers * 1. Redistributions of source code must retain the above copyright
11c43633c6SAlan Somers * notice, this list of conditions and the following disclaimer.
12c43633c6SAlan Somers * 2. Redistributions in binary form must reproduce the above copyright
13c43633c6SAlan Somers * notice, this list of conditions and the following disclaimer in the
14c43633c6SAlan Somers * documentation and/or other materials provided with the distribution.
15c43633c6SAlan Somers *
16c43633c6SAlan Somers * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17c43633c6SAlan Somers * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18c43633c6SAlan Somers * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19c43633c6SAlan Somers * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20c43633c6SAlan Somers * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21c43633c6SAlan Somers * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22c43633c6SAlan Somers * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23c43633c6SAlan Somers * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24c43633c6SAlan Somers * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25c43633c6SAlan Somers * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26c43633c6SAlan Somers * SUCH DAMAGE.
27c43633c6SAlan Somers */
28c43633c6SAlan Somers
29c43633c6SAlan Somers #include <sys/param.h>
30c43633c6SAlan Somers
31c43633c6SAlan Somers #include <atf-c.h>
32c43633c6SAlan Somers
33c43633c6SAlan Somers #include "../utils.h"
34c43633c6SAlan Somers
35c43633c6SAlan Somers /*
36c43633c6SAlan Somers * Test cases.
37c43633c6SAlan Somers */
38c43633c6SAlan Somers
3929280824SCharlie Root ATF_TC_WITHOUT_HEAD(aligned_even_length);
ATF_TC_BODY(aligned_even_length,tc)4029280824SCharlie Root ATF_TC_BODY(aligned_even_length, tc)
41c43633c6SAlan Somers {
42c43633c6SAlan Somers u_char data[] __aligned(sizeof(u_short)) =
43c43633c6SAlan Somers {0x12, 0x34, 0x56, 0x78};
44c43633c6SAlan Somers u_short sum;
45c43633c6SAlan Somers
46c43633c6SAlan Somers sum = in_cksum(data, nitems(data));
4729280824SCharlie Root u_char *c_sum = (u_char *)∑
4829280824SCharlie Root ATF_REQUIRE(c_sum[0] == 0x97 && c_sum[1] == 0x53);
49c43633c6SAlan Somers }
50c43633c6SAlan Somers
5129280824SCharlie Root ATF_TC_WITHOUT_HEAD(aligned_odd_length);
ATF_TC_BODY(aligned_odd_length,tc)5229280824SCharlie Root ATF_TC_BODY(aligned_odd_length, tc)
53c43633c6SAlan Somers {
54c43633c6SAlan Somers u_char data[] __aligned(sizeof(u_short)) =
55c43633c6SAlan Somers {0x12, 0x34, 0x56, 0x78, 0x9a};
56c43633c6SAlan Somers u_short sum;
57c43633c6SAlan Somers
58c43633c6SAlan Somers sum = in_cksum(data, nitems(data));
5929280824SCharlie Root u_char *c_sum = (u_char *)∑
6029280824SCharlie Root ATF_REQUIRE(c_sum[0] == 0xfd && c_sum[1] == 0x52);
61c43633c6SAlan Somers }
62c43633c6SAlan Somers
6329280824SCharlie Root ATF_TC_WITHOUT_HEAD(unaligned_even_length);
ATF_TC_BODY(unaligned_even_length,tc)6429280824SCharlie Root ATF_TC_BODY(unaligned_even_length, tc)
65c43633c6SAlan Somers {
66c43633c6SAlan Somers u_char data[] __aligned(sizeof(u_short)) =
67c43633c6SAlan Somers {0x00, 0x12, 0x34, 0x56, 0x78};
68c43633c6SAlan Somers u_short sum;
69c43633c6SAlan Somers
70c43633c6SAlan Somers sum = in_cksum(data + 1, nitems(data) - 1);
7129280824SCharlie Root u_char *c_sum = (u_char *)∑
7229280824SCharlie Root ATF_REQUIRE(c_sum[0] == 0x97 && c_sum[1] == 0x53);
73c43633c6SAlan Somers }
74c43633c6SAlan Somers
7529280824SCharlie Root ATF_TC_WITHOUT_HEAD(unaligned_odd_length);
ATF_TC_BODY(unaligned_odd_length,tc)7629280824SCharlie Root ATF_TC_BODY(unaligned_odd_length, tc)
77c43633c6SAlan Somers {
78c43633c6SAlan Somers u_char data[] __aligned(sizeof(u_short)) =
79c43633c6SAlan Somers {0x00, 0x12, 0x34, 0x56, 0x78, 0x9a};
80c43633c6SAlan Somers u_short sum;
81c43633c6SAlan Somers
82c43633c6SAlan Somers sum = in_cksum(data + 1, nitems(data) - 1);
8329280824SCharlie Root u_char *c_sum = (u_char *)∑
8429280824SCharlie Root ATF_REQUIRE(c_sum[0] == 0xfd && c_sum[1] == 0x52);
85c43633c6SAlan Somers }
86c43633c6SAlan Somers
87c43633c6SAlan Somers /*
88c43633c6SAlan Somers * Main.
89c43633c6SAlan Somers */
90c43633c6SAlan Somers
ATF_TP_ADD_TCS(tp)91c43633c6SAlan Somers ATF_TP_ADD_TCS(tp)
92c43633c6SAlan Somers {
9329280824SCharlie Root ATF_TP_ADD_TC(tp, aligned_even_length);
9429280824SCharlie Root ATF_TP_ADD_TC(tp, aligned_odd_length);
9529280824SCharlie Root ATF_TP_ADD_TC(tp, unaligned_even_length);
9629280824SCharlie Root ATF_TP_ADD_TC(tp, unaligned_odd_length);
97c43633c6SAlan Somers
98c43633c6SAlan Somers return (atf_no_error());
99c43633c6SAlan Somers }
100