verify empty body and/or empty yang input/output

This commit is contained in:
Olof hagsand 2019-01-21 14:28:44 +01:00
parent 34d7f60ca4
commit 0e09996073
5 changed files with 91 additions and 98 deletions

View file

@ -1269,9 +1269,8 @@ api_operations_post_input(clicon_handle h,
xml_name_set(xdata, "data");
/* Here xdata is:
* <data><input xmlns="urn:example:clixon">...</input></data>
* Validate that exactly only <input> tag
*/
#if 0
#if 1
if (debug){
cbuf *ccc=cbuf_new();
if (clicon_xml2cbuf(ccc, xdata, 0, 0) < 0)
@ -1279,6 +1278,7 @@ api_operations_post_input(clicon_handle h,
clicon_debug(1, "%s DATA:%s", __FUNCTION__, cbuf_get(ccc));
}
#endif
/* Validate that exactly only <input> tag */
if ((xinput = xml_child_i_type(xdata, 0, CX_ELMNT)) == NULL ||
strcmp(xml_name(xinput),"input") != 0 ||
xml_child_nr_type(xdata, CX_ELMNT) != 1){
@ -1356,8 +1356,9 @@ api_operations_post_output(clicon_handle h,
cxobj *x;
cxobj *xok;
cbuf *cbret = NULL;
int ret;
int isempty;
// clicon_debug(1, "%s", __FUNCTION__);
if ((cbret = cbuf_new()) == NULL){
clicon_err(OE_UNIX, 0, "cbuf_new");
goto done;
@ -1380,7 +1381,7 @@ api_operations_post_output(clicon_handle h,
/* 9. Translate to restconf RPC data */
xml_name_set(xoutput, "output");
/* xoutput should now look: <output><x xmlns="uri">0</x></output> */
#if 0
#if 1
if (debug){
cbuf *ccc=cbuf_new();
if (clicon_xml2cbuf(ccc, xoutput, 0, 0) < 0)
@ -1388,56 +1389,19 @@ api_operations_post_output(clicon_handle h,
clicon_debug(1, "%s XOUTPUT:%s", __FUNCTION__, cbuf_get(ccc));
}
#endif
/* Validate output (in case handlers are wrong) */
if (youtput==NULL){
/* Special case, no yang output
* RFC 7950 7.14.4
* If the RPC operation invocation succeeded and no output parameters
* are returned, the <rpc-reply> contains a single <ok/> element
* RFC 8040 3.6.2
* If the "rpc" statement has no "output" section, the response message
* MUST NOT include a message-body and MUST send a "204 No Content"
* status-line instead.
*/
if ((xok = xml_child_i_type(xoutput, 0, CX_ELMNT)) == NULL ||
strcmp(xml_name(xok),"ok") != 0 ||
xml_child_nr_type(xoutput, CX_ELMNT) != 1){
/* Internal error - invalid output from rpc handler */
if (xok){
if (netconf_operation_failed_xml(&xerr, "application",
"Internal error: Empty RPC reply is not ok") < 0)
goto done;
}
else
if (netconf_operation_failed_xml(&xerr, "application",
"Internal error: Empty RPC reply should have OK") < 0)
goto done;
if ((xe = xpath_first(xerr, "rpc-error")) == NULL){
clicon_err(OE_XML, EINVAL, "rpc-error not found (internal error)");
goto done;
}
if (api_return_err(h, r, xe, pretty, use_xml) < 0)
goto done;
goto fail;
}
FCGX_SetExitStatus(204, r->out); /* OK */
FCGX_FPrintF(r->out, "Status: 204 No Content\r\n");
FCGX_FPrintF(r->out, "\r\n");
goto fail;
}
else{
xml_spec_set(xoutput, youtput); /* needed for xml_spec_populate */
if (0){
/* Sanity check of outgoing XML
* For now, skip outgoing checks.
* (1) Does not handle <ok/> properly
* (2) Uncertain how validation errors should be logged/handled
*/
if (youtput!=NULL){
xml_spec_set(xoutput, youtput); /* needed for xml_spec_populate */
#if 0
if (xml_apply(xoutput, CX_ELMNT, xml_spec_populate, yspec) < 0)
goto done;
if ((ret = xml_yang_validate_all(xoutput, cbret)) < 0)
goto done;
if (ret == 1 &&
(ret = xml_yang_validate_add(xoutput, cbret)) < 0)
goto done;
@ -1452,6 +1416,27 @@ api_operations_post_output(clicon_handle h,
goto done;
goto fail;
}
#endif
}
/* Special case, no yang output (single <ok/> - or empty body?)
* RFC 7950 7.14.4
* If the RPC operation invocation succeeded and no output parameters
* are returned, the <rpc-reply> contains a single <ok/> element
* RFC 8040 3.6.2
* If the "rpc" statement has no "output" section, the response message
* MUST NOT include a message-body and MUST send a "204 No Content"
* status-line instead.
*/
isempty = xml_child_nr_type(xoutput, CX_ELMNT) == 0 ||
(xml_child_nr_type(xoutput, CX_ELMNT) == 1 &&
(xok = xml_child_i_type(xoutput, 0, CX_ELMNT)) != NULL &&
strcmp(xml_name(xok),"ok")==0);
if (isempty) {
/* Internal error - invalid output from rpc handler */
FCGX_SetExitStatus(204, r->out); /* OK */
FCGX_FPrintF(r->out, "Status: 204 No Content\r\n");
FCGX_FPrintF(r->out, "\r\n");
goto fail;
}
/* Clear namespace of parameters */
x = NULL;
@ -1460,7 +1445,6 @@ api_operations_post_output(clicon_handle h,
if (xml_purge(xa) < 0)
goto done;
}
}
/* Set namespace on output */
if (xmlns_set(xoutput, NULL, namespace) < 0)
goto done;
@ -1643,7 +1627,7 @@ api_operations_post(clicon_handle h,
}
/* Here xtop is:
<rpc username="foo"><myfn xmlns="uri"><x>42</x></myfn></rpc> */
#if 0
#if 1
if (debug){
cbuf *ccc=cbuf_new();
if (clicon_xml2cbuf(ccc, xtop, 0, 0) < 0)
@ -1652,8 +1636,8 @@ api_operations_post(clicon_handle h,
__FUNCTION__, cbuf_get(ccc));
}
#endif
/* 6. Validate outgoing RPC and fill in defaults */
if (xml_spec_populate_rpc(h, xtop, yspec) < 0)
/* 6. Validate incoming RPC and fill in defaults */
if (xml_spec_populate_rpc(h, xtop, yspec) < 0) /* */
goto done;
if ((ret = xml_yang_validate_rpc(xtop, cbret)) < 0)
goto done;
@ -1710,7 +1694,7 @@ api_operations_post(clicon_handle h,
/* 8. Receive reply from local/backend handler as Netconf RPC
* <rpc-reply><x xmlns="uri">0</x></rpc-reply>
*/
#if 0
#if 1
if (debug){
cbuf *ccc=cbuf_new();
if (clicon_xml2cbuf(ccc, xret, 0, 0) < 0)

View file

@ -76,7 +76,20 @@ module clixon-example {
}
}
rpc empty {
description "Smallest possible RPC with no input or output";
description "Smallest possible RPC with no input or output sections";
}
rpc optional {
description "Small RPC with optional input and output";
input {
leaf x {
type string;
}
}
output {
leaf x {
type string;
}
}
}
rpc example {
description "Some example input/output for testing RFC7950 7.14.

View file

@ -156,7 +156,9 @@ example_rpc(clicon_handle h, /* Clicon handle */
goto done;
}
cprintf(cbret, "<rpc-reply>");
while ((x = xml_child_each(xe, x, CX_ELMNT)) != NULL) {
if (!xml_child_nr_type(xe, CX_ELMNT))
cprintf(cbret, "<ok/>");
else while ((x = xml_child_each(xe, x, CX_ELMNT)) != NULL) {
if (xmlns_set(x, NULL, namespace) < 0)
goto done;
if (clicon_xml2cbuf(cbret, x, 0, 0) < 0)
@ -168,7 +170,6 @@ example_rpc(clicon_handle h, /* Clicon handle */
return retval;
}
/*! Called to get state data from plugin
* @param[in] h Clicon handle
* @param[in] xpath String with XPATH syntax. or NULL for all
@ -338,6 +339,12 @@ clixon_plugin_init(clicon_handle h)
"empty"/* Xml tag when callback is made */
) < 0)
goto done;
/* Same as example but with optional input/output */
if (rpc_callback_register(h, example_rpc,
NULL,
"optional"/* Xml tag when callback is made */
) < 0)
goto done;
if (rpc_callback_register(h, example_rpc,
NULL,
"example"/* Xml tag when callback is made */
@ -349,4 +356,3 @@ clixon_plugin_init(clicon_handle h)
done:
return NULL;
}

View file

@ -30,27 +30,6 @@ cat <<EOF > $cfg
</config>
EOF
cat <<EOF > $fyang
module mymod{
yang-version 1.1;
namespace "urn:example:my";
prefix me;
import clixon-example {
prefix ex;
}
import ietf-interfaces {
prefix if;
}
import ietf-ip {
prefix ip;
}
import ietf-inet-types {
prefix "inet";
revision-date "2013-07-15";
}
}
EOF
# This is a fixed 'state' implemented in routing_backend. It is assumed to be always there
state='{"clixon-example:state": {"op": "42"}}'
@ -94,12 +73,12 @@ expecteq "$(curl -s -H 'Accept: application/yang-data+xml' -G http://localhost/r
# Should be alphabetically ordered
new2 "restconf get restconf/operations. RFC8040 3.3.2 (json)"
expecteq "$(curl -sG http://localhost/restconf/operations)" '{"operations": {"clixon-example:client-rpc": null,"clixon-example:empty": null,"clixon-example:example": null,"clixon-lib:debug": null}
expecteq "$(curl -sG http://localhost/restconf/operations)" '{"operations": {"clixon-example:client-rpc": null,"clixon-example:empty": null,"clixon-example:optional": null,"clixon-example:example": null,"clixon-lib:debug": null}
'
new "restconf get restconf/operations. RFC8040 3.3.2 (xml)"
ret=$(curl -s -H "Accept: application/yang-data+xml" -G http://localhost/restconf/operations)
expect='<operations><client-rpc xmlns="urn:example:clixon"/><empty xmlns="urn:example:clixon"/><example xmlns="urn:example:clixon"/><debug xmlns="http://clicon.org/lib"/></operations>'
expect='<operations><client-rpc xmlns="urn:example:clixon"/><empty xmlns="urn:example:clixon"/><optional xmlns="urn:example:clixon"/><example xmlns="urn:example:clixon"/><debug xmlns="http://clicon.org/lib"/></operations>'
match=`echo $ret | grep -EZo "$expect"`
if [ -z "$match" ]; then
err "$expect" "$ret"
@ -275,7 +254,7 @@ new2 "restconf rpc using wrong prefix"
expecteq "$(curl -s -X POST -d '{"wrong:input":{"routing-instance-name":"ipv4"}}' http://localhost/restconf/operations/wrong:example)" '{"ietf-restconf:errors" : {"error": {"error-type": "protocol","error-tag": "operation-failed","error-severity": "error","error-message": "yang module not found"}}} '
new "restconf local client rpc using POST xml"
ret=$(curl -s -X POST -H "Accept: application/yang-data+xml" -d '{"clixon-example:input":{"request":"example"}}' http://localhost/restconf/operations/clixon-example:client-rpc)
ret=$(curl -s -i -X POST -H "Accept: application/yang-data+xml" -d '{"clixon-example:input":{"request":"example"}}' http://localhost/restconf/operations/clixon-example:client-rpc)
expect='<output xmlns="urn:example:clixon"><result>ok</result></output>'
match=`echo $ret | grep -EZo "$expect"`
if [ -z "$match" ]; then

View file

@ -2,6 +2,9 @@
# RPC tests
# Validate parameters in restconf and netconf, check namespaces, etc
# See rfc8040 3.6
# Use the example application that has one mandatory input arg,
# At the end is an alternative Yang without mandatory arg for
# valid empty input and output.
APPNAME=example
# include err() and new() functions and creates $dir
@ -95,7 +98,7 @@ expecteof "$clixon_netconf -qf $cfg" 0 '<rpc xmlns="urn:ietf:params:xml:ns:netco
# 2. Then error cases
#
new "restconf empy rpc with null input"
new "restconf empty rpc with null input"
ret=$(curl -is -X POST -d '{"clixon-example:input":null}' http://localhost/restconf/operations/clixon-example:empty)
expect="204 No Content"
match=`echo $ret | grep -EZo "$expect"`
@ -103,9 +106,18 @@ if [ -z "$match" ]; then
err "$expect" "$ret"
fi
new2 "restconf empy rpc with input x"
new2 "restconf empty rpc with input x"
expecteq "$(curl -s -X POST -d '{"clixon-example:input":{"x":0}}' http://localhost/restconf/operations/clixon-example:empty)" '{"ietf-restconf:errors" : {"error": {"error-type": "application","error-tag": "unknown-element","error-info": {"bad-element": "x"},"error-severity": "error"}}} '
# cornercase: optional has yang input/output sections but test without body
new "restconf optional rpc with null input and output"
ret=$(curl -is -X POST -d '{"clixon-example:input":null}' http://localhost/restconf/operations/clixon-example:optional)
expect="204 No Content"
match=`echo $ret | grep -EZo "$expect"`
if [ -z "$match" ]; then
err "$expect" "$ret"
fi
new2 "restconf omit mandatory"
expecteq "$(curl -s -X POST -d '{"clixon-example:input":null}' http://localhost/restconf/operations/clixon-example:example)" '{"ietf-restconf:errors" : {"error": {"error-type": "application","error-tag": "missing-element","error-info": {"bad-element": "x"},"error-severity": "error","error-message": "Mandatory variable"}}} '
@ -118,7 +130,6 @@ expecteq "$(curl -s -X POST -d '{"clixon-example:input":{"x":"0"}}' http://local
new2 "restconf example missing input"
expecteq "$(curl -s -X POST -d '{"clixon-example:input":null}' http://localhost/restconf/operations/ietf-netconf:edit-config)" '{"ietf-restconf:errors" : {"error": {"error-type": "protocol","error-tag": "operation-failed","error-severity": "error","error-message": "yang module not found"}}} '
new "netconf kill-session missing session-id mandatory"
expecteof "$clixon_netconf -qf $cfg" 0 '<rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"><kill-session/></rpc>]]>]]>' '^<rpc-reply><rpc-error><error-type>application</error-type><error-tag>missing-element</error-tag><error-info><bad-element>session-id</bad-element></error-info><error-severity>error</error-severity><error-message>Mandatory variable</error-message></rpc-error></rpc-reply>]]>]]>$'