Skip to content

Commit 8270548

Browse files
committed
C++: Extend unit tests
Signed-off-by: Matija Amidzic <[email protected]>
1 parent b552d9f commit 8270548

File tree

4 files changed

+368
-8
lines changed

4 files changed

+368
-8
lines changed

swig/cpp/tests/test_libyang.cpp

Lines changed: 136 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,142 @@ TEST(test_ly_set)
431431
ASSERT_EQ(0, set->number());
432432
} catch( const std::exception& e ) {
433433
mt::printFailed(e.what(), stdout);
434-
return;
434+
throw;
435+
}
436+
}
437+
438+
TEST(test_ly_ctx_new_ylpath)
439+
{
440+
const char *yang_folder = TESTS_DIR "/api/files";
441+
const char *path = TESTS_DIR "/api/files/ylpath.xml";
442+
443+
try {
444+
auto ctx = std::make_shared<libyang::Context>(yang_folder, path, LYD_XML, 0);
445+
ASSERT_FALSE(nullptr == ctx);
446+
auto list = std::make_shared<std::vector<std::string>>(ctx->get_searchdirs());
447+
ASSERT_FALSE(nullptr == list);
448+
ASSERT_EQ(1, list->size());
449+
450+
} catch( const std::exception& e ) {
451+
mt::printFailed(e.what(), stdout);
452+
throw;
453+
}
454+
}
455+
456+
TEST(test_ly_ctx_new_ylmem)
457+
{
458+
const char *yang_folder = TESTS_DIR "/api/files";
459+
460+
try {
461+
auto ctx = std::make_shared<libyang::Context>(yang_folder);
462+
ASSERT_FALSE(nullptr == ctx);
463+
auto info = ctx->info();
464+
ASSERT_FALSE(nullptr == info);
465+
ASSERT_EQ(LYD_VAL_OK, info->validity());
466+
467+
auto mem = info->print_mem(LYD_XML, 0);
468+
auto new_ctx = std::make_shared<libyang::Context>(yang_folder, LYD_XML, mem.data(), 0);
469+
ASSERT_FALSE(nullptr == new_ctx);
470+
auto list = std::make_shared<std::vector<std::string>>(new_ctx->get_searchdirs());
471+
ASSERT_FALSE(nullptr == list);
472+
ASSERT_EQ(1, list->size());
473+
} catch( const std::exception& e ) {
474+
mt::printFailed(e.what(), stdout);
475+
throw;
476+
}
477+
}
478+
479+
TEST(test_ly_ctx_get_module_iter)
480+
{
481+
const char *yang_folder = TESTS_DIR "/api/files";
482+
const char *module_name = "a";
483+
484+
try {
485+
auto ctx = std::make_shared<libyang::Context>(yang_folder);
486+
ASSERT_NOTNULL(ctx);
487+
488+
auto module = ctx->load_module(module_name);
489+
ASSERT_NOTNULL(module);
490+
ASSERT_STREQ(module_name, module->name());
491+
auto list = std::make_shared<std::vector<libyang::S_Module>>(ctx->get_module_iter());
492+
ASSERT_NOTNULL(list);
493+
ASSERT_EQ(7, list->size());
494+
} catch( const std::exception& e ) {
495+
mt::printFailed(e.what(), stdout);
496+
throw;
497+
}
498+
}
499+
500+
TEST(test_ly_ctx_get_disabled_module_iter)
501+
{
502+
const char *yang_folder = TESTS_DIR "/api/files";
503+
const char *module_name = "x";
504+
505+
try {
506+
auto ctx = std::make_shared<libyang::Context>(yang_folder);
507+
ASSERT_NOTNULL(ctx);
508+
509+
auto module = ctx->load_module(module_name);
510+
ASSERT_NOTNULL(module);
511+
ASSERT_STREQ(module_name, module->name());
512+
// FIXME no way to disable module from here
513+
514+
auto list = std::make_shared<std::vector<libyang::S_Module>>(ctx->get_disabled_module_iter());
515+
ASSERT_NOTNULL(list);
516+
ASSERT_EQ(0, list->size());
517+
} catch( const std::exception& e ) {
518+
mt::printFailed(e.what(), stdout);
519+
throw;
520+
}
521+
}
522+
523+
TEST(test_ly_ctx_data_instantiables)
524+
{
525+
const char *yang_folder = TESTS_DIR "/api/files";
526+
const char *module_name = "b";
527+
528+
try {
529+
auto ctx = std::make_shared<libyang::Context>(yang_folder);
530+
ASSERT_NOTNULL(ctx);
531+
532+
auto module = ctx->load_module(module_name);
533+
ASSERT_NOTNULL(module);
534+
ASSERT_STREQ(module_name, module->name());
535+
536+
auto list = std::make_shared<std::vector<libyang::S_Schema_Node>>(ctx->data_instantiables(0));
537+
ASSERT_NOTNULL(list);
538+
ASSERT_EQ(5, list->size());
539+
} catch( const std::exception& e ) {
540+
mt::printFailed(e.what(), stdout);
541+
throw;
542+
}
543+
}
544+
545+
TEST(test_ly_ctx_get_node)
546+
{
547+
const char *yang_folder = TESTS_DIR "/api/files";
548+
const char *module_name = "b";
549+
550+
try {
551+
auto ctx = std::make_shared<libyang::Context>(yang_folder);
552+
ASSERT_NOTNULL(ctx);
553+
554+
auto module = ctx->load_module(module_name);
555+
ASSERT_NOTNULL(module);
556+
ASSERT_STREQ(module_name, module->name());
557+
558+
auto list = std::make_shared<std::vector<libyang::S_Schema_Node>>(ctx->data_instantiables(0));
559+
ASSERT_NOTNULL(list);
560+
ASSERT_EQ(5, list->size());
561+
562+
auto schema = list->back();
563+
ASSERT_NOTNULL(schema);
564+
auto node = ctx->get_node(schema, "/b:x/b:bubba", 0);
565+
ASSERT_NOTNULL(node);
566+
567+
} catch( const std::exception& e ) {
568+
mt::printFailed(e.what(), stdout);
569+
throw;
435570
}
436571
}
437572

swig/cpp/tests/test_tree_data.cpp

Lines changed: 147 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ const char *lys_module_a = \
6565
<type name=\"string\"/> \
6666
<default value=\"def\"/> \
6767
</leaf> \
68+
<anydata name=\"any-data\"/> \
6869
</container> \
6970
<leaf name=\"y\"><type name=\"string\"/></leaf> \
7071
<anyxml name=\"any\"/> \
@@ -231,10 +232,6 @@ TEST(test_ly_data_node)
231232

232233
auto new_node = std::make_shared<libyang::Data_Node>(root, root->child()->schema()->module(), "bar-y");
233234
ASSERT_NOTNULL(new_node);
234-
new_node = std::make_shared<libyang::Data_Node>(root, root->schema()->module(), "number32", "100");
235-
ASSERT_NOTNULL(new_node);
236-
auto dup_node = new_node->dup(0);
237-
ASSERT_NOTNULL(dup_node);
238235
} catch( const std::exception& e ) {
239236
mt::printFailed(e.what(), stdout);
240237
throw;
@@ -480,11 +477,11 @@ TEST(test_ly_data_node_find_instance)
480477
ASSERT_EQ(1, set->number());
481478
} catch (const std::exception& e) {
482479
mt::printFailed(e.what(), stdout);
483-
return;
480+
throw;
484481
}
485482
}
486483

487-
TEST(test_ly_data_node_validate)
484+
TEST(test_ly_data_node_validate_ctx)
488485
{
489486
const char *yang_folder = TESTS_DIR "/api/files";
490487
const char *config_file = TESTS_DIR "/api/files/a.xml";
@@ -620,7 +617,150 @@ TEST(test_ly_data_node_path)
620617
ASSERT_STREQ("/a:x/bubba", path.c_str());
621618
} catch (const std::exception& e) {
622619
mt::printFailed(e.what(), stdout);
623-
return;
620+
throw;
621+
}
622+
}
623+
624+
TEST(test_ly_data_node_leaf)
625+
{
626+
const char *yang_folder = TESTS_DIR "/api/files";
627+
const char *config_file = TESTS_DIR "/api/files/a.xml";
628+
629+
try {
630+
auto ctx = std::make_shared<libyang::Context>(yang_folder);
631+
ASSERT_NOTNULL(ctx);
632+
ctx->parse_module_mem(lys_module_a, LYS_IN_YIN);
633+
auto root = ctx->parse_data_path(config_file, LYD_XML, LYD_OPT_CONFIG | LYD_OPT_STRICT);
634+
ASSERT_NOTNULL(root);
635+
636+
auto new_node = std::make_shared<libyang::Data_Node>(root, root->schema()->module(), "number32", "100");
637+
ASSERT_NOTNULL(new_node);
638+
} catch( const std::exception& e ) {
639+
mt::printFailed(e.what(), stdout);
640+
throw;
641+
}
642+
}
643+
644+
TEST(test_ly_data_node_anydata)
645+
{
646+
const char *yang_folder = TESTS_DIR "/api/files";
647+
const char *config_file = TESTS_DIR "/api/files/a.xml";
648+
649+
try {
650+
auto ctx = std::make_shared<libyang::Context>(yang_folder);
651+
ASSERT_NOTNULL(ctx);
652+
ctx->parse_module_mem(lys_module_a, LYS_IN_YIN);
653+
auto root = ctx->parse_data_path(config_file, LYD_XML, LYD_OPT_CONFIG | LYD_OPT_STRICT);
654+
ASSERT_NOTNULL(root);
655+
auto mod = ctx->get_module("a", nullptr, 1);
656+
657+
auto new_node = std::make_shared<libyang::Data_Node>(root, mod, "any-data", "100", LYD_ANYDATA_CONSTSTRING);
658+
ASSERT_NOTNULL(new_node);
659+
} catch( const std::exception& e ) {
660+
mt::printFailed(e.what(), stdout);
661+
throw;
662+
}
663+
}
664+
665+
TEST(test_ly_data_node_dup)
666+
{
667+
const char *yang_folder = TESTS_DIR "/api/files";
668+
const char *config_file = TESTS_DIR "/api/files/a.xml";
669+
670+
try {
671+
auto ctx = std::make_shared<libyang::Context>(yang_folder);
672+
ASSERT_NOTNULL(ctx);
673+
ctx->parse_module_mem(lys_module_a, LYS_IN_YIN);
674+
auto root = ctx->parse_data_path(config_file, LYD_XML, LYD_OPT_CONFIG | LYD_OPT_STRICT);
675+
ASSERT_NOTNULL(root);
676+
677+
auto new_node = std::make_shared<libyang::Data_Node>(root, root->child()->schema()->module(), "bar-y");
678+
ASSERT_NOTNULL(new_node);
679+
auto dup_node = new_node->dup(0);
680+
ASSERT_NOTNULL(dup_node);
681+
} catch( const std::exception& e ) {
682+
mt::printFailed(e.what(), stdout);
683+
throw;
684+
}
685+
}
686+
687+
TEST(test_ly_data_node_dup_to_ctx)
688+
{
689+
const char *sch = "module x {"
690+
" namespace urn:x;"
691+
" prefix x;"
692+
" leaf x { type string; }}";
693+
const char *data = "<x xmlns=\"urn:x\">hello</x>";
694+
695+
try {
696+
auto ctx1 = std::make_shared<libyang::Context>(nullptr);
697+
ASSERT_NOTNULL(ctx1);
698+
ctx1->parse_module_mem(sch, LYS_IN_YANG);
699+
auto data1 = ctx1->parse_data_mem(data, LYD_XML, LYD_OPT_CONFIG | LYD_OPT_STRICT);
700+
ASSERT_NOTNULL(data1);
701+
702+
auto ctx2 = std::make_shared<libyang::Context>(nullptr);
703+
ASSERT_NOTNULL(ctx2);
704+
// we expect NULL due to missing schema in the second ctx
705+
auto dup_node = data1->dup_to_ctx(1, ctx2);
706+
ASSERT_NULL(dup_node);
707+
708+
ctx2->parse_module_mem(sch, LYS_IN_YANG);
709+
// now we expect success due to schema being added to the second ctx
710+
dup_node = data1->dup_to_ctx(0, ctx2);
711+
ASSERT_NOTNULL(dup_node);
712+
} catch( const std::exception& e ) {
713+
mt::printFailed(e.what(), stdout);
714+
throw;
715+
}
716+
}
717+
718+
TEST(test_ly_data_node_validate_node)
719+
{
720+
const char *yang_folder = TESTS_DIR "/api/files";
721+
const char *config_file = TESTS_DIR "/api/files/a.xml";
722+
723+
try {
724+
auto ctx = std::make_shared<libyang::Context>(yang_folder);
725+
ASSERT_NOTNULL(ctx);
726+
ctx->parse_module_mem(lys_module_a, LYS_IN_YIN);
727+
auto root = ctx->parse_data_path(config_file, LYD_XML, LYD_OPT_CONFIG | LYD_OPT_STRICT);
728+
ASSERT_NOTNULL(root);
729+
730+
auto rc = root->validate(LYD_OPT_CONFIG, ctx);
731+
ASSERT_EQ(0, rc);
732+
auto new_node = std::make_shared<libyang::Data_Node>(root, root->schema()->module(), "number32", "1");
733+
ASSERT_NOTNULL(new_node);
734+
rc = root->validate(LYD_OPT_CONFIG, new_node);
735+
ASSERT_EQ(0, rc);
736+
} catch (const std::exception& e) {
737+
mt::printFailed(e.what(), stdout);
738+
throw;
739+
}
740+
}
741+
742+
TEST(test_ly_data_node_validate_value)
743+
{
744+
const char *yang_folder = TESTS_DIR "/api/files";
745+
const char *config_file = TESTS_DIR "/api/files/a.xml";
746+
747+
try {
748+
auto ctx = std::make_shared<libyang::Context>(yang_folder);
749+
ASSERT_NOTNULL(ctx);
750+
ctx->parse_module_mem(lys_module_a, LYS_IN_YIN);
751+
auto root = ctx->parse_data_path(config_file, LYD_XML, LYD_OPT_CONFIG | LYD_OPT_STRICT);
752+
ASSERT_NOTNULL(root);
753+
754+
auto rc = root->validate(LYD_OPT_CONFIG, ctx);
755+
ASSERT_EQ(0, rc);
756+
auto new_node = std::make_shared<libyang::Data_Node>(root, root->schema()->module(), "number32", "1");
757+
ASSERT_NOTNULL(new_node);
758+
ASSERT_EQ(new_node->validate_value("1"), 0);
759+
ASSERT_EQ(new_node->validate_value("100"), 0);
760+
ASSERT_EQ(new_node->validate_value("110000000"), 0);
761+
} catch (const std::exception &e) {
762+
mt::printFailed(e.what(), stdout);
763+
throw;
624764
}
625765
}
626766

swig/cpp/tests/test_tree_schema.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,4 +515,52 @@ TEST(test_ly_schema_node_path)
515515
}
516516
}
517517

518+
TEST(test_ly_module_data_instatiables)
519+
{
520+
const char *yang_folder = TESTS_DIR "/api/files";
521+
const char *module_name1 = "b";
522+
523+
try {
524+
auto ctx = std::make_shared<libyang::Context>(yang_folder);
525+
ASSERT_NOTNULL(ctx);
526+
527+
auto module = ctx->load_module(module_name1);
528+
ASSERT_NOTNULL(module);
529+
ASSERT_STREQ(module_name1, module->name());
530+
531+
auto list = std::make_shared<std::vector<libyang::S_Schema_Node>>(module->data_instantiables(0));
532+
ASSERT_NOTNULL(list);
533+
ASSERT_EQ(1, list->size());
534+
} catch( const std::exception& e ) {
535+
mt::printFailed(e.what(), stdout);
536+
throw;
537+
}
538+
}
539+
540+
TEST(test_ly_schema_child_instatiables)
541+
{
542+
const char *yang_folder = TESTS_DIR "/api/files";
543+
const char *module_name = "b";
544+
545+
try {
546+
auto ctx = std::make_shared<libyang::Context>(yang_folder);
547+
ASSERT_NOTNULL(ctx);
548+
549+
auto module = ctx->load_module(module_name);
550+
ASSERT_NOTNULL(module);
551+
ASSERT_STREQ(module_name, module->name());
552+
553+
auto list = std::make_shared<std::vector<libyang::S_Schema_Node>>(module->data_instantiables(0));
554+
ASSERT_NOTNULL(list);
555+
ASSERT_EQ(1, list->size());
556+
auto child_list = std::make_shared<std::vector<libyang::S_Schema_Node>>(list->front()->child_instantiables(0));
557+
ASSERT_NOTNULL(child_list);
558+
ASSERT_EQ(3, child_list->size());
559+
560+
} catch( const std::exception& e ) {
561+
mt::printFailed(e.what(), stdout);
562+
throw;
563+
}
564+
}
565+
518566
TEST_MAIN();

0 commit comments

Comments
 (0)