blob: 3ecb96e3ce8b29a67fc5cb268e53ffc44104f2e5 [file] [log] [blame]
Tomáš Pecka292bc9c2021-01-11 22:03:11 +01001#include "trompeloeil_doctest.h"
Tomáš Pecka79344c82021-09-16 18:25:59 +02002#include <arpa/inet.h>
3#include <sdbus-c++/sdbus-c++.h>
4#include "dbus-helpers/dbus_resolve1_server.h"
Tomáš Pecka292bc9c2021-01-11 22:03:11 +01005#include "pretty_printers.h"
Tomáš Peckaf976c5b2021-01-23 21:19:52 +01006#include "system/IETFSystem.h"
Tomáš Pecka292bc9c2021-01-11 22:03:11 +01007#include "test_log_setup.h"
8#include "test_sysrepo_helpers.h"
9#include "tests/configure.cmake.h"
10
11using namespace std::literals;
12
Tomáš Peckaf976c5b2021-01-23 21:19:52 +010013TEST_CASE("Sysrepo ietf-system")
Tomáš Pecka292bc9c2021-01-11 22:03:11 +010014{
15 trompeloeil::sequence seq1;
16
17 TEST_SYSREPO_INIT_LOGS;
18 TEST_SYSREPO_INIT;
Tomáš Pecka879a6032021-02-03 17:21:48 +010019 TEST_SYSREPO_INIT_CLIENT;
Tomáš Pecka292bc9c2021-01-11 22:03:11 +010020
Tomáš Pecka79344c82021-09-16 18:25:59 +020021 auto dbusConnServer = sdbus::createSessionBusConnection();
22 auto dbusConnClient = sdbus::createSessionBusConnection();
23
24 dbusConnServer->enterEventLoopAsync();
25 dbusConnClient->enterEventLoopAsync();
26
27 DbusResolve1Server dbusServer(*dbusConnServer);
28
Tomáš Pecka292bc9c2021-01-11 22:03:11 +010029 SECTION("Test system-state")
30 {
Tomáš Pecka292bc9c2021-01-11 22:03:11 +010031 static const auto modulePrefix = "/ietf-system:system-state"s;
32
33 SECTION("Valid data")
34 {
35 std::filesystem::path file;
36 std::map<std::string, std::string> expected;
37
38 SECTION("Real data")
39 {
40 file = CMAKE_CURRENT_SOURCE_DIR "/tests/system/os-release";
41 expected = {
Jan Kundrát51f1d6d2021-02-19 00:41:28 +010042 {"/os-name", "CzechLight"},
43 {"/os-release", "v4-105-g8294175-dirty"},
44 {"/os-version", "v4-105-g8294175-dirty"},
Tomáš Pecka292bc9c2021-01-11 22:03:11 +010045 };
46 }
47
48 SECTION("Missing =")
49 {
50 file = CMAKE_CURRENT_SOURCE_DIR "/tests/system/missing-equal";
51 expected = {
Jan Kundrát51f1d6d2021-02-19 00:41:28 +010052 {"/os-name", ""},
53 {"/os-release", ""},
54 {"/os-version", ""},
Tomáš Pecka292bc9c2021-01-11 22:03:11 +010055 };
56 }
57
58 SECTION("Empty values")
59 {
60 file = CMAKE_CURRENT_SOURCE_DIR "/tests/system/empty-values";
61 expected = {
Jan Kundrát51f1d6d2021-02-19 00:41:28 +010062 {"/os-name", ""},
63 {"/os-release", ""},
64 {"/os-version", ""},
Tomáš Pecka292bc9c2021-01-11 22:03:11 +010065 };
66 }
67
Tomáš Pecka79344c82021-09-16 18:25:59 +020068 auto sysrepo = std::make_shared<velia::system::IETFSystem>(srSess, file, *dbusConnClient, dbusConnServer->getUniqueName());
Jan Kundrát51f1d6d2021-02-19 00:41:28 +010069 REQUIRE(dataFromSysrepo(client, modulePrefix + "/platform", SR_DS_OPERATIONAL) == expected);
Tomáš Pecka292bc9c2021-01-11 22:03:11 +010070 }
71
72 SECTION("Invalid data (missing VERSION and NAME keys)")
73 {
Tomáš Pecka79344c82021-09-16 18:25:59 +020074 REQUIRE_THROWS_AS(std::make_shared<velia::system::IETFSystem>(srSess, CMAKE_CURRENT_SOURCE_DIR "/tests/system/missing-keys", *dbusConnClient, dbusConnServer->getUniqueName()), std::out_of_range);
Tomáš Pecka292bc9c2021-01-11 22:03:11 +010075 }
76 }
Tomáš Pecka879a6032021-02-03 17:21:48 +010077
Jan Kundrátdcd50f02021-02-18 23:28:26 +010078 SECTION("dummy values")
79 {
Tomáš Pecka79344c82021-09-16 18:25:59 +020080 auto sys = std::make_shared<velia::system::IETFSystem>(srSess, CMAKE_CURRENT_SOURCE_DIR "/tests/system/os-release", *dbusConnClient, dbusConnServer->getUniqueName());
Jan Kundrátdcd50f02021-02-18 23:28:26 +010081 const char* xpath;
82
83 SECTION("location") {
84 xpath = "/ietf-system:system/location";
85 }
86
87 SECTION("contact") {
88 xpath = "/ietf-system:system/contact";
89 }
90
91 client->session_switch_ds(SR_DS_OPERATIONAL);
92 REQUIRE_THROWS_WITH(client->get_item(xpath), "Item not found");
93
94 client->session_switch_ds(SR_DS_RUNNING);
95 client->set_item_str(xpath, "lamparna");
96
97 REQUIRE(!!client->get_item(xpath));
98 }
99
Jan Kundrát51f1d6d2021-02-19 00:41:28 +0100100 SECTION("clock")
101 {
Tomáš Pecka79344c82021-09-16 18:25:59 +0200102 auto sys = std::make_shared<velia::system::IETFSystem>(srSess, CMAKE_CURRENT_SOURCE_DIR "/tests/system/os-release", *dbusConnClient, dbusConnServer->getUniqueName());
Jan Kundrát51f1d6d2021-02-19 00:41:28 +0100103 client->session_switch_ds(SR_DS_OPERATIONAL);
104 REQUIRE(!!client->get_item("/ietf-system:system-state/clock/current-datetime"));
105 }
106
Tomáš Pecka79344c82021-09-16 18:25:59 +0200107 SECTION("DNS resolvers")
108 {
109 auto sysrepo = std::make_shared<velia::system::IETFSystem>(srSess, CMAKE_CURRENT_SOURCE_DIR "/tests/system/os-release", *dbusConnClient, dbusConnServer->getUniqueName());
110 std::map<std::string, std::string> expected;
111
112 dbusServer.setFallbackDNSEx({
113 {0, AF_INET, {8, 8, 8, 8}, 0, "prvni.googlovsky.dns"},
114 {0, AF_INET, {8, 8, 4, 4}, 0, "druhy.googlovsky.dns"},
115 {2, AF_INET6, {0x20, 0x01, 0x48, 0x60, 0x48, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x88, 0x88}, 0, "this.was.in.my.resolved"},
116 });
117
118 SECTION("Both DNS and Fallback DNS") {
119 dbusServer.setDNSEx({
120 {0, AF_INET, {127, 0, 0, 1}, 0, "ahoj.com"},
121 {2, AF_INET, {127, 0, 0, 1}, 0, "czech.light"},
122 {2, AF_INET6, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 53, "idk.net"},
123 });
124
125 expected = {
126 {"/options", ""},
127 {"/server[name='127.0.0.1']", ""},
128 {"/server[name='127.0.0.1']/name", "127.0.0.1"},
129 {"/server[name='127.0.0.1']/udp-and-tcp", ""},
130 {"/server[name='127.0.0.1']/udp-and-tcp/address", "127.0.0.1"},
131 {"/server[name='::1']", ""},
132 {"/server[name='::1']/name", "::1"},
133 {"/server[name='::1']/udp-and-tcp", ""},
134 {"/server[name='::1']/udp-and-tcp/address", "::1"},
135 };
136 }
137
138 SECTION("FallbackDNS only")
139 {
140 expected = {
141 {"/options", ""},
142 {"/server[name='2001:4860:4860::8888']", ""},
143 {"/server[name='2001:4860:4860::8888']/name", "2001:4860:4860::8888"},
144 {"/server[name='2001:4860:4860::8888']/udp-and-tcp", ""},
145 {"/server[name='2001:4860:4860::8888']/udp-and-tcp/address", "2001:4860:4860::8888"},
146 {"/server[name='8.8.4.4']", ""},
147 {"/server[name='8.8.4.4']/name", "8.8.4.4"},
148 {"/server[name='8.8.4.4']/udp-and-tcp", ""},
149 {"/server[name='8.8.4.4']/udp-and-tcp/address", "8.8.4.4"},
150 {"/server[name='8.8.8.8']", ""},
151 {"/server[name='8.8.8.8']/name", "8.8.8.8"},
152 {"/server[name='8.8.8.8']/udp-and-tcp", ""},
153 {"/server[name='8.8.8.8']/udp-and-tcp/address", "8.8.8.8"},
154 };
155 }
156
157 REQUIRE(dataFromSysrepo(client, "/ietf-system:system/dns-resolver", SR_DS_OPERATIONAL) == expected);
158 }
159
Tomáš Pecka879a6032021-02-03 17:21:48 +0100160#ifdef TEST_RPC_SYSTEM_REBOOT
161 SECTION("RPC system-restart")
162 {
Tomáš Pecka79344c82021-09-16 18:25:59 +0200163 auto sysrepo = std::make_shared<velia::system::IETFSystem>(srSess, CMAKE_CURRENT_SOURCE_DIR "/tests/system/os-release", dbusConnection);
Tomáš Pecka879a6032021-02-03 17:21:48 +0100164
165 auto rpcInput = std::make_shared<sysrepo::Vals>(0);
166 auto res = client->rpc_send("/ietf-system:system-restart", rpcInput);
167 REQUIRE(res->val_cnt() == 0);
168 }
169#endif
Tomáš Pecka292bc9c2021-01-11 22:03:11 +0100170}