]> git.ipfire.org Git - thirdparty/bird.git/blame - nest/a-set_test.c
OSPF: Allow loopback nexthop in OSPFv3-IPv4
[thirdparty/bird.git] / nest / a-set_test.c
CommitLineData
9b0a0ba9
OZ
1/*
2 * BIRD -- Set/Community-list Operations Tests
3 *
4 * (c) 2015 CZ.NIC z.s.p.o.
5 *
6 * Can be freely distributed and used under the terms of the GNU GPL.
7 */
8
9#include "test/birdtest.h"
10#include "test/bt-utils.h"
11
12#include "lib/net.h"
13#include "nest/route.h"
14#include "nest/attrs.h"
15#include "lib/resource.h"
16
17#define SET_SIZE 10
4c553c5a
MM
18static const struct adata *set_sequence; /* <0; SET_SIZE) */
19static const struct adata *set_sequence_same; /* <0; SET_SIZE) */
20static const struct adata *set_sequence_higher; /* <SET_SIZE; 2*SET_SIZE) */
21static const struct adata *set_random;
9b0a0ba9
OZ
22
23#define BUFFER_SIZE 1000
24static byte buf[BUFFER_SIZE] = {};
25
26#define SET_SIZE_FOR_FORMAT_OUTPUT 10
27
9b0a0ba9
OZ
28enum set_type
29{
30 SET_TYPE_INT,
31 SET_TYPE_EC
32};
33
34static void
4c553c5a 35generate_set_sequence(enum set_type type, int len)
9b0a0ba9
OZ
36{
37 struct adata empty_as_path = {};
38 set_sequence = set_sequence_same = set_sequence_higher = set_random = &empty_as_path;
9b0a0ba9
OZ
39
40 int i;
4c553c5a 41 for (i = 0; i < len; i++)
9b0a0ba9
OZ
42 {
43 if (type == SET_TYPE_INT)
44 {
d814a8cb
MM
45 set_sequence = int_set_add(tmp_linpool, set_sequence, i);
46 set_sequence_same = int_set_add(tmp_linpool, set_sequence_same, i);
47 set_sequence_higher = int_set_add(tmp_linpool, set_sequence_higher, i + SET_SIZE);
48 set_random = int_set_add(tmp_linpool, set_random, bt_random());
9b0a0ba9
OZ
49 }
50 else if (type == SET_TYPE_EC)
51 {
d814a8cb
MM
52 set_sequence = ec_set_add(tmp_linpool, set_sequence, i);
53 set_sequence_same = ec_set_add(tmp_linpool, set_sequence_same, i);
54 set_sequence_higher = ec_set_add(tmp_linpool, set_sequence_higher, i + SET_SIZE);
55 set_random = ec_set_add(tmp_linpool, set_random, (bt_random() << 32 | bt_random()));
9b0a0ba9
OZ
56 }
57 else
58 bt_abort_msg("This should be unreachable");
59 }
60}
61
62/*
63 * SET INT TESTS
64 */
65
66static int
67t_set_int_contains(void)
68{
69 int i;
70
4c553c5a 71 generate_set_sequence(SET_TYPE_INT, SET_SIZE);
9b0a0ba9
OZ
72
73 bt_assert(int_set_get_size(set_sequence) == SET_SIZE);
74
75 for (i = 0; i < SET_SIZE; i++)
76 bt_assert(int_set_contains(set_sequence, i));
77 bt_assert(int_set_contains(set_sequence, -1) == 0);
78 bt_assert(int_set_contains(set_sequence, SET_SIZE) == 0);
79
80 int *data = int_set_get_data(set_sequence);
81 for (i = 0; i < SET_SIZE; i++)
82 bt_assert_msg(data[i] == i, "(data[i] = %d) == i = %d)", data[i], i);
83
5e3cd0e5 84 return 1;
9b0a0ba9
OZ
85}
86
87static int
88t_set_int_union(void)
89{
4c553c5a 90 generate_set_sequence(SET_TYPE_INT, SET_SIZE);
9b0a0ba9 91
4c553c5a 92 const struct adata *set_union;
d814a8cb 93 set_union = int_set_union(tmp_linpool, set_sequence, set_sequence_same);
9b0a0ba9 94 bt_assert(int_set_get_size(set_union) == SET_SIZE);
08571b20 95 bt_assert(int_set_format(set_union, ISF_ROUTER_ID, 2, buf, BUFFER_SIZE) == 0);
9b0a0ba9 96
d814a8cb 97 set_union = int_set_union(tmp_linpool, set_sequence, set_sequence_higher);
9b0a0ba9 98 bt_assert_msg(int_set_get_size(set_union) == SET_SIZE*2, "int_set_get_size(set_union) %d, SET_SIZE*2 %d", int_set_get_size(set_union), SET_SIZE*2);
08571b20 99 bt_assert(int_set_format(set_union, ISF_ROUTER_ID, 2, buf, BUFFER_SIZE) == 0);
9b0a0ba9 100
5e3cd0e5 101 return 1;
9b0a0ba9
OZ
102}
103
104static int
105t_set_int_format(void)
106{
4c553c5a 107 generate_set_sequence(SET_TYPE_INT, SET_SIZE_FOR_FORMAT_OUTPUT);
9b0a0ba9 108
08571b20 109 bt_assert(int_set_format(set_sequence, ISF_ROUTER_ID, 0, buf, BUFFER_SIZE) == 0);
9b0a0ba9
OZ
110 bt_assert(strcmp(buf, "0.0.0.0 0.0.0.1 0.0.0.2 0.0.0.3 0.0.0.4 0.0.0.5 0.0.0.6 0.0.0.7 0.0.0.8 0.0.0.9") == 0);
111
112 bzero(buf, BUFFER_SIZE);
08571b20 113 bt_assert(int_set_format(set_sequence, ISF_ROUTER_ID, 2, buf, BUFFER_SIZE) == 0);
9b0a0ba9
OZ
114 bt_assert(strcmp(buf, "0.0.0.2 0.0.0.3 0.0.0.4 0.0.0.5 0.0.0.6 0.0.0.7 0.0.0.8 0.0.0.9") == 0);
115
116 bzero(buf, BUFFER_SIZE);
08571b20 117 bt_assert(int_set_format(set_sequence, ISF_COMMUNITY_LIST, 0, buf, BUFFER_SIZE) == 0);
9b0a0ba9
OZ
118 bt_assert(strcmp(buf, "(0,0) (0,1) (0,2) (0,3) (0,4) (0,5) (0,6) (0,7) (0,8) (0,9)") == 0);
119
08571b20
MM
120 bzero(buf, BUFFER_SIZE);
121 bt_assert(int_set_format(set_sequence, ISF_NUMBERS, 0, buf, BUFFER_SIZE) == 0);
122 bt_assert(strcmp(buf, "0 1 2 3 4 5 6 7 8 9") == 0);
123
5e3cd0e5 124 return 1;
9b0a0ba9
OZ
125}
126
127static int
128t_set_int_delete(void)
129{
4c553c5a 130 generate_set_sequence(SET_TYPE_INT, SET_SIZE);
9b0a0ba9 131
4c553c5a 132 const struct adata *deleting_sequence = set_sequence;
9b0a0ba9
OZ
133 u32 i;
134 for (i = 0; i < SET_SIZE; i++)
135 {
d814a8cb 136 deleting_sequence = int_set_del(tmp_linpool, deleting_sequence, i);
9b0a0ba9
OZ
137 bt_assert_msg(int_set_get_size(deleting_sequence) == (int) (SET_SIZE-1-i),
138 "int_set_get_size(deleting_sequence) %d == SET_SIZE-1-i %d",
139 int_set_get_size(deleting_sequence),
140 SET_SIZE-1-i);
141 }
142
143 bt_assert(int_set_get_size(set_sequence) == SET_SIZE);
144
5e3cd0e5 145 return 1;
9b0a0ba9
OZ
146}
147
148/*
149 * SET EC TESTS
150 */
151
152static int
153t_set_ec_contains(void)
154{
155 u32 i;
156
4c553c5a 157 generate_set_sequence(SET_TYPE_EC, SET_SIZE);
9b0a0ba9
OZ
158
159 bt_assert(ec_set_get_size(set_sequence) == SET_SIZE);
160
161 for (i = 0; i < SET_SIZE; i++)
162 bt_assert(ec_set_contains(set_sequence, i));
163 bt_assert(ec_set_contains(set_sequence, -1) == 0);
164 bt_assert(ec_set_contains(set_sequence, SET_SIZE) == 0);
165
166// int *data = ec_set_get_data(set_sequence);
167// for (i = 0; i < SET_SIZE; i++)
168// bt_assert_msg(data[i] == (SET_SIZE-1-i), "(data[i] = %d) == ((SET_SIZE-1-i) = %d)", data[i], SET_SIZE-1-i);
169
5e3cd0e5 170 return 1;
9b0a0ba9
OZ
171}
172
173static int
174t_set_ec_union(void)
175{
4c553c5a 176 generate_set_sequence(SET_TYPE_EC, SET_SIZE);
9b0a0ba9 177
4c553c5a 178 const struct adata *set_union;
d814a8cb 179 set_union = ec_set_union(tmp_linpool, set_sequence, set_sequence_same);
9b0a0ba9
OZ
180 bt_assert(ec_set_get_size(set_union) == SET_SIZE);
181 bt_assert(ec_set_format(set_union, 0, buf, BUFFER_SIZE) == 0);
182
d814a8cb 183 set_union = ec_set_union(tmp_linpool, set_sequence, set_sequence_higher);
9b0a0ba9
OZ
184 bt_assert_msg(ec_set_get_size(set_union) == SET_SIZE*2, "ec_set_get_size(set_union) %d, SET_SIZE*2 %d", ec_set_get_size(set_union), SET_SIZE*2);
185 bt_assert(ec_set_format(set_union, 0, buf, BUFFER_SIZE) == 0);
186
5e3cd0e5 187 return 1;
9b0a0ba9
OZ
188}
189
190static int
191t_set_ec_format(void)
192{
4c553c5a 193 const struct adata empty_as_path = {};
9b0a0ba9 194 set_sequence = set_sequence_same = set_sequence_higher = set_random = &empty_as_path;
9b0a0ba9
OZ
195
196 u64 i = 0;
d814a8cb 197 set_sequence = ec_set_add(tmp_linpool, set_sequence, i);
9b0a0ba9 198 for (i = 1; i < SET_SIZE_FOR_FORMAT_OUTPUT; i++)
d814a8cb 199 set_sequence = ec_set_add(tmp_linpool, set_sequence, i + ((i%2) ? ((u64)EC_RO << 48) : ((u64)EC_RT << 48)));
9b0a0ba9
OZ
200
201 bt_assert(ec_set_format(set_sequence, 0, buf, BUFFER_SIZE) == 0);
202 bt_assert_msg(strcmp(buf, "(unknown 0x0, 0, 0) (ro, 0, 1) (rt, 0, 2) (ro, 0, 3) (rt, 0, 4) (ro, 0, 5) (rt, 0, 6) (ro, 0, 7) (rt, 0, 8) (ro, 0, 9)") == 0,
203 "ec_set_format() returns '%s'", buf);
204
5e3cd0e5 205 return 1;
9b0a0ba9
OZ
206}
207
208static int
209t_set_ec_delete(void)
210{
4c553c5a 211 generate_set_sequence(SET_TYPE_EC, SET_SIZE);
9b0a0ba9 212
4c553c5a 213 const struct adata *deleting_sequence = set_sequence;
9b0a0ba9
OZ
214 u32 i;
215 for (i = 0; i < SET_SIZE; i++)
216 {
d814a8cb 217 deleting_sequence = ec_set_del(tmp_linpool, deleting_sequence, i);
9b0a0ba9
OZ
218 bt_assert_msg(ec_set_get_size(deleting_sequence) == (int) (SET_SIZE-1-i),
219 "ec_set_get_size(deleting_sequence) %d == SET_SIZE-1-i %d",
220 ec_set_get_size(deleting_sequence), SET_SIZE-1-i);
221 }
222
223 bt_assert(ec_set_get_size(set_sequence) == SET_SIZE);
224
5e3cd0e5 225 return 1;
9b0a0ba9
OZ
226}
227
228int
229main(int argc, char *argv[])
230{
231 bt_init(argc, argv);
232
233 bt_test_suite(t_set_int_contains, "Testing sets of integers: contains, get_data");
234 bt_test_suite(t_set_int_format, "Testing sets of integers: format");
235 bt_test_suite(t_set_int_union, "Testing sets of integers: union");
236 bt_test_suite(t_set_int_delete, "Testing sets of integers: delete");
237
238 bt_test_suite(t_set_ec_contains, "Testing sets of Extended Community values: contains, get_data");
239 bt_test_suite(t_set_ec_format, "Testing sets of Extended Community values: format");
240 bt_test_suite(t_set_ec_union, "Testing sets of Extended Community values: union");
241 bt_test_suite(t_set_ec_delete, "Testing sets of Extended Community values: delete");
242
243 return bt_exit_value();
244}