blob: 459bacfd44cb917cf78845aaabd1063a92bf1179 [file] [log] [blame]
Ratan Gupta35297172018-11-28 18:40:16 +05301#include "mock_network_manager.hpp"
Ratan Gupta8ab17922017-05-25 13:07:05 +05302#include "mock_syscall.hpp"
Ratan Gupta8ab17922017-05-25 13:07:05 +05303
Gunnar Mills57d9c502018-09-14 14:42:34 -05004#include <arpa/inet.h>
Ratan Gupta8ab17922017-05-25 13:07:05 +05305#include <net/if.h>
6#include <netinet/in.h>
Ratan Gupta255d5142017-08-10 09:02:08 +05307#include <stdlib.h>
8
William A. Kennington IIIf51620d2019-04-21 18:54:13 -07009#include <sdbusplus/bus.hpp>
William A. Kennington III84bfe672022-07-13 14:15:30 -070010#include <stdplus/gtest/tmp.hpp>
Gunnar Mills57d9c502018-09-14 14:42:34 -050011
12#include <gtest/gtest.h>
Ratan Gupta8ab17922017-05-25 13:07:05 +053013
14namespace phosphor
15{
16namespace network
17{
18
William A. Kennington III9ecb90e2022-10-14 03:12:43 -070019using ::testing::Key;
20using ::testing::UnorderedElementsAre;
Ratan Gupta255d5142017-08-10 09:02:08 +053021
William A. Kennington III84bfe672022-07-13 14:15:30 -070022class TestNetworkManager : public stdplus::gtest::TestWithTmp
Ratan Gupta8ab17922017-05-25 13:07:05 +053023{
Gunnar Mills57d9c502018-09-14 14:42:34 -050024 public:
Patrick Williamsc38b0712022-07-22 19:26:54 -050025 sdbusplus::bus_t bus;
Manojkiran Edaacd6dd52019-10-15 15:00:51 +053026 MockManager manager;
Gunnar Mills57d9c502018-09-14 14:42:34 -050027 TestNetworkManager() :
William A. Kennington IIIf51620d2019-04-21 18:54:13 -070028 bus(sdbusplus::bus::new_default()),
William A. Kennington III84bfe672022-07-13 14:15:30 -070029 manager(bus, "/xyz/openbmc_test/abc", CaseTmpDir())
Gunnar Mills57d9c502018-09-14 14:42:34 -050030 {
William A. Kennington III77848562022-10-26 17:32:57 -070031 system::mock_clear();
Gunnar Mills57d9c502018-09-14 14:42:34 -050032 }
Ratan Gupta255d5142017-08-10 09:02:08 +053033
Gunnar Mills57d9c502018-09-14 14:42:34 -050034 void createInterfaces()
35 {
36 manager.createInterfaces();
37 }
Ratan Gupta8ab17922017-05-25 13:07:05 +053038};
39
40// getifaddrs will not return any interface
41TEST_F(TestNetworkManager, NoInterface)
42{
William A. Kennington III2e09d272022-10-14 17:15:00 -070043 createInterfaces();
44 EXPECT_TRUE(manager.getInterfaces().empty());
Ratan Gupta8ab17922017-05-25 13:07:05 +053045}
Ratan Gupta8ab17922017-05-25 13:07:05 +053046// getifaddrs returns single interface.
47TEST_F(TestNetworkManager, WithSingleInterface)
48{
William A. Kennington III8cff3802019-04-21 20:17:27 -070049 // Adds the following ip in the getifaddrs list.
William A. Kennington III77848562022-10-26 17:32:57 -070050 system::mock_addIF({.idx = 2, .flags = 0, .name = "igb1"});
Ratan Gupta8ab17922017-05-25 13:07:05 +053051
William A. Kennington III8cff3802019-04-21 20:17:27 -070052 // Now create the interfaces which will call the mocked getifaddrs
53 // which returns the above interface detail.
54 createInterfaces();
William A. Kennington III9ecb90e2022-10-14 03:12:43 -070055 EXPECT_THAT(manager.getInterfaces(), UnorderedElementsAre(Key("igb1")));
Ratan Gupta8ab17922017-05-25 13:07:05 +053056}
57
58// getifaddrs returns two interfaces.
59TEST_F(TestNetworkManager, WithMultipleInterfaces)
60{
William A. Kennington III77848562022-10-26 17:32:57 -070061 system::mock_addIF({.idx = 1, .flags = 0, .name = "igb0"});
62 system::mock_addIF({.idx = 2, .flags = 0, .name = "igb1"});
Ratan Gupta8ab17922017-05-25 13:07:05 +053063
William A. Kennington III8cff3802019-04-21 20:17:27 -070064 createInterfaces();
William A. Kennington III9ecb90e2022-10-14 03:12:43 -070065 EXPECT_THAT(manager.getInterfaces(),
66 UnorderedElementsAre(Key("igb0"), Key("igb1")));
Ratan Gupta8ab17922017-05-25 13:07:05 +053067}
Gunnar Mills57d9c502018-09-14 14:42:34 -050068} // namespace network
69} // namespace phosphor