npf_table_test.c revision 1.2.2.6 1 1.2.2.6 riz /* $NetBSD: npf_table_test.c,v 1.2.2.6 2012/11/18 21:48:56 riz Exp $ */
2 1.2.2.2 riz
3 1.2.2.2 riz /*
4 1.2.2.2 riz * NPF tableset test.
5 1.2.2.2 riz *
6 1.2.2.2 riz * Public Domain.
7 1.2.2.2 riz */
8 1.2.2.2 riz
9 1.2.2.2 riz #include <sys/types.h>
10 1.2.2.2 riz
11 1.2.2.2 riz #include "npf_impl.h"
12 1.2.2.2 riz #include "npf_test.h"
13 1.2.2.2 riz
14 1.2.2.2 riz static const char *ip_list[] = {
15 1.2.2.2 riz "192.168.1.1",
16 1.2.2.2 riz "10.0.0.1",
17 1.2.2.2 riz "192.168.2.1",
18 1.2.2.2 riz "10.1.0.1",
19 1.2.2.2 riz "192.168.100.253",
20 1.2.2.2 riz "10.0.5.1",
21 1.2.2.2 riz "192.168.128.127",
22 1.2.2.2 riz "10.0.0.2",
23 1.2.2.2 riz };
24 1.2.2.2 riz
25 1.2.2.6 riz static const uint16_t ip6_list[][8] = {
26 1.2.2.6 riz {
27 1.2.2.6 riz htons(0xfe80), 0x0, 0x0, 0x0,
28 1.2.2.6 riz htons(0x2a0), htons(0xc0ff), htons(0xfe10), htons(0x1234)
29 1.2.2.6 riz },
30 1.2.2.6 riz {
31 1.2.2.6 riz htons(0xfe80), 0x0, 0x0, 0x0,
32 1.2.2.6 riz htons(0x2a0), htons(0xc0ff), 0x00, 0x0
33 1.2.2.6 riz },
34 1.2.2.6 riz {
35 1.2.2.6 riz htons(0xfe80), 0x0, 0x0, 0x0,
36 1.2.2.6 riz 0x0, 0x0, 0x0, 0x0
37 1.2.2.6 riz },
38 1.2.2.6 riz {
39 1.2.2.6 riz htons(0xfe80), 0x0, 0x0, 0x0,
40 1.2.2.6 riz htons(0x2a0), htons(0xc0ff), htons(0xfe10), htons(0x1230)
41 1.2.2.6 riz }
42 1.2.2.5 riz };
43 1.2.2.5 riz
44 1.2.2.2 riz #define HASH_TID 1
45 1.2.2.2 riz #define TREE_TID 2
46 1.2.2.2 riz
47 1.2.2.2 riz bool
48 1.2.2.2 riz npf_table_test(bool verbose)
49 1.2.2.2 riz {
50 1.2.2.2 riz npf_addr_t addr_storage, *addr = &addr_storage;
51 1.2.2.5 riz const int nm = NPF_NO_NETMASK;
52 1.2.2.2 riz npf_tableset_t *tblset;
53 1.2.2.2 riz npf_table_t *t1, *t2;
54 1.2.2.5 riz int error, alen;
55 1.2.2.6 riz bool fail = false;
56 1.2.2.2 riz u_int i;
57 1.2.2.2 riz
58 1.2.2.2 riz npf_tableset_sysinit();
59 1.2.2.2 riz
60 1.2.2.2 riz tblset = npf_tableset_create();
61 1.2.2.6 riz fail |= !(tblset != NULL);
62 1.2.2.2 riz
63 1.2.2.2 riz /* Table ID 1, using hash table with 256 lists. */
64 1.2.2.2 riz t1 = npf_table_create(HASH_TID, NPF_TABLE_HASH, 256);
65 1.2.2.6 riz fail |= !(t1 != NULL);
66 1.2.2.2 riz error = npf_tableset_insert(tblset, t1);
67 1.2.2.6 riz fail |= !(error == 0);
68 1.2.2.2 riz
69 1.2.2.2 riz /* Check for double-insert. */
70 1.2.2.2 riz error = npf_tableset_insert(tblset, t1);
71 1.2.2.6 riz fail |= !(error != 0);
72 1.2.2.2 riz
73 1.2.2.2 riz /* Table ID 2, using RB-tree. */
74 1.2.2.2 riz t2 = npf_table_create(TREE_TID, NPF_TABLE_TREE, 0);
75 1.2.2.6 riz fail |= !(t2 != NULL);
76 1.2.2.2 riz error = npf_tableset_insert(tblset, t2);
77 1.2.2.6 riz fail |= !(error == 0);
78 1.2.2.2 riz
79 1.2.2.3 riz /* Attempt to match non-existing entries - should fail. */
80 1.2.2.3 riz addr->s6_addr32[0] = inet_addr(ip_list[0]);
81 1.2.2.5 riz alen = sizeof(struct in_addr);
82 1.2.2.3 riz
83 1.2.2.5 riz error = npf_table_lookup(tblset, HASH_TID, alen, addr);
84 1.2.2.6 riz fail |= !(error != 0);
85 1.2.2.3 riz
86 1.2.2.5 riz error = npf_table_lookup(tblset, TREE_TID, alen, addr);
87 1.2.2.6 riz fail |= !(error != 0);
88 1.2.2.3 riz
89 1.2.2.2 riz /* Fill both tables with IP addresses. */
90 1.2.2.2 riz for (i = 0; i < __arraycount(ip_list); i++) {
91 1.2.2.2 riz addr->s6_addr32[0] = inet_addr(ip_list[i]);
92 1.2.2.2 riz
93 1.2.2.5 riz error = npf_table_insert(tblset, HASH_TID, alen, addr, nm);
94 1.2.2.6 riz fail |= !(error == 0);
95 1.2.2.5 riz error = npf_table_insert(tblset, HASH_TID, alen, addr, nm);
96 1.2.2.6 riz fail |= !(error != 0);
97 1.2.2.2 riz
98 1.2.2.5 riz error = npf_table_insert(tblset, TREE_TID, alen, addr, nm);
99 1.2.2.6 riz fail |= !(error == 0);
100 1.2.2.5 riz error = npf_table_insert(tblset, TREE_TID, alen, addr, nm);
101 1.2.2.6 riz fail |= !(error != 0);
102 1.2.2.2 riz }
103 1.2.2.2 riz
104 1.2.2.2 riz /* Attempt to add duplicates - should fail. */
105 1.2.2.2 riz addr->s6_addr32[0] = inet_addr(ip_list[0]);
106 1.2.2.5 riz alen = sizeof(struct in_addr);
107 1.2.2.2 riz
108 1.2.2.5 riz error = npf_table_insert(tblset, HASH_TID, alen, addr, nm);
109 1.2.2.6 riz fail |= !(error != 0);
110 1.2.2.2 riz
111 1.2.2.5 riz error = npf_table_insert(tblset, TREE_TID, alen, addr, nm);
112 1.2.2.6 riz fail |= !(error != 0);
113 1.2.2.2 riz
114 1.2.2.2 riz /* Reference checks. */
115 1.2.2.2 riz t1 = npf_table_get(tblset, HASH_TID);
116 1.2.2.6 riz fail |= !(t1 != NULL);
117 1.2.2.2 riz npf_table_put(t1);
118 1.2.2.2 riz
119 1.2.2.2 riz t2 = npf_table_get(tblset, TREE_TID);
120 1.2.2.6 riz fail |= !(t2 != NULL);
121 1.2.2.2 riz npf_table_put(t2);
122 1.2.2.2 riz
123 1.2.2.2 riz /* Match (validate) each IP entry. */
124 1.2.2.2 riz for (i = 0; i < __arraycount(ip_list); i++) {
125 1.2.2.2 riz addr->s6_addr32[0] = inet_addr(ip_list[i]);
126 1.2.2.2 riz
127 1.2.2.5 riz error = npf_table_lookup(tblset, HASH_TID, alen, addr);
128 1.2.2.6 riz fail |= !(error == 0);
129 1.2.2.2 riz
130 1.2.2.5 riz error = npf_table_lookup(tblset, TREE_TID, alen, addr);
131 1.2.2.6 riz fail |= !(error == 0);
132 1.2.2.2 riz }
133 1.2.2.2 riz
134 1.2.2.5 riz /* IPv6 addresses. */
135 1.2.2.5 riz memcpy(addr, ip6_list[0], sizeof(ip6_list[0]));
136 1.2.2.5 riz alen = sizeof(struct in6_addr);
137 1.2.2.5 riz
138 1.2.2.5 riz error = npf_table_insert(tblset, HASH_TID, alen, addr, nm);
139 1.2.2.6 riz fail |= !(error == 0);
140 1.2.2.5 riz error = npf_table_lookup(tblset, HASH_TID, alen, addr);
141 1.2.2.6 riz fail |= !(error == 0);
142 1.2.2.5 riz error = npf_table_remove(tblset, HASH_TID, alen, addr, nm);
143 1.2.2.6 riz fail |= !(error == 0);
144 1.2.2.5 riz
145 1.2.2.5 riz error = npf_table_insert(tblset, TREE_TID, alen, addr, nm);
146 1.2.2.6 riz fail |= !(error == 0);
147 1.2.2.5 riz error = npf_table_lookup(tblset, TREE_TID, alen, addr);
148 1.2.2.6 riz fail |= !(error == 0);
149 1.2.2.5 riz error = npf_table_remove(tblset, TREE_TID, alen, addr, nm);
150 1.2.2.6 riz fail |= !(error == 0);
151 1.2.2.5 riz
152 1.2.2.5 riz /*
153 1.2.2.5 riz * Masking: 96, 32, 127.
154 1.2.2.5 riz */
155 1.2.2.5 riz
156 1.2.2.5 riz memcpy(addr, ip6_list[1], sizeof(ip6_list[1]));
157 1.2.2.5 riz error = npf_table_insert(tblset, TREE_TID, alen, addr, 96);
158 1.2.2.6 riz fail |= !(error == 0);
159 1.2.2.5 riz
160 1.2.2.5 riz memcpy(addr, ip6_list[0], sizeof(ip6_list[0]));
161 1.2.2.5 riz error = npf_table_lookup(tblset, TREE_TID, alen, addr);
162 1.2.2.6 riz fail |= !(error == 0);
163 1.2.2.5 riz
164 1.2.2.5 riz memcpy(addr, ip6_list[1], sizeof(ip6_list[1]));
165 1.2.2.5 riz error = npf_table_remove(tblset, TREE_TID, alen, addr, 96);
166 1.2.2.6 riz fail |= !(error == 0);
167 1.2.2.5 riz
168 1.2.2.5 riz
169 1.2.2.5 riz memcpy(addr, ip6_list[2], sizeof(ip6_list[2]));
170 1.2.2.5 riz error = npf_table_insert(tblset, TREE_TID, alen, addr, 32);
171 1.2.2.6 riz fail |= !(error == 0);
172 1.2.2.5 riz
173 1.2.2.5 riz memcpy(addr, ip6_list[0], sizeof(ip6_list[0]));
174 1.2.2.5 riz error = npf_table_lookup(tblset, TREE_TID, alen, addr);
175 1.2.2.6 riz fail |= !(error == 0);
176 1.2.2.5 riz
177 1.2.2.5 riz memcpy(addr, ip6_list[2], sizeof(ip6_list[2]));
178 1.2.2.5 riz error = npf_table_remove(tblset, TREE_TID, alen, addr, 32);
179 1.2.2.6 riz fail |= !(error == 0);
180 1.2.2.5 riz
181 1.2.2.5 riz
182 1.2.2.5 riz memcpy(addr, ip6_list[3], sizeof(ip6_list[3]));
183 1.2.2.5 riz error = npf_table_insert(tblset, TREE_TID, alen, addr, 126);
184 1.2.2.6 riz fail |= !(error == 0);
185 1.2.2.5 riz
186 1.2.2.5 riz memcpy(addr, ip6_list[0], sizeof(ip6_list[0]));
187 1.2.2.5 riz error = npf_table_lookup(tblset, TREE_TID, alen, addr);
188 1.2.2.6 riz fail |= !(error != 0);
189 1.2.2.5 riz
190 1.2.2.5 riz memcpy(addr, ip6_list[3], sizeof(ip6_list[3]));
191 1.2.2.5 riz error = npf_table_remove(tblset, TREE_TID, alen, addr, 126);
192 1.2.2.6 riz fail |= !(error == 0);
193 1.2.2.5 riz
194 1.2.2.5 riz
195 1.2.2.5 riz alen = sizeof(struct in_addr);
196 1.2.2.5 riz
197 1.2.2.5 riz /* Remove all IPv4 entries. */
198 1.2.2.2 riz for (i = 0; i < __arraycount(ip_list); i++) {
199 1.2.2.2 riz addr->s6_addr32[0] = inet_addr(ip_list[i]);
200 1.2.2.2 riz
201 1.2.2.5 riz error = npf_table_remove(tblset, HASH_TID, alen, addr, nm);
202 1.2.2.6 riz fail |= !(error == 0);
203 1.2.2.2 riz
204 1.2.2.5 riz error = npf_table_remove(tblset, TREE_TID, alen, addr, nm);
205 1.2.2.6 riz fail |= !(error == 0);
206 1.2.2.2 riz }
207 1.2.2.2 riz
208 1.2.2.2 riz npf_tableset_destroy(tblset);
209 1.2.2.2 riz npf_tableset_sysfini();
210 1.2.2.2 riz
211 1.2.2.6 riz return !fail;
212 1.2.2.2 riz }
213