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<std::pair<std::string, uint32_t>>
12     parseConfig(std::istream& ss);
13 }
14 
15 namespace logging
16 {
17 namespace test
18 {
19 
20 std::string getConfig(const char* filePath)
21 {
22     std::fstream stream(filePath, std::fstream::in);
23     std::string line;
24     std::getline(stream, line);
25     return line;
26 }
27 
28 TEST_F(TestRemoteLogging, testOnlyAddress)
29 {
30     config->address("1.1.1.1");
31     EXPECT_EQ(getConfig(configFilePath.c_str()), "*.* /dev/null");
32 }
33 
34 TEST_F(TestRemoteLogging, testOnlyPort)
35 {
36     config->port(100);
37     EXPECT_EQ(getConfig(configFilePath.c_str()), "*.* /dev/null");
38 }
39 
40 TEST_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 
47 TEST_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("");
53     EXPECT_EQ(getConfig(configFilePath.c_str()), "*.* /dev/null");
54 }
55 
56 TEST_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);
62     EXPECT_EQ(getConfig(configFilePath.c_str()), "*.* /dev/null");
63 }
64 
65 TEST_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 
72 TEST_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 
83 TEST_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 
92 TEST_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 
101 TEST_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 
110 TEST_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 
119 TEST_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 
128 TEST_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 
137 } // namespace test
138 } // namespace logging
139 } // namespace phosphor
140