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 | |
| 29 | #include <syslog.h> |
| 30 | |
| 31 | #include <errno.h> |
| 32 | #include <gtest/gtest.h> |
| 33 | |
| 34 | #include "utils.h" |
| 35 | |
| 36 | TEST(syslog, syslog_percent_m) { |
| 37 | ExecTestHelper eth; |
| 38 | eth.Run( |
| 39 | [&]() { |
| 40 | openlog("foo", LOG_PERROR, LOG_AUTH); |
| 41 | errno = EINVAL; |
| 42 | syslog(LOG_ERR, "a b c: %m"); |
| 43 | closelog(); |
| 44 | exit(0); |
| 45 | }, |
| 46 | 0, "foo: a b c: Invalid argument\n"); |
| 47 | } |
| 48 | |
| 49 | TEST(syslog, syslog_empty) { |
| 50 | ExecTestHelper eth; |
| 51 | eth.Run( |
| 52 | [&]() { |
| 53 | openlog("foo", LOG_PERROR, LOG_AUTH); |
| 54 | errno = EINVAL; |
| 55 | syslog(LOG_ERR, ""); |
| 56 | closelog(); |
| 57 | exit(0); |
| 58 | }, |
| 59 | 0, "foo: \n"); |
| 60 | } |
| 61 | |
| 62 | TEST(syslog, syslog_truncation) { |
| 63 | ExecTestHelper eth; |
| 64 | eth.Run( |
| 65 | [&]() { |
| 66 | openlog("bar", LOG_PERROR, LOG_AUTH); |
| 67 | char too_long[2048] = {}; |
| 68 | memset(too_long, 'x', sizeof(too_long) - 1); |
| 69 | syslog(LOG_ERR, "%s", too_long); |
| 70 | closelog(); |
| 71 | exit(0); |
| 72 | }, |
| 73 | 0, "bar: x{1023}\n"); |
| 74 | } |