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