]> git.saurik.com Git - apple/xnu.git/blob - tools/tests/darwintests/netbsd_utimensat.c
xnu-4570.41.2.tar.gz
[apple/xnu.git] / tools / tests / darwintests / netbsd_utimensat.c
1 /* $NetBSD: t_utimensat.c,v 1.6 2017/01/10 15:13:56 christos 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_utimensat.c,v 1.6 2017/01/10 15:13:56 christos Exp $");
33
34 #include <sys/param.h>
35 #include <sys/stat.h>
36 #include <sys/time.h>
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <limits.h>
40 #include <paths.h>
41 #include <stdio.h>
42 #include <string.h>
43 #include <unistd.h>
44
45 #include <darwintest.h>
46 #include <darwintest_utils.h>
47
48 #define DIR "dir"
49 #define FILE "dir/utimensat"
50 #define BASEFILE "utimensat"
51 #define LINK "dir/symlink"
52 #define BASELINK "symlink"
53 #define FILEERR "dir/symlink"
54
55 static const struct timespec tptr[] = {
56 { 0x12345678, 987654321 },
57 { 0x15263748, 123456789 },
58 };
59
60 static void chtmpdir(void)
61 {
62 T_SETUPBEGIN;
63 T_ASSERT_POSIX_ZERO(chdir(dt_tmpdir()), NULL);
64
65 // <rdar://problem/31780295> dt_tmpdir() should guarantee a clean directory for each run
66 unlink(FILE);
67 unlink(LINK);
68 rmdir(DIR);
69
70 T_SETUPEND;
71 }
72
73 T_DECL(netbsd_utimensat_fd, "See that utimensat works with fd")
74 {
75 chtmpdir();
76
77 int dfd;
78 int fd;
79 struct stat st;
80
81 T_ASSERT_POSIX_ZERO(mkdir(DIR, 0755), NULL);
82 T_ASSERT_POSIX_SUCCESS((fd = open(FILE, O_CREAT|O_RDWR, 0644)), NULL);
83 T_ASSERT_POSIX_ZERO(close(fd), NULL);
84
85 T_ASSERT_POSIX_SUCCESS((dfd = open(DIR, O_RDONLY, 0)), NULL);
86 T_ASSERT_POSIX_ZERO(utimensat(dfd, BASEFILE, tptr, 0), NULL);
87 T_ASSERT_POSIX_ZERO(close(dfd), NULL);
88
89 T_ASSERT_POSIX_ZERO(stat(FILE, &st), NULL);
90 T_ASSERT_EQ(st.st_atimespec.tv_sec, tptr[0].tv_sec, NULL);
91 T_ASSERT_EQ(st.st_atimespec.tv_nsec, tptr[0].tv_nsec, NULL);
92 T_ASSERT_EQ(st.st_mtimespec.tv_sec, tptr[1].tv_sec, NULL);
93 T_ASSERT_EQ(st.st_mtimespec.tv_nsec, tptr[1].tv_nsec, NULL);
94 }
95
96 T_DECL(netbsd_utimensat_fdcwd, "See that utimensat works with fd as AT_FDCWD")
97 {
98 chtmpdir();
99
100 int fd;
101 struct stat st;
102
103 T_ASSERT_POSIX_ZERO(mkdir(DIR, 0755), NULL);
104 T_ASSERT_POSIX_SUCCESS((fd = open(FILE, O_CREAT|O_RDWR, 0644)), NULL);
105 T_ASSERT_POSIX_ZERO(close(fd), NULL);
106
107 T_ASSERT_POSIX_ZERO(chdir(DIR), NULL);
108 T_ASSERT_POSIX_ZERO(utimensat(AT_FDCWD, BASEFILE, tptr, 0), NULL);
109
110 T_ASSERT_POSIX_ZERO(stat(BASEFILE, &st), NULL);
111 T_ASSERT_EQ(st.st_atimespec.tv_sec, tptr[0].tv_sec, NULL);
112 T_ASSERT_EQ(st.st_atimespec.tv_nsec, tptr[0].tv_nsec, NULL);
113 T_ASSERT_EQ(st.st_mtimespec.tv_sec, tptr[1].tv_sec, NULL);
114 T_ASSERT_EQ(st.st_mtimespec.tv_nsec, tptr[1].tv_nsec, NULL);
115 }
116
117 T_DECL(netbsd_utimensat_fdcwderr, "See that utimensat fails with fd as AT_FDCWD and bad path")
118 {
119 chtmpdir();
120
121 T_ASSERT_POSIX_ZERO(mkdir(DIR, 0755), NULL);
122 T_ASSERT_EQ(utimensat(AT_FDCWD, FILEERR, tptr, 0), -1, NULL);
123 }
124
125 T_DECL(netbsd_utimensat_fderr1, "See that utimensat fail with bad path")
126 {
127 chtmpdir();
128
129 int dfd;
130
131 T_ASSERT_POSIX_ZERO(mkdir(DIR, 0755), NULL);
132 T_ASSERT_POSIX_SUCCESS((dfd = open(DIR, O_RDONLY, 0)), NULL);
133 T_ASSERT_EQ(utimensat(dfd, FILEERR, tptr, 0), -1, NULL);
134 T_ASSERT_POSIX_ZERO(close(dfd), NULL);
135 }
136
137 T_DECL(netbsd_utimensat_fderr2, "See that utimensat fails with bad fdat")
138 {
139 chtmpdir();
140
141 int dfd;
142 int fd;
143 char cwd[MAXPATHLEN];
144
145 T_ASSERT_POSIX_ZERO(mkdir(DIR, 0755), NULL);
146 T_ASSERT_POSIX_SUCCESS((fd = open(FILE, O_CREAT|O_RDWR, 0644)), NULL);
147 T_ASSERT_POSIX_ZERO(close(fd), NULL);
148
149 T_ASSERT_POSIX_SUCCESS((dfd = open(getcwd(cwd, MAXPATHLEN), O_RDONLY, 0)), NULL);
150 T_ASSERT_EQ(utimensat(dfd, BASEFILE, tptr, 0), -1, NULL);
151 T_ASSERT_POSIX_ZERO(close(dfd), NULL);
152 }
153
154 T_DECL(netbsd_utimensat_fderr3, "See that utimensat fails with fd as -1")
155 {
156 chtmpdir();
157
158 int fd;
159
160 T_ASSERT_POSIX_ZERO(mkdir(DIR, 0755), NULL);
161 T_ASSERT_POSIX_SUCCESS((fd = open(FILE, O_CREAT|O_RDWR, 0644)), NULL);
162 T_ASSERT_POSIX_ZERO(close(fd), NULL);
163
164 T_ASSERT_EQ(utimensat(-1, FILE, tptr, 0), -1, NULL);
165 }
166
167 T_DECL(netbsd_utimensat_fdlink, "See that utimensat works on symlink")
168 {
169 chtmpdir();
170
171 int dfd;
172 struct stat st;
173
174 T_ASSERT_POSIX_ZERO(mkdir(DIR, 0755), NULL);
175 T_ASSERT_POSIX_ZERO(symlink(FILE, LINK), NULL); /* NB: FILE does not exists */
176
177 T_ASSERT_POSIX_SUCCESS((dfd = open(DIR, O_RDONLY, 0)), NULL);
178
179 T_ASSERT_EQ(utimensat(dfd, BASELINK, tptr, 0), -1, NULL);
180 T_ASSERT_EQ(errno, ENOENT, NULL);
181
182 T_ASSERT_POSIX_ZERO(utimensat(dfd, BASELINK, tptr, AT_SYMLINK_NOFOLLOW), NULL);
183
184 T_ASSERT_POSIX_ZERO(close(dfd), NULL);
185
186 T_ASSERT_POSIX_ZERO(lstat(LINK, &st), NULL);
187 T_ASSERT_EQ(st.st_atimespec.tv_sec, tptr[0].tv_sec, NULL);
188 T_ASSERT_EQ(st.st_atimespec.tv_nsec, tptr[0].tv_nsec, NULL);
189 T_ASSERT_EQ(st.st_mtimespec.tv_sec, tptr[1].tv_sec, NULL);
190 T_ASSERT_EQ(st.st_mtimespec.tv_nsec, tptr[1].tv_nsec, NULL);
191 }