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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
|
/* libnih
*
* test_logging.c - test suite for nih/logging.c
*
* Copyright © 2010 Scott James Remnant <scott@netsplit.com>.
* Copyright © 2010 Canonical Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2, as
* published by the Free Software Foundation.
*
* 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; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <nih/test.h>
#include <stdio.h>
#include <string.h>
#include <nih/macros.h>
#include <nih/alloc.h>
#include <nih/string.h>
#include <nih/logging.h>
#include <nih/main.h>
extern char *__abort_msg __attribute__ ((weak));
static NihLogLevel last_priority = NIH_LOG_UNKNOWN;
static char * last_message = NULL;
static int
my_logger (NihLogLevel priority,
const char *message)
{
TEST_ALLOC_SAFE {
last_priority = priority;
last_message = strdup (message);
}
if (! strcmp (message, "this should error"))
return -1;
return 0;
}
void
test_set_logger (void)
{
/* Check that we can change the logger function, and that the
* function is called for the logging macros that follow.
*/
TEST_FUNCTION ("nih_log_set_logger");
TEST_ALLOC_FAIL {
nih_log_set_priority (NIH_LOG_MESSAGE);
nih_log_set_logger (my_logger);
last_priority = NIH_LOG_UNKNOWN;
nih_fatal ("some message");
TEST_EQ (last_priority, NIH_LOG_FATAL);
free (last_message);
nih_log_set_logger (nih_logger_printf);
}
}
void
test_set_priority (void)
{
/* Check that we reduce the minimum priority and end up getting
* log messages we wouldn't have previously received.
*/
TEST_FUNCTION ("nih_log_set_priority");
TEST_ALLOC_FAIL {
nih_log_set_logger (my_logger);
nih_log_set_priority (NIH_LOG_DEBUG);
last_priority = NIH_LOG_UNKNOWN;
nih_debug ("some message");
TEST_EQ (last_priority, NIH_LOG_DEBUG);
free (last_message);
nih_log_set_logger (nih_logger_printf);
nih_log_set_priority (NIH_LOG_MESSAGE);
}
}
void
test_log_message (void)
{
char *msg = NULL;
int ret;
TEST_FUNCTION ("nih_log_message");
nih_log_set_logger (my_logger);
/* Check that a message with high enough priority makes it through
* to the logger.
*/
TEST_FEATURE ("with message of high enough priority");
TEST_ALLOC_FAIL {
last_priority = NIH_LOG_UNKNOWN;
last_message = NULL;
ret = nih_log_message (NIH_LOG_FATAL,
"message with %s %d formatting",
"some", 20);
TEST_EQ (ret, 0);
TEST_EQ (last_priority, NIH_LOG_FATAL);
TEST_EQ_STR (last_message, "message with some 20 formatting");
free (last_message);
}
/* Check that a message with insufficient priority does not make it
* through to the logger.
*/
TEST_FEATURE ("with message of insufficient priority");
TEST_ALLOC_FAIL {
last_priority = NIH_LOG_UNKNOWN;
last_message = NULL;
ret = nih_log_message (NIH_LOG_DEBUG, "not high enough");
TEST_GT (ret, 0);
TEST_EQ (last_priority, NIH_LOG_UNKNOWN);
TEST_EQ_P (last_message, NULL);
}
/* Check that an error code returned from the logger is returned
* by the function.
*/
TEST_FEATURE ("with error code returned from logger");
TEST_ALLOC_FAIL {
ret = nih_log_message (NIH_LOG_FATAL, "this should error");
TEST_LT (ret, 0);
free (last_message);
}
/* Check that a fatal message is also stored in the glibc __abort_msg
* variable.
*/
if (&__abort_msg) {
TEST_FEATURE ("with fatal message");
TEST_ALLOC_FAIL {
__abort_msg = NULL;
last_priority = NIH_LOG_UNKNOWN;
last_message = NULL;
ret = nih_log_message (NIH_LOG_FATAL,
"message with %s %d formatting",
"some", 20);
TEST_EQ (ret, 0);
TEST_EQ (last_priority, NIH_LOG_FATAL);
TEST_EQ_STR (last_message, "message with some 20 formatting");
TEST_NE_P (__abort_msg, NULL);
TEST_ALLOC_PARENT (__abort_msg, NULL);
TEST_EQ_STR (__abort_msg, "message with some 20 formatting");
free (last_message);
}
/* Check that a fatal message can safely overwrite one already stored
* in the glibc __abort_msg variable.
*/
TEST_FEATURE ("with second fatal message");
TEST_ALLOC_FAIL {
TEST_ALLOC_SAFE {
msg = nih_strdup (NULL, "test");
}
__abort_msg = msg;
TEST_FREE_TAG (msg);
last_priority = NIH_LOG_UNKNOWN;
last_message = NULL;
ret = nih_log_message (NIH_LOG_FATAL,
"message with %s %d formatting",
"some", 20);
TEST_EQ (ret, 0);
TEST_EQ (last_priority, NIH_LOG_FATAL);
TEST_EQ_STR (last_message, "message with some 20 formatting");
TEST_FREE (msg);
TEST_NE_P (__abort_msg, NULL);
TEST_ALLOC_PARENT (__abort_msg, NULL);
TEST_EQ_STR (__abort_msg, "message with some 20 formatting");
free (last_message);
}
} else {
printf ("SKIP: __abort_msg not available\n");
}
/* Check that the nih_debug macro wraps the call properly and
* includes the function in which the message occurred.
*/
TEST_FUNCTION ("nih_debug");
TEST_ALLOC_FAIL {
last_priority = NIH_LOG_UNKNOWN;
last_message = NULL;
nih_log_set_priority (NIH_LOG_DEBUG);
ret = nih_debug ("%s debugging message", "a");
TEST_EQ (ret, 0);
TEST_EQ (last_priority, NIH_LOG_DEBUG);
TEST_EQ_STR (last_message,
"test_log_message: a debugging message");
free (last_message);
}
/* Check that the nih_info macro wraps the call properly. */
TEST_FUNCTION ("nih_info");
TEST_ALLOC_FAIL {
last_priority = NIH_LOG_UNKNOWN;
last_message = NULL;
ret = nih_info ("%d formatted %s", 47, "message");
TEST_EQ (ret, 0);
TEST_EQ (last_priority, NIH_LOG_INFO);
TEST_EQ_STR (last_message, "47 formatted message");
free (last_message);
}
/* Check that the nih_warn macro wraps the call properly. */
TEST_FUNCTION ("nih_warn");
TEST_ALLOC_FAIL {
last_priority = NIH_LOG_UNKNOWN;
last_message = NULL;
ret = nih_warn ("%d formatted %s", -2, "text");
TEST_EQ (ret, 0);
TEST_EQ (last_priority, NIH_LOG_WARN);
TEST_EQ_STR (last_message, "-2 formatted text");
free (last_message);
}
/* Check that the nih_message macro wraps the call properly.
*/
TEST_FUNCTION ("nih_message");
TEST_ALLOC_FAIL {
last_priority = NIH_LOG_UNKNOWN;
last_message = NULL;
ret = nih_message ("%d formatted %s", -24, "string");
TEST_EQ (ret, 0);
TEST_EQ (last_priority, NIH_LOG_MESSAGE);
TEST_EQ_STR (last_message, "-24 formatted string");
free (last_message);
}
/* Check that the nih_error macro wraps the call properly. */
TEST_FUNCTION ("nih_error");
TEST_ALLOC_FAIL {
last_priority = NIH_LOG_UNKNOWN;
last_message = NULL;
ret = nih_error ("formatted %d %s", 42, "text");
TEST_EQ (ret, 0);
TEST_EQ (last_priority, NIH_LOG_ERROR);
TEST_EQ_STR (last_message, "formatted 42 text");
free (last_message);
}
/* Check that the nih_fatal macro wraps the call properly. */
TEST_FUNCTION ("nih_fatal");
TEST_ALLOC_FAIL {
last_priority = NIH_LOG_UNKNOWN;
last_message = NULL;
ret = nih_fatal ("%s message %d", "formatted", 999);
TEST_EQ (ret, 0);
TEST_EQ (last_priority, NIH_LOG_FATAL);
TEST_EQ_STR (last_message, "formatted message 999");
free (last_message);
}
nih_log_set_priority (NIH_LOG_MESSAGE);
nih_log_set_logger (nih_logger_printf);
}
void
test_logger_printf (void)
{
FILE *output;
int ret = 0;
TEST_FUNCTION ("nih_logger_printf");
output = tmpfile ();
nih_log_set_priority (NIH_LOG_DEBUG);
/* Check that a low priority message is output to stdout and formatted
* correctly, without the program name prefixed to the front.
*/
TEST_FEATURE ("with low priority message");
TEST_ALLOC_FAIL {
TEST_DIVERT_STDOUT (output) {
ret = nih_log_message (NIH_LOG_DEBUG,
"message with %s %d formatting",
"some", 20);
}
rewind (output);
TEST_EQ (ret, 0);
TEST_FILE_EQ (output, "message with some 20 formatting\n");
TEST_FILE_END (output);
TEST_FILE_RESET (output);
}
/* Check that ordinary messages are output on stdout and formatted
* correctly, without the program name prefixed on the front.
*/
TEST_FEATURE ("with ordinary message");
TEST_ALLOC_FAIL {
TEST_DIVERT_STDOUT (output) {
ret = nih_log_message (NIH_LOG_MESSAGE,
"message with %s %d formatting",
"some", 20);
}
rewind (output);
TEST_EQ (ret, 0);
TEST_FILE_EQ (output, "message with some 20 formatting\n");
TEST_FILE_END (output);
TEST_FILE_RESET (output);
}
/* Check that warning messages are output on stderr and formatted
* correctly, with the program name prefixed on the front.
*/
TEST_FEATURE ("with ordinary message");
TEST_ALLOC_FAIL {
TEST_DIVERT_STDERR (output) {
ret = nih_log_message (NIH_LOG_WARN,
"message with %s %d formatting",
"some", 20);
}
rewind (output);
TEST_EQ (ret, 0);
TEST_FILE_EQ (output, "test: message with some 20 formatting\n");
TEST_FILE_END (output);
TEST_FILE_RESET (output);
}
/* Check that a high priority message is output to stderr and formatted
* correctly, with the program name prefixed on the front.
*/
TEST_FEATURE ("with high priority message");
TEST_ALLOC_FAIL {
TEST_DIVERT_STDERR (output) {
ret = nih_log_message (NIH_LOG_FATAL,
"%s message %d formatted",
"error", -1);
}
rewind (output);
TEST_EQ (ret, 0);
TEST_FILE_EQ (output, "test: error message -1 formatted\n");
TEST_FILE_END (output);
TEST_FILE_RESET (output);
}
/* Check that a message with a GNU-style filename prefix is correctly
* formatted, with no space between the program name and the message.
*/
TEST_FEATURE ("with prefixed message");
TEST_ALLOC_FAIL {
TEST_DIVERT_STDERR (output) {
ret = nih_log_message (NIH_LOG_FATAL,
"%s:%d: some error or other",
"example.txt", 303);
}
rewind (output);
TEST_EQ (ret, 0);
TEST_FILE_EQ (output, "test:example.txt:303: some error or other\n");
TEST_FILE_END (output);
TEST_FILE_RESET (output);
}
/* Check that an error code is returned if the output stream is
* closed.
*/
TEST_FEATURE ("with closed stream");
TEST_ALLOC_FAIL {
TEST_DIVERT_STDERR (output) {
close (STDERR_FILENO);
ret = nih_log_message (NIH_LOG_FATAL, "an error message");
}
TEST_LT (ret, 0);
}
fclose (output);
nih_log_set_priority (NIH_LOG_MESSAGE);
}
int
main (int argc,
char *argv[])
{
program_name = "test";
test_set_logger ();
test_set_priority ();
test_log_message ();
test_logger_printf ();
return 0;
}
|