Tomáš Pecka | 292bc9c | 2021-01-11 22:03:11 +0100 | [diff] [blame] | 1 | #include "trompeloeil_doctest.h" |
Tomáš Pecka | 79344c8 | 2021-09-16 18:25:59 +0200 | [diff] [blame] | 2 | #include <arpa/inet.h> |
| 3 | #include <sdbus-c++/sdbus-c++.h> |
| 4 | #include "dbus-helpers/dbus_resolve1_server.h" |
Tomáš Pecka | 292bc9c | 2021-01-11 22:03:11 +0100 | [diff] [blame] | 5 | #include "pretty_printers.h" |
Tomáš Pecka | f976c5b | 2021-01-23 21:19:52 +0100 | [diff] [blame] | 6 | #include "system/IETFSystem.h" |
Tomáš Pecka | 292bc9c | 2021-01-11 22:03:11 +0100 | [diff] [blame] | 7 | #include "test_log_setup.h" |
| 8 | #include "test_sysrepo_helpers.h" |
| 9 | #include "tests/configure.cmake.h" |
| 10 | |
| 11 | using namespace std::literals; |
| 12 | |
Tomáš Pecka | f976c5b | 2021-01-23 21:19:52 +0100 | [diff] [blame] | 13 | TEST_CASE("Sysrepo ietf-system") |
Tomáš Pecka | 292bc9c | 2021-01-11 22:03:11 +0100 | [diff] [blame] | 14 | { |
| 15 | trompeloeil::sequence seq1; |
| 16 | |
| 17 | TEST_SYSREPO_INIT_LOGS; |
| 18 | TEST_SYSREPO_INIT; |
Tomáš Pecka | 879a603 | 2021-02-03 17:21:48 +0100 | [diff] [blame] | 19 | TEST_SYSREPO_INIT_CLIENT; |
Tomáš Pecka | 292bc9c | 2021-01-11 22:03:11 +0100 | [diff] [blame] | 20 | |
Tomáš Pecka | 79344c8 | 2021-09-16 18:25:59 +0200 | [diff] [blame] | 21 | auto dbusConnServer = sdbus::createSessionBusConnection(); |
| 22 | auto dbusConnClient = sdbus::createSessionBusConnection(); |
| 23 | |
| 24 | dbusConnServer->enterEventLoopAsync(); |
| 25 | dbusConnClient->enterEventLoopAsync(); |
| 26 | |
| 27 | DbusResolve1Server dbusServer(*dbusConnServer); |
| 28 | |
Tomáš Pecka | 292bc9c | 2021-01-11 22:03:11 +0100 | [diff] [blame] | 29 | SECTION("Test system-state") |
| 30 | { |
Tomáš Pecka | 292bc9c | 2021-01-11 22:03:11 +0100 | [diff] [blame] | 31 | 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át | 51f1d6d | 2021-02-19 00:41:28 +0100 | [diff] [blame] | 42 | {"/os-name", "CzechLight"}, |
| 43 | {"/os-release", "v4-105-g8294175-dirty"}, |
| 44 | {"/os-version", "v4-105-g8294175-dirty"}, |
Tomáš Pecka | 292bc9c | 2021-01-11 22:03:11 +0100 | [diff] [blame] | 45 | }; |
| 46 | } |
| 47 | |
| 48 | SECTION("Missing =") |
| 49 | { |
| 50 | file = CMAKE_CURRENT_SOURCE_DIR "/tests/system/missing-equal"; |
| 51 | expected = { |
Jan Kundrát | 51f1d6d | 2021-02-19 00:41:28 +0100 | [diff] [blame] | 52 | {"/os-name", ""}, |
| 53 | {"/os-release", ""}, |
| 54 | {"/os-version", ""}, |
Tomáš Pecka | 292bc9c | 2021-01-11 22:03:11 +0100 | [diff] [blame] | 55 | }; |
| 56 | } |
| 57 | |
| 58 | SECTION("Empty values") |
| 59 | { |
| 60 | file = CMAKE_CURRENT_SOURCE_DIR "/tests/system/empty-values"; |
| 61 | expected = { |
Jan Kundrát | 51f1d6d | 2021-02-19 00:41:28 +0100 | [diff] [blame] | 62 | {"/os-name", ""}, |
| 63 | {"/os-release", ""}, |
| 64 | {"/os-version", ""}, |
Tomáš Pecka | 292bc9c | 2021-01-11 22:03:11 +0100 | [diff] [blame] | 65 | }; |
| 66 | } |
| 67 | |
Tomáš Pecka | 79344c8 | 2021-09-16 18:25:59 +0200 | [diff] [blame] | 68 | auto sysrepo = std::make_shared<velia::system::IETFSystem>(srSess, file, *dbusConnClient, dbusConnServer->getUniqueName()); |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame^] | 69 | REQUIRE(dataFromSysrepo(client, modulePrefix + "/platform", sysrepo::Datastore::Operational) == expected); |
Tomáš Pecka | 292bc9c | 2021-01-11 22:03:11 +0100 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | SECTION("Invalid data (missing VERSION and NAME keys)") |
| 73 | { |
Tomáš Pecka | 79344c8 | 2021-09-16 18:25:59 +0200 | [diff] [blame] | 74 | 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áš Pecka | 292bc9c | 2021-01-11 22:03:11 +0100 | [diff] [blame] | 75 | } |
| 76 | } |
Tomáš Pecka | 879a603 | 2021-02-03 17:21:48 +0100 | [diff] [blame] | 77 | |
Jan Kundrát | dcd50f0 | 2021-02-18 23:28:26 +0100 | [diff] [blame] | 78 | SECTION("dummy values") |
| 79 | { |
Tomáš Pecka | 79344c8 | 2021-09-16 18:25:59 +0200 | [diff] [blame] | 80 | auto sys = std::make_shared<velia::system::IETFSystem>(srSess, CMAKE_CURRENT_SOURCE_DIR "/tests/system/os-release", *dbusConnClient, dbusConnServer->getUniqueName()); |
Jan Kundrát | dcd50f0 | 2021-02-18 23:28:26 +0100 | [diff] [blame] | 81 | 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 | |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame^] | 91 | client.switchDatastore(sysrepo::Datastore::Operational); |
| 92 | REQUIRE(!client.getData(xpath)); |
Jan Kundrát | dcd50f0 | 2021-02-18 23:28:26 +0100 | [diff] [blame] | 93 | |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame^] | 94 | client.switchDatastore(sysrepo::Datastore::Running); |
| 95 | client.setItem(xpath, "lamparna"); |
Jan Kundrát | dcd50f0 | 2021-02-18 23:28:26 +0100 | [diff] [blame] | 96 | |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame^] | 97 | REQUIRE(client.getData(xpath)); |
Jan Kundrát | dcd50f0 | 2021-02-18 23:28:26 +0100 | [diff] [blame] | 98 | } |
| 99 | |
Jan Kundrát | 51f1d6d | 2021-02-19 00:41:28 +0100 | [diff] [blame] | 100 | SECTION("clock") |
| 101 | { |
Tomáš Pecka | 79344c8 | 2021-09-16 18:25:59 +0200 | [diff] [blame] | 102 | auto sys = std::make_shared<velia::system::IETFSystem>(srSess, CMAKE_CURRENT_SOURCE_DIR "/tests/system/os-release", *dbusConnClient, dbusConnServer->getUniqueName()); |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame^] | 103 | client.switchDatastore(sysrepo::Datastore::Operational); |
| 104 | REQUIRE(client.getData("/ietf-system:system-state/clock/current-datetime")); |
Jan Kundrát | 51f1d6d | 2021-02-19 00:41:28 +0100 | [diff] [blame] | 105 | } |
| 106 | |
Tomáš Pecka | 79344c8 | 2021-09-16 18:25:59 +0200 | [diff] [blame] | 107 | 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 | |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame^] | 157 | REQUIRE(dataFromSysrepo(client, "/ietf-system:system/dns-resolver", sysrepo::Datastore::Operational) == expected); |
Tomáš Pecka | 79344c8 | 2021-09-16 18:25:59 +0200 | [diff] [blame] | 158 | } |
| 159 | |
Tomáš Pecka | 879a603 | 2021-02-03 17:21:48 +0100 | [diff] [blame] | 160 | #ifdef TEST_RPC_SYSTEM_REBOOT |
| 161 | SECTION("RPC system-restart") |
| 162 | { |
Tomáš Pecka | 79344c8 | 2021-09-16 18:25:59 +0200 | [diff] [blame] | 163 | auto sysrepo = std::make_shared<velia::system::IETFSystem>(srSess, CMAKE_CURRENT_SOURCE_DIR "/tests/system/os-release", dbusConnection); |
Tomáš Pecka | 879a603 | 2021-02-03 17:21:48 +0100 | [diff] [blame] | 164 | |
| 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áš Pecka | 292bc9c | 2021-01-11 22:03:11 +0100 | [diff] [blame] | 170 | } |