blob: a34a5e7f75300cf5f7ab38150745b8191354df43 [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 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050031 }
Ratan Gupta255d5142017-08-10 09:02:08 +053032
Gunnar Mills57d9c502018-09-14 14:42:34 -050033 void createInterfaces()
34 {
35 manager.createInterfaces();
36 }
Ratan Gupta8ab17922017-05-25 13:07:05 +053037};
38
39// getifaddrs will not return any interface
40TEST_F(TestNetworkManager, NoInterface)
41{
William A. Kennington III2e09d272022-10-14 17:15:00 -070042 mock_clear();
43 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 mock_clear();
William A. Kennington III862275a2019-04-22 20:37:08 -070050
William A. Kennington III8cff3802019-04-21 20:17:27 -070051 // Adds the following ip in the getifaddrs list.
William A. Kennington III9ecb90e2022-10-14 03:12:43 -070052 mock_addIF("igb1", /*idx=*/2);
53 mock_addIP("igb1", "192.0.2.3", "255.255.255.128");
Ratan Gupta8ab17922017-05-25 13:07:05 +053054
William A. Kennington III8cff3802019-04-21 20:17:27 -070055 // Now create the interfaces which will call the mocked getifaddrs
56 // which returns the above interface detail.
57 createInterfaces();
William A. Kennington III9ecb90e2022-10-14 03:12:43 -070058 EXPECT_THAT(manager.getInterfaces(), UnorderedElementsAre(Key("igb1")));
Ratan Gupta8ab17922017-05-25 13:07:05 +053059}
60
61// getifaddrs returns two interfaces.
62TEST_F(TestNetworkManager, WithMultipleInterfaces)
63{
William A. Kennington III8cff3802019-04-21 20:17:27 -070064 mock_clear();
William A. Kennington III862275a2019-04-22 20:37:08 -070065
William A. Kennington III9ecb90e2022-10-14 03:12:43 -070066 mock_addIF("igb0", /*idx=*/1);
67 mock_addIP("igb0", "192.0.2.2", "255.255.255.128");
68 mock_addIF("igb1", /*idx=*/2);
69 mock_addIP("igb1", "192.0.2.3", "255.255.255.128");
Ratan Gupta8ab17922017-05-25 13:07:05 +053070
William A. Kennington III8cff3802019-04-21 20:17:27 -070071 createInterfaces();
William A. Kennington III9ecb90e2022-10-14 03:12:43 -070072 EXPECT_THAT(manager.getInterfaces(),
73 UnorderedElementsAre(Key("igb0"), Key("igb1")));
Ratan Gupta8ab17922017-05-25 13:07:05 +053074}
Gunnar Mills57d9c502018-09-14 14:42:34 -050075} // namespace network
76} // namespace phosphor