Branch data Line data Source code
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 : : #undef NDEBUG
19 : : #include "packets.h"
20 : : #include <assert.h>
21 : : #include <stdio.h>
22 : : #include <stdlib.h>
23 : : #include <string.h>
24 : : #include "ovstest.h"
25 : :
26 : : static void
27 : 1 : test_ipv4_cidr(void)
28 : : {
29 [ - + ]: 1 : assert(ip_is_cidr(htonl(0x00000000)));
30 [ - + ]: 1 : assert(ip_is_cidr(htonl(0x80000000)));
31 [ - + ]: 1 : assert(ip_is_cidr(htonl(0xf0000000)));
32 [ - + ]: 1 : assert(ip_is_cidr(htonl(0xffffffe0)));
33 [ - + ]: 1 : assert(ip_is_cidr(htonl(0xffffffff)));
34 : :
35 [ - + ]: 1 : assert(!ip_is_cidr(htonl(0x00000001)));
36 [ - + ]: 1 : assert(!ip_is_cidr(htonl(0x40000000)));
37 [ - + ]: 1 : assert(!ip_is_cidr(htonl(0x0fffffff)));
38 [ - + ]: 1 : assert(!ip_is_cidr(htonl(0xffffffd0)));
39 : 1 : }
40 : :
41 : : static void
42 : 1 : test_ipv6_static_masks(void)
43 : : {
44 : : /* The 'exact' and 'any' addresses should be identical to
45 : : * 'in6addr_exact' and 'in6addr_any' definitions, but we redefine
46 : : * them here since the pre-defined ones are used in the functions
47 : : * we're testing. */
48 : 1 : struct in6_addr exact = {{{ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, \
49 : : 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff }}};
50 : :
51 : 1 : struct in6_addr any = {{{ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, \
52 : : 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }}};
53 : :
54 : 1 : struct in6_addr neither = {{{ 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef, \
55 : : 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef }}};
56 : :
57 [ - + ]: 1 : assert(ipv6_mask_is_exact(&exact));
58 [ - + ]: 1 : assert(!ipv6_mask_is_exact(&any));
59 [ - + ]: 1 : assert(!ipv6_mask_is_exact(&neither));
60 : :
61 [ - + ]: 1 : assert(!ipv6_mask_is_any(&exact));
62 [ - + ]: 1 : assert(ipv6_mask_is_any(&any));
63 [ - + ]: 1 : assert(!ipv6_mask_is_any(&neither));
64 : :
65 : 1 : }
66 : :
67 : : static void
68 : 1 : test_ipv6_cidr(void)
69 : : {
70 : : struct in6_addr dest;
71 : :
72 : 1 : struct in6_addr src = {{{ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, \
73 : : 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }}};
74 : :
75 : 1 : dest = ipv6_create_mask(0);
76 [ - + ]: 1 : assert(ipv6_mask_is_any(&dest));
77 [ - + ]: 1 : assert(ipv6_count_cidr_bits(&dest) == 0);
78 [ - + ]: 1 : assert(ipv6_is_cidr(&dest));
79 : :
80 : 1 : dest = ipv6_create_mask(128);
81 [ - + ]: 1 : assert(ipv6_mask_is_exact(&dest));
82 [ - + ]: 1 : assert(ipv6_count_cidr_bits(&dest) == 128);
83 [ - + ]: 1 : assert(ipv6_is_cidr(&dest));
84 : :
85 : 1 : dest = ipv6_create_mask(1);
86 [ - + ]: 1 : assert(ipv6_count_cidr_bits(&dest) == 1);
87 [ - + ]: 1 : assert(ipv6_is_cidr(&dest));
88 : :
89 : 1 : dest = ipv6_create_mask(13);
90 [ - + ]: 1 : assert(ipv6_count_cidr_bits(&dest) == 13);
91 [ - + ]: 1 : assert(ipv6_is_cidr(&dest));
92 : :
93 : 1 : dest = ipv6_create_mask(64);
94 [ - + ]: 1 : assert(ipv6_count_cidr_bits(&dest) == 64);
95 [ - + ]: 1 : assert(ipv6_is_cidr(&dest));
96 : :
97 : 1 : dest = ipv6_create_mask(95);
98 [ - + ]: 1 : assert(ipv6_count_cidr_bits(&dest) == 95);
99 [ - + ]: 1 : assert(ipv6_is_cidr(&dest));
100 : :
101 : 1 : dest = ipv6_create_mask(96);
102 [ - + ]: 1 : assert(ipv6_count_cidr_bits(&dest) == 96);
103 [ - + ]: 1 : assert(ipv6_is_cidr(&dest));
104 : :
105 : 1 : dest = ipv6_create_mask(97);
106 [ - + ]: 1 : assert(ipv6_count_cidr_bits(&dest) == 97);
107 [ - + ]: 1 : assert(ipv6_is_cidr(&dest));
108 : :
109 : 1 : dest = ipv6_create_mask(127);
110 [ - + ]: 1 : assert(ipv6_count_cidr_bits(&dest) == 127);
111 [ - + ]: 1 : assert(ipv6_is_cidr(&dest));
112 : :
113 : 1 : src.s6_addr[8] = 0xf0;
114 [ - + ]: 1 : assert(ipv6_is_cidr(&src));
115 [ - + ]: 1 : assert(ipv6_count_cidr_bits(&src) == 68);
116 : :
117 : 1 : src.s6_addr[15] = 0x01;
118 [ - + ]: 1 : assert(!ipv6_is_cidr(&src));
119 : 1 : src.s6_addr[15] = 0x00;
120 [ - + ]: 1 : assert(ipv6_is_cidr(&src));
121 : :
122 : 1 : src.s6_addr[8] = 0x0f;
123 [ - + ]: 1 : assert(!ipv6_is_cidr(&src));
124 : 1 : }
125 : :
126 : :
127 : : static void
128 : 1 : test_ipv6_masking(void)
129 : : {
130 : : struct in6_addr dest;
131 : : struct in6_addr mask;
132 : :
133 : 1 : mask = ipv6_create_mask(0);
134 : 1 : dest = ipv6_addr_bitand(&in6addr_exact, &mask);
135 [ - + ]: 1 : assert(ipv6_count_cidr_bits(&dest) == 0);
136 : :
137 : 1 : mask = ipv6_create_mask(1);
138 : 1 : dest = ipv6_addr_bitand(&in6addr_exact, &mask);
139 [ - + ]: 1 : assert(ipv6_count_cidr_bits(&dest) == 1);
140 : :
141 : 1 : mask = ipv6_create_mask(13);
142 : 1 : dest = ipv6_addr_bitand(&in6addr_exact, &mask);
143 [ - + ]: 1 : assert(ipv6_count_cidr_bits(&dest) == 13);
144 : :
145 : 1 : mask = ipv6_create_mask(127);
146 : 1 : dest = ipv6_addr_bitand(&in6addr_exact, &mask);
147 [ - + ]: 1 : assert(ipv6_count_cidr_bits(&dest) == 127);
148 : :
149 : 1 : mask = ipv6_create_mask(128);
150 : 1 : dest = ipv6_addr_bitand(&in6addr_exact, &mask);
151 [ - + ]: 1 : assert(ipv6_count_cidr_bits(&dest) == 128);
152 : 1 : }
153 : :
154 : : static void
155 : 1 : test_ipv6_parsing(void)
156 : : {
157 : : struct in6_addr o_ipv6, p_ipv6;
158 : : struct in6_addr mask;
159 : :
160 : 1 : inet_pton(AF_INET6, "2001:db8:0:0:0:0:2:1", &o_ipv6);
161 : :
162 : 1 : ipv6_parse_masked("2001:db8:0:0:0:0:2:1/64", &p_ipv6, &mask);
163 [ - + ]: 1 : assert(ipv6_addr_equals(&o_ipv6, &p_ipv6));
164 [ - + ]: 1 : assert(ipv6_count_cidr_bits(&mask) == 64);
165 : :
166 : 1 : ipv6_parse_masked("2001:db8:0:0:0:0:2:1/ffff:ffff:ffff:ffff::",
167 : : &p_ipv6, &mask);
168 [ - + ]: 1 : assert(ipv6_addr_equals(&o_ipv6, &p_ipv6));
169 [ - + ]: 1 : assert(ipv6_count_cidr_bits(&mask) == 64);
170 : :
171 : 1 : ipv6_parse_masked("2001:db8:0:0:0:0:2:1", &p_ipv6, &mask);
172 [ - + ]: 1 : assert(ipv6_addr_equals(&o_ipv6, &p_ipv6));
173 [ - + ]: 1 : assert(ipv6_count_cidr_bits(&mask) == 128);
174 : 1 : }
175 : :
176 : : static void
177 : 1 : test_packets_main(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
178 : : {
179 : 1 : test_ipv4_cidr();
180 : 1 : test_ipv6_static_masks();
181 : 1 : test_ipv6_cidr();
182 : 1 : test_ipv6_masking();
183 : 1 : test_ipv6_parsing();
184 : 1 : }
185 : :
186 : 1176 : OVSTEST_REGISTER("test-packets", test_packets_main);
|