Statistics
| Branch: | Revision:

iof-bird / bird-2.0.1 / nest / a-path_test.c @ 6b3f1a54

History | View | Annotate | Download (5.85 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_default(&root_pool);
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 1;
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_default(&root_pool);
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 120
89
  byte buf[BUFFER_SIZE] = {};
90

    
91
  as_path_format(&empty_as_path, buf, BUFFER_SIZE);
92
  bt_assert_msg(strcmp(buf, "") == 0, "Buffer(%zu): '%s'", strlen(buf), buf);
93

    
94
  as_path_format(as_path, buf, BUFFER_SIZE);
95
  bt_assert_msg(strcmp(buf, "4294967294 4294967293 4294967292 4294967291 4294967290 4294967289 4294967288 4294967287 4294967286 4294967285") == 0, "Buffer(%zu): '%s'", strlen(buf), buf);
96

    
97
#define SMALL_BUFFER_SIZE 25
98
  byte buf2[SMALL_BUFFER_SIZE] = {};
99
  as_path_format(as_path, buf2, SMALL_BUFFER_SIZE);
100
  bt_assert_msg(strcmp(buf2, "4294967294 42...") == 0, "Small Buffer(%zu): '%s'", strlen(buf2), buf2);
101

    
102
  rfree(lp);
103

    
104
  return 1;
105
}
106

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

    
118
static int
119
t_path_include(void)
120
{
121
  resource_init();
122

    
123
  struct adata empty_as_path = {};
124
  struct adata *as_path = &empty_as_path;
125
  struct linpool *lp = lp_new_default(&root_pool);
126

    
127
  u32 as_nums[AS_PATH_LENGTH] = {};
128
  int i;
129
  for (i = 0; i < AS_PATH_LENGTH; i++)
130
  {
131
    u32 val = bt_random();
132
    as_nums[i] = val;
133
    as_path = as_path_prepend(lp, as_path, val);
134
  }
135

    
136
  for (i = 0; i < AS_PATH_LENGTH; i++)
137
  {
138
    int counts_of_contains = count_asn_in_array(as_nums, as_nums[i]);
139
    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]);
140

    
141
    bt_assert(as_path_filter(lp, as_path, NULL, as_nums[i], 0) != NULL);
142
    bt_assert(as_path_filter(lp, as_path, NULL, as_nums[i], 1) != NULL);
143
  }
144

    
145
  for (i = 0; i < 10000; i++)
146
  {
147
    u32 test_val = bt_random();
148
    int counts_of_contains = count_asn_in_array(as_nums, test_val);
149
    int result = as_path_contains(as_path, test_val, (counts_of_contains == 0 ? 1 : counts_of_contains));
150

    
151
    if (counts_of_contains)
152
      bt_assert_msg(result, "As path should contain %d-times the number %u", counts_of_contains, test_val);
153
    else
154
      bt_assert_msg(result == 0, "As path should not contain the number %u", test_val);
155
  }
156

    
157
  rfree(lp);
158

    
159
  return 1;
160
}
161

    
162
#if 0
163
static int
164
t_as_path_converting(void)
165
{
166
  resource_init();
167

168
  struct adata empty_as_path = {};
169
  struct adata *as_path = &empty_as_path;
170
  struct linpool *lp = lp_new_default(&root_pool);
171
#define AS_PATH_LENGTH_FOR_CONVERTING_TEST 10
172

173
  int i;
174
  for (i = 0; i < AS_PATH_LENGTH_FOR_CONVERTING_TEST; i++)
175
    as_path = as_path_prepend(lp, as_path, i);
176

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

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

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

205
  return 1;
206
}
207
#endif
208

    
209
int
210
main(int argc, char *argv[])
211
{
212
  bt_init(argc, argv);
213

    
214
  bt_test_suite(t_as_path_match, "Testing AS path matching and some a-path utilities.");
215
  bt_test_suite(t_path_format, "Testing formating as path into byte buffer");
216
  bt_test_suite(t_path_include, "Testing including a AS number in AS path");
217
  // bt_test_suite(t_as_path_converting, "Testing as_path_convert_to_*() output constancy");
218

    
219
  return bt_exit_value();
220
}