Skip to content

Commit

Permalink
parsers BUGFIX updating hash of keyless lists
Browse files Browse the repository at this point in the history
do not update keyless list's hash (and do not add it into the parent's
hash table) on its child node change if the keyless lis t 's hash is 0,
i.e. it is being parsed and hash will be set later when all the children
are parsed.

Fixes #867
  • Loading branch information
rkrejci committed Sep 4, 2019
1 parent 83fbe2f commit 8d5678e
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/tree_data.c
Original file line number Diff line number Diff line change
Expand Up @@ -382,9 +382,9 @@ lyd_keyless_list_hash_change(struct lyd_node *parent)
{
int r;

while (parent && (parent->schema->flags & LYS_CONFIG_R)) {
while (parent && !(parent->schema->flags & LYS_CONFIG_W)) {
if (parent->schema->nodetype == LYS_LIST) {
if (!((struct lys_node_list *)parent->schema)->keys_size) {
if (parent->hash && !((struct lys_node_list *)parent->schema)->keys_size) {
if (parent->parent && parent->parent->ht) {
/* remove the list from the parent */
r = lyht_remove(parent->parent->ht, &parent, parent->hash);
Expand Down
15 changes: 15 additions & 0 deletions tests/data/files/keyless.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"keyless:c": {
"x": [
"1",
"2",
"3",
"4"
],
"l": [
{
"a": "a"
}
]
}
}
9 changes: 9 additions & 0 deletions tests/data/files/keyless.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<c xmlns="urn:test:keyless">
<x>1</x>
<x>2</x>
<x>3</x>
<x>4</x>
<l>
<a>a</a>
</l>
</c>
13 changes: 13 additions & 0 deletions tests/data/files/keyless.yang
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module keyless {
namespace urn:test:keyless;
prefix m;
yang-version 1.1;

container c {
leaf-list x { type string;}
list l {
config false;
leaf a { type string;}
}
}
}
55 changes: 55 additions & 0 deletions tests/data/test_parse_print.c
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,8 @@ test_parse_print_json(void **state)
st->str2 = NULL;
lyd_free(st->dt);
st->dt = NULL;
lyd_free(st->rpc_act);
st->rpc_act = NULL;

/* inline notif */
fd = open(innotif, O_RDONLY);
Expand All @@ -573,6 +575,58 @@ test_parse_print_json(void **state)
assert_string_equal(st->str1, st->str2);
}

static void test_parse_print_keyless(void **state)
{
struct state *st = (*state);
struct stat s;
int fd;
const char *yang = TESTS_DIR"/data/files/keyless.yang";
const char *json = TESTS_DIR"/data/files/keyless.json";
const char *xml = TESTS_DIR"/data/files/keyless.xml";


ly_ctx_destroy(st->ctx, NULL);
assert_non_null(st->ctx = ly_ctx_new(TESTS_DIR"/data/files", 0));
assert_non_null(st->mod = lys_parse_path(st->ctx, yang, LYS_IN_YANG));

/* keyless list - JSON */
fd = open(json, O_RDONLY);
fstat(fd, &s);
st->str1 = malloc(s.st_size + 1);
assert_ptr_not_equal(st->str1, NULL);
assert_int_equal(read(fd, st->str1, s.st_size), s.st_size);
st->str1[s.st_size] = '\0';

st->dt = lyd_parse_path(st->ctx, json, LYD_JSON, LYD_OPT_DATA | LYD_OPT_DATA_NO_YANGLIB, NULL);
assert_ptr_not_equal(st->dt, NULL);
lyd_print_mem(&(st->str2), st->dt, LYD_JSON, LYP_FORMAT);

assert_string_equal(st->str1, st->str2);

close(fd);
fd = -1;
free(st->str1);
st->str1 = NULL;
free(st->str2);
st->str2 = NULL;
lyd_free_withsiblings(st->dt);
st->dt = NULL;

/* keyless list - XML */
fd = open(xml, O_RDONLY);
fstat(fd, &s);
st->str1 = malloc(s.st_size + 1);
assert_ptr_not_equal(st->str1, NULL);
assert_int_equal(read(fd, st->str1, s.st_size), s.st_size);
st->str1[s.st_size] = '\0';

st->dt = lyd_parse_path(st->ctx, xml, LYD_XML, LYD_OPT_DATA | LYD_OPT_DATA_NO_YANGLIB, NULL);
assert_ptr_not_equal(st->dt, NULL);
lyd_print_mem(&(st->str2), st->dt, LYD_XML, LYP_FORMAT);

assert_string_equal(st->str1, st->str2);
}

static void
test_parse_print_lyb(void **state)
{
Expand Down Expand Up @@ -982,6 +1036,7 @@ int main(void)
cmocka_unit_test_teardown(test_parse_print_yang, teardown_f),
cmocka_unit_test_setup_teardown(test_parse_print_xml, setup_f, teardown_f),
cmocka_unit_test_setup_teardown(test_parse_print_json, setup_f, teardown_f),
cmocka_unit_test_setup_teardown(test_parse_print_keyless, setup_f, teardown_f),
cmocka_unit_test_setup_teardown(test_parse_print_lyb, setup_f, teardown_f),
cmocka_unit_test_setup_teardown(test_parse_print_oookeys_xml, setup_f, teardown_f),
cmocka_unit_test_setup_teardown(test_parse_print_oookeys_json, setup_f, teardown_f),
Expand Down

0 comments on commit 8d5678e

Please sign in to comment.