etc/protocols - sync with NetBSD-8
[minix.git] / tests / lib / libc / c063 / t_readlinkat.c
blobd354ff59b88608089942e9bff70c418eb1bb1416
1 /* $NetBSD: t_readlinkat.c,v 1.3 2013/03/17 04:46:06 jmmv Exp $ */
3 /*-
4 * Copyright (c) 2012 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Emmanuel Dreyfus.
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.
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.
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_readlinkat.c,v 1.3 2013/03/17 04:46:06 jmmv Exp $");
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>
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"
51 ATF_TC(readlinkat_fd);
52 ATF_TC_HEAD(readlinkat_fd, tc)
54 atf_tc_set_md_var(tc, "descr", "See that readlinkat works with fd");
56 ATF_TC_BODY(readlinkat_fd, tc)
58 int dfd;
59 int fd;
60 ssize_t len;
61 char buf[MAXPATHLEN];
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);
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);
74 ATF_REQUIRE(strcmp(buf, FILE) == 0);
77 ATF_TC(readlinkat_fdcwd);
78 ATF_TC_HEAD(readlinkat_fdcwd, tc)
80 atf_tc_set_md_var(tc, "descr",
81 "See that readlinkat works with fd as AT_FDCWD");
83 ATF_TC_BODY(readlinkat_fdcwd, tc)
85 int fd;
86 ssize_t len;
87 char buf[MAXPATHLEN];
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);
94 len = readlinkat(AT_FDCWD, LINK, buf, sizeof(buf)-1);
95 ATF_REQUIRE(len != -1);
96 buf[len] = 0;
98 ATF_REQUIRE(strcmp(buf, FILE) == 0);
101 ATF_TC(readlinkat_fdcwderr);
102 ATF_TC_HEAD(readlinkat_fdcwderr, tc)
104 atf_tc_set_md_var(tc, "descr",
105 "See that readlinkat fails with fd as AT_FDCWD and bad path");
107 ATF_TC_BODY(readlinkat_fdcwderr, tc)
109 char buf[MAXPATHLEN];
111 ATF_REQUIRE(readlinkat(AT_FDCWD, LINK, buf, sizeof(buf)) == -1);
114 ATF_TC(readlinkat_fderr1);
115 ATF_TC_HEAD(readlinkat_fderr1, tc)
117 atf_tc_set_md_var(tc, "descr", "See that readlinkat fail with bad path");
119 ATF_TC_BODY(readlinkat_fderr1, tc)
121 int dfd;
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);
129 ATF_TC(readlinkat_fderr2);
130 ATF_TC_HEAD(readlinkat_fderr2, tc)
132 atf_tc_set_md_var(tc, "descr", "See that readlinkat fails with fd as -1");
134 ATF_TC_BODY(readlinkat_fderr2, tc)
136 int fd;
137 char buf[MAXPATHLEN];
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);
144 ATF_REQUIRE(readlinkat(-1, LINK, buf, sizeof(buf)) == -1);
147 ATF_TP_ADD_TCS(tp)
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);
156 return atf_no_error();