summaryrefslogtreecommitdiffstats
path: root/tests/lib/test_xref.c
blob: fca0d47f388e7d288d3816ed4cd300ab81b01832 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * xref tests
 * Copyright (C) 2020  David Lamparter for NetDEF, Inc.
 */

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