Skip to content

Commit 4fdfab2

Browse files
committed
parser & printer CHANGE xpath1.0 type correct handling
Also test included. Fixes #402
1 parent 804ec71 commit 4fdfab2

File tree

5 files changed

+152
-2
lines changed

5 files changed

+152
-2
lines changed

src/parser.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1462,6 +1462,7 @@ lyp_parse_value(struct lys_type *type, const char **value_, struct lyxml_elem *x
14621462
int store, int dflt)
14631463
{
14641464
struct lys_type *ret = NULL, *t;
1465+
struct lys_tpdf *tpdf;
14651466
int c, len, found = 0, hidden;
14661467
unsigned int i, j;
14671468
int64_t num;
@@ -1898,6 +1899,26 @@ lyp_parse_value(struct lys_type *type, const char **value_, struct lyxml_elem *x
18981899
goto cleanup;
18991900
}
19001901

1902+
/* special handling of ietf-yang-types xpath1.0 */
1903+
for (tpdf = type->der;
1904+
tpdf->module && strcmp(tpdf->name, "xpath1.0") && strcmp(tpdf->module->name, "ietf-yang-types");
1905+
tpdf = tpdf->type.der);
1906+
if (tpdf->module && xml) {
1907+
/* convert value into the json format */
1908+
value = transform_xml2json(type->parent->module->ctx, value, xml, 1, 1, 0);
1909+
if (!value) {
1910+
/* invalid instance-identifier format */
1911+
LOGVAL(LYE_INVAL, LY_VLOG_LYD, contextnode, *value_, itemname);
1912+
goto cleanup;
1913+
}
1914+
1915+
if (value != *value_) {
1916+
/* update the changed value */
1917+
lydict_remove(type->parent->module->ctx, *value_);
1918+
*value_ = value;
1919+
}
1920+
}
1921+
19011922
if (store) {
19021923
/* store the result */
19031924
val->string = value;

src/printer_xml.c

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ xml_print_leaf(struct lyout *out, int level, const struct lyd_node *node, int to
261261
{
262262
const struct lyd_node_leaf_list *leaf = (struct lyd_node_leaf_list *)node, *iter;
263263
const struct lys_type *type;
264+
struct lys_tpdf *tpdf;
264265
const char *ns, *mod_name;
265266
const char **prefs, **nss;
266267
const char *xml_expr;
@@ -287,8 +288,24 @@ xml_print_leaf(struct lyout *out, int level, const struct lyd_node *node, int to
287288
datatype = leaf->value_type & LY_DATA_TYPE_MASK;
288289
printvalue:
289290
switch (datatype) {
290-
case LY_TYPE_BINARY:
291291
case LY_TYPE_STRING:
292+
type = lyd_leaf_type((struct lyd_node_leaf_list *)leaf);
293+
if (!type) {
294+
/* error */
295+
ly_print(out, "\"(!error!)\"");
296+
return EXIT_FAILURE;
297+
}
298+
for (tpdf = type->der;
299+
tpdf->module && strcmp(tpdf->name, "xpath1.0") && strcmp(tpdf->module->name, "ietf-yang-types");
300+
tpdf = tpdf->type.der);
301+
/* special handling of ietf-yang-types xpath1.0 */
302+
if (tpdf->module) {
303+
/* avoid code duplication - use instance-identifier printer which gets necessary namespaces to print */
304+
datatype = LY_TYPE_INST;
305+
goto printvalue;
306+
}
307+
/* fallthrough */
308+
case LY_TYPE_BINARY:
292309
case LY_TYPE_BITS:
293310
case LY_TYPE_ENUM:
294311
case LY_TYPE_BOOL:

tests/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ cmake_minimum_required(VERSION 2.8.12)
44
set(CMAKE_MACOSX_RPATH TRUE)
55

66
set(api_tests test_libyang test_tree_schema test_xml test_dict test_tree_data test_tree_data_dup test_tree_data_merge test_xpath test_xpath_1.1 test_diff)
7-
set(data_tests test_data_initialization test_leafref_remove test_instid_remove test_keys test_autodel test_when test_when_1.1 test_must_1.1 test_defaults test_emptycont test_unique test_mandatory test_json test_parse_print test_values test_metadata)
7+
set(data_tests test_data_initialization test_leafref_remove test_instid_remove test_keys test_autodel test_when test_when_1.1 test_must_1.1 test_defaults test_emptycont test_unique test_mandatory test_json test_parse_print test_values test_metadata test_yangtypes_xpath)
88
set(schema_yin_tests test_print_transform)
99
set(schema_tests test_ietf test_augment test_deviation test_refine test_typedef test_import test_include test_feature test_conformance test_leaflist test_extensions test_status)
1010
set(conformance_tests test_sec6_1_1 test_sec6_2 test_sec5_1 test_sec5_5 test_sec6_1_3 test_sec6_2_1 test_sec7_1 test_sec7_2 test_sec7_3 test_sec7_3_1 test_sec7_3_4 test_sec7_5_2 test_sec7_5_4 test_sec7_5_5 test_sec7_6_2 test_sec7_6_3 test_sec7_6_4 test_sec7_6_5 test_sec7_7_2 test_sec7_7_3 test_sec7_7_4 test_sec7_7_5 test_sec7_8_1 test_sec7_8_2 test_sec7_8_3 test_sec7_9_1 test_sec7_9_2 test_sec7_9_3 test_sec7_9_4 test_sec7_10 test_sec7_11 test_sec7_12_1 test_sec7_12_2 test_sec7_13_1 test_sec7_13_2 test_sec7_13_3 test_sec7_14 test_sec7_15 test_sec7_16_1 test_sec7_16_2 test_sec7_18_1 test_sec7_18_2 test_sec7_18_3_1 test_sec7_18_3_2 test_sec7_19_1 test_sec7_19_2 test_sec7_19_5 test_sec9_2 test_sec9_3 test_sec9_4_4 test_sec9_4_6 test_sec9_5 test_sec9_6 test_sec9_7 test_sec9_8 test_sec9_9 test_sec9_10 test_sec9_11 test_sec9_12 test_sec9_13)

tests/data/files/nacm.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<nacm xmlns="urn:ietf:params:xml:ns:yang:ietf-netconf-acm">
2+
<rule-list>
3+
<name>test-list</name>
4+
<rule>
5+
<name>test-rule</name>
6+
<path xmlns:a="urn:all">/a:cont1/a:leaf3</path>
7+
<action>deny</action>
8+
</rule>
9+
</rule-list>
10+
</nacm>
11+
<cont1 xmlns="urn:all">
12+
<list1>
13+
<leaf18>aa</leaf18>
14+
</list1>
15+
</cont1>

tests/data/test_yangtypes_xpath.c

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/**
2+
* @file test_yangtypes_xpath.c
3+
* @author Michal Vasko <[email protected]>
4+
* @brief Cmocka tests for resolving ietf-yang-types type xpath1.0.
5+
*
6+
* Copyright (c) 2017 CESNET, z.s.p.o.
7+
*
8+
* This source code is licensed under BSD 3-Clause License (the "License").
9+
* You may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* https://opensource.org/licenses/BSD-3-Clause
13+
*/
14+
15+
#include <stdio.h>
16+
#include <stdlib.h>
17+
#include <setjmp.h>
18+
#include <cmocka.h>
19+
20+
#include "tests/config.h"
21+
#include "libyang.h"
22+
23+
struct state {
24+
struct ly_ctx *ctx;
25+
struct lyd_node *dt;
26+
char *xml;
27+
};
28+
29+
static int
30+
setup_f(void **state)
31+
{
32+
struct state *st;
33+
34+
(*state) = st = calloc(1, sizeof *st);
35+
if (!st) {
36+
fprintf(stderr, "Memory allocation error");
37+
return -1;
38+
}
39+
40+
/* libyang context */
41+
st->ctx = ly_ctx_new(NULL, 0);
42+
if (!st->ctx) {
43+
fprintf(stderr, "Failed to create context.\n");
44+
goto error;
45+
}
46+
47+
return 0;
48+
49+
error:
50+
ly_ctx_destroy(st->ctx, NULL);
51+
free(st);
52+
(*state) = NULL;
53+
54+
return -1;
55+
}
56+
57+
static int
58+
teardown_f(void **state)
59+
{
60+
struct state *st = (*state);
61+
62+
lyd_free_withsiblings(st->dt);
63+
ly_ctx_destroy(st->ctx, NULL);
64+
free(st->xml);
65+
free(st);
66+
(*state) = NULL;
67+
68+
return 0;
69+
}
70+
71+
static void
72+
test_acm_yangtypes_xpath(void **state)
73+
{
74+
struct state *st = (struct state *)*state;
75+
76+
/* schema */
77+
assert_ptr_not_equal(lys_parse_path(st->ctx, TESTS_DIR"/schema/yang/ietf/ietf-netconf-acm.yang", LYS_IN_YANG), NULL);
78+
assert_ptr_not_equal(lys_parse_path(st->ctx, TESTS_DIR"/data/files/all.yang", LYS_IN_YANG), NULL);
79+
80+
/* data */
81+
st->dt = lyd_parse_path(st->ctx, TESTS_DIR"/data/files/nacm.xml", LYD_XML, LYD_OPT_CONFIG /*DATA_NO_YANGLIB*/);
82+
assert_ptr_not_equal(st->dt, NULL);
83+
84+
assert_string_equal(((struct lyd_node_leaf_list *)st->dt->child->child->next->child->next)->value_str, "/all:cont1/leaf3");
85+
86+
lyd_print_mem(&(st->xml), st->dt, LYD_XML, 0);
87+
assert_string_equal(st->xml, "<nacm xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-acm\"><rule-list><name>test-list</name><rule><name>test-rule</name><path xmlns:all_mod=\"urn:all\">/all_mod:cont1/all_mod:leaf3</path><action>deny</action></rule></rule-list></nacm>");
88+
}
89+
90+
int main(void)
91+
{
92+
const struct CMUnitTest tests[] = {
93+
cmocka_unit_test_setup_teardown(test_acm_yangtypes_xpath, setup_f, teardown_f),
94+
};
95+
96+
return cmocka_run_group_tests(tests, NULL, NULL);
97+
}

0 commit comments

Comments
 (0)