blob: 9bc32df0d5285db82d37b6e62d5ca9d806411d07 [file] [log] [blame]
Elliott Hughes213d9432023-03-01 22:56:13 +00001/*
2 * Copyright (C) 2023 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
Elliott Hughesafb8e052023-10-23 17:45:13 -070029#include <stddef.h> // glibc's <syslog.h> breaks without this; musl seems fine.
30
31#define SYSLOG_NAMES
Elliott Hughes213d9432023-03-01 22:56:13 +000032#include <syslog.h>
33
34#include <errno.h>
35#include <gtest/gtest.h>
36
37#include "utils.h"
38
Elliott Hughes5cfe7dc2025-07-27 19:26:46 -070039TEST(syslog, setlogmask) {
40 // The default should include all log priorities.
41 EXPECT_EQ(LOG_UPTO(LOG_DEBUG), setlogmask(0));
42}
43
44TEST(syslog, setlogmask_works) {
45 ExecTestHelper eth;
46 eth.Run(
47 [&]() {
48 setlogmask(LOG_MASK(LOG_INFO));
49 openlog("foo", LOG_PERROR, LOG_AUTH);
50 dprintf(STDERR_FILENO, "<");
51 syslog(LOG_INFO, "a");
52 dprintf(STDERR_FILENO, ">\n");
53 dprintf(STDERR_FILENO, "<");
54 syslog(LOG_DEBUG, "b");
55 dprintf(STDERR_FILENO, ">\n");
56 closelog();
57 exit(0);
58 },
59 0, "<foo: a\n>\n<>\n");
60}
61
Elliott Hughes213d9432023-03-01 22:56:13 +000062TEST(syslog, syslog_percent_m) {
63 ExecTestHelper eth;
64 eth.Run(
65 [&]() {
66 openlog("foo", LOG_PERROR, LOG_AUTH);
67 errno = EINVAL;
68 syslog(LOG_ERR, "a b c: %m");
69 closelog();
70 exit(0);
71 },
72 0, "foo: a b c: Invalid argument\n");
73}
74
75TEST(syslog, syslog_empty) {
76 ExecTestHelper eth;
77 eth.Run(
78 [&]() {
79 openlog("foo", LOG_PERROR, LOG_AUTH);
80 errno = EINVAL;
81 syslog(LOG_ERR, "");
82 closelog();
83 exit(0);
84 },
85 0, "foo: \n");
86}
87
88TEST(syslog, syslog_truncation) {
89 ExecTestHelper eth;
90 eth.Run(
91 [&]() {
92 openlog("bar", LOG_PERROR, LOG_AUTH);
93 char too_long[2048] = {};
94 memset(too_long, 'x', sizeof(too_long) - 1);
95 syslog(LOG_ERR, "%s", too_long);
96 closelog();
97 exit(0);
98 },
99 0, "bar: x{1023}\n");
100}
Elliott Hughesafb8e052023-10-23 17:45:13 -0700101
102static int by_name(const CODE* array, const char* name) {
103 for (auto c = array; c->c_name != nullptr; c++) {
104 if (!strcmp(c->c_name, name)) return c->c_val;
105 }
106 return -1;
107}
108
109static const char* by_value(const CODE* array, int value) {
110 for (auto c = array; c->c_name != nullptr; c++) {
111 if (c->c_val == value) return c->c_name;
112 }
113 return nullptr;
114}
115
116TEST(syslog, facilitynames) {
117 ASSERT_STREQ("auth", by_value(facilitynames, LOG_AUTH));
118 ASSERT_STREQ("local7", by_value(facilitynames, LOG_LOCAL7));
119 ASSERT_EQ(LOG_AUTH, by_name(facilitynames, "auth"));
120 ASSERT_EQ(LOG_LOCAL7, by_name(facilitynames, "local7"));
121}
122
123TEST(syslog, prioritynames) {
124 ASSERT_STREQ("alert", by_value(prioritynames, LOG_ALERT));
125 ASSERT_STREQ("err", by_value(prioritynames, LOG_ERR));
126 ASSERT_STREQ("warn", by_value(prioritynames, LOG_WARNING));
127 ASSERT_EQ(LOG_ALERT, by_name(prioritynames, "alert"));
128 ASSERT_EQ(LOG_ERR, by_name(prioritynames, "err"));
129 ASSERT_EQ(LOG_WARNING, by_name(prioritynames, "warn"));
130 ASSERT_EQ(LOG_WARNING, by_name(prioritynames, "warning"));
131}