]> git.proxmox.com Git - mirror_frr.git/blob - tests/lib/test_xref.c
*: require semicolon after FRR_DAEMON_INFO & co.
[mirror_frr.git] / tests / lib / test_xref.c
1 /*
2 * xref tests
3 * Copyright (C) 2020 David Lamparter for NetDEF, Inc.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; see the file COPYING; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #include <zebra.h>
21 #include "xref.h"
22 #include "log.h"
23
24 /*
25 * "lib/test_xref.c" (only 1 directory component included)
26 * "logging call"
27 * 0x00000003 (network byte order - LOG_ERR)
28 * 0x00000000 (network byte order - EC / zero here)
29 *
30 * note there are no '\0' terminators included for the strings
31 *
32 * SHA256
33 * => 71a65ce6e81517f642c8f55fb2af6f181f7df54357913b5b577aa61a663fdd4c
34 * & 0f -> 0x01 'H'
35 * & f001 -> 0x07 '7'
36 * & 3e -> 0x13 'K'
37 * & c007 -> 0x12 'J'
38 * & f8 -> 0x0b 'B'
39 * etc.
40 * (for reference: base32ch[] = "0123456789ABCDEFGHJKMNPQRSTVWXYZ")
41 *
42 * (bits are consumed starting with the lowest bit, and the first character
43 * only consumes 4 bits and has the 5th bit at 1)
44 */
45
46 static const char *expect_uid = "H7KJB-67TBH";
47 static bool test_logcall(void)
48 {
49 zlog_err("logging call");
50
51 return true;
52 }
53
54 static void check_xref(const struct xref *xref, bool *found, bool *error)
55 {
56 const char *file = xref->file, *p;
57
58 p = strrchr(file, '/');
59 if (p)
60 file = p + 1;
61
62 if (strcmp(file, "test_xref.c"))
63 return;
64 if (xref->type != XREFT_LOGMSG)
65 return;
66 if (strcmp(xref->func, "test_logcall"))
67 return;
68
69 printf("xref: %s:%d %s() type=%d uid=%s\n",
70 xref->file, xref->line, xref->func, xref->type,
71 xref->xrefdata ? xref->xrefdata->uid : "--");
72
73 if (*found) {
74 printf("duplicate xref!\n");
75 *error = true;
76 }
77
78 const struct xref_logmsg *logmsg;
79
80 logmsg = container_of(xref, struct xref_logmsg, xref);
81 if (strcmp(logmsg->fmtstring, "logging call")) {
82 printf("log message mismatch!\n");
83 *error = true;
84 }
85 if (logmsg->priority != LOG_ERR || logmsg->ec != 0) {
86 printf("metadata mismatch!\n");
87 *error = true;
88 }
89
90 *found = true;
91
92 if (!xref->xrefdata) {
93 printf("no unique ID?\n");
94 *error = true;
95 return;
96 }
97
98 if (strcmp(xref->xrefdata->uid, expect_uid)) {
99 printf("unique ID mismatch, expected %s, got %s\n",
100 expect_uid, xref->xrefdata->uid);
101 *error = true;
102 }
103 }
104
105 static bool test_lookup(void)
106 {
107 struct xref_block *xb;
108 bool found = false, error = false;
109
110 for (xb = xref_blocks; xb; xb = xb->next) {
111 const struct xref * const *xrefp;
112
113 for (xrefp = xb->start; xrefp < xb->stop; xrefp++) {
114 const struct xref *xref = *xrefp;
115
116 if (!xref)
117 continue;
118
119 check_xref(xref, &found, &error);
120 }
121 }
122 return found && !error;
123 }
124
125 bool (*tests[])(void) = {
126 test_lookup,
127 test_logcall,
128 };
129
130 XREF_SETUP();
131
132 int main(int argc, char **argv)
133 {
134 zlog_aux_init("NONE: ", ZLOG_DISABLED);
135
136 for (unsigned int i = 0; i < array_size(tests); i++)
137 if (!tests[i]())
138 return 1;
139 return 0;
140 }