1 /* $NetBSD: t_readlinkat.c,v 1.3 2013/03/17 04:46:06 jmmv Exp $ */ 2 3 /*- 4 * Copyright (c) 2012 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Emmanuel Dreyfus. 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_readlinkat.c,v 1.3 2013/03/17 04:46:06 jmmv Exp $"); 33 34 #include <atf-c.h> 35 #include <errno.h> 36 #include <fcntl.h> 37 #include <limits.h> 38 #include <paths.h> 39 #include <stdio.h> 40 #include <string.h> 41 #include <unistd.h> 42 #include <sys/param.h> 43 #ifdef __FreeBSD__ 44 #include <sys/stat.h> 45 #endif 46 47 #define DIR "dir" 48 #define FILE "dir/readlinkat" 49 #define BASEFILE "readlinkat" 50 #define LINK "dir/symlink" 51 #define BASELINK "symlink" 52 #define FILEERR "dir/readlinkaterr" 53 54 ATF_TC(readlinkat_fd); 55 ATF_TC_HEAD(readlinkat_fd, tc) 56 { 57 atf_tc_set_md_var(tc, "descr", "See that readlinkat works with fd"); 58 } 59 ATF_TC_BODY(readlinkat_fd, tc) 60 { 61 int dfd; 62 int fd; 63 ssize_t len; 64 char buf[MAXPATHLEN]; 65 66 ATF_REQUIRE(mkdir(DIR, 0755) == 0); 67 ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1); 68 ATF_REQUIRE(close(fd) == 0); 69 ATF_REQUIRE(symlink(FILE, LINK) == 0); 70 71 ATF_REQUIRE((dfd = open(DIR, O_RDONLY, 0)) != -1); 72 len = readlinkat(dfd, BASELINK, buf, sizeof(buf)-1); 73 ATF_REQUIRE(len != -1); 74 buf[len] = 0; 75 ATF_REQUIRE(close(dfd) == 0); 76 77 ATF_REQUIRE(strcmp(buf, FILE) == 0); 78 } 79 80 ATF_TC(readlinkat_fdcwd); 81 ATF_TC_HEAD(readlinkat_fdcwd, tc) 82 { 83 atf_tc_set_md_var(tc, "descr", 84 "See that readlinkat works with fd as AT_FDCWD"); 85 } 86 ATF_TC_BODY(readlinkat_fdcwd, tc) 87 { 88 int fd; 89 ssize_t len; 90 char buf[MAXPATHLEN]; 91 92 ATF_REQUIRE(mkdir(DIR, 0755) == 0); 93 ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1); 94 ATF_REQUIRE(close(fd) == 0); 95 ATF_REQUIRE(symlink(FILE, LINK) == 0); 96 97 len = readlinkat(AT_FDCWD, LINK, buf, sizeof(buf)-1); 98 ATF_REQUIRE(len != -1); 99 buf[len] = 0; 100 101 ATF_REQUIRE(strcmp(buf, FILE) == 0); 102 } 103 104 ATF_TC(readlinkat_fdcwderr); 105 ATF_TC_HEAD(readlinkat_fdcwderr, tc) 106 { 107 atf_tc_set_md_var(tc, "descr", 108 "See that readlinkat fails with fd as AT_FDCWD and bad path"); 109 } 110 ATF_TC_BODY(readlinkat_fdcwderr, tc) 111 { 112 char buf[MAXPATHLEN]; 113 114 ATF_REQUIRE(readlinkat(AT_FDCWD, LINK, buf, sizeof(buf)) == -1); 115 } 116 117 ATF_TC(readlinkat_fderr1); 118 ATF_TC_HEAD(readlinkat_fderr1, tc) 119 { 120 atf_tc_set_md_var(tc, "descr", "See that readlinkat fail with bad path"); 121 } 122 ATF_TC_BODY(readlinkat_fderr1, tc) 123 { 124 int dfd; 125 126 ATF_REQUIRE(mkdir(DIR, 0755) == 0); 127 ATF_REQUIRE((dfd = open(DIR, O_RDONLY, 0)) != -1); 128 ATF_REQUIRE(readlinkat(dfd, FILEERR, F_OK, 0) == -1); 129 ATF_REQUIRE(close(dfd) == 0); 130 } 131 132 ATF_TC(readlinkat_fderr2); 133 ATF_TC_HEAD(readlinkat_fderr2, tc) 134 { 135 atf_tc_set_md_var(tc, "descr", "See that readlinkat fails with fd as -1"); 136 } 137 ATF_TC_BODY(readlinkat_fderr2, tc) 138 { 139 int fd; 140 char buf[MAXPATHLEN]; 141 142 ATF_REQUIRE(mkdir(DIR, 0755) == 0); 143 ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1); 144 ATF_REQUIRE(close(fd) == 0); 145 ATF_REQUIRE(symlink(FILE, LINK) == 0); 146 147 ATF_REQUIRE(readlinkat(-1, LINK, buf, sizeof(buf)) == -1); 148 } 149 150 ATF_TP_ADD_TCS(tp) 151 { 152 153 ATF_TP_ADD_TC(tp, readlinkat_fd); 154 ATF_TP_ADD_TC(tp, readlinkat_fdcwd); 155 ATF_TP_ADD_TC(tp, readlinkat_fdcwderr); 156 ATF_TP_ADD_TC(tp, readlinkat_fderr1); 157 ATF_TP_ADD_TC(tp, readlinkat_fderr2); 158 159 return atf_no_error(); 160 } 161