Elliott Hughes | 213d943 | 2023-03-01 22:56:13 +0000 | [diff] [blame] | 1 | /* |
| 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 Hughes | afb8e05 | 2023-10-23 17:45:13 -0700 | [diff] [blame] | 29 | #include <stddef.h> // glibc's <syslog.h> breaks without this; musl seems fine. |
| 30 | |
| 31 | #define SYSLOG_NAMES |
Elliott Hughes | 213d943 | 2023-03-01 22:56:13 +0000 | [diff] [blame] | 32 | #include <syslog.h> |
| 33 | |
| 34 | #include <errno.h> |
| 35 | #include <gtest/gtest.h> |
| 36 | |
| 37 | #include "utils.h" |
| 38 | |
| 39 | TEST(syslog, syslog_percent_m) { |
| 40 | ExecTestHelper eth; |
| 41 | eth.Run( |
| 42 | [&]() { |
| 43 | openlog("foo", LOG_PERROR, LOG_AUTH); |
| 44 | errno = EINVAL; |
| 45 | syslog(LOG_ERR, "a b c: %m"); |
| 46 | closelog(); |
| 47 | exit(0); |
| 48 | }, |
| 49 | 0, "foo: a b c: Invalid argument\n"); |
| 50 | } |
| 51 | |
| 52 | TEST(syslog, syslog_empty) { |
| 53 | ExecTestHelper eth; |
| 54 | eth.Run( |
| 55 | [&]() { |
| 56 | openlog("foo", LOG_PERROR, LOG_AUTH); |
| 57 | errno = EINVAL; |
| 58 | syslog(LOG_ERR, ""); |
| 59 | closelog(); |
| 60 | exit(0); |
| 61 | }, |
| 62 | 0, "foo: \n"); |
| 63 | } |
| 64 | |
| 65 | TEST(syslog, syslog_truncation) { |
| 66 | ExecTestHelper eth; |
| 67 | eth.Run( |
| 68 | [&]() { |
| 69 | openlog("bar", LOG_PERROR, LOG_AUTH); |
| 70 | char too_long[2048] = {}; |
| 71 | memset(too_long, 'x', sizeof(too_long) - 1); |
| 72 | syslog(LOG_ERR, "%s", too_long); |
| 73 | closelog(); |
| 74 | exit(0); |
| 75 | }, |
| 76 | 0, "bar: x{1023}\n"); |
| 77 | } |
Elliott Hughes | afb8e05 | 2023-10-23 17:45:13 -0700 | [diff] [blame] | 78 | |
| 79 | static int by_name(const CODE* array, const char* name) { |
| 80 | for (auto c = array; c->c_name != nullptr; c++) { |
| 81 | if (!strcmp(c->c_name, name)) return c->c_val; |
| 82 | } |
| 83 | return -1; |
| 84 | } |
| 85 | |
| 86 | static const char* by_value(const CODE* array, int value) { |
| 87 | for (auto c = array; c->c_name != nullptr; c++) { |
| 88 | if (c->c_val == value) return c->c_name; |
| 89 | } |
| 90 | return nullptr; |
| 91 | } |
| 92 | |
| 93 | TEST(syslog, facilitynames) { |
| 94 | ASSERT_STREQ("auth", by_value(facilitynames, LOG_AUTH)); |
| 95 | ASSERT_STREQ("local7", by_value(facilitynames, LOG_LOCAL7)); |
| 96 | ASSERT_EQ(LOG_AUTH, by_name(facilitynames, "auth")); |
| 97 | ASSERT_EQ(LOG_LOCAL7, by_name(facilitynames, "local7")); |
| 98 | } |
| 99 | |
| 100 | TEST(syslog, prioritynames) { |
| 101 | ASSERT_STREQ("alert", by_value(prioritynames, LOG_ALERT)); |
| 102 | ASSERT_STREQ("err", by_value(prioritynames, LOG_ERR)); |
| 103 | ASSERT_STREQ("warn", by_value(prioritynames, LOG_WARNING)); |
| 104 | ASSERT_EQ(LOG_ALERT, by_name(prioritynames, "alert")); |
| 105 | ASSERT_EQ(LOG_ERR, by_name(prioritynames, "err")); |
| 106 | ASSERT_EQ(LOG_WARNING, by_name(prioritynames, "warn")); |
| 107 | ASSERT_EQ(LOG_WARNING, by_name(prioritynames, "warning")); |
| 108 | } |