netdev-dpdk: fix mbuf leaks
[cascardo/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 #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 test_ipv4_cidr(void)
28 {
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)));
34
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)));
39 }
40
41 static void
42 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     struct in6_addr exact   = {{{ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, \
49                                   0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff }}};
50
51     struct in6_addr any     = {{{ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, \
52                                   0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }}};
53
54     struct in6_addr neither = {{{ 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef, \
55                                   0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef }}};
56
57     assert(ipv6_mask_is_exact(&exact));
58     assert(!ipv6_mask_is_exact(&any));
59     assert(!ipv6_mask_is_exact(&neither));
60
61     assert(!ipv6_mask_is_any(&exact));
62     assert(ipv6_mask_is_any(&any));
63     assert(!ipv6_mask_is_any(&neither));
64
65 }
66
67 static void
68 test_ipv6_cidr(void)
69 {
70     struct in6_addr dest;
71
72     struct in6_addr src   = {{{ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, \
73                                 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }}};
74
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));
79
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));
84
85     dest = ipv6_create_mask(1);
86     assert(ipv6_count_cidr_bits(&dest) == 1);
87     assert(ipv6_is_cidr(&dest));
88
89     dest = ipv6_create_mask(13);
90     assert(ipv6_count_cidr_bits(&dest) == 13);
91     assert(ipv6_is_cidr(&dest));
92
93     dest = ipv6_create_mask(64);
94     assert(ipv6_count_cidr_bits(&dest) == 64);
95     assert(ipv6_is_cidr(&dest));
96
97     dest = ipv6_create_mask(95);
98     assert(ipv6_count_cidr_bits(&dest) == 95);
99     assert(ipv6_is_cidr(&dest));
100
101     dest = ipv6_create_mask(96);
102     assert(ipv6_count_cidr_bits(&dest) == 96);
103     assert(ipv6_is_cidr(&dest));
104
105     dest = ipv6_create_mask(97);
106     assert(ipv6_count_cidr_bits(&dest) == 97);
107     assert(ipv6_is_cidr(&dest));
108
109     dest = ipv6_create_mask(127);
110     assert(ipv6_count_cidr_bits(&dest) == 127);
111     assert(ipv6_is_cidr(&dest));
112
113     src.s6_addr[8] = 0xf0;
114     assert(ipv6_is_cidr(&src));
115     assert(ipv6_count_cidr_bits(&src) == 68);
116
117     src.s6_addr[15] = 0x01;
118     assert(!ipv6_is_cidr(&src));
119     src.s6_addr[15] = 0x00;
120     assert(ipv6_is_cidr(&src));
121
122     src.s6_addr[8] = 0x0f;
123     assert(!ipv6_is_cidr(&src));
124 }
125
126
127 static void
128 test_ipv6_masking(void)
129 {
130     struct in6_addr dest;
131     struct in6_addr mask;
132
133     mask = ipv6_create_mask(0);
134     dest = ipv6_addr_bitand(&in6addr_exact, &mask);
135     assert(ipv6_count_cidr_bits(&dest) == 0);
136
137     mask = ipv6_create_mask(1);
138     dest = ipv6_addr_bitand(&in6addr_exact, &mask);
139     assert(ipv6_count_cidr_bits(&dest) == 1);
140
141     mask = ipv6_create_mask(13);
142     dest = ipv6_addr_bitand(&in6addr_exact, &mask);
143     assert(ipv6_count_cidr_bits(&dest) == 13);
144
145     mask = ipv6_create_mask(127);
146     dest = ipv6_addr_bitand(&in6addr_exact, &mask);
147     assert(ipv6_count_cidr_bits(&dest) == 127);
148
149     mask = ipv6_create_mask(128);
150     dest = ipv6_addr_bitand(&in6addr_exact, &mask);
151     assert(ipv6_count_cidr_bits(&dest) == 128);
152 }
153
154 static void
155 test_ipv6_parsing(void)
156 {
157     struct in6_addr o_ipv6, p_ipv6;
158     struct in6_addr mask;
159
160     inet_pton(AF_INET6, "2001:db8:0:0:0:0:2:1", &o_ipv6);
161
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);
165
166     ipv6_parse_masked("2001:db8:0:0:0:0:2:1/ffff:ffff:ffff:ffff::",
167                       &p_ipv6, &mask);
168     assert(ipv6_addr_equals(&o_ipv6, &p_ipv6));
169     assert(ipv6_count_cidr_bits(&mask) == 64);
170
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);
174 }
175
176 static void
177 test_packets_main(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
178 {
179     test_ipv4_cidr();
180     test_ipv6_static_masks();
181     test_ipv6_cidr();
182     test_ipv6_masking();
183     test_ipv6_parsing();
184 }
185
186 OVSTEST_REGISTER("test-packets", test_packets_main);