sbuf_core_test.c revision 321118
1/*-
2 * Copyright (c) 2017 Ngie Cooper <ngie@freebsd.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: stable/10/lib/libsbuf/tests/sbuf_core_test.c 321118 2017-07-18 08:30:58Z ngie $");
29
30#include <sys/param.h>
31#include <sys/sbuf.h>
32#include <errno.h>
33#include <stdarg.h>
34#include <stdio.h>
35#include <stdlib.h>
36#include <string.h>
37#include <unistd.h>
38
39#include <atf-c.h>
40
41#include "sbuf_test_common.h"
42
43static char	test_string[] = "this is a test string";
44#define	TEST_STRING_CHOP_COUNT	5
45_Static_assert(nitems(test_string) > TEST_STRING_CHOP_COUNT,
46    "test_string is too short");
47
48ATF_TC_WITHOUT_HEAD(sbuf_clear_test);
49ATF_TC_BODY(sbuf_clear_test, tc)
50{
51	struct sbuf *sb;
52	ssize_t buf_len;
53	pid_t child_proc;
54
55	sb = sbuf_new_auto();
56	ATF_REQUIRE_MSG(sb != NULL, "sbuf_new_auto failed: %s",
57	    strerror(errno));
58
59	ATF_REQUIRE_MSG(sbuf_cat(sb, test_string) == 0, "sbuf_cat failed");
60
61	/*
62	 * Cheat so we can get the contents of the buffer before calling
63	 * sbuf_finish(3) below, making additional sbuf changes impossible.
64	 */
65	child_proc = atf_utils_fork();
66	if (child_proc == 0) {
67		sbuf_putbuf(sb);
68		exit(0);
69	}
70	atf_utils_wait(child_proc, 0, test_string, "");
71
72	sbuf_clear(sb);
73
74	ATF_REQUIRE_MSG(sbuf_finish(sb) == 0, "sbuf_finish failed: %s",
75	    strerror(errno));
76
77	buf_len = sbuf_len(sb);
78	ATF_REQUIRE_MSG(buf_len == 0, "sbuf_len (%zd) != 0", buf_len);
79	ATF_REQUIRE_STREQ_MSG(sbuf_data(sb), "",
80	    "sbuf (\"%s\") was not empty", sbuf_data(sb));
81
82	sbuf_delete(sb);
83}
84
85ATF_TC_WITHOUT_HEAD(sbuf_done_and_sbuf_finish_test);
86ATF_TC_BODY(sbuf_done_and_sbuf_finish_test, tc)
87{
88	struct sbuf *sb;
89
90	sb = sbuf_new_auto();
91	ATF_REQUIRE_MSG(sb != NULL, "sbuf_new_auto failed: %s",
92	    strerror(errno));
93
94	ATF_CHECK(sbuf_done(sb) == 0);
95
96	ATF_REQUIRE_MSG(sbuf_finish(sb) == 0, "sbuf_finish failed: %s",
97	    strerror(errno));
98
99	ATF_CHECK(sbuf_done(sb) != 0);
100
101	sbuf_delete(sb);
102}
103
104ATF_TC_WITHOUT_HEAD(sbuf_len_test);
105ATF_TC_BODY(sbuf_len_test, tc)
106{
107	struct sbuf *sb;
108	ssize_t buf_len, test_string_len;
109	int i;
110
111	sb = sbuf_new_auto();
112	ATF_REQUIRE_MSG(sb != NULL, "sbuf_new_auto failed: %s",
113	    strerror(errno));
114
115	test_string_len = strlen(test_string);
116	for (i = 0; i < 20; i++) {
117		buf_len = sbuf_len(sb);
118		ATF_REQUIRE_MSG(buf_len == (ssize_t)(i * test_string_len),
119		    "sbuf_len (%zd) != %zu", buf_len, i * test_string_len);
120		ATF_REQUIRE_MSG(sbuf_cat(sb, test_string) == 0, "sbuf_cat failed");
121	}
122
123#ifdef	HAVE_SBUF_SET_FLAGS
124	sbuf_set_flags(sb, SBUF_INCLUDENUL);
125	ATF_REQUIRE_MSG((ssize_t)(i * test_string_len + 1) == sbuf_len(sb),
126	    "sbuf_len(..) didn't report the NUL char");
127#endif
128
129	ATF_REQUIRE_MSG(sbuf_finish(sb) == 0, "sbuf_finish failed: %s",
130	    strerror(errno));
131
132	sbuf_delete(sb);
133}
134
135ATF_TC_WITHOUT_HEAD(sbuf_setpos_test);
136ATF_TC_BODY(sbuf_setpos_test, tc)
137{
138	struct sbuf *sb;
139	size_t test_string_chopped_len, test_string_len;
140	ssize_t buf_len;
141
142	sb = sbuf_new_auto();
143	ATF_REQUIRE_MSG(sb != NULL, "sbuf_new_auto failed: %s",
144	    strerror(errno));
145
146	/*
147	 * An obvious sanity check -- if sbuf_len(..) lies, these invariants
148	 * are impossible to test.
149	 */
150	ATF_REQUIRE(sbuf_len(sb) == 0);
151
152	ATF_CHECK(sbuf_setpos(sb, -1) == -1);
153	ATF_CHECK(sbuf_setpos(sb, 0) == 0);
154	ATF_CHECK(sbuf_setpos(sb, 1) == -1);
155
156	ATF_REQUIRE_MSG(sbuf_cat(sb, test_string) == 0, "sbuf_cat failed");
157
158	buf_len = sbuf_len(sb);
159	test_string_len = strlen(test_string);
160	test_string_chopped_len = test_string_len - TEST_STRING_CHOP_COUNT;
161	ATF_REQUIRE_MSG(buf_len == (ssize_t)test_string_len,
162	    "sbuf length (%zd) != test_string length (%zu)", buf_len,
163	    test_string_len);
164
165	/* Out of bounds (under length) */
166	ATF_CHECK(sbuf_setpos(sb, -1) == -1);
167	/*
168	 * Out of bounds (over length)
169	 *
170	 * Note: SBUF_INCLUDENUL not set, so take '\0' into account.
171	 */
172	ATF_CHECK(sbuf_setpos(sb, test_string_len + 2) == -1);
173	/* Within bounds */
174	ATF_CHECK(sbuf_setpos(sb, test_string_chopped_len) == 0);
175
176	ATF_REQUIRE_MSG(sbuf_finish(sb) == 0, "sbuf_finish failed: %s",
177	    strerror(errno));
178
179	buf_len = sbuf_len(sb);
180	ATF_REQUIRE_MSG(buf_len == (ssize_t)test_string_chopped_len,
181	    "sbuf_setpos didn't truncate string as expected");
182	ATF_REQUIRE_MSG(strncmp(sbuf_data(sb), test_string, buf_len) == 0,
183	    "sbuf (\"%s\") != test string (\"%s\") for [0,%zd]", sbuf_data(sb),
184	    test_string, buf_len);
185
186	sbuf_delete(sb);
187}
188
189ATF_TP_ADD_TCS(tp)
190{
191
192	ATF_TP_ADD_TC(tp, sbuf_clear_test);
193	ATF_TP_ADD_TC(tp, sbuf_done_and_sbuf_finish_test);
194	ATF_TP_ADD_TC(tp, sbuf_len_test);
195#if 0
196	/* TODO */
197#ifdef	HAVE_SBUF_CLEAR_FLAGS
198	ATF_TP_ADD_TC(tp, sbuf_clear_flags_test);
199#endif
200#ifdef	HAVE_SBUF_GET_FLAGS
201	ATF_TP_ADD_TC(tp, sbuf_get_flags_test);
202#endif
203	ATF_TP_ADD_TC(tp, sbuf_new_positive_test);
204	ATF_TP_ADD_TC(tp, sbuf_new_negative_test);
205#ifdef	HAVE_SBUF_SET_FLAGS
206	ATF_TP_ADD_TC(tp, sbuf_set_flags_test);
207#endif
208#endif
209	ATF_TP_ADD_TC(tp, sbuf_setpos_test);
210
211	return (atf_no_error());
212}
213