* Error messages for invalid number ranges and string lengths have been uniformed and changed.

* Error messages for invalid ranges are now on the form:
  ```
    Number 23 out of range: 1-10
    String length 23 out of range: 1-10
  ```
This commit is contained in:
Olof hagsand 2019-06-20 13:59:55 +02:00
parent c683bc9d0f
commit 97529a20a4
8 changed files with 388 additions and 33 deletions

View file

@ -69,6 +69,12 @@
### API changes on existing features (you may need to change your code)
* Error messages for invalid number ranges and string lengths have been uniformed and changed.
* Error messages for invalid ranges are now on the form:
```
Number 23 out of range: 1-10
String length 23 out of range: 1-10
```
* On validation callbacks, XML_FLAG_ADD is added to all nodes at startup validation, not just the top-level. This is the same behaviour as for steady-state validation.
* All hash_ functions have been prefixed with `clicon_` to avoid name collision with other packages (frr)
* All calls to the following functions must be changed: `hash_init`, `hash_free`, `hash_lookup`, `hash_value`, `hash_add`, `hash_del`, `hash_dump`, and `hash_keys`.

View file

@ -589,10 +589,6 @@ xmldb_get_zerocopy(clicon_handle h,
db_elmnt *de = NULL;
db_elmnt de0 = {0,};
if (!clicon_option_bool(h, "CLICON_XMLDB_CACHE")){
clicon_err(OE_CFG, 0, "CLICON_XMLDB_CACHE must be set");
goto done;
}
if ((yspec = clicon_dbspec_yang(h)) == NULL){
clicon_err(OE_YANG, ENOENT, "No yang spec");
goto done;

View file

@ -588,6 +588,105 @@ cv_validate_pattern(clicon_handle h,
(rmax && (i) > cv_##type##_get(rmax)))
/*! Error messsage for int violating ranges
* @note contains kludge - duplicate loop
*/
static int
outofrange(cg_var *cv0,
cvec *cvv,
char **reason)
{
int retval = -1;
cbuf *cb = NULL;
cg_var *cv1;
cg_var *cv2;
int i;
if ((cb = cbuf_new()) == NULL)
goto done;
cprintf(cb, "Number ");
cv2cbuf(cv0, cb);
cprintf(cb, " out of range: ");
/* Kludge: need to repeat the same loop as in the main function in
cv_validate1 */
i = 0;
while (i<cvec_len(cvv)){
cv1 = cvec_i(cvv, i++); /* Increment to check for max pair */
if (strcmp(cv_name_get(cv1),"range_min") != 0){
clicon_err(OE_YANG, EINVAL, "Internal error, expected range_min");
goto done;
}
if (i<cvec_len(cvv) &&
(cv2 = cvec_i(cvv, i)) != NULL &&
strcmp(cv_name_get(cv2),"range_max") == 0){
i++;
}
else
cv2 = cv1;
if (i>2)
cprintf(cb, ", ");
cv2cbuf(cv1, cb);
cprintf(cb, "-");
cv2cbuf(cv2, cb);
}
if (reason && (*reason = strdup(cbuf_get(cb))) == NULL)
goto done;
if (cb)
cbuf_free(cb);
retval = 0;
done:
return retval;
}
/*! Error messsage for string violating string limits
* @note contains kludge - duplicate loop
*/
static int
outoflength(uint64_t u64,
cvec *cvv,
char **reason)
{
int retval = -1;
cbuf *cb = NULL;
cg_var *cv1;
cg_var *cv2;
int i;
if ((cb = cbuf_new()) == NULL)
goto done;
cprintf(cb, "String length %" PRIu64 " out of range: ", u64);
/* Kludge: need to repeat the same loop as in the main function in
cv_validate1 */
i = 0;
while (i<cvec_len(cvv)){
cv1 = cvec_i(cvv, i++); /* Increment to check for max pair */
if (strcmp(cv_name_get(cv1),"range_min") != 0){
clicon_err(OE_YANG, EINVAL, "Internal error, expected range_min");
goto done;
}
if (i<cvec_len(cvv) &&
(cv2 = cvec_i(cvv, i)) != NULL &&
strcmp(cv_name_get(cv2),"range_max") == 0){
i++;
}
else
cv2 = cv1;
if (i>2)
cprintf(cb, ", ");
cv2cbuf(cv1, cb);
cprintf(cb, "-");
cv2cbuf(cv2, cb);
}
if (reason && (*reason = strdup(cbuf_get(cb))) == NULL)
goto done;
if (cb)
cbuf_free(cb);
retval = 0;
done:
return retval;
}
/*! Validate CLIgen variable
* @param[in] h Clicon handle
* @param[in] cv A cligen variable to validate. This is a correctly parsed cv.
@ -600,6 +699,7 @@ cv_validate_pattern(clicon_handle h,
* @retval 0 Validation not OK, malloced reason is returned. Free reason with free()
* @retval 1 Validation OK
* @note reason if given must be freed by caller
* @see cv_validate Corresponding type check in cligen
*/
static int
cv_validate1(clicon_handle h,
@ -705,14 +805,13 @@ cv_validate1(clicon_handle h,
/* Check fails */
if (i==cvec_len(cvv)){ /* And it is last */
if (reason){
if (reti)
*reason = cligen_reason("Number out of range: %"
PRId64, ii);
else if (retu)
*reason = cligen_reason("Number out of range: %"
PRIu64, uu);
if (reti || retu){
if (outofrange(cv, cvv, reason) < 0)
goto done;
}
else
*reason = cligen_reason("string length out of range: %" PRIu64, uu);
if (outoflength(uu, cvv, reason) < 0)
goto done;
}
goto fail;
}

View file

@ -6,7 +6,8 @@
: ${pattern:=test_*.sh}
if [ $# -gt 0 ]; then
echo "usage: $0 # detailed logs and stopon first error"
echo "usage: $0 # detailed logs and stop on first error. Use pattern=\"\" $0 to"
echo " Use pattern=<pattern> $0 to narrow down test cases"
exit -1
fi

View file

@ -3,7 +3,6 @@
# First a list with 0-5 base elements, insert in different places
# Second varying yangs: container, leaf, list, leaf-list, choice, user-order list
# Magic line must be first in script (see README.md)
s="$_" ; . ./lib.sh || if [ "$s" = $0 ]; then exit 0; else return 0; fi
@ -27,7 +26,6 @@ cat <<EOF > $cfg
<CLICON_SOCK>/usr/local/var/$APPNAME/$APPNAME.sock</CLICON_SOCK>
<CLICON_BACKEND_PIDFILE>/usr/local/var/$APPNAME/$APPNAME.pidfile</CLICON_BACKEND_PIDFILE>
<CLICON_XMLDB_DIR>$dir</CLICON_XMLDB_DIR>
<CLICON_XMLDB_CACHE>true</CLICON_XMLDB_CACHE>
</clixon-config>
EOF

View file

@ -177,7 +177,7 @@ new "3. Validate system-error config (9999 not in range)"
expecteof "$clixon_netconf -qf $cfg" 0 "<rpc><edit-config><target><candidate/></target><config><x xmlns='urn:example:clixon'><y><a>$nr</a><b>9999</b></y></x></config></edit-config></rpc>]]>]]>" '^<rpc-reply><ok/></rpc-reply>]]>]]>$'
new "Validate system-error validate (should fail)"
expecteof "$clixon_netconf -qf $cfg" 0 '<rpc><validate><source><candidate/></source></validate></rpc>]]>]]>' '^<rpc-reply><rpc-error><error-type>application</error-type><error-tag>bad-element</error-tag><error-info><bad-element>b</bad-element></error-info><error-severity>error</error-severity><error-message>Number out of range: 9999</error-message></rpc-error></rpc-reply>]]>]]>$'
expecteof "$clixon_netconf -qf $cfg" 0 '<rpc><validate><source><candidate/></source></validate></rpc>]]>]]>' '^<rpc-reply><rpc-error><error-type>application</error-type><error-tag>bad-element</error-tag><error-info><bad-element>b</bad-element></error-info><error-severity>error</error-severity><error-message>Number 9999 out of range: 0-100</error-message></rpc-error></rpc-reply>]]>]]>$'
new "Validate system-error discard-changes"
expecteof "$clixon_netconf -qf $cfg" 0 "<rpc><discard-changes/></rpc>]]>]]>" "^<rpc-reply><ok/></rpc-reply>]]>]]>$"

View file

@ -197,7 +197,7 @@ EOF
# Type tests.
# Parameters:
# 1: dbcache true/false
# 1: dbcache: cache, nocache, cache-zerocopy
testrun(){
dbcache=$1
new "test params: -f $cfg # dbcache: $dbcache"
@ -216,7 +216,7 @@ testrun(){
<CLICON_BACKEND_PIDFILE>/usr/local/var/$APPNAME/$APPNAME.pidfile</CLICON_BACKEND_PIDFILE>
<CLICON_CLI_GENMODEL_COMPLETION>1</CLICON_CLI_GENMODEL_COMPLETION>
<CLICON_XMLDB_DIR>/usr/local/var/$APPNAME</CLICON_XMLDB_DIR>
<CLICON_XMLDB_CACHE>$dbcache</CLICON_XMLDB_CACHE>
<CLICON_DATASTORE_CACHE>$dbcache</CLICON_DATASTORE_CACHE>
<CLICON_XMLDB_FORMAT>$format</CLICON_XMLDB_FORMAT>
</clixon-config>
EOF
@ -231,7 +231,7 @@ EOF
start_backend -s init -f $cfg
new "waiting"
sleep $RCWAIT
wait_backend
fi
new "cli set transitive string. type is alpha followed by number and is defined in three levels of modules"
@ -360,7 +360,7 @@ EOF
expecteof "$clixon_netconf -qf $cfg" 0 '<rpc><edit-config><target><candidate/></target><config><num1 xmlns="urn:example:clixon">-1</num1></config></edit-config></rpc>]]>]]>' "^<rpc-reply><ok/></rpc-reply>]]>]]>$"
new "netconf validate num1 -1 wrong"
expecteof "$clixon_netconf -qf $cfg" 0 "<rpc><validate><source><candidate/></source></validate></rpc>]]>]]>" '^<rpc-reply><rpc-error><error-type>application</error-type><error-tag>bad-element</error-tag><error-info><bad-element>num1</bad-element></error-info><error-severity>error</error-severity><error-message>Number out of range: -1</error-message></rpc-error></rpc-reply>]]>]]>$'
expecteof "$clixon_netconf -qf $cfg" 0 "<rpc><validate><source><candidate/></source></validate></rpc>]]>]]>" '^<rpc-reply><rpc-error><error-type>application</error-type><error-tag>bad-element</error-tag><error-info><bad-element>num1</bad-element></error-info><error-severity>error</error-severity><error-message>Number -1 out of range: 1-1</error-message></rpc-error></rpc-reply>]]>]]>$'
new "netconf discard-changes"
expecteof "$clixon_netconf -qf $cfg" 0 "<rpc><discard-changes/></rpc>]]>]]>" "^<rpc-reply><ok/></rpc-reply>]]>]]>$"
@ -368,7 +368,7 @@ EOF
#-------- num2 range and blanks
new "cli range test num2 3 error"
expectfn "$clixon_cli -1f $cfg -l o set num2 3" 255 '^CLI syntax error: "set num2 3": Number out of range: 3$'
expectfn "$clixon_cli -1f $cfg -l o set num2 3" 255 '^CLI syntax error: "set num2 3": Number 3 out of range: 4-4000$'
new "cli range test num2 1000 ok"
expectfn "$clixon_cli -1f $cfg -l o set num2 1000" 0 '^$'
@ -380,7 +380,7 @@ EOF
expecteof "$clixon_netconf -qf $cfg" 0 '<rpc><edit-config><target><candidate/></target><config><num2 xmlns="urn:example:clixon">3</num2></config></edit-config></rpc>]]>]]>' "^<rpc-reply><ok/></rpc-reply>]]>]]>$"
new "netconf validate num2 3 fail"
expecteof "$clixon_netconf -qf $cfg" 0 "<rpc><validate><source><candidate/></source></validate></rpc>]]>]]>" '^<rpc-reply><rpc-error><error-type>application</error-type><error-tag>bad-element</error-tag><error-info><bad-element>num2</bad-element></error-info><error-severity>error</error-severity><error-message>Number out of range: 3</error-message></rpc-error></rpc-reply>]]>]]>$'
expecteof "$clixon_netconf -qf $cfg" 0 "<rpc><validate><source><candidate/></source></validate></rpc>]]>]]>" '^<rpc-reply><rpc-error><error-type>application</error-type><error-tag>bad-element</error-tag><error-info><bad-element>num2</bad-element></error-info><error-severity>error</error-severity><error-message>Number 3 out of range: 4-4000</error-message></rpc-error></rpc-reply>]]>]]>$'
new "netconf range set num2 1000 ok"
expecteof "$clixon_netconf -qf $cfg" 0 '<rpc><edit-config><target><candidate/></target><config><num2 xmlns="urn:example:clixon">1000</num2></config></edit-config></rpc>]]>]]>' "^<rpc-reply><ok/></rpc-reply>]]>]]>$"
@ -392,7 +392,7 @@ EOF
expecteof "$clixon_netconf -qf $cfg" 0 '<rpc><edit-config><target><candidate/></target><config><num2 xmlns="urn:example:clixon">5000</num2></config></edit-config></rpc>]]>]]>' "^<rpc-reply><ok/></rpc-reply>]]>]]>$"
new "netconf validate num2 5000 fail"
expecteof "$clixon_netconf -qf $cfg" 0 "<rpc><validate><source><candidate/></source></validate></rpc>]]>]]>" '^<rpc-reply><rpc-error><error-type>application</error-type><error-tag>bad-element</error-tag><error-info><bad-element>num2</bad-element></error-info><error-severity>error</error-severity><error-message>Number out of range: 5000</error-message></rpc-error></rpc-reply>]]>]]>$'
expecteof "$clixon_netconf -qf $cfg" 0 "<rpc><validate><source><candidate/></source></validate></rpc>]]>]]>" '^<rpc-reply><rpc-error><error-type>application</error-type><error-tag>bad-element</error-tag><error-info><bad-element>num2</bad-element></error-info><error-severity>error</error-severity><error-message>Number 5000 out of range: 4-4000</error-message></rpc-error></rpc-reply>]]>]]>$'
new "netconf discard-changes"
expecteof "$clixon_netconf -qf $cfg" 0 "<rpc><discard-changes/></rpc>]]>]]>" "^<rpc-reply><ok/></rpc-reply>]]>]]>$"
@ -420,7 +420,7 @@ EOF
#-------- num4 multiple ranges 1..2 | 42..50
new "cli range test num4 multiple 0 fail"
expectfn "$clixon_cli -1f $cfg -l o set num4 0" 255 '^CLI syntax error: "set num4 0": Number out of range: 0$'
expectfn "$clixon_cli -1f $cfg -l o set num4 0" 255 '^CLI syntax error: "set num4 0": Number 0 out of range: 1-2, 42-50$'
new "cli range test num4 multiple 2 ok"
expectfn "$clixon_cli -1f $cfg -l e set num4 2" 0 '^$'
@ -477,7 +477,7 @@ EOF
expecteof "$clixon_netconf -qf $cfg" 0 '<rpc><edit-config><target><candidate/></target><config><dec xmlns="urn:example:clixon">-3.59</dec></config></edit-config></rpc>]]>]]>' "^<rpc-reply><ok/></rpc-reply>]]>]]>$"
new "netconf range dec64 -3.59 validate fail"
expecteof "$clixon_netconf -qf $cfg" 0 "<rpc><validate><source><candidate/></source></validate></rpc>]]>]]>" '^<rpc-reply><rpc-error><error-type>application</error-type><error-tag>bad-element</error-tag><error-info><bad-element>dec</bad-element></error-info><error-severity>error</error-severity><error-message>Number out of range'
expecteof "$clixon_netconf -qf $cfg" 0 "<rpc><validate><source><candidate/></source></validate></rpc>]]>]]>" '^<rpc-reply><rpc-error><error-type>application</error-type><error-tag>bad-element</error-tag><error-info><bad-element>dec</bad-element></error-info><error-severity>error</error-severity><error-message>Number -3.590 out of range'
new "netconf range dec64 -3.5"
expecteof "$clixon_netconf -qf $cfg" 0 '<rpc><edit-config><target><candidate/></target><config><dec xmlns="urn:example:clixon">-3.500</dec></config></edit-config></rpc>]]>]]>' "^<rpc-reply><ok/></rpc-reply>]]>]]>$"
@ -489,13 +489,13 @@ EOF
expecteof "$clixon_netconf -qf $cfg" 0 '<rpc><edit-config><target><candidate/></target><config><dec xmlns="urn:example:clixon">-2</dec></config></edit-config></rpc>]]>]]>' "^<rpc-reply><ok/></rpc-reply>]]>]]>$"
new "netconf range dec64 -2 validate fail"
expecteof "$clixon_netconf -qf $cfg" 0 "<rpc><validate><source><candidate/></source></validate></rpc>]]>]]>" '^<rpc-reply><rpc-error><error-type>application</error-type><error-tag>bad-element</error-tag><error-info><bad-element>dec</bad-element></error-info><error-severity>error</error-severity><error-message>Number out of range'
expecteof "$clixon_netconf -qf $cfg" 0 "<rpc><validate><source><candidate/></source></validate></rpc>]]>]]>" '^<rpc-reply><rpc-error><error-type>application</error-type><error-tag>bad-element</error-tag><error-info><bad-element>dec</bad-element></error-info><error-severity>error</error-severity><error-message>Number -2.000 out of range'
new "netconf range dec64 -0.001"
expecteof "$clixon_netconf -qf $cfg" 0 '<rpc><edit-config><target><candidate/></target><config><dec xmlns="urn:example:clixon">-0.001</dec></config></edit-config></rpc>]]>]]>' "^<rpc-reply><ok/></rpc-reply>]]>]]>$"
new "netconf range dec64 -0.001 validate fail"
expecteof "$clixon_netconf -qf $cfg" 0 "<rpc><validate><source><candidate/></source></validate></rpc>]]>]]>" '^<rpc-reply><rpc-error><error-type>application</error-type><error-tag>bad-element</error-tag><error-info><bad-element>dec</bad-element></error-info><error-severity>error</error-severity><error-message>Number out of range'
expecteof "$clixon_netconf -qf $cfg" 0 "<rpc><validate><source><candidate/></source></validate></rpc>]]>]]>" '^<rpc-reply><rpc-error><error-type>application</error-type><error-tag>bad-element</error-tag><error-info><bad-element>dec</bad-element></error-info><error-severity>error</error-severity><error-message>Number -0.001 out of range'
new "netconf range dec64 0.0"
expecteof "$clixon_netconf -qf $cfg" 0 '<rpc><edit-config><target><candidate/></target><config><dec xmlns="urn:example:clixon">0.0</dec></config></edit-config></rpc>]]>]]>' "^<rpc-reply><ok/></rpc-reply>]]>]]>$"
@ -507,12 +507,12 @@ EOF
expecteof "$clixon_netconf -qf $cfg" 0 '<rpc><edit-config><target><candidate/></target><config><dec xmlns="urn:example:clixon">+0.001</dec></config></edit-config></rpc>]]>]]>' "^<rpc-reply><ok/></rpc-reply>]]>]]>$"
new "netconf range dec64 +0.001 validate fail"
expecteof "$clixon_netconf -qf $cfg" 0 "<rpc><validate><source><candidate/></source></validate></rpc>]]>]]>" '^<rpc-reply><rpc-error><error-type>application</error-type><error-tag>bad-element</error-tag><error-info><bad-element>dec</bad-element></error-info><error-severity>error</error-severity><error-message>Number out of range'
expecteof "$clixon_netconf -qf $cfg" 0 "<rpc><validate><source><candidate/></source></validate></rpc>]]>]]>" '^<rpc-reply><rpc-error><error-type>application</error-type><error-tag>bad-element</error-tag><error-info><bad-element>dec</bad-element></error-info><error-severity>error</error-severity><error-message>Number 0.001 out of range'
#----------------string ranges---------------------
#-------- len1 single range (2)
new "cli length test len1 1 fail"
expectfn "$clixon_cli -1f $cfg -l o set len1 x" 255 '^CLI syntax error: "set len1 x": String length not within limits: 1$'
expectfn "$clixon_cli -1f $cfg -l o set len1 x" 255 '^CLI syntax error: "set len1 x": String length 1 out of range: 2-2$'
new "cli length test len1 2 OK"
expectfn "$clixon_cli -1f $cfg -l o set len1 xy" 0 '^$'
@ -527,12 +527,12 @@ EOF
expecteof "$clixon_netconf -qf $cfg" 0 '<rpc><edit-config><target><candidate/></target><config><len1 xmlns="urn:example:clixon">x</len1></config></edit-config></rpc>]]>]]>' "^<rpc-reply><ok/></rpc-reply>]]>]]>$"
new "netconf validate len1 1 wrong"
expecteof "$clixon_netconf -qf $cfg" 0 "<rpc><validate><source><candidate/></source></validate></rpc>]]>]]>" '^<rpc-reply><rpc-error><error-type>application</error-type><error-tag>bad-element</error-tag><error-info><bad-element>len1</bad-element></error-info><error-severity>error</error-severity><error-message>string length out of range: 1</error-message></rpc-error></rpc-reply>]]>]]>$'
expecteof "$clixon_netconf -qf $cfg" 0 "<rpc><validate><source><candidate/></source></validate></rpc>]]>]]>" '^<rpc-reply><rpc-error><error-type>application</error-type><error-tag>bad-element</error-tag><error-info><bad-element>len1</bad-element></error-info><error-severity>error</error-severity><error-message>String length 1 out of range: 2-2</error-message></rpc-error></rpc-reply>]]>]]>$'
#-------- len2 range and blanks
new "cli length test len2 3 error"
expectfn "$clixon_cli -1f $cfg -l o set len2 ab" 255 '^CLI syntax error: "set len2 ab": String length not within limits: 2$'
expectfn "$clixon_cli -1f $cfg -l o set len2 ab" 255 '^CLI syntax error: "set len2 ab": String length 2 out of range: 4-4000$'
new "cli length test len2 42 ok"
expectfn "$clixon_cli -1f $cfg -l o set len2 hejhophdsakjhkjsadhkjsahdkjsad" 0 '^$'
@ -547,7 +547,7 @@ EOF
#-------- len4 multiple ranges 2..3 | 20-29
new "cli length test len4 1 error"
expectfn "$clixon_cli -1f $cfg -l o set len4 a" 255 '^CLI syntax error: "set len4 a": String length not within limits: 1$'
expectfn "$clixon_cli -1f $cfg -l o set len4 a" 255 '^CLI syntax error: "set len4 a": String length 1 out of range: 2-3, 20-29$'
new "cli length test len4 2 ok"
expectfn "$clixon_cli -1f $cfg -l o set len4 ab" 0 '^$'
@ -614,9 +614,12 @@ EOF
}
# Run without db cache
testrun false
testrun nocache
# Run with db cache
testrun true
testrun cache
# Run with zero-copy XXX does not work
#testrun cache-zerocopy
rm -rf $dir

252
test/test_type_range.sh Executable file
View file

@ -0,0 +1,252 @@
#!/bin/bash
# Range type tests.
# Mainly error messages and multiple ranges
# Tests all int types including decimal64 and string length ranges
# See also test_type.sh
# Magic line must be first in script (see README.md)
s="$_" ; . ./lib.sh || if [ "$s" = $0 ]; then exit 0; else return 0; fi
APPNAME=example
# Which format to use as datastore format internally
: ${format:=xml}
cfg=$dir/conf_yang.xml
fyang=$dir/type.yang
dclispec=$dir/clispec/
# XXX: add more types, now only uint8 and int8
cat <<EOF > $fyang
module example{
yang-version 1.1;
namespace "urn:example:clixon";
prefix ex;
typedef tint8{
type int8{
range "1..10 | 14..20";
}
}
typedef tint16{
type int16{
range "1..10 | 14..20";
}
}
typedef tint32{
type int32{
range "1..10 | 14..20";
}
}
typedef tint64{
type int64{
range "1..10 | 14..20";
}
}
typedef tuint8{
type uint8{
range "1..10 | 14..20";
}
}
typedef tuint16{
type uint16{
range "1..10 | 14..20";
}
}
typedef tuint32{
type uint32{
range "1..10 | 14..20";
}
}
typedef tuint64{
type uint64{
range "1..10 | 14..20";
}
}
typedef tdecimal64{
type decimal64{
fraction-digits 3;
range "1..10 | 14..20";
}
}
typedef tstring{
type string{
length "1..10 | 14..20";
}
}
leaf lint8 {
type tint8;
}
leaf lint16 {
type tint16;
}
leaf lint32 {
type tint32;
}
leaf lint64 {
type tint64;
}
leaf luint8 {
type tuint8;
}
leaf luint16 {
type tuint16;
}
leaf luint32 {
type tuint32;
}
leaf luint64 {
type tuint64;
}
leaf ldecimal64 {
type tdecimal64;
}
leaf lstring {
type tstring;
}
}
EOF
mkdir $dclispec
# clispec for both generated cli and a hardcoded range check
cat <<EOF > $dclispec/clispec.cli
CLICON_MODE="example";
CLICON_PROMPT="%U@%H> ";
CLICON_PLUGIN="example_cli";
# Manually added (not generated)
manual hint8 <id:int8 range[1:10] range[14:20]>;
manual hint16 <id:int16 range[1:10] range[14:20]>;
manual hint32 <id:int32 range[1:10] range[14:20]>;
manual hint64 <id:int64 range[1:10] range[14:20]>;
manual huint8 <id:uint8 range[1:10] range[14:20]>;
manual huint16 <id:uint16 range[1:10] range[14:20]>;
manual huint32 <id:uint32 range[1:10] range[14:20]>;
manual huint64 <id:uint64 range[1:10] range[14:20]>;
manual hdecimal64 <id:decimal64 range[1:10] range[14:20]>;
manual hstring <id:string length[1:10] length[14:20]>;
# Generated cli
set @datamodel, cli_set();
merge @datamodel, cli_merge();
create @datamodel, cli_create();
show, cli_show_config("candidate", "text", "/");
quit("Quit"), cli_quit();
EOF
cat <<EOF > $cfg
<clixon-config xmlns="http://clicon.org/config">
<CLICON_CONFIGFILE>$cfg</CLICON_CONFIGFILE>
<CLICON_YANG_DIR>$dir</CLICON_YANG_DIR>
<CLICON_YANG_DIR>/usr/local/share/clixon</CLICON_YANG_DIR>
<CLICON_YANG_DIR>$IETFRFC</CLICON_YANG_DIR>
<CLICON_YANG_MAIN_FILE>$fyang</CLICON_YANG_MAIN_FILE>
<CLICON_CLISPEC_DIR>$dclispec</CLICON_CLISPEC_DIR>
<CLICON_CLI_DIR>/usr/local/lib/$APPNAME/cli</CLICON_CLI_DIR>
<CLICON_CLI_MODE>$APPNAME</CLICON_CLI_MODE>
<CLICON_SOCK>/usr/local/var/$APPNAME/$APPNAME.sock</CLICON_SOCK>
<CLICON_BACKEND_PIDFILE>/usr/local/var/$APPNAME/$APPNAME.pidfile</CLICON_BACKEND_PIDFILE>
<CLICON_CLI_GENMODEL_COMPLETION>1</CLICON_CLI_GENMODEL_COMPLETION>
<CLICON_XMLDB_DIR>/usr/local/var/$APPNAME</CLICON_XMLDB_DIR>
<CLICON_XMLDB_FORMAT>$format</CLICON_XMLDB_FORMAT>
</clixon-config>
EOF
# Type range tests.
# Parameters: 1: type (eg uint8)
# 2: val OK
# 3: eval Invalid value
# 4: post (eg .000 - special for decimal64, others should have "")
testrange(){
t=$1
val=$2
eval=$3
post=$4
if [ $t = "string" ]; then # special case for string type error msg
len=$(echo -n "$eval" | wc -c)
errmsg="String length $len out of range: 1$post-10$post, 14$post-20$post"
else
errmsg="Number $eval$post out of range: 1$post-10$post, 14$post-20$post"
fi
new "generated cli set $t leaf invalid"
expectfn "$clixon_cli -1f $cfg -l o set l$t $eval" 255 "$errmsg";
new "generated cli set $t leaf OK"
expectfn "$clixon_cli -1f $cfg -l o set l$t $val" 0 '^$'
# XXX Error in cligen order: Unknown command vs Number out of range
# olof@vandal> set luint8 0
# CLI syntax error: "set luint8 0": Number 0 is out of range: 14 - 20
# olof@vandal> set luint8 1
# olof@vandal> set luint8 0
# CLI syntax error: "set luint8 0": Unknown command
# (SAME AS FIRST ^)
# new "generated cli set $t leaf invalid"
# expectfn "$clixon_cli -1f $cfg -l o set l$t $eval" 255 "\"set l$t $eval\": Number $eval out of range: 1-10, 14-20";
new "manual cli set $t leaf OK"
expectfn "$clixon_cli -1f $cfg -l o man h$t $val" 0 '^$'
new "manual cli set $t leaf invalid"
echo "$clixon_cli -1f $cfg -l o set h$t $eval"
expectfn "$clixon_cli -1f $cfg -l o set l$t $eval" 255 'Unknown command'
new "discard"
expecteof "$clixon_netconf -qf $cfg" 0 "<rpc><discard-changes/></rpc>]]>]]>" "^<rpc-reply><ok/></rpc-reply>]]>]]>$"
new "Netconf set invalid $t leaf"
expecteof "$clixon_netconf -qf $cfg" 0 "<rpc><edit-config><target><candidate/></target><config><l$t xmlns=\"urn:example:clixon\">$eval</l$t></config></edit-config></rpc>]]>]]>" "^<rpc-reply><ok/></rpc-reply>]]>]]>$"
new "netconf get config"
expecteof "$clixon_netconf -qf $cfg" 0 "<rpc><get-config><source><candidate/></source></get-config></rpc>]]>]]>" "^<rpc-reply><data><l$t xmlns=\"urn:example:clixon\">$eval</l$t></data></rpc-reply>]]>]]>$"
new "netconf validate invalid range"
expecteof "$clixon_netconf -qf $cfg" 0 "<rpc><validate><source><candidate/></source></validate></rpc>]]>]]>" "^<rpc-reply><rpc-error><error-type>application</error-type><error-tag>bad-element</error-tag><error-info><bad-element>l$t</bad-element></error-info><error-severity>error</error-severity><error-message>$errmsg</error-message></rpc-error></rpc-reply>]]>]]>$"
new "discard"
expecteof "$clixon_netconf -qf $cfg" 0 "<rpc><discard-changes/></rpc>]]>]]>" "^<rpc-reply><ok/></rpc-reply>]]>]]>$"
}
if [ $BE -ne 0 ]; then
new "kill old backend"
sudo clixon_backend -zf $cfg
if [ $? -ne 0 ]; then
err
fi
new "start backend -s init -f $cfg"
start_backend -s init -f $cfg
new "waiting"
wait_backend
fi
new "test params: -f $cfg"
# Test all int types
for t in int8 int16 int32 int64 uint8 uint16 uint32 uint64; do
testrange $t 1 0 ""
done
# decimal64 requires 3 decimals as postfix
testrange decimal64 1 0 ".000"
# test string with lengthlimit
testrange string "012" "01234567890" ""
if [ $BE -ne 0 ]; then
new "Kill backend"
# Check if premature kill
pid=`pgrep -u root -f clixon_backend`
if [ -z "$pid" ]; then
err "backend already dead"
fi
# kill backend
stop_backend -f $cfg
fi
rm -rf $dir