2 * Copyright (c) 2017 Cyril S. E. Schubert. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
33 #include <sys/types.h>
39 static errno_t error_code;
40 static const char * message;
43 h(const char * msg, void * ptr __unused, errno_t error)
50 ATF_TC_WITHOUT_HEAD(null_ptr);
51 ATF_TC_BODY(null_ptr, tc)
53 ATF_CHECK_MSG(gets_s(NULL, 1) == NULL,
54 "gets_s() failed to handle NULL pointer");
58 ATF_TC_WITHOUT_HEAD(normal);
59 ATF_TC_BODY(normal, tc)
65 // close(STDIN_FILENO);
66 // close(STDOUT_FILENO);
69 if ((kidpid = fork()) == 0) {
75 stdin = fdopen(nfd, "r");
76 ATF_CHECK_MSG(gets_s(b, sizeof(b)) == 0, "gets_s() normal failed");
82 stdout = fdopen(fd[1], "w");
85 (void) waitpid(kidpid, &stat, WEXITED);
90 ATF_TC_WITHOUT_HEAD(n_gt_rmax);
91 ATF_TC_BODY(n_gt_rmax, tc)
95 ATF_CHECK_MSG(gets_s(&b, RSIZE_MAX + 1) == NULL,
96 "gets_s() n > RSIZE_MAX");
100 ATF_TC_WITHOUT_HEAD(n_eq_zero);
101 ATF_TC_BODY(n_eq_zero, tc)
105 ATF_CHECK_MSG(gets_s(&b, 0) == NULL, "gets_s() n is zero");
108 /* n > rmax, handler */
109 ATF_TC_WITHOUT_HEAD(n_gt_rmax_handler);
110 ATF_TC_BODY(n_gt_rmax_handler, tc)
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");
122 /* n == 0, handler */
123 ATF_TC_WITHOUT_HEAD(n_eq_zero_handler);
124 ATF_TC_BODY(n_eq_zero_handler, tc)
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");
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());