-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathunsafe.cpp
231 lines (192 loc) · 9.14 KB
/
unsafe.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
* Copyright (C) 2021 CESNET, https://photonics.cesnet.cz/
*
* Written by Václav Kubernát <[email protected]>
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <doctest/doctest.h>
#include <libyang-cpp/Context.hpp>
#include <libyang-cpp/Utils.hpp>
#include <libyang/libyang.h>
#include <libyang/tree_data.h>
#include "example_schema.hpp"
#include "test_vars.hpp"
#include "utils/filesystem_path.hpp"
namespace {
using ctx_deleter_t = decltype([](auto ctx) constexpr {
ly_ctx_destroy(ctx);
});
using node_deleter_t = decltype([](auto node) constexpr {
lyd_free_all(node);
});
}
TEST_CASE("Unsafe methods")
{
ly_ctx* ctx;
ly_ctx_new(nullptr, 0, &ctx);
// When wrapping raw lyd_nodes, the context struct however is not managed and needs to be released manually (for
// example with a unique_ptr), like below.
auto ctx_deleter = std::unique_ptr<ly_ctx, ctx_deleter_t>(ctx);
lys_parse_mem(ctx, example_schema.c_str(), LYS_IN_YANG, nullptr);
auto data = R"({ "example-schema:leafInt32": 32 })";
DOCTEST_SUBCASE("createUnmanagedContext")
{
DOCTEST_SUBCASE("Custom deleter")
{
ctx_deleter.release();
auto wrapped = libyang::createUnmanagedContext(ctx, ly_ctx_destroy);
}
DOCTEST_SUBCASE("No custom deleter")
{
auto wrapped = libyang::createUnmanagedContext(ctx, nullptr);
}
}
DOCTEST_SUBCASE("wrapRawNode")
{
lyd_node* node;
lyd_parse_data_mem(ctx, data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, &node);
// The wrapped node does take ownership of the lyd_node* and deletes it on destruction of the class.
auto wrapped = libyang::wrapRawNode(node);
REQUIRE(wrapped.path() == "/example-schema:leafInt32");
DOCTEST_SUBCASE("Automatic memory")
{
// nothing
}
DOCTEST_SUBCASE("Releasing the raw node")
{
auto releasedNode = libyang::releaseRawNode(wrapped);
lyd_free_all(releasedNode);
}
DOCTEST_SUBCASE("Retrieving the raw node")
{
// Calling this should not make memory problems.
libyang::getRawNode(wrapped);
}
REQUIRE_THROWS(libyang::wrapRawNode(nullptr));
}
DOCTEST_SUBCASE("wrapUnmanagedRawNode")
{
lyd_node* node;
lyd_parse_data_mem(ctx, data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, &node);
// With wrapConstRawNode, the node needs to be released manually.
auto node_deleter = std::unique_ptr<lyd_node, node_deleter_t>(node);
// The wrapped node does NOT take ownership of the lyd_node*.
auto wrapped = libyang::wrapUnmanagedRawNode(const_cast<const lyd_node*>(node));
REQUIRE(wrapped.path() == "/example-schema:leafInt32");
// The schema should still be accessible.
REQUIRE(wrapped.schema().name() == "leafInt32");
for (const auto& node : wrapped.childrenDfs()) {
node.path();
}
// It is possible to create unmanaged sets.
for (const auto& node : wrapped.findXPath("/example-schema:leafInt32")) {
node.path();
}
// You can do low level tree manipulation with the unmanaged tree.
// You have two trees `wrapped` and `anotherNodeWrapped` and you want to do some manipulation in C++.
DOCTEST_SUBCASE("Inserting an UNMANAGED node into an unmanaged node")
{
lyd_node* anotherNode;
lyd_new_path(nullptr, ctx, "/example-schema:leafInt8", "0", LYD_VALIDATE_PRESENT, &anotherNode);
auto anotherNodeWrapped = libyang::wrapUnmanagedRawNode(const_cast<const lyd_node*>(anotherNode));
wrapped.insertSibling(anotherNodeWrapped);
// Both are still unmanaged, both are accessible.
REQUIRE(wrapped.path() == "/example-schema:leafInt32");
REQUIRE(anotherNodeWrapped.path() == "/example-schema:leafInt8");
DOCTEST_SUBCASE("no explicit unlink") { }
DOCTEST_SUBCASE("unlink an unmanaged node from an unmanaged node")
{
REQUIRE(wrapped.findPath("/example-schema:leafInt8"));
REQUIRE(anotherNodeWrapped.findPath("/example-schema:leafInt32"));
// After unlink they are not reachable from each other
anotherNodeWrapped.unlink();
REQUIRE(!wrapped.findPath("/example-schema:leafInt8"));
REQUIRE(!anotherNodeWrapped.findPath("/example-schema:leafInt32"));
lyd_free_all(anotherNode);
}
}
// You have a C++ managed node and you want to insert that into an unmanaged node.
DOCTEST_SUBCASE("Inserting a MANAGED node into an unmanaged node")
{
lyd_node* anotherNode;
lyd_new_path(nullptr, ctx, "/example-schema:leafInt8", "0", LYD_VALIDATE_PRESENT, &anotherNode);
auto anotherNodeWrapped = libyang::wrapRawNode(anotherNode);
wrapped.insertSibling(anotherNodeWrapped);
// BOTH are now unmanaged, both are accessible.
REQUIRE(wrapped.path() == "/example-schema:leafInt32");
REQUIRE(anotherNodeWrapped.path() == "/example-schema:leafInt8");
DOCTEST_SUBCASE("no explicit unlink") { }
DOCTEST_SUBCASE("unlink a managed node from an unmanaged node")
{
REQUIRE(wrapped.findPath("/example-schema:leafInt8"));
REQUIRE(anotherNodeWrapped.findPath("/example-schema:leafInt32"));
// After unlink they are not reachable from each other
anotherNodeWrapped.unlink();
REQUIRE(!wrapped.findPath("/example-schema:leafInt8"));
REQUIRE(!anotherNodeWrapped.findPath("/example-schema:leafInt32"));
// this is still unmanaged and we need to delete it
lyd_free_all(anotherNode);
}
}
// You have a C++ managed node and you want to insert an unmanaged node into it.
DOCTEST_SUBCASE("Inserting a UNMANAGED node into a managed node")
{
lyd_node* anotherNode;
lyd_new_path(nullptr, ctx, "/example-schema:leafInt8", "0", LYD_VALIDATE_PRESENT, &anotherNode);
auto anotherNodeWrapped = libyang::wrapRawNode(anotherNode);
anotherNodeWrapped.insertSibling(wrapped);
// BOTH are now managed by C++, both are accessible.
REQUIRE(wrapped.path() == "/example-schema:leafInt32");
REQUIRE(anotherNodeWrapped.path() == "/example-schema:leafInt8");
// Since the original `wrapped` pointer is now managed by C++, we have to release our C management.
(void)node_deleter.release();
}
DOCTEST_SUBCASE("Query identity from unmanaged node")
{
lyd_node* node;
lyd_new_path(nullptr, ctx, "/example-schema:leafFoodTypedef", "example-schema:pizza", LYD_VALIDATE_PRESENT, &node);
auto wrappedNode = libyang::wrapUnmanagedRawNode(const_cast<const lyd_node*>(node));
REQUIRE(wrappedNode.path() == "/example-schema:leafFoodTypedef");
REQUIRE(wrappedNode.asTerm().valueStr() == "example-schema:pizza");
auto typeIdIdentity = std::get<libyang::IdentityRef>(wrappedNode.asTerm().value()).schema;
REQUIRE(typeIdIdentity.module().name() == "example-schema");
REQUIRE(typeIdIdentity.name() == "pizza");
auto node_deleter = std::unique_ptr<lyd_node, node_deleter_t>(node);
}
REQUIRE_THROWS(libyang::wrapUnmanagedRawNode(nullptr));
}
DOCTEST_SUBCASE("wrapUnmanagedRawNode metadata")
{
std::string in = R"(
{
"example-schema:leafInt8": -127,
"@example-schema:leafInt8": {
"ietf-netconf:operation": "merge"
},
"example-schema:leafString": "",
"@example-schema:leafString": {
"ietf-netconf:operation": "remove"
}
}
)";
REQUIRE(ly_ctx_set_searchdir(ctx, PATH_TO_LY_STRING(TESTS_DIR / "yang")) == LY_SUCCESS);
REQUIRE(lys_parse_path(ctx, PATH_TO_LY_STRING(TESTS_DIR / "yang" / "[email protected]"), LYS_IN_YANG, nullptr) == LY_SUCCESS);
lyd_node* node;
REQUIRE(lyd_parse_data_mem(ctx, in.c_str(), LYD_JSON, LYD_PARSE_OPAQ, LYD_VALIDATE_PRESENT, &node) == LY_SUCCESS);
REQUIRE(!!node);
auto node_deleter = std::unique_ptr<lyd_node, node_deleter_t>(node);
auto wrapped = libyang::wrapUnmanagedRawNode(const_cast<const lyd_node*>(node));
REQUIRE(wrapped.path() == "/example-schema:leafInt8");
REQUIRE(wrapped.nextSibling().has_value());
REQUIRE(wrapped.nextSibling()->path() == "/example-schema:leafString");
REQUIRE(!wrapped.meta().empty());
REQUIRE(wrapped.meta().begin()->name() == "operation");
REQUIRE(wrapped.meta().begin()->valueStr() == "merge");
REQUIRE(wrapped.meta().begin()->module().name() == "ietf-netconf");
REQUIRE(!wrapped.nextSibling()->meta().empty());
REQUIRE(wrapped.nextSibling()->meta().begin()->name() == "operation");
REQUIRE(wrapped.nextSibling()->meta().begin()->valueStr() == "remove");
REQUIRE(wrapped.nextSibling()->meta().begin()->module().name() == "ietf-netconf");
}
}