aboutsummaryrefslogtreecommitdiffstats
path: root/test/packetdrill/checksum_test.c
blob: 08ef2e13589e183587019e5b7af1dc9e176d670a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
 * Copyright 2013 Google Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */
/*
 * Author: ncardwell@google.com (Neal Cardwell)
 *
 * Unit test for checksum.c.
 */

#include "checksum.h"

#include <arpa/inet.h>
#include "assert.h"
#include "ip.h"
#include "ipv6.h"
#include "sctp.h"
#include "tcp.h"

static void test_tcp_udp_v4_checksum(void)
{
	u8 data[] = {
		0x45, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x00, 0x00,
		0xff, 0x06, 0xf9, 0x10, 0x01, 0x01, 0x01, 0x01,
		0xc0, 0xa8, 0x00, 0x01, 0x04, 0xd2, 0xeb, 0x35,
		0x00, 0x00, 0x00, 0x00, 0xc6, 0xf0, 0x56, 0x00,
		0xa0, 0x12, 0x16, 0xa0, 0x54, 0x12, 0x00, 0x00,
		0x02, 0x04, 0x05, 0xb4, 0x04, 0x02, 0x08, 0x0a,
		0x00, 0x00, 0x02, 0xbc, 0x00, 0x06, 0x0a, 0xd8,
		0x01, 0x03, 0x03, 0x07,
	};

	struct in_addr src_ip, dst_ip;
	struct tcp *tcp = (struct tcp *) (data + sizeof(struct ipv4));
	int len = sizeof(data) - sizeof(struct ipv4);
	u16 checksum = 0;

	assert(inet_pton(AF_INET, "1.1.1.1", &src_ip) == 1);
	assert(inet_pton(AF_INET, "192.168.0.1", &dst_ip) == 1);

	checksum =
	    ntohs(tcp_udp_v4_checksum(src_ip, dst_ip, IPPROTO_TCP, tcp, len));
	assert(checksum == 0);

	tcp->check = 0;
	checksum =
	    ntohs(tcp_udp_v4_checksum(src_ip, dst_ip, IPPROTO_TCP, tcp, len));
	assert(checksum == 0x5412);
}

static void test_tcp_udp_v6_checksum(void)
{
	u8 data[] = {
		0x60, 0x00, 0x00, 0x00, 0x00, 0x20, 0x06, 0xff,
		0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
		0xfd, 0x3d, 0xfa, 0x7b, 0xd1, 0x7d, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
		0xd3, 0xe2, 0x1f, 0x90, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x80, 0x02, 0x80, 0x18,
		0x06, 0x60, 0x00, 0x00, 0x02, 0x04, 0x03, 0xe8,
		0x04, 0x02, 0x01, 0x01, 0x01, 0x03, 0x03, 0x07,
	};

	struct ipv6 *ipv6 = (struct ipv6 *) (data);
	struct tcp *tcp = (struct tcp *) (data + sizeof(struct ipv6));
	int len = sizeof(data) - sizeof(struct ipv6);
	u16 checksum = 0;

	checksum =
	    ntohs(tcp_udp_v6_checksum(&ipv6->src_ip,
				      &ipv6->dst_ip,
				      IPPROTO_TCP, tcp, len));
	assert(checksum == 0);

	tcp->check = 0;
	checksum =
	    ntohs(tcp_udp_v6_checksum(&ipv6->src_ip,
				      &ipv6->dst_ip,
				      IPPROTO_TCP, tcp, len));
	assert(checksum == 0x0660);
}

static void test_ipv4_checksum(void)
{
	u8 data[] = {
		0x45, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x00, 0x00,
		0xff, 0x06, 0xf9, 0x10, 0x01, 0x01, 0x01, 0x01,
		0xc0, 0xa8, 0x00, 0x01,
	};
	struct ipv4 *ip = (struct ipv4 *) data;
	u16 checksum = 0;

	checksum = ntohs(ipv4_checksum(data, sizeof(data)));
	assert(checksum == 0);

	ip->check = 0;
	checksum = ntohs(ipv4_checksum(data, sizeof(data)));
	assert(checksum == 0xf910);
}

static void test_sctp_crc32c(void)
{
	u8 data[] = {
		0x07, 0xd0, 0xd6, 0x61, 0x11, 0x0c, 0xc5, 0x6c,
		0xda, 0xd7, 0x37, 0x74, 0x06, 0x00, 0x00, 0x0f,
		0x00, 0x0c, 0x00, 0x0b, 0x47, 0x6f, 0x6f, 0x64,
		0x62, 0x79, 0x65, 0x00,
	};
	struct sctp_common_header *sctp_common_header;
	u32 crc32c;

	sctp_common_header = (struct sctp_common_header *)data;
	sctp_common_header->crc32c = 0;
	crc32c = ntohl(sctp_crc32c(data, sizeof(data)));
	assert(crc32c == 0xdad73774);
}

int main(void)
{
	test_tcp_udp_v4_checksum();
	test_tcp_udp_v6_checksum();
	test_ipv4_checksum();
	test_sctp_crc32c();
	return 0;
}