]>
git.proxmox.com Git - mirror_ovs.git/blob - tests/test-packets.c
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.
29 assert(ip_is_cidr(htonl(0x00000000)));
30 assert(ip_is_cidr(htonl(0x80000000)));
31 assert(ip_is_cidr(htonl(0xf0000000)));
32 assert(ip_is_cidr(htonl(0xffffffe0)));
33 assert(ip_is_cidr(htonl(0xffffffff)));
35 assert(!ip_is_cidr(htonl(0x00000001)));
36 assert(!ip_is_cidr(htonl(0x40000000)));
37 assert(!ip_is_cidr(htonl(0x0fffffff)));
38 assert(!ip_is_cidr(htonl(0xffffffd0)));
42 test_ipv6_static_masks(void)
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
48 struct in6_addr exact
= {{{ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, \
49 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff }}};
51 struct in6_addr any
= {{{ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, \
52 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }}};
54 struct in6_addr neither
= {{{ 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef, \
55 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef }}};
57 assert(ipv6_mask_is_exact(&exact
));
58 assert(!ipv6_mask_is_exact(&any
));
59 assert(!ipv6_mask_is_exact(&neither
));
61 assert(!ipv6_mask_is_any(&exact
));
62 assert(ipv6_mask_is_any(&any
));
63 assert(!ipv6_mask_is_any(&neither
));
72 struct in6_addr src
= {{{ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, \
73 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }}};
75 dest
= ipv6_create_mask(0);
76 assert(ipv6_mask_is_any(&dest
));
77 assert(ipv6_count_cidr_bits(&dest
) == 0);
78 assert(ipv6_is_cidr(&dest
));
80 dest
= ipv6_create_mask(128);
81 assert(ipv6_mask_is_exact(&dest
));
82 assert(ipv6_count_cidr_bits(&dest
) == 128);
83 assert(ipv6_is_cidr(&dest
));
85 dest
= ipv6_create_mask(1);
86 assert(ipv6_count_cidr_bits(&dest
) == 1);
87 assert(ipv6_is_cidr(&dest
));
89 dest
= ipv6_create_mask(13);
90 assert(ipv6_count_cidr_bits(&dest
) == 13);
91 assert(ipv6_is_cidr(&dest
));
93 dest
= ipv6_create_mask(64);
94 assert(ipv6_count_cidr_bits(&dest
) == 64);
95 assert(ipv6_is_cidr(&dest
));
97 dest
= ipv6_create_mask(95);
98 assert(ipv6_count_cidr_bits(&dest
) == 95);
99 assert(ipv6_is_cidr(&dest
));
101 dest
= ipv6_create_mask(96);
102 assert(ipv6_count_cidr_bits(&dest
) == 96);
103 assert(ipv6_is_cidr(&dest
));
105 dest
= ipv6_create_mask(97);
106 assert(ipv6_count_cidr_bits(&dest
) == 97);
107 assert(ipv6_is_cidr(&dest
));
109 dest
= ipv6_create_mask(127);
110 assert(ipv6_count_cidr_bits(&dest
) == 127);
111 assert(ipv6_is_cidr(&dest
));
113 src
.s6_addr
[8] = 0xf0;
114 assert(ipv6_is_cidr(&src
));
115 assert(ipv6_count_cidr_bits(&src
) == 68);
117 src
.s6_addr
[15] = 0x01;
118 assert(!ipv6_is_cidr(&src
));
119 src
.s6_addr
[15] = 0x00;
120 assert(ipv6_is_cidr(&src
));
122 src
.s6_addr
[8] = 0x0f;
123 assert(!ipv6_is_cidr(&src
));
128 test_ipv6_masking(void)
130 struct in6_addr dest
;
131 struct in6_addr mask
;
133 mask
= ipv6_create_mask(0);
134 dest
= ipv6_addr_bitand(&in6addr_exact
, &mask
);
135 assert(ipv6_count_cidr_bits(&dest
) == 0);
137 mask
= ipv6_create_mask(1);
138 dest
= ipv6_addr_bitand(&in6addr_exact
, &mask
);
139 assert(ipv6_count_cidr_bits(&dest
) == 1);
141 mask
= ipv6_create_mask(13);
142 dest
= ipv6_addr_bitand(&in6addr_exact
, &mask
);
143 assert(ipv6_count_cidr_bits(&dest
) == 13);
145 mask
= ipv6_create_mask(127);
146 dest
= ipv6_addr_bitand(&in6addr_exact
, &mask
);
147 assert(ipv6_count_cidr_bits(&dest
) == 127);
149 mask
= ipv6_create_mask(128);
150 dest
= ipv6_addr_bitand(&in6addr_exact
, &mask
);
151 assert(ipv6_count_cidr_bits(&dest
) == 128);
155 test_ipv6_parsing(void)
157 struct in6_addr o_ipv6
, p_ipv6
;
158 struct in6_addr mask
;
160 inet_pton(AF_INET6
, "2001:db8:0:0:0:0:2:1", &o_ipv6
);
162 ipv6_parse_masked("2001:db8:0:0:0:0:2:1/64", &p_ipv6
, &mask
);
163 assert(ipv6_addr_equals(&o_ipv6
, &p_ipv6
));
164 assert(ipv6_count_cidr_bits(&mask
) == 64);
166 ipv6_parse_masked("2001:db8:0:0:0:0:2:1/ffff:ffff:ffff:ffff::",
168 assert(ipv6_addr_equals(&o_ipv6
, &p_ipv6
));
169 assert(ipv6_count_cidr_bits(&mask
) == 64);
171 ipv6_parse_masked("2001:db8:0:0:0:0:2:1", &p_ipv6
, &mask
);
172 assert(ipv6_addr_equals(&o_ipv6
, &p_ipv6
));
173 assert(ipv6_count_cidr_bits(&mask
) == 128);
177 test_packets_main(int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
180 test_ipv6_static_masks();
186 OVSTEST_REGISTER("test-packets", test_packets_main
);