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