CiscoDevNet / ydk-gen

Generate model-driven APIs from YANG models
http://ciscodevnet.github.io/ydk-gen/
Apache License 2.0
135 stars 74 forks source link

C++ CodecService fails to encode non-top level entity to XML subtree #1049

Closed ygorelik closed 3 years ago

ygorelik commented 3 years ago

Current Behavior

When tried to get XML subtree from non-top level entity the CodecService fails with segmentation violation.

Steps to Reproduce

Add the following unit test to the suite of bundle tests:

TEST_CASE("xml_codec_augment_subtree")
{
    CodecServiceProvider codec_provider{EncodingFormat::XML};
    CodecService codec_service{};

    auto passive = make_unique<ydktest_sanity::Runner::Passive>();
    passive->name = "xyz";

    auto ifc = make_shared<ydktest_sanity::Runner::Passive::Interfac>();
    ifc->test = "abc";
    passive->interfac.append(ifc);

    passive->testc->xyz = make_shared<ydktest_sanity::Runner::Passive::Testc::Xyz>();
    passive->testc->xyz->parent = passive.get();
    passive->testc->xyz->xyz = 25;

    auto xml = codec_service.encode(codec_provider, *passive, true, true);
    cout << xml << endl;
}

Run the unit test and observe the issue:

~/ydk-gen/sdk/cpp/tests/build$ ./ydk_bundle_test xml_codec_augment_subtree

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ydk_bundle_test is a Catch v1.7.1 host application.
Run with -? for options

-------------------------------------------------------------------------------
xml_codec_augment_subtree
-------------------------------------------------------------------------------
/home/ygorelik/ydk-gen/sdk/cpp/tests/test_xml_subtree.cpp:370
...............................................................................

/home/ygorelik/ydk-gen/sdk/cpp/tests/test_xml_subtree.cpp:370: FAILED:
due to a fatal error condition:
  SIGSEGV - Segmentation violation signal

===============================================================================
test cases: 1 | 1 failed
assertions: 1 | 1 failed

Segmentation fault (core dumped)

System Information

YDK-0.8.5

ygorelik commented 3 years ago

Suggested workaround

Initialize root schema by creating connection to the device, then use XmlSubtreeCodec to get the XML subtree:

TEST_CASE("xml_codec_augment_subtree")
{
    ydk::path::Repository repo{TEST_HOME};
    ydk::path::NetconfSession session{repo, "127.0.0.1", "admin", "admin", 12022};
    XmlSubtreeCodec xml_codec{};

    auto passive = make_unique<ydktest_sanity::Runner::Passive>();
    passive->name = "xyz";

    auto ifc = make_shared<ydktest_sanity::Runner::Passive::Interfac>();
    ifc->test = "abc";
    passive->interfac.append(ifc);

    passive->testc->xyz = make_shared<ydktest_sanity::Runner::Passive::Testc::Xyz>();
    passive->testc->xyz->parent = passive.get();
    passive->testc->xyz->xyz = 25;

    auto xml = xml_codec.encode(*passive, session.get_root_schema());
    cout << xml << endl;
}

Run the test and observe correct results:

ygorelik@bionic:~/ydk-gen/sdk/cpp/tests/build$ ./ydk_bundle_test xml_codec_augment_subtree
<passive xmlns="http://cisco.com/ns/yang/ydktest-sanity">
  <name>xyz</name>
  <interfac>
    <test>abc</test>
  </interfac>
  <testc xmlns="http://cisco.com/ns/yang/ydktest-sanity-augm">
    <xyz>
      <xyz>25</xyz>
    </xyz>
  </testc>
</passive>
===============================================================================
test cases: 1 | 1 passed
assertions: - none -
ygorelik commented 3 years ago

Resolved the issue in my fork. It will be included to the next patch release - 0.8.5.3.