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

web_page_get.c « common « zbxsysinfo « libs « tests - github.com/zabbix/zabbix.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 22811829dbd803d07b6791a8df95a7371fd841f8 (plain)
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
195
196
197
198
199
200
/*
** Zabbix
** Copyright (C) 2001-2022 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 "zbxmockutil.h"

#include "zbxcomms.h"
#include "zbxsysinfo.h"
#include "../../../../src/libs/zbxsysinfo/sysinfo.h"
#include "../../../../src/libs/zbxsysinfo/common/http.h"

#ifndef HAVE_LIBCURL

#define STR_TEST_TYPE	"legacy"
#define STR_FIELD_OUT	"out.req"

static const char	*http_req;
static size_t		http_len;

int	__wrap_zbx_tcp_connect(zbx_socket_t *s, const char *source_ip, const char *ip, unsigned short port,
		int timeout, unsigned int tls_connect, const char *tls_arg1, const char *tls_arg2);
int	__wrap_zbx_tcp_send_ext(zbx_socket_t *s, const char *data, size_t len, unsigned char flags, int timeout);
ssize_t	__wrap_zbx_tcp_recv_raw_ext(zbx_socket_t *s, int timeout);
void	__wrap_zbx_tcp_close(zbx_socket_t *s);

#else

#define STR_TEST_TYPE	"libcurl"
#define STR_FIELD_OUT	"out.url"

static void	*page_data = NULL;
static size_t	(*cb_ptr)(void *ptr, size_t size, size_t nmemb, void *userdata);
static char	*req_url = NULL;
static int	dummy;

CURL		*__wrap_curl_easy_init(void);
CURLcode	__wrap_curl_easy_setopt(CURL *easyhandle, int opt, void *val);
CURLcode	__wrap_curl_easy_perform(CURL *easyhandle);
void		__wrap_curl_easy_cleanup(CURL *easyhandle);

#endif

void	zbx_mock_test_entry(void **state)
{
	AGENT_REQUEST	request;
	AGENT_RESULT 	param_result;
	int		expected_result, actual_result;
	const char	*buffer, *init_param, *test_type;
	char		*rvalue;

	ZBX_UNUSED(state);

	test_type = zbx_mock_get_parameter_string("in.test_type");

	if (0 != strcmp(test_type, "both") && 0 != strcmp(test_type, STR_TEST_TYPE))
		skip();

	expected_result = zbx_mock_str_to_return_code(zbx_mock_get_parameter_string("out.return"));

	zbx_init_agent_request(&request);
	zbx_init_agent_result(&param_result);
	init_param = zbx_mock_get_parameter_string("in.key");

	if (SUCCEED != zbx_parse_item_key(init_param, &request))
		fail_msg("Cannot parse item key: %s", init_param);

	if (expected_result != (actual_result = web_page_get(&request, &param_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_FAIL == expected_result)
	{
		buffer = zbx_mock_get_parameter_string("out.error");
		rvalue = (NULL != ZBX_GET_MSG_RESULT(&param_result)) ? *ZBX_GET_MSG_RESULT(&param_result) : NULL;
	}
	else
	{
		buffer = zbx_mock_get_parameter_string(STR_FIELD_OUT);
		rvalue = (NULL != ZBX_GET_TEXT_RESULT(&param_result)) ? *ZBX_GET_TEXT_RESULT(&param_result) : NULL;
	}

	if (NULL == rvalue)
		fail_msg("Got 'NULL' response.");

#ifndef HAVE_LIBCURL
	if (SYSINFO_RET_OK == expected_result)
		rvalue[strlen(rvalue) - 1] = '\0'; /* remove last char from __wrap_zbx_tcp_recv_raw_ext */
#endif

	if (0 != strcmp(buffer, rvalue))
		fail_msg("Got '%s' instead of '%s' as a value.", rvalue, buffer);

	zbx_free_agent_request(&request);
	zbx_free_agent_result(&param_result);
}

#ifdef HAVE_LIBCURL
CURL	*__wrap_curl_easy_init(void)
{
	return (CURL*)&dummy;
}

CURLcode	__wrap_curl_easy_setopt(CURL *easyhandle, int opt, void *val)
{
	ZBX_UNUSED(easyhandle);

	switch (opt)
	{
		case CURLOPT_URL:
			req_url = zbx_strdup(req_url, (char*)val);
			break;
		case CURLOPT_WRITEFUNCTION:
			*(void **)(&cb_ptr) = val;
			break;
		case CURLOPT_WRITEDATA:
			page_data = val;
			break;
	}

	return CURLE_OK;
}

CURLcode	__wrap_curl_easy_perform(CURL *easyhandle)
{
	ZBX_UNUSED(easyhandle);

	cb_ptr(req_url, 1, strlen(req_url), page_data);
	zbx_free(req_url);

	return CURLE_OK;
}

void	__wrap_curl_easy_cleanup(CURL *easyhandle)
{
	ZBX_UNUSED(easyhandle);

	return;
}
#else
int	__wrap_zbx_tcp_connect(zbx_socket_t *s, const char *source_ip, const char *ip, unsigned short port,
		int timeout, unsigned int tls_connect, const char *tls_arg1, const char *tls_arg2)
{
	ZBX_UNUSED(s);
	ZBX_UNUSED(source_ip);
	ZBX_UNUSED(ip);
	ZBX_UNUSED(port);
	ZBX_UNUSED(timeout);
	ZBX_UNUSED(tls_connect);
	ZBX_UNUSED(tls_arg1);
	ZBX_UNUSED(tls_arg2);

	return SUCCEED;
}

int	__wrap_zbx_tcp_send_ext(zbx_socket_t *s, const char *data, size_t len, unsigned char flags, int timeout)
{
	ZBX_UNUSED(s);
	ZBX_UNUSED(flags);
	ZBX_UNUSED(timeout);

	http_req = data;
	http_len = len;
	return SUCCEED;
}

ssize_t	__wrap_zbx_tcp_recv_raw_ext(zbx_socket_t *s, int timeout)
{
	ZBX_UNUSED(s);
	ZBX_UNUSED(timeout);

	s->buffer = (char *)http_req;
	s->buffer[http_len++] = '*'; /* workaround for zbx_rtrim */
	s->buffer[http_len++] = '\0';
	return http_len;
}

void	__wrap_zbx_tcp_close(zbx_socket_t *s)
{
	ZBX_UNUSED(s);
}
#endif