Ratan Gupta | 213517b | 2018-04-28 13:41:09 +0530 | [diff] [blame] | 1 | #include <gtest/gtest.h> |
| 2 | #include <netinet/in.h> |
| 3 | #include "snmp_util.hpp" |
| 4 | #include <xyz/openbmc_project/Common/error.hpp> |
| 5 | |
| 6 | namespace phosphor |
| 7 | { |
| 8 | namespace network |
| 9 | { |
| 10 | namespace snmp |
| 11 | { |
| 12 | |
| 13 | using InternalFailure = |
| 14 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
| 15 | |
| 16 | TEST(TestUtil, IpValidation) |
| 17 | { |
| 18 | // valid IPv4 address |
| 19 | std::string ipaddress = "0.0.0.0"; |
| 20 | EXPECT_EQ(ipaddress, resolveAddress(ipaddress)); |
| 21 | |
| 22 | ipaddress = "9.3.185.83"; |
| 23 | EXPECT_EQ(ipaddress, resolveAddress(ipaddress)); |
| 24 | |
| 25 | // Invalid IPv4 address |
| 26 | ipaddress = "9.3.185.a"; |
| 27 | EXPECT_THROW(resolveAddress(ipaddress), InternalFailure); |
| 28 | |
| 29 | ipaddress = "9.3.a.83"; |
| 30 | EXPECT_THROW(resolveAddress(ipaddress), InternalFailure); |
| 31 | |
| 32 | ipaddress = "x.x.x.x"; |
| 33 | EXPECT_THROW(resolveAddress(ipaddress), InternalFailure); |
| 34 | |
| 35 | // valid IPv6 address |
| 36 | ipaddress = "0:0:0:0:0:0:0:0"; |
| 37 | EXPECT_EQ("::", resolveAddress(ipaddress)); |
| 38 | |
| 39 | ipaddress = "1:0:0:0:0:0:0:8"; |
| 40 | EXPECT_EQ("1::8", resolveAddress(ipaddress)); |
| 41 | |
| 42 | ipaddress = "1::8"; |
| 43 | EXPECT_EQ(ipaddress, resolveAddress(ipaddress)); |
| 44 | |
| 45 | ipaddress = "0:0:0:0:0:FFFF:204.152.189.116"; |
| 46 | EXPECT_EQ("::ffff:204.152.189.116", resolveAddress(ipaddress)); |
| 47 | |
| 48 | ipaddress = "::ffff:204.152.189.116"; |
| 49 | EXPECT_EQ(ipaddress, resolveAddress(ipaddress)); |
| 50 | |
| 51 | ipaddress = "a:0:0:0:0:FFFF:204.152.189.116"; |
| 52 | EXPECT_EQ("a::ffff:cc98:bd74", resolveAddress(ipaddress)); |
| 53 | |
| 54 | // Invalid IPv6 address |
| 55 | ipaddress = "abcd::xyz::"; |
| 56 | EXPECT_THROW(resolveAddress(ipaddress), InternalFailure); |
| 57 | |
| 58 | // resolve the local host |
| 59 | ipaddress = "localhost"; |
| 60 | auto isLocal = false; |
| 61 | auto addr = resolveAddress(ipaddress); |
| 62 | if (addr == "127.0.0.1" || addr == "::1") |
| 63 | { |
| 64 | isLocal = true; |
| 65 | } |
| 66 | EXPECT_TRUE(isLocal); |
| 67 | } |
| 68 | |
Ratan Gupta | aea53d0 | 2018-09-06 17:56:59 +0530 | [diff] [blame^] | 69 | } // namespace snmp |
| 70 | } // namespace network |
Ratan Gupta | 213517b | 2018-04-28 13:41:09 +0530 | [diff] [blame] | 71 | } // namespace phosphor |