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