aboutsummaryrefslogtreecommitdiff
path: root/lib/libsbuf/tests/sbuf_core_test.c
blob: 13a27933c20d69fa46964d3ab9bc532102d95f24 (plain) (blame)
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*-
 * Copyright (c) 2017 Enji Cooper <ngie@freebsd.org>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/sbuf.h>
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <atf-c.h>

#include "sbuf_test_common.h"

static char	test_string[] = "this is a test string";
#define	TEST_STRING_CHOP_COUNT	5
_Static_assert(nitems(test_string) > TEST_STRING_CHOP_COUNT,
    "test_string is too short");

ATF_TC_WITHOUT_HEAD(sbuf_clear_test);
ATF_TC_BODY(sbuf_clear_test, tc)
{
	struct sbuf *sb;
	ssize_t buf_len;
	pid_t child_proc;

	sb = sbuf_new_auto();
	ATF_REQUIRE_MSG(sb != NULL, "sbuf_new_auto failed: %s",
	    strerror(errno));

	ATF_REQUIRE_MSG(sbuf_cat(sb, test_string) == 0, "sbuf_cat failed");

	/*
	 * Cheat so we can get the contents of the buffer before calling
	 * sbuf_finish(3) below, making additional sbuf changes impossible.
	 */
	child_proc = atf_utils_fork();
	if (child_proc == 0) {
		ATF_REQUIRE_EQ_MSG(0, sbuf_finish(sb), "sbuf_finish failed: %s",
		    strerror(errno));

		sbuf_putbuf(sb);
		exit(0);
	}
	atf_utils_wait(child_proc, 0, test_string, "");

	sbuf_clear(sb);

	ATF_REQUIRE_MSG(sbuf_finish(sb) == 0, "sbuf_finish failed: %s",
	    strerror(errno));

	buf_len = sbuf_len(sb);
	ATF_REQUIRE_MSG(buf_len == 0, "sbuf_len (%zd) != 0", buf_len);
	ATF_REQUIRE_STREQ_MSG(sbuf_data(sb), "",
	    "sbuf (\"%s\") was not empty", sbuf_data(sb));

	sbuf_delete(sb);
}

ATF_TC_WITHOUT_HEAD(sbuf_done_and_sbuf_finish_test);
ATF_TC_BODY(sbuf_done_and_sbuf_finish_test, tc)
{
	struct sbuf *sb;

	sb = sbuf_new_auto();
	ATF_REQUIRE_MSG(sb != NULL, "sbuf_new_auto failed: %s",
	    strerror(errno));

	ATF_CHECK(sbuf_done(sb) == 0);

	ATF_REQUIRE_MSG(sbuf_finish(sb) == 0, "sbuf_finish failed: %s",
	    strerror(errno));

	ATF_CHECK(sbuf_done(sb) != 0);

	sbuf_delete(sb);
}

static int
drain_ret0(void *arg, const char *data, int len)
{

	(void)arg;
	(void)data;
	(void)len;

	return (0);
}

ATF_TC_WITHOUT_HEAD(sbuf_drain_ret0_test);
ATF_TC_BODY(sbuf_drain_ret0_test, tc)
{
	struct sbuf *sb;

	sb = sbuf_new_auto();

	sbuf_set_drain(sb, drain_ret0, NULL);

	sbuf_cat(sb, test_string);

	ATF_CHECK_EQ_MSG(-1, sbuf_finish(sb),
	    "required to return error when drain func returns 0");
	ATF_CHECK_EQ_MSG(EDEADLK, errno,
	    "errno required to be EDEADLK when drain func returns 0");
}

ATF_TC_WITHOUT_HEAD(sbuf_len_test);
ATF_TC_BODY(sbuf_len_test, tc)
{
	struct sbuf *sb;
	ssize_t buf_len, test_string_len;
	int i;

	sb = sbuf_new_auto();
	ATF_REQUIRE_MSG(sb != NULL, "sbuf_new_auto failed: %s",
	    strerror(errno));

	test_string_len = strlen(test_string);
	for (i = 0; i < 20; i++) {
		buf_len = sbuf_len(sb);
		ATF_REQUIRE_MSG(buf_len == (ssize_t)(i * test_string_len),
		    "sbuf_len (%zd) != %zu", buf_len, i * test_string_len);
		ATF_REQUIRE_MSG(sbuf_cat(sb, test_string) == 0, "sbuf_cat failed");
	}

#ifdef	HAVE_SBUF_SET_FLAGS
	sbuf_set_flags(sb, SBUF_INCLUDENUL);
	ATF_REQUIRE_MSG((ssize_t)(i * test_string_len + 1) == sbuf_len(sb),
	    "sbuf_len(..) didn't report the NUL char");
#endif

	ATF_REQUIRE_MSG(sbuf_finish(sb) == 0, "sbuf_finish failed: %s",
	    strerror(errno));

	sbuf_delete(sb);
}

ATF_TC_WITHOUT_HEAD(sbuf_new_fixedlen);
ATF_TC_BODY(sbuf_new_fixedlen, tc)
{
	char buf[strlen(test_string) + 1];
	struct sbuf sb;
	pid_t child_proc;

	sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);

	sbuf_cat(&sb, test_string);

	child_proc = atf_utils_fork();
	if (child_proc == 0) {
		ATF_REQUIRE_EQ_MSG(0, sbuf_finish(&sb), "sbuf_finish failed: %s",
		    strerror(errno));

		sbuf_putbuf(&sb);
		exit(0);
	}
	atf_utils_wait(child_proc, 0, test_string, "");

	sbuf_putc(&sb, ' ');

	ATF_CHECK_EQ_MSG(-1, sbuf_finish(&sb), "failed to return error on overflow");

	sbuf_delete(&sb);
}

ATF_TC_WITHOUT_HEAD(sbuf_setpos_test);
ATF_TC_BODY(sbuf_setpos_test, tc)
{
	struct sbuf *sb;
	size_t test_string_chopped_len, test_string_len;
	ssize_t buf_len;

	sb = sbuf_new_auto();
	ATF_REQUIRE_MSG(sb != NULL, "sbuf_new_auto failed: %s",
	    strerror(errno));

	/*
	 * An obvious sanity check -- if sbuf_len(..) lies, these invariants
	 * are impossible to test.
	 */
	ATF_REQUIRE(sbuf_len(sb) == 0);

	ATF_CHECK(sbuf_setpos(sb, -1) == -1);
	ATF_CHECK(sbuf_setpos(sb, 0) == 0);
	ATF_CHECK(sbuf_setpos(sb, 1) == -1);

	ATF_REQUIRE_MSG(sbuf_cat(sb, test_string) == 0, "sbuf_cat failed");

	buf_len = sbuf_len(sb);
	test_string_len = strlen(test_string);
	test_string_chopped_len = test_string_len - TEST_STRING_CHOP_COUNT;
	ATF_REQUIRE_MSG(buf_len == (ssize_t)test_string_len,
	    "sbuf length (%zd) != test_string length (%zu)", buf_len,
	    test_string_len);

	/* Out of bounds (under length) */
	ATF_CHECK(sbuf_setpos(sb, -1) == -1);
	/*
	 * Out of bounds (over length)
	 *
	 * Note: SBUF_INCLUDENUL not set, so take '\0' into account.
	 */
	ATF_CHECK(sbuf_setpos(sb, test_string_len + 2) == -1);
	/* Within bounds */
	ATF_CHECK(sbuf_setpos(sb, test_string_chopped_len) == 0);

	ATF_REQUIRE_MSG(sbuf_finish(sb) == 0, "sbuf_finish failed: %s",
	    strerror(errno));

	buf_len = sbuf_len(sb);
	ATF_REQUIRE_MSG(buf_len == (ssize_t)test_string_chopped_len,
	    "sbuf_setpos didn't truncate string as expected");
	ATF_REQUIRE_MSG(strncmp(sbuf_data(sb), test_string, buf_len) == 0,
	    "sbuf (\"%s\") != test string (\"%s\") for [0,%zd]", sbuf_data(sb),
	    test_string, buf_len);

	sbuf_delete(sb);
}

ATF_TP_ADD_TCS(tp)
{

	ATF_TP_ADD_TC(tp, sbuf_clear_test);
	ATF_TP_ADD_TC(tp, sbuf_done_and_sbuf_finish_test);
	ATF_TP_ADD_TC(tp, sbuf_drain_ret0_test);
	ATF_TP_ADD_TC(tp, sbuf_len_test);
	ATF_TP_ADD_TC(tp, sbuf_new_fixedlen);
#if 0
	/* TODO */
#ifdef	HAVE_SBUF_CLEAR_FLAGS
	ATF_TP_ADD_TC(tp, sbuf_clear_flags_test);
#endif
#ifdef	HAVE_SBUF_GET_FLAGS
	ATF_TP_ADD_TC(tp, sbuf_get_flags_test);
#endif
	ATF_TP_ADD_TC(tp, sbuf_new_positive_test);
	ATF_TP_ADD_TC(tp, sbuf_new_negative_test);
#ifdef	HAVE_SBUF_SET_FLAGS
	ATF_TP_ADD_TC(tp, sbuf_set_flags_test);
#endif
#endif
	ATF_TP_ADD_TC(tp, sbuf_setpos_test);

	return (atf_no_error());
}