Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright (C) 2020 YADRO |
| 3 | |
| 4 | #include "config.hpp" |
| 5 | |
Nan Zhou | 042b5ba | 2021-06-18 09:32:45 -0700 | [diff] [blame] | 6 | #include <sys/un.h> |
| 7 | |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 8 | #include <gtest/gtest.h> |
| 9 | |
| 10 | // Names of environment variables |
| 11 | static const char* SOCKET_ID = "SOCKET_ID"; |
Nan Zhou | 042b5ba | 2021-06-18 09:32:45 -0700 | [diff] [blame] | 12 | static const char* MODE = "MODE"; |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 13 | static const char* BUF_MAXSIZE = "BUF_MAXSIZE"; |
| 14 | static const char* BUF_MAXTIME = "BUF_MAXTIME"; |
| 15 | static const char* FLUSH_FULL = "FLUSH_FULL"; |
| 16 | static const char* HOST_STATE = "HOST_STATE"; |
| 17 | static const char* OUT_DIR = "OUT_DIR"; |
| 18 | static const char* MAX_FILES = "MAX_FILES"; |
Nan Zhou | 042b5ba | 2021-06-18 09:32:45 -0700 | [diff] [blame] | 19 | static const char* STREAM_DST = "STREAM_DST"; |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 20 | |
| 21 | /** |
| 22 | * @class ConfigTest |
| 23 | * @brief Configuration tests. |
| 24 | */ |
| 25 | class ConfigTest : public ::testing::Test |
| 26 | { |
| 27 | protected: |
| 28 | void SetUp() override |
| 29 | { |
| 30 | resetEnv(); |
| 31 | } |
| 32 | |
| 33 | void TearDown() override |
| 34 | { |
| 35 | resetEnv(); |
| 36 | } |
| 37 | |
| 38 | /** @brief Reset environment variables. */ |
| 39 | void resetEnv() const |
| 40 | { |
| 41 | unsetenv(SOCKET_ID); |
Nan Zhou | 042b5ba | 2021-06-18 09:32:45 -0700 | [diff] [blame] | 42 | unsetenv(MODE); |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 43 | unsetenv(BUF_MAXSIZE); |
| 44 | unsetenv(BUF_MAXTIME); |
| 45 | unsetenv(FLUSH_FULL); |
| 46 | unsetenv(HOST_STATE); |
| 47 | unsetenv(OUT_DIR); |
| 48 | unsetenv(MAX_FILES); |
Nan Zhou | 042b5ba | 2021-06-18 09:32:45 -0700 | [diff] [blame] | 49 | unsetenv(STREAM_DST); |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 50 | } |
| 51 | }; |
| 52 | |
| 53 | TEST_F(ConfigTest, Defaults) |
| 54 | { |
| 55 | Config cfg; |
| 56 | EXPECT_STREQ(cfg.socketId, ""); |
Nan Zhou | 042b5ba | 2021-06-18 09:32:45 -0700 | [diff] [blame] | 57 | EXPECT_EQ(cfg.mode, Mode::bufferMode); |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 58 | EXPECT_EQ(cfg.bufMaxSize, 3000); |
| 59 | EXPECT_EQ(cfg.bufMaxTime, 0); |
| 60 | EXPECT_EQ(cfg.bufFlushFull, false); |
| 61 | EXPECT_STREQ(cfg.hostState, "/xyz/openbmc_project/state/host0"); |
| 62 | EXPECT_STREQ(cfg.outDir, "/var/lib/obmc/hostlogs"); |
| 63 | EXPECT_EQ(cfg.maxFiles, 10); |
Nan Zhou | 042b5ba | 2021-06-18 09:32:45 -0700 | [diff] [blame] | 64 | EXPECT_STREQ(cfg.streamDestination, "/run/rsyslog/console_input"); |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 65 | } |
| 66 | |
Nan Zhou | 042b5ba | 2021-06-18 09:32:45 -0700 | [diff] [blame] | 67 | TEST_F(ConfigTest, LoadInBufferMode) |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 68 | { |
| 69 | setenv(SOCKET_ID, "id123", 1); |
Nan Zhou | 042b5ba | 2021-06-18 09:32:45 -0700 | [diff] [blame] | 70 | setenv(MODE, "buffer", 1); |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 71 | setenv(BUF_MAXSIZE, "1234", 1); |
| 72 | setenv(BUF_MAXTIME, "4321", 1); |
| 73 | setenv(FLUSH_FULL, "true", 1); |
| 74 | setenv(HOST_STATE, "host123", 1); |
| 75 | setenv(OUT_DIR, "path123", 1); |
| 76 | setenv(MAX_FILES, "1122", 1); |
| 77 | |
| 78 | Config cfg; |
| 79 | EXPECT_STREQ(cfg.socketId, "id123"); |
Nan Zhou | 042b5ba | 2021-06-18 09:32:45 -0700 | [diff] [blame] | 80 | EXPECT_EQ(cfg.mode, Mode::bufferMode); |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 81 | EXPECT_EQ(cfg.bufMaxSize, 1234); |
| 82 | EXPECT_EQ(cfg.bufMaxTime, 4321); |
| 83 | EXPECT_EQ(cfg.bufFlushFull, true); |
| 84 | EXPECT_STREQ(cfg.hostState, "host123"); |
| 85 | EXPECT_STREQ(cfg.outDir, "path123"); |
| 86 | EXPECT_EQ(cfg.maxFiles, 1122); |
Nan Zhou | 042b5ba | 2021-06-18 09:32:45 -0700 | [diff] [blame] | 87 | // This should be default. |
| 88 | EXPECT_STREQ(cfg.streamDestination, "/run/rsyslog/console_input"); |
| 89 | } |
| 90 | |
| 91 | TEST_F(ConfigTest, LoadInStreamMode) |
| 92 | { |
| 93 | setenv(SOCKET_ID, "id123", 1); |
| 94 | setenv(MODE, "stream", 1); |
| 95 | setenv(STREAM_DST, "path123", 1); |
| 96 | |
| 97 | Config cfg; |
| 98 | EXPECT_STREQ(cfg.socketId, "id123"); |
| 99 | EXPECT_EQ(cfg.mode, Mode::streamMode); |
| 100 | EXPECT_STREQ(cfg.streamDestination, "path123"); |
| 101 | |
| 102 | // These should be default. |
| 103 | EXPECT_EQ(cfg.bufMaxSize, 3000); |
| 104 | EXPECT_EQ(cfg.bufMaxTime, 0); |
| 105 | EXPECT_EQ(cfg.bufFlushFull, false); |
| 106 | EXPECT_STREQ(cfg.hostState, "/xyz/openbmc_project/state/host0"); |
| 107 | EXPECT_STREQ(cfg.outDir, "/var/lib/obmc/hostlogs"); |
| 108 | EXPECT_EQ(cfg.maxFiles, 10); |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | TEST_F(ConfigTest, InvalidNumeric) |
| 112 | { |
| 113 | setenv(BUF_MAXSIZE, "-1234", 1); |
Nan Zhou | 042b5ba | 2021-06-18 09:32:45 -0700 | [diff] [blame] | 114 | EXPECT_THROW(Config(), std::invalid_argument); |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | TEST_F(ConfigTest, InvalidBoolean) |
| 118 | { |
| 119 | setenv(FLUSH_FULL, "invalid", 1); |
Nan Zhou | 042b5ba | 2021-06-18 09:32:45 -0700 | [diff] [blame] | 120 | EXPECT_THROW(Config(), std::invalid_argument); |
| 121 | setenv(FLUSH_FULL, "true", 1); |
| 122 | EXPECT_NO_THROW(Config()); |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 123 | } |
| 124 | |
Nan Zhou | 042b5ba | 2021-06-18 09:32:45 -0700 | [diff] [blame] | 125 | TEST_F(ConfigTest, Mode) |
| 126 | { |
| 127 | setenv(MODE, "invalid", 1); |
| 128 | EXPECT_THROW(Config(), std::invalid_argument); |
| 129 | setenv(MODE, "stream", 1); |
| 130 | EXPECT_EQ(Config().mode, Mode::streamMode); |
| 131 | setenv(MODE, "buffer", 1); |
| 132 | EXPECT_EQ(Config().mode, Mode::bufferMode); |
| 133 | } |
| 134 | |
| 135 | TEST_F(ConfigTest, InvalidBufferModeConfig) |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 136 | { |
| 137 | setenv(BUF_MAXSIZE, "0", 1); |
| 138 | setenv(BUF_MAXTIME, "0", 1); |
| 139 | setenv(FLUSH_FULL, "true", 1); |
Nan Zhou | 042b5ba | 2021-06-18 09:32:45 -0700 | [diff] [blame] | 140 | EXPECT_THROW(Config(), std::invalid_argument); |
| 141 | } |
| 142 | |
| 143 | TEST_F(ConfigTest, InvalidStreamModeConfig) |
| 144 | { |
| 145 | std::string tooLong(sizeof(sockaddr_un::sun_path), '0'); |
| 146 | setenv(MODE, "stream", 1); |
| 147 | setenv(STREAM_DST, tooLong.c_str(), 1); |
| 148 | EXPECT_THROW(Config(), std::invalid_argument); |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 149 | } |