diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | configure.ac | 1 | ||||
-rw-r--r-- | src/zabbix_server/preprocessor/item_preproc.c | 3 | ||||
-rw-r--r-- | tests/zabbix_server/preprocessor/Makefile.am | 23 | ||||
-rw-r--r-- | tests/zabbix_server/preprocessor/item_preproc_test.c | 23 | ||||
-rw-r--r-- | tests/zabbix_server/preprocessor/item_preproc_test.h | 24 | ||||
-rw-r--r-- | tests/zabbix_server/preprocessor/item_preproc_xpath.c | 60 | ||||
-rw-r--r-- | tests/zabbix_server/preprocessor/item_preproc_xpath.yaml | 105 |
8 files changed, 239 insertions, 1 deletions
diff --git a/.gitignore b/.gitignore index a98ffc120a3..ac4468b21e4 100644 --- a/.gitignore +++ b/.gitignore @@ -486,5 +486,6 @@ tests/zabbix_server/preprocessor/.deps tests/zabbix_server/preprocessor/ADFGIJLPST tests/zabbix_server/preprocessor/Makefile tests/zabbix_server/preprocessor/Makefile.in +tests/zabbix_server/preprocessor/item_preproc_xpath tests/zabbix_server/preprocessor/zbx_item_preproc /zabbix-*.tar.gz diff --git a/configure.ac b/configure.ac index 2295c93db6c..064926ee564 100644 --- a/configure.ac +++ b/configure.ac @@ -1582,6 +1582,7 @@ SENDER_LDFLAGS="$SENDER_LDFLAGS $ZLIB_LDFLAGS $LIBPTHREAD_LDFLAGS" SENDER_LIBS="$SENDER_LIBS $ZLIB_LIBS $LIBPTHREAD_LIBS" AM_CONDITIONAL(HAVE_IPMI, [test "x$have_ipmi" = "xyes"]) +AM_CONDITIONAL(HAVE_LIBXML2, test "x$have_libxml2" = "xyes") dnl Check if Zabbix internal IPC services are used have_ipcservice="no" diff --git a/src/zabbix_server/preprocessor/item_preproc.c b/src/zabbix_server/preprocessor/item_preproc.c index fdafec61eb8..5ecf1e2e18f 100644 --- a/src/zabbix_server/preprocessor/item_preproc.c +++ b/src/zabbix_server/preprocessor/item_preproc.c @@ -1870,3 +1870,6 @@ int zbx_item_preproc_handle_error(zbx_variant_t *value, const zbx_preproc_op_t * return FAIL; } } +#ifdef HAVE_TESTS +# include "../../../tests/zabbix_server/preprocessor/item_preproc_test.c" +#endif diff --git a/tests/zabbix_server/preprocessor/Makefile.am b/tests/zabbix_server/preprocessor/Makefile.am index 5cdcf1209ee..6b7719e104c 100644 --- a/tests/zabbix_server/preprocessor/Makefile.am +++ b/tests/zabbix_server/preprocessor/Makefile.am @@ -1,5 +1,14 @@ if SERVER -noinst_PROGRAMS = zbx_item_preproc +SERVER_tests = zbx_item_preproc + +if HAVE_LIBXML2 +SERVER_tests += item_preproc_xpath +endif + +noinst_PROGRAMS = $(SERVER_tests) + +COMMON_SRC_FILES = \ + ../../zbxmocktest.h JSON_LIBS = \ $(top_srcdir)/tests/libzbxmocktest.a \ @@ -32,4 +41,16 @@ zbx_item_preproc_LDADD += @SERVER_LIBS@ zbx_item_preproc_LDFLAGS = @SERVER_LDFLAGS@ zbx_item_preproc_CFLAGS = -I@top_srcdir@/tests @LIBXML2_CFLAGS@ + +item_preproc_xpath_SOURCES = \ + ../../../src/zabbix_server/preprocessor/item_preproc.c \ + item_preproc_xpath.c \ + $(COMMON_SRC_FILES) + +item_preproc_xpath_LDADD = $(JSON_LIBS) + +item_preproc_xpath_LDADD += @SERVER_LIBS@ +item_preproc_xpath_LDFLAGS = @SERVER_LDFLAGS@ + +item_preproc_xpath_CFLAGS = -I@top_srcdir@/tests @LIBXML2_CFLAGS@ endif diff --git a/tests/zabbix_server/preprocessor/item_preproc_test.c b/tests/zabbix_server/preprocessor/item_preproc_test.c new file mode 100644 index 00000000000..27c58a97701 --- /dev/null +++ b/tests/zabbix_server/preprocessor/item_preproc_test.c @@ -0,0 +1,23 @@ +/* +** Zabbix +** Copyright (C) 2001-2019 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. +**/ + +int zbx_item_preproc_xpath(zbx_variant_t *value, const char *params, char **errmsg) +{ + return item_preproc_xpath(value, params, errmsg); +} diff --git a/tests/zabbix_server/preprocessor/item_preproc_test.h b/tests/zabbix_server/preprocessor/item_preproc_test.h new file mode 100644 index 00000000000..203b187878e --- /dev/null +++ b/tests/zabbix_server/preprocessor/item_preproc_test.h @@ -0,0 +1,24 @@ +/* +** Zabbix +** Copyright (C) 2001-2019 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 ITEM_PREPROC_TEST_H +#define ITEM_PREPROC_TEST_H + +int zbx_item_preproc_xpath(zbx_variant_t *value, const char *params, char **errmsg); + +#endif diff --git a/tests/zabbix_server/preprocessor/item_preproc_xpath.c b/tests/zabbix_server/preprocessor/item_preproc_xpath.c new file mode 100644 index 00000000000..7a9ff5b0bf7 --- /dev/null +++ b/tests/zabbix_server/preprocessor/item_preproc_xpath.c @@ -0,0 +1,60 @@ +/* +** Zabbix +** Copyright (C) 2001-2019 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 <stdio.h> +#include "zbxmocktest.h" +#include "zbxmockdata.h" +#include "zbxmockutil.h" +#include "zbxmockassert.h" +#include "common.h" + +#include "item_preproc_test.h" +#include "zbxembed.h" + +zbx_es_t es_engine; + +void zbx_mock_test_entry(void **state) +{ + zbx_variant_t value; + const char *xml; + const char *xpath, *exp_xml; + char *errmsg = NULL; + int act_ret, exp_ret; + + ZBX_UNUSED(state); + + xml = zbx_mock_get_parameter_string("in.xml"); + xpath = zbx_mock_get_parameter_string("in.xpath"); + exp_xml = zbx_mock_get_parameter_string("out.result"); + zbx_variant_set_str(&value, zbx_strdup(NULL, xml)); + + act_ret = zbx_item_preproc_xpath(&value, xpath, &errmsg); + + exp_ret = zbx_mock_str_to_return_code(zbx_mock_get_parameter_string("out.return")); + zbx_mock_assert_int_eq("return value", exp_ret, act_ret); + + if (FAIL == act_ret) + { + zbx_mock_assert_ptr_ne("error message", NULL, errmsg); + zbx_free(errmsg); + } + else + zbx_mock_assert_str_eq("result", exp_xml, value.data.str); + + zbx_variant_clear(&value); +} diff --git a/tests/zabbix_server/preprocessor/item_preproc_xpath.yaml b/tests/zabbix_server/preprocessor/item_preproc_xpath.yaml new file mode 100644 index 00000000000..bec529d3e45 --- /dev/null +++ b/tests/zabbix_server/preprocessor/item_preproc_xpath.yaml @@ -0,0 +1,105 @@ +--- +test case: 'empty input parameters' +in: + xml: '' + xpath: '' +out: + result: '' + return: 'FAIL' +--- +test case: 'single start tag' +in: + xml: '<a>' + xpath: '' +out: + result: '' + return: 'FAIL' +--- +test case: 'single end tag' +in: + xml: '<a/>' + xpath: '' +out: + result: '' + return: 'FAIL' +--- +test case: 'wrong operation format' +in: + xml: '<a/>' + xpath: '/a[\' +out: + result: '' + return: 'FAIL' +--- +test case: 'wrong operation expression' +in: + xml: '<a/>' + xpath: '1 div 0' +out: + result: '' + return: 'FAIL' +--- +test case: 'wrong operation format 2' +in: + xml: '<a/>' + xpath: '-a' +out: + result: '' + return: 'FAIL' +--- +test case: 'empty output' +in: + xml: '<a/>' + xpath: '/b' +out: + result: '' + return: 'SUCCEED' +--- +test case: 'successful expression' +in: + xml: '<a/>' + xpath: '3 div 2' +out: + result: '1.5' + return: 'SUCCEED' +--- +test case: 'return end tag' +in: + xml: '<a/>' + xpath: '/a' +out: + result: '<a/>' + return: 'SUCCEED' +--- +test case: 'return text' +in: + xml: '<a>1</a>' + xpath: '/a/text()' +out: + result: '1' + return: 'SUCCEED' +--- +test case: 'return string' +in: + xml: '<a>1</a>' + xpath: 'string(/a)' +out: + result: '1' + return: 'SUCCEED' +--- +test case: 'return attribute' +in: + xml: '<a b="10">1</a>' + xpath: 'string(/a/@b)' +out: + result: '10' + return: 'SUCCEED' +--- +test case: 'return pattern' +in: + xml: '<a><b x="1"/><c x="2"/><d x="1"/></a>' + xpath: '//*[@x="1"]' +out: + result: '<b x="1"/><d x="1"/>' + return: 'SUCCEED' +... |