From fdf335fb2fea73c8021062b5328a88d41657d4c7 Mon Sep 17 00:00:00 2001 From: Olof hagsand Date: Fri, 29 May 2020 12:39:07 +0200 Subject: [PATCH] new function ; Added valgrind mem check in leak tests; pattern CDATA tests --- CHANGELOG.md | 1 + lib/clixon/clixon_xml_io.h | 1 + lib/src/clixon_yang.c | 2 +- lib/src/clixon_yang_parse_lib.c | 24 ++++++++--------- test/lib.sh | 6 +++++ test/test_pattern.sh | 48 ++++++++++++++++++++++++++++++++- 6 files changed, 68 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6cafe636..1c89b007 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,7 @@ Expected: July 2020 ### Minor changes +* Added new function `clicon_xml2str()` to complement xml_print and others that returns a malloced string. * Added new function `xml_child_index_each()` to iterate over the children of an XML node according to the order defined by an explicit index variable. This is a complement to `xml_child_each()` which iterates using the default order. ## 4.5.0 diff --git a/lib/clixon/clixon_xml_io.h b/lib/clixon/clixon_xml_io.h index fa5f1df7..3b83f7df 100644 --- a/lib/clixon/clixon_xml_io.h +++ b/lib/clixon/clixon_xml_io.h @@ -46,6 +46,7 @@ int clicon_xml2file(FILE *f, cxobj *x, int level, int prettyprint); int xml_print(FILE *f, cxobj *xn); int clicon_xml2cbuf(cbuf *cb, cxobj *x, int level, int prettyprint, int32_t depth); +char *clicon_xml2str(cxobj *x); int xmltree2cbuf(cbuf *cb, cxobj *x, int level); int clixon_xml_parse_file(int fd, yang_bind yb, yang_stmt *yspec, char *endtag, cxobj **xt, cxobj **xerr); diff --git a/lib/src/clixon_yang.c b/lib/src/clixon_yang.c index c1b4a54b..6b333c49 100644 --- a/lib/src/clixon_yang.c +++ b/lib/src/clixon_yang.c @@ -530,7 +530,7 @@ ys_dup(yang_stmt *old) return new; } -/*! Insert yang statement as child of a parent yang_statement, last in list +/*! Append yang statement as child of a parent yang_statement, last in list * * @param[in] ys_parent Add child to this parent * @param[in] ys_child Add this child diff --git a/lib/src/clixon_yang_parse_lib.c b/lib/src/clixon_yang_parse_lib.c index f834bf3e..d4d04176 100644 --- a/lib/src/clixon_yang_parse_lib.c +++ b/lib/src/clixon_yang_parse_lib.c @@ -465,9 +465,9 @@ yang_expand_grouping(yang_stmt *yn) /*! Parse a string containing a YANG spec into a parse-tree * - * Syntax parsing. A string is input and a syntax-tree is returned (or error). - * A variable record is also returned containing a list of (global) variable values. - * (cloned from cligen) + * Syntax parsing. A string is input and a YANG syntax-tree is returned (or error). + * As a side-effect, Yang modules present in the text will be inserted under the global Yang + * specification * @param[in] str String of yang statements * @param[in] name Log string, typically filename * @param[in] yspec Yang specification. @@ -523,7 +523,7 @@ yang_parse_str(char *str, /*! Parse yang spec from an open file descriptor * @param[in] fd File descriptor containing the YANG file as ASCII characters * @param[in] name For debug, eg filename - * @param[in] ysp Yang specification. Should have been created by caller using yspec_new + * @param[in] yspec Yang specification. Should have been created by caller using yspec_new * @retval ymod Top-level yang (sub)module * @retval NULL Error * @note this function simply parse a yang spec, no dependencies or checks @@ -531,7 +531,7 @@ yang_parse_str(char *str, yang_stmt * yang_parse_file(int fd, const char *name, - yang_stmt *ysp) + yang_stmt *yspec) { char *buf = NULL; int i; @@ -564,7 +564,7 @@ yang_parse_file(int fd, } buf[i++] = (char)(c&0xff); } /* read a line */ - if ((ymod = yang_parse_str(buf, name, ysp)) < 0) + if ((ymod = yang_parse_str(buf, name, yspec)) < 0) goto done; done: if (buf) @@ -685,7 +685,7 @@ yang_parse_find_match(clicon_handle h, * * Similar to clicon_yang_str(), just read a file first * @param[in] filename Name of file - * @param[in] ysp Yang specification. Should have been created by caller using yspec_new + * @param[in] yspec Yang specification. Should have been created by caller using yspec_new * @retval ymod Top-level yang (sub)module * @retval NULL Error encountered @@ -694,7 +694,7 @@ yang_parse_find_match(clicon_handle h, */ yang_stmt * yang_parse_filename(const char *filename, - yang_stmt *ysp) + yang_stmt *yspec) { yang_stmt *ymod = NULL; int fd = -1; @@ -709,7 +709,7 @@ yang_parse_filename(const char *filename, clicon_err(OE_YANG, errno, "open(%s)", filename); goto done; } - if ((ymod = yang_parse_file(fd, filename, ysp)) < 0) + if ((ymod = yang_parse_file(fd, filename, yspec)) < 0) goto done; done: if (fd != -1) @@ -723,7 +723,7 @@ yang_parse_filename(const char *filename, * @param[in] h CLICON handle * @param[in] module Module name * @param[in] revision Revision (or NULL) - * @param[in] ysp Yang statement + * @param[in] yspec Yang statement * @retval 0 OK * @retval -1 Error * @@ -733,7 +733,7 @@ static yang_stmt * yang_parse_module(clicon_handle h, const char *module, const char *revision, - yang_stmt *ysp) + yang_stmt *yspec) { cbuf *fbuf = NULL; char *filename; @@ -759,7 +759,7 @@ yang_parse_module(clicon_handle h, goto done; } filename = cbuf_get(fbuf); - if ((ymod = yang_parse_filename(filename, ysp)) == NULL) + if ((ymod = yang_parse_filename(filename, yspec)) == NULL) goto done; if ((yrev = yang_find(ymod, Y_REVISION, NULL)) != NULL) revm = cv_uint32_get(yang_cv_get(yrev)); diff --git a/test/lib.sh b/test/lib.sh index cf5e93b4..dce04ec5 100755 --- a/test/lib.sh +++ b/test/lib.sh @@ -162,6 +162,12 @@ err(){ # Test is previous test had valgrind errors if so quit checkvalgrind(){ if [ -f $valgrindfile ]; then + res=$(cat $valgrindfile | grep -e "Invalid" |awk '{print $4}' | grep -v '^0$') + if [ -n "$res" ]; then + >&2 cat $valgrindfile + sudo rm -f $valgrindfile + exit -1 + fi res=$(cat $valgrindfile | grep -e "reachable" -e "lost:"|awk '{print $4}' | grep -v '^0$') if [ -n "$res" ]; then >&2 cat $valgrindfile diff --git a/test/test_pattern.sh b/test/test_pattern.sh index bbcf0115..1e723547 100755 --- a/test/test_pattern.sh +++ b/test/test_pattern.sh @@ -349,6 +349,12 @@ module pattern{ pattern 'Z|[\+\-]\d{2}:\d{2}'; } } + leaf p45 { + description "Recognizing a CDATA pattern"; + type string { + pattern ""; + } + } } } EOF @@ -356,9 +362,13 @@ EOF # Send a string via netconf for pattern matching # It assumes a yang with a hardcoded container to work properly # The function can expect matching or fail (negative test) +# Arguments: +# 1: leaf tag in yang +# 2: expected match(1) or fail(0) +# 3: match string testrun(){ leaf="$1" # leaf tag under with pattern to test - mat="$2" # expected match (1) or fail (0) + mat="$2" # expected match (1) or fail (0) str0="$3" # content string (to match against) # URI-encode the string to be sent with netconf @@ -694,6 +704,21 @@ testrun "p$pnr" 1 '+12:07' testrun "p$pnr" 1 'Z' testrun "p$pnr" 1 '-01:38' +let pnr=45 +new "Test for pattern leaf p$pnr CDATA matcher" +testrun "p$pnr" 1 '' +testrun "p$pnr" 0 '' +testrun "p$pnr" 1 '' +testrun "p$pnr" 0 '' # too long +# Negative tests where one char at a time is removed +testrun "p$pnr" 0 '![CDATA[0123456789]]>' +testrun "p$pnr" 0 '<[CDATA[0123456789]]>' +testrun "p$pnr" 0 '' +testrun "p$pnr" 0 '' +testrun "p$pnr" 0 '' +#testrun "p$pnr" 0 '' # XML parse error +#testrun "p$pnr" 0 '" 255 'CLI syntax error' + +new "CLI tests for CDATA, should fail" +expectfn "$clixon_cli -1f $cfg -l o set c p45 " 255 'CLI syntax error' + +new "CLI tests for CDATA, should fail" +expectfn "$clixon_cli -1f $cfg -l o set c p45 ![CDATA[foo]]>" 255 'CLI syntax error' + +new "CLI tests for CDATA, should fail" +expectfn "$clixon_cli -1f $cfg -l o set c p45 " 0 '^$' if [ $BE -ne 0 ]; then new "Kill backend"