blob: 65936f5f3b58633aa4a8e49d2a6d03bb09e77dee [file] [log] [blame]
Artem Senicheve8837d52020-06-07 11:59:04 +03001// SPDX-License-Identifier: Apache-2.0
2// Copyright (C) 2020 YADRO
3
4#include "config.hpp"
5
Nan Zhou042b5ba2021-06-18 09:32:45 -07006#include <sys/un.h>
7
Artem Senicheve8837d52020-06-07 11:59:04 +03008#include <gtest/gtest.h>
9
10// Names of environment variables
11static const char* SOCKET_ID = "SOCKET_ID";
Nan Zhou042b5ba2021-06-18 09:32:45 -070012static const char* MODE = "MODE";
Artem Senicheve8837d52020-06-07 11:59:04 +030013static const char* BUF_MAXSIZE = "BUF_MAXSIZE";
14static const char* BUF_MAXTIME = "BUF_MAXTIME";
15static const char* FLUSH_FULL = "FLUSH_FULL";
16static const char* HOST_STATE = "HOST_STATE";
17static const char* OUT_DIR = "OUT_DIR";
18static const char* MAX_FILES = "MAX_FILES";
Nan Zhou042b5ba2021-06-18 09:32:45 -070019static const char* STREAM_DST = "STREAM_DST";
Artem Senicheve8837d52020-06-07 11:59:04 +030020
21/**
22 * @class ConfigTest
23 * @brief Configuration tests.
24 */
25class 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 Zhou042b5ba2021-06-18 09:32:45 -070042 unsetenv(MODE);
Artem Senicheve8837d52020-06-07 11:59:04 +030043 unsetenv(BUF_MAXSIZE);
44 unsetenv(BUF_MAXTIME);
45 unsetenv(FLUSH_FULL);
46 unsetenv(HOST_STATE);
47 unsetenv(OUT_DIR);
48 unsetenv(MAX_FILES);
Nan Zhou042b5ba2021-06-18 09:32:45 -070049 unsetenv(STREAM_DST);
Artem Senicheve8837d52020-06-07 11:59:04 +030050 }
51};
52
53TEST_F(ConfigTest, Defaults)
54{
55 Config cfg;
56 EXPECT_STREQ(cfg.socketId, "");
Nan Zhou042b5ba2021-06-18 09:32:45 -070057 EXPECT_EQ(cfg.mode, Mode::bufferMode);
Artem Senicheve8837d52020-06-07 11:59:04 +030058 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 Zhou042b5ba2021-06-18 09:32:45 -070064 EXPECT_STREQ(cfg.streamDestination, "/run/rsyslog/console_input");
Artem Senicheve8837d52020-06-07 11:59:04 +030065}
66
Nan Zhou042b5ba2021-06-18 09:32:45 -070067TEST_F(ConfigTest, LoadInBufferMode)
Artem Senicheve8837d52020-06-07 11:59:04 +030068{
69 setenv(SOCKET_ID, "id123", 1);
Nan Zhou042b5ba2021-06-18 09:32:45 -070070 setenv(MODE, "buffer", 1);
Artem Senicheve8837d52020-06-07 11:59:04 +030071 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 Zhou042b5ba2021-06-18 09:32:45 -070080 EXPECT_EQ(cfg.mode, Mode::bufferMode);
Artem Senicheve8837d52020-06-07 11:59:04 +030081 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 Zhou042b5ba2021-06-18 09:32:45 -070087 // This should be default.
88 EXPECT_STREQ(cfg.streamDestination, "/run/rsyslog/console_input");
89}
90
91TEST_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 Senicheve8837d52020-06-07 11:59:04 +0300109}
110
111TEST_F(ConfigTest, InvalidNumeric)
112{
113 setenv(BUF_MAXSIZE, "-1234", 1);
Nan Zhou042b5ba2021-06-18 09:32:45 -0700114 EXPECT_THROW(Config(), std::invalid_argument);
Artem Senicheve8837d52020-06-07 11:59:04 +0300115}
116
117TEST_F(ConfigTest, InvalidBoolean)
118{
119 setenv(FLUSH_FULL, "invalid", 1);
Nan Zhou042b5ba2021-06-18 09:32:45 -0700120 EXPECT_THROW(Config(), std::invalid_argument);
121 setenv(FLUSH_FULL, "true", 1);
122 EXPECT_NO_THROW(Config());
Artem Senicheve8837d52020-06-07 11:59:04 +0300123}
124
Nan Zhou042b5ba2021-06-18 09:32:45 -0700125TEST_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
135TEST_F(ConfigTest, InvalidBufferModeConfig)
Artem Senicheve8837d52020-06-07 11:59:04 +0300136{
137 setenv(BUF_MAXSIZE, "0", 1);
138 setenv(BUF_MAXTIME, "0", 1);
139 setenv(FLUSH_FULL, "true", 1);
Nan Zhou042b5ba2021-06-18 09:32:45 -0700140 EXPECT_THROW(Config(), std::invalid_argument);
141}
142
143TEST_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 Senicheve8837d52020-06-07 11:59:04 +0300149}