diff options
-rw-r--r-- | .gitattributes | 1 | ||||
-rw-r--r-- | .gitignore | 6 | ||||
-rw-r--r-- | Makefile.am | 7 | ||||
-rw-r--r-- | configure.ac | 1 | ||||
-rw-r--r-- | include/cfg.h | 2 | ||||
-rw-r--r-- | tests/Makefile.am | 2 | ||||
-rw-r--r-- | tests/libs/Makefile.am | 1 | ||||
-rw-r--r-- | tests/libs/zbxconf/Makefile.am | 74 | ||||
-rw-r--r-- | tests/libs/zbxconf/parse_cfg_file.c | 383 | ||||
-rw-r--r-- | tests/libs/zbxconf/parse_cfg_file.yaml | 114 | ||||
-rw-r--r-- | tests/libs/zbxsysinfo/Makefile.am | 12 | ||||
-rw-r--r-- | tests/zbxmockdb.c | 1 | ||||
-rw-r--r-- | tests/zbxmockdir.c | 47 | ||||
-rw-r--r-- | tests/zbxmockstat.c | 57 |
14 files changed, 704 insertions, 4 deletions
diff --git a/.gitattributes b/.gitattributes index f25501ac60e..cea24061a36 100644 --- a/.gitattributes +++ b/.gitattributes @@ -349,6 +349,7 @@ src/zabbix_server/taskmanager/Makefile.am -text src/zabbix_server/taskmanager/proxy_tasks.c -text 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/zbxsysinfo/parse_item_key.yaml -text tests/libs/zbxsysinfo/process.yaml -text diff --git a/.gitignore b/.gitignore index 7a0f7b45cb2..a044ca5145a 100644 --- a/.gitignore +++ b/.gitignore @@ -261,6 +261,7 @@ src/libs/zbxtasks/.deps src/libs/zbxtasks/Makefile src/libs/zbxtasks/Makefile.in src/zabbix_agent/.deps +src/zabbix_agent/.dirstamp src/zabbix_agent/Makefile src/zabbix_agent/Makefile.in src/zabbix_agent/zabbix_agent @@ -371,6 +372,10 @@ tests/Makefile.in tests/libs/.deps tests/libs/Makefile tests/libs/Makefile.in +tests/libs/zbxconf/.deps +tests/libs/zbxconf/Makefile +tests/libs/zbxconf/Makefile.in +tests/libs/zbxconf/parse_cfg_file tests/libs/zbxdbhigh/.deps tests/libs/zbxdbhigh/DBselect_uint64 tests/libs/zbxdbhigh/Makefile @@ -379,6 +384,7 @@ tests/libs/zbxsysinfo/.deps tests/libs/zbxsysinfo/Makefile tests/libs/zbxsysinfo/Makefile.in tests/libs/zbxsysinfo/parse_item_key +tests/libs/zbxsysinfo/process upgrades/Makefile upgrades/Makefile.in /zabbix-*.tar.gz diff --git a/Makefile.am b/Makefile.am index 333c78c7f91..d59f52457a1 100644 --- a/Makefile.am +++ b/Makefile.am @@ -113,12 +113,19 @@ WRAP_IO_FUNCS = \ -Wl,--wrap=fclose \ -Wl,--wrap=fgets +WRAP_FS_FUNCS = \ + -Wl,--wrap=opendir \ + -Wl,--wrap=readdir \ + -Wl,--wrap=stat \ + -Wl,--wrap=__xstat + WRAP_EXIT = \ -Wl,--wrap=exit COMMON_WRAP_FUNCS = \ $(WRAP_DB_FUNCS) \ $(WRAP_IO_FUNCS) \ + $(WRAP_FS_FUNCS) \ $(WRAP_EXIT) tests: diff --git a/configure.ac b/configure.ac index b6b46837658..378adf66ad0 100644 --- a/configure.ac +++ b/configure.ac @@ -1586,6 +1586,7 @@ dnl ***************************************************************** AC_CHECK_FILE(tests, [AC_CONFIG_FILES( tests/Makefile tests/libs/Makefile + tests/libs/zbxconf/Makefile tests/libs/zbxdbhigh/Makefile tests/libs/zbxsysinfo/Makefile )]) diff --git a/include/cfg.h b/include/cfg.h index 826da674f7a..3461cbad227 100644 --- a/include/cfg.h +++ b/include/cfg.h @@ -49,7 +49,7 @@ extern int CONFIG_TIMEOUT; struct cfg_line { - char *parameter; + const char *parameter; void *variable; int type; int mandatory; diff --git a/tests/Makefile.am b/tests/Makefile.am index d429eb33542..d2997e8d7b9 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -12,7 +12,9 @@ libzbxmocktest_a_SOURCES = \ libzbxmockdata_a_SOURCES = \ zbxmockdb.c \ + zbxmockdir.c \ zbxmockexit.c \ zbxmockfile.c \ + zbxmockstat.c \ zbxmockdata.c \ zbxmockdata.h diff --git a/tests/libs/Makefile.am b/tests/libs/Makefile.am index 673547e9838..5a052b838f9 100644 --- a/tests/libs/Makefile.am +++ b/tests/libs/Makefile.am @@ -1,4 +1,5 @@ SUBDIRS = \ + zbxconf \ zbxdbhigh \ zbxsysinfo diff --git a/tests/libs/zbxconf/Makefile.am b/tests/libs/zbxconf/Makefile.am new file mode 100644 index 00000000000..9d609cbe4e5 --- /dev/null +++ b/tests/libs/zbxconf/Makefile.am @@ -0,0 +1,74 @@ +if SERVER +noinst_PROGRAMS = parse_cfg_file + +parse_cfg_file_SOURCES = \ + parse_cfg_file.c \ + ../../../src/zabbix_server/actions.c \ + ../../../src/zabbix_server/events.c \ + ../../../src/zabbix_server/operations.c \ + ../../zbxmocktest.h + +parse_cfg_file_LDADD = \ + $(top_srcdir)/src/zabbix_server/escalator/libzbxescalator.a \ + $(top_srcdir)/src/zabbix_server/scripts/libzbxscripts.a \ + $(top_srcdir)/src/zabbix_server/poller/libzbxpoller.a \ + $(top_srcdir)/src/zabbix_server/alerter/libzbxalerter.a \ + $(top_srcdir)/src/zabbix_server/dbsyncer/libzbxdbsyncer.a \ + $(top_srcdir)/src/zabbix_server/dbconfig/libzbxdbconfig.a \ + $(top_srcdir)/src/zabbix_server/discoverer/libzbxdiscoverer.a \ + $(top_srcdir)/src/zabbix_server/pinger/libzbxpinger.a \ + $(top_srcdir)/src/zabbix_server/poller/libzbxpoller.a \ + $(top_srcdir)/src/zabbix_server/housekeeper/libzbxhousekeeper.a \ + $(top_srcdir)/src/zabbix_server/timer/libzbxtimer.a \ + $(top_srcdir)/src/zabbix_server/trapper/libzbxtrapper.a \ + $(top_srcdir)/src/zabbix_server/snmptrapper/libzbxsnmptrapper.a \ + $(top_srcdir)/src/zabbix_server/httppoller/libzbxhttppoller.a \ + $(top_srcdir)/src/zabbix_server/escalator/libzbxescalator.a \ + $(top_srcdir)/src/zabbix_server/proxypoller/libzbxproxypoller.a \ + $(top_srcdir)/src/zabbix_server/selfmon/libzbxselfmon.a \ + $(top_srcdir)/src/zabbix_server/vmware/libzbxvmware.a \ + $(top_srcdir)/src/zabbix_server/taskmanager/libzbxtaskmanager.a \ + $(top_srcdir)/src/zabbix_server/ipmi/libipmi.a \ + $(top_srcdir)/src/zabbix_server/odbc/libzbxodbc.a \ + $(top_srcdir)/src/zabbix_server/scripts/libzbxscripts.a \ + $(top_srcdir)/src/zabbix_server/preprocessor/libpreprocessor.a \ + $(top_srcdir)/src/libs/zbxsysinfo/libzbxserversysinfo.a \ + $(top_srcdir)/src/libs/zbxsysinfo/common/libcommonsysinfo.a \ + $(top_srcdir)/src/libs/zbxsysinfo/simple/libsimplesysinfo.a \ + $(top_srcdir)/src/libs/zbxlog/libzbxlog.a \ + $(top_srcdir)/src/libs/zbxserver/libzbxserver.a \ + $(top_srcdir)/src/libs/zbxsysinfo/libzbxserversysinfo.a \ + $(top_srcdir)/src/libs/zbxsysinfo/common/libcommonsysinfo.a \ + $(top_srcdir)/src/libs/zbxsysinfo/simple/libsimplesysinfo.a \ + $(top_srcdir)/src/libs/zbxdbcache/libzbxdbcache.a \ + $(top_srcdir)/src/libs/zbxmemory/libzbxmemory.a \ + $(top_srcdir)/src/libs/zbxregexp/libzbxregexp.a \ + $(top_srcdir)/src/libs/zbxself/libzbxself.a \ + $(top_srcdir)/src/libs/zbxnix/libzbxnix.a \ + $(top_srcdir)/src/libs/zbxalgo/libzbxalgo.a \ + $(top_srcdir)/src/libs/zbxsys/libzbxsys.a \ + $(top_srcdir)/src/libs/zbxconf/libzbxconf.a \ + $(top_srcdir)/src/libs/zbxmedia/libzbxmedia.a \ + $(top_srcdir)/src/libs/zbxcommon/libzbxcommon.a \ + $(top_srcdir)/src/libs/zbxcrypto/libzbxcrypto.a \ + $(top_srcdir)/src/libs/zbxcomms/libzbxcomms.a \ + $(top_srcdir)/src/libs/zbxcommon/libzbxcommon.a \ + $(top_srcdir)/src/libs/zbxcrypto/libzbxcrypto.a \ + $(top_srcdir)/src/libs/zbxcommshigh/libzbxcommshigh.a \ + $(top_srcdir)/src/libs/zbxjson/libzbxjson.a \ + $(top_srcdir)/src/libs/zbxhttp/libzbxhttp.a \ + $(top_srcdir)/src/libs/zbxipcservice/libzbxipcservice.a \ + $(top_srcdir)/src/libs/zbxexec/libzbxexec.a \ + $(top_srcdir)/src/libs/zbxicmpping/libzbxicmpping.a \ + $(top_srcdir)/src/libs/zbxdbupgrade/libzbxdbupgrade.a \ + $(top_srcdir)/src/libs/zbxdbhigh/libzbxdbhigh.a \ + $(top_srcdir)/src/libs/zbxdb/libzbxdb.a \ + $(top_srcdir)/src/libs/zbxmodules/libzbxmodules.a \ + $(top_srcdir)/src/libs/zbxtasks/libzbxtasks.a \ + $(top_srcdir)/tests/libzbxmocktest.a \ + $(top_srcdir)/tests/libzbxmockdata.a + +parse_cfg_file_LDADD += @SERVER_LIBS@ + +parse_cfg_file_CFLAGS = -I@top_srcdir@/tests +endif diff --git a/tests/libs/zbxconf/parse_cfg_file.c b/tests/libs/zbxconf/parse_cfg_file.c new file mode 100644 index 00000000000..d7e05967068 --- /dev/null +++ b/tests/libs/zbxconf/parse_cfg_file.c @@ -0,0 +1,383 @@ +/* +** 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 "common.h" +#include "cfg.h" + +void zbx_mock_test_entry(void **state) +{ + zbx_mock_error_t error; + zbx_mock_handle_t handle, parameters, parameter; + const char *cfg_file, *validation, *tmp, **multi_string, *string_list; + int strict = 42, exit_code, parameter_count = 0, i; + struct cfg_line *cfg = NULL; + void **expected_values = NULL; + + ZBX_UNUSED(state); + + if (ZBX_MOCK_SUCCESS != (error = zbx_mock_in_parameter("configuration file", &handle)) || + ZBX_MOCK_SUCCESS != (error = zbx_mock_string(handle, &cfg_file))) + { + fail_msg("Cannot get configuration file from test case data: %s", zbx_mock_error_string(error)); + } + + if (ZBX_MOCK_SUCCESS != (error = zbx_mock_in_parameter("validation", &handle)) || + ZBX_MOCK_SUCCESS != (error = zbx_mock_string(handle, &validation))) + { + fail_msg("Cannot get validation mode from test case data: %s", zbx_mock_error_string(error)); + } + + if (0 == strcmp(validation, "not strict")) + strict = ZBX_CFG_NOT_STRICT; + else if (0 == strcmp(validation, "strict")) + strict = ZBX_CFG_STRICT; + else + fail_msg("Invalid validation mode \"%s\".", validation); + + if (ZBX_MOCK_SUCCESS != (error = zbx_mock_in_parameter("parameters", ¶meters))) + fail_msg("Cannot get description of parameters from test case data: %s", zbx_mock_error_string(error)); + + while (ZBX_MOCK_SUCCESS == (error = zbx_mock_vector_element(parameters, ¶meter))) + { + cfg = zbx_realloc(cfg, (parameter_count + 1) * sizeof(struct cfg_line)); + expected_values = zbx_realloc(expected_values, (parameter_count + 1) * sizeof(void *)); + + if (ZBX_MOCK_SUCCESS != (error = zbx_mock_object_member(parameter, "name", &handle)) || + ZBX_MOCK_SUCCESS != (error = zbx_mock_string(handle, &tmp))) + { + fail_msg("Cannot get name of parameter #%d: %s", parameter_count + 1, + zbx_mock_error_string(error)); + } + + cfg[parameter_count].parameter = tmp; + + if (ZBX_MOCK_SUCCESS != (error = zbx_mock_object_member(parameter, "type", &handle)) || + ZBX_MOCK_SUCCESS != (error = zbx_mock_string(handle, &tmp))) + { + fail_msg("Cannot get type of parameter #%d: %s", parameter_count + 1, + zbx_mock_error_string(error)); + } + + if (0 == strcmp(tmp, "numeric")) + { + if (ZBX_MOCK_NO_SUCH_MEMBER == (error = zbx_mock_object_member(parameter, "expected", &handle))) + { + expected_values[parameter_count] = NULL; + } + else if (ZBX_MOCK_SUCCESS == error && ZBX_MOCK_SUCCESS == (error = zbx_mock_string(handle, &tmp))) + { + expected_values[parameter_count] = zbx_malloc(NULL, sizeof(zbx_uint64_t)); + + if (SUCCEED != is_uint64(tmp, expected_values[parameter_count])) + { + fail_msg("Expected value \"%s\" of parameter #%d is not numeric.", tmp, + parameter_count + 1); + } + } + else + break; + + cfg[parameter_count].variable = zbx_malloc(NULL, sizeof(zbx_uint64_t)); + *(zbx_uint64_t *)cfg[parameter_count].variable = (zbx_uint64_t)-1; + + if (ZBX_MOCK_NO_SUCH_MEMBER == (error = zbx_mock_object_member(parameter, "expected", &handle))) + { + expected_values[parameter_count] = NULL; + } + else if (ZBX_MOCK_SUCCESS == error && ZBX_MOCK_SUCCESS == (error = zbx_mock_string(handle, &tmp))) + { + expected_values[parameter_count] = zbx_malloc(NULL, sizeof(zbx_uint64_t)); + + if (SUCCEED != is_uint64(tmp, expected_values[parameter_count])) + { + fail_msg("Expected value \"%s\" of parameter #%d is not numeric.", tmp, + parameter_count + 1); + } + } + else + break; + + if (ZBX_MOCK_NO_SUCH_MEMBER == (error = zbx_mock_object_member(parameter, "min", &handle))) + { + cfg[parameter_count].min = 0; + } + else if (ZBX_MOCK_SUCCESS == error && ZBX_MOCK_SUCCESS == (error = zbx_mock_string(handle, &tmp))) + { + zbx_uint64_t min; + + if (SUCCEED != is_uint64(tmp, &min)) + { + fail_msg("Minimum allowed value \"%s\" of parameter #%d is not numeric.", tmp, + parameter_count + 1); + } + } + else + break; + + if (ZBX_MOCK_NO_SUCH_MEMBER == (error = zbx_mock_object_member(parameter, "max", &handle))) + { + cfg[parameter_count].max = 0; + } + else if (ZBX_MOCK_SUCCESS == error && ZBX_MOCK_SUCCESS == (error = zbx_mock_string(handle, &tmp))) + { + zbx_uint64_t max; + + if (SUCCEED != is_uint64(tmp, &max)) + { + fail_msg("Maximum allowed value \"%s\" of parameter #%d is not numeric.", tmp, + parameter_count + 1); + } + } + else + break; + + cfg[parameter_count].type = TYPE_UINT64; /* no separate treatment for TYPE_INT */ + } + else if (0 == strcmp(tmp, "string")) + { + if (ZBX_MOCK_NO_SUCH_MEMBER == (error = zbx_mock_object_member(parameter, "expected", &handle))) + { + expected_values[parameter_count] = NULL; + } + else if (ZBX_MOCK_SUCCESS == error && ZBX_MOCK_SUCCESS == (error = zbx_mock_string(handle, &tmp))) + { + expected_values[parameter_count] = zbx_malloc(NULL, sizeof(char *)); + *(const char **)expected_values[parameter_count] = tmp; + } + else + break; + + cfg[parameter_count].variable = zbx_malloc(NULL, sizeof(char *)); + *(char **)cfg[parameter_count].variable = NULL; + cfg[parameter_count].min = 0; + cfg[parameter_count].max = 0; + cfg[parameter_count].type = TYPE_STRING; + } + else if (0 == strcmp(tmp, "string list")) + { + expected_values[parameter_count] = zbx_malloc(NULL, sizeof(zbx_mock_handle_t)); + + if (ZBX_MOCK_NO_SUCH_MEMBER == (error = zbx_mock_object_member(parameter, "expected", + expected_values[parameter_count]))) + { + fail_msg("Missing expected field for parameter #%d of string list type, use [] instead.", + parameter_count + 1); + } + + if (ZBX_MOCK_SUCCESS != error) + break; + + cfg[parameter_count].variable = zbx_malloc(NULL, sizeof(char *)); + *(char **)cfg[parameter_count].variable = NULL; + cfg[parameter_count].min = 0; + cfg[parameter_count].max = 0; + cfg[parameter_count].type = TYPE_STRING_LIST; + } + else if (0 == strcmp(tmp, "multi string")) + { + expected_values[parameter_count] = zbx_malloc(NULL, sizeof(zbx_mock_handle_t)); + + if (ZBX_MOCK_NO_SUCH_MEMBER == (error = zbx_mock_object_member(parameter, "expected", + expected_values[parameter_count]))) + { + fail_msg("Missing expected field for parameter #%d of multi string type, use [] instead.", + parameter_count + 1); + } + + if (ZBX_MOCK_SUCCESS != error) + break; + + cfg[parameter_count].variable = zbx_malloc(NULL, sizeof(char **)); + *(char ***)cfg[parameter_count].variable = NULL; + zbx_strarr_init((char ***)cfg[parameter_count].variable); + cfg[parameter_count].min = 0; + cfg[parameter_count].max = 0; + cfg[parameter_count].type = TYPE_MULTISTRING; + } + else + fail_msg("Invalid type \"%s\" of parameter #%d.", tmp, parameter_count + 1); + + if (ZBX_MOCK_SUCCESS != (error = zbx_mock_object_member(parameter, "mandatory", &handle)) || + ZBX_MOCK_SUCCESS != (error = zbx_mock_string(handle, &tmp))) + { + fail_msg("Cannot get mandatory flag of parameter #%d: %s", parameter_count + 1, + zbx_mock_error_string(error)); + } + + if (0 == strcmp(tmp, "yes")) + cfg[parameter_count].mandatory = PARM_MAND; + else if (0 == strcmp(tmp, "no")) + cfg[parameter_count].mandatory = PARM_OPT; + else + fail_msg("Invalid mandatory flag \"%s\" of parameter #%d.", tmp, parameter_count + 1); + + parameter_count++; + } + + if (ZBX_MOCK_END_OF_VECTOR != error) + { + fail_msg("Cannot get description of parameter #%d from test case data: %s", parameter_count + 1, + zbx_mock_error_string(error)); + } + + cfg = zbx_realloc(cfg, (parameter_count + 1) * sizeof(struct cfg_line)); + cfg[parameter_count].parameter = NULL; + + parse_cfg_file(cfg_file, cfg, ZBX_CFG_FILE_REQUIRED, strict); + + if (ZBX_MOCK_NO_EXIT_CODE != (error = zbx_mock_exit_code(&exit_code))) + { + if (ZBX_MOCK_SUCCESS == error) + fail_msg("parse_cfg_file() was expected to call exit(%d), but has not.", exit_code); + else + fail_msg("Cannot get exit code from test case data: %s", zbx_mock_error_string(error)); + } + + for (i = 0; i < parameter_count; i++) + { + switch (cfg[i].type) + { + case TYPE_MULTISTRING: + multi_string = *(const char ***)cfg[i].variable; + while (ZBX_MOCK_SUCCESS == (error = zbx_mock_vector_element( + *(zbx_mock_handle_t *)expected_values[i], &handle)) && + ZBX_MOCK_SUCCESS == (error = zbx_mock_string(handle, &tmp))) + { + if (NULL == *multi_string) + { + fail_msg("Values of multi string parameter \"%s\" ended while \"%s\"" + " was expected.", cfg[i].parameter, tmp); + } + + if (0 != strcmp(*multi_string, tmp)) + { + fail_msg("Value \"%s\" of multi string parameter \"%s\"" + " differs from expected \"%s\".", *multi_string, + cfg[i].parameter, tmp); + } + + multi_string++; + } + if (ZBX_MOCK_END_OF_VECTOR != error) + { + fail_msg("Cannot get expected values of multi string parameter \"%s\": %s", + cfg[i].parameter, zbx_mock_error_string(error)); + } + if (NULL != *multi_string) + { + fail_msg("Value of multi string parameter \"%s\" ends with unexpected \"%s\"" + " (and maybe more).", cfg[i].parameter, *multi_string); + } + break; + case TYPE_STRING_LIST: + string_list = *(const char **)cfg[i].variable; + while (ZBX_MOCK_SUCCESS == (error = zbx_mock_vector_element( + *(zbx_mock_handle_t *)expected_values[i], &handle)) && + ZBX_MOCK_SUCCESS == (error = zbx_mock_string(handle, &tmp))) + { + if ('\0' == *string_list) + { + fail_msg("Value of string list parameter \"%s\" ended when \"%s{,...}\"" + " was expected.", cfg[i].parameter, tmp); + } + + if (0 != strncmp(string_list, tmp, strlen(tmp))) + { + fail_msg("Value of string list parameter \"%s\" starting with \"%s\"" + " differs from expected \"%s{,...}\".", cfg[i].parameter, + string_list, tmp); + } + + string_list += strlen(tmp); + + if (',' != *string_list) + { + if ('\0' != *string_list) + { + fail_msg("Value of string list parameter \"%s\" starting with" + " \"%s\" differs from expected.", cfg[i].parameter, + string_list); + } + } + else + string_list++; + } + if (ZBX_MOCK_END_OF_VECTOR != error) + { + fail_msg("Cannot get expected value of string list parameter \"%s\": %s", + cfg[i].parameter, zbx_mock_error_string(error)); + } + if ('\0' != *string_list) + { + fail_msg("Values of string list parameter \"%s\" ends with unexpected \"%s\".", + cfg[i].parameter, string_list); + } + break; + case TYPE_STRING: + if (0 != strcmp(*(char **)cfg[i].variable, *(char **)expected_values[i])) + { + fail_msg("Value \"%s\" of string parameter \"%s\" differs from expected \"%s\".", + *(char **)cfg[i].variable, cfg[i].parameter, + *(char **)expected_values[i]); + } + break; + case TYPE_UINT64: + if (*(zbx_uint64_t *)cfg[i].variable != *(zbx_uint64_t *)expected_values[i]) + { + fail_msg("Value " ZBX_FS_UI64 " of numeric parameter \"%s\"" + " differs from expected (" ZBX_FS_UI64 ").", + *(zbx_uint64_t *)cfg[i].variable, cfg[i].parameter, + *(zbx_uint64_t *)expected_values[i]); + } + break; + default: + fail_msg("Invalid type of parameter \"%s\" when doing validation.", cfg[i].parameter); + } + } + + for (i = 0; i < parameter_count; i++) + { + switch (cfg[i].type) + { + case TYPE_MULTISTRING: + zbx_strarr_free(*(char ***)cfg[i].variable); + zbx_free(cfg[i].variable); + zbx_free(expected_values[i]); + break; + case TYPE_STRING_LIST: + case TYPE_STRING: + zbx_free(*(char **)cfg[i].variable); + zbx_free(cfg[i].variable); + zbx_free(expected_values[i]); + break; + case TYPE_UINT64: + zbx_free(cfg[i].variable); + zbx_free(expected_values[i]); + break; + default: + fail_msg("Invalid type of parameter \"%s\" when doing cleanup.", cfg[i].parameter); + } + } + + zbx_free(expected_values); + zbx_free(cfg); +} diff --git a/tests/libs/zbxconf/parse_cfg_file.yaml b/tests/libs/zbxconf/parse_cfg_file.yaml new file mode 100644 index 00000000000..0da12586b50 --- /dev/null +++ b/tests/libs/zbxconf/parse_cfg_file.yaml @@ -0,0 +1,114 @@ +# NOTE: underlying mocks for filesystem allow only files in "Include=...", no directories, no patterns +--- +test case: valid configuration file +in: + configuration file: foo.conf + validation: strict + parameters: + - name: StartDBSyncers + type: numeric + mandatory: no + min: 1 + max: 100 + expect: 4 + - name: DBPassword + type: string + mandatory: yes + expect: 'r3ally_$tr0ng_pa$$w0rd' + - name: Server + type: string list + mandatory: yes + expect: [1.2.3.4, 5.6.7.8, localhost] + - name: LoadModule + type: multi string + mandatory: no + expect: [dummy.so, smarty.so] +files: + foo.conf: | + # whitespace should not matter + Server = 1.2.3.4 , 5.6.7.8 , localhost + LoadModule=dummy.so + StartDBSyncers=4 + Include= db.conf # DB credentials + LoadModule=smArty.so + #LoadModule=beauty.so + db.conf: | + DBPassword=r3ally_$tr0ng_pa$$w0rd +--- +test case: recursive self inclusion +in: + configuration file: file.conf + validation: strict + parameters: [] +exit code: failure +files: + file.conf: | + Include=file.conf +--- +test case: recursive inclusion loop +in: + configuration file: foo.conf + validation: strict + parameters: [] +exit code: failure +files: + foo.conf: | + Include=bar.conf + bar.conf: | + Include=foo.conf +--- +test case: maximum allowed inclusion levels +in: + configuration file: 1.conf + validation: strict + parameters: [] +files: + 1.conf: | + Include=2.conf + 2.conf: | + Include=3.conf + 3.conf: | + Include=4.conf + 4.conf: | + Include=5.conf + 5.conf: | + Include=6.conf + 6.conf: | + Include=7.conf + 7.conf: | + Include=8.conf + 8.conf: | + Include=9.conf + 9.conf: | + Include=10.conf + 10.conf: | +--- +test case: too many inclusion levels +in: + configuration file: 1.conf + validation: strict + parameters: [] +exit code: failure +files: + 1.conf: | + Include=2.conf + 2.conf: | + Include=3.conf + 3.conf: | + Include=4.conf + 4.conf: | + Include=5.conf + 5.conf: | + Include=6.conf + 6.conf: | + Include=7.conf + 7.conf: | + Include=8.conf + 8.conf: | + Include=9.conf + 9.conf: | + Include=10.conf + 10.conf: | + Include=11.conf + 11.conf: | +... diff --git a/tests/libs/zbxsysinfo/Makefile.am b/tests/libs/zbxsysinfo/Makefile.am index 9f87498cc30..0305fd4f542 100644 --- a/tests/libs/zbxsysinfo/Makefile.am +++ b/tests/libs/zbxsysinfo/Makefile.am @@ -1,6 +1,14 @@ if SERVER -noinst_PROGRAMS = parse_item_key +SERVER_tests = parse_item_key +endif + +if AGENT +AGENT_tests = process +endif +noinst_PROGRAMS = $(SERVER_tests) $(AGENT_tests) + +if SERVER parse_item_key_SOURCES = \ parse_item_key.c \ ../../../src/zabbix_server/actions.c \ @@ -73,8 +81,6 @@ parse_item_key_CFLAGS = -I@top_srcdir@/tests endif if AGENT -noinst_PROGRAMS = process - process_SOURCES = \ process.c \ ../../../src/zabbix_agent/active.c ./../../src/zabbix_agent/active.h\ diff --git a/tests/zbxmockdb.c b/tests/zbxmockdb.c index 3e0adb90467..63ae7fb2f13 100644 --- a/tests/zbxmockdb.c +++ b/tests/zbxmockdb.c @@ -213,4 +213,5 @@ void __wrap_DBbegin(void) int __wrap_DBcommit(void) { + return ZBX_DB_OK; } diff --git a/tests/zbxmockdir.c b/tests/zbxmockdir.c new file mode 100644 index 00000000000..dba4e727baa --- /dev/null +++ b/tests/zbxmockdir.c @@ -0,0 +1,47 @@ +/* +** 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. +**/ + +/* make sure that __wrap_*() prototypes match unwrapped counterparts */ + +#define opendir __wrap_opendir +#define readdir __wrap_readdir +#include <dirent.h> +#undef opendir +#undef readdir + +#include "zbxmocktest.h" +#include "zbxmockdata.h" + +#include "common.h" + +DIR *__wrap_opendir(const char *name) +{ + ZBX_UNUSED(name); + + errno = ENOENT; + return NULL; +} + +struct dirent *__wrap_readdir(DIR *dirp) +{ + ZBX_UNUSED(dirp); + + errno = EBADF; + return NULL; +} diff --git a/tests/zbxmockstat.c b/tests/zbxmockstat.c new file mode 100644 index 00000000000..4bcf2cb7625 --- /dev/null +++ b/tests/zbxmockstat.c @@ -0,0 +1,57 @@ +/* +** 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 "common.h" + +int __wrap_stat(const char *pathname, struct stat *buf) +{ + zbx_mock_error_t error; + zbx_mock_handle_t handle; + + if (ZBX_MOCK_SUCCESS == (error = zbx_mock_file(pathname, &handle))) + { + buf->st_mode = S_IFMT & S_IFREG; + return 0; + } + + if (ZBX_MOCK_NO_PARAMETER != error) + { + fail_msg("Error during path \"%s\" lookup among files: %s", pathname, + zbx_mock_error_string(error)); + } + + if (0) /* directory lookup is not implemented */ + { + buf->st_mode = S_IFMT & S_IFDIR; + return 0; + } + + errno = ENOENT; /* No such file or directory */ + return -1; +} + +int __wrap___xstat(int ver, const char *pathname, struct stat *buf) +{ + ZBX_UNUSED(ver); + + return __wrap_stat(pathname, buf); +} |