Debian Codegraph @

tests/t-logging.c

raw
1 /* t-logging.c - Check the logging interface
2 * Copyright (C) 2018 g10 Code GmbH
3 *
4 * This file is part of Libgpg-error.
5 *
6 * Libgpg-error is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public License
8 * as published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgpg-error is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, see <https://www.gnu.org/licenses/>.
18 * SPDX-License-Identifier: LGPL-2.1-or-later
19 */
20
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <assert.h>
29 #include <unistd.h>
30
31 #define PGM "t-logging"
32 #include "t-common.h"
33
34 /* The memory based estream we use for logging. */
35 static estream_t logmemfp;
36
37
38 static const char *
39 my_strusage (int level)
40 {
41 const char *p;
42
43 switch (level)
44 {
45 case 9: p = "LGPL-2.1-or-later"; break;
46 case 11: p = PGM; break;
47 default: p = NULL;
48 }
49 return p;
50 }
51
52
53 /* Read all data from the log stream into a new malloced buffer and return
54 * that buffer. The buffer is always 0 terminated. Either returns a
55 * string or dies. The stream will be truncated to zero. */
56 static char *
57 log_to_string (void)
58 {
59 #define NCHUNK 1024
60 estream_t stream = gpgrt_log_get_stream ();
61 char *buffer;
62 size_t bufsize, buflen;
63 size_t nread;
64
65 gpgrt_log_flush ();
66 gpgrt_rewind (stream);
67
68 buffer = NULL;
69 buflen = bufsize = 0;
70 do
71 {
72 bufsize += NCHUNK;
73 buffer = realloc (buffer, bufsize+1);
74 if (!buffer)
75 die ("malloc failed at line %d\n", __LINE__);
76
77 nread = gpgrt_fread (buffer + buflen, 1, NCHUNK, stream);
78 if (nread < NCHUNK && gpgrt_ferror (stream))
79 die ("fread failed at line %d: %s\n", __LINE__,
80 gpg_strerror (gpg_err_code_from_syserror ()));
81 buflen += nread;
82 }
83 while (nread == NCHUNK);
84 buffer[nread] = 0;
85
86 if (strlen (buffer) != buflen)
87 fail ("stream_to_string detected an embedded nul");
88
89 gpgrt_ftruncate (stream, 0);
90 return buffer;
91 #undef NCHUNK
92 }
93
94
95 static void
96 check_log_info (void)
97 {
98 char *logbuf;
99
100 log_info ("first log\n");
101 logbuf = log_to_string ();
102 if (strcmp (logbuf, "t-logging: first log\n"))
103 fail ("log_info test failed at line %d\n", __LINE__);
104 free (logbuf);
105
106 /* The second line should not have a LF. */
107 log_info ("second log line");
108 log_info ("third log line");
109 logbuf = log_to_string ();
110 if (strcmp (logbuf, ("t-logging: second log line\n"
111 "t-logging: third log line")))
112 fail ("log_info test failed at line %d\n", __LINE__);
113 free (logbuf);
114
115 /* Now a multi line log. */
116 log_info ("This is log line 1\nand 2\nand 3\n");
117 logbuf = log_to_string ();
118 if (strcmp (logbuf, ("t-logging: This is log line 1\n"
119 "and 2\n"
120 "and 3\n")))
121 fail ("log_info test failed at line %d\n", __LINE__);
122 free (logbuf);
123
124 /* With arguments. */
125 log_info ("file '%s' line %d: %s\n", "/foo/bar.txt", 20, "not found");
126 logbuf = log_to_string ();
127 if (strcmp (logbuf, "t-logging: file '/foo/bar.txt' line 20: not found\n"))
128 fail ("log_info test failed at line %d\n", __LINE__);
129 free (logbuf);
130
131 /* With arguments and a control char in the string arg. */
132 log_info ("file '%s' line %d: %s\n", "/foo/bar.txt\b", 20, "not found");
133 logbuf = log_to_string ();
134 if (strcmp (logbuf,
135 "t-logging: file '/foo/bar.txt\\b' line 20: not found\n"))
136 fail ("log_info test failed at line %d\n", __LINE__);
137 free (logbuf);
138
139 /* With arguments and the prefix in a string arg. */
140 log_info ("file '%s': %s\n", "/foo/bar.txt\nt-logging", "not \x01 found");
141 logbuf = log_to_string ();
142 if (strcmp (logbuf,
143 "t-logging: file '/foo/bar.txt\\nt-logging': not \\x01 found\n"))
144 fail ("log_info test failed at line %d\n", __LINE__);
145 free (logbuf);
146
147 /* With arguments and byte with bit 7 set in a string arg. */
148 log_info ("file '%s': %s\n", "/foo/bar.txt\n", "not \x81 found");
149 logbuf = log_to_string ();
150 if (strcmp (logbuf,
151 "t-logging: file '/foo/bar.txt\\n': not \x81 found\n"))
152 fail ("log_info test failed at line %d\n", __LINE__);
153 /* show ("===>%s<===\n", logbuf); */
154
155 free (logbuf);
156 }
157
158
159 static void
160 check_with_pid (void)
161 {
162 char testbuf[100];
163 char *logbuf;
164
165 snprintf (testbuf, sizeof testbuf, "t-logging[%u]: ",
166 (unsigned int)getpid ());
167
168 log_info ("first log\n");
169 logbuf = log_to_string ();
170 if (strncmp (logbuf, testbuf, strlen (testbuf))
171 || strcmp (logbuf+strlen (testbuf), "first log\n"))
172 fail ("log_with_pid test failed at line %d\n", __LINE__);
173 free (logbuf);
174
175 log_info ("This is log line 1\nand 2\nand 3\n");
176 logbuf = log_to_string ();
177 if (strncmp (logbuf, testbuf, strlen (testbuf))
178 || strcmp (logbuf+strlen (testbuf), ("This is log line 1\n"
179 "and 2\n"
180 "and 3\n")))
181 fail ("log_with_pid test failed at line %d\n", __LINE__);
182 free (logbuf);
183 }
184
185
186 static void
187 check_log_error (void)
188 {
189 char *logbuf;
190
191 if (log_get_errorcount (0))
192 fail ("log_get_errorcount() != 0 at line %d\n", __LINE__);
193
194 log_error ("Hola, something went wrong\n");
195 if (log_get_errorcount (0) != 1)
196 fail ("log_get_errorcount() != 1 at line %d\n", __LINE__);
197 logbuf = log_to_string ();
198 if (strcmp (logbuf, "t-logging: Hola, something went wrong\n"))
199 fail ("log_info test failed at line %d\n", __LINE__);
200 free (logbuf);
201 if (log_get_errorcount (0) != 1)
202 fail ("log_get_errorcount() != 1 at line %d\n", __LINE__);
203 if (log_get_errorcount (1) != 1) /* note: clear returns old value. */
204 fail ("log_get_errorcount() != 1 at line %d\n", __LINE__);
205 if (log_get_errorcount (0))
206 fail ("log_get_errorcount() != 0 after clear at line %d\n", __LINE__);
207 }
208
209
210 int
211 main (int argc, char **argv)
212 {
213 gpgrt_opt_t opts[] = {
214 ARGPARSE_x ('v', "verbose", NONE, 0, "Print more diagnostics"),
215 ARGPARSE_s_n('d', "debug", "Flyswatter"),
216 ARGPARSE_end()
217 };
218 gpgrt_argparse_t pargs = { &argc, &argv, 0 };
219
220 gpgrt_set_strusage (my_strusage);
221 gpgrt_log_set_prefix (gpgrt_strusage (11), GPGRT_LOG_WITH_PREFIX);
222
223 while (gpgrt_argparse (NULL, &pargs, opts))
224 {
225 switch (pargs.r_opt)
226 {
227 case 'v': verbose++; break;
228 case 'd': debug++; break;
229 default : pargs.err = ARGPARSE_PRINT_ERROR; break;
230 }
231 }
232 gpgrt_argparse (NULL, &pargs, NULL);
233
234 show ("testing logging using a memory log stream\n");
235 logmemfp = gpgrt_fopenmem (0, "w+b");
236 if (!logmemfp)
237 die ("fopenmem failed at line %d\n", __LINE__);
238 gpgrt_log_set_sink (NULL, logmemfp, -1);
239
240 check_log_info ();
241 gpgrt_log_set_prefix (NULL, GPGRT_LOG_WITH_PREFIX|GPGRT_LOG_WITH_PID);
242 check_with_pid ();
243 gpgrt_log_set_prefix (NULL, GPGRT_LOG_WITH_PREFIX);
244 check_log_error ();
245
246 /* FIXME: Add more tests. */
247
248 show ("testing logging finished\n");
249 return !!errorcount;
250 }