blob: 5298719d90bf0789e43ba3cdf7b6e593ed0aaa51 [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);
Ratan Gupta8ab17922017-05-25 13:07:05 +053053
William A. Kennington III8cff3802019-04-21 20:17:27 -070054 // Now create the interfaces which will call the mocked getifaddrs
55 // which returns the above interface detail.
56 createInterfaces();
William A. Kennington III9ecb90e2022-10-14 03:12:43 -070057 EXPECT_THAT(manager.getInterfaces(), UnorderedElementsAre(Key("igb1")));
Ratan Gupta8ab17922017-05-25 13:07:05 +053058}
59
60// getifaddrs returns two interfaces.
61TEST_F(TestNetworkManager, WithMultipleInterfaces)
62{
William A. Kennington III8cff3802019-04-21 20:17:27 -070063 mock_clear();
William A. Kennington III862275a2019-04-22 20:37:08 -070064
William A. Kennington III9ecb90e2022-10-14 03:12:43 -070065 mock_addIF("igb0", /*idx=*/1);
William A. Kennington III9ecb90e2022-10-14 03:12:43 -070066 mock_addIF("igb1", /*idx=*/2);
Ratan Gupta8ab17922017-05-25 13:07:05 +053067
William A. Kennington III8cff3802019-04-21 20:17:27 -070068 createInterfaces();
William A. Kennington III9ecb90e2022-10-14 03:12:43 -070069 EXPECT_THAT(manager.getInterfaces(),
70 UnorderedElementsAre(Key("igb0"), Key("igb1")));
Ratan Gupta8ab17922017-05-25 13:07:05 +053071}
Gunnar Mills57d9c502018-09-14 14:42:34 -050072} // namespace network
73} // namespace phosphor