blob: b48185af393771bb937036b2cc9b38871dc2c79b [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{
Ivan Mikhaylov22e86952023-06-09 17:54:03 +030011extern std::optional<
12 std::tuple<std::string, uint32_t, NetworkClient::TransportProtocol>>
Lei YUa1c43382021-04-15 20:26:25 +080013 parseConfig(std::istream& ss);
14}
15
Deepak Kodihalli5ac1bde2018-08-30 05:38:44 -050016namespace logging
17{
18namespace test
19{
20
21std::string getConfig(const char* filePath)
22{
23 std::fstream stream(filePath, std::fstream::in);
24 std::string line;
Patrick Venture30047bf2018-11-01 18:52:15 -070025 std::getline(stream, line);
Deepak Kodihalli5ac1bde2018-08-30 05:38:44 -050026 return line;
27}
28
29TEST_F(TestRemoteLogging, testOnlyAddress)
30{
31 config->address("1.1.1.1");
Paul Fertser445665a2022-03-17 14:45:42 +000032 EXPECT_EQ(getConfig(configFilePath.c_str()), "*.* /dev/null");
Deepak Kodihalli5ac1bde2018-08-30 05:38:44 -050033}
34
35TEST_F(TestRemoteLogging, testOnlyPort)
36{
37 config->port(100);
Paul Fertser445665a2022-03-17 14:45:42 +000038 EXPECT_EQ(getConfig(configFilePath.c_str()), "*.* /dev/null");
Deepak Kodihalli5ac1bde2018-08-30 05:38:44 -050039}
40
41TEST_F(TestRemoteLogging, testGoodConfig)
42{
43 config->address("1.1.1.1");
44 config->port(100);
45 EXPECT_EQ(getConfig(configFilePath.c_str()), "*.* @@1.1.1.1:100");
46}
47
48TEST_F(TestRemoteLogging, testClearAddress)
49{
50 config->address("1.1.1.1");
51 config->port(100);
52 EXPECT_EQ(getConfig(configFilePath.c_str()), "*.* @@1.1.1.1:100");
53 config->address("");
Paul Fertser445665a2022-03-17 14:45:42 +000054 EXPECT_EQ(getConfig(configFilePath.c_str()), "*.* /dev/null");
Deepak Kodihalli5ac1bde2018-08-30 05:38:44 -050055}
56
57TEST_F(TestRemoteLogging, testClearPort)
58{
59 config->address("1.1.1.1");
60 config->port(100);
61 EXPECT_EQ(getConfig(configFilePath.c_str()), "*.* @@1.1.1.1:100");
62 config->port(0);
Paul Fertser445665a2022-03-17 14:45:42 +000063 EXPECT_EQ(getConfig(configFilePath.c_str()), "*.* /dev/null");
Deepak Kodihalli5ac1bde2018-08-30 05:38:44 -050064}
65
Lei YUa1c43382021-04-15 20:26:25 +080066TEST_F(TestRemoteLogging, testGoodIPv6Config)
67{
68 config->address("abcd:ef01::01");
69 config->port(50000);
70 EXPECT_EQ(getConfig(configFilePath.c_str()), "*.* @@[abcd:ef01::01]:50000");
71}
72
73TEST_F(TestRemoteLogging, parseConfigGoodIpv6)
74{
75 // A good case
76 std::string str = "*.* @@[abcd:ef01::01]:50000";
77 std::stringstream ss(str);
78 auto ret = phosphor::rsyslog_config::internal::parseConfig(ss);
79 EXPECT_TRUE(ret);
Ivan Mikhaylov22e86952023-06-09 17:54:03 +030080 EXPECT_EQ(std::get<0>(*ret), "abcd:ef01::01");
81 EXPECT_EQ(std::get<1>(*ret), 50000);
Lei YUa1c43382021-04-15 20:26:25 +080082}
83
84TEST_F(TestRemoteLogging, parseConfigBadIpv6WithoutRightBracket)
85{
86 // Bad case: without ]
87 std::string str = "*.* @@[abcd:ef01::01:50000";
88 std::stringstream ss(str);
89 auto ret = phosphor::rsyslog_config::internal::parseConfig(ss);
90 EXPECT_FALSE(ret);
91}
92
93TEST_F(TestRemoteLogging, parseConfigBadIpv6WithoutLeftBracket)
94{
95 // Bad case: without [
96 std::string str = "*.* @@abcd:ef01::01]:50000";
97 std::stringstream ss(str);
98 auto ret = phosphor::rsyslog_config::internal::parseConfig(ss);
99 EXPECT_FALSE(ret);
100}
101
102TEST_F(TestRemoteLogging, parseConfigBadIpv6WithoutPort)
103{
104 // Bad case: without port
105 std::string str = "*.* @@[abcd:ef01::01]:";
106 std::stringstream ss(str);
107 auto ret = phosphor::rsyslog_config::internal::parseConfig(ss);
108 EXPECT_FALSE(ret);
109}
110
111TEST_F(TestRemoteLogging, parseConfigBadIpv6InvalidPort)
112{
113 // Bad case: without port
114 std::string str = "*.* @@[abcd:ef01::01]:xxx";
115 std::stringstream ss(str);
116 auto ret = phosphor::rsyslog_config::internal::parseConfig(ss);
117 EXPECT_FALSE(ret);
118}
119
120TEST_F(TestRemoteLogging, parseConfigBadIpv6WihtoutColon)
121{
122 // Bad case: invalid IPv6 address
123 std::string str = "*.* @@[abcd:ef01::01]";
124 std::stringstream ss(str);
125 auto ret = phosphor::rsyslog_config::internal::parseConfig(ss);
126 EXPECT_FALSE(ret);
127}
128
129TEST_F(TestRemoteLogging, parseConfigBadEmpty)
130{
131 // Bad case: invalid IPv6 address
132 std::string str = "";
133 std::stringstream ss(str);
134 auto ret = phosphor::rsyslog_config::internal::parseConfig(ss);
135 EXPECT_FALSE(ret);
136}
137
Ivan Mikhaylov22e86952023-06-09 17:54:03 +0300138TEST_F(TestRemoteLogging, parseConfigTCP)
139{
140 // A good case
141 std::string str = "*.* @@[abcd:ef01::01]:50000";
142 std::stringstream ss(str);
143 auto ret = phosphor::rsyslog_config::internal::parseConfig(ss);
144 EXPECT_TRUE(ret);
145 EXPECT_EQ(std::get<2>(*ret),
146 phosphor::rsyslog_config::NetworkClient::TransportProtocol::TCP);
147}
148
149TEST_F(TestRemoteLogging, parseConfigUdp)
150{
151 // A good case
152 std::string str = "*.* @[abcd:ef01::01]:50000";
153 std::stringstream ss(str);
154 auto ret = phosphor::rsyslog_config::internal::parseConfig(ss);
155 EXPECT_TRUE(ret);
156 EXPECT_EQ(std::get<2>(*ret),
157 phosphor::rsyslog_config::NetworkClient::TransportProtocol::UDP);
158}
159
160TEST_F(TestRemoteLogging, createUdpConfig)
161{
162 // A good case
163 config->address("abcd:ef01::01");
164 config->port(50000);
165 config->transportProtocol(
166 phosphor::rsyslog_config::NetworkClient::TransportProtocol::UDP);
167 EXPECT_EQ(getConfig(configFilePath.c_str()), "*.* @[abcd:ef01::01]:50000");
168}
169
Patrick Venturef18bf832018-10-26 18:14:00 -0700170} // namespace test
171} // namespace logging
172} // namespace phosphor