blob: ca9a5daade89ddddb44b112e7291c7e32686083b [file] [log] [blame]
Václav Kubernát07204242018-06-04 18:12:09 +02001/*
2 * Copyright (C) 2018 CESNET, https://photonics.cesnet.cz/
3 * Copyright (C) 2018 FIT CVUT, https://fit.cvut.cz/
4 *
5 * Written by Václav Kubernát <kubervac@fit.cvut.cz>
6 *
7*/
8
Jan Kundráta33cf082019-03-28 11:55:57 +01009#include "trompeloeil_doctest.h"
Jan Kundrát76b3ddc2019-07-03 10:31:27 +020010#include <boost/core/demangle.hpp>
Václav Kubernát24df80e2018-06-06 15:18:03 +020011#include "ast_commands.hpp"
Václav Kubernát07204242018-06-04 18:12:09 +020012#include "parser.hpp"
Václav Kubernátbddbb172018-06-13 16:27:39 +020013#include "static_schema.hpp"
Václav Kubernát509ce652019-05-29 19:46:44 +020014#include "utils.hpp"
Václav Kubernát07204242018-06-04 18:12:09 +020015
Jan Kundrát76b3ddc2019-07-03 10:31:27 +020016std::ostream& operator<<(std::ostream& s, const set_ cmd)
17{
18 return s << "Command SET {path: " << pathToAbsoluteSchemaString(cmd.m_path) << ", type " << boost::core::demangle(cmd.m_data.type().name()) << ", data: " << leafDataToString(cmd.m_data) << "}";
19}
20
Václav Kubernát07204242018-06-04 18:12:09 +020021TEST_CASE("leaf editing")
22{
Václav Kubernátbddbb172018-06-13 16:27:39 +020023 auto schema = std::make_shared<StaticSchema>();
Václav Kubernát744f57f2018-06-29 22:46:26 +020024 schema->addModule("mod");
Václav Kubernáteeb38842019-03-20 19:46:05 +010025 schema->addModule("pizza-module");
Václav Kubernát744f57f2018-06-29 22:46:26 +020026 schema->addContainer("", "mod:contA");
27 schema->addLeaf("", "mod:leafString", yang::LeafDataTypes::String);
28 schema->addLeaf("", "mod:leafDecimal", yang::LeafDataTypes::Decimal);
29 schema->addLeaf("", "mod:leafBool", yang::LeafDataTypes::Bool);
Ivona Oboňová88c78ca2019-07-02 18:40:07 +020030 schema->addLeaf("", "mod:leafInt8", yang::LeafDataTypes::Int8);
31 schema->addLeaf("", "mod:leafInt16", yang::LeafDataTypes::Int16);
32 schema->addLeaf("", "mod:leafInt32", yang::LeafDataTypes::Int32);
33 schema->addLeaf("", "mod:leafInt64", yang::LeafDataTypes::Int64);
34 schema->addLeaf("", "mod:leafUint8", yang::LeafDataTypes::Uint8);
35 schema->addLeaf("", "mod:leafUint16", yang::LeafDataTypes::Uint16);
36 schema->addLeaf("", "mod:leafUint32", yang::LeafDataTypes::Uint32);
37 schema->addLeaf("", "mod:leafUint64", yang::LeafDataTypes::Uint64);
Václav Kubernátab538992019-03-06 15:30:50 +010038 schema->addLeaf("", "mod:leafBinary", yang::LeafDataTypes::Binary);
Václav Kubernáteeb38842019-03-20 19:46:05 +010039 schema->addIdentity(std::nullopt, ModuleValuePair{"mod", "food"});
40 schema->addIdentity(std::nullopt, ModuleValuePair{"mod", "vehicle"});
41 schema->addIdentity(ModuleValuePair{"mod", "food"}, ModuleValuePair{"mod", "pizza"});
42 schema->addIdentity(ModuleValuePair{"mod", "food"}, ModuleValuePair{"mod", "spaghetti"});
43 schema->addIdentity(ModuleValuePair{"mod", "pizza"}, ModuleValuePair{"pizza-module", "hawaii"});
44 schema->addLeafIdentityRef("", "mod:foodIdentRef", ModuleValuePair{"mod", "food"});
45 schema->addLeafIdentityRef("", "mod:pizzaIdentRef", ModuleValuePair{"mod", "pizza"});
46 schema->addLeafIdentityRef("mod:contA", "mod:identInCont", ModuleValuePair{"mod", "pizza"});
Václav Kubernát744f57f2018-06-29 22:46:26 +020047 schema->addLeafEnum("", "mod:leafEnum", {"lol", "data", "coze"});
48 schema->addLeaf("mod:contA", "mod:leafInCont", yang::LeafDataTypes::String);
49 schema->addList("", "mod:list", {"number"});
50 schema->addLeaf("mod:list", "mod:leafInList", yang::LeafDataTypes::String);
Václav Kubernát6a8d1d92019-04-24 20:30:36 +020051 schema->addLeafRef("", "mod:refToString", "mod:leafString");
52 schema->addLeafRef("", "mod:refToInt8", "mod:leafInt8");
Václav Kubernát07204242018-06-04 18:12:09 +020053 Parser parser(schema);
54 std::string input;
55 std::ostringstream errorStream;
56
57 SECTION("valid input")
58 {
59 set_ expected;
60
Václav Kubernátab538992019-03-06 15:30:50 +010061 SECTION("set mod:leafString some_data")
Václav Kubernát07204242018-06-04 18:12:09 +020062 {
Václav Kubernát744f57f2018-06-29 22:46:26 +020063 input = "set mod:leafString some_data";
Václav Kubernát2eaceb82018-10-08 19:56:30 +020064 expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafString")});
Václav Kubernátebca2552018-06-08 19:06:02 +020065 expected.m_data = std::string("some_data");
Václav Kubernát07204242018-06-04 18:12:09 +020066 }
67
Václav Kubernát744f57f2018-06-29 22:46:26 +020068 SECTION("set mod:contA/leafInCont more_data")
Václav Kubernát07204242018-06-04 18:12:09 +020069 {
Václav Kubernát744f57f2018-06-29 22:46:26 +020070 input = "set mod:contA/leafInCont more_data";
Václav Kubernát2eaceb82018-10-08 19:56:30 +020071 expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, container_("contA")});
72 expected.m_path.m_nodes.push_back(dataNode_{leaf_("leafInCont")});
Václav Kubernátebca2552018-06-08 19:06:02 +020073 expected.m_data = std::string("more_data");
74 }
75
Václav Kubernátc18ffac2020-01-10 16:06:06 +010076 SECTION("set mod:contA/leafInCont more d\tata") // spaces in string
77 {
78 input = "set mod:contA/leafInCont more d\tata";
79 expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, container_("contA")});
80 expected.m_path.m_nodes.push_back(dataNode_{leaf_("leafInCont")});
81 expected.m_data = std::string("more d\tata");
82 }
83
Václav Kubernátab538992019-03-06 15:30:50 +010084 SECTION("set mod:list[number=1]/leafInList another_data")
Václav Kubernátebca2552018-06-08 19:06:02 +020085 {
Václav Kubernát744f57f2018-06-29 22:46:26 +020086 input = "set mod:list[number=1]/leafInList another_data";
Václav Kubernátebca2552018-06-08 19:06:02 +020087 auto keys = std::map<std::string, std::string>{
88 {"number", "1"}};
Václav Kubernát2eaceb82018-10-08 19:56:30 +020089 expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, listElement_("list", keys)});
90 expected.m_path.m_nodes.push_back(dataNode_{leaf_("leafInList")});
Václav Kubernátebca2552018-06-08 19:06:02 +020091 expected.m_data = std::string("another_data");
92 }
93
94 SECTION("data types")
95 {
96 SECTION("string")
97 {
Václav Kubernát744f57f2018-06-29 22:46:26 +020098 input = "set mod:leafString somedata";
Václav Kubernát2eaceb82018-10-08 19:56:30 +020099 expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafString")});
Václav Kubernátebca2552018-06-08 19:06:02 +0200100 expected.m_data = std::string("somedata");
101 }
102
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200103 SECTION("int8")
Václav Kubernátebca2552018-06-08 19:06:02 +0200104 {
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200105 input = "set mod:leafInt8 2";
106 expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafInt8")});
107 expected.m_data = int8_t{2};
108 }
109
110 SECTION("negative int8")
111 {
112 input = "set mod:leafInt8 -10";
113 expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafInt8")});
114 expected.m_data = int8_t{-10};
115 }
116
117 SECTION("uint8")
118 {
119 input = "set mod:leafUint8 2";
120 expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafUint8")});
121 expected.m_data = uint8_t{2};
122 }
123
124 SECTION("int16")
125 {
126 input = "set mod:leafInt16 30000";
127 expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafInt16")});
128 expected.m_data = int16_t{30'000};
129 }
130
131 SECTION("uint16")
132 {
133 input = "set mod:leafUint16 30000";
134 expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafUint16")});
135 expected.m_data = uint16_t{30'000};
136 }
137
138 SECTION("int32")
139 {
140 input = "set mod:leafInt32 30000";
141 expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafInt32")});
142 expected.m_data = int32_t{30'000};
143 }
144
145 SECTION("uint32")
146 {
147 input = "set mod:leafUint32 30000";
148 expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafUint32")});
149 expected.m_data = uint32_t{30'000};
150 }
151
152 SECTION("int32")
153 {
154 input = "set mod:leafInt32 30000";
155 expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafInt32")});
156 expected.m_data = int32_t{30'000};
157 }
158
159 SECTION("uint64")
160 {
161 input = "set mod:leafUint64 30000";
162 expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafUint64")});
163 expected.m_data = uint64_t{30'000};
Václav Kubernátebca2552018-06-08 19:06:02 +0200164 }
165
166 SECTION("decimal")
167 {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200168 input = "set mod:leafDecimal 3.14159";
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200169 expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafDecimal")});
Václav Kubernátebca2552018-06-08 19:06:02 +0200170 expected.m_data = 3.14159;
171 }
172
173 SECTION("enum")
174 {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200175 input = "set mod:leafEnum coze";
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200176 expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafEnum")});
Václav Kubernátebca2552018-06-08 19:06:02 +0200177 expected.m_data = enum_("coze");
178 }
179
180 SECTION("bool")
181 {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200182 input = "set mod:leafBool true";
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200183 expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafBool")});
Václav Kubernátebca2552018-06-08 19:06:02 +0200184 expected.m_data = true;
185 }
Václav Kubernátab538992019-03-06 15:30:50 +0100186
187 SECTION("binary")
188 {
189 SECTION("zero ending '='")
190 {
191 input = "set mod:leafBinary This/IsABase64EncodedSomething++/342431++";
192 expected.m_data = binary_{"This/IsABase64EncodedSomething++/342431++"};
193 }
194
195 SECTION("one ending '='")
196 {
197 input = "set mod:leafBinary This/IsABase64EncodedSomething++/342431++=";
198 expected.m_data = binary_{"This/IsABase64EncodedSomething++/342431++="};
199 }
200
201 SECTION("two ending '='")
202 {
203 input = "set mod:leafBinary This/IsABase64EncodedSomething++/342431++==";
204 expected.m_data = binary_{"This/IsABase64EncodedSomething++/342431++=="};
205 }
206 expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafBinary")});
207 }
Václav Kubernáteeb38842019-03-20 19:46:05 +0100208
209 SECTION("identityRef")
210 {
211 SECTION("foodIdentRef")
212 {
213 input = "set mod:foodIdentRef ";
214 expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("foodIdentRef")});
215
216 SECTION("food")
217 {
218 input += "food";
219 expected.m_data = identityRef_("food");
220 }
221 SECTION("mod:food")
222 {
223 input += "mod:food";
224 expected.m_data = identityRef_("mod", "food");
225 }
226 SECTION("pizza")
227 {
228 input += "pizza";
229 expected.m_data = identityRef_("pizza");
230 }
231 SECTION("mod:pizza")
232 {
233 input += "mod:pizza";
234 expected.m_data = identityRef_("mod", "pizza");
235 }
236 SECTION("pizza-module:hawaii")
237 {
238 input += "pizza-module:hawaii";
239 expected.m_data = identityRef_("pizza-module", "hawaii");
240 }
241 }
242 SECTION("pizzaIdentRef")
243 {
244 input = "set mod:pizzaIdentRef ";
245 expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("pizzaIdentRef")});
246 SECTION("pizza")
247 {
248 input += "pizza";
249 expected.m_data = identityRef_("pizza");
250 }
251 SECTION("mod:pizza")
252 {
253 input += "mod:pizza";
254 expected.m_data = identityRef_("mod", "pizza");
255 }
256 SECTION("pizza-module:hawaii")
257 {
258 input += "pizza-module:hawaii";
259 expected.m_data = identityRef_("pizza-module", "hawaii");
260 }
261 }
262 SECTION("mod:contA/identInCont")
263 {
264 input = "set mod:contA/identInCont ";
265 expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, container_("contA")});
266 expected.m_path.m_nodes.push_back(dataNode_(leaf_("identInCont")));
267 SECTION("pizza")
268 {
269 input += "pizza";
270 expected.m_data = identityRef_("pizza");
271 }
272 SECTION("mod:pizza")
273 {
274 input += "mod:pizza";
275 expected.m_data = identityRef_("mod", "pizza");
276 }
277 SECTION("pizza-module:hawaii")
278 {
279 input += "pizza-module:hawaii";
280 expected.m_data = identityRef_("pizza-module", "hawaii");
281 }
282 }
283 }
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200284 SECTION("leafRef")
285 {
286 SECTION("refToString")
287 {
288 input = "set mod:refToString blabal";
289 expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("refToString")});
290 expected.m_data = std::string("blabal");
291 }
292
293 SECTION("refToInt8")
294 {
295 input = "set mod:refToInt8 42";
296 expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("refToInt8")});
297 expected.m_data = int8_t{42};
298 }
299 }
Václav Kubernát07204242018-06-04 18:12:09 +0200300 }
301
302 command_ command = parser.parseCommand(input, errorStream);
303 REQUIRE(command.type() == typeid(set_));
304 REQUIRE(boost::get<set_>(command) == expected);
305 }
306
307 SECTION("invalid input")
308 {
309 SECTION("missing space between a command and its arguments")
310 {
311 SECTION("setleaf some_data")
312 {
313 input = "setleaf some_data";
314 }
315 }
316
317 SECTION("missing space between arguments")
318 {
319 SECTION("set leaflol")
320 {
321 input = "set leaflol";
322 }
323 }
324
325 SECTION("non-leaf identifiers")
326 {
327 SECTION("set nonexistent blabla")
328 {
329 input = "set nonexistent blabla";
330 }
331
332 SECTION("set contA abde")
333 {
334 input = "set contA abde";
335 }
336 }
337
Václav Kubernátebca2552018-06-08 19:06:02 +0200338 SECTION("wrong types")
339 {
340 SECTION("set leafBool blabla")
341 {
342 input = "set leafBool blabla";
343 }
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200344 SECTION("set leafUint8 blabla")
Václav Kubernátebca2552018-06-08 19:06:02 +0200345 {
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200346 input = "set leafUint8 blabla";
Václav Kubernátebca2552018-06-08 19:06:02 +0200347 }
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200348 SECTION("set leafUint8 -5")
Václav Kubernátebca2552018-06-08 19:06:02 +0200349 {
Ivona Oboňová88c78ca2019-07-02 18:40:07 +0200350 input = "set leafUint8 -5";
351 }
352 SECTION("set leafInt8 blabla")
353 {
354 input = "set leafInt8 blabla";
355 }
356 SECTION("set leafInt8 130")
357 {
358 input = "set leafInt8 130";
359 }
360 SECTION("set leafUint16 blabla")
361 {
362 input = "set leafUint16 blabla";
363 }
364 SECTION("set leafInt16 blabla")
365 {
366 input = "set leafInt16 blabla";
367 }
368 SECTION("set leafUint32 blabla")
369 {
370 input = "set leafUint32 blabla";
371 }
372 SECTION("set leafInt32 blabla")
373 {
374 input = "set leafInt32 blabla";
375 }
376 SECTION("set leafUint64 blabla")
377 {
378 input = "set leafUint64 blabla";
379 }
380 SECTION("set leafInt64 blabla")
381 {
382 input = "set leafInt64 blabla";
Václav Kubernátebca2552018-06-08 19:06:02 +0200383 }
384 SECTION("set leafEnum blabla")
385 {
386 input = "set leafEnum blabla";
387 }
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200388 SECTION("set mod:refToInt8 blabla")
389 {
390 input = "set mod:refToInt8 blabla";
391 }
Václav Kubernátebca2552018-06-08 19:06:02 +0200392 }
393
Václav Kubernátab538992019-03-06 15:30:50 +0100394 SECTION("wrong base64 strings")
395 {
396 SECTION("invalid character")
397 input = "set leafBinary dbahj-";
398 SECTION("equal sign in the middle")
399 input = "set leafBinary db=ahj";
400 }
401
Václav Kubernáteeb38842019-03-20 19:46:05 +0100402 SECTION("non-existing identity")
403 {
404 input = "set mod:foodIdentRef identityBLABLA";
405 }
406
407 SECTION("setting identities with wrong bases")
408 {
409 SECTION("set mod:foodIdentRef mod:vehicle")
410 {
411 input = "set mod:foodIdentRef mod:vehicle";
412 }
413 SECTION("set mod:pizzaIdentRef mod:food")
414 {
415 input = "set mod:pizzaIdentRef mod:food";
416 }
417 }
418 SECTION("setting different module identities without prefix")
419 {
420 input = "set mod:pizzaIdentRef hawaii";
421 }
422 SECTION("identity prefix without name")
423 {
424 input = "set mod:contA/identInCont pizza-module:";
425 }
426
Jan Kundrátc381e632019-03-14 13:39:11 +0100427 REQUIRE_THROWS_AS(parser.parseCommand(input, errorStream), InvalidCommandException);
Václav Kubernát07204242018-06-04 18:12:09 +0200428 }
429}