diff options
-rw-r--r-- | .gitignore | 3 | ||||
-rw-r--r-- | ChangeLog | 2 | ||||
-rw-r--r-- | include/zbxjson.h | 12 | ||||
-rw-r--r-- | src/libs/zbxdbhigh/proxy.c | 32 | ||||
-rw-r--r-- | src/libs/zbxjson/json.c | 126 | ||||
-rw-r--r-- | tests/libs/zbxjson/Makefile.am | 43 | ||||
-rw-r--r-- | tests/libs/zbxjson/mock_json.c | 38 | ||||
-rw-r--r-- | tests/libs/zbxjson/mock_json.h | 25 | ||||
-rw-r--r-- | tests/libs/zbxjson/zbx_json_decodevalue.c | 63 | ||||
-rw-r--r-- | tests/libs/zbxjson/zbx_json_decodevalue.yaml | 357 | ||||
-rw-r--r-- | tests/libs/zbxjson/zbx_json_decodevalue_dyn.c | 63 | ||||
-rw-r--r-- | tests/libs/zbxjson/zbx_json_decodevalue_dyn.yaml | 383 |
12 files changed, 1062 insertions, 85 deletions
diff --git a/.gitignore b/.gitignore index c5b8933543f..2a9ea39461b 100644 --- a/.gitignore +++ b/.gitignore @@ -128,6 +128,8 @@ tests/libs/zbxdbhigh/DBselect_uint64 tests/libs/zbxhistory/zbx_history_get_values tests/libs/zbxjson/zbx_json_path_open tests/libs/zbxjson/jsonpath_next +tests/libs/zbxjson/zbx_json_decodevalue +tests/libs/zbxjson/zbx_json_decodevalue_dyn tests/libs/zbxprometheus/prometheus_filter_init tests/libs/zbxprometheus/prometheus_parse_row tests/libs/zbxprometheus/zbx_prometheus_pattern @@ -145,6 +147,7 @@ tests/libs/zbxsysinfo/linux/SYSTEM_BOOTTIME tests/libs/zbxsysinfo/linux/SYSTEM_CPU_INTR tests/libs/zbxsysinfo/linux/SYSTEM_CPU_SWITCHES tests/libs/zbxsysinfo/linux/VFS_FS_DISCOVERY +tests/libs/zbxsysinfo/linux/SYSTEM_HW_CHASSIS tests/libs/zbxsysinfo/parse_item_key tests/libs/zbxsysinfo/process tests/libs/zbxcommon/zbx_user_macro_parse diff --git a/ChangeLog b/ChangeLog index f8f6ed6cc4c..e4edc021895 100644 --- a/ChangeLog +++ b/ChangeLog @@ -16,6 +16,7 @@ Changes for 4.2.3rc1 New features: Bug fixes: +.......PS. [ZBX-16151] fixed theoretical possibility of large numbers in json data being truncated, added boolean value support to json parser (wiper) ..F....... [ZBX-15778] fixed wrong filtering by "Age less than" and "Show suppressed problems" in trigger overview (vasilijs) ........S. [ZBX-16150] fixed inactive, unmounted, unaccessible VMware datastore causes unknown column nan insertion in field list (akozlovs) A......... [ZBX-16122] fixed api validation of trigger dependency (averza) @@ -413,6 +414,7 @@ Changes for 4.0.9rc1 New features: Bug fixes: +.......PS. [ZBX-16151] fixed theoretical possibility of large numbers in json data being truncated, added boolean value support to json parser (wiper) ..F....... [ZBX-15778] fixed wrong filtering by "Age less than" and "Show suppressed problems" in trigger overview (vasilijs) ..F....... [ZBX-15585] fixed web scenarios pair manager issue when fields are duplicating on post type toggle (talbergs) ........S. [ZBX-16150] fixed inactive, unmounted, unaccessible VMware datastore causes unknown column nan insertion in field list (akozlovs) diff --git a/include/zbxjson.h b/include/zbxjson.h index 298ac365ef8..8e450d19c23 100644 --- a/include/zbxjson.h +++ b/include/zbxjson.h @@ -153,7 +153,9 @@ typedef enum ZBX_JSON_TYPE_INT, ZBX_JSON_TYPE_ARRAY, ZBX_JSON_TYPE_OBJECT, - ZBX_JSON_TYPE_NULL + ZBX_JSON_TYPE_NULL, + ZBX_JSON_TYPE_TRUE, + ZBX_JSON_TYPE_FALSE } zbx_json_type_t; @@ -200,9 +202,10 @@ int zbx_json_close(struct zbx_json *j); int zbx_json_open(const char *buffer, struct zbx_json_parse *jp); const char *zbx_json_next(const struct zbx_json_parse *jp, const char *p); -const char *zbx_json_next_value(const struct zbx_json_parse *jp, const char *p, char *string, size_t len, int *is_null); +const char *zbx_json_next_value(const struct zbx_json_parse *jp, const char *p, char *string, size_t len, + zbx_json_type_t *type); const char *zbx_json_next_value_dyn(const struct zbx_json_parse *jp, const char *p, char **string, - size_t *string_alloc, int *is_null); + size_t *string_alloc, zbx_json_type_t *type); const char *zbx_json_pair_next(const struct zbx_json_parse *jp, const char *p, char *name, size_t len); const char *zbx_json_pair_by_name(const struct zbx_json_parse *jp, const char *name); int zbx_json_value_by_name(const struct zbx_json_parse *jp, const char *name, char *string, size_t len); @@ -211,7 +214,8 @@ int zbx_json_brackets_open(const char *p, struct zbx_json_parse *out); int zbx_json_brackets_by_name(const struct zbx_json_parse *jp, const char *name, struct zbx_json_parse *out); int zbx_json_object_is_empty(const struct zbx_json_parse *jp); int zbx_json_count(const struct zbx_json_parse *jp); -const char *zbx_json_decodevalue(const char *p, char *string, size_t size, int *is_null); +const char *zbx_json_decodevalue(const char *p, char *string, size_t size, zbx_json_type_t *type); +const char *zbx_json_decodevalue_dyn(const char *p, char **string, size_t *string_alloc, zbx_json_type_t *type); void zbx_json_escape(char **string); int zbx_json_path_check(const char *path, char * error, size_t errlen); diff --git a/src/libs/zbxdbhigh/proxy.c b/src/libs/zbxdbhigh/proxy.c index 7b7d16754e7..4eb28b29863 100644 --- a/src/libs/zbxdbhigh/proxy.c +++ b/src/libs/zbxdbhigh/proxy.c @@ -948,7 +948,7 @@ static int compare_nth_field(const ZBX_FIELD **fields, const char *rec_data, int static int process_proxyconfig_table(const ZBX_TABLE *table, struct zbx_json_parse *jp_obj, zbx_vector_uint64_t *del, char **error) { - int f, fields_count, insert, is_null, i, ret = FAIL, id_field_nr = 0, move_out = 0, + int f, fields_count, insert, i, ret = FAIL, id_field_nr = 0, move_out = 0, move_field_nr = 0; const ZBX_FIELD *fields[ZBX_MAX_FIELDS]; struct zbx_json_parse jp_data, jp_row; @@ -1176,8 +1176,9 @@ static int process_proxyconfig_table(const ZBX_TABLE *table, struct zbx_json_par if (1 == move_out) { - int last_n = 0; - size_t last_pos = 0; + int last_n = 0; + size_t last_pos = 0; + zbx_json_type_t type; /* locate a copy of this record as found in database */ id_offset.id = recid; @@ -1189,7 +1190,7 @@ static int process_proxyconfig_table(const ZBX_TABLE *table, struct zbx_json_par /* find the field requiring special preprocessing in JSON record */ f = 1; - while (NULL != (pf = zbx_json_next_value_dyn(&jp_row, pf, &buf, &buf_alloc, &is_null))) + while (NULL != (pf = zbx_json_next_value_dyn(&jp_row, pf, &buf, &buf_alloc, &type))) { /* parse values for the entry (lines 10-12 in T1) */ @@ -1206,7 +1207,7 @@ static int process_proxyconfig_table(const ZBX_TABLE *table, struct zbx_json_par } if (0 != compare_nth_field(fields, recs + p_id_offset->offset, move_field_nr, buf, - is_null, &last_n, &last_pos)) + (ZBX_JSON_TYPE_NULL == type), &last_n, &last_pos)) { zbx_vector_uint64_append(&moves, recid); } @@ -1296,9 +1297,10 @@ static int process_proxyconfig_table(const ZBX_TABLE *table, struct zbx_json_par /* iterate the entries (lines 9, 14 and 19 in T1) */ while (NULL != (p = zbx_json_next(&jp_data, p))) { - int rec_differ = 0; /* how many fields differ */ - int last_n = 0; - size_t tmp_offset = sql_offset, last_pos = 0; + int rec_differ = 0; /* how many fields differ */ + int last_n = 0; + size_t tmp_offset = sql_offset, last_pos = 0; + zbx_json_type_t type; zbx_json_brackets_open(p, &jp_row); pf = zbx_json_next_value_dyn(&jp_row, NULL, &buf, &buf_alloc, NULL); @@ -1319,7 +1321,7 @@ static int process_proxyconfig_table(const ZBX_TABLE *table, struct zbx_json_par zbx_vector_ptr_append(&values, value); /* add the rest of fields */ - for (f = 1; NULL != (pf = zbx_json_next_value_dyn(&jp_row, pf, &buf, &buf_alloc, &is_null)); + for (f = 1; NULL != (pf = zbx_json_next_value_dyn(&jp_row, pf, &buf, &buf_alloc, &type)); f++) { if (f == fields_count) @@ -1329,7 +1331,7 @@ static int process_proxyconfig_table(const ZBX_TABLE *table, struct zbx_json_par goto clean; } - if (0 != is_null && 0 != (fields[f]->flags & ZBX_NOTNULL)) + if (ZBX_JSON_TYPE_NULL == type && 0 != (fields[f]->flags & ZBX_NOTNULL)) { *error = zbx_dsprintf(*error, "column \"%s.%s\" cannot be null", table->table, fields[f]->name); @@ -1347,7 +1349,7 @@ static int process_proxyconfig_table(const ZBX_TABLE *table, struct zbx_json_par ZBX_STR2UINT64(value->ui64, buf); break; case ZBX_TYPE_ID: - if (0 == is_null) + if (ZBX_JSON_TYPE_NULL != type) ZBX_STR2UINT64(value->ui64, buf); else value->ui64 = 0; @@ -1413,7 +1415,7 @@ static int process_proxyconfig_table(const ZBX_TABLE *table, struct zbx_json_par zbx_snprintf_alloc(&sql, &sql_alloc, &sql_offset, "update %s set ", table->table); - for (f = 1; NULL != (pf = zbx_json_next_value_dyn(&jp_row, pf, &buf, &buf_alloc, &is_null)); + for (f = 1; NULL != (pf = zbx_json_next_value_dyn(&jp_row, pf, &buf, &buf_alloc, &type)); f++) { int field_differ = 1; @@ -1427,7 +1429,7 @@ static int process_proxyconfig_table(const ZBX_TABLE *table, struct zbx_json_par goto clean; } - if (0 != is_null && 0 != (fields[f]->flags & ZBX_NOTNULL)) + if (ZBX_JSON_TYPE_NULL == type && 0 != (fields[f]->flags & ZBX_NOTNULL)) { *error = zbx_dsprintf(*error, "column \"%s.%s\" cannot be null", table->table, fields[f]->name); @@ -1442,7 +1444,7 @@ static int process_proxyconfig_table(const ZBX_TABLE *table, struct zbx_json_par } if (0 == (field_differ = compare_nth_field(fields, recs + p_id_offset->offset, f, buf, - is_null, &last_n, &last_pos))) + (ZBX_JSON_TYPE_NULL == type), &last_n, &last_pos))) { continue; } @@ -1459,7 +1461,7 @@ static int process_proxyconfig_table(const ZBX_TABLE *table, struct zbx_json_par zbx_snprintf_alloc(&sql, &sql_alloc, &sql_offset, "%s=", fields[f]->name); rec_differ++; - if (0 != is_null) + if (ZBX_JSON_TYPE_NULL == type) { zbx_strcpy_alloc(&sql, &sql_alloc, &sql_offset, "null,"); continue; diff --git a/src/libs/zbxjson/json.c b/src/libs/zbxjson/json.c index 8fc367e6619..b07df7c6189 100644 --- a/src/libs/zbxjson/json.c +++ b/src/libs/zbxjson/json.c @@ -492,6 +492,10 @@ static zbx_json_type_t __zbx_json_type(const char *p) return ZBX_JSON_TYPE_OBJECT; if ('n' == p[0] && 'u' == p[1] && 'l' == p[2] && 'l' == p[3]) return ZBX_JSON_TYPE_NULL; + if ('t' == p[0] && 'r' == p[1] && 'u' == p[2] && 'e' == p[3]) + return ZBX_JSON_TYPE_TRUE; + if ('f' == p[0] && 'a' == p[1] && 'l' == p[2] && 's' == p[3] && 'e' == p[4]) + return ZBX_JSON_TYPE_FALSE; zbx_set_json_strerror("invalid type of JSON value \"%.64s\"", p); @@ -671,14 +675,6 @@ const char *zbx_json_next(const struct zbx_json_parse *jp, const char *p) return NULL; } -static const char *zbx_json_decodenull(const char *p) -{ - if ('n' == p[0] && 'u' == p[1] && 'l' == p[2] && 'l' == p[3]) - return p + 4; - - return NULL; -} - /****************************************************************************** * * * Function: zbx_is_valid_json_hex * @@ -871,6 +867,9 @@ static const char *zbx_json_copy_string(const char *p, char *out, size_t size) { char *start = out; + if (0 == size) + return NULL; + p++; while ('\0' != *p) @@ -910,7 +909,7 @@ static const char *zbx_json_copy_string(const char *p, char *out, size_t size) * * * Function: zbx_json_copy_value * * * - * Purpose: copies json value * + * Purpose: copies unquoted (numeric, boolean) json value * * * * Parameters: p - [IN] a pointer to the next character in string * * len - [IN] the value length * @@ -920,83 +919,79 @@ static const char *zbx_json_copy_string(const char *p, char *out, size_t size) * Return value: A pointer to the next character in input string or NULL if * * string copying failed. * * * - * Comments: String values are converted (leading/trailing " dropped and * - * escape sequences translated) while other values are simply * - * copied. * - * * ******************************************************************************/ -static const char *zbx_json_copy_value(const char *p, size_t len, char *out, size_t size) +static const char *zbx_json_copy_unquoted_value(const char *p, size_t len, char *out, size_t size) { - if (ZBX_JSON_TYPE_STRING == __zbx_json_type(p)) - { - if (NULL == zbx_json_copy_string(p, out, size)) - return NULL; - } - else - zbx_strlcpy(out, p, MIN(size, len + 1)); + if (size < len + 1) + return NULL; + + memcpy(out, p, len); + out[len] = '\0'; return p + len; } -const char *zbx_json_decodevalue(const char *p, char *string, size_t size, int *is_null) +const char *zbx_json_decodevalue(const char *p, char *string, size_t size, zbx_json_type_t *type) { - size_t len; + size_t len; + zbx_json_type_t type_local; - switch (__zbx_json_type(p)) - { - case ZBX_JSON_TYPE_STRING: - case ZBX_JSON_TYPE_INT: - if (NULL != is_null) - *is_null = 0; + if (0 == (len = json_parse_value(p, NULL))) + return NULL; - if (0 == (len = json_parse_value(p, NULL))) - return NULL; + type_local = __zbx_json_type(p); + + if (NULL != type) + *type = type_local; - return zbx_json_copy_value(p, len, string, size); + switch (type_local) + { + case ZBX_JSON_TYPE_STRING: + return zbx_json_copy_string(p, string, size); case ZBX_JSON_TYPE_NULL: - if (NULL != is_null) - *is_null = 1; + if (0 == size) + return NULL; *string = '\0'; - return zbx_json_decodenull(p); + return p + len; + case ZBX_JSON_TYPE_TRUE: + case ZBX_JSON_TYPE_INT: + case ZBX_JSON_TYPE_FALSE: + return zbx_json_copy_unquoted_value(p, len, string, size); default: return NULL; } } -static const char *zbx_json_decodevalue_dyn(const char *p, char **string, size_t *string_alloc, int *is_null) +const char *zbx_json_decodevalue_dyn(const char *p, char **string, size_t *string_alloc, zbx_json_type_t *type) { - size_t len; + size_t len; + zbx_json_type_t type_local; + + if (0 == (len = json_parse_value(p, NULL))) + return NULL; - switch (__zbx_json_type(p)) + if (*string_alloc <= len) { - case ZBX_JSON_TYPE_STRING: - case ZBX_JSON_TYPE_INT: - if (NULL != is_null) - *is_null = 0; + *string_alloc = len + 1; + *string = (char *)zbx_realloc(*string, *string_alloc); + } - if (0 == (len = json_parse_value(p, NULL))) - return NULL; + type_local = __zbx_json_type(p); - if (*string_alloc <= len) - { - *string_alloc = len + 1; - *string = (char *)zbx_realloc(*string, *string_alloc); - } + if (NULL != type) + *type = type_local; - return zbx_json_copy_value(p, len, *string, *string_alloc); + switch (type_local) + { + case ZBX_JSON_TYPE_STRING: + return zbx_json_copy_string(p, *string, *string_alloc); case ZBX_JSON_TYPE_NULL: - if (NULL != is_null) - *is_null = 1; - - if (*string_alloc < 1) - { - *string_alloc = 1; - *string = (char *)zbx_realloc(*string, *string_alloc); - } - **string = '\0'; - - return zbx_json_decodenull(p); + return p + len; + case ZBX_JSON_TYPE_INT: + case ZBX_JSON_TYPE_TRUE: + case ZBX_JSON_TYPE_FALSE: + return zbx_json_copy_unquoted_value(p, len, *string, *string_alloc); default: return NULL; } @@ -1057,12 +1052,13 @@ const char *zbx_json_pair_by_name(const struct zbx_json_parse *jp, const char *n * Author: Alexander Vladishev * * * ******************************************************************************/ -const char *zbx_json_next_value(const struct zbx_json_parse *jp, const char *p, char *string, size_t len, int *is_null) +const char *zbx_json_next_value(const struct zbx_json_parse *jp, const char *p, char *string, size_t len, + zbx_json_type_t *type) { if (NULL == (p = zbx_json_next(jp, p))) return NULL; - return zbx_json_decodevalue(p, string, len, is_null); + return zbx_json_decodevalue(p, string, len, type); } /****************************************************************************** @@ -1071,12 +1067,12 @@ const char *zbx_json_next_value(const struct zbx_json_parse *jp, const char *p, * * ******************************************************************************/ const char *zbx_json_next_value_dyn(const struct zbx_json_parse *jp, const char *p, char **string, - size_t *string_alloc, int *is_null) + size_t *string_alloc, zbx_json_type_t *type) { if (NULL == (p = zbx_json_next(jp, p))) return NULL; - return zbx_json_decodevalue_dyn(p, string, string_alloc, is_null); + return zbx_json_decodevalue_dyn(p, string, string_alloc, type); } /****************************************************************************** diff --git a/tests/libs/zbxjson/Makefile.am b/tests/libs/zbxjson/Makefile.am index 2fc552ad79d..e1d549a2e5b 100644 --- a/tests/libs/zbxjson/Makefile.am +++ b/tests/libs/zbxjson/Makefile.am @@ -1,4 +1,8 @@ -noinst_PROGRAMS = jsonpath_next zbx_json_path_open +noinst_PROGRAMS = \ + jsonpath_next \ + zbx_json_path_open \ + zbx_json_decodevalue \ + zbx_json_decodevalue_dyn JSON_LIBS = \ $(top_srcdir)/tests/libzbxmocktest.a \ @@ -17,6 +21,8 @@ JSON_LIBS = \ $(top_srcdir)/src/libs/zbxconf/libzbxconf.a \ $(top_srcdir)/tests/libzbxmockdata.a +# jsonpath_next + jsonpath_next_SOURCES = \ jsonpath_next.c \ ../../zbxmocktest.h @@ -30,6 +36,8 @@ endif jsonpath_next_CFLAGS = -I@top_srcdir@/tests +# zbx_json_path_open + zbx_json_path_open_SOURCES = \ zbx_json_path_open.c \ ../../zbxmocktest.h @@ -42,3 +50,36 @@ zbx_json_path_open_LDFLAGS = @SERVER_LDFLAGS@ endif zbx_json_path_open_CFLAGS = -I@top_srcdir@/tests + +# zbx_json_decodevalue + +zbx_json_decodevalue_SOURCES = \ + zbx_json_decodevalue.c \ + mock_json.c mock_json.h \ + ../../zbxmocktest.h + +zbx_json_decodevalue_LDADD = $(JSON_LIBS) + +if SERVER +zbx_json_decodevalue_LDADD += @SERVER_LIBS@ +zbx_json_decodevalue_LDFLAGS = @SERVER_LDFLAGS@ +endif + +zbx_json_decodevalue_CFLAGS = -I@top_srcdir@/tests + +# zbx_json_decodevalue_dyn + +zbx_json_decodevalue_dyn_SOURCES = \ + zbx_json_decodevalue_dyn.c \ + mock_json.c mock_json.h \ + ../../zbxmocktest.h + +zbx_json_decodevalue_dyn_LDADD = $(JSON_LIBS) + +if SERVER +zbx_json_decodevalue_dyn_LDADD += @SERVER_LIBS@ +zbx_json_decodevalue_dyn_LDFLAGS = @SERVER_LDFLAGS@ +endif + +zbx_json_decodevalue_dyn_CFLAGS = -I@top_srcdir@/tests + diff --git a/tests/libs/zbxjson/mock_json.c b/tests/libs/zbxjson/mock_json.c new file mode 100644 index 00000000000..56172b0b3ac --- /dev/null +++ b/tests/libs/zbxjson/mock_json.c @@ -0,0 +1,38 @@ +/* +** 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 "zbxmocktest.h" +#include "zbxmockdata.h" +#include "zbxmockassert.h" + +#include "common.h" +#include "zbxjson.h" + +const char *zbx_mock_json_type_to_str(int type) +{ + static const char *json_types[] = { + "ZBX_JSON_TYPE_UNKNOWN", "ZBX_JSON_TYPE_STRING", "ZBX_JSON_TYPE_INT", + "ZBX_JSON_TYPE_ARRAY", "ZBX_JSON_TYPE_OBJECT", "ZBX_JSON_TYPE_NULL", + "ZBX_JSON_TYPE_TRUE", "ZBX_JSON_TYPE_FALSE"}; + + if (0 > type || ZBX_JSON_TYPE_FALSE < type) + fail_msg("Unknown json type: %d", type); + + return json_types[type]; +} diff --git a/tests/libs/zbxjson/mock_json.h b/tests/libs/zbxjson/mock_json.h new file mode 100644 index 00000000000..7926bef73c9 --- /dev/null +++ b/tests/libs/zbxjson/mock_json.h @@ -0,0 +1,25 @@ +/* +** 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 ZABBIX_MOCK_JSON_H +#define ZABBIX_MOCK_JSON_H + +const char *zbx_mock_json_type_to_str(int type); + +#endif diff --git a/tests/libs/zbxjson/zbx_json_decodevalue.c b/tests/libs/zbxjson/zbx_json_decodevalue.c new file mode 100644 index 00000000000..85f9c807301 --- /dev/null +++ b/tests/libs/zbxjson/zbx_json_decodevalue.c @@ -0,0 +1,63 @@ +/* +** 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 "common.h" +#include "zbxjson.h" + +#include "zbxmocktest.h" +#include "zbxmockdata.h" +#include "zbxmockassert.h" +#include "zbxmockutil.h" + +#include "mock_json.h" + +void zbx_mock_test_entry(void **state) +{ + const char *data, *result; + size_t size; + char *buffer; + int expected_offset; + zbx_json_type_t returned_type; + + ZBX_UNUSED(state); + + data = zbx_mock_get_parameter_string("in.data"); + size = zbx_mock_get_parameter_uint64("in.size"); + buffer = (0 != size ? (char *)zbx_malloc(NULL, size) : NULL); + + result = zbx_json_decodevalue(data, buffer, size, &returned_type); + + expected_offset = atoi(zbx_mock_get_parameter_string("out.offset")); + + if (0 == expected_offset) + zbx_mock_assert_ptr_eq("Returned value", NULL, result); + else + zbx_mock_assert_ptr_ne("Returned value", NULL, result); + + if (NULL != result) + { + zbx_mock_assert_int_eq("Returned value offset", expected_offset, result - data); + zbx_mock_assert_str_eq("Returned json type", zbx_mock_get_parameter_string("out.type"), + zbx_mock_json_type_to_str(returned_type)); + zbx_mock_assert_str_eq("Returned value", buffer, zbx_mock_get_parameter_string("out.value")); + } + + zbx_free(buffer); +} + diff --git a/tests/libs/zbxjson/zbx_json_decodevalue.yaml b/tests/libs/zbxjson/zbx_json_decodevalue.yaml new file mode 100644 index 00000000000..0878b95bcae --- /dev/null +++ b/tests/libs/zbxjson/zbx_json_decodevalue.yaml @@ -0,0 +1,357 @@ +--- +test case: Decode fail '' +in: + data: '' + size: 1024 +out: + offset: 0 +--- +test case: Decode fail 'a b' +in: + data: 'a b' + size: 1024 +out: + offset: 0 +--- +test case: Decode fail ' 1' +in: + data: ' 1' + size: 1024 +out: + offset: 0 +--- +test case: Decode fail '+1' +in: + data: '+1' + size: 1024 +out: + offset: 0 +--- +test case: Decode fail '--1' +in: + data: '--1' + size: 1024 +out: + offset: 0 +--- +test case: Decode fail '.5' +in: + data: '.5' + size: 1024 +out: + offset: 0 +--- +test case: Decode fail '01' +in: + data: '01' + size: 1024 +out: + offset: 0 +--- +test case: Decode fail '"a\X"' +in: + data: '"a\X"' + size: 1024 +out: + offset: 0 +--- +test case: Decode fail '"a\' +in: + data: '"a\' + size: 1024 +out: + offset: 0 +--- +test case: Decode fail '"a' +in: + data: '"a' + size: 1024 +out: + offset: 0 +--- +test case: Decode fail '"' +in: + data: '"' + size: 1024 +out: + offset: 0 +--- +test case: Decode fail '[1, 2]' +in: + data: '[1, 2]' + size: 1024 +out: + offset: 0 +--- +test case: Decode fail '{"a":1}' +in: + data: '{"a":1}' + size: 1024 +out: + offset: 0 +--- +test case: Decode fail 'NULL' +in: + data: 'NULL' + size: 1024 +out: + offset: 0 +--- +test case: Decode fail 'TRUE' +in: + data: 'TRUE' + size: 1024 +out: + offset: 0 +--- +test case: Decode fail 'FALSE' +in: + data: 'FALSE' + size: 1024 +out: + offset: 0 +--- +test case: Decode fail '"longer value"' with small output buffer +in: + data: '"longer value"' + size: 10 +out: + offset: 0 +--- +test case: Decode fail '12345678901234567890' with small output buffer +in: + data: '12345678901234567890' + size: 10 +out: + offset: 0 +--- +test case: Decode fail 'true' with small output buffer +in: + data: 'true' + size: 2 +out: + offset: 0 +--- +test case: Decode fail 'false' with small output buffer +in: + data: 'false' + size: 2 +out: + offset: 0 +--- +test case: Decode fail 'null' with small output buffer +in: + data: 'null' + size: 0 +out: + offset: 0 +--- +test case: Decode fail '"text"' with 0 output buffer +in: + data: '"text"' + size: 0 +out: + offset: 0 +--- +test case: Decode fail 'true' with 0 output buffer +in: + data: 'true' + size: 0 +out: + offset: 0 +--- +test case: Decode fail 'false' with 0 output buffer +in: + data: 'false' + size: 0 +out: + offset: 0 +--- +test case: Decode fail 'null' with 0 output buffer +in: + data: 'null' + size: 0 +out: + offset: 0 +--- +test case: Decode fail '12345' with 0 output buffer +in: + data: '12345' + size: 0 +out: + offset: 0 +--- +test case: Decode success '1' +in: + data: '1' + size: 1024 +out: + offset: 1 + type: ZBX_JSON_TYPE_INT + value: '1' +--- +test case: Decode success '1.5' +in: + data: '1.5' + size: 1024 +out: + offset: 3 + type: ZBX_JSON_TYPE_INT + value: '1.5' +--- +test case: Decode success '-1' +in: + data: '-1' + size: 1024 +out: + offset: 2 + type: ZBX_JSON_TYPE_INT + value: '-1' +--- +test case: Decode success '0' +in: + data: '0' + size: 1024 +out: + offset: 1 + type: ZBX_JSON_TYPE_INT + value: '0' +--- +test case: Decode success '1e5' +in: + data: '1e5' + size: 1024 +out: + offset: 3 + type: ZBX_JSON_TYPE_INT + value: '1e5' +--- +test case: Decode success '1e-2' +in: + data: '1e-2' + size: 1024 +out: + offset: 4 + type: ZBX_JSON_TYPE_INT + value: '1e-2' +--- +test case: Decode success '-0.5E10' +in: + data: '-0.5E10' + size: 1024 +out: + offset: 7 + type: ZBX_JSON_TYPE_INT + value: '-0.5E10' +--- +test case: Decode success '-0.5E+10' +in: + data: '-0.5E+10' + size: 1024 +out: + offset: 8 + type: ZBX_JSON_TYPE_INT + value: '-0.5E+10' +--- +test case: Decode success '1e03' +in: + data: '1e03' + size: 1024 +out: + offset: 4 + type: ZBX_JSON_TYPE_INT + value: '1e03' +--- +test case: Decode success 'true' +in: + data: 'true' + size: 5 +out: + offset: 4 + type: ZBX_JSON_TYPE_TRUE + value: 'true' +--- +test case: Decode fail 'true' with output buffer 1 byte less than needed +in: + data: 'true' + size: 4 +out: + offset: 0 +--- +test case: Decode success 'false' +in: + data: 'false' + size: 6 +out: + offset: 5 + type: ZBX_JSON_TYPE_FALSE + value: 'false' +--- +test case: Decode fail 'false' with output buffer 1 byte less than needed +in: + data: 'false' + size: 5 +out: + offset: 0 +--- +test case: Decode success 'null' +in: + data: 'null' + size: 1 +out: + offset: 4 + type: ZBX_JSON_TYPE_NULL + value: '' +--- +test case: Decode success '"a"' +in: + data: '"a"' + size: 1024 +out: + offset: 3 + type: ZBX_JSON_TYPE_STRING + value: 'a' +--- +test case: Decode success '"\\/"' +in: + data: '"\\/"' + size: 1024 +out: + offset: 5 + type: ZBX_JSON_TYPE_STRING + value: '\/' +--- +test case: Decode success '"value \"1\""' +in: + data: '"value \"1\""' + size: 1024 +out: + offset: 13 + type: ZBX_JSON_TYPE_STRING + value: 'value "1"' +--- +test case: Decode success '"value \"1\""'with output buffer having the required size +in: + data: '"value \"1\""' + size: 10 +out: + offset: 13 + type: ZBX_JSON_TYPE_STRING + value: 'value "1"' +--- +test case: Decode fail '"value \"1\""' with output buffer 1 byte less than needed +in: + data: '"value \"1\""' + size: 9 +out: + offset: 0 +--- +test case: Decode success '"\u0420\u0435\u0433\u0438\u043e\u043d"' +in: + data: '"\u0420\u0435\u0433\u0438\u043e\u043d"' + size: 1024 +out: + offset: 38 + type: ZBX_JSON_TYPE_STRING + value: 'Регион' +... diff --git a/tests/libs/zbxjson/zbx_json_decodevalue_dyn.c b/tests/libs/zbxjson/zbx_json_decodevalue_dyn.c new file mode 100644 index 00000000000..978e457be92 --- /dev/null +++ b/tests/libs/zbxjson/zbx_json_decodevalue_dyn.c @@ -0,0 +1,63 @@ +/* +** 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 "common.h" +#include "zbxjson.h" + +#include "zbxmocktest.h" +#include "zbxmockdata.h" +#include "zbxmockassert.h" +#include "zbxmockutil.h" + +#include "mock_json.h" + +void zbx_mock_test_entry(void **state) +{ + const char *data, *result; + size_t size; + char *buffer; + int expected_offset; + zbx_json_type_t returned_type; + + ZBX_UNUSED(state); + + data = zbx_mock_get_parameter_string("in.data"); + size = zbx_mock_get_parameter_uint64("in.size"); + buffer = (0 != size ? (char *)zbx_malloc(NULL, size) : NULL); + + result = zbx_json_decodevalue_dyn(data, &buffer, &size, &returned_type); + + expected_offset = atoi(zbx_mock_get_parameter_string("out.offset")); + + if (0 == expected_offset) + zbx_mock_assert_ptr_eq("Returned value", NULL, result); + else + zbx_mock_assert_ptr_ne("Returned value", NULL, result); + + if (NULL != result) + { + zbx_mock_assert_int_eq("Returned value offset", expected_offset, result - data); + zbx_mock_assert_str_eq("Returned json type", zbx_mock_get_parameter_string("out.type"), + zbx_mock_json_type_to_str(returned_type)); + zbx_mock_assert_str_eq("Returned value", buffer, zbx_mock_get_parameter_string("out.value")); + } + + zbx_free(buffer); +} + diff --git a/tests/libs/zbxjson/zbx_json_decodevalue_dyn.yaml b/tests/libs/zbxjson/zbx_json_decodevalue_dyn.yaml new file mode 100644 index 00000000000..7bded75b515 --- /dev/null +++ b/tests/libs/zbxjson/zbx_json_decodevalue_dyn.yaml @@ -0,0 +1,383 @@ +--- +test case: Decode fail '' +in: + data: '' + size: 1024 +out: + offset: 0 +--- +test case: Decode fail 'a b' +in: + data: 'a b' + size: 1024 +out: + offset: 0 +--- +test case: Decode fail ' 1' +in: + data: ' 1' + size: 1024 +out: + offset: 0 +--- +test case: Decode fail '+1' +in: + data: '+1' + size: 1024 +out: + offset: 0 +--- +test case: Decode fail '--1' +in: + data: '--1' + size: 1024 +out: + offset: 0 +--- +test case: Decode fail '.5' +in: + data: '.5' + size: 1024 +out: + offset: 0 +--- +test case: Decode fail '01' +in: + data: '01' + size: 1024 +out: + offset: 0 +--- +test case: Decode fail '"a\X"' +in: + data: '"a\X"' + size: 1024 +out: + offset: 0 +--- +test case: Decode fail '"a\' +in: + data: '"a\' + size: 1024 +out: + offset: 0 +--- +test case: Decode fail '"a' +in: + data: '"a' + size: 1024 +out: + offset: 0 +--- +test case: Decode fail '"' +in: + data: '"' + size: 1024 +out: + offset: 0 +--- +test case: Decode fail '[1, 2]' +in: + data: '[1, 2]' + size: 1024 +out: + offset: 0 +--- +test case: Decode fail '{"a":1}' +in: + data: '{"a":1}' + size: 1024 +out: + offset: 0 +--- +test case: Decode fail 'NULL' +in: + data: 'NULL' + size: 1024 +out: + offset: 0 +--- +test case: Decode fail 'TRUE' +in: + data: 'TRUE' + size: 1024 +out: + offset: 0 +--- +test case: Decode fail 'FALSE' +in: + data: 'FALSE' + size: 1024 +out: + offset: 0 +--- +test case: Decode success '"longer value"' with small output buffer +in: + data: '"longer value"' + size: 10 +out: + offset: 14 + type: ZBX_JSON_TYPE_STRING + value: longer value +--- +test case: Decode success '12345678901234567890' with small output buffer +in: + data: '12345678901234567890' + size: 10 +out: + offset: 20 + type: ZBX_JSON_TYPE_INT + value: 12345678901234567890 +--- +test case: Decode fail 'true' with small output buffer +in: + data: 'true' + size: 2 +out: + offset: 4 + type: ZBX_JSON_TYPE_TRUE + value: true +--- +test case: Decode fail 'false' with small output buffer +in: + data: 'false' + size: 2 +out: + offset: 5 + type: ZBX_JSON_TYPE_FALSE + value: false +--- +test case: Decode fail 'null' with small output buffer +in: + data: 'null' + size: 0 +out: + offset: 4 + type: ZBX_JSON_TYPE_NULL + value: '' +--- +test case: Decode fail '"text"' with 0 output buffer +in: + data: '"text"' + size: 0 +out: + offset: 6 + type: ZBX_JSON_TYPE_STRING + value: text +--- +test case: Decode fail 'true' with 0 output buffer +in: + data: 'true' + size: 0 +out: + offset: 4 + type: ZBX_JSON_TYPE_TRUE + value: true +--- +test case: Decode fail 'false' with 0 output buffer +in: + data: 'false' + size: 0 +out: + offset: 5 + type: ZBX_JSON_TYPE_FALSE + value: false +--- +test case: Decode fail 'null' with 0 output buffer +in: + data: 'null' + size: 0 +out: + offset: 4 + type: ZBX_JSON_TYPE_NULL + value: '' +--- +test case: Decode fail '12345' with 0 output buffer +in: + data: '12345' + size: 0 +out: + offset: 5 + type: ZBX_JSON_TYPE_INT + value: 12345 +--- +test case: Decode success '1' +in: + data: '1' + size: 1024 +out: + offset: 1 + type: ZBX_JSON_TYPE_INT + value: '1' +--- +test case: Decode success '1.5' +in: + data: '1.5' + size: 1024 +out: + offset: 3 + type: ZBX_JSON_TYPE_INT + value: '1.5' +--- +test case: Decode success '-1' +in: + data: '-1' + size: 1024 +out: + offset: 2 + type: ZBX_JSON_TYPE_INT + value: '-1' +--- +test case: Decode success '0' +in: + data: '0' + size: 1024 +out: + offset: 1 + type: ZBX_JSON_TYPE_INT + value: '0' +--- +test case: Decode success '1e5' +in: + data: '1e5' + size: 1024 +out: + offset: 3 + type: ZBX_JSON_TYPE_INT + value: '1e5' +--- +test case: Decode success '1e-2' +in: + data: '1e-2' + size: 1024 +out: + offset: 4 + type: ZBX_JSON_TYPE_INT + value: '1e-2' +--- +test case: Decode success '-0.5E10' +in: + data: '-0.5E10' + size: 1024 +out: + offset: 7 + type: ZBX_JSON_TYPE_INT + value: '-0.5E10' +--- +test case: Decode success '-0.5E+10' +in: + data: '-0.5E+10' + size: 1024 +out: + offset: 8 + type: ZBX_JSON_TYPE_INT + value: '-0.5E+10' +--- +test case: Decode success '1e03' +in: + data: '1e03' + size: 1024 +out: + offset: 4 + type: ZBX_JSON_TYPE_INT + value: '1e03' +--- +test case: Decode success 'true' +in: + data: 'true' + size: 5 +out: + offset: 4 + type: ZBX_JSON_TYPE_TRUE + value: 'true' +--- +test case: Decode fail 'true' with output buffer 1 byte less than needed +in: + data: 'true' + size: 4 +out: + offset: 4 + type: ZBX_JSON_TYPE_TRUE + value: 'true' +--- +test case: Decode success 'false' +in: + data: 'false' + size: 6 +out: + offset: 5 + type: ZBX_JSON_TYPE_FALSE + value: 'false' +--- +test case: Decode fail 'false' with output buffer 1 byte less than needed +in: + data: 'false' + size: 5 +out: + offset: 5 + type: ZBX_JSON_TYPE_FALSE + value: 'false' +--- +test case: Decode success 'null' +in: + data: 'null' + size: 1 +out: + offset: 4 + type: ZBX_JSON_TYPE_NULL + value: '' +--- +test case: Decode success '"a"' +in: + data: '"a"' + size: 1024 +out: + offset: 3 + type: ZBX_JSON_TYPE_STRING + value: 'a' +--- +test case: Decode success '"\\/"' +in: + data: '"\\/"' + size: 1024 +out: + offset: 5 + type: ZBX_JSON_TYPE_STRING + value: '\/' +--- +test case: Decode success '"value \"1\""' +in: + data: '"value \"1\""' + size: 1024 +out: + offset: 13 + type: ZBX_JSON_TYPE_STRING + value: 'value "1"' +--- +test case: Decode success '"value \"1\""'with output buffer having the required size +in: + data: '"value \"1\""' + size: 10 +out: + offset: 13 + type: ZBX_JSON_TYPE_STRING + value: 'value "1"' +--- +test case: Decode fail '"value \"1\""' with output buffer 1 byte less than needed +in: + data: '"value \"1\""' + size: 9 +out: + offset: 13 + type: ZBX_JSON_TYPE_STRING + value: 'value "1"' +--- +test case: Decode success '"\u0420\u0435\u0433\u0438\u043e\u043d"' +in: + data: '"\u0420\u0435\u0433\u0438\u043e\u043d"' + size: 1024 +out: + offset: 38 + type: ZBX_JSON_TYPE_STRING + value: 'Регион' +... |