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 44 #define DIR "dir" 45 #define FILE "dir/readlinkat" 46 #define BASEFILE "readlinkat" 47 #define LINK "dir/symlink" 48 #define BASELINK "symlink" 49 #define FILEERR "dir/readlinkaterr" 50 51 ATF_TC(readlinkat_fd); 52 ATF_TC_HEAD(readlinkat_fd, tc) 53 { 54 atf_tc_set_md_var(tc, "descr", "See that readlinkat works with fd"); 55 } 56 ATF_TC_BODY(readlinkat_fd, tc) 57 { 58 int dfd; 59 int fd; 60 ssize_t len; 61 char buf[MAXPATHLEN]; 62 63 ATF_REQUIRE(mkdir(DIR, 0755) == 0); 64 ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1); 65 ATF_REQUIRE(close(fd) == 0); 66 ATF_REQUIRE(symlink(FILE, LINK) == 0); 67 68 ATF_REQUIRE((dfd = open(DIR, O_RDONLY, 0)) != -1); 69 len = readlinkat(dfd, BASELINK, buf, sizeof(buf)-1); 70 ATF_REQUIRE(len != -1); 71 buf[len] = 0; 72 ATF_REQUIRE(close(dfd) == 0); 73 74 ATF_REQUIRE(strcmp(buf, FILE) == 0); 75 } 76 77 ATF_TC(readlinkat_fdcwd); 78 ATF_TC_HEAD(readlinkat_fdcwd, tc) 79 { 80 atf_tc_set_md_var(tc, "descr", 81 "See that readlinkat works with fd as AT_FDCWD"); 82 } 83 ATF_TC_BODY(readlinkat_fdcwd, tc) 84 { 85 int fd; 86 ssize_t len; 87 char buf[MAXPATHLEN]; 88 89 ATF_REQUIRE(mkdir(DIR, 0755) == 0); 90 ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1); 91 ATF_REQUIRE(close(fd) == 0); 92 ATF_REQUIRE(symlink(FILE, LINK) == 0); 93 94 len = readlinkat(AT_FDCWD, LINK, buf, sizeof(buf)-1); 95 ATF_REQUIRE(len != -1); 96 buf[len] = 0; 97 98 ATF_REQUIRE(strcmp(buf, FILE) == 0); 99 } 100 101 ATF_TC(readlinkat_fdcwderr); 102 ATF_TC_HEAD(readlinkat_fdcwderr, tc) 103 { 104 atf_tc_set_md_var(tc, "descr", 105 "See that readlinkat fails with fd as AT_FDCWD and bad path"); 106 } 107 ATF_TC_BODY(readlinkat_fdcwderr, tc) 108 { 109 char buf[MAXPATHLEN]; 110 111 ATF_REQUIRE(readlinkat(AT_FDCWD, LINK, buf, sizeof(buf)) == -1); 112 } 113 114 ATF_TC(readlinkat_fderr1); 115 ATF_TC_HEAD(readlinkat_fderr1, tc) 116 { 117 atf_tc_set_md_var(tc, "descr", "See that readlinkat fail with bad path"); 118 } 119 ATF_TC_BODY(readlinkat_fderr1, tc) 120 { 121 int dfd; 122 123 ATF_REQUIRE(mkdir(DIR, 0755) == 0); 124 ATF_REQUIRE((dfd = open(DIR, O_RDONLY, 0)) != -1); 125 ATF_REQUIRE(readlinkat(dfd, FILEERR, F_OK, 0) == -1); 126 ATF_REQUIRE(close(dfd) == 0); 127 } 128 129 ATF_TC(readlinkat_fderr2); 130 ATF_TC_HEAD(readlinkat_fderr2, tc) 131 { 132 atf_tc_set_md_var(tc, "descr", "See that readlinkat fails with fd as -1"); 133 } 134 ATF_TC_BODY(readlinkat_fderr2, tc) 135 { 136 int fd; 137 char buf[MAXPATHLEN]; 138 139 ATF_REQUIRE(mkdir(DIR, 0755) == 0); 140 ATF_REQUIRE((fd = open(FILE, O_CREAT|O_RDWR, 0644)) != -1); 141 ATF_REQUIRE(close(fd) == 0); 142 ATF_REQUIRE(symlink(FILE, LINK) == 0); 143 144 ATF_REQUIRE(readlinkat(-1, LINK, buf, sizeof(buf)) == -1); 145 } 146 147 ATF_TP_ADD_TCS(tp) 148 { 149 150 ATF_TP_ADD_TC(tp, readlinkat_fd); 151 ATF_TP_ADD_TC(tp, readlinkat_fdcwd); 152 ATF_TP_ADD_TC(tp, readlinkat_fdcwderr); 153 ATF_TP_ADD_TC(tp, readlinkat_fderr1); 154 ATF_TP_ADD_TC(tp, readlinkat_fderr2); 155 156 return atf_no_error(); 157 } 158