void wht_unit_test() { struct wht wht, wht2; uint8_t buffer1[8] = { 1, 1, 1, 1, 1, 1, 1, 1 }; uint8_t buffer2[8] = { 1, 0, 1, 0, 1, 0, 1, 0 }; uint8_t buffer3[8] = { 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, // 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf }; uint8_t buffer4[4] = { 255, 254, 253, 252 }; zfile output; output = zattach(stdout, "w"); if (output == NULL) { fprintf(stderr, "error: could not initialize (possibly compressed) stdout for writing\n"); } wht_init(&wht); wht_update(&wht, buffer1, sizeof(buffer1), 1); wht_printf_scaled(&wht, output, sizeof(buffer1)); wht_init(&wht); wht_update(&wht, buffer2, sizeof(buffer2), 1); wht_printf_scaled(&wht, output, sizeof(buffer2)); wht_init(&wht); wht_update(&wht, buffer3, sizeof(buffer3), 1); wht_printf_scaled(&wht, output, sizeof(buffer3)); wht_init(&wht); wht_init(&wht2); wht_update(&wht, buffer4, 1, 1); /* note: only reading first byte */ wht_update(&wht, buffer4, 1, 1); /* note: only reading first byte */ wht_update(&wht, buffer4, 1, 1); /* note: only reading first byte */ wht_printf_scaled_bidir(&wht, 3, &wht2, 0, output); }
/** * \fn int radix_trie_unit_test() * \params none * \return 0 on success * \return 1 on failure */ int radix_trie_unit_test () { struct radix_trie rt, rt2; enum status err; struct in_addr a[10]; unsigned int af[10]; unsigned int i; unsigned int test_failed = 0; unsigned int flag; zfile output; output = zattach(stdout, "w"); if (output == NULL) { fprintf(stderr, "%s: error: could not initialize (possibly compressed) stdout for writing\n", __FUNCTION__); } for (i=0; i<32; i++) { flag = index_to_flag(i); printf("index: %u\tflag: %x\tindex: %u\n", i, flag, flag_to_index(flag)); } err = radix_trie_init(&rt); if (err != ok) { fprintf(stderr, "%s: error: could not initialize radix_trie\n", __FUNCTION__); } a[0].s_addr = htonl(0xcafebabe); af[0] = 1; a[1].s_addr = htonl(0xcafedada); af[1] = 2; a[2].s_addr = htonl(0xbaddecaf); af[2] = 4; a[3].s_addr = htonl(0x01234567); af[3] = 8; a[4].s_addr = htonl(0xffeeddcc); af[4] = 16; a[5].s_addr = htonl(0x0a9b8c7d); af[5] = 32; a[6].s_addr = htonl(0xfedcba98); af[6] = 64; a[7].s_addr = htonl(0x76543210); af[7] = 128; a[8].s_addr = htonl(0xa1b2c3d4); af[8] = 256; printf("testing add\n"); flag = 1; for (i=0; i<3; i++) { if (radix_trie_add_subnet(&rt, a[i], 32, af[i]) != ok) { zprintf(output, "error: could not add subnet %s\n", inet_ntoa(a[i])); test_failed = 1; } } for (i=6; i<9; i++) { if (radix_trie_add_subnet(&rt, a[i], 16, af[i]) != ok) { zprintf(output, "error: could not add subnet %s\n", inet_ntoa(a[i])); test_failed = 1; } } printf("testing lookup (expecting success)\n"); for (i=0; i<3; i++) { if (radix_trie_lookup_addr(&rt, a[i]) != af[i]) { zprintf(output, "error: could not lookup subnet %s\n", inet_ntoa(a[i])); test_failed = 1; } } for (i=6; i<9; i++) { if (radix_trie_lookup_addr(&rt, a[i]) != af[i]) { zprintf(output, "error: could not lookup subnet %s\n", inet_ntoa(a[i])); test_failed = 1; } } printf("testing lookup (expecting failure)\n"); for (i=3; i<6; i++) { if (radix_trie_lookup_addr(&rt, a[i]) != 0) { zprintf(output, "error: false positive lookup subnet %s\n", inet_ntoa(a[i])); test_failed = 1; } } printf("testing 14-bit add\n"); for (i=0; i<3; i++) { if (radix_trie_add_subnet(&rt, a[i], 14, 0x100) != ok) { zprintf(output, "error: could not add subnet %s\n", inet_ntoa(a[i])); test_failed = 1; } } printf("testing 14-bit lookup (expecting success)\n"); for (i=0; i<3; i++) { unsigned int f = radix_trie_lookup_addr(&rt, a[i]); if (f != (af[i] | 0x100)) { zprintf(output, "error: could not lookup address %s (%x), got %x instead\n", inet_ntoa(a[i]), htonl(a[i].s_addr), f); test_failed = 1; } } printf("testing 15-bit add\n"); for (i=0; i<3; i++) { if (radix_trie_add_subnet(&rt, a[i], 15, 0x1000) != ok) { zprintf(output, "error: could not add subnet %s\n", inet_ntoa(a[i])); test_failed = 1; } } printf("testing 15-bit lookup (expecting success)\n"); for (i=0; i<3; i++) { unsigned int f = radix_trie_lookup_addr(&rt, a[i]); if (f != (af[i] | 0x1000 | 0x100)) { zprintf(output, "error: could not lookup address %s (%x), got %x but expected %x\n", inet_ntoa(a[i]), htonl(a[i].s_addr), f, (af[i] | 0x1000 | 0x100)); test_failed = 1; } } printf("testing lookup (expecting failure)\n"); for (i=3; i<6; i++) { if (radix_trie_lookup_addr(&rt, a[i]) != 0) { zprintf(output, "error: false positive lookup address %s\n", inet_ntoa(a[i])); test_failed = 1; } } if (test_failed) { printf("FAILURE; at least one test failed\n"); } else { printf("all tests passed\n"); } printf("-----------------------------------\n"); radix_trie_print(&rt); printf("testing high level interface\n"); err = radix_trie_init(&rt2); if (err != ok) { fprintf(stderr, "error: could not initialize radix_trie\n"); } attr_flags internal_attr, c2_attr, watchlist_attr, attr; struct in_addr addr; internal_attr = radix_trie_add_attr_label(&rt2, "internal"); printf("attr: %x\n", internal_attr); c2_attr = radix_trie_add_attr_label(&rt2, "c2"); watchlist_attr = radix_trie_add_attr_label(&rt2, "watchlist"); addr = hex2addr(0xcafe0000); if (radix_trie_add_subnet(&rt2, addr, 16, internal_attr) != ok) { zprintf(output, "error: could not add subnet %s\n", inet_ntoa(addr)); test_failed = 1; } attr = radix_trie_lookup_addr(&rt2, addr); if ((attr & internal_attr) == 0) { zprintf(output, "error: attribute lookup failed (expected %x, got %x)\n", internal_attr, attr); test_failed = 1; } addr = hex2addr(0xdecaf000); if (radix_trie_add_subnet(&rt2, addr, 20, internal_attr) != ok) { zprintf(output, "error: could not add subnet %s\n", inet_ntoa(addr)); test_failed = 1; } attr = radix_trie_lookup_addr(&rt2, addr); if ((attr & internal_attr) == 0) { zprintf(output, "error: attribute lookup failed (expected %x, got %x)\n", internal_attr, attr); test_failed = 1; } addr = hex2addr(0xdadacafe); if (radix_trie_add_subnet(&rt2, addr, 32, c2_attr) != ok) { zprintf(output, "error: could not add subnet %s\n", inet_ntoa(addr)); test_failed = 1; } attr = radix_trie_lookup_addr(&rt2, addr); if ((attr & c2_attr) == 0) { zprintf(output, "error: attribute lookup failed (expected %x, got %x)\n", c2_attr, attr); test_failed = 1; } addr = hex2addr(0xdadacafe); if (radix_trie_add_subnet(&rt2, addr, 8, watchlist_attr) != ok) { zprintf(output, "error: could not add subnet %s\n", inet_ntoa(addr)); test_failed = 1; } attr = radix_trie_lookup_addr(&rt2, addr); if ((attr & watchlist_attr) == 0) { zprintf(output, "error: attribute lookup failed (expected %x, got %x)\n", watchlist_attr, attr); test_failed = 1; } addr = hex2addr(0xffffffff); if (radix_trie_add_subnet(&rt2, addr, 1, watchlist_attr) != ok) { zprintf(output, "error: could not add subnet %s\n", inet_ntoa(addr)); test_failed = 1; } attr = radix_trie_lookup_addr(&rt2, addr); if ((attr & watchlist_attr) == 0) { zprintf(output, "error: attribute lookup failed (expected %x, got %x)\n", c2_attr, attr); test_failed = 1; } if (test_failed) { printf("FAILURE; at least one test failed\n"); } else { printf("all high level interface tests passed\n"); } printf("-----------------------------------\n"); radix_trie_print(&rt2); printf("-----------------------------------\n"); if (radix_trie_high_level_unit_test() != ok) { test_failed = 1; } return test_failed; /* 0 on success, 1 otherwise */ }
/* * Function to initiate the radix_trie unit tests * returns 0 on success * returns 1 on failure */ static int radix_trie_high_level_unit_test () { struct radix_trie rt; attr_flags flag_internal, flag_malware, flag; char *configfile = "internal.net"; struct in_addr addr; enum status err; unsigned test_failed = 0; zfile output; output = zattach(stdout, "w"); if (output == NULL) { fprintf(stderr, "%s: error: could not initialize (possibly compressed) stdout for writing\n", __FUNCTION__); } /* initialize */ err = radix_trie_init(&rt); if (err != ok) { fprintf(stderr, "%s: error: could not initialize radix_trie\n", __FUNCTION__); } /* create a label */ flag_internal = radix_trie_add_attr_label(&rt, "internal"); if (flag_internal == 0) { fprintf(stderr, "%s: error: count not add label\n", __FUNCTION__); test_failed = 1; return failure; } flag_malware = radix_trie_add_attr_label(&rt, "malware"); if (flag_internal == 0) { fprintf(stderr, "%s: error: count not add label\n", __FUNCTION__); test_failed = 1; return failure; } /* add subnets from file */ err = radix_trie_add_subnets_from_file(&rt, configfile, flag_internal, stderr); if (err != ok) { fprintf(stderr, "%s: error: could not add subnets to radix_trie from file %s\n", __FUNCTION__, configfile); test_failed = 1; } printf("++++++++++++\n"); err = radix_trie_add_subnets_from_file(&rt, configfile, flag_malware, stderr); if (err != ok) { fprintf(stderr, "%s: error: could not add subnets to radix_trie from file %s\n", __FUNCTION__, configfile); test_failed = 1; } /* verify addresses and labels */ addr.s_addr = htonl(0xc0a80101); /* 192.168.1.1 */ flag = radix_trie_lookup_addr(&rt, addr); if ((flag & flag_internal) == 0) { zprintf(output, "error: attribute lookup failed (expected %x, got %x)\n", flag_internal, flag); test_failed = 1; } attr_flags_json_print_labels(&rt, flag, "addr", output); addr.s_addr = htonl(0x08080808); /* not internal */ flag = radix_trie_lookup_addr(&rt, addr); if ((flag & flag_internal) == 1) { zprintf(output, "error: attribute lookup failed (did not expect %x, but got %x)\n", flag_internal, flag); test_failed = 1; } attr_flags_json_print_labels(&rt, flag, "addr", output); printf("\n==================\n"); radix_trie_print(&rt); if (test_failed) { printf("FAILURE; at least one test failed\n"); } else { printf("all tests passed\n"); } return test_failed; /* 0 on success, 1 otherwise */ }