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