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 | { |
| 11 | extern std::optional<std::pair<std::string, uint32_t>> |
| 12 | parseConfig(std::istream& ss); |
| 13 | } |
| 14 | |
Deepak Kodihalli | 5ac1bde | 2018-08-30 05:38:44 -0500 | [diff] [blame] | 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; |
Patrick Venture | 30047bf | 2018-11-01 18:52:15 -0700 | [diff] [blame] | 24 | std::getline(stream, line); |
Deepak Kodihalli | 5ac1bde | 2018-08-30 05:38:44 -0500 | [diff] [blame] | 25 | return line; |
| 26 | } |
| 27 | |
| 28 | TEST_F(TestRemoteLogging, testOnlyAddress) |
| 29 | { |
| 30 | config->address("1.1.1.1"); |
Paul Fertser | 445665a | 2022-03-17 14:45:42 +0000 | [diff] [blame] | 31 | EXPECT_EQ(getConfig(configFilePath.c_str()), "*.* /dev/null"); |
Deepak Kodihalli | 5ac1bde | 2018-08-30 05:38:44 -0500 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | TEST_F(TestRemoteLogging, testOnlyPort) |
| 35 | { |
| 36 | config->port(100); |
Paul Fertser | 445665a | 2022-03-17 14:45:42 +0000 | [diff] [blame] | 37 | EXPECT_EQ(getConfig(configFilePath.c_str()), "*.* /dev/null"); |
Deepak Kodihalli | 5ac1bde | 2018-08-30 05:38:44 -0500 | [diff] [blame] | 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(""); |
Paul Fertser | 445665a | 2022-03-17 14:45:42 +0000 | [diff] [blame] | 53 | EXPECT_EQ(getConfig(configFilePath.c_str()), "*.* /dev/null"); |
Deepak Kodihalli | 5ac1bde | 2018-08-30 05:38:44 -0500 | [diff] [blame] | 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); |
Paul Fertser | 445665a | 2022-03-17 14:45:42 +0000 | [diff] [blame] | 62 | EXPECT_EQ(getConfig(configFilePath.c_str()), "*.* /dev/null"); |
Deepak Kodihalli | 5ac1bde | 2018-08-30 05:38:44 -0500 | [diff] [blame] | 63 | } |
| 64 | |
Lei YU | a1c4338 | 2021-04-15 20:26:25 +0800 | [diff] [blame] | 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 | |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 137 | } // namespace test |
| 138 | } // namespace logging |
| 139 | } // namespace phosphor |