xref: /freebsd/lib/libsbuf/tests/sbuf_stdio_test.c (revision e430d1ed78d021db4e9760d9800393b627156745)
1 /*-
2  * Copyright (c) 2017 Enji Cooper <ngie@freebsd.org>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25 
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
28 
29 #include <sys/param.h>
30 #include <sys/sbuf.h>
31 #include <errno.h>
32 #include <stdarg.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
37 
38 #include <atf-c.h>
39 
40 #include "sbuf_test_common.h"
41 
42 static char	test_string[] = "this is a test string";
43 
44 #define	MESSAGE_FORMAT	"message: %s\n"
45 #define	MESSAGE_SEPARATOR	';'
46 
47 static int
48 sbuf_vprintf_helper(struct sbuf *sb, const char * restrict format, ...)
49 {
50 	va_list ap;
51 	int rc;
52 
53 	va_start(ap, format);
54 
55 	rc = sbuf_vprintf(sb, format, ap);
56 
57 	va_end(ap);
58 
59 	return (rc);
60 }
61 
62 ATF_TC_WITHOUT_HEAD(sbuf_printf_test);
63 ATF_TC_BODY(sbuf_printf_test, tc)
64 {
65 	struct sbuf *sb;
66 	char *test_string_tmp;
67 
68 	asprintf(&test_string_tmp, "%s%c" MESSAGE_FORMAT,
69 	    test_string, MESSAGE_SEPARATOR, test_string);
70 	ATF_REQUIRE_MSG(test_string_tmp != NULL, "asprintf failed");
71 
72 	sb = sbuf_new_auto();
73 	ATF_REQUIRE_MSG(sb != NULL, "sbuf_new_auto failed: %s",
74 	    strerror(errno));
75 
76 	ATF_REQUIRE_MSG(sbuf_cat(sb, test_string) == 0, "sbuf_cat failed");
77 	ATF_REQUIRE_MSG(sbuf_putc(sb, MESSAGE_SEPARATOR) == 0,
78 	    "sbuf_putc failed");
79 
80 	ATF_REQUIRE_MSG(sbuf_printf(sb, MESSAGE_FORMAT, test_string) == 0,
81 	    "sbuf_printf failed");
82 
83 	ATF_REQUIRE_MSG(sbuf_finish(sb) == 0, "sbuf_finish failed: %s",
84 	    strerror(errno));
85 
86 	ATF_REQUIRE_STREQ_MSG(sbuf_data(sb), test_string_tmp,
87 	    "sbuf (\"%s\") != test string (\"%s\")", sbuf_data(sb),
88 	    test_string_tmp);
89 
90 	sbuf_delete(sb);
91 
92 	free(test_string_tmp);
93 }
94 
95 ATF_TC_WITHOUT_HEAD(sbuf_putbuf_test);
96 ATF_TC_BODY(sbuf_putbuf_test, tc)
97 {
98 	struct sbuf *sb;
99 	pid_t child_proc;
100 
101 	sb = sbuf_new_auto();
102 	ATF_REQUIRE_MSG(sb != NULL, "sbuf_new_auto failed: %s",
103 	    strerror(errno));
104 
105 	ATF_REQUIRE_MSG(sbuf_cat(sb, test_string) == 0, "sbuf_cat failed");
106 
107 	child_proc = atf_utils_fork();
108 	if (child_proc == 0) {
109 		sbuf_putbuf(sb);
110 		exit(0);
111 	}
112 	atf_utils_wait(child_proc, 0, test_string, "");
113 
114 	ATF_REQUIRE_MSG(sbuf_finish(sb) == 0, "sbuf_finish failed: %s",
115 	    strerror(errno));
116 
117 	sbuf_delete(sb);
118 }
119 
120 ATF_TC_WITHOUT_HEAD(sbuf_vprintf_test);
121 ATF_TC_BODY(sbuf_vprintf_test, tc)
122 {
123 	struct sbuf *sb;
124 	char *test_string_tmp;
125 	int rc;
126 
127 	asprintf(&test_string_tmp, "%s%c" MESSAGE_FORMAT,
128 	    test_string, MESSAGE_SEPARATOR, test_string);
129 	ATF_REQUIRE_MSG(test_string_tmp != NULL, "asprintf failed");
130 
131 	sb = sbuf_new_auto();
132 	ATF_REQUIRE_MSG(sb != NULL, "sbuf_new_auto failed: %s",
133 	    strerror(errno));
134 
135 	ATF_REQUIRE_MSG(sbuf_cat(sb, test_string) == 0, "sbuf_cat failed");
136 	ATF_REQUIRE_MSG(sbuf_putc(sb, MESSAGE_SEPARATOR) == 0,
137 	    "sbuf_putc failed");
138 
139 	rc = sbuf_vprintf_helper(sb, MESSAGE_FORMAT, test_string);
140 	ATF_REQUIRE_MSG(rc == 0, "sbuf_vprintf failed");
141 
142 	ATF_REQUIRE_MSG(sbuf_finish(sb) == 0, "sbuf_finish failed: %s",
143 	    strerror(errno));
144 
145 	ATF_REQUIRE_STREQ_MSG(sbuf_data(sb), test_string_tmp,
146 	    "sbuf (\"%s\") != test string (\"%s\")", sbuf_data(sb),
147 	    test_string_tmp);
148 
149 	sbuf_delete(sb);
150 }
151 
152 ATF_TP_ADD_TCS(tp)
153 {
154 
155 	ATF_TP_ADD_TC(tp, sbuf_printf_test);
156 	ATF_TP_ADD_TC(tp, sbuf_putbuf_test);
157 	ATF_TP_ADD_TC(tp, sbuf_vprintf_test);
158 
159 	return (atf_no_error());
160 }
161