]> git.puffer.fish Git - mirror/frr.git/commitdiff
tests: add unit test for xrefs
authorDavid Lamparter <equinox@diac24.net>
Sat, 18 Jul 2020 09:39:31 +0000 (11:39 +0200)
committerDavid Lamparter <equinox@diac24.net>
Mon, 1 Feb 2021 16:28:11 +0000 (17:28 +0100)
Signed-off-by: David Lamparter <equinox@diac24.net>
tests/.gitignore
tests/lib/test_xref.c [new file with mode: 0644]
tests/lib/test_xref.py [new file with mode: 0644]
tests/subdir.am

index b1b8f92a87f1647c529c522aa0b8e7946d529861..ca20b0ecac2c5f48ae3df51c98f304bbb356fa3b 100644 (file)
@@ -47,6 +47,7 @@
 /lib/test_ttable
 /lib/test_typelist
 /lib/test_versioncmp
+/lib/test_xref
 /lib/test_zlog
 /lib/test_zmq
 /ospf6d/test_lsdb
diff --git a/tests/lib/test_xref.c b/tests/lib/test_xref.c
new file mode 100644 (file)
index 0000000..700950d
--- /dev/null
@@ -0,0 +1,140 @@
+/*
+ * xref tests
+ * Copyright (C) 2020  David Lamparter for NetDEF, Inc.
+ *
+ * This program 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 of the License, or (at your option)
+ * any later version.
+ *
+ * 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; 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 "xref.h"
+#include "log.h"
+
+/*
+ * "lib/test_xref.c" (only 1 directory component included)
+ * "logging call"
+ * 0x00000003 (network byte order - LOG_ERR)
+ * 0x00000000 (network byte order - EC / zero here)
+ *
+ * note there are no '\0' terminators included for the strings
+ *
+ * SHA256
+ * => 71a65ce6e81517f642c8f55fb2af6f181f7df54357913b5b577aa61a663fdd4c
+ *  & 0f -> 0x01      'H'
+ *  & f001 -> 0x07    '7'
+ *  &   3e -> 0x13    'K'
+ *  &   c007 -> 0x12  'J'
+ *  &     f8 -> 0x0b  'B'
+ * etc.
+ * (for reference: base32ch[] = "0123456789ABCDEFGHJKMNPQRSTVWXYZ")
+ *
+ * (bits are consumed starting with the lowest bit, and the first character
+ * only consumes 4 bits and has the 5th bit at 1)
+ */
+
+static const char *expect_uid = "H7KJB-67TBH";
+static bool test_logcall(void)
+{
+       zlog_err("logging call");
+
+       return true;
+}
+
+static void check_xref(const struct xref *xref, bool *found, bool *error)
+{
+       const char *file = xref->file, *p;
+
+       p = strrchr(file, '/');
+       if (p)
+               file = p + 1;
+
+       if (strcmp(file, "test_xref.c"))
+               return;
+       if (xref->type != XREFT_LOGMSG)
+               return;
+       if (strcmp(xref->func, "test_logcall"))
+               return;
+
+       printf("xref: %s:%d %s() type=%d uid=%s\n",
+              xref->file, xref->line, xref->func, xref->type,
+              xref->xrefdata ? xref->xrefdata->uid : "--");
+
+       if (*found) {
+               printf("duplicate xref!\n");
+               *error = true;
+       }
+
+       const struct xref_logmsg *logmsg;
+
+       logmsg = container_of(xref, struct xref_logmsg, xref);
+       if (strcmp(logmsg->fmtstring, "logging call")) {
+               printf("log message mismatch!\n");
+               *error = true;
+       }
+       if (logmsg->priority != LOG_ERR || logmsg->ec != 0) {
+               printf("metadata mismatch!\n");
+               *error = true;
+       }
+
+       *found = true;
+
+       if (!xref->xrefdata) {
+               printf("no unique ID?\n");
+               *error = true;
+               return;
+       }
+
+       if (strcmp(xref->xrefdata->uid, expect_uid)) {
+               printf("unique ID mismatch, expected %s, got %s\n",
+                      expect_uid, xref->xrefdata->uid);
+               *error = true;
+       }
+}
+
+static bool test_lookup(void)
+{
+       struct xref_block *xb;
+       bool found = false, error = false;
+
+       for (xb = xref_blocks; xb; xb = xb->next) {
+               const struct xref * const *xrefp;
+
+               for (xrefp = xb->start; xrefp < xb->stop; xrefp++) {
+                       const struct xref *xref = *xrefp;
+
+                       if (!xref)
+                               continue;
+
+                       check_xref(xref, &found, &error);
+               }
+       }
+       return found && !error;
+}
+
+bool (*tests[])(void) = {
+       test_lookup,
+       test_logcall,
+};
+
+XREF_SETUP()
+
+int main(int argc, char **argv)
+{
+       zlog_aux_init("NONE: ", ZLOG_DISABLED);
+
+       for (unsigned int i = 0; i < array_size(tests); i++)
+               if (!tests[i]())
+                       return 1;
+       return 0;
+}
diff --git a/tests/lib/test_xref.py b/tests/lib/test_xref.py
new file mode 100644 (file)
index 0000000..8c3db3e
--- /dev/null
@@ -0,0 +1,6 @@
+import frrtest
+
+class TestXref(frrtest.TestMultiOut):
+    program = './test_xref'
+
+TestXref.exit_cleanly()
index b2ce54ba662ad8ecea8d1b501a8927b94e8f82c9..370e6a49a96b3e961b3a3d885e50c6d9d3288421 100644 (file)
@@ -91,6 +91,7 @@ check_PROGRAMS = \
        tests/lib/test_ttable \
        tests/lib/test_typelist \
        tests/lib/test_versioncmp \
+       tests/lib/test_xref \
        tests/lib/test_zlog \
        tests/lib/test_graph \
        tests/lib/cli/test_cli \
@@ -350,6 +351,10 @@ tests_lib_test_versioncmp_CFLAGS = $(TESTS_CFLAGS)
 tests_lib_test_versioncmp_CPPFLAGS = $(TESTS_CPPFLAGS)
 tests_lib_test_versioncmp_LDADD = $(ALL_TESTS_LDADD)
 tests_lib_test_versioncmp_SOURCES = tests/lib/test_versioncmp.c
+tests_lib_test_xref_CFLAGS = $(TESTS_CFLAGS)
+tests_lib_test_xref_CPPFLAGS = $(TESTS_CPPFLAGS)
+tests_lib_test_xref_LDADD = $(ALL_TESTS_LDADD)
+tests_lib_test_xref_SOURCES = tests/lib/test_xref.c
 tests_lib_test_zlog_CFLAGS = $(TESTS_CFLAGS)
 tests_lib_test_zlog_CPPFLAGS = $(TESTS_CPPFLAGS)
 tests_lib_test_zlog_LDADD = $(ALL_TESTS_LDADD)
@@ -413,6 +418,7 @@ EXTRA_DIST += \
        tests/lib/test_ttable.refout \
        tests/lib/test_typelist.py \
        tests/lib/test_versioncmp.py \
+       tests/lib/test_xref.py \
        tests/lib/test_zlog.py \
        tests/lib/test_graph.py \
        tests/lib/test_graph.refout \