tests: add unit test for xrefs

Signed-off-by: David Lamparter <equinox@diac24.net>
This commit is contained in:
David Lamparter 2020-07-18 11:39:31 +02:00
parent 87d383171d
commit 494d842022
4 changed files with 153 additions and 0 deletions

1
tests/.gitignore vendored
View file

@ -47,6 +47,7 @@
/lib/test_ttable /lib/test_ttable
/lib/test_typelist /lib/test_typelist
/lib/test_versioncmp /lib/test_versioncmp
/lib/test_xref
/lib/test_zlog /lib/test_zlog
/lib/test_zmq /lib/test_zmq
/ospf6d/test_lsdb /ospf6d/test_lsdb

140
tests/lib/test_xref.c Normal file
View file

@ -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;
}

6
tests/lib/test_xref.py Normal file
View file

@ -0,0 +1,6 @@
import frrtest
class TestXref(frrtest.TestMultiOut):
program = './test_xref'
TestXref.exit_cleanly()

View file

@ -91,6 +91,7 @@ check_PROGRAMS = \
tests/lib/test_ttable \ tests/lib/test_ttable \
tests/lib/test_typelist \ tests/lib/test_typelist \
tests/lib/test_versioncmp \ tests/lib/test_versioncmp \
tests/lib/test_xref \
tests/lib/test_zlog \ tests/lib/test_zlog \
tests/lib/test_graph \ tests/lib/test_graph \
tests/lib/cli/test_cli \ 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_CPPFLAGS = $(TESTS_CPPFLAGS)
tests_lib_test_versioncmp_LDADD = $(ALL_TESTS_LDADD) tests_lib_test_versioncmp_LDADD = $(ALL_TESTS_LDADD)
tests_lib_test_versioncmp_SOURCES = tests/lib/test_versioncmp.c 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_CFLAGS = $(TESTS_CFLAGS)
tests_lib_test_zlog_CPPFLAGS = $(TESTS_CPPFLAGS) tests_lib_test_zlog_CPPFLAGS = $(TESTS_CPPFLAGS)
tests_lib_test_zlog_LDADD = $(ALL_TESTS_LDADD) tests_lib_test_zlog_LDADD = $(ALL_TESTS_LDADD)
@ -413,6 +418,7 @@ EXTRA_DIST += \
tests/lib/test_ttable.refout \ tests/lib/test_ttable.refout \
tests/lib/test_typelist.py \ tests/lib/test_typelist.py \
tests/lib/test_versioncmp.py \ tests/lib/test_versioncmp.py \
tests/lib/test_xref.py \
tests/lib/test_zlog.py \ tests/lib/test_zlog.py \
tests/lib/test_graph.py \ tests/lib/test_graph.py \
tests/lib/test_graph.refout \ tests/lib/test_graph.refout \