1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
/*
** Zabbix
** Copyright (C) 2001-2018 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 "sysinfo.h"
#include "../../../../src/libs/zbxsysinfo/common/system.h"
#define LONG_DATETIME_LENGTH 30 /* Length of datetime like "2017-12-18,14:06:09.123,+02:30" */
#define SHORT_DATETIME_LENGTH 19 /* Length of datetime like "2017-12-18,14:06:09" */
static zbx_timespec_t timespec;
static void zbx_mock_time(void)
{
static int time_parsed = 0;
zbx_mock_error_t error;
zbx_mock_handle_t param_handle;
const char *timestamp;
struct tm tm;
int ms, hour, min;
char sign, tmp[16];
size_t length;
if (0 != time_parsed)
return; /* timestamp param was already parsed */
memset(&tm, 0, sizeof(tm));
memset(×pec, 0, sizeof(timespec));
if (ZBX_MOCK_SUCCESS != (error = zbx_mock_in_parameter("timestamp", ¶m_handle)) ||
ZBX_MOCK_SUCCESS != (error = zbx_mock_string(param_handle, ×tamp)))
{
fail_msg("Cannot get expected 'timestamp' parameter from test case data: %s",
zbx_mock_error_string(error));
}
length = strlen(timestamp);
if (SHORT_DATETIME_LENGTH == length || LONG_DATETIME_LENGTH == length)
{
if (6 != sscanf(timestamp, "%04d-%02d-%02d,%02d:%02d:%02d", &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
&tm.tm_hour, &tm.tm_min, &tm.tm_sec) || 1900 > tm.tm_year || 1 > tm.tm_mon ||
12 < tm.tm_mon || 1 > tm.tm_mday || 31 < tm.tm_mday || 0 > tm.tm_hour ||
23 < tm.tm_hour || 0 > tm.tm_min || 59 < tm.tm_min || 0 > tm.tm_sec || 59 < tm.tm_sec)
{
fail_msg("Cannot parse date and time part of 'timestamp' parameter: %s", timestamp);
}
tm.tm_year -= 1900;
tm.tm_mon--;
if (LONG_DATETIME_LENGTH == length)
{
if (4 != sscanf(timestamp + SHORT_DATETIME_LENGTH, ".%d,%c%d:%d", &ms, &sign, &hour, &min) ||
0 > ms || 1000 <= ms || ('-' != sign && '+' != sign) || 0 > hour || 23 < hour ||
0 > min || 59 < min)
{
fail_msg("Cannot parse ms and timezone part of 'timestamp' parameter: %s", timestamp);
}
timespec.ns = ms * 1000000;
zbx_snprintf(tmp, sizeof(tmp), "ZBX%c%02d:%02d", (sign == '+' ? '-' : '+'), hour, min);
if (0 != setenv("TZ", tmp, 1))
fail_msg("Cannot set timezone value: %s", tmp);
tzset();
}
timespec.sec = mktime(&tm);
}
else
{
/* Fallback to numeric timestamp format */
if (FAIL == is_uint32(timestamp, ×pec.sec))
fail_msg("Cannot convert 'timestamp' parameter value to numeric: %s", timestamp);
}
time_parsed = 1;
}
void zbx_mock_test_entry(void **state)
{
AGENT_REQUEST request;
AGENT_RESULT param_result;
zbx_mock_error_t error;
zbx_mock_handle_t param_handle;
const char *expected_value_string, *expected_return_string, *key_string;
char *value = NULL;
int expected_result = SYSINFO_RET_FAIL, actual_result;
ZBX_UNUSED(state);
if (ZBX_MOCK_SUCCESS != (error = zbx_mock_out_parameter("return", ¶m_handle)) ||
ZBX_MOCK_SUCCESS != (error = zbx_mock_string(param_handle, &expected_return_string)))
{
fail_msg("Cannot get expected 'return' parameter from test case data: %s",
zbx_mock_error_string(error));
}
if (0 == strcmp("SYSINFO_RET_OK", expected_return_string))
expected_result = SYSINFO_RET_OK;
else if (0 == strcmp("SYSINFO_RET_FAIL", expected_return_string))
expected_result = SYSINFO_RET_FAIL;
else
fail_msg("Got unexpected 'return' parameter from test case data: %s", expected_return_string);
if (ZBX_MOCK_SUCCESS != (error = zbx_mock_out_parameter("result", ¶m_handle)) ||
ZBX_MOCK_SUCCESS != (error = zbx_mock_string(param_handle, &expected_value_string)))
{
fail_msg("Cannot get expected 'result' parameter from test case data: %s",
zbx_mock_error_string(error));
}
if (ZBX_MOCK_SUCCESS != (error = zbx_mock_in_parameter("key", ¶m_handle)) ||
ZBX_MOCK_SUCCESS != zbx_mock_string(param_handle, &key_string))
{
fail_msg("Cannot get expected 'key' parameter from test case data: %s",
zbx_mock_error_string(error));
}
init_request(&request);
init_result(¶m_result);
if (SUCCEED != parse_item_key(key_string, &request))
fail_msg("Cannot parse item key from string '%s'", key_string);
if (0 != strcmp(request.key, "system.localtime"))
fail_msg("Got unexpected item key parameter from test case data: %s", key_string);
if (expected_result != (actual_result = SYSTEM_LOCALTIME(&request, ¶m_result)))
{
fail_msg("Got '%s' instead of '%s' as a result.", zbx_sysinfo_ret_string(actual_result),
zbx_sysinfo_ret_string(expected_result));
}
if (SYSINFO_RET_OK == expected_result)
value = (NULL != GET_TEXT_RESULT(¶m_result)) ? *GET_TEXT_RESULT(¶m_result) : NULL;
else
value = (NULL != GET_MSG_RESULT(¶m_result)) ? *GET_MSG_RESULT(¶m_result) : NULL;
if (NULL == value || 0 != strcmp(expected_value_string, value))
{
fail_msg("Got '%s' instead of '%s' as a value.", (NULL != value ? value : "NULL"),
expected_value_string);
}
free_request(&request);
free_result(¶m_result);
}
time_t __wrap_time(time_t *seconds)
{
zbx_mock_time();
if (NULL != seconds)
*seconds = timespec.sec;
return timespec.sec;
}
int __wrap_gettimeofday(struct timeval *__restrict tv, __timezone_ptr_t tz)
{
if (NULL != tv)
{
zbx_mock_time();
tv->tv_sec = timespec.sec;
tv->tv_usec = timespec.ns / 1000;
}
if (NULL != tz)
fail_msg("Timezone param in gettimeofday() call is not set to null");
return 0;
}
|