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