1 /* $NetBSD: t_mktemp.c,v 1.4 2021/01/11 20:31:34 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 2013, 2020 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Christos Zoulas and Jukka Ruohonen. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 #include <sys/cdefs.h> 32 __RCSID("$NetBSD: t_mktemp.c,v 1.4 2021/01/11 20:31:34 christos Exp $"); 33 34 #include <sys/stat.h> 35 36 #include <atf-c.h> 37 #include <fcntl.h> 38 #include <limits.h> 39 #include <stdio.h> 40 #include <stdlib.h> 41 #include <unistd.h> 42 43 static int 44 check_mode(struct stat sa, const int mode, const int dir) 45 { 46 47 if (dir == 0 && S_ISREG(sa.st_mode) == 0) 48 return -1; 49 50 if (dir != 0 && S_ISDIR(sa.st_mode) == 0) 51 return -1; 52 53 if ((sa.st_mode & mode) == 0) 54 return -1; 55 56 return 0; 57 } 58 59 ATF_TC(mktemp_not_exist); 60 ATF_TC_HEAD(mktemp_not_exist, tc) 61 { 62 atf_tc_set_md_var(tc, "descr", "Test that mktemp(3)" 63 " does not fail on a path that does not exist"); 64 } 65 66 ATF_TC_BODY(mktemp_not_exist, tc) 67 { 68 char template[] = "I will barf/XXXXXX"; 69 ATF_REQUIRE(mktemp(template) != NULL); 70 } 71 72 ATF_TC(mktemp_large_template); 73 ATF_TC_HEAD(mktemp_large_template, tc) 74 { 75 atf_tc_set_md_var(tc, "descr", "Test that mktemp " 76 "accepts arbitrarily large templates (PR lib/55441)"); 77 } 78 79 ATF_TC_BODY(mktemp_large_template, tc) 80 { 81 const char *path = "/tmp/mktemp.XXXXXX"; 82 char *template; 83 size_t i; 84 long name_max; 85 size_t tlen; 86 87 name_max = pathconf("/tmp/", _PC_NAME_MAX); 88 ATF_REQUIRE(name_max > 0); 89 90 tlen = (size_t)name_max + sizeof("/tmp/"); 91 template = malloc(tlen); 92 ATF_REQUIRE(template != NULL); 93 94 ATF_REQUIRE(strcpy(template, path) != NULL); 95 ATF_REQUIRE(mktemp(template) != NULL); 96 ATF_REQUIRE(strlen(template) == strlen(path)); 97 ATF_REQUIRE(strcmp(template, path) != 0); 98 ATF_REQUIRE(strncmp(template, "/tmp/mktemp.", 12) == 0); 99 100 (void)memset(template, '\0', tlen); 101 (void)strcpy(template, "/tmp/mktemp."); 102 103 for (i = 12; i < tlen - 1; i++) 104 template[i] = 'X'; 105 106 ATF_REQUIRE(mktemp(template) != NULL); 107 ATF_REQUIRE(strlen(template) == tlen - 1); 108 ATF_REQUIRE(strcmp(template, path) != 0); 109 ATF_REQUIRE(strncmp(template, "/tmp/mktemp.", 12) == 0); 110 free(template); 111 } 112 113 ATF_TC(mkstemp_basic); 114 ATF_TC_HEAD(mkstemp_basic, tc) 115 { 116 atf_tc_set_md_var(tc, "descr", "A basic test of mkstemp(3)"); 117 } 118 119 ATF_TC_BODY(mkstemp_basic, tc) 120 { 121 char template[] = "/tmp/mktemp.XXXXXX"; 122 struct stat sa; 123 int fd; 124 125 (void)memset(&sa, 0, sizeof(struct stat)); 126 127 fd = mkstemp(template); 128 129 ATF_REQUIRE(fd != -1); 130 ATF_REQUIRE(strncmp(template, "/tmp/mktemp.", 12) == 0); 131 ATF_REQUIRE(write(fd, "X", 1) == 1); 132 ATF_REQUIRE(fstat(fd, &sa) == 0); 133 ATF_REQUIRE(check_mode(sa, 0600, 0) == 0); 134 ATF_REQUIRE(close(fd) == 0); 135 ATF_REQUIRE(unlink(template) == 0); 136 } 137 138 ATF_TC(mkstemps_basic); 139 ATF_TC_HEAD(mkstemps_basic, tc) 140 { 141 atf_tc_set_md_var(tc, "descr", "A basic test of mkstemps(3)"); 142 } 143 144 ATF_TC_BODY(mkstemps_basic, tc) 145 { 146 char template[] = "/tmp/mktemp.XXXyyy"; 147 char *suffix = strchr(template, 'y'); 148 struct stat sa; 149 int fd; 150 151 (void)memset(&sa, 0, sizeof(struct stat)); 152 153 fd = mkstemps(template, 3); 154 155 ATF_REQUIRE(fd != -1); 156 ATF_REQUIRE(strncmp(template, "/tmp/mktemp.", 12) == 0); 157 ATF_REQUIRE(strcmp(suffix, "yyy") == 0); 158 ATF_REQUIRE(write(fd, "X", 1) == 1); 159 ATF_REQUIRE(fstat(fd, &sa) == 0); 160 ATF_REQUIRE(check_mode(sa, 0600, 0) == 0); 161 ATF_REQUIRE(close(fd) == 0); 162 ATF_REQUIRE(unlink(template) == 0); 163 } 164 165 ATF_TC(mkdtemp_basic); 166 ATF_TC_HEAD(mkdtemp_basic, tc) 167 { 168 atf_tc_set_md_var(tc, "descr", "A basic test of mkdtemp(3)"); 169 } 170 171 ATF_TC_BODY(mkdtemp_basic, tc) 172 { 173 char template[] = "/tmp/mktemp.XXXXXX"; 174 char *path = mkdtemp(template); 175 struct stat sa; 176 177 (void)memset(&sa, 0, sizeof(struct stat)); 178 179 ATF_REQUIRE(path != NULL); 180 ATF_REQUIRE(strncmp(template, "/tmp/mktemp.", 12) == 0); 181 ATF_REQUIRE(stat(path, &sa) == 0); 182 ATF_REQUIRE(check_mode(sa, 0700, 1) == 0); 183 ATF_REQUIRE(rmdir(path) == 0); 184 } 185 186 ATF_TC(mkostemp_basic); 187 ATF_TC_HEAD(mkostemp_basic, tc) 188 { 189 atf_tc_set_md_var(tc, "descr", "A basic test of mkostemp(3)"); 190 } 191 192 ATF_TC_BODY(mkostemp_basic, tc) 193 { 194 static const int flags[] = { 195 O_APPEND, O_DIRECT, 196 O_SHLOCK, O_EXLOCK, 197 O_SYNC, O_CLOEXEC 198 }; 199 200 char template[] = "/tmp/mktemp.XXXXXX"; 201 struct stat sa; 202 size_t i; 203 int fd; 204 205 for (i = 0; i < __arraycount(flags); i++) { 206 207 (void)memset(&sa, 0, sizeof(struct stat)); 208 209 fd = mkostemp(template, flags[i]); 210 211 ATF_REQUIRE(fd != -1); 212 ATF_REQUIRE(strncmp(template, "/tmp/mktemp.", 12) == 0); 213 ATF_REQUIRE(write(fd, "X", 1) == 1); 214 ATF_REQUIRE(fstat(fd, &sa) == 0); 215 ATF_REQUIRE(check_mode(sa, 0600 | flags[i], 0) == 0); 216 ATF_REQUIRE(close(fd) == 0); 217 ATF_REQUIRE(unlink(template) == 0); 218 } 219 } 220 221 ATF_TC(mkostemps_basic); 222 ATF_TC_HEAD(mkostemps_basic, tc) 223 { 224 atf_tc_set_md_var(tc, "descr", "A basic test of mkostemps(3)"); 225 } 226 227 ATF_TC_BODY(mkostemps_basic, tc) 228 { 229 static const int flags[] = { 230 O_APPEND, O_DIRECT, 231 O_SHLOCK, O_EXLOCK, 232 O_SYNC, O_CLOEXEC 233 }; 234 235 char template[] = "/tmp/mktemp.XXXyyy"; 236 char *suffix = strchr(template, 'y'); 237 struct stat sa; 238 size_t i; 239 int fd; 240 241 for (i = 0; i < __arraycount(flags); i++) { 242 243 (void)memset(&sa, 0, sizeof(struct stat)); 244 245 fd = mkostemps(template, 3, flags[i]); 246 247 ATF_REQUIRE(fd != -1); 248 ATF_REQUIRE(strncmp(template, "/tmp/mktemp.", 12) == 0); 249 ATF_REQUIRE(strcmp(suffix, "yyy") == 0); 250 ATF_REQUIRE(write(fd, "X", 1) == 1); 251 ATF_REQUIRE(fstat(fd, &sa) == 0); 252 ATF_REQUIRE(check_mode(sa, 0600 | flags[i], 0) == 0); 253 ATF_REQUIRE(close(fd) == 0); 254 ATF_REQUIRE(unlink(template) == 0); 255 } 256 } 257 258 ATF_TP_ADD_TCS(tp) 259 { 260 261 ATF_TP_ADD_TC(tp, mktemp_not_exist); 262 ATF_TP_ADD_TC(tp, mktemp_large_template); 263 ATF_TP_ADD_TC(tp, mkstemp_basic); 264 ATF_TP_ADD_TC(tp, mkstemps_basic); 265 ATF_TP_ADD_TC(tp, mkdtemp_basic); 266 ATF_TP_ADD_TC(tp, mkostemp_basic); 267 ATF_TP_ADD_TC(tp, mkostemps_basic); 268 269 return atf_no_error(); 270 } 271