1 /*- 2 * Copyright (c) 2017 Juniper Networks. All rights reserved. 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 <stdlib.h> 32 #include <string.h> 33 34 #include <atf-c.h> 35 36 static errno_t e; 37 static const char * restrict m; 38 39 void 40 h(const char * restrict msg, void * restrict ptr __unused, errno_t error) 41 { 42 e = error; 43 m = msg; 44 } 45 46 /* null ptr */ 47 ATF_TC_WITHOUT_HEAD(null_ptr); 48 ATF_TC_BODY(null_ptr, tc) 49 { 50 assert(memset_s(0, 1, 1, 1) != 0); 51 } 52 53 /* smax > rmax */ 54 ATF_TC_WITHOUT_HEAD(smax_gt_rmax); 55 ATF_TC_BODY(smax_gt_rmax, tc) 56 { 57 char b; 58 59 assert(memset_s(&b, RSIZE_MAX + 1, 1, 1) != 0); 60 } 61 62 /* smax < 0 */ 63 ATF_TC_WITHOUT_HEAD(smax_lt_zero); 64 ATF_TC_BODY(smax_lt_zero, tc) 65 { 66 char b; 67 68 assert(memset_s(&b, -1, 1, 1) != 0); 69 } 70 71 /* normal */ 72 ATF_TC_WITHOUT_HEAD(normal); 73 ATF_TC_BODY(normal, tc) 74 { 75 char b; 76 77 b = 3; 78 assert(memset_s(&b, 1, 5, 1) == 0); 79 assert(b == 5); 80 } 81 82 /* n > rmax */ 83 ATF_TC_WITHOUT_HEAD(n_gt_rmax); 84 ATF_TC_BODY(n_gt_rmax, tc) 85 { 86 char b; 87 88 assert(memset_s(&b, 1, 1, RSIZE_MAX + 1) != 0); 89 } 90 91 /* n < 0 */ 92 ATF_TC_WITHOUT_HEAD(n_lt_zero); 93 ATF_TC_BODY(n_lt_zero, tc) 94 { 95 char b; 96 97 assert(memset_s(&b, 1, 1, -1) != 0); 98 } 99 100 /* n < smax */ 101 ATF_TC_WITHOUT_HEAD(n_lt_smax); 102 ATF_TC_BODY(n_lt_smax, tc) 103 { 104 char b[3] = {1, 2, 3}; 105 106 assert(memset_s(&b[0], 3, 9, 1) == 0); 107 assert(b[0] == 9); 108 assert(b[1] == 2); 109 assert(b[2] == 3); 110 } 111 112 /* n > smax, handler */ 113 ATF_TC_WITHOUT_HEAD(n_gt_smax); 114 ATF_TC_BODY(n_gt_smax, tc) 115 { 116 char b[3] = {1, 2, 3}; 117 118 e = 0; 119 m = NULL; 120 set_constraint_handler_s(h); 121 assert(memset_s(&b[0], 1, 9, 3) != 0); 122 assert(e > 0); 123 assert(strcmp(m, "memset_s : n > smax") == 0); 124 assert(b[0] == 9); 125 assert(b[1] == 2); 126 assert(b[2] == 3); 127 } 128 129 /* smax > rmax, handler */ 130 ATF_TC_WITHOUT_HEAD(smax_gt_rmax_handler); 131 ATF_TC_BODY(smax_gt_rmax_handler, tc) 132 { 133 char b; 134 135 e = 0; 136 m = NULL; 137 set_constraint_handler_s(h); 138 assert(memset_s(&b, RSIZE_MAX + 1, 1, 1) != 0); 139 assert(e > 0); 140 assert(strcmp(m, "memset_s : smax > RSIZE_MAX") == 0); 141 } 142 143 /* smax < 0, handler */ 144 ATF_TC_WITHOUT_HEAD(smax_lt_zero_handler); 145 ATF_TC_BODY(smax_lt_zero_handler, tc) 146 { 147 char b; 148 149 e = 0; 150 m = NULL; 151 set_constraint_handler_s(h); 152 assert(memset_s(&b, -1, 1, 1) != 0); 153 assert(e > 0); 154 assert(strcmp(m, "memset_s : smax > RSIZE_MAX") == 0); 155 } 156 157 /* n > rmax, handler */ 158 ATF_TC_WITHOUT_HEAD(n_gt_rmax_handler); 159 ATF_TC_BODY(n_gt_rmax_handler, tc) 160 { 161 char b; 162 163 e = 0; 164 m = NULL; 165 set_constraint_handler_s(h); 166 assert(memset_s(&b, 1, 1, RSIZE_MAX + 1) != 0); 167 assert(e > 0); 168 assert(strcmp(m, "memset_s : n > RSIZE_MAX") == 0); 169 } 170 171 /* n < 0, handler */ 172 ATF_TC_WITHOUT_HEAD(n_lt_zero_handler); 173 ATF_TC_BODY(n_lt_zero_handler, tc) 174 { 175 char b; 176 177 e = 0; 178 m = NULL; 179 set_constraint_handler_s(h); 180 assert(memset_s(&b, 1, 1, -1) != 0); 181 assert(e > 0); 182 assert(strcmp(m, "memset_s : n > RSIZE_MAX") == 0); 183 } 184 185 ATF_TP_ADD_TCS(tp) 186 { 187 ATF_TP_ADD_TC(tp, null_ptr); 188 ATF_TP_ADD_TC(tp, smax_gt_rmax); 189 ATF_TP_ADD_TC(tp, smax_lt_zero); 190 ATF_TP_ADD_TC(tp, normal); 191 ATF_TP_ADD_TC(tp, n_gt_rmax); 192 ATF_TP_ADD_TC(tp, n_lt_zero); 193 ATF_TP_ADD_TC(tp, n_gt_smax); 194 ATF_TP_ADD_TC(tp, n_lt_smax); 195 ATF_TP_ADD_TC(tp, smax_gt_rmax_handler); 196 ATF_TP_ADD_TC(tp, smax_lt_zero_handler); 197 ATF_TP_ADD_TC(tp, n_gt_rmax_handler); 198 ATF_TP_ADD_TC(tp, n_lt_zero_handler); 199 return (atf_no_error()); 200 } 201