]> git.proxmox.com Git - mirror_frr.git/commitdiff
tests: ospf6d: basic LSDB tests
authorDavid Lamparter <equinox@opensourcerouting.org>
Sat, 8 Jul 2017 13:30:34 +0000 (15:30 +0200)
committerDavid Lamparter <equinox@opensourcerouting.org>
Tue, 11 Jul 2017 13:04:12 +0000 (15:04 +0200)
Needed these while rewriting LSDB iteration.

NB: this commit fails because of a bug in ospf_lsdb_get_next, which will
SEGV when the LSDB is actually empty.  Whooo...  (this is fixed in the
following commits.)

Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
tests/.gitignore
tests/Makefile.am
tests/ospf6d/test_lsdb.c [new file with mode: 0644]
tests/ospf6d/test_lsdb.in [new file with mode: 0644]
tests/ospf6d/test_lsdb.py [new file with mode: 0644]
tests/ospf6d/test_lsdb.refout [new file with mode: 0644]

index 5279016b92126e46f7fd2264e54a8ad31aa15b4a..6d54ae155b43132088bc0e1975ebac0275712b44 100644 (file)
@@ -44,3 +44,4 @@ __pycache__
 /lib/test_timer_correctness
 /lib/test_timer_performance
 /lib/test_ttable
+/ospf6d/test_lsdb
index 559d76970269e75bc32523b6f8a96f23d1fc993b..c10c3def68d00eb51d6fec72d69692f336aa60fa 100644 (file)
@@ -24,6 +24,14 @@ else
 TESTS_BGPD =
 endif
 
+if OSPF6D
+TESTS_OSPF6D = \
+       ospf6d/test_lsdb \
+       # end
+else
+TESTS_OSPF6D =
+endif
+
 if ENABLE_BGP_VNC
 BGP_VNC_RFP_LIB=@top_builddir@/$(LIBRFP)/librfp.a
 else
@@ -31,6 +39,7 @@ BGP_VNC_RFP_LIB =
 endif
 
 lib/cli/test_cli.o: lib/cli/test_cli_clippy.c
+ospf6d/test_lsdb.o: ospf6d/test_lsdb_clippy.c
 
 check_PROGRAMS = \
        lib/test_buffer \
@@ -51,7 +60,9 @@ check_PROGRAMS = \
        lib/test_ttable \
        lib/cli/test_cli \
        lib/cli/test_commands \
-       $(TESTS_BGPD)
+       $(TESTS_BGPD) \
+       $(TESTS_OSPF6D) \
+       # end
 
 ../vtysh/vtysh_cmd.c:
        $(MAKE) -C ../vtysh vtysh_cmd.c
@@ -100,8 +111,11 @@ bgpd_test_ecommunity_SOURCES = bgpd/test_ecommunity.c
 bgpd_test_mp_attr_SOURCES = bgpd/test_mp_attr.c
 bgpd_test_mpath_SOURCES = bgpd/test_mpath.c
 
+ospf6d_test_lsdb_SOURCES = ospf6d/test_lsdb.c lib/cli/common_cli.c
+
 ALL_TESTS_LDADD = ../lib/libfrr.la @LIBCAP@
 BGP_TEST_LDADD = ../bgpd/libbgp.a $(BGP_VNC_RFP_LIB) $(ALL_TESTS_LDADD) -lm
+OSPF6_TEST_LDADD = ../ospf6d/libospf6.a $(ALL_TESTS_LDADD)
 
 lib_test_buffer_LDADD = $(ALL_TESTS_LDADD)
 lib_test_checksum_LDADD = $(ALL_TESTS_LDADD)
@@ -126,6 +140,7 @@ bgpd_test_capability_LDADD = $(BGP_TEST_LDADD)
 bgpd_test_ecommunity_LDADD = $(BGP_TEST_LDADD)
 bgpd_test_mp_attr_LDADD = $(BGP_TEST_LDADD)
 bgpd_test_mpath_LDADD = $(BGP_TEST_LDADD)
+ospf6d_test_lsdb_LDADD = $(OSPF6_TEST_LDADD)
 
 EXTRA_DIST = \
     runtests.py \
@@ -148,7 +163,11 @@ EXTRA_DIST = \
     lib/test_stream.refout \
     lib/test_table.py \
     lib/test_timer_correctness.py \
-    lib/test_ttable.py
+    lib/test_ttable.py \
+    ospf6d/test_lsdb.py \
+    ospf6d/test_lsdb.in \
+    ospf6d/test_lsdb.refout \
+    # end
 
 .PHONY: tests.xml
 tests.xml: $(check_PROGRAMS)
diff --git a/tests/ospf6d/test_lsdb.c b/tests/ospf6d/test_lsdb.c
new file mode 100644 (file)
index 0000000..29d7b51
--- /dev/null
@@ -0,0 +1,253 @@
+/*
+ * CLI/command dummy handling tester
+ *
+ * Copyright (C) 2015 by David Lamparter,
+ *                   for Open Source Routing / NetDEF, Inc.
+ *
+ * Quagga is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2, or (at your option) any
+ * later version.
+ *
+ * Quagga 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; see the file COPYING; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <zebra.h>
+
+#include "prefix.h"
+#include "vector.h"
+#include "vty.h"
+
+#include "ospf6d/ospf6_lsa.h"
+#include "ospf6d/ospf6_lsdb.h"
+
+#include "tests/lib/cli/common_cli.h"
+#include "tests/ospf6d/test_lsdb_clippy.c"
+
+static struct ospf6_lsdb *lsdb;
+
+static struct ospf6_lsa **lsas = NULL;
+static size_t lsa_count = 0;
+
+static void lsa_check_resize(size_t len)
+{
+       if (lsa_count >= len)
+               return;
+       lsas = realloc(lsas, len * sizeof(lsas[0]));
+       memset(lsas + lsa_count, 0, sizeof(lsas[0]) * (len - lsa_count));
+
+       lsa_count = len;
+}
+
+DEFPY(lsa_set, lsa_set_cmd,
+      "lsa set (0-999999)$idx {type (0-65535)|id A.B.C.D|adv A.B.C.D}",
+      "LSA\n"
+      "set\n"
+      "LSA index in array\n"
+      "OSPF6 type code\n"
+      "OSPF6 type code\n"
+      "LS-ID\n"
+      "LS-ID\n"
+      "Advertising router\n"
+      "Advertising router\n")
+{
+       struct ospf6_lsa_header hdr;
+       memset(&hdr, 0, sizeof(hdr));
+       hdr.type = htons(type);
+       hdr.id = id.s_addr;
+       hdr.adv_router = adv.s_addr;
+
+       lsa_check_resize(idx + 1);
+       if (lsas[idx])
+               ospf6_lsa_unlock(lsas[idx]);
+       lsas[idx] = ospf6_lsa_create_headeronly(&hdr);
+       ospf6_lsa_lock(lsas[idx]);
+       return CMD_SUCCESS;
+}
+
+DEFPY(lsa_drop, lsa_drop_cmd,
+      "lsa drop (0-999999)$idx",
+      "LSA\n"
+      "drop reference\n"
+      "LSA index in array\n")
+{
+       if ((size_t)idx >= lsa_count)
+               return CMD_SUCCESS;
+       if (lsas[idx]->lock != 1)
+               vty_outln(vty, "refcount at %u", lsas[idx]->lock);
+       ospf6_lsa_unlock(lsas[idx]);
+       lsas[idx] = NULL;
+       return CMD_SUCCESS;
+}
+
+
+DEFPY(lsdb_add, lsdb_add_cmd,
+      "lsdb add (0-999999)$idx",
+      "LSDB\n"
+      "insert LSA into LSDB\n"
+      "LSA index in array\n")
+{
+       ospf6_lsdb_add(lsas[idx], lsdb);
+       return CMD_SUCCESS;
+}
+
+DEFPY(lsdb_remove, lsdb_remove_cmd,
+      "lsdb remove (0-999999)$idx",
+      "LSDB\n"
+      "remove LSA from LSDB\n"
+      "LSA index in array\n")
+{
+       ospf6_lsdb_remove(lsas[idx], lsdb);
+       return CMD_SUCCESS;
+}
+
+static void lsa_show_oneline(struct vty *vty, struct ospf6_lsa *lsa)
+{
+       char adv_router[64], id[64];
+
+       if (!lsa) {
+               vty_outln(vty, "lsa = NULL");
+               return;
+       }
+       inet_ntop(AF_INET, &lsa->header->id,
+                       id, sizeof (id));
+       inet_ntop(AF_INET, &lsa->header->adv_router,
+                       adv_router, sizeof (adv_router));
+       vty_outln(vty, "type %u adv %s id %s",
+                       ntohs(lsa->header->type), adv_router, id);
+}
+
+DEFPY(lsdb_walk, lsdb_walk_cmd,
+      "lsdb walk",
+      "LSDB\n"
+      "walk entries\n")
+{
+       struct ospf6_lsa *lsa;
+       unsigned cnt = 0;
+       for (ALL_LSDB(lsdb, lsa)) {
+               lsa_show_oneline(vty, lsa);
+               cnt++;
+       }
+       vty_outln(vty, "%u entries.", cnt);
+       return CMD_SUCCESS;
+}
+
+DEFPY(lsdb_walk_type, lsdb_walk_type_cmd,
+      "lsdb walk type (0-65535)",
+      "LSDB\n"
+      "walk entries\n"
+      "entry type\n"
+      "entry type\n")
+{
+       struct ospf6_lsa *lsa;
+       unsigned cnt = 0;
+       type = htons(type);
+       for (ALL_LSDB_TYPED(lsdb, type, lsa)) {
+               lsa_show_oneline(vty, lsa);
+               cnt++;
+       }
+       vty_outln(vty, "%u entries.", cnt);
+       return CMD_SUCCESS;
+}
+
+DEFPY(lsdb_walk_type_adv, lsdb_walk_type_adv_cmd,
+      "lsdb walk type (0-65535) adv A.B.C.D",
+      "LSDB\n"
+      "walk entries\n"
+      "entry type\n"
+      "entry type\n"
+      "advertising router ID\n"
+      "advertising router ID\n")
+{
+       struct ospf6_lsa *lsa;
+       unsigned cnt = 0;
+       type = htons(type);
+       for (ALL_LSDB_TYPED_ADVRTR(lsdb, type, adv.s_addr, lsa)) {
+               lsa_show_oneline(vty, lsa);
+               cnt++;
+       }
+       vty_outln(vty, "%u entries.", cnt);
+       return CMD_SUCCESS;
+}
+
+DEFPY(lsdb_get, lsdb_get_cmd,
+      "lsdb <get-next|get> type (0-65535) adv A.B.C.D id A.B.C.D",
+      "LSDB\n"
+      "get entry's successor\n"
+      "entry type\n"
+      "entry type\n"
+      "advertising router ID\n"
+      "advertising router ID\n"
+      "LS-ID\n"
+      "LS-ID\n")
+{
+       struct ospf6_lsa *lsa;
+       type = htons(type);
+       if (!strcmp(argv[1]->text, "get-next"))
+               lsa = ospf6_lsdb_lookup_next(type, id.s_addr, adv.s_addr, lsdb);
+       else
+               lsa = ospf6_lsdb_lookup(type, id.s_addr, adv.s_addr, lsdb);
+       lsa_show_oneline(vty, lsa);
+       return CMD_SUCCESS;
+}
+
+DEFPY(lsa_refcounts, lsa_refcounts_cmd,
+      "lsa refcounts",
+      "LSA\n"
+      "show reference counts\n")
+{
+       for (size_t i = 0; i < lsa_count; i++)
+               if (lsas[i])
+                       vty_outln(vty, "[%zu] %u", i, lsas[i]->lock);
+       return CMD_SUCCESS;
+}
+
+DEFPY(lsdb_create, lsdb_create_cmd,
+      "lsdb create",
+      "LSDB\n"
+      "create LSDB\n")
+{
+       if (lsdb)
+               ospf6_lsdb_delete(lsdb);
+       lsdb = ospf6_lsdb_create(NULL);
+       return CMD_SUCCESS;
+}
+
+DEFPY(lsdb_delete, lsdb_delete_cmd,
+      "lsdb delete",
+      "LSDB\n"
+      "delete LSDB\n")
+{
+       ospf6_lsdb_delete(lsdb);
+       lsdb = NULL;
+       return CMD_SUCCESS;
+}
+
+
+struct zebra_privs_t ospf6d_privs;
+
+void test_init(int argc, char **argv)
+{
+       ospf6_lsa_init();
+
+       install_element(ENABLE_NODE, &lsa_set_cmd);
+       install_element(ENABLE_NODE, &lsa_refcounts_cmd);
+       install_element(ENABLE_NODE, &lsa_drop_cmd);
+
+       install_element(ENABLE_NODE, &lsdb_create_cmd);
+       install_element(ENABLE_NODE, &lsdb_delete_cmd);
+
+       install_element(ENABLE_NODE, &lsdb_add_cmd);
+       install_element(ENABLE_NODE, &lsdb_remove_cmd);
+       install_element(ENABLE_NODE, &lsdb_walk_cmd);
+       install_element(ENABLE_NODE, &lsdb_walk_type_cmd);
+       install_element(ENABLE_NODE, &lsdb_walk_type_adv_cmd);
+       install_element(ENABLE_NODE, &lsdb_get_cmd);
+}
diff --git a/tests/ospf6d/test_lsdb.in b/tests/ospf6d/test_lsdb.in
new file mode 100644 (file)
index 0000000..0475f3d
--- /dev/null
@@ -0,0 +1,72 @@
+lsa set 0 type 1 adv 1.2.3.4 id 0.0.0.1
+lsa set 1 type 1 adv 1.2.3.4 id 0.0.0.2
+lsa set 2 type 2 adv 1.2.3.4 id 0.0.0.3
+lsa set 3 type 2 adv 128.2.3.4 id 0.0.0.4
+lsa set 4 type 2 adv 128.2.3.4 id 0.0.0.5
+lsa set 5 type 3 adv 0.0.0.1 id 0.0.0.6
+lsa refcounts
+
+lsdb create
+
+lsdb walk
+lsdb walk type 1
+lsdb walk type 2
+lsdb get type 1 adv 1.2.3.4 id 0.0.0.2
+lsdb get-next type 1 adv 1.2.3.4 id 0.0.0.2
+lsa refcounts
+
+lsdb add 0
+lsdb add 1
+lsa refcounts
+
+lsdb walk
+lsdb walk type 1
+lsdb walk type 2
+lsdb get type 1 adv 1.2.3.4 id 0.0.0.2
+lsdb get-next type 1 adv 1.2.3.4 id 0.0.0.2
+lsa refcounts
+
+lsdb remove 0
+lsdb add 2
+lsdb add 3
+lsdb add 4
+lsa refcounts
+
+lsdb walk
+lsdb walk type 1
+lsdb walk type 2
+lsdb get type 1 adv 1.2.3.4 id 0.0.0.2
+lsdb get-next type 1 adv 1.2.3.4 id 0.0.0.2
+lsa refcounts
+
+lsdb add 5
+lsa refcounts
+
+lsdb walk
+lsdb walk type 1
+lsdb walk type 2
+lsdb get type 1 adv 1.2.3.4 id 0.0.0.2
+lsdb get-next type 1 adv 1.2.3.4 id 0.0.0.2
+lsa refcounts
+
+lsdb remove 1
+lsdb remove 5
+lsa refcounts
+
+lsdb walk
+lsdb walk type 1
+lsdb walk type 2
+lsdb get type 1 adv 1.2.3.4 id 0.0.0.2
+lsdb get-next type 1 adv 1.2.3.4 id 0.0.0.2
+lsa refcounts
+
+lsdb delete
+
+lsa refcounts
+lsa drop 0
+lsa drop 1
+lsa drop 2
+lsa drop 3
+lsa drop 4
+lsa drop 5
+
diff --git a/tests/ospf6d/test_lsdb.py b/tests/ospf6d/test_lsdb.py
new file mode 100644 (file)
index 0000000..6a94395
--- /dev/null
@@ -0,0 +1,4 @@
+import frrtest
+
+class TestLSDB(frrtest.TestRefOut):
+    program = './test_lsdb'
diff --git a/tests/ospf6d/test_lsdb.refout b/tests/ospf6d/test_lsdb.refout
new file mode 100644 (file)
index 0000000..8b17652
--- /dev/null
@@ -0,0 +1,192 @@
+test# lsa set 0 type 1 adv 1.2.3.4 id 0.0.0.1\r
+test# lsa set 1 type 1 adv 1.2.3.4 id 0.0.0.2\r
+test# lsa set 2 type 2 adv 1.2.3.4 id 0.0.0.3\r
+test# lsa set 3 type 2 adv 128.2.3.4 id 0.0.0.4\r
+test# lsa set 4 type 2 adv 128.2.3.4 id 0.0.0.5\r
+test# lsa set 5 type 3 adv 0.0.0.1 id 0.0.0.6\r
+test# lsa refcounts\r
+[0] 1\r
+[1] 1\r
+[2] 1\r
+[3] 1\r
+[4] 1\r
+[5] 1\r
+test# \r
+test# lsdb create\r
+test# \r
+test# lsdb walk\r
+0 entries.\r
+test# lsdb walk type 1\r
+0 entries.\r
+test# lsdb walk type 2\r
+0 entries.\r
+test# lsdb get type 1 adv 1.2.3.4 id 0.0.0.2\r
+lsa = NULL\r
+test# lsdb get-next type 1 adv 1.2.3.4 id 0.0.0.2\r
+lsa = NULL\r
+test# lsa refcounts\r
+[0] 1\r
+[1] 1\r
+[2] 1\r
+[3] 1\r
+[4] 1\r
+[5] 1\r
+test# \r
+test# lsdb add 0\r
+test# lsdb add 1\r
+test# lsa refcounts\r
+[0] 2\r
+[1] 2\r
+[2] 1\r
+[3] 1\r
+[4] 1\r
+[5] 1\r
+test# \r
+test# lsdb walk\r
+type 1 adv 1.2.3.4 id 0.0.0.1\r
+type 1 adv 1.2.3.4 id 0.0.0.2\r
+2 entries.\r
+test# lsdb walk type 1\r
+type 1 adv 1.2.3.4 id 0.0.0.1\r
+type 1 adv 1.2.3.4 id 0.0.0.2\r
+2 entries.\r
+test# lsdb walk type 2\r
+0 entries.\r
+test# lsdb get type 1 adv 1.2.3.4 id 0.0.0.2\r
+type 1 adv 1.2.3.4 id 0.0.0.2\r
+test# lsdb get-next type 1 adv 1.2.3.4 id 0.0.0.2\r
+lsa = NULL\r
+test# lsa refcounts\r
+[0] 2\r
+[1] 2\r
+[2] 1\r
+[3] 1\r
+[4] 1\r
+[5] 1\r
+test# \r
+test# lsdb remove 0\r
+test# lsdb add 2\r
+test# lsdb add 3\r
+test# lsdb add 4\r
+test# lsa refcounts\r
+[0] 1\r
+[1] 2\r
+[2] 2\r
+[3] 2\r
+[4] 2\r
+[5] 1\r
+test# \r
+test# lsdb walk\r
+type 1 adv 1.2.3.4 id 0.0.0.2\r
+type 2 adv 1.2.3.4 id 0.0.0.3\r
+type 2 adv 128.2.3.4 id 0.0.0.4\r
+type 2 adv 128.2.3.4 id 0.0.0.5\r
+4 entries.\r
+test# lsdb walk type 1\r
+type 1 adv 1.2.3.4 id 0.0.0.2\r
+1 entries.\r
+test# lsdb walk type 2\r
+type 2 adv 1.2.3.4 id 0.0.0.3\r
+type 2 adv 128.2.3.4 id 0.0.0.4\r
+type 2 adv 128.2.3.4 id 0.0.0.5\r
+3 entries.\r
+test# lsdb get type 1 adv 1.2.3.4 id 0.0.0.2\r
+type 1 adv 1.2.3.4 id 0.0.0.2\r
+test# lsdb get-next type 1 adv 1.2.3.4 id 0.0.0.2\r
+type 2 adv 1.2.3.4 id 0.0.0.3\r
+test# lsa refcounts\r
+[0] 1\r
+[1] 2\r
+[2] 2\r
+[3] 2\r
+[4] 2\r
+[5] 1\r
+test# \r
+test# lsdb add 5\r
+test# lsa refcounts\r
+[0] 1\r
+[1] 2\r
+[2] 2\r
+[3] 2\r
+[4] 2\r
+[5] 2\r
+test# \r
+test# lsdb walk\r
+type 1 adv 1.2.3.4 id 0.0.0.2\r
+type 2 adv 1.2.3.4 id 0.0.0.3\r
+type 2 adv 128.2.3.4 id 0.0.0.4\r
+type 2 adv 128.2.3.4 id 0.0.0.5\r
+type 3 adv 0.0.0.1 id 0.0.0.6\r
+5 entries.\r
+test# lsdb walk type 1\r
+type 1 adv 1.2.3.4 id 0.0.0.2\r
+1 entries.\r
+test# lsdb walk type 2\r
+type 2 adv 1.2.3.4 id 0.0.0.3\r
+type 2 adv 128.2.3.4 id 0.0.0.4\r
+type 2 adv 128.2.3.4 id 0.0.0.5\r
+3 entries.\r
+test# lsdb get type 1 adv 1.2.3.4 id 0.0.0.2\r
+type 1 adv 1.2.3.4 id 0.0.0.2\r
+test# lsdb get-next type 1 adv 1.2.3.4 id 0.0.0.2\r
+type 2 adv 1.2.3.4 id 0.0.0.3\r
+test# lsa refcounts\r
+[0] 1\r
+[1] 2\r
+[2] 2\r
+[3] 2\r
+[4] 2\r
+[5] 2\r
+test# \r
+test# lsdb remove 1\r
+test# lsdb remove 5\r
+test# lsa refcounts\r
+[0] 1\r
+[1] 1\r
+[2] 2\r
+[3] 2\r
+[4] 2\r
+[5] 1\r
+test# \r
+test# lsdb walk\r
+type 2 adv 1.2.3.4 id 0.0.0.3\r
+type 2 adv 128.2.3.4 id 0.0.0.4\r
+type 2 adv 128.2.3.4 id 0.0.0.5\r
+3 entries.\r
+test# lsdb walk type 1\r
+0 entries.\r
+test# lsdb walk type 2\r
+type 2 adv 1.2.3.4 id 0.0.0.3\r
+type 2 adv 128.2.3.4 id 0.0.0.4\r
+type 2 adv 128.2.3.4 id 0.0.0.5\r
+3 entries.\r
+test# lsdb get type 1 adv 1.2.3.4 id 0.0.0.2\r
+lsa = NULL\r
+test# lsdb get-next type 1 adv 1.2.3.4 id 0.0.0.2\r
+type 2 adv 1.2.3.4 id 0.0.0.3\r
+test# lsa refcounts\r
+[0] 1\r
+[1] 1\r
+[2] 2\r
+[3] 2\r
+[4] 2\r
+[5] 1\r
+test# \r
+test# lsdb delete\r
+test# \r
+test# lsa refcounts\r
+[0] 1\r
+[1] 1\r
+[2] 1\r
+[3] 1\r
+[4] 1\r
+[5] 1\r
+test# lsa drop 0\r
+test# lsa drop 1\r
+test# lsa drop 2\r
+test# lsa drop 3\r
+test# lsa drop 4\r
+test# lsa drop 5\r
+test# \r
+test# 
+end.