2 * Copyright (c) 2011, 2014 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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)));
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)));
44 test_ipv6_static_masks(void)
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
50 struct in6_addr exact = {{{ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, \
51 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff }}};
53 struct in6_addr any = {{{ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, \
54 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }}};
56 struct in6_addr neither = {{{ 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef, \
57 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef }}};
59 assert(ipv6_mask_is_exact(&exact));
60 assert(!ipv6_mask_is_exact(&any));
61 assert(!ipv6_mask_is_exact(&neither));
63 assert(!ipv6_mask_is_any(&exact));
64 assert(ipv6_mask_is_any(&any));
65 assert(!ipv6_mask_is_any(&neither));
74 struct in6_addr src = {{{ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, \
75 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }}};
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));
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));
87 dest = ipv6_create_mask(1);
88 assert(ipv6_count_cidr_bits(&dest) == 1);
89 assert(ipv6_is_cidr(&dest));
91 dest = ipv6_create_mask(13);
92 assert(ipv6_count_cidr_bits(&dest) == 13);
93 assert(ipv6_is_cidr(&dest));
95 dest = ipv6_create_mask(64);
96 assert(ipv6_count_cidr_bits(&dest) == 64);
97 assert(ipv6_is_cidr(&dest));
99 dest = ipv6_create_mask(95);
100 assert(ipv6_count_cidr_bits(&dest) == 95);
101 assert(ipv6_is_cidr(&dest));
103 dest = ipv6_create_mask(96);
104 assert(ipv6_count_cidr_bits(&dest) == 96);
105 assert(ipv6_is_cidr(&dest));
107 dest = ipv6_create_mask(97);
108 assert(ipv6_count_cidr_bits(&dest) == 97);
109 assert(ipv6_is_cidr(&dest));
111 dest = ipv6_create_mask(127);
112 assert(ipv6_count_cidr_bits(&dest) == 127);
113 assert(ipv6_is_cidr(&dest));
115 src.s6_addr[8] = 0xf0;
116 assert(ipv6_is_cidr(&src));
117 assert(ipv6_count_cidr_bits(&src) == 68);
119 src.s6_addr[15] = 0x01;
120 assert(!ipv6_is_cidr(&src));
121 src.s6_addr[15] = 0x00;
122 assert(ipv6_is_cidr(&src));
124 src.s6_addr[8] = 0x0f;
125 assert(!ipv6_is_cidr(&src));
130 test_ipv6_masking(void)
132 struct in6_addr dest;
133 struct in6_addr mask;
135 mask = ipv6_create_mask(0);
136 dest = ipv6_addr_bitand(&in6addr_exact, &mask);
137 assert(ipv6_count_cidr_bits(&dest) == 0);
139 mask = ipv6_create_mask(1);
140 dest = ipv6_addr_bitand(&in6addr_exact, &mask);
141 assert(ipv6_count_cidr_bits(&dest) == 1);
143 mask = ipv6_create_mask(13);
144 dest = ipv6_addr_bitand(&in6addr_exact, &mask);
145 assert(ipv6_count_cidr_bits(&dest) == 13);
147 mask = ipv6_create_mask(127);
148 dest = ipv6_addr_bitand(&in6addr_exact, &mask);
149 assert(ipv6_count_cidr_bits(&dest) == 127);
151 mask = ipv6_create_mask(128);
152 dest = ipv6_addr_bitand(&in6addr_exact, &mask);
153 assert(ipv6_count_cidr_bits(&dest) == 128);
157 test_packets_main(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
160 test_ipv6_static_masks();
165 OVSTEST_REGISTER("test-packets", test_packets_main);