]> git.ipfire.org Git - thirdparty/bird.git/blob - nest/a-path_test.c
BMP: Fix reconfiguration
[thirdparty/bird.git] / nest / a-path_test.c
1 /*
2 * BIRD -- Path 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 "nest/route.h"
13 #include "nest/attrs.h"
14 #include "lib/resource.h"
15 #include "filter/data.h"
16
17 #define TESTS_NUM 30
18 #define AS_PATH_LENGTH 1000
19
20 #if AS_PATH_LENGTH > AS_PATH_MAXLEN
21 #warning "AS_PATH_LENGTH should be <= AS_PATH_MAXLEN"
22 #endif
23
24 static int
25 t_as_path_match(void)
26 {
27 int round;
28 for (round = 0; round < TESTS_NUM; round++)
29 {
30 struct adata empty_as_path = {};
31 struct adata *as_path = &empty_as_path;
32 u32 first_prepended, last_prepended;
33 first_prepended = last_prepended = 0;
34
35 struct f_path_mask *mask = alloca(sizeof(struct f_path_mask) + AS_PATH_LENGTH * sizeof(struct f_path_mask_item));
36 mask->len = AS_PATH_LENGTH;
37 for (int i = AS_PATH_LENGTH - 1; i >= 0; i--)
38 {
39 u32 val = bt_random();
40 as_path = as_path_prepend(tmp_linpool, as_path, val);
41 bt_debug("Prepending ASN: %10u \n", val);
42
43 if (i == 0)
44 last_prepended = val;
45 if (i == AS_PATH_LENGTH-1)
46 first_prepended = val;
47
48 mask->item[i].kind = PM_ASN;
49 mask->item[i].asn = val;
50 }
51
52 bt_assert_msg(as_path_match(as_path, mask), "Mask should match with AS path");
53
54 u32 asn;
55
56 bt_assert(as_path_get_first(as_path, &asn));
57 bt_assert_msg(asn == last_prepended, "as_path_get_first() should return the last prepended ASN");
58
59 bt_assert(as_path_get_last(as_path, &asn));
60 bt_assert_msg(asn == first_prepended, "as_path_get_last() should return the first prepended ASN");
61
62 tmp_flush();
63 }
64
65 return 1;
66 }
67
68 static int
69 t_path_format(void)
70 {
71 struct adata empty_as_path = {};
72 struct adata *as_path = &empty_as_path;
73
74 uint i;
75 for (i = 4294967285; i <= 4294967294; i++)
76 {
77 as_path = as_path_prepend(tmp_linpool, as_path, i);
78 bt_debug("Prepending ASN: %10u \n", i);
79 }
80
81 #define BUFFER_SIZE 120
82 byte buf[BUFFER_SIZE] = {};
83
84 as_path_format(&empty_as_path, buf, BUFFER_SIZE);
85 bt_assert_msg(strcmp(buf, "") == 0, "Buffer(%zu): '%s'", strlen(buf), buf);
86
87 as_path_format(as_path, buf, BUFFER_SIZE);
88 bt_assert_msg(strcmp(buf, "4294967294 4294967293 4294967292 4294967291 4294967290 4294967289 4294967288 4294967287 4294967286 4294967285") == 0, "Buffer(%zu): '%s'", strlen(buf), buf);
89
90 #define SMALL_BUFFER_SIZE 25
91 byte buf2[SMALL_BUFFER_SIZE] = {};
92 as_path_format(as_path, buf2, SMALL_BUFFER_SIZE);
93 bt_assert_msg(strcmp(buf2, "4294967294 42...") == 0, "Small Buffer(%zu): '%s'", strlen(buf2), buf2);
94
95 tmp_flush();
96
97 return 1;
98 }
99
100 static int
101 count_asn_in_array(const u32 *array, u32 asn)
102 {
103 int counts_of_contains = 0;
104 int u;
105 for (u = 0; u < AS_PATH_LENGTH; u++)
106 if (array[u] == asn)
107 counts_of_contains++;
108 return counts_of_contains;
109 }
110
111 static int
112 t_path_include(void)
113 {
114 struct adata empty_as_path = {};
115 struct adata *as_path = &empty_as_path;
116
117 u32 as_nums[AS_PATH_LENGTH] = {};
118 int i;
119 for (i = 0; i < AS_PATH_LENGTH; i++)
120 {
121 u32 val = bt_random();
122 as_nums[i] = val;
123 as_path = as_path_prepend(tmp_linpool, as_path, val);
124 }
125
126 for (i = 0; i < AS_PATH_LENGTH; i++)
127 {
128 int counts_of_contains = count_asn_in_array(as_nums, as_nums[i]);
129 bt_assert_msg(as_path_contains(as_path, as_nums[i], counts_of_contains), "AS Path should contains %d-times number %d", counts_of_contains, as_nums[i]);
130
131 struct f_val v = { .type = T_INT, .val.i = as_nums[i] };
132 bt_assert(as_path_filter(tmp_linpool, as_path, &v, 0) != NULL);
133 bt_assert(as_path_filter(tmp_linpool, as_path, &v, 1) != NULL);
134 }
135
136 for (i = 0; i < 10000; i++)
137 {
138 u32 test_val = bt_random();
139 int counts_of_contains = count_asn_in_array(as_nums, test_val);
140 int result = as_path_contains(as_path, test_val, (counts_of_contains == 0 ? 1 : counts_of_contains));
141
142 if (counts_of_contains)
143 bt_assert_msg(result, "As path should contain %d-times the number %u", counts_of_contains, test_val);
144 else
145 bt_assert_msg(result == 0, "As path should not contain the number %u", test_val);
146 }
147
148 tmp_flush();
149
150 return 1;
151 }
152
153 #if 0
154 static int
155 t_as_path_converting(void)
156 {
157 struct adata empty_as_path = {};
158 struct adata *as_path = &empty_as_path;
159 #define AS_PATH_LENGTH_FOR_CONVERTING_TEST 10
160
161 int i;
162 for (i = 0; i < AS_PATH_LENGTH_FOR_CONVERTING_TEST; i++)
163 as_path = as_path_prepend(tmp_linpool, as_path, i);
164
165 bt_debug("data length: %u \n", as_path->length);
166
167 byte buffer[100] = {};
168 int used_size = as_path_convert_to_new(as_path, buffer, AS_PATH_LENGTH_FOR_CONVERTING_TEST-1);
169 bt_debug("as_path_convert_to_new: len %d \n%s\n", used_size, buffer);
170 for (i = 0; i < used_size; i++)
171 {
172 bt_debug("\\03%d", buffer[i]);
173 }
174 bt_debug("\n");
175 bt_assert(memcmp(buffer,
176 "\032\039\030\030\030\030\030\030\030\039\030\030\030\030\030\030\030\038\030\030\030\030\030\030"
177 "\030\037\030\030\030\030\030\030\030\036\030\030\030\030",
178 38));
179
180 bzero(buffer, sizeof(buffer));
181 int new_used;
182 used_size = as_path_convert_to_old(as_path, buffer, &new_used);
183 bt_debug("as_path_convert_to_old: len %d, new_used: %d \n", used_size, new_used);
184 for (i = 0; i < used_size; i++)
185 {
186 bt_debug("\\03%d", buffer[i]);
187 }
188 bt_debug("\n");
189 bt_assert(memcmp(buffer,
190 "\032\0310\030\039\030\038\030\037\030\036\030\035\030\034\030\033\030\032\030\031\030\030",
191 22));
192
193 return 1;
194 }
195 #endif
196
197 int
198 main(int argc, char *argv[])
199 {
200 bt_init(argc, argv);
201
202 bt_test_suite(t_as_path_match, "Testing AS path matching and some a-path utilities.");
203 bt_test_suite(t_path_format, "Testing formating as path into byte buffer");
204 bt_test_suite(t_path_include, "Testing including a AS number in AS path");
205 // bt_test_suite(t_as_path_converting, "Testing as_path_convert_to_*() output constancy");
206
207 return bt_exit_value();
208 }