]>
Commit | Line | Data |
---|---|---|
f696f12f | 1 | /* |
e0edde6f | 2 | * Copyright (c) 2011 Nicira, Inc. |
f696f12f JP |
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 | ||
23 | #undef NDEBUG | |
24 | #include <assert.h> | |
25 | ||
26 | ||
27 | static void | |
28 | test_ipv4_cidr(void) | |
29 | { | |
30 | assert(ip_is_cidr(htonl(0x00000000))); | |
31 | assert(ip_is_cidr(htonl(0x80000000))); | |
32 | assert(ip_is_cidr(htonl(0xf0000000))); | |
33 | assert(ip_is_cidr(htonl(0xffffffe0))); | |
34 | assert(ip_is_cidr(htonl(0xffffffff))); | |
35 | ||
36 | assert(!ip_is_cidr(htonl(0x00000001))); | |
37 | assert(!ip_is_cidr(htonl(0x40000000))); | |
38 | assert(!ip_is_cidr(htonl(0x0fffffff))); | |
39 | assert(!ip_is_cidr(htonl(0xffffffd0))); | |
40 | } | |
41 | ||
d31f1109 JP |
42 | static void |
43 | test_ipv6_static_masks(void) | |
44 | { | |
45 | /* The 'exact' and 'any' addresses should be identical to | |
46 | * 'in6addr_exact' and 'in6addr_any' definitions, but we redefine | |
47 | * them here since the pre-defined ones are used in the functions | |
48 | * we're testing. */ | |
49 | struct in6_addr exact = {{{ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, \ | |
50 | 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff }}}; | |
51 | ||
52 | struct in6_addr any = {{{ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, \ | |
53 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }}}; | |
54 | ||
55 | struct in6_addr neither = {{{ 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef, \ | |
56 | 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef }}}; | |
57 | ||
58 | assert(ipv6_mask_is_exact(&exact)); | |
59 | assert(!ipv6_mask_is_exact(&any)); | |
60 | assert(!ipv6_mask_is_exact(&neither)); | |
61 | ||
62 | assert(!ipv6_mask_is_any(&exact)); | |
63 | assert(ipv6_mask_is_any(&any)); | |
64 | assert(!ipv6_mask_is_any(&neither)); | |
65 | ||
66 | } | |
67 | ||
68 | static void | |
69 | test_ipv6_cidr(void) | |
70 | { | |
71 | struct in6_addr dest; | |
72 | ||
73 | struct in6_addr src = {{{ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, \ | |
74 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }}}; | |
75 | ||
76 | dest = ipv6_create_mask(0); | |
77 | assert(ipv6_mask_is_any(&dest)); | |
78 | assert(ipv6_count_cidr_bits(&dest) == 0); | |
79 | assert(ipv6_is_cidr(&dest)); | |
80 | ||
81 | dest = ipv6_create_mask(128); | |
82 | assert(ipv6_mask_is_exact(&dest)); | |
83 | assert(ipv6_count_cidr_bits(&dest) == 128); | |
84 | assert(ipv6_is_cidr(&dest)); | |
85 | ||
86 | dest = ipv6_create_mask(1); | |
87 | assert(ipv6_count_cidr_bits(&dest) == 1); | |
88 | assert(ipv6_is_cidr(&dest)); | |
89 | ||
90 | dest = ipv6_create_mask(13); | |
91 | assert(ipv6_count_cidr_bits(&dest) == 13); | |
92 | assert(ipv6_is_cidr(&dest)); | |
93 | ||
94 | dest = ipv6_create_mask(64); | |
95 | assert(ipv6_count_cidr_bits(&dest) == 64); | |
96 | assert(ipv6_is_cidr(&dest)); | |
97 | ||
98 | dest = ipv6_create_mask(95); | |
99 | assert(ipv6_count_cidr_bits(&dest) == 95); | |
100 | assert(ipv6_is_cidr(&dest)); | |
101 | ||
102 | dest = ipv6_create_mask(96); | |
103 | assert(ipv6_count_cidr_bits(&dest) == 96); | |
104 | assert(ipv6_is_cidr(&dest)); | |
105 | ||
106 | dest = ipv6_create_mask(97); | |
107 | assert(ipv6_count_cidr_bits(&dest) == 97); | |
108 | assert(ipv6_is_cidr(&dest)); | |
109 | ||
110 | dest = ipv6_create_mask(127); | |
111 | assert(ipv6_count_cidr_bits(&dest) == 127); | |
112 | assert(ipv6_is_cidr(&dest)); | |
113 | ||
114 | src.s6_addr[8] = 0xf0; | |
115 | assert(ipv6_is_cidr(&src)); | |
116 | assert(ipv6_count_cidr_bits(&src) == 68); | |
117 | ||
118 | src.s6_addr[15] = 0x01; | |
119 | assert(!ipv6_is_cidr(&src)); | |
120 | src.s6_addr[15] = 0x00; | |
121 | assert(ipv6_is_cidr(&src)); | |
122 | ||
123 | src.s6_addr[8] = 0x0f; | |
124 | assert(!ipv6_is_cidr(&src)); | |
125 | } | |
126 | ||
127 | ||
128 | static void | |
129 | test_ipv6_masking(void) | |
130 | { | |
131 | struct in6_addr dest; | |
132 | struct in6_addr mask; | |
133 | ||
134 | mask = ipv6_create_mask(0); | |
135 | dest = ipv6_addr_bitand(&in6addr_exact, &mask); | |
136 | assert(ipv6_count_cidr_bits(&dest) == 0); | |
137 | ||
138 | mask = ipv6_create_mask(1); | |
139 | dest = ipv6_addr_bitand(&in6addr_exact, &mask); | |
140 | assert(ipv6_count_cidr_bits(&dest) == 1); | |
141 | ||
142 | mask = ipv6_create_mask(13); | |
143 | dest = ipv6_addr_bitand(&in6addr_exact, &mask); | |
144 | assert(ipv6_count_cidr_bits(&dest) == 13); | |
145 | ||
146 | mask = ipv6_create_mask(127); | |
147 | dest = ipv6_addr_bitand(&in6addr_exact, &mask); | |
148 | assert(ipv6_count_cidr_bits(&dest) == 127); | |
149 | ||
150 | mask = ipv6_create_mask(128); | |
151 | dest = ipv6_addr_bitand(&in6addr_exact, &mask); | |
152 | assert(ipv6_count_cidr_bits(&dest) == 128); | |
153 | } | |
154 | ||
f696f12f JP |
155 | int |
156 | main(void) | |
157 | { | |
158 | test_ipv4_cidr(); | |
d31f1109 JP |
159 | test_ipv6_static_masks(); |
160 | test_ipv6_cidr(); | |
161 | test_ipv6_masking(); | |
f696f12f JP |
162 | |
163 | return 0; | |
164 | } |