]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - lib/libsbuf/tests/sbuf_stdio_test.c
MFC r368207,368607:
[FreeBSD/stable/10.git] / lib / libsbuf / tests / sbuf_stdio_test.c
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$");
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
43 static char     test_string[] = "this is a test string";
44
45 #define MESSAGE_FORMAT  "message: %s\n"
46 #define MESSAGE_SEPARATOR       ';'
47
48 static int
49 sbuf_vprintf_helper(struct sbuf *sb, const char * restrict format, ...)
50 {
51         va_list ap;
52         int rc;
53
54         va_start(ap, format);
55
56         rc = sbuf_vprintf(sb, format, ap);
57
58         va_end(ap);
59
60         return (rc);
61 }
62
63 ATF_TC_WITHOUT_HEAD(sbuf_printf_test);
64 ATF_TC_BODY(sbuf_printf_test, tc)
65 {
66         struct sbuf *sb;
67         char *test_string_tmp;
68
69         asprintf(&test_string_tmp, "%s%c" MESSAGE_FORMAT,
70             test_string, MESSAGE_SEPARATOR, test_string);
71         ATF_REQUIRE_MSG(test_string_tmp != NULL, "asprintf failed");
72
73         sb = sbuf_new_auto();
74         ATF_REQUIRE_MSG(sb != NULL, "sbuf_new_auto failed: %s",
75             strerror(errno));
76
77         ATF_REQUIRE_MSG(sbuf_cat(sb, test_string) == 0, "sbuf_cat failed");
78         ATF_REQUIRE_MSG(sbuf_putc(sb, MESSAGE_SEPARATOR) == 0,
79             "sbuf_putc failed");
80
81         ATF_REQUIRE_MSG(sbuf_printf(sb, MESSAGE_FORMAT, test_string) == 0,
82             "sbuf_printf failed");
83
84         ATF_REQUIRE_MSG(sbuf_finish(sb) == 0, "sbuf_finish failed: %s",
85             strerror(errno));
86
87         ATF_REQUIRE_STREQ_MSG(sbuf_data(sb), test_string_tmp,
88             "sbuf (\"%s\") != test string (\"%s\")", sbuf_data(sb),
89             test_string_tmp);
90
91         sbuf_delete(sb);
92
93         free(test_string_tmp);
94 }
95
96 ATF_TC_WITHOUT_HEAD(sbuf_putbuf_test);
97 ATF_TC_BODY(sbuf_putbuf_test, tc)
98 {
99         struct sbuf *sb;
100         pid_t child_proc;
101
102         sb = sbuf_new_auto();
103         ATF_REQUIRE_MSG(sb != NULL, "sbuf_new_auto failed: %s",
104             strerror(errno));
105
106         ATF_REQUIRE_MSG(sbuf_cat(sb, test_string) == 0, "sbuf_cat failed");
107
108         child_proc = atf_utils_fork();
109         if (child_proc == 0) {
110                 sbuf_putbuf(sb);
111                 exit(0);
112         }
113         atf_utils_wait(child_proc, 0, test_string, "");
114
115         ATF_REQUIRE_MSG(sbuf_finish(sb) == 0, "sbuf_finish failed: %s",
116             strerror(errno));
117
118         sbuf_delete(sb);
119 }
120
121 ATF_TC_WITHOUT_HEAD(sbuf_vprintf_test);
122 ATF_TC_BODY(sbuf_vprintf_test, tc)
123 {
124         struct sbuf *sb;
125         char *test_string_tmp;
126         int rc;
127
128         asprintf(&test_string_tmp, "%s%c" MESSAGE_FORMAT,
129             test_string, MESSAGE_SEPARATOR, test_string);
130         ATF_REQUIRE_MSG(test_string_tmp != NULL, "asprintf failed");
131
132         sb = sbuf_new_auto();
133         ATF_REQUIRE_MSG(sb != NULL, "sbuf_new_auto failed: %s",
134             strerror(errno));
135
136         ATF_REQUIRE_MSG(sbuf_cat(sb, test_string) == 0, "sbuf_cat failed");
137         ATF_REQUIRE_MSG(sbuf_putc(sb, MESSAGE_SEPARATOR) == 0,
138             "sbuf_putc failed");
139
140         rc = sbuf_vprintf_helper(sb, MESSAGE_FORMAT, test_string);
141         ATF_REQUIRE_MSG(rc == 0, "sbuf_vprintf failed");
142
143         ATF_REQUIRE_MSG(sbuf_finish(sb) == 0, "sbuf_finish failed: %s",
144             strerror(errno));
145
146         ATF_REQUIRE_STREQ_MSG(sbuf_data(sb), test_string_tmp,
147             "sbuf (\"%s\") != test string (\"%s\")", sbuf_data(sb),
148             test_string_tmp);
149
150         sbuf_delete(sb);
151 }
152
153 ATF_TP_ADD_TCS(tp)
154 {
155
156         ATF_TP_ADD_TC(tp, sbuf_printf_test);
157         ATF_TP_ADD_TC(tp, sbuf_putbuf_test);
158         ATF_TP_ADD_TC(tp, sbuf_vprintf_test);
159
160         return (atf_no_error());
161 }