diff options
| author | Gunnar Mills <gmills@us.ibm.com> | 2018-09-14 14:42:34 -0500 |
|---|---|---|
| committer | Ratan K Gupta <ratagupt@linux.vnet.ibm.com> | 2018-09-15 00:47:14 -0400 |
| commit | 57d9c5066e2fc28d3e89b1bd9fd3ee98e27c4c48 (patch) | |
| tree | f232adeab7b6a89cd392d4300be70451d335d14f /test | |
| parent | ce26282763be6a9d7fa21fcc93d5da8682d3a021 (diff) | |
| download | phosphor-networkd-57d9c5066e2fc28d3e89b1bd9fd3ee98e27c4c48.tar.gz phosphor-networkd-57d9c5066e2fc28d3e89b1bd9fd3ee98e27c4c48.zip | |
clang-format: Update to match docs repo
Update the .clang-format file and run clang-format-6.0.
This .clang-format matches the example one in
https://github.com/openbmc/docs/blob/master/cpp-style-and-conventions.md#clang-formatting
Change-Id: Ia331c9a5b040e1a3c45a0ebf1b8d776d93b05ae5
Signed-off-by: Gunnar Mills <gmills@us.ibm.com>
Diffstat (limited to 'test')
| -rw-r--r-- | test/mock_syscall.cpp | 33 | ||||
| -rw-r--r-- | test/test_config_parser.cpp | 52 | ||||
| -rw-r--r-- | test/test_dns_updater.cpp | 65 | ||||
| -rw-r--r-- | test/test_ethernet_interface.cpp | 208 | ||||
| -rw-r--r-- | test/test_network_manager.cpp | 90 | ||||
| -rw-r--r-- | test/test_rtnetlink.cpp | 116 | ||||
| -rw-r--r-- | test/test_util.cpp | 54 | ||||
| -rw-r--r-- | test/test_vlan_interface.cpp | 157 | ||||
| -rw-r--r-- | test/test_watch.cpp | 86 |
9 files changed, 405 insertions, 456 deletions
diff --git a/test/mock_syscall.cpp b/test/mock_syscall.cpp index 173b67f..28ebed2 100644 --- a/test/mock_syscall.cpp +++ b/test/mock_syscall.cpp @@ -1,43 +1,42 @@ -#include <sys/types.h> -#include <sys/socket.h> - -#include <net/if.h> -#include <netinet/in.h> #include <arpa/inet.h> - #include <ifaddrs.h> +#include <net/if.h> +#include <netinet/in.h> +#include <sys/socket.h> +#include <sys/types.h> #define MAX_IFADDRS 5 int debugging = false; /* Data for mocking getifaddrs */ -struct ifaddr_storage { +struct ifaddr_storage +{ struct ifaddrs ifaddr; struct sockaddr_storage addr; struct sockaddr_storage mask; struct sockaddr_storage bcast; } mock_ifaddr_storage[MAX_IFADDRS]; -struct ifaddrs *mock_ifaddrs = nullptr; +struct ifaddrs* mock_ifaddrs = nullptr; int ifaddr_count = 0; /* Stub library functions */ -void freeifaddrs(ifaddrs *ifp) +void freeifaddrs(ifaddrs* ifp) { - return ; + return; } void mock_addIP(const char* name, const char* addr, const char* mask, unsigned int flags) { - struct ifaddrs *ifaddr = &mock_ifaddr_storage[ifaddr_count].ifaddr; + struct ifaddrs* ifaddr = &mock_ifaddr_storage[ifaddr_count].ifaddr; - struct sockaddr_in *in = reinterpret_cast<sockaddr_in*> - (&mock_ifaddr_storage[ifaddr_count].addr); - struct sockaddr_in *mask_in = reinterpret_cast<sockaddr_in*> - (&mock_ifaddr_storage[ifaddr_count].mask); + struct sockaddr_in* in = + reinterpret_cast<sockaddr_in*>(&mock_ifaddr_storage[ifaddr_count].addr); + struct sockaddr_in* mask_in = + reinterpret_cast<sockaddr_in*>(&mock_ifaddr_storage[ifaddr_count].mask); in->sin_family = AF_INET; in->sin_port = 0; @@ -58,14 +57,12 @@ void mock_addIP(const char* name, const char* addr, const char* mask, mock_ifaddr_storage[ifaddr_count - 1].ifaddr.ifa_next = ifaddr; ifaddr_count++; mock_ifaddrs = &mock_ifaddr_storage[0].ifaddr; - } -int getifaddrs(ifaddrs **ifap) +int getifaddrs(ifaddrs** ifap) { *ifap = mock_ifaddrs; if (mock_ifaddrs == nullptr) return -1; return (0); } - diff --git a/test/test_config_parser.cpp b/test/test_config_parser.cpp index 1c902d2..99effad 100644 --- a/test/test_config_parser.cpp +++ b/test/test_config_parser.cpp @@ -1,14 +1,14 @@ -#include <gtest/gtest.h> +#include "config.h" #include "config_parser.hpp" - #include "xyz/openbmc_project/Common/error.hpp" -#include <phosphor-logging/elog-errors.hpp> -#include "config.h" #include <exception> -#include <stdexcept> #include <fstream> +#include <phosphor-logging/elog-errors.hpp> +#include <stdexcept> + +#include <gtest/gtest.h> namespace phosphor { @@ -17,31 +17,31 @@ namespace network class TestConfigParser : public testing::Test { - public: - config::Parser parser; - TestConfigParser() - { - remove("/tmp/eth0.network"); - std::ofstream filestream("/tmp/eth0.network"); + public: + config::Parser parser; + TestConfigParser() + { + remove("/tmp/eth0.network"); + std::ofstream filestream("/tmp/eth0.network"); - filestream << "[Match]\nName=eth0\n" << - "[Network]\nDHCP=true\n[DHCP]\nClientIdentifier= mac\n"; - filestream.close(); - parser.setFile("/tmp/eth0.network"); - } + filestream << "[Match]\nName=eth0\n" + << "[Network]\nDHCP=true\n[DHCP]\nClientIdentifier= mac\n"; + filestream.close(); + parser.setFile("/tmp/eth0.network"); + } - bool isValueFound(const std::vector<std::string>& values, - const std::string& expectedValue) + bool isValueFound(const std::vector<std::string>& values, + const std::string& expectedValue) + { + for (const auto& value : values) { - for (const auto& value : values) + if (expectedValue == value) { - if (expectedValue == value) - { - return true; - } + return true; } - return false; } + return false; + } }; TEST_F(TestConfigParser, ReadConfigDataFromFile) @@ -82,5 +82,5 @@ TEST_F(TestConfigParser, KeyNotFound) remove("/tmp/eth0.network"); } -}//namespace network -}//namespace phosphor +} // namespace network +} // namespace phosphor diff --git a/test/test_dns_updater.cpp b/test/test_dns_updater.cpp index 8697d4c..2a425c0 100644 --- a/test/test_dns_updater.cpp +++ b/test/test_dns_updater.cpp @@ -1,13 +1,14 @@ #include "dns_updater.hpp" -#include <gtest/gtest.h> - -#include <fstream> #include <experimental/filesystem> +#include <fstream> + +#include <gtest/gtest.h> static constexpr auto IN_FILE = "/tmp/" __BASE_FILE__ "netif_state"; static constexpr auto OUT_FILE = "/tmp/" __BASE_FILE__ "resolv.conf"; -static constexpr auto COMPARE_FILE = "/tmp/" __BASE_FILE__ "resolv_compare.conf"; +static constexpr auto COMPARE_FILE = + "/tmp/" __BASE_FILE__ "resolv_compare.conf"; static constexpr auto DNS_ENTRY_1 = "DNS=1.2.3.4\n"; static constexpr auto DNS_ENTRY_2 = "DNS=5.6.7.8\n"; @@ -15,38 +16,38 @@ namespace fs = std::experimental::filesystem; class DnsUpdateTest : public ::testing::Test { - public: - // Gets called as part of each TEST_F construction - DnsUpdateTest() - { - // Create a file containing DNS entries like in netif/state - std::ofstream file(IN_FILE); - file << DNS_ENTRY_1; - file << DNS_ENTRY_2; + public: + // Gets called as part of each TEST_F construction + DnsUpdateTest() + { + // Create a file containing DNS entries like in netif/state + std::ofstream file(IN_FILE); + file << DNS_ENTRY_1; + file << DNS_ENTRY_2; - // Create a file to compare the results against - std::ofstream compare(COMPARE_FILE); - compare << "### Generated by phosphor-networkd ###\n"; - compare << "nameserver 1.2.3.4\n"; - compare << "nameserver 5.6.7.8\n"; - } + // Create a file to compare the results against + std::ofstream compare(COMPARE_FILE); + compare << "### Generated by phosphor-networkd ###\n"; + compare << "nameserver 1.2.3.4\n"; + compare << "nameserver 5.6.7.8\n"; + } - // Gets called as part of each TEST_F destruction - ~DnsUpdateTest() + // Gets called as part of each TEST_F destruction + ~DnsUpdateTest() + { + if (fs::exists(IN_FILE)) + { + fs::remove(IN_FILE); + } + if (fs::exists(OUT_FILE)) + { + fs::remove(OUT_FILE); + } + if (fs::exists(COMPARE_FILE)) { - if (fs::exists(IN_FILE)) - { - fs::remove(IN_FILE); - } - if (fs::exists(OUT_FILE)) - { - fs::remove(OUT_FILE); - } - if (fs::exists(COMPARE_FILE)) - { - fs::remove(COMPARE_FILE); - } + fs::remove(COMPARE_FILE); } + } }; /** @brief Makes outfile is updated with right contents diff --git a/test/test_ethernet_interface.cpp b/test/test_ethernet_interface.cpp index 2181006..1b284a2 100644 --- a/test/test_ethernet_interface.cpp +++ b/test/test_ethernet_interface.cpp @@ -1,10 +1,7 @@ #include "config_parser.hpp" -#include "network_manager.hpp" -#include "mock_syscall.hpp" #include "ipaddress.hpp" - -#include <gtest/gtest.h> -#include <sdbusplus/bus.hpp> +#include "mock_syscall.hpp" +#include "network_manager.hpp" #include <arpa/inet.h> #include <net/if.h> @@ -13,6 +10,9 @@ #include <exception> #include <fstream> +#include <sdbusplus/bus.hpp> + +#include <gtest/gtest.h> namespace phosphor { @@ -21,116 +21,103 @@ namespace network class TestEthernetInterface : public testing::Test { - public: - - sdbusplus::bus::bus bus; - Manager manager; - EthernetInterface interface; - std::string confDir; - TestEthernetInterface() - : bus(sdbusplus::bus::new_default()), - manager(bus, "/xyz/openbmc_test/network", "/tmp/"), - interface(bus, "/xyz/openbmc_test/network/test0", false, manager) - - { - setConfDir(); - } - - void setConfDir() - { - char tmp[] = "/tmp/EthernetInterface.XXXXXX"; - confDir = mkdtemp(tmp); - manager.setConfDir(confDir); - } - - ~TestEthernetInterface() + public: + sdbusplus::bus::bus bus; + Manager manager; + EthernetInterface interface; + std::string confDir; + TestEthernetInterface() : + bus(sdbusplus::bus::new_default()), + manager(bus, "/xyz/openbmc_test/network", "/tmp/"), + interface(bus, "/xyz/openbmc_test/network/test0", false, manager) + + { + setConfDir(); + } + + void setConfDir() + { + char tmp[] = "/tmp/EthernetInterface.XXXXXX"; + confDir = mkdtemp(tmp); + manager.setConfDir(confDir); + } + + ~TestEthernetInterface() + { + if (confDir != "") { - if(confDir != "") - { - fs::remove_all(confDir); - } + fs::remove_all(confDir); } + } + int countIPObjects() + { + return interface.getAddresses().size(); + } - int countIPObjects() + bool isIPObjectExist(const std::string& ipaddress) + { + auto address = interface.getAddresses().find(ipaddress); + if (address == interface.getAddresses().end()) { - return interface.getAddresses().size(); + return false; } + return true; + } - bool isIPObjectExist(const std::string& ipaddress) + bool deleteIPObject(const std::string& ipaddress) + { + auto address = interface.getAddresses().find(ipaddress); + if (address == interface.getAddresses().end()) { - auto address = interface.getAddresses().find(ipaddress); - if (address == interface.getAddresses().end()) - { - return false; - } - return true; - - } - - bool deleteIPObject(const std::string& ipaddress) - { - auto address = interface.getAddresses().find(ipaddress); - if (address == interface.getAddresses().end()) - { - return false; - } - address->second->delete_(); - return true; - } - - std::string getObjectPath(const std::string& ipaddress, - uint8_t subnetMask, - const std::string& gateway) - { - IP::Protocol addressType = IP::Protocol::IPv4; - - return interface.generateObjectPath(addressType, - ipaddress, - subnetMask, - gateway); - } - - void createIPObject(IP::Protocol addressType, - const std::string& ipaddress, - uint8_t subnetMask, - const std::string& gateway) - { - interface.iP(addressType, - ipaddress, - subnetMask, - gateway - ); - + return false; } - - // Validates if the DNS entries have been correctly processed - void validateResolvFile(ServerList values) + address->second->delete_(); + return true; + } + + std::string getObjectPath(const std::string& ipaddress, uint8_t subnetMask, + const std::string& gateway) + { + IP::Protocol addressType = IP::Protocol::IPv4; + + return interface.generateObjectPath(addressType, ipaddress, subnetMask, + gateway); + } + + void createIPObject(IP::Protocol addressType, const std::string& ipaddress, + uint8_t subnetMask, const std::string& gateway) + { + interface.iP(addressType, ipaddress, subnetMask, gateway); + } + + // Validates if the DNS entries have been correctly processed + void validateResolvFile(ServerList values) + { + // Check whether the entries has been written to resolv.conf + fs::path resolvFile = confDir; + resolvFile /= "resolv.conf"; + + // Passed in "value" is what is read from the config file + interface.writeDNSEntries(values, resolvFile); + std::string expectedServers = + "### Generated manually via dbus settings ###"; + expectedServers += + "nameserver 9.1.1.1nameserver 9.2.2.2nameserver 9.3.3.3"; + + std::string actualServers{}; + std::fstream stream(resolvFile.string().c_str(), std::fstream::in); + for (std::string line; std::getline(stream, line);) { - // Check whether the entries has been written to resolv.conf - fs::path resolvFile = confDir; - resolvFile /= "resolv.conf"; - - // Passed in "value" is what is read from the config file - interface.writeDNSEntries(values, resolvFile); - std::string expectedServers = "### Generated manually via dbus settings ###"; - expectedServers += - "nameserver 9.1.1.1nameserver 9.2.2.2nameserver 9.3.3.3"; - - std::string actualServers{}; - std::fstream stream(resolvFile.string().c_str(), std::fstream::in); - for (std::string line; std::getline(stream, line);) - { - actualServers += line; - } - EXPECT_EQ(expectedServers , actualServers); + actualServers += line; } + EXPECT_EQ(expectedServers, actualServers); + } }; TEST_F(TestEthernetInterface, NoIPaddress) { EXPECT_EQ(countIPObjects(), 0); - } TEST_F(TestEthernetInterface, AddIPAddress) @@ -138,7 +125,6 @@ TEST_F(TestEthernetInterface, AddIPAddress) IP::Protocol addressType = IP::Protocol::IPv4; createIPObject(addressType, "10.10.10.10", 16, "10.10.10.1"); EXPECT_EQ(true, isIPObjectExist("10.10.10.10")); - } TEST_F(TestEthernetInterface, AddMultipleAddress) @@ -148,7 +134,6 @@ TEST_F(TestEthernetInterface, AddMultipleAddress) createIPObject(addressType, "20.20.20.20", 16, "20.20.20.1"); EXPECT_EQ(true, isIPObjectExist("10.10.10.10")); EXPECT_EQ(true, isIPObjectExist("20.20.20.20")); - } TEST_F(TestEthernetInterface, DeleteIPAddress) @@ -159,7 +144,6 @@ TEST_F(TestEthernetInterface, DeleteIPAddress) deleteIPObject("10.10.10.10"); EXPECT_EQ(false, isIPObjectExist("10.10.10.10")); EXPECT_EQ(true, isIPObjectExist("20.20.20.20")); - } TEST_F(TestEthernetInterface, DeleteInvalidIPAddress) @@ -180,19 +164,15 @@ TEST_F(TestEthernetInterface, CheckObjectPath) hashString += std::to_string(prefix); hashString += gateway; - - hexId << std::hex << ((std::hash<std::string> {}( - hashString)) & 0xFFFFFFFF); + hexId << std::hex << ((std::hash<std::string>{}(hashString)) & 0xFFFFFFFF); expectedObjectPath += hexId.str(); - EXPECT_EQ(expectedObjectPath, getObjectPath(ipaddress, - prefix, - gateway)); + EXPECT_EQ(expectedObjectPath, getObjectPath(ipaddress, prefix, gateway)); } TEST_F(TestEthernetInterface, addNameServers) { - ServerList servers = {"9.1.1.1","9.2.2.2","9.3.3.3"}; + ServerList servers = {"9.1.1.1", "9.2.2.2", "9.3.3.3"}; interface.nameservers(servers); fs::path filePath = confDir; filePath /= "00-bmc-test0.network"; @@ -200,14 +180,14 @@ TEST_F(TestEthernetInterface, addNameServers) config::ReturnCode rc = config::ReturnCode::SUCCESS; config::ValueList values; std::tie(rc, values) = parser.getValues("Network", "DNS"); - EXPECT_EQ(servers , values); + EXPECT_EQ(servers, values); validateResolvFile(values); } TEST_F(TestEthernetInterface, addNTPServers) { - ServerList servers = {"10.1.1.1","10.2.2.2","10.3.3.3"}; + ServerList servers = {"10.1.1.1", "10.2.2.2", "10.3.3.3"}; interface.nTPServers(servers); fs::path filePath = confDir; filePath /= "00-bmc-test0.network"; @@ -215,8 +195,8 @@ TEST_F(TestEthernetInterface, addNTPServers) config::ReturnCode rc = config::ReturnCode::SUCCESS; config::ValueList values; std::tie(rc, values) = parser.getValues("Network", "NTP"); - EXPECT_EQ(servers , values); + EXPECT_EQ(servers, values); } -}// namespce network -}// namespace phosphor +} // namespace network +} // namespace phosphor diff --git a/test/test_network_manager.cpp b/test/test_network_manager.cpp index 2776bcf..1c0a116 100644 --- a/test/test_network_manager.cpp +++ b/test/test_network_manager.cpp @@ -1,20 +1,19 @@ -#include "network_manager.hpp" #include "mock_syscall.hpp" - -#include "xyz/openbmc_project/Common/error.hpp" -#include <phosphor-logging/elog-errors.hpp> +#include "network_manager.hpp" #include "timer.hpp" +#include "xyz/openbmc_project/Common/error.hpp" -#include <gtest/gtest.h> -#include <sdbusplus/bus.hpp> - +#include <arpa/inet.h> #include <net/if.h> #include <netinet/in.h> -#include <arpa/inet.h> #include <stdlib.h> #include <exception> #include <experimental/filesystem> +#include <phosphor-logging/elog-errors.hpp> +#include <sdbusplus/bus.hpp> + +#include <gtest/gtest.h> namespace phosphor { @@ -25,49 +24,48 @@ namespace fs = std::experimental::filesystem; class TestNetworkManager : public testing::Test { - public: - - sdbusplus::bus::bus bus; - Manager manager; - std::string confDir; - TestNetworkManager() - : bus(sdbusplus::bus::new_default()), - manager(bus, "/xyz/openbmc_test/abc", "/tmp") - { - setConfDir(); - } + public: + sdbusplus::bus::bus bus; + Manager manager; + std::string confDir; + TestNetworkManager() : + bus(sdbusplus::bus::new_default()), + manager(bus, "/xyz/openbmc_test/abc", "/tmp") + { + setConfDir(); + } - ~TestNetworkManager() + ~TestNetworkManager() + { + if (confDir != "") { - if(confDir != "") - { - fs::remove_all(confDir); - } + fs::remove_all(confDir); } + } - void setConfDir() - { - char tmp[] = "/tmp/NetworkManager.XXXXXX"; - confDir = mkdtemp(tmp); - manager.setConfDir(confDir); - } + void setConfDir() + { + char tmp[] = "/tmp/NetworkManager.XXXXXX"; + confDir = mkdtemp(tmp); + manager.setConfDir(confDir); + } - void createInterfaces() - { - manager.createInterfaces(); - } + void createInterfaces() + { + manager.createInterfaces(); + } - int getSize() - { - return manager.interfaces.size(); - } + int getSize() + { + return manager.interfaces.size(); + } - bool isInterfaceAdded(std::string intf) - { - return manager.interfaces.find(intf) != manager.interfaces.end() ? - true : - false; - } + bool isInterfaceAdded(std::string intf) + { + return manager.interfaces.find(intf) != manager.interfaces.end() + ? true + : false; + } }; // getifaddrs will not return any interface @@ -130,5 +128,5 @@ TEST_F(TestNetworkManager, WithMultipleInterfaces) } } -}// namespce network -}// namespace phosphor +} // namespace network +} // namespace phosphor diff --git a/test/test_rtnetlink.cpp b/test/test_rtnetlink.cpp index 203019d..4be3743 100644 --- a/test/test_rtnetlink.cpp +++ b/test/test_rtnetlink.cpp @@ -1,16 +1,15 @@ -#include "types.hpp" -#include "rtnetlink_server.hpp" -#include "network_manager.hpp" #include "mock_syscall.hpp" +#include "network_manager.hpp" +#include "rtnetlink_server.hpp" #include "timer.hpp" #include "types.hpp" +#include <linux/rtnetlink.h> +#include <net/if.h> -#include <gtest/gtest.h> #include <sdbusplus/bus.hpp> -#include <net/if.h> -#include <linux/rtnetlink.h> +#include <gtest/gtest.h> namespace phosphor { @@ -35,78 +34,68 @@ void refreshObjects() void initializeTimers() { - std::function<void()> refreshFunc( - std::bind(&refreshObjects)); - - refreshObjectTimer = - std::make_unique<Timer>(refreshFunc); + std::function<void()> refreshFunc(std::bind(&refreshObjects)); + refreshObjectTimer = std::make_unique<Timer>(refreshFunc); } class TestRtNetlink : public testing::Test { - public: - std::string confDir; - phosphor::Descriptor smartSock; - + public: + std::string confDir; + phosphor::Descriptor smartSock; + TestRtNetlink() + { + manager = + std::make_unique<Manager>(bus, "/xyz/openbmc_test/bcd", "/tmp"); + sd_event* events; + sd_event_default(&events); + eventPtr.reset(events); + events = nullptr; + setConfDir(); + initializeTimers(); + createNetLinkSocket(); + bus.attach_event(eventPtr.get(), SD_EVENT_PRIORITY_NORMAL); + rtnetlink::Server svr(eventPtr, smartSock); + } - TestRtNetlink() - { - manager = - std::make_unique<Manager>(bus, - "/xyz/openbmc_test/bcd", - "/tmp"); - sd_event* events; - sd_event_default(&events); - eventPtr.reset(events); - events = nullptr; - setConfDir(); - initializeTimers(); - createNetLinkSocket(); - bus.attach_event(eventPtr.get(), SD_EVENT_PRIORITY_NORMAL); - rtnetlink::Server svr(eventPtr, smartSock); - } - - ~TestRtNetlink() + ~TestRtNetlink() + { + if (confDir.empty()) { - if (confDir.empty()) - { - fs::remove_all(confDir); - } + fs::remove_all(confDir); } + } - void setConfDir() - { - char tmp[] = "/tmp/NetworkManager.XXXXXX"; - confDir = mkdtemp(tmp); - manager->setConfDir(confDir); - } + void setConfDir() + { + char tmp[] = "/tmp/NetworkManager.XXXXXX"; + confDir = mkdtemp(tmp); + manager->setConfDir(confDir); + } - bool isInterfaceAdded(std::string intf) - { - return manager->interfaces.find(intf) != manager->interfaces.end()? - true : - false; - } + bool isInterfaceAdded(std::string intf) + { + return manager->interfaces.find(intf) != manager->interfaces.end() + ? true + : false; + } - void createNetLinkSocket() - { - //RtnetLink socket - auto fd = socket(PF_NETLINK, SOCK_RAW | SOCK_NONBLOCK, - NETLINK_ROUTE); - smartSock.set(fd); - } + void createNetLinkSocket() + { + // RtnetLink socket + auto fd = socket(PF_NETLINK, SOCK_RAW | SOCK_NONBLOCK, NETLINK_ROUTE); + smartSock.set(fd); + } }; - TEST_F(TestRtNetlink, WithSingleInterface) { using namespace std::chrono; // Adds the following ip in the getifaddrs list. - mock_addIP("igb5", "127.0.0.1", "255.255.255.128", - IFF_UP | IFF_RUNNING); + mock_addIP("igb5", "127.0.0.1", "255.255.255.128", IFF_UP | IFF_RUNNING); constexpr auto BUFSIZE = 4096; std::array<char, BUFSIZE> msgBuf = {0}; @@ -126,14 +115,13 @@ TEST_F(TestRtNetlink, WithSingleInterface) int i = 3; while (i--) { - //wait for timer to expire - std::this_thread::sleep_for( - std::chrono::milliseconds(refreshTimeout)); + // wait for timer to expire + std::this_thread::sleep_for(std::chrono::milliseconds(refreshTimeout)); sd_event_run(eventPtr.get(), 10); }; EXPECT_EQ(true, isInterfaceAdded("igb5")); } -}// namespce network -}// namespace phosphor +} // namespace network +} // namespace phosphor diff --git a/test/test_util.cpp b/test/test_util.cpp index bc3ec64..437b3cf 100644 --- a/test/test_util.cpp +++ b/test/test_util.cpp @@ -1,7 +1,9 @@ -#include <gtest/gtest.h> -#include <netinet/in.h> #include "util.hpp" +#include <netinet/in.h> + +#include <gtest/gtest.h> + namespace phosphor { namespace network @@ -9,12 +11,11 @@ namespace network class TestUtil : public testing::Test { - public: - TestUtil() - { - // Empty - } - + public: + TestUtil() + { + // Empty + } }; TEST_F(TestUtil, IpValidation) @@ -54,8 +55,6 @@ TEST_F(TestUtil, IpValidation) ipaddress = "1::8"; EXPECT_EQ(true, isValidIP(AF_INET6, ipaddress)); - - } TEST_F(TestUtil, PrefixValidation) @@ -77,10 +76,8 @@ TEST_F(TestUtil, PrefixValidation) prefixLength = 65; EXPECT_EQ(false, isValidPrefix(AF_INET, prefixLength)); - } - TEST_F(TestUtil, MacValidation) { std::string macaddress = "00:00:00:00:00:00"; @@ -97,7 +94,6 @@ TEST_F(TestUtil, MacValidation) macaddress = "hhh:GGG:iii:jjj:kkk:lll"; EXPECT_EQ(false, phosphor::network::mac_address::validate(macaddress)); - } TEST_F(TestUtil, ConvertV4MasktoPrefix) @@ -118,7 +114,7 @@ TEST_F(TestUtil, ConvertV4MasktoPrefix) prefix = toCidr(AF_INET, mask); EXPECT_EQ(prefix, 11); - //Invalid Mask + // Invalid Mask mask = "255.0.255.0"; prefix = toCidr(AF_INET, mask); EXPECT_EQ(prefix, 0); @@ -138,7 +134,7 @@ TEST_F(TestUtil, convertV6MasktoPrefix) prefix = toCidr(AF_INET6, mask); EXPECT_EQ(prefix, 38); - //Invalid Mask + // Invalid Mask mask = "ffff:0fff::"; prefix = toCidr(AF_INET6, mask); EXPECT_EQ(prefix, 0); @@ -179,24 +175,24 @@ TEST_F(TestUtil, convertPrefixToMask) TEST_F(TestUtil, getNetworkAddress) { - std::string address = getNetworkID(AF_INET,"9.3.23.251",24); - EXPECT_EQ("9.3.23.0",address); + std::string address = getNetworkID(AF_INET, "9.3.23.251", 24); + EXPECT_EQ("9.3.23.0", address); - address = getNetworkID(AF_INET,"9.3.23.251",25); - EXPECT_EQ("9.3.23.128",address); + address = getNetworkID(AF_INET, "9.3.23.251", 25); + EXPECT_EQ("9.3.23.128", address); - address = getNetworkID(AF_INET6,"2001:db8:abcd:dd12::0",64); - EXPECT_EQ("2001:db8:abcd:dd12::",address); + address = getNetworkID(AF_INET6, "2001:db8:abcd:dd12::0", 64); + EXPECT_EQ("2001:db8:abcd:dd12::", address); - address = getNetworkID(AF_INET,"a.b.c.d",25); - EXPECT_EQ("",address); + address = getNetworkID(AF_INET, "a.b.c.d", 25); + EXPECT_EQ("", address); - address = getNetworkID(AF_INET6,"2001:db8:gghh:dd12::0",64); - EXPECT_EQ("",address); + address = getNetworkID(AF_INET6, "2001:db8:gghh:dd12::0", 64); + EXPECT_EQ("", address); - address = getNetworkID(AF_INET6,"fe80::201:6cff:fe80:228",64); - EXPECT_EQ("fe80::",address); + address = getNetworkID(AF_INET6, "fe80::201:6cff:fe80:228", 64); + EXPECT_EQ("fe80::", address); } -}// namespce network -}// namespace phosphor +} // namespace network +} // namespace phosphor diff --git a/test/test_vlan_interface.cpp b/test/test_vlan_interface.cpp index 00384f1..1bcd87a 100644 --- a/test/test_vlan_interface.cpp +++ b/test/test_vlan_interface.cpp @@ -1,17 +1,18 @@ -#include "network_manager.hpp" -#include "mock_syscall.hpp" #include "config_parser.hpp" -#include "vlan_interface.hpp" #include "ipaddress.hpp" +#include "mock_syscall.hpp" +#include "network_manager.hpp" +#include "vlan_interface.hpp" -#include <gtest/gtest.h> -#include <sdbusplus/bus.hpp> - +#include <arpa/inet.h> #include <net/if.h> #include <netinet/in.h> -#include <arpa/inet.h> + #include <exception> #include <experimental/filesystem> +#include <sdbusplus/bus.hpp> + +#include <gtest/gtest.h> namespace phosphor { @@ -22,98 +23,89 @@ namespace fs = std::experimental::filesystem; class TestVlanInterface : public testing::Test { - public: + public: + sdbusplus::bus::bus bus; + Manager manager; + EthernetInterface interface; + std::string confDir; + TestVlanInterface() : + bus(sdbusplus::bus::new_default()), + manager(bus, "/xyz/openbmc_test/network", "/tmp"), + interface(bus, "/xyz/openbmc_test/network/test0", false, manager) - sdbusplus::bus::bus bus; - Manager manager; - EthernetInterface interface; - std::string confDir; - TestVlanInterface() - : bus(sdbusplus::bus::new_default()), - manager(bus, "/xyz/openbmc_test/network", "/tmp"), - interface(bus, "/xyz/openbmc_test/network/test0", false, manager) - - { - setConfDir(); - } + { + setConfDir(); + } - ~TestVlanInterface() + ~TestVlanInterface() + { + if (confDir != "") { - if(confDir != "") - { - fs::remove_all(confDir); - } + fs::remove_all(confDir); } + } - void setConfDir() - { - char tmp[] = "/tmp/VlanInterface.XXXXXX"; - confDir = mkdtemp(tmp); - manager.setConfDir(confDir); - } + void setConfDir() + { + char tmp[] = "/tmp/VlanInterface.XXXXXX"; + confDir = mkdtemp(tmp); + manager.setConfDir(confDir); + } - void createVlan(VlanId id) - { - interface.createVLAN(id); - } + void createVlan(VlanId id) + { + interface.createVLAN(id); + } - void deleteVlan(const std::string& interfaceName) - { - interface.deleteVLANObject(interfaceName); - } + void deleteVlan(const std::string& interfaceName) + { + interface.deleteVLANObject(interfaceName); + } - int countIPObjects() - { - return interface.getAddresses().size(); - } + int countIPObjects() + { + return interface.getAddresses().size(); + } - bool isIPObjectExist(const std::string& ipaddress) + bool isIPObjectExist(const std::string& ipaddress) + { + auto address = interface.getAddresses().find(ipaddress); + if (address == interface.getAddresses().end()) { - auto address = interface.getAddresses().find(ipaddress); - if (address == interface.getAddresses().end()) - { - return false; - } - return true; - + return false; } + return true; + } - bool deleteIPObject(const std::string& ipaddress) + bool deleteIPObject(const std::string& ipaddress) + { + auto address = interface.getAddresses().find(ipaddress); + if (address == interface.getAddresses().end()) { - auto address = interface.getAddresses().find(ipaddress); - if (address == interface.getAddresses().end()) - { - return false; - } - address->second->delete_(); - return true; + return false; } + address->second->delete_(); + return true; + } - void createIPObject(IP::Protocol addressType, - const std::string& ipaddress, - uint8_t subnetMask, - const std::string& gateway) - { - interface.iP(addressType, - ipaddress, - subnetMask, - gateway - ); - - } + void createIPObject(IP::Protocol addressType, const std::string& ipaddress, + uint8_t subnetMask, const std::string& gateway) + { + interface.iP(addressType, ipaddress, subnetMask, gateway); + } - bool isValueFound(const std::vector<std::string>& values, - const std::string& expectedValue) + bool isValueFound(const std::vector<std::string>& values, + const std::string& expectedValue) + { + for (const auto& value : values) { - for (const auto& value : values) + if (expectedValue == value) { - if (expectedValue == value) - { - return true; - } + return true; } - return false; } + return false; + } }; TEST_F(TestVlanInterface, createVLAN) @@ -140,7 +132,6 @@ TEST_F(TestVlanInterface, createVLAN) expectedValue = "50"; found = isValueFound(values, expectedValue); EXPECT_EQ(found, true); - } TEST_F(TestVlanInterface, deleteVLAN) @@ -153,7 +144,7 @@ TEST_F(TestVlanInterface, deleteVLAN) filePath /= "test0.50.netdev"; if (fs::is_regular_file(filePath.string())) { - fileFound = true; + fileFound = true; } EXPECT_EQ(fileFound, false); } @@ -201,5 +192,5 @@ TEST_F(TestVlanInterface, createMultipleVLAN) deleteVlan("test0.60"); } -}// namespce network -}// namespace phosphor +} // namespace network +} // namespace phosphor diff --git a/test/test_watch.cpp b/test/test_watch.cpp index 3ab3e22..167c903 100644 --- a/test/test_watch.cpp +++ b/test/test_watch.cpp @@ -1,10 +1,10 @@ -#include "watch.hpp" #include "types.hpp" +#include "watch.hpp" -#include <gtest/gtest.h> - -#include <fstream> #include <experimental/filesystem> +#include <fstream> + +#include <gtest/gtest.h> static constexpr auto TRIGGER_FILE = "/tmp/" __BASE_FILE__ "netif_state"; @@ -12,52 +12,50 @@ namespace fs = std::experimental::filesystem; class WatchTest : public ::testing::Test { - public: - // systemd event handler - sd_event* events; + public: + // systemd event handler + sd_event* events; - // Need this so that events can be initialized. - int rc; + // Need this so that events can be initialized. + int rc; - // Gets called as part of each TEST_F construction - WatchTest() - : rc(sd_event_default(&events)), - eventPtr(events) - { - // Create a file containing DNS entries like in netif/state - std::ofstream file(TRIGGER_FILE); - file << ""; + // Gets called as part of each TEST_F construction + WatchTest() : rc(sd_event_default(&events)), eventPtr(events) + { + // Create a file containing DNS entries like in netif/state + std::ofstream file(TRIGGER_FILE); + file << ""; - // Check for successful creation of - // event handler - EXPECT_GE(rc, 0); - } + // Check for successful creation of + // event handler + EXPECT_GE(rc, 0); + } - // Gets called as part of each TEST_F destruction - ~WatchTest() + // Gets called as part of each TEST_F destruction + ~WatchTest() + { + if (fs::exists(TRIGGER_FILE)) { - if (fs::exists(TRIGGER_FILE)) - { - fs::remove(TRIGGER_FILE); - } + fs::remove(TRIGGER_FILE); } + } - // unique_ptr for sd_event - phosphor::network::EventPtr eventPtr; + // unique_ptr for sd_event + phosphor::network::EventPtr eventPtr; - // Count of callback invocation - int count = 0; + // Count of callback invocation + int count = 0; - // This is supposed to get hit twice - // Once at the beginning to see if there is anything - // and the second time when the data is fired. - void callBackHandler(const fs::path& file) - { - count++; + // This is supposed to get hit twice + // Once at the beginning to see if there is anything + // and the second time when the data is fired. + void callBackHandler(const fs::path& file) + { + count++; - // Expect that the file is what we wanted - EXPECT_EQ(file, TRIGGER_FILE); - } + // Expect that the file is what we wanted + EXPECT_EQ(file, TRIGGER_FILE); + } }; /** @brief Makes sure that the inotify event is fired @@ -65,9 +63,9 @@ class WatchTest : public ::testing::Test TEST_F(WatchTest, validateEventNotification) { // Create a watch object and register the handler - phosphor::network::inotify::Watch watch(eventPtr, TRIGGER_FILE, - std::bind(&WatchTest::callBackHandler, this, - std::placeholders::_1)); + phosphor::network::inotify::Watch watch( + eventPtr, TRIGGER_FILE, + std::bind(&WatchTest::callBackHandler, this, std::placeholders::_1)); // Reading the event post subscription callBackHandler(TRIGGER_FILE); @@ -82,7 +80,7 @@ TEST_F(WatchTest, validateEventNotification) // Pump the data and get notification { std::ofstream file(TRIGGER_FILE); - file <<"DNS=1.2.3.4\n"; + file << "DNS=1.2.3.4\n"; } sd_event_run(eventPtr.get(), 10); |

