]> git.proxmox.com Git - mirror_ovs.git/blob - tests/test-packets.c
unit-test: Link 29 test programs into ovstest
[mirror_ovs.git] / tests / test-packets.c
1 /*
2 * Copyright (c) 2011, 2014 Nicira, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <config.h>
18 #include "packets.h"
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include "ovstest.h"
23
24 #undef NDEBUG
25 #include <assert.h>
26
27
28 static void
29 test_ipv4_cidr(void)
30 {
31 assert(ip_is_cidr(htonl(0x00000000)));
32 assert(ip_is_cidr(htonl(0x80000000)));
33 assert(ip_is_cidr(htonl(0xf0000000)));
34 assert(ip_is_cidr(htonl(0xffffffe0)));
35 assert(ip_is_cidr(htonl(0xffffffff)));
36
37 assert(!ip_is_cidr(htonl(0x00000001)));
38 assert(!ip_is_cidr(htonl(0x40000000)));
39 assert(!ip_is_cidr(htonl(0x0fffffff)));
40 assert(!ip_is_cidr(htonl(0xffffffd0)));
41 }
42
43 static void
44 test_ipv6_static_masks(void)
45 {
46 /* The 'exact' and 'any' addresses should be identical to
47 * 'in6addr_exact' and 'in6addr_any' definitions, but we redefine
48 * them here since the pre-defined ones are used in the functions
49 * we're testing. */
50 struct in6_addr exact = {{{ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, \
51 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff }}};
52
53 struct in6_addr any = {{{ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, \
54 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }}};
55
56 struct in6_addr neither = {{{ 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef, \
57 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef }}};
58
59 assert(ipv6_mask_is_exact(&exact));
60 assert(!ipv6_mask_is_exact(&any));
61 assert(!ipv6_mask_is_exact(&neither));
62
63 assert(!ipv6_mask_is_any(&exact));
64 assert(ipv6_mask_is_any(&any));
65 assert(!ipv6_mask_is_any(&neither));
66
67 }
68
69 static void
70 test_ipv6_cidr(void)
71 {
72 struct in6_addr dest;
73
74 struct in6_addr src = {{{ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, \
75 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }}};
76
77 dest = ipv6_create_mask(0);
78 assert(ipv6_mask_is_any(&dest));
79 assert(ipv6_count_cidr_bits(&dest) == 0);
80 assert(ipv6_is_cidr(&dest));
81
82 dest = ipv6_create_mask(128);
83 assert(ipv6_mask_is_exact(&dest));
84 assert(ipv6_count_cidr_bits(&dest) == 128);
85 assert(ipv6_is_cidr(&dest));
86
87 dest = ipv6_create_mask(1);
88 assert(ipv6_count_cidr_bits(&dest) == 1);
89 assert(ipv6_is_cidr(&dest));
90
91 dest = ipv6_create_mask(13);
92 assert(ipv6_count_cidr_bits(&dest) == 13);
93 assert(ipv6_is_cidr(&dest));
94
95 dest = ipv6_create_mask(64);
96 assert(ipv6_count_cidr_bits(&dest) == 64);
97 assert(ipv6_is_cidr(&dest));
98
99 dest = ipv6_create_mask(95);
100 assert(ipv6_count_cidr_bits(&dest) == 95);
101 assert(ipv6_is_cidr(&dest));
102
103 dest = ipv6_create_mask(96);
104 assert(ipv6_count_cidr_bits(&dest) == 96);
105 assert(ipv6_is_cidr(&dest));
106
107 dest = ipv6_create_mask(97);
108 assert(ipv6_count_cidr_bits(&dest) == 97);
109 assert(ipv6_is_cidr(&dest));
110
111 dest = ipv6_create_mask(127);
112 assert(ipv6_count_cidr_bits(&dest) == 127);
113 assert(ipv6_is_cidr(&dest));
114
115 src.s6_addr[8] = 0xf0;
116 assert(ipv6_is_cidr(&src));
117 assert(ipv6_count_cidr_bits(&src) == 68);
118
119 src.s6_addr[15] = 0x01;
120 assert(!ipv6_is_cidr(&src));
121 src.s6_addr[15] = 0x00;
122 assert(ipv6_is_cidr(&src));
123
124 src.s6_addr[8] = 0x0f;
125 assert(!ipv6_is_cidr(&src));
126 }
127
128
129 static void
130 test_ipv6_masking(void)
131 {
132 struct in6_addr dest;
133 struct in6_addr mask;
134
135 mask = ipv6_create_mask(0);
136 dest = ipv6_addr_bitand(&in6addr_exact, &mask);
137 assert(ipv6_count_cidr_bits(&dest) == 0);
138
139 mask = ipv6_create_mask(1);
140 dest = ipv6_addr_bitand(&in6addr_exact, &mask);
141 assert(ipv6_count_cidr_bits(&dest) == 1);
142
143 mask = ipv6_create_mask(13);
144 dest = ipv6_addr_bitand(&in6addr_exact, &mask);
145 assert(ipv6_count_cidr_bits(&dest) == 13);
146
147 mask = ipv6_create_mask(127);
148 dest = ipv6_addr_bitand(&in6addr_exact, &mask);
149 assert(ipv6_count_cidr_bits(&dest) == 127);
150
151 mask = ipv6_create_mask(128);
152 dest = ipv6_addr_bitand(&in6addr_exact, &mask);
153 assert(ipv6_count_cidr_bits(&dest) == 128);
154 }
155
156 static void
157 test_packets_main(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
158 {
159 test_ipv4_cidr();
160 test_ipv6_static_masks();
161 test_ipv6_cidr();
162 test_ipv6_masking();
163 }
164
165 OVSTEST_REGISTER("test-packets", test_packets_main);