Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validation does not catch missing mandatory leaf #51

Closed
lukasmacko opened this issue Apr 19, 2016 · 0 comments
Closed

Validation does not catch missing mandatory leaf #51

lukasmacko opened this issue Apr 19, 2016 · 0 comments

Comments

@lukasmacko
Copy link
Contributor

Missing mandatory leaf is not detected in a sibling subtree by lyd_validate function. However, invalid data can not be loaded by lyd_parse_path.
libyang version 0.8.54

#include <stdio.h>
#include <stdlib.h>
#include <libyang/libyang.h>

#define XPATH "/ietf-interfaces:interfaces/interface[name='gigaeth0']/ietf-ip:ipv6/address[ip='fe80::ab8']/prefix-length"


int main(int argc, char **argv)
{
   /* create context load module*/
   struct ly_ctx *ctx = ly_ctx_new(".");
   lys_parse_path(ctx, "iana-if-type.yin", LYS_IN_YIN);
   const struct lys_module *module = lys_parse_path(ctx, "ietf-ip@2014-06-16.yin", LYS_IN_YIN);
   if (NULL == module) {
      return 1;
   }

   /* load data file*/
   struct lyd_node *root = lyd_parse_path(ctx, "data.xml", LYD_XML, LYD_OPT_STRICT | LYD_OPT_CONFIG);
   if (NULL == root || 0 != lyd_validate(&root, LYD_OPT_STRICT | LYD_OPT_NOAUTODEL | LYD_OPT_CONFIG)){
      puts("Validation failed");
      return 2;
   }

   /* remove mandatory element */
   struct ly_set *set = lyd_get_node(root, XPATH);
   if (NULL == set || set->number < 1){
      return 3;
   }
   lyd_unlink(set->set.d[0]);
   lyd_free(set->set.d[0]);

   /* VALIDATION SHOULD FAIL !!!*/
   if (0 != lyd_validate(&root, LYD_OPT_STRICT | LYD_OPT_NOAUTODEL | LYD_OPT_CONFIG)){
      puts("Validation failed");
      return 4;
   }

   /* write invalid content */   
   FILE *f = fopen("out.xml", "w");
   if (NULL == f) {
     return 5;
   }
   if (0 != lyd_print_file(f, root, LYD_XML, LYP_WITHSIBLINGS | LYP_FORMAT)){
      return 6;
   }

   /* unable to load "valid" written content */
   struct lyd_node *edited = lyd_parse_path(ctx, "out.xml", LYD_XML, LYD_OPT_STRICT | LYD_OPT_CONFIG);
   if (NULL == edited){
      puts("Error");
      return 7;
   }

   ly_set_free(set);
   lyd_free_withsiblings(root);
   ly_ctx_destroy(ctx, NULL);
   return 0;

}

data.xml

<interfaces xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces">
  <interface>
    <name>eth0</name>
    <description>Ethernet 0</description>
    <type>ethernetCsmacd</type>
    <enabled>true</enabled>
    <ipv4 xmlns="urn:ietf:params:xml:ns:yang:ietf-ip">
      <enabled>true</enabled>
      <mtu>1500</mtu>
      <address>
        <ip>192.168.2.100</ip>
        <prefix-length>24</prefix-length>
      </address>
    </ipv4>
  </interface>
  <interface>
    <name>eth1</name>
    <description>Ethernet 1</description>
    <type>ethernetCsmacd</type>
    <enabled>true</enabled>
    <ipv4 xmlns="urn:ietf:params:xml:ns:yang:ietf-ip">
      <enabled>true</enabled>
      <mtu>1500</mtu>
      <address>
        <ip>10.10.1.5</ip>
        <prefix-length>16</prefix-length>
      </address>
    </ipv4>
  </interface>
  <interface>
    <name>gigaeth0</name>
    <description>GigabitEthernet 0</description>
    <type>ethernetCsmacd</type>
    <enabled>false</enabled>
    <ipv6 xmlns="urn:ietf:params:xml:ns:yang:ietf-ip">
      <address>
        <ip>fe80::ab8</ip>
        <prefix-length>64</prefix-length>
      </address>
    </ipv6>
  </interface>
</interfaces>

Used .yin files can be found here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant