xref: /freebsd/lib/libc/tests/stdio/gets_s_test.c (revision 924226fba12cc9a228c73b956e1b7fa24c60b055)
1 /*-
2  * Copyright (c) 2017 Cyril S. E. Schubert
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 REGENTS 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 REGENTS 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 <assert.h>
30 #include <stdint.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <sys/types.h>
34 #include <unistd.h>
35 #include <sys/wait.h>
36 
37 #include <atf-c.h>
38 
39 static errno_t error_code;
40 static const char * message;
41 
42 void
43 h(const char * msg, void * ptr __unused, errno_t error)
44 {
45 	error_code = error;
46 	message = msg;
47 }
48 
49 /* null ptr */
50 ATF_TC_WITHOUT_HEAD(null_ptr);
51 ATF_TC_BODY(null_ptr, tc)
52 {
53 	ATF_CHECK_MSG(gets_s(NULL, 1) == NULL,
54 		"gets_s() failed to handle NULL pointer");
55 }
56 
57 /* normal */
58 ATF_TC_WITHOUT_HEAD(normal);
59 ATF_TC_BODY(normal, tc)
60 {
61 	pid_t	kidpid;
62 	int	fd[2];
63 	int	nfd;
64 
65 	// close(STDIN_FILENO);
66 	// close(STDOUT_FILENO);
67 	pipe(fd);
68 
69 	if ((kidpid = fork()) == 0) {
70 		char	b[10];
71 
72 		close(fd[1]);
73 		nfd = dup2(fd[0], 0);
74 		close(fd[0]);
75 		stdin = fdopen(nfd, "r");
76 		ATF_CHECK_MSG(gets_s(b, sizeof(b)) == 0, "gets_s() normal failed");
77 		fclose(stdin);
78 	} else {
79 		int stat;
80 
81 		close(fd[0]);
82 		stdout = fdopen(fd[1], "w");
83 		puts("a sting");
84 		fclose(stdout);
85 		(void) waitpid(kidpid, &stat, WEXITED);
86 	}
87 }
88 
89 /* n > rmax */
90 ATF_TC_WITHOUT_HEAD(n_gt_rmax);
91 ATF_TC_BODY(n_gt_rmax, tc)
92 {
93 	char b;
94 
95 	ATF_CHECK_MSG(gets_s(&b, RSIZE_MAX + 1) == NULL,
96 		"gets_s() n > RSIZE_MAX");
97 }
98 
99 /* n == 0 */
100 ATF_TC_WITHOUT_HEAD(n_eq_zero);
101 ATF_TC_BODY(n_eq_zero, tc)
102 {
103 	char b;
104 
105 	ATF_CHECK_MSG(gets_s(&b, 0) == NULL, "gets_s() n is zero");
106 }
107 
108 /* n > rmax, handler */
109 ATF_TC_WITHOUT_HEAD(n_gt_rmax_handler);
110 ATF_TC_BODY(n_gt_rmax_handler, tc)
111 {
112 	char b;
113 
114 	error_code = 0;
115 	message = NULL;
116 	set_constraint_handler_s(h);
117 	ATF_CHECK_MSG(gets_s(&b, RSIZE_MAX + 1) == NULL, "gets_s() n > RSIZE_MAX");
118 	ATF_CHECK_MSG(error_code > 0, "gets_s() error code is %d", error_code);
119 	ATF_CHECK_MSG(strcmp(message, "gets_s : n > RSIZE_MAX") == 0, "gets_s(): incorrect error message");
120 }
121 
122 /* n == 0, handler */
123 ATF_TC_WITHOUT_HEAD(n_eq_zero_handler);
124 ATF_TC_BODY(n_eq_zero_handler, tc)
125 {
126 	char b;
127 
128 	error_code = 0;
129 	message = NULL;
130 	set_constraint_handler_s(h);
131 	ATF_CHECK(gets_s(&b, 0) == NULL);
132 	ATF_CHECK_MSG(error_code > 0, "gets_s() error code is %d", error_code);
133 	ATF_CHECK_MSG(strcmp(message, "gets_s : n == 0") == 0, "gets_s(): incorrect error message");
134 }
135 
136 ATF_TP_ADD_TCS(tp)
137 {
138 	ATF_TP_ADD_TC(tp, null_ptr);
139 	ATF_TP_ADD_TC(tp, normal);
140 	ATF_TP_ADD_TC(tp, n_gt_rmax);
141 	ATF_TP_ADD_TC(tp, n_eq_zero);
142 	ATF_TP_ADD_TC(tp, n_gt_rmax_handler);
143 	ATF_TP_ADD_TC(tp, n_eq_zero_handler);
144 	return (atf_no_error());
145 }
146