a-path_test.c 5.87 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
/*
 *	BIRD -- Path Operations Tests
 *
 *	(c) 2015 CZ.NIC z.s.p.o.
 *
 *	Can be freely distributed and used under the terms of the GNU GPL.
 */

#include "test/birdtest.h"
#include "test/bt-utils.h"

#include "nest/route.h"
#include "nest/attrs.h"
#include "lib/resource.h"

#define TESTS_NUM 30
#define AS_PATH_LENGTH 1000

#if AS_PATH_LENGTH > AS_PATH_MAXLEN
#warning "AS_PATH_LENGTH should be <= AS_PATH_MAXLEN"
#endif

static int
t_as_path_match(void)
{
  resource_init();

  int round;
  for (round = 0; round < TESTS_NUM; round++)
  {
    struct adata empty_as_path = {};
    struct adata *as_path = &empty_as_path;
    u32 first_prepended, last_prepended;
    first_prepended = last_prepended = 0;
35
    struct linpool *lp = lp_new_default(&root_pool);
36

37 38 39
    struct f_path_mask *mask = alloca(sizeof(struct f_path_mask) + AS_PATH_LENGTH * sizeof(struct f_path_mask_item));
    mask->len = AS_PATH_LENGTH;
    for (int i = AS_PATH_LENGTH - 1; i >= 0; i--)
40 41 42 43 44 45
    {
      u32 val = bt_random();
      as_path = as_path_prepend(lp, as_path, val);
      bt_debug("Prepending ASN: %10u \n", val);

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

50 51
      mask->item[i].kind = PM_ASN;
      mask->item[i].asn  = val;
52 53
    }

54
    bt_assert_msg(as_path_match(as_path, mask), "Mask should match with AS path");
55 56 57 58 59 60 61 62 63 64 65 66

    u32 asn;

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

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

    rfree(lp);
  }

67
  return 1;
68 69 70 71 72 73 74 75 76
}

static int
t_path_format(void)
{
  resource_init();

  struct adata empty_as_path = {};
  struct adata *as_path = &empty_as_path;
77
  struct linpool *lp = lp_new_default(&root_pool);
78 79 80 81 82 83 84 85

  uint i;
  for (i = 4294967285; i <= 4294967294; i++)
  {
    as_path = as_path_prepend(lp, as_path, i);
    bt_debug("Prepending ASN: %10u \n", i);
  }

86
#define BUFFER_SIZE 120
87
  byte buf[BUFFER_SIZE] = {};
88 89 90 91

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

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

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

  rfree(lp);

102
  return 1;
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
}

static int
count_asn_in_array(const u32 *array, u32 asn)
{
  int counts_of_contains = 0;
  int u;
  for (u = 0; u < AS_PATH_LENGTH; u++)
    if (array[u] == asn)
	counts_of_contains++;
  return counts_of_contains;
}

static int
t_path_include(void)
{
  resource_init();

  struct adata empty_as_path = {};
  struct adata *as_path = &empty_as_path;
123
  struct linpool *lp = lp_new_default(&root_pool);
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156

  u32 as_nums[AS_PATH_LENGTH] = {};
  int i;
  for (i = 0; i < AS_PATH_LENGTH; i++)
  {
    u32 val = bt_random();
    as_nums[i] = val;
    as_path = as_path_prepend(lp, as_path, val);
  }

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

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

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

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

  rfree(lp);

157
  return 1;
158 159
}

Ondřej Zajíček's avatar
Ondřej Zajíček committed
160
#if 0
161 162 163 164 165 166 167
static int
t_as_path_converting(void)
{
  resource_init();

  struct adata empty_as_path = {};
  struct adata *as_path = &empty_as_path;
168
  struct linpool *lp = lp_new_default(&root_pool);
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
#define AS_PATH_LENGTH_FOR_CONVERTING_TEST 10

  int i;
  for (i = 0; i < AS_PATH_LENGTH_FOR_CONVERTING_TEST; i++)
    as_path = as_path_prepend(lp, as_path, i);

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

  byte buffer[100] = {};
  int used_size = as_path_convert_to_new(as_path, buffer, AS_PATH_LENGTH_FOR_CONVERTING_TEST-1);
  bt_debug("as_path_convert_to_new: len %d \n%s\n", used_size, buffer);
  for (i = 0; i < used_size; i++)
  {
    bt_debug("\\03%d", buffer[i]);
  }
  bt_debug("\n");
  bt_assert(memcmp(buffer,
		   "\032\039\030\030\030\030\030\030\030\039\030\030\030\030\030\030\030\038\030\030\030\030\030\030"
		   "\030\037\030\030\030\030\030\030\030\036\030\030\030\030",
		   38));

  bzero(buffer, sizeof(buffer));
  int new_used;
  used_size = as_path_convert_to_old(as_path, buffer, &new_used);
  bt_debug("as_path_convert_to_old: len %d, new_used: %d \n", used_size, new_used);
  for (i = 0; i < used_size; i++)
  {
    bt_debug("\\03%d", buffer[i]);
  }
  bt_debug("\n");
  bt_assert(memcmp(buffer,
		   "\032\0310\030\039\030\038\030\037\030\036\030\035\030\034\030\033\030\032\030\031\030\030",
		   22));

203
  return 1;
204
}
Ondřej Zajíček's avatar
Ondřej Zajíček committed
205
#endif
206 207 208 209 210 211 212 213 214

int
main(int argc, char *argv[])
{
  bt_init(argc, argv);

  bt_test_suite(t_as_path_match, "Testing AS path matching and some a-path utilities.");
  bt_test_suite(t_path_format, "Testing formating as path into byte buffer");
  bt_test_suite(t_path_include, "Testing including a AS number in AS path");
Ondřej Zajíček's avatar
Ondřej Zajíček committed
215
  // bt_test_suite(t_as_path_converting, "Testing as_path_convert_to_*() output constancy");
216 217 218

  return bt_exit_value();
}