Statistics
| Branch: | Revision:

iof-bird-daemon / nest / a-path_test.c @ 9b0a0ba9

History | View | Annotate | Download (5.63 KB)

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

    
16
#define TESTS_NUM 30
17
#define AS_PATH_LENGTH 1000
18

    
19
#if AS_PATH_LENGTH > AS_PATH_MAXLEN
20
#warning "AS_PATH_LENGTH should be <= AS_PATH_MAXLEN"
21
#endif
22

    
23
static int
24
t_as_path_match(void)
25
{
26
  resource_init();
27

    
28
  int round;
29
  for (round = 0; round < TESTS_NUM; round++)
30
  {
31
    struct adata empty_as_path = {};
32
    struct adata *as_path = &empty_as_path;
33
    u32 first_prepended, last_prepended;
34
    first_prepended = last_prepended = 0;
35
    struct linpool *lp = lp_new(&root_pool, 0);
36

    
37
    struct f_path_mask mask[AS_PATH_LENGTH] = {};
38
    int i;
39
    for (i = 0; i < AS_PATH_LENGTH; i++)
40
    {
41
      u32 val = bt_random();
42
      as_path = as_path_prepend(lp, as_path, val);
43
      bt_debug("Prepending ASN: %10u \n", val);
44

    
45
      if (i == 0)
46
        first_prepended = val;
47
      if (i == AS_PATH_LENGTH-1)
48
        last_prepended = val;
49

    
50
      mask[i].kind = PM_ASN;
51
      mask[i].val  = val;
52
      if (i)
53
        mask[i].next = &mask[i-1];
54
    }
55

    
56
    bt_assert_msg(as_path_match(as_path, &mask[AS_PATH_LENGTH-1]), "Mask should match with AS path");
57

    
58
    u32 asn;
59

    
60
    bt_assert(as_path_get_first(as_path, &asn));
61
    bt_assert_msg(asn == last_prepended, "as_path_get_first() should return the last prepended ASN");
62

    
63
    bt_assert(as_path_get_last(as_path, &asn));
64
    bt_assert_msg(asn == first_prepended, "as_path_get_last() should return the first prepended ASN");
65

    
66
    rfree(lp);
67
  }
68

    
69
  return BT_SUCCESS;
70
}
71

    
72
static int
73
t_path_format(void)
74
{
75
  resource_init();
76

    
77
  struct adata empty_as_path = {};
78
  struct adata *as_path = &empty_as_path;
79
  struct linpool *lp = lp_new(&root_pool, 0);
80

    
81
  uint i;
82
  for (i = 4294967285; i <= 4294967294; i++)
83
  {
84
    as_path = as_path_prepend(lp, as_path, i);
85
    bt_debug("Prepending ASN: %10u \n", i);
86
  }
87

    
88
#define BUFFER_SIZE 26
89
  byte buf[BUFFER_SIZE] = {};
90
  as_path_format(as_path, buf, BUFFER_SIZE);
91
  bt_assert_msg(strcmp(buf, "4294967294 4294967293 ...") == 0, "Buffer(%zu): '%s'", strlen(buf), buf);
92

    
93
#define SMALL_BUFFER_SIZE 25
94
  byte buf2[SMALL_BUFFER_SIZE] = {};
95
  as_path_format(as_path, buf2, SMALL_BUFFER_SIZE);
96
  bt_assert_msg(strcmp(buf2, "4294967294 ...") == 0, "Small Buffer(%zu): '%s'", strlen(buf2), buf2);
97

    
98
  rfree(lp);
99

    
100
  return BT_SUCCESS;
101
}
102

    
103
static int
104
count_asn_in_array(const u32 *array, u32 asn)
105
{
106
  int counts_of_contains = 0;
107
  int u;
108
  for (u = 0; u < AS_PATH_LENGTH; u++)
109
    if (array[u] == asn)
110
        counts_of_contains++;
111
  return counts_of_contains;
112
}
113

    
114
static int
115
t_path_include(void)
116
{
117
  resource_init();
118

    
119
  struct adata empty_as_path = {};
120
  struct adata *as_path = &empty_as_path;
121
  struct linpool *lp = lp_new(&root_pool, 0);
122

    
123
  u32 as_nums[AS_PATH_LENGTH] = {};
124
  int i;
125
  for (i = 0; i < AS_PATH_LENGTH; i++)
126
  {
127
    u32 val = bt_random();
128
    as_nums[i] = val;
129
    as_path = as_path_prepend(lp, as_path, val);
130
  }
131

    
132
  for (i = 0; i < AS_PATH_LENGTH; i++)
133
  {
134
    int counts_of_contains = count_asn_in_array(as_nums, as_nums[i]);
135
    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]);
136

    
137
    bt_assert(as_path_filter(lp, as_path, NULL, as_nums[i], 0) != NULL);
138
    bt_assert(as_path_filter(lp, as_path, NULL, as_nums[i], 1) != NULL);
139
  }
140

    
141
  for (i = 0; i < 10000; i++)
142
  {
143
    u32 test_val = bt_random();
144
    int counts_of_contains = count_asn_in_array(as_nums, test_val);
145
    int result = as_path_contains(as_path, test_val, (counts_of_contains == 0 ? 1 : counts_of_contains));
146

    
147
    if (counts_of_contains)
148
      bt_assert_msg(result, "As path should contain %d-times the number %u", counts_of_contains, test_val);
149
    else
150
      bt_assert_msg(result == 0, "As path should not contain the number %u", test_val);
151
  }
152

    
153
  rfree(lp);
154

    
155
  return BT_SUCCESS;
156
}
157

    
158
static int
159
t_as_path_converting(void)
160
{
161
  resource_init();
162

    
163
  struct adata empty_as_path = {};
164
  struct adata *as_path = &empty_as_path;
165
  struct linpool *lp = lp_new(&root_pool, 0);
166
#define AS_PATH_LENGTH_FOR_CONVERTING_TEST 10
167

    
168
  int i;
169
  for (i = 0; i < AS_PATH_LENGTH_FOR_CONVERTING_TEST; i++)
170
    as_path = as_path_prepend(lp, as_path, i);
171

    
172
  bt_debug("data length: %u \n", as_path->length);
173

    
174
  byte buffer[100] = {};
175
  int used_size = as_path_convert_to_new(as_path, buffer, AS_PATH_LENGTH_FOR_CONVERTING_TEST-1);
176
  bt_debug("as_path_convert_to_new: len %d \n%s\n", used_size, buffer);
177
  for (i = 0; i < used_size; i++)
178
  {
179
    bt_debug("\\03%d", buffer[i]);
180
  }
181
  bt_debug("\n");
182
  bt_assert(memcmp(buffer,
183
                   "\032\039\030\030\030\030\030\030\030\039\030\030\030\030\030\030\030\038\030\030\030\030\030\030"
184
                   "\030\037\030\030\030\030\030\030\030\036\030\030\030\030",
185
                   38));
186

    
187
  bzero(buffer, sizeof(buffer));
188
  int new_used;
189
  used_size = as_path_convert_to_old(as_path, buffer, &new_used);
190
  bt_debug("as_path_convert_to_old: len %d, new_used: %d \n", used_size, new_used);
191
  for (i = 0; i < used_size; i++)
192
  {
193
    bt_debug("\\03%d", buffer[i]);
194
  }
195
  bt_debug("\n");
196
  bt_assert(memcmp(buffer,
197
                   "\032\0310\030\039\030\038\030\037\030\036\030\035\030\034\030\033\030\032\030\031\030\030",
198
                   22));
199

    
200
  return BT_SUCCESS;
201
}
202

    
203
int
204
main(int argc, char *argv[])
205
{
206
  bt_init(argc, argv);
207

    
208
  bt_test_suite(t_as_path_match, "Testing AS path matching and some a-path utilities.");
209
  bt_test_suite(t_path_format, "Testing formating as path into byte buffer");
210
  bt_test_suite(t_path_include, "Testing including a AS number in AS path");
211
  bt_test_suite(t_as_path_converting, "Testing as_path_convert_to_*() output constancy");
212

    
213
  return bt_exit_value();
214
}