blob: 2e011370467905033ca574b342070e01ec20471c [file] [log] [blame]
Tomáš Pecka7acf3922021-08-10 11:16:57 +02001#include "trompeloeil_doctest.h"
2#include <sdbus-c++/sdbus-c++.h>
3#include <unistd.h>
4#include "dbus-helpers/dbus_network1_server.h"
5#include "pretty_printers.h"
6#include "system/LLDP.h"
7#include "test_log_setup.h"
8#include "tests/configure.cmake.h"
9
10using namespace std::literals;
11
12namespace velia::system {
13bool operator==(const NeighborEntry& a, const NeighborEntry& b)
14{
15 return std::tie(a.m_portId, a.m_properties) == std::tie(b.m_portId, b.m_properties);
16}
17}
18
19TEST_CASE("Parsing with the mock")
20{
21 TEST_INIT_LOGS;
22
23 const auto serverBus = "local.pid" + std::to_string(getpid()) + ".org.freedesktop.network1";
24 auto dbusServerConnection = sdbus::createSessionBusConnection(serverBus);
25 dbusServerConnection->enterEventLoopAsync();
26
27 std::vector<std::pair<int, std::string>> links;
28 std::string dataDir;
29 std::vector<velia::system::NeighborEntry> expected;
30
31 SECTION("LLDP active on a single link")
32 {
33 links = {{1, "lo"}, {2, "enp0s25"}, {3, "wlp3s0"}, {4, "tun0"}, {5, "br-53662f640039"}, {6, "docker0"}, {7, "br-e78120c0adda"}, {8, "ve-image"}};
34 dataDir = "single-link";
35 expected = {
36 {"ve-image", {
37 {"remoteSysName", "image"},
38 {"remotePortId", "host0"},
39 {"remoteChassisId", "7062a9e41c924ac6942da39c56e6b820"},
40 {"systemCapabilitiesSupported", "bridge router station-only"},
41 {"systemCapabilitiesEnabled", "station-only"},
42 }}};
43 }
44
45 SECTION("No LLDP enabled")
46 {
47 links = {{1, "lo"}, {2, "enp0s25"}, {3, "wlp3s0"}, {4, "tun0"}, {5, "br-53662f640039"}, {6, "docker0"}, {7, "br-e78120c0adda"}, {8, "ve-image"}};
48 dataDir = "no-link";
49 expected = {};
50 }
51
52 SECTION("Two LLDP links")
53 {
54 links = {{1, "lo"}, {2, "enp0s25"}, {3, "enp0s31f6"}, {4, "ve-image"}};
55 dataDir = "two-links";
56 expected = {
57 {"enp0s31f6", {
58 {"remoteSysName", "sw-a1128-01.fit.cvut.cz"},
59 {"remotePortId", "Gi3/0/7"},
60 {"remoteChassisId", "00:b8:b3:e6:17:80"},
61 {"systemCapabilitiesSupported", "bridge router"},
62 {"systemCapabilitiesEnabled", "bridge"},
63 }},
64 {"ve-image", {
65 {"remoteSysName", "image"},
66 {"remotePortId", "host0"},
67 {"remoteChassisId", "8b90f96f448140fb9b5d9d68e86d052e"},
68 {"systemCapabilitiesSupported", "bridge router station-only"},
69 {"systemCapabilitiesEnabled", "station-only"},
70 }},
71 };
72 }
73
74 SECTION("Multiple neighbors on one interface")
75 {
76 links = {{1, "host0"}};
77 dataDir = "multiple-neighbors";
78 expected = {
79 {"host0", {
80 {"remoteSysName", "image"},
81 {"remotePortId", "host0"},
82 {"remoteChassisId", "1631331c24bb499bb644fcdf7c9fd467"},
83 {"systemCapabilitiesSupported", "bridge router station-only"},
84 {"systemCapabilitiesEnabled", "station-only"},
85 }},
86 {"host0", {
87 {"remoteSysName", "enterprise"},
88 {"remotePortId", "vb-image2"},
89 {"remoteChassisId", "1efe5cecbfc248a09065ad6177a98b41"},
90 {"systemCapabilitiesSupported", "bridge router station-only"},
91 {"systemCapabilitiesEnabled", "station-only"},
92 }},
93 };
94 }
95
96 auto dbusServer = DbusServer(*dbusServerConnection);
97 dbusServer.setLinks(links); // intentionally not mocking DbusMockServer::ListLinks but using explicit set/get pattern so I can avoid an unneccesary dependency on trompeloeil
98
99 auto dbusClient = sdbus::createSessionBusConnection();
100 auto lldp = std::make_shared<velia::system::LLDPDataProvider>(std::filesystem::path(CMAKE_CURRENT_SOURCE_DIR "/tests/lldp/"s) / dataDir, *dbusClient, serverBus);
101
102 REQUIRE(lldp->getNeighbors() == expected);
103}
104
105#if LIST_NEIGHBORS_RUN
106TEST_CASE("Real systemd")
107{
108 TEST_INIT_LOGS;
109
110 auto dbusConnection = sdbus::createSystemBusConnection();
111 auto lldp = std::make_shared<lldp::lldp::LLDPDataProvider>("/run/systemd/netif/lldp", *dbusConnection, "org.freedesktop.network1");
112 [[maybe_unused]] auto x = lldp->getNeighbors();
113}
114#endif