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

zbxmockfile.c « tests - github.com/zabbix/zabbix.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9b25aa5e263de239712358f220a3bded862954e1 (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
/*
** 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.
**/

#define fopen	__real_fopen
#define fclose	__real_fclose
#define fgets	__real_fgets
#include <stdio.h>
#undef fopen
#undef fclose
#undef fgets

#include "zbxmocktest.h"
#include "zbxmockdata.h"

#include "common.h"

#define ZBX_MOCK_MAX_FILES	16

void	*mock_streams[ZBX_MOCK_MAX_FILES];

struct zbx_mock_IO_FILE
{
	const char	*contents;
};

static int	is_profiler_path(const char *path)
{
	size_t	len;

	len = strlen(path);

	if ((ZBX_CONST_STRLEN(".gcda") < len && 0 == strcmp(path + len - ZBX_CONST_STRLEN(".gcda"), ".gcda")) ||
			(ZBX_CONST_STRLEN(".gcno") < len &&
					0 == strcmp(path + len - ZBX_CONST_STRLEN(".gcno"), ".gcno")))
	{
		return SUCCEED;
	}

	return FAIL;
}

static int	is_mock_stream(FILE *stream)
{
	int	i;

	for (i = 0; i < ZBX_MOCK_MAX_FILES && NULL != mock_streams[i]; i++)
	{
		if (stream == mock_streams[i])
			return SUCCEED;
	}

	return FAIL;
}

FILE	*__wrap_fopen(const char *path, const char *mode)
{
	zbx_mock_error_t	error;
	zbx_mock_handle_t	file_contents;
	const char		*contents;
	struct zbx_mock_IO_FILE	*file = NULL;

	if (SUCCEED == is_profiler_path(path))
		return __real_fopen(path, mode);

	if (0 != strcmp(mode, "r"))
	{
		fail_msg("fopen() modes other than \"r\" are not supported.");
	}
	else if (ZBX_MOCK_NO_PARAMETER == (error = zbx_mock_file(path, &file_contents)))
	{
		errno = ENOENT;	/* No such file or directory */
	}
	else if (ZBX_MOCK_SUCCESS != error)
	{
		fail_msg("Error while trying to open path \"%s\" from test case data: %s", path,
				zbx_mock_error_string(error));
	}
	else if (ZBX_MOCK_SUCCESS != (error = zbx_mock_string(file_contents, &contents)))
	{
		fail_msg("Error while trying to get contents of file \"%s\" from test case data: %s", path,
				zbx_mock_error_string(error));
	}
	else
	{
		int	i;

		for (i = 0; i < ZBX_MOCK_MAX_FILES && NULL != mock_streams[i]; i++)
			;

		if (i < ZBX_MOCK_MAX_FILES)
		{
			file = zbx_malloc(file, sizeof(struct zbx_mock_IO_FILE));
			file->contents = contents;
			mock_streams[i] = file;
		}
		else
			errno = EMFILE;	/* The per-process limit on the number of open file descriptors has been reached */
	}

	return (FILE *)file;
}

int	__wrap_fclose(FILE *stream)
{
	if (SUCCEED != is_mock_stream(stream))
		return __real_fclose(stream);

	zbx_free(stream);
	return 0;
}

char	*__wrap_fgets(char *s, int size, FILE *stream)
{
	struct zbx_mock_IO_FILE	*file = (struct zbx_mock_IO_FILE *)stream;
	int			length;
	const char		*newline;

	if (SUCCEED != is_mock_stream(stream))
		return __real_fgets(s, size, stream);

	assert_non_null(s);
	assert_true(0 < size);

	if ('\0' == *file->contents)
		return NULL;

	if (size - 1 < (length = strlen(file->contents)))
		length = size - 1;

	if (NULL != (newline = strchr(file->contents, '\n')) && newline - file->contents + 1 < length)
		length = newline - file->contents + 1;

	assert_int_equal(length, zbx_snprintf(s, size, "%.*s", length, file->contents));
	file->contents += length;
	return s;
}