Annotation of embedaddon/bird2/nest/a-path_test.c, revision 1.1
1.1 ! misho 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 = alloca(sizeof(struct f_path_mask) + AS_PATH_LENGTH * sizeof(struct f_path_mask_item));
! 38: mask->len = AS_PATH_LENGTH;
! 39: for (int i = AS_PATH_LENGTH - 1; i >= 0; 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: last_prepended = val;
! 47: if (i == AS_PATH_LENGTH-1)
! 48: first_prepended = val;
! 49:
! 50: mask->item[i].kind = PM_ASN;
! 51: 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: u32 asn;
! 57:
! 58: bt_assert(as_path_get_first(as_path, &asn));
! 59: bt_assert_msg(asn == last_prepended, "as_path_get_first() should return the last prepended ASN");
! 60:
! 61: bt_assert(as_path_get_last(as_path, &asn));
! 62: bt_assert_msg(asn == first_prepended, "as_path_get_last() should return the first prepended ASN");
! 63:
! 64: rfree(lp);
! 65: }
! 66:
! 67: return 1;
! 68: }
! 69:
! 70: static int
! 71: t_path_format(void)
! 72: {
! 73: resource_init();
! 74:
! 75: struct adata empty_as_path = {};
! 76: struct adata *as_path = &empty_as_path;
! 77: struct linpool *lp = lp_new_default(&root_pool);
! 78:
! 79: uint i;
! 80: for (i = 4294967285; i <= 4294967294; i++)
! 81: {
! 82: as_path = as_path_prepend(lp, as_path, i);
! 83: bt_debug("Prepending ASN: %10u \n", i);
! 84: }
! 85:
! 86: #define BUFFER_SIZE 120
! 87: byte buf[BUFFER_SIZE] = {};
! 88:
! 89: as_path_format(&empty_as_path, buf, BUFFER_SIZE);
! 90: bt_assert_msg(strcmp(buf, "") == 0, "Buffer(%zu): '%s'", strlen(buf), buf);
! 91:
! 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: #define SMALL_BUFFER_SIZE 25
! 96: byte buf2[SMALL_BUFFER_SIZE] = {};
! 97: 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: rfree(lp);
! 101:
! 102: return 1;
! 103: }
! 104:
! 105: static int
! 106: count_asn_in_array(const u32 *array, u32 asn)
! 107: {
! 108: int counts_of_contains = 0;
! 109: int u;
! 110: for (u = 0; u < AS_PATH_LENGTH; u++)
! 111: if (array[u] == asn)
! 112: counts_of_contains++;
! 113: return counts_of_contains;
! 114: }
! 115:
! 116: static int
! 117: t_path_include(void)
! 118: {
! 119: resource_init();
! 120:
! 121: struct adata empty_as_path = {};
! 122: struct adata *as_path = &empty_as_path;
! 123: struct linpool *lp = lp_new_default(&root_pool);
! 124:
! 125: u32 as_nums[AS_PATH_LENGTH] = {};
! 126: int i;
! 127: for (i = 0; i < AS_PATH_LENGTH; i++)
! 128: {
! 129: u32 val = bt_random();
! 130: as_nums[i] = val;
! 131: as_path = as_path_prepend(lp, as_path, val);
! 132: }
! 133:
! 134: for (i = 0; i < AS_PATH_LENGTH; i++)
! 135: {
! 136: int counts_of_contains = count_asn_in_array(as_nums, as_nums[i]);
! 137: 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]);
! 138:
! 139: bt_assert(as_path_filter(lp, as_path, NULL, as_nums[i], 0) != NULL);
! 140: bt_assert(as_path_filter(lp, as_path, NULL, as_nums[i], 1) != NULL);
! 141: }
! 142:
! 143: for (i = 0; i < 10000; i++)
! 144: {
! 145: u32 test_val = bt_random();
! 146: int counts_of_contains = count_asn_in_array(as_nums, test_val);
! 147: int result = as_path_contains(as_path, test_val, (counts_of_contains == 0 ? 1 : counts_of_contains));
! 148:
! 149: if (counts_of_contains)
! 150: bt_assert_msg(result, "As path should contain %d-times the number %u", counts_of_contains, test_val);
! 151: else
! 152: bt_assert_msg(result == 0, "As path should not contain the number %u", test_val);
! 153: }
! 154:
! 155: rfree(lp);
! 156:
! 157: return 1;
! 158: }
! 159:
! 160: #if 0
! 161: static int
! 162: t_as_path_converting(void)
! 163: {
! 164: resource_init();
! 165:
! 166: struct adata empty_as_path = {};
! 167: struct adata *as_path = &empty_as_path;
! 168: struct linpool *lp = lp_new_default(&root_pool);
! 169: #define AS_PATH_LENGTH_FOR_CONVERTING_TEST 10
! 170:
! 171: int i;
! 172: for (i = 0; i < AS_PATH_LENGTH_FOR_CONVERTING_TEST; i++)
! 173: as_path = as_path_prepend(lp, as_path, i);
! 174:
! 175: bt_debug("data length: %u \n", as_path->length);
! 176:
! 177: byte buffer[100] = {};
! 178: int used_size = as_path_convert_to_new(as_path, buffer, AS_PATH_LENGTH_FOR_CONVERTING_TEST-1);
! 179: bt_debug("as_path_convert_to_new: len %d \n%s\n", used_size, buffer);
! 180: for (i = 0; i < used_size; i++)
! 181: {
! 182: bt_debug("\\03%d", buffer[i]);
! 183: }
! 184: bt_debug("\n");
! 185: bt_assert(memcmp(buffer,
! 186: "\032\039\030\030\030\030\030\030\030\039\030\030\030\030\030\030\030\038\030\030\030\030\030\030"
! 187: "\030\037\030\030\030\030\030\030\030\036\030\030\030\030",
! 188: 38));
! 189:
! 190: bzero(buffer, sizeof(buffer));
! 191: int new_used;
! 192: used_size = as_path_convert_to_old(as_path, buffer, &new_used);
! 193: bt_debug("as_path_convert_to_old: len %d, new_used: %d \n", used_size, new_used);
! 194: for (i = 0; i < used_size; i++)
! 195: {
! 196: bt_debug("\\03%d", buffer[i]);
! 197: }
! 198: bt_debug("\n");
! 199: bt_assert(memcmp(buffer,
! 200: "\032\0310\030\039\030\038\030\037\030\036\030\035\030\034\030\033\030\032\030\031\030\030",
! 201: 22));
! 202:
! 203: return 1;
! 204: }
! 205: #endif
! 206:
! 207: int
! 208: main(int argc, char *argv[])
! 209: {
! 210: bt_init(argc, argv);
! 211:
! 212: bt_test_suite(t_as_path_match, "Testing AS path matching and some a-path utilities.");
! 213: bt_test_suite(t_path_format, "Testing formating as path into byte buffer");
! 214: bt_test_suite(t_path_include, "Testing including a AS number in AS path");
! 215: // bt_test_suite(t_as_path_converting, "Testing as_path_convert_to_*() output constancy");
! 216:
! 217: return bt_exit_value();
! 218: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>