xref: /freebsd/lib/libc/tests/stdio/gets_s_test.c (revision a77546fbb3a791e372a0f1e423082ebdb74db336)
1*a77546fbSCy Schubert /*-
2*a77546fbSCy Schubert  * Copyright (c) 2017 Cyril S. E. Schubert.  All rights reserved.
3*a77546fbSCy Schubert  *
4*a77546fbSCy Schubert  * Redistribution and use in source and binary forms, with or without
5*a77546fbSCy Schubert  * modification, are permitted provided that the following conditions
6*a77546fbSCy Schubert  * are met:
7*a77546fbSCy Schubert  * 1. Redistributions of source code must retain the above copyright
8*a77546fbSCy Schubert  *    notice, this list of conditions and the following disclaimer.
9*a77546fbSCy Schubert  * 2. Redistributions in binary form must reproduce the above copyright
10*a77546fbSCy Schubert  *    notice, this list of conditions and the following disclaimer in the
11*a77546fbSCy Schubert  *    documentation and/or other materials provided with the distribution.
12*a77546fbSCy Schubert  *
13*a77546fbSCy Schubert  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
14*a77546fbSCy Schubert  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15*a77546fbSCy Schubert  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16*a77546fbSCy Schubert  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
17*a77546fbSCy Schubert  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18*a77546fbSCy Schubert  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19*a77546fbSCy Schubert  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20*a77546fbSCy Schubert  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21*a77546fbSCy Schubert  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22*a77546fbSCy Schubert  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23*a77546fbSCy Schubert  * SUCH DAMAGE.
24*a77546fbSCy Schubert  */
25*a77546fbSCy Schubert 
26*a77546fbSCy Schubert #include <sys/cdefs.h>
27*a77546fbSCy Schubert __FBSDID("$FreeBSD$");
28*a77546fbSCy Schubert 
29*a77546fbSCy Schubert #include <assert.h>
30*a77546fbSCy Schubert #include <stdint.h>
31*a77546fbSCy Schubert #include <stdio.h>
32*a77546fbSCy Schubert #include <stdlib.h>
33*a77546fbSCy Schubert #include <sys/types.h>
34*a77546fbSCy Schubert #include <unistd.h>
35*a77546fbSCy Schubert #include <sys/wait.h>
36*a77546fbSCy Schubert 
37*a77546fbSCy Schubert #include <atf-c.h>
38*a77546fbSCy Schubert 
39*a77546fbSCy Schubert static errno_t error_code;
40*a77546fbSCy Schubert static const char * message;
41*a77546fbSCy Schubert 
42*a77546fbSCy Schubert void
43*a77546fbSCy Schubert h(const char * msg, void * ptr __unused, errno_t error)
44*a77546fbSCy Schubert {
45*a77546fbSCy Schubert 	error_code = error;
46*a77546fbSCy Schubert 	message = msg;
47*a77546fbSCy Schubert }
48*a77546fbSCy Schubert 
49*a77546fbSCy Schubert /* null ptr */
50*a77546fbSCy Schubert ATF_TC_WITHOUT_HEAD(null_ptr);
51*a77546fbSCy Schubert ATF_TC_BODY(null_ptr, tc)
52*a77546fbSCy Schubert {
53*a77546fbSCy Schubert 	ATF_CHECK_MSG(gets_s(NULL, 1) == NULL,
54*a77546fbSCy Schubert 		"gets_s() failed to handle NULL pointer");
55*a77546fbSCy Schubert }
56*a77546fbSCy Schubert 
57*a77546fbSCy Schubert /* normal */
58*a77546fbSCy Schubert ATF_TC_WITHOUT_HEAD(normal);
59*a77546fbSCy Schubert ATF_TC_BODY(normal, tc)
60*a77546fbSCy Schubert {
61*a77546fbSCy Schubert 	pid_t	kidpid;
62*a77546fbSCy Schubert 	int	fd[2];
63*a77546fbSCy Schubert 	int	nfd;
64*a77546fbSCy Schubert 
65*a77546fbSCy Schubert 	// close(STDIN_FILENO);
66*a77546fbSCy Schubert 	// close(STDOUT_FILENO);
67*a77546fbSCy Schubert 	pipe(fd);
68*a77546fbSCy Schubert 
69*a77546fbSCy Schubert 	if ((kidpid = fork()) == 0) {
70*a77546fbSCy Schubert 		char	b[10];
71*a77546fbSCy Schubert 
72*a77546fbSCy Schubert 		close(fd[1]);
73*a77546fbSCy Schubert 		nfd = dup2(fd[0], 0);
74*a77546fbSCy Schubert 		close(fd[0]);
75*a77546fbSCy Schubert 		stdin = fdopen(nfd, "r");
76*a77546fbSCy Schubert 		ATF_CHECK_MSG(gets_s(b, sizeof(b)) == 0, "gets_s() normal failed");
77*a77546fbSCy Schubert 		fclose(stdin);
78*a77546fbSCy Schubert 	} else {
79*a77546fbSCy Schubert 		int stat;
80*a77546fbSCy Schubert 
81*a77546fbSCy Schubert 		close(fd[0]);
82*a77546fbSCy Schubert 		stdout = fdopen(fd[1], "w");
83*a77546fbSCy Schubert 		puts("a sting");
84*a77546fbSCy Schubert 		fclose(stdout);
85*a77546fbSCy Schubert 		(void) waitpid(kidpid, &stat, WEXITED);
86*a77546fbSCy Schubert 	}
87*a77546fbSCy Schubert }
88*a77546fbSCy Schubert 
89*a77546fbSCy Schubert /* n > rmax */
90*a77546fbSCy Schubert ATF_TC_WITHOUT_HEAD(n_gt_rmax);
91*a77546fbSCy Schubert ATF_TC_BODY(n_gt_rmax, tc)
92*a77546fbSCy Schubert {
93*a77546fbSCy Schubert 	char b;
94*a77546fbSCy Schubert 
95*a77546fbSCy Schubert 	ATF_CHECK_MSG(gets_s(&b, RSIZE_MAX + 1) == NULL,
96*a77546fbSCy Schubert 		"gets_s() n > RSIZE_MAX");
97*a77546fbSCy Schubert }
98*a77546fbSCy Schubert 
99*a77546fbSCy Schubert /* n == 0 */
100*a77546fbSCy Schubert ATF_TC_WITHOUT_HEAD(n_eq_zero);
101*a77546fbSCy Schubert ATF_TC_BODY(n_eq_zero, tc)
102*a77546fbSCy Schubert {
103*a77546fbSCy Schubert 	char b;
104*a77546fbSCy Schubert 
105*a77546fbSCy Schubert 	ATF_CHECK_MSG(gets_s(&b, 0) == NULL, "gets_s() n is zero");
106*a77546fbSCy Schubert }
107*a77546fbSCy Schubert 
108*a77546fbSCy Schubert /* n > rmax, handler */
109*a77546fbSCy Schubert ATF_TC_WITHOUT_HEAD(n_gt_rmax_handler);
110*a77546fbSCy Schubert ATF_TC_BODY(n_gt_rmax_handler, tc)
111*a77546fbSCy Schubert {
112*a77546fbSCy Schubert 	char b;
113*a77546fbSCy Schubert 
114*a77546fbSCy Schubert 	error_code = 0;
115*a77546fbSCy Schubert 	message = NULL;
116*a77546fbSCy Schubert 	set_constraint_handler_s(h);
117*a77546fbSCy Schubert 	ATF_CHECK_MSG(gets_s(&b, RSIZE_MAX + 1) == NULL, "gets_s() n > RSIZE_MAX");
118*a77546fbSCy Schubert 	ATF_CHECK_MSG(error_code > 0, "gets_s() error code is %d", error_code);
119*a77546fbSCy Schubert 	ATF_CHECK_MSG(strcmp(message, "gets_s : n > RSIZE_MAX") == 0, "gets_s(): incorrect error message");
120*a77546fbSCy Schubert }
121*a77546fbSCy Schubert 
122*a77546fbSCy Schubert /* n == 0, handler */
123*a77546fbSCy Schubert ATF_TC_WITHOUT_HEAD(n_eq_zero_handler);
124*a77546fbSCy Schubert ATF_TC_BODY(n_eq_zero_handler, tc)
125*a77546fbSCy Schubert {
126*a77546fbSCy Schubert 	char b;
127*a77546fbSCy Schubert 
128*a77546fbSCy Schubert 	error_code = 0;
129*a77546fbSCy Schubert 	message = NULL;
130*a77546fbSCy Schubert 	set_constraint_handler_s(h);
131*a77546fbSCy Schubert 	ATF_CHECK(gets_s(&b, 0) == NULL);
132*a77546fbSCy Schubert 	ATF_CHECK_MSG(error_code > 0, "gets_s() error code is %d", error_code);
133*a77546fbSCy Schubert 	ATF_CHECK_MSG(strcmp(message, "gets_s : n == 0") == 0, "gets_s(): incorrect error message");
134*a77546fbSCy Schubert }
135*a77546fbSCy Schubert 
136*a77546fbSCy Schubert ATF_TP_ADD_TCS(tp)
137*a77546fbSCy Schubert {
138*a77546fbSCy Schubert 	ATF_TP_ADD_TC(tp, null_ptr);
139*a77546fbSCy Schubert 	ATF_TP_ADD_TC(tp, normal);
140*a77546fbSCy Schubert 	ATF_TP_ADD_TC(tp, n_gt_rmax);
141*a77546fbSCy Schubert 	ATF_TP_ADD_TC(tp, n_eq_zero);
142*a77546fbSCy Schubert 	ATF_TP_ADD_TC(tp, n_gt_rmax_handler);
143*a77546fbSCy Schubert 	ATF_TP_ADD_TC(tp, n_eq_zero_handler);
144*a77546fbSCy Schubert 	return (atf_no_error());
145*a77546fbSCy Schubert }
146