1 #include "remote_logging_tests.hpp"
2 
3 #include <fstream>
4 #include <string>
5 
6 namespace phosphor
7 {
8 
9 namespace rsyslog_config::internal
10 {
11 extern std::optional<
12     std::tuple<std::string, uint32_t, NetworkClient::TransportProtocol>>
13     parseConfig(std::istream& ss);
14 }
15 
16 namespace logging
17 {
18 namespace test
19 {
20 
21 std::string getConfig(const char* filePath)
22 {
23     std::fstream stream(filePath, std::fstream::in);
24     std::string line;
25     std::getline(stream, line);
26     return line;
27 }
28 
29 TEST_F(TestRemoteLogging, testOnlyAddress)
30 {
31     config->address("1.1.1.1");
32     EXPECT_EQ(getConfig(configFilePath.c_str()), "*.* /dev/null");
33 }
34 
35 TEST_F(TestRemoteLogging, testOnlyPort)
36 {
37     config->port(100);
38     EXPECT_EQ(getConfig(configFilePath.c_str()), "*.* /dev/null");
39 }
40 
41 TEST_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 
48 TEST_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("");
54     EXPECT_EQ(getConfig(configFilePath.c_str()), "*.* /dev/null");
55 }
56 
57 TEST_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);
63     EXPECT_EQ(getConfig(configFilePath.c_str()), "*.* /dev/null");
64 }
65 
66 TEST_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 
73 TEST_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);
80     EXPECT_EQ(std::get<0>(*ret), "abcd:ef01::01");
81     EXPECT_EQ(std::get<1>(*ret), 50000);
82 }
83 
84 TEST_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 
93 TEST_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 
102 TEST_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 
111 TEST_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 
120 TEST_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 
129 TEST_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 
138 TEST_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 
149 TEST_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 
160 TEST_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 
170 } // namespace test
171 } // namespace logging
172 } // namespace phosphor
173