]> git.ipfire.org Git - thirdparty/bird.git/blame - nest/a-path_test.c
BMP: Fix reconfiguration
[thirdparty/bird.git] / nest / a-path_test.c
CommitLineData
9b0a0ba9
OZ
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"
8f3c6151 15#include "filter/data.h"
9b0a0ba9
OZ
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
24static int
25t_as_path_match(void)
26{
9b0a0ba9
OZ
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;
9b0a0ba9 34
4c553c5a
MM
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--)
9b0a0ba9
OZ
38 {
39 u32 val = bt_random();
d814a8cb 40 as_path = as_path_prepend(tmp_linpool, as_path, val);
9b0a0ba9
OZ
41 bt_debug("Prepending ASN: %10u \n", val);
42
43 if (i == 0)
a82f692e 44 last_prepended = val;
4c553c5a
MM
45 if (i == AS_PATH_LENGTH-1)
46 first_prepended = val;
9b0a0ba9 47
4c553c5a
MM
48 mask->item[i].kind = PM_ASN;
49 mask->item[i].asn = val;
9b0a0ba9
OZ
50 }
51
4c553c5a 52 bt_assert_msg(as_path_match(as_path, mask), "Mask should match with AS path");
9b0a0ba9
OZ
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
d814a8cb 62 tmp_flush();
9b0a0ba9
OZ
63 }
64
5e3cd0e5 65 return 1;
9b0a0ba9
OZ
66}
67
68static int
69t_path_format(void)
70{
9b0a0ba9
OZ
71 struct adata empty_as_path = {};
72 struct adata *as_path = &empty_as_path;
9b0a0ba9
OZ
73
74 uint i;
75 for (i = 4294967285; i <= 4294967294; i++)
76 {
d814a8cb 77 as_path = as_path_prepend(tmp_linpool, as_path, i);
9b0a0ba9
OZ
78 bt_debug("Prepending ASN: %10u \n", i);
79 }
80
5509e17d 81#define BUFFER_SIZE 120
9b0a0ba9 82 byte buf[BUFFER_SIZE] = {};
5509e17d
OZ
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
9b0a0ba9 87 as_path_format(as_path, buf, BUFFER_SIZE);
5509e17d 88 bt_assert_msg(strcmp(buf, "4294967294 4294967293 4294967292 4294967291 4294967290 4294967289 4294967288 4294967287 4294967286 4294967285") == 0, "Buffer(%zu): '%s'", strlen(buf), buf);
9b0a0ba9
OZ
89
90#define SMALL_BUFFER_SIZE 25
91 byte buf2[SMALL_BUFFER_SIZE] = {};
92 as_path_format(as_path, buf2, SMALL_BUFFER_SIZE);
5509e17d 93 bt_assert_msg(strcmp(buf2, "4294967294 42...") == 0, "Small Buffer(%zu): '%s'", strlen(buf2), buf2);
9b0a0ba9 94
d814a8cb 95 tmp_flush();
9b0a0ba9 96
5e3cd0e5 97 return 1;
9b0a0ba9
OZ
98}
99
100static int
101count_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
111static int
112t_path_include(void)
113{
9b0a0ba9
OZ
114 struct adata empty_as_path = {};
115 struct adata *as_path = &empty_as_path;
9b0a0ba9
OZ
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;
d814a8cb 123 as_path = as_path_prepend(tmp_linpool, as_path, val);
9b0a0ba9
OZ
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
8f3c6151 131 struct f_val v = { .type = T_INT, .val.i = as_nums[i] };
2e5bfeb7
MM
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);
9b0a0ba9
OZ
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
d814a8cb 148 tmp_flush();
9b0a0ba9 149
5e3cd0e5 150 return 1;
9b0a0ba9
OZ
151}
152
c42ecaab 153#if 0
9b0a0ba9
OZ
154static int
155t_as_path_converting(void)
156{
9b0a0ba9
OZ
157 struct adata empty_as_path = {};
158 struct adata *as_path = &empty_as_path;
9b0a0ba9
OZ
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++)
d814a8cb 163 as_path = as_path_prepend(tmp_linpool, as_path, i);
9b0a0ba9
OZ
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
5e3cd0e5 193 return 1;
9b0a0ba9 194}
c42ecaab 195#endif
9b0a0ba9
OZ
196
197int
198main(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");
c42ecaab 205 // bt_test_suite(t_as_path_converting, "Testing as_path_convert_to_*() output constancy");
9b0a0ba9
OZ
206
207 return bt_exit_value();
208}