blob: 53298c6329e5743ee0c553d24c9420d76a59bd54 [file] [log] [blame]
Deepak Kodihalli5ac1bde2018-08-30 05:38:44 -05001#include "remote_logging_tests.hpp"
Patrick Venturef18bf832018-10-26 18:14:00 -07002
Deepak Kodihalli5ac1bde2018-08-30 05:38:44 -05003#include <fstream>
Patrick Venture30047bf2018-11-01 18:52:15 -07004#include <string>
Deepak Kodihalli5ac1bde2018-08-30 05:38:44 -05005
6namespace phosphor
7{
Lei YUa1c43382021-04-15 20:26:25 +08008
9namespace rsyslog_config::internal
10{
11extern std::optional<std::pair<std::string, uint32_t>>
12 parseConfig(std::istream& ss);
13}
14
Deepak Kodihalli5ac1bde2018-08-30 05:38:44 -050015namespace logging
16{
17namespace test
18{
19
20std::string getConfig(const char* filePath)
21{
22 std::fstream stream(filePath, std::fstream::in);
23 std::string line;
Patrick Venture30047bf2018-11-01 18:52:15 -070024 std::getline(stream, line);
Deepak Kodihalli5ac1bde2018-08-30 05:38:44 -050025 return line;
26}
27
28TEST_F(TestRemoteLogging, testOnlyAddress)
29{
30 config->address("1.1.1.1");
Santosh Puranik0a0b5ea2019-05-22 05:25:30 -050031 EXPECT_EQ(getConfig(configFilePath.c_str()), "*.* ~");
Deepak Kodihalli5ac1bde2018-08-30 05:38:44 -050032}
33
34TEST_F(TestRemoteLogging, testOnlyPort)
35{
36 config->port(100);
Santosh Puranik0a0b5ea2019-05-22 05:25:30 -050037 EXPECT_EQ(getConfig(configFilePath.c_str()), "*.* ~");
Deepak Kodihalli5ac1bde2018-08-30 05:38:44 -050038}
39
40TEST_F(TestRemoteLogging, testGoodConfig)
41{
42 config->address("1.1.1.1");
43 config->port(100);
44 EXPECT_EQ(getConfig(configFilePath.c_str()), "*.* @@1.1.1.1:100");
45}
46
47TEST_F(TestRemoteLogging, testClearAddress)
48{
49 config->address("1.1.1.1");
50 config->port(100);
51 EXPECT_EQ(getConfig(configFilePath.c_str()), "*.* @@1.1.1.1:100");
52 config->address("");
Santosh Puranik0a0b5ea2019-05-22 05:25:30 -050053 EXPECT_EQ(getConfig(configFilePath.c_str()), "*.* ~");
Deepak Kodihalli5ac1bde2018-08-30 05:38:44 -050054}
55
56TEST_F(TestRemoteLogging, testClearPort)
57{
58 config->address("1.1.1.1");
59 config->port(100);
60 EXPECT_EQ(getConfig(configFilePath.c_str()), "*.* @@1.1.1.1:100");
61 config->port(0);
Santosh Puranik0a0b5ea2019-05-22 05:25:30 -050062 EXPECT_EQ(getConfig(configFilePath.c_str()), "*.* ~");
Deepak Kodihalli5ac1bde2018-08-30 05:38:44 -050063}
64
Lei YUa1c43382021-04-15 20:26:25 +080065TEST_F(TestRemoteLogging, testGoodIPv6Config)
66{
67 config->address("abcd:ef01::01");
68 config->port(50000);
69 EXPECT_EQ(getConfig(configFilePath.c_str()), "*.* @@[abcd:ef01::01]:50000");
70}
71
72TEST_F(TestRemoteLogging, parseConfigGoodIpv6)
73{
74 // A good case
75 std::string str = "*.* @@[abcd:ef01::01]:50000";
76 std::stringstream ss(str);
77 auto ret = phosphor::rsyslog_config::internal::parseConfig(ss);
78 EXPECT_TRUE(ret);
79 EXPECT_EQ(ret->first, "abcd:ef01::01");
80 EXPECT_EQ(ret->second, 50000);
81}
82
83TEST_F(TestRemoteLogging, parseConfigBadIpv6WithoutRightBracket)
84{
85 // Bad case: without ]
86 std::string str = "*.* @@[abcd:ef01::01:50000";
87 std::stringstream ss(str);
88 auto ret = phosphor::rsyslog_config::internal::parseConfig(ss);
89 EXPECT_FALSE(ret);
90}
91
92TEST_F(TestRemoteLogging, parseConfigBadIpv6WithoutLeftBracket)
93{
94 // Bad case: without [
95 std::string str = "*.* @@abcd:ef01::01]:50000";
96 std::stringstream ss(str);
97 auto ret = phosphor::rsyslog_config::internal::parseConfig(ss);
98 EXPECT_FALSE(ret);
99}
100
101TEST_F(TestRemoteLogging, parseConfigBadIpv6WithoutPort)
102{
103 // Bad case: without port
104 std::string str = "*.* @@[abcd:ef01::01]:";
105 std::stringstream ss(str);
106 auto ret = phosphor::rsyslog_config::internal::parseConfig(ss);
107 EXPECT_FALSE(ret);
108}
109
110TEST_F(TestRemoteLogging, parseConfigBadIpv6InvalidPort)
111{
112 // Bad case: without port
113 std::string str = "*.* @@[abcd:ef01::01]:xxx";
114 std::stringstream ss(str);
115 auto ret = phosphor::rsyslog_config::internal::parseConfig(ss);
116 EXPECT_FALSE(ret);
117}
118
119TEST_F(TestRemoteLogging, parseConfigBadIpv6WihtoutColon)
120{
121 // Bad case: invalid IPv6 address
122 std::string str = "*.* @@[abcd:ef01::01]";
123 std::stringstream ss(str);
124 auto ret = phosphor::rsyslog_config::internal::parseConfig(ss);
125 EXPECT_FALSE(ret);
126}
127
128TEST_F(TestRemoteLogging, parseConfigBadEmpty)
129{
130 // Bad case: invalid IPv6 address
131 std::string str = "";
132 std::stringstream ss(str);
133 auto ret = phosphor::rsyslog_config::internal::parseConfig(ss);
134 EXPECT_FALSE(ret);
135}
136
Patrick Venturef18bf832018-10-26 18:14:00 -0700137} // namespace test
138} // namespace logging
139} // namespace phosphor