diff options
Diffstat (limited to 'tests/libs')
-rw-r--r-- | tests/libs/Makefile.am | 2 | ||||
-rw-r--r-- | tests/libs/zbxjson/Makefile.am | 23 | ||||
-rw-r--r-- | tests/libs/zbxjson/zbx_json_path_open.c | 88 | ||||
-rw-r--r-- | tests/libs/zbxjson/zbx_json_path_open.yaml | 263 |
4 files changed, 376 insertions, 0 deletions
diff --git a/tests/libs/Makefile.am b/tests/libs/Makefile.am index 5a052b838f9..a48fc9f9498 100644 --- a/tests/libs/Makefile.am +++ b/tests/libs/Makefile.am @@ -1,5 +1,7 @@ SUBDIRS = \ zbxconf \ zbxdbhigh \ + zbxjson \ zbxsysinfo + diff --git a/tests/libs/zbxjson/Makefile.am b/tests/libs/zbxjson/Makefile.am new file mode 100644 index 00000000000..f58ac0e34b8 --- /dev/null +++ b/tests/libs/zbxjson/Makefile.am @@ -0,0 +1,23 @@ +noinst_PROGRAMS = zbx_json_path_open + +zbx_json_path_open_SOURCES = \ + zbx_json_path_open.c \ + ../../zbxmocktest.h + +zbx_json_path_open_LDADD = \ + $(top_srcdir)/tests/libzbxmocktest.a \ + $(top_srcdir)/tests/libzbxmockdata.a \ + $(top_srcdir)/src/libs/zbxjson/libzbxjson.a \ + $(top_srcdir)/src/libs/zbxalgo/libzbxalgo.a \ + $(top_srcdir)/src/libs/zbxcommon/libzbxcommon.a \ + $(top_srcdir)/src/libs/zbxmemory/libzbxmemory.a \ + $(top_srcdir)/src/libs/zbxlog/libzbxlog.a \ + $(top_srcdir)/src/libs/zbxsys/libzbxsys.a \ + $(top_srcdir)/src/libs/zbxconf/libzbxconf.a \ + $(top_srcdir)/tests/libzbxmockdata.a + + +#parse_item_key_LDADD += @SERVER_LIBS@ + +zbx_json_path_open_CFLAGS = -I@top_srcdir@/tests + diff --git a/tests/libs/zbxjson/zbx_json_path_open.c b/tests/libs/zbxjson/zbx_json_path_open.c new file mode 100644 index 00000000000..fb7d1aba6b5 --- /dev/null +++ b/tests/libs/zbxjson/zbx_json_path_open.c @@ -0,0 +1,88 @@ +/* +** Zabbix +** Copyright (C) 2001-2017 Zabbix SIA +** +** This program is free software; you can redistribute it and/or modify +** it under the terms of the GNU General Public License as published by +** the Free Software Foundation; either version 2 of the License, or +** (at your option) any later version. +** +** This program is distributed in the hope that it will be useful, +** but WITHOUT ANY WARRANTY; without even the implied warranty of +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +** GNU General Public License for more details. +** +** You should have received a copy of the GNU General Public License +** along with this program; if not, write to the Free Software +** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +**/ + +#include "zbxmocktest.h" +#include "zbxmockdata.h" +#include "zbxmockassert.h" +#include "zbxmockutil.h" + +#include "common.h" +#include "zbxjson.h" + +/* 'internal' jsonpath support function prototype */ +int zbx_jsonpath_next(const char *path, const char **pnext, zbx_strloc_t *loc, int *type); + +void zbx_mock_test_entry(void **state) +{ + zbx_mock_handle_t components, component; + const char *path, *next = NULL, *component_class, *component_value; + zbx_mock_error_t err; + zbx_strloc_t loc; + int type, ret; + char *buffer; + + ZBX_UNUSED(state); + + zbx_mock_get_parameter_string("in.path", &path); + zbx_mock_get_parameter_handle("out.components", &components); + + buffer = zbx_malloc(NULL, strlen(path) + 1); + + do + { + if (ZBX_MOCK_SUCCESS != (err = zbx_mock_vector_element(components, &component))) + fail_msg("Too many path components parsed"); + + zbx_mock_get_object_member_string(component, "class", &component_class); + + if (SUCCEED != (ret = zbx_jsonpath_next(path, &next, &loc, &type))) + { + zbx_mock_assert_streq("Return value", component_class, "fail"); + break; + } + else + zbx_mock_assert_strne("Return value", component_class, "fail"); + + switch (type) + { + case 0: /* ZBX_JSONPATH_COMPONENT_DOT */ + zbx_mock_assert_streq("Component class", component_class, "dot"); + break; + case 1: /* ZBX_JSONPATH_COMPONENT_BRACKET */ + zbx_mock_assert_streq("Component class", component_class, "bracket"); + break; + + case 2: /* ZBX_JSONPATH_ARRAY_INDEX */ + zbx_mock_assert_streq("Component class", component_class, "index"); + break; + } + + zbx_strlcpy(buffer, path + loc.l, loc.r - loc.l + 2); + + zbx_mock_get_object_member_string(component, "value", &component_value); + zbx_mock_assert_streq("Component value", component_value, buffer); + } + while ('\0' != *next); + + if (ZBX_MOCK_SUCCESS == zbx_mock_vector_element(components, &component)) + fail_msg("Not enough path components parsed"); + + zbx_free(buffer); +} + diff --git a/tests/libs/zbxjson/zbx_json_path_open.yaml b/tests/libs/zbxjson/zbx_json_path_open.yaml new file mode 100644 index 00000000000..ddec0aa41db --- /dev/null +++ b/tests/libs/zbxjson/zbx_json_path_open.yaml @@ -0,0 +1,263 @@ +# Invalid JSON paths + +--- +test case: Invalid path "" +in: + path: "" +out: + components: + - class: fail +--- +test case: Invalid path $ +in: + path: $ +out: + components: + - class: fail +--- +test case: Invalid path $. +in: + path: $. +out: + components: + - class: fail +--- +test case: Invalid path $['a' +in: + path: $['a' +out: + components: + - class: fail +--- +test case: Invalid path $[a']" +in: + path: $[a']" +out: + components: + - class: fail +--- +test case: Invalid path $['a' +in: + path: $['a' +out: + components: + - class: fail +--- +test case: Invalid path $[''] +in: + path: $[''] +out: + components: + - class: fail +--- +test case: Invalid path $.a[] +in: + path: $.a[] +out: + components: + - class: dot + value: a + - class: fail +--- +test case: Invalid path $.a[1 +in: + path: $.a[1 +out: + components: + - class: dot + value: a + - class: fail +--- +test case: Invalid path $['a'][] +in: + path: $['a'][] +out: + components: + - class: bracket + value: a + - class: fail +--- +test case: Invalid path $['a'][1 +in: + path: $['a'][1 +out: + components: + - class: bracket + value: a + - class: fail +--- +test case: Invalid path $.a. +in: + path: $.a. +out: + components: + - class: dot + value: a + result: fail +--- +test case: Invalid path $.['a'] +in: + path: $.['a'] +out: + components: + - class: fail + +# Valid JSON paths + +--- +test case: Valid path $.a +in: + path: $.a +out: + components: + - class: dot + value: a + result: ok +--- +test case: Valid path $['a'] +in: + path: $['a'] +out: + components: + - class: bracket + value: a +--- +test case: Valid path $[ 'a' ] +in: + path: $[ 'a' ] +out: + components: + - class: bracket + value: a +--- +test case: Valid path $["a"] +in: + path: $["a"] +out: + components: + - class: bracket + value: a +--- +test case: Valid path $.a.b +in: + path: $.a.b +out: + components: + - class: dot + value: a + - class: dot + value: b +--- +test case: Valid path $['a'].b +in: + path: $['a'].b +out: + components: + - class: bracket + value: a + - class: dot + value: b +--- +test case: Valid path $['a']['b'] +in: + path: $['a']['b'] +out: + components: + - class: bracket + value: a + - class: bracket + value: b +--- +test case: Valid path $.a['b'] +in: + path: $.a['b'] +out: + components: + - class: dot + value: a + - class: bracket + value: b +--- +test case: Valid path $.a[0] +in: + path: $.a[0] +out: + components: + - class: dot + value: a + - class: index + value: 0 +--- +test case: Valid path $.a[0].b[1] +in: + path: $.a[0].b[1] +out: + components: + - class: dot + value: a + - class: index + value: 0 + - class: dot + value: b + - class: index + value: 1 +--- +test case: Valid path $.a[1000] +in: + path: $.a[1000] +out: + components: + - class: dot + value: a + - class: index + value: 1000 +--- +test case: Valid path $.a[ 1 ] +in: + path: $.a[ 1 ] +out: + components: + - class: dot + value: a + - class: index + value: 1 +--- +test case: Valid path $['a'][2] +in: + path: $['a'][2] +out: + components: + - class: bracket + value: a + - class: index + value: 2 +--- +test case: Valid path $['a'][2]['b'][3] +in: + path: $['a'][2]['b'][3] +out: + components: + - class: bracket + value: a + - class: index + value: 2 + - class: bracket + value: b + - class: index + value: 3 +--- +test case: Valid path $[1][2] +in: + path: $[1][2] +out: + components: + - class: index + value: 1 + - class: index + value: 2 +... + + + + + |