Deepak Kodihalli | 5ac1bde | 2018-08-30 05:38:44 -0500 | [diff] [blame] | 1 | #include "remote_logging_tests.hpp" |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 2 | |
Deepak Kodihalli | 5ac1bde | 2018-08-30 05:38:44 -0500 | [diff] [blame] | 3 | #include <fstream> |
Patrick Venture | 30047bf | 2018-11-01 18:52:15 -0700 | [diff] [blame] | 4 | #include <string> |
Deepak Kodihalli | 5ac1bde | 2018-08-30 05:38:44 -0500 | [diff] [blame] | 5 | |
| 6 | namespace phosphor |
| 7 | { |
Lei YU | a1c4338 | 2021-04-15 20:26:25 +0800 | [diff] [blame] | 8 | |
| 9 | namespace rsyslog_config::internal |
| 10 | { |
Ivan Mikhaylov | 22e8695 | 2023-06-09 17:54:03 +0300 | [diff] [blame] | 11 | extern std::optional< |
| 12 | std::tuple<std::string, uint32_t, NetworkClient::TransportProtocol>> |
Lei YU | a1c4338 | 2021-04-15 20:26:25 +0800 | [diff] [blame] | 13 | parseConfig(std::istream& ss); |
| 14 | } |
| 15 | |
Deepak Kodihalli | 5ac1bde | 2018-08-30 05:38:44 -0500 | [diff] [blame] | 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; |
Patrick Venture | 30047bf | 2018-11-01 18:52:15 -0700 | [diff] [blame] | 25 | std::getline(stream, line); |
Deepak Kodihalli | 5ac1bde | 2018-08-30 05:38:44 -0500 | [diff] [blame] | 26 | return line; |
| 27 | } |
| 28 | |
| 29 | TEST_F(TestRemoteLogging, testOnlyAddress) |
| 30 | { |
| 31 | config->address("1.1.1.1"); |
Paul Fertser | 445665a | 2022-03-17 14:45:42 +0000 | [diff] [blame] | 32 | EXPECT_EQ(getConfig(configFilePath.c_str()), "*.* /dev/null"); |
Deepak Kodihalli | 5ac1bde | 2018-08-30 05:38:44 -0500 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | TEST_F(TestRemoteLogging, testOnlyPort) |
| 36 | { |
| 37 | config->port(100); |
Paul Fertser | 445665a | 2022-03-17 14:45:42 +0000 | [diff] [blame] | 38 | EXPECT_EQ(getConfig(configFilePath.c_str()), "*.* /dev/null"); |
Deepak Kodihalli | 5ac1bde | 2018-08-30 05:38:44 -0500 | [diff] [blame] | 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(""); |
Paul Fertser | 445665a | 2022-03-17 14:45:42 +0000 | [diff] [blame] | 54 | EXPECT_EQ(getConfig(configFilePath.c_str()), "*.* /dev/null"); |
Deepak Kodihalli | 5ac1bde | 2018-08-30 05:38:44 -0500 | [diff] [blame] | 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); |
Paul Fertser | 445665a | 2022-03-17 14:45:42 +0000 | [diff] [blame] | 63 | EXPECT_EQ(getConfig(configFilePath.c_str()), "*.* /dev/null"); |
Deepak Kodihalli | 5ac1bde | 2018-08-30 05:38:44 -0500 | [diff] [blame] | 64 | } |
| 65 | |
Lei YU | a1c4338 | 2021-04-15 20:26:25 +0800 | [diff] [blame] | 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); |
Ivan Mikhaylov | 22e8695 | 2023-06-09 17:54:03 +0300 | [diff] [blame] | 80 | EXPECT_EQ(std::get<0>(*ret), "abcd:ef01::01"); |
| 81 | EXPECT_EQ(std::get<1>(*ret), 50000); |
Lei YU | a1c4338 | 2021-04-15 20:26:25 +0800 | [diff] [blame] | 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 | |
Ivan Mikhaylov | 22e8695 | 2023-06-09 17:54:03 +0300 | [diff] [blame] | 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 | |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 170 | } // namespace test |
| 171 | } // namespace logging |
| 172 | } // namespace phosphor |