Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/zabbix/zabbix.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndris Zeila <andris.zeila@zabbix.com>2017-12-06 18:22:35 +0300
committerAndris Zeila <andris.zeila@zabbix.com>2017-12-06 18:22:35 +0300
commitd9d817ca2d08cac472199f396ecc608a683a1746 (patch)
tree018aca8dbd9a3840a9c4fe3d1c3e40cbe671b5b2
parente4ba801924531b0002e36542ae79471b8167656e (diff)
.......... [DEV-753] initial version of test application with assert and utility additions to mock framework
-rw-r--r--.gitattributes1
-rw-r--r--configure.ac1
-rw-r--r--src/libs/zbxjson/json.c3
-rw-r--r--tests/Makefile.am6
-rw-r--r--tests/libs/Makefile.am2
-rw-r--r--tests/libs/zbxjson/Makefile.am23
-rw-r--r--tests/libs/zbxjson/zbx_json_path_open.c88
-rw-r--r--tests/libs/zbxjson/zbx_json_path_open.yaml263
-rw-r--r--tests/zbxmockassert.c64
-rw-r--r--tests/zbxmockassert.h35
-rw-r--r--tests/zbxmockdata.c176
-rw-r--r--tests/zbxmockdata.h4
-rw-r--r--tests/zbxmockutil.c59
-rw-r--r--tests/zbxmockutil.h30
14 files changed, 744 insertions, 11 deletions
diff --git a/.gitattributes b/.gitattributes
index cea24061a36..390e89519aa 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -351,6 +351,7 @@ src/zabbix_server/taskmanager/taskmanager.c -text
src/zabbix_server/taskmanager/taskmanager.h -text
tests/libs/zbxconf/parse_cfg_file.yaml -text
tests/libs/zbxdbhigh/DBselect_uint64.yaml -text
+tests/libs/zbxjson/zbx_json_path_open.yaml -text
tests/libs/zbxsysinfo/parse_item_key.yaml -text
tests/libs/zbxsysinfo/process.yaml -text
upgrades/dbpatches/1.1/1.0_to_1.1alpha1/mysql/Hub.png -text svneol=unset#image/png
diff --git a/configure.ac b/configure.ac
index 378adf66ad0..f47461005c4 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1588,6 +1588,7 @@ AC_CHECK_FILE(tests, [AC_CONFIG_FILES(
tests/libs/Makefile
tests/libs/zbxconf/Makefile
tests/libs/zbxdbhigh/Makefile
+ tests/libs/zbxjson/Makefile
tests/libs/zbxsysinfo/Makefile
)])
diff --git a/src/libs/zbxjson/json.c b/src/libs/zbxjson/json.c
index f7979a3c9fd..b0840d7aa79 100644
--- a/src/libs/zbxjson/json.c
+++ b/src/libs/zbxjson/json.c
@@ -1013,7 +1013,7 @@ static int zbx_jsonpath_error(const char *path)
* FAIL - json path parsing error *
* *
******************************************************************************/
-static int zbx_jsonpath_next(const char *path, const char **pnext, zbx_strloc_t *loc, int *type)
+int zbx_jsonpath_next(const char *path, const char **pnext, zbx_strloc_t *loc, int *type)
{
const char *next = *pnext;
size_t pos;
@@ -1198,4 +1198,3 @@ void zbx_json_value_dyn(const struct zbx_json_parse *jp, char **string, size_t *
}
}
-
diff --git a/tests/Makefile.am b/tests/Makefile.am
index d2997e8d7b9..156dec62e98 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -17,4 +17,8 @@ libzbxmockdata_a_SOURCES = \
zbxmockfile.c \
zbxmockstat.c \
zbxmockdata.c \
- zbxmockdata.h
+ zbxmockdata.h \
+ zbxmockassert.c \
+ zbxmockassert.h \
+ zbxmockutil.c \
+ zbxmockutil.h
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
+...
+
+
+
+
+
diff --git a/tests/zbxmockassert.c b/tests/zbxmockassert.c
new file mode 100644
index 00000000000..4796f8a9970
--- /dev/null
+++ b/tests/zbxmockassert.c
@@ -0,0 +1,64 @@
+/*
+** 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 "common.h"
+
+void cm_print_error(const char * const format, ...);
+
+#define _FAIL(file, line, prefix, message, ...) \
+ do \
+ { \
+ cm_print_error("%s%s" message, (NULL != prefix_msg ? prefix_msg : ""), \
+ (NULL != prefix_msg && '\0' != *prefix_msg ? ": " : ""), \
+ __VA_ARGS__); \
+ _fail(file, line); \
+ } while(0);
+
+void __zbx_mock_assert_streq(const char *file, int line, const char *prefix_msg, const char *expected_value,
+ const char *return_value)
+{
+ if (0 != strcmp(return_value, expected_value))
+ {
+ _FAIL(file, line, prefix_msg, "Expected \"%s\" while got \"%s\"\n", expected_value, return_value);
+ }
+}
+
+void __zbx_mock_assert_strne(const char *file, int line, const char *prefix_msg, const char *expected_value,
+ const char *return_value)
+{
+ if (0 == strcmp(return_value, expected_value))
+ {
+ _FAIL(file, line, prefix_msg, "Expected not \"%s\" while got \"%s\"\n", expected_value, return_value);
+ }
+}
+
+void __zbx_mock_assert_uint64eq(const char *file, int line, const char *prefix_msg, zbx_uint64_t expected_value,
+ zbx_uint64_t return_value)
+{
+ if (return_value != expected_value)
+ {
+ _FAIL(file, line, prefix_msg, "Expected \"" ZBX_FS_UI64 "\" while got \"" ZBX_FS_UI64 "\"\n",
+ expected_value, return_value);
+ }
+}
+
diff --git a/tests/zbxmockassert.h b/tests/zbxmockassert.h
new file mode 100644
index 00000000000..786038e25b7
--- /dev/null
+++ b/tests/zbxmockassert.h
@@ -0,0 +1,35 @@
+/*
+** 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.
+**/
+
+#ifndef ZABBIX_MOCK_ASSERT_H
+#define ZABBIX_MOCK_ASSERT_H
+
+void __zbx_mock_assert_streq(const char *file, int line, const char *prefix_msg, const char *expected_value,
+ const char *return_value);
+
+void __zbx_mock_assert_strne(const char *file, int line, const char *prefix_msg, const char *expected_value,
+ const char *return_value);
+
+#define zbx_mock_assert_streq(prefix_msg, expected_value, return_value) \
+ __zbx_mock_assert_streq(__FILE__, __LINE__, prefix_msg, expected_value, return_value)
+
+#define zbx_mock_assert_strne(prefix_msg, expected_value, return_value) \
+ __zbx_mock_assert_strne(__FILE__, __LINE__, prefix_msg, expected_value, return_value)
+
+#endif
diff --git a/tests/zbxmockdata.c b/tests/zbxmockdata.c
index d82f02e3c4c..8f5a060fb6e 100644
--- a/tests/zbxmockdata.c
+++ b/tests/zbxmockdata.c
@@ -28,12 +28,14 @@
static zbx_vector_ptr_t handle_pool; /* a place to store handles provided to mock data user */
static zbx_vector_str_t string_pool; /* a place to store strings provided to mock data user */
static yaml_document_t test_case; /* parsed YAML document with test case data */
+static const yaml_node_t *root; /* the root document node */
static const yaml_node_t *in = NULL; /* pointer to "in" section of test case document */
static const yaml_node_t *out = NULL; /* pointer to "out" section of test case document */
static const yaml_node_t *db_data = NULL; /* pointer to "db data" section of test case document */
static const yaml_node_t *files = NULL; /* pointer to "files" section of test case document */
static const yaml_node_t *exit_code = NULL; /* pointer to "exit code" section of test case document */
+
typedef struct
{
const yaml_node_t *node; /* node of test_case document handle is associated with */
@@ -83,6 +85,17 @@ static int zbx_yaml_scalar_cmp(const char *str, const yaml_node_t *node)
return strncmp(str, (const char *)node->data.scalar.value, node->data.scalar.length);
}
+static int zbx_yaml_scalar_ncmp(const char *str, size_t len, const yaml_node_t *node)
+{
+ if (YAML_SCALAR_NODE != node->type)
+ fail_msg("Internal error: scalar comparison of nonscalar node.");
+
+ if (len != node->data.scalar.length)
+ return -1;
+
+ return strncmp(str, (const char *)node->data.scalar.value, node->data.scalar.length);
+}
+
/* TODO: validate that keys in "in", "out", "db data" are scalars; validate "db data" */
int zbx_mock_data_init(void **state)
{
@@ -95,8 +108,6 @@ int zbx_mock_data_init(void **state)
if (0 != yaml_parser_load(&parser, &test_case))
{
- const yaml_node_t *root;
-
if (NULL != (root = yaml_document_get_root_node(&test_case)))
{
yaml_document_t tmp;
@@ -283,12 +294,14 @@ const char *zbx_mock_error_string(zbx_mock_error_t error)
return "Provided handle is not a string handle.";
case ZBX_MOCK_INTERNAL_ERROR:
return "Internal error, please report to maintainers.";
+ case ZBX_MOCK_INVALID_YAML_PATH:
+ return "Invalid YAML path syntax.";
default:
return "Unknown error.";
}
}
-static zbx_mock_error_t zbx_mock_parameter(zbx_mock_parameter_t type, const char *name, zbx_mock_handle_t *parameter)
+static zbx_mock_error_t zbx_mock_builtin_parameter(zbx_mock_parameter_t type, const char *name, zbx_mock_handle_t *parameter)
{
const yaml_node_t *source;
const yaml_node_pair_t *pair;
@@ -338,22 +351,22 @@ static zbx_mock_error_t zbx_mock_parameter(zbx_mock_parameter_t type, const char
zbx_mock_error_t zbx_mock_in_parameter(const char *name, zbx_mock_handle_t *parameter)
{
- return zbx_mock_parameter(ZBX_MOCK_IN, name, parameter);
+ return zbx_mock_builtin_parameter(ZBX_MOCK_IN, name, parameter);
}
zbx_mock_error_t zbx_mock_out_parameter(const char *name, zbx_mock_handle_t *parameter)
{
- return zbx_mock_parameter(ZBX_MOCK_OUT, name, parameter);
+ return zbx_mock_builtin_parameter(ZBX_MOCK_OUT, name, parameter);
}
zbx_mock_error_t zbx_mock_db_rows(const char *data_source, zbx_mock_handle_t *rows)
{
- return zbx_mock_parameter(ZBX_MOCK_DB_DATA, data_source, rows);
+ return zbx_mock_builtin_parameter(ZBX_MOCK_DB_DATA, data_source, rows);
}
zbx_mock_error_t zbx_mock_file(const char *path, zbx_mock_handle_t *file)
{
- return zbx_mock_parameter(ZBX_MOCK_FILES, path, file);
+ return zbx_mock_builtin_parameter(ZBX_MOCK_FILES, path, file);
}
zbx_mock_error_t zbx_mock_exit_code(int *status)
@@ -444,3 +457,152 @@ zbx_mock_error_t zbx_mock_string(zbx_mock_handle_t string, const char **value)
*value = tmp;
return ZBX_MOCK_SUCCESS;
}
+
+static zbx_mock_error_t zbx_yaml_path_next(const char **pnext, const char **key, int *key_len, int *index)
+{
+ const char *next = *pnext;
+ size_t pos;
+ char quotes;
+
+ while ('.' == *next)
+ next++;
+
+ /* process dot notation component */
+ if ('[' != *next)
+ {
+ *key = next;
+
+ while (0 != isalnum(*next) || '_' == *next)
+ next++;
+
+ if (*key == next)
+ return ZBX_MOCK_INVALID_YAML_PATH;
+
+ *key_len = next - *key;
+
+ if ('\0' != *next && '.' != *next && '[' != *next)
+ return ZBX_MOCK_INVALID_YAML_PATH;
+
+ *pnext = next;
+ *index = 0;
+
+ return ZBX_MOCK_SUCCESS;
+ }
+
+ while (*(++next) == ' ')
+ ;
+
+ /* process array index component */
+ if (0 != isdigit(*next))
+ {
+ for (pos = 0; 0 != isdigit(next[pos]); pos++)
+ ;
+
+ if (0 == pos)
+ return ZBX_MOCK_INVALID_YAML_PATH;
+
+ *key = next;
+ *key_len = pos;
+
+ next += pos;
+
+ while (*next == ' ')
+ next++;
+
+ if (']' != *next++)
+ return ZBX_MOCK_INVALID_YAML_PATH;
+
+ *pnext = next;
+ *index = 1;
+
+ return ZBX_MOCK_SUCCESS;
+ }
+
+ /* process bracket notation component */
+
+ if ('\'' != *next && '"' != *next)
+ return ZBX_MOCK_INVALID_YAML_PATH;
+
+ *key = next + 1;
+
+ for (quotes = *next++; quotes != *next; next++)
+ {
+ if ('\0' == *next)
+ return ZBX_MOCK_INVALID_YAML_PATH;
+ }
+
+ if (*key == next)
+ return ZBX_MOCK_INVALID_YAML_PATH;
+
+ *key_len = next - *key;
+
+ while (*(++next) == ' ')
+ ;
+
+ if (']' != *next++)
+ return ZBX_MOCK_INVALID_YAML_PATH;
+
+ *pnext = next;
+ *index = 0;
+
+ return ZBX_MOCK_SUCCESS;
+}
+
+static zbx_mock_error_t zbx_mock_parameter_rec(const yaml_node_t *node, const char *path, zbx_mock_handle_t *parameter)
+{
+ const yaml_node_pair_t *pair;
+ const char *pnext = path, *key_name;
+ int err, key_len, index;
+
+ /* end of the path, return whatever has been found */
+ if ('\0' == *pnext)
+ {
+ *parameter = zbx_mock_handle_alloc(node);
+ return ZBX_MOCK_SUCCESS;
+ }
+
+ if (ZBX_MOCK_SUCCESS != (err = zbx_yaml_path_next(&pnext, &key_name, &key_len, &index)))
+ return err;
+
+ /* the path component is array index, attempt to extract sequence element */
+ if (0 != index)
+ {
+ const yaml_node_t *element;
+
+ if (YAML_SEQUENCE_NODE != node->type)
+ return ZBX_MOCK_NOT_A_VECTOR;
+
+ index = atoi(key_name);
+
+ if (0 > index || index >= (node->data.sequence.items.top - node->data.sequence.items.start))
+ return ZBX_MOCK_END_OF_VECTOR;
+
+ element = yaml_document_get_node(&test_case, node->data.sequence.items.start[index]);
+ return zbx_mock_parameter_rec(element, pnext, parameter);
+ }
+
+ /* the patch component is object key, attempt to extract object member */
+
+ if (YAML_MAPPING_NODE != node->type)
+ return ZBX_MOCK_NOT_AN_OBJECT;
+
+ for (pair = node->data.mapping.pairs.start; pair < node->data.mapping.pairs.top; pair++)
+ {
+ const yaml_node_t *key, *value;
+
+ key = yaml_document_get_node(&test_case, pair->key);
+
+ if (0 == zbx_yaml_scalar_ncmp(key_name, key_len, key))
+ {
+ value = yaml_document_get_node(&test_case, pair->value);
+ return zbx_mock_parameter_rec(value, pnext, parameter);
+ }
+ }
+
+ return ZBX_MOCK_NO_SUCH_MEMBER;
+}
+
+zbx_mock_error_t zbx_mock_parameter(const char *path, zbx_mock_handle_t *parameter)
+{
+ return zbx_mock_parameter_rec(root, path, parameter);
+}
diff --git a/tests/zbxmockdata.h b/tests/zbxmockdata.h
index a4fb63572ae..434f153e365 100644
--- a/tests/zbxmockdata.h
+++ b/tests/zbxmockdata.h
@@ -36,7 +36,8 @@ typedef enum
ZBX_MOCK_NOT_A_VECTOR,
ZBX_MOCK_END_OF_VECTOR,
ZBX_MOCK_NOT_A_STRING,
- ZBX_MOCK_INTERNAL_ERROR
+ ZBX_MOCK_INTERNAL_ERROR,
+ ZBX_MOCK_INVALID_YAML_PATH
}
zbx_mock_error_t;
@@ -50,5 +51,6 @@ zbx_mock_error_t zbx_mock_exit_code(int *status);
zbx_mock_error_t zbx_mock_object_member(zbx_mock_handle_t object, const char *name, zbx_mock_handle_t *member);
zbx_mock_error_t zbx_mock_vector_element(zbx_mock_handle_t vector, zbx_mock_handle_t *element);
zbx_mock_error_t zbx_mock_string(zbx_mock_handle_t string, const char **value);
+zbx_mock_error_t zbx_mock_parameter(const char *path, zbx_mock_handle_t *parameter);
#endif /* ZABBIX_MOCK_DATA_H */
diff --git a/tests/zbxmockutil.c b/tests/zbxmockutil.c
new file mode 100644
index 00000000000..ccfa54f2a76
--- /dev/null
+++ b/tests/zbxmockutil.c
@@ -0,0 +1,59 @@
+/*
+** 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"
+
+void zbx_mock_get_parameter_string(const char *path, const char **value)
+{
+ int err;
+ zbx_mock_handle_t handle;
+
+ if (ZBX_MOCK_SUCCESS != (err = zbx_mock_parameter(path, &handle)) ||
+ ZBX_MOCK_SUCCESS != (err = zbx_mock_string(handle, value)))
+ {
+ fail_msg("Cannot read parameter at \"%s\": %s", path, zbx_mock_error_string(err));
+ }
+}
+
+void zbx_mock_get_object_member_string(zbx_mock_handle_t object, const char *name, const char **value)
+{
+ int err;
+ zbx_mock_handle_t handle;
+
+
+ if (ZBX_MOCK_SUCCESS != (err = zbx_mock_object_member(object, name, &handle)) ||
+ ZBX_MOCK_SUCCESS != (err = zbx_mock_string(handle, value)))
+ {
+ fail_msg("Cannot read object member \"%s\": %s", name, zbx_mock_error_string(err));
+ }
+}
+
+
+void zbx_mock_get_parameter_handle(const char *path, zbx_mock_handle_t *handle)
+{
+ int err;
+
+ if (ZBX_MOCK_SUCCESS != (err = zbx_mock_parameter(path, handle)))
+ fail_msg("Cannot read parameter at \"%s\": %s", path, zbx_mock_error_string(err));
+}
diff --git a/tests/zbxmockutil.h b/tests/zbxmockutil.h
new file mode 100644
index 00000000000..c0ecf7a8ee8
--- /dev/null
+++ b/tests/zbxmockutil.h
@@ -0,0 +1,30 @@
+/*
+** 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.
+**/
+
+#ifndef ZABBIX_MOCK_UTIL_H
+#define ZABBIX_MOCK_UTIL_H
+
+#include "zbxmockdata.h"
+
+void zbx_mock_get_parameter_string(const char *path, const char **value);
+void zbx_mock_get_parameter_handle(const char *path, zbx_mock_handle_t *handle);
+
+void zbx_mock_get_object_member_string(zbx_mock_handle_t object, const char *name, const char **value);
+
+#endif