diff options
author | Sander Vrijders <[email protected]> | 2017-01-21 17:16:58 +0000 |
---|---|---|
committer | Sander Vrijders <[email protected]> | 2017-01-21 17:16:58 +0000 |
commit | 317821ca093f7c22e0bf1a9bb15e17c3c501e498 (patch) | |
tree | 819ed39b4cb7b1dd2fb82f62f516bb9fd24e8432 /src/lib/tests/rib_test.c | |
parent | a5b135e9f409d59668c52be9fac5d94dae8ebe94 (diff) | |
parent | 9c92dd66d5e7fab3a3e243abbad9a20b29891fee (diff) | |
download | ouroboros-317821ca093f7c22e0bf1a9bb15e17c3c501e498.tar.gz ouroboros-317821ca093f7c22e0bf1a9bb15e17c3c501e498.zip |
Merged in dstaesse/ouroboros/be-rib (pull request #350)
Be rib
Diffstat (limited to 'src/lib/tests/rib_test.c')
-rw-r--r-- | src/lib/tests/rib_test.c | 222 |
1 files changed, 222 insertions, 0 deletions
diff --git a/src/lib/tests/rib_test.c b/src/lib/tests/rib_test.c new file mode 100644 index 00000000..37503941 --- /dev/null +++ b/src/lib/tests/rib_test.c @@ -0,0 +1,222 @@ +/* + * Ouroboros - Copyright (C) 2016 - 2017 + * + * Test of the RIB + * + * Dimitri Staessens <[email protected]> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#include <ouroboros/config.h> +#include <ouroboros/time_utils.h> +#include <ouroboros/rib.h> +#include <ouroboros/rqueue.h> +#include <ouroboros/errno.h> + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +int rib_test(int argc, + char ** argv) +{ + uint64_t * address; + + size_t addr_size = 8; + size_t addr_chk; + + char * addr_name; + + ro_set_t * set; + rqueue_t * rq; + + int ret; + + char tmp[RIB_MAX_PATH_LEN]; + + struct timespec t = {0, 100 * BILLION}; + + (void) argc; + (void) argv; + + address = malloc(sizeof(*address)); + if (address == NULL) + return -ENOMEM; + + if (rib_init()) { + printf("Failed to initialize rib.\n"); + return -1; + } + + rib_fini(); + + if (rib_init()) { + printf("Failed to re-initialize rib.\n"); + return -1; + } + + if (rib_add(RIB_ROOT, "static_info")) { + printf("Failed to add element to rib.\n"); + rib_fini(); + return -1; + } + + if (!rib_has("/static_info")) { + printf("Failed to find added element.\n"); + rib_fini(); + return -1; + } + + if (rib_add(RIB_ROOT, "dynamic_info")) { + printf("Failed to add element to rib.\n"); + rib_fini(); + return -1; + } + + if (rib_add("/static_info", "addr_size")) { + printf("Failed to add sub-element to rib.\n"); + rib_fini(); + return -1; + } + + if (rib_write("/static_info/addr_size", + &addr_size, sizeof(addr_size))) { + printf("Failed to add sub-element to rib.\n"); + rib_fini(); + return -1; + } + + if (rib_add("/static_info", "addresses")) { + printf("Failed to add sub-element to rib.\n"); + rib_fini(); + return -1; + } + + if (!rib_has("/static_info/addr_size")) { + printf("Failed to find added subelement.\n"); + rib_fini(); + return -1; + } + + if (rib_read("/static_info/addr_size", + &addr_chk, sizeof(addr_chk)) + != sizeof(addr_chk)) { + printf("Failed to read added element.\n"); + rib_fini(); + return -1; + } + + if (addr_chk != addr_size) { + printf("Failed to verify added element contents.\n"); + rib_fini(); + return -1; + } + + addr_size = 16; + + if (rib_write("/static_info/addr_size", + &addr_size, sizeof(addr_size))) { + printf("Failed to write into added element.\n"); + rib_fini(); + return -1; + } + + if (rib_read("/static_info/addr_size", + &addr_chk, sizeof(addr_chk)) + != sizeof(addr_chk)) { + printf("Failed to verify added element update size.\n"); + rib_fini(); + return -1; + } + + if (addr_chk != addr_size) { + printf("Failed to verify added element update size.\n"); + rib_fini(); + return -1; + } + + addr_name = rib_name_gen(address, sizeof(*address)); + if (addr_name == NULL) { + printf("Failed to create a name.\n"); + rib_fini(); + return -1; + } + + strcpy(tmp, "/dynamic_info"); + + if (rib_add(tmp, addr_name)) { + free(addr_name); + printf("Failed to add address.\n"); + rib_fini(); + return -1; + } + + rib_path_append(tmp, addr_name); + + if (rib_put(tmp, address, sizeof(*address))) { + free(addr_name); + printf("Failed to add address.\n"); + rib_fini(); + return -1; + } + + free(addr_name); + + set = ro_set_create(); + if (set == NULL) { + printf("Failed to create ro_set.\n"); + rib_fini(); + return -1; + } + + rq = rqueue_create(); + if (rq == NULL) { + printf("Failed to create rqueue.\n"); + ro_set_destroy(set); + rib_fini(); + return -1; + } + + if (ro_set_add(set, "/static_info", RO_ALL_OPS)) { + printf("Failed to add to rqueue.\n"); + ro_set_destroy(set); + rqueue_destroy(rq); + rib_fini(); + return -1; + } + + ret = rib_event_wait(set, rq, &t); + if (ret != -ETIMEDOUT) { + printf("Wait failed to timeout: %d.\n", ret); + ro_set_destroy(set); + rqueue_destroy(rq); + rib_fini(); + return -1; + } + + if (rib_del("/static_info")) { + printf("Failed to delete rib subtree.\n"); + rib_fini(); + return -1; + } + + ro_set_destroy(set); + + rqueue_destroy(rq); + + rib_fini(); + + return 0; +} |