xref: /xnu-11417.140.69/tests/netbsd_utimensat.c (revision 43a90889846e00bfb5cf1d255cdc0a701a1e05a4)
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 DIRPATH "dir"
49 #define FILEPATH "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 chtmpdir(void)
62 {
63 	T_SETUPBEGIN;
64 	T_ASSERT_POSIX_SUCCESS(chdir(dt_tmpdir()), NULL);
65 
66 	// <rdar://problem/31780295> dt_tmpdir() should guarantee a clean directory for each run
67 	unlink(FILEPATH);
68 	unlink(LINK);
69 	rmdir(DIRPATH);
70 
71 	// Skip the test if the current working directory is not on APFS.
72 	struct statfs sfs = { 0 };
73 	T_QUIET; T_ASSERT_POSIX_SUCCESS(statfs(".", &sfs), NULL);
74 	if (memcmp(&sfs.f_fstypename[0], "apfs", strlen("apfs")) != 0) {
75 		T_SKIP("utimensat is APFS-only, but working directory is non-APFS");
76 	}
77 
78 	T_SETUPEND;
79 }
80 
81 T_DECL(netbsd_utimensat_fd, "See that utimensat works with fd")
82 {
83 	chtmpdir();
84 
85 	int dfd;
86 	int fd;
87 	struct stat st;
88 
89 	T_ASSERT_POSIX_SUCCESS(mkdir(DIRPATH, 0755), NULL);
90 	T_ASSERT_POSIX_SUCCESS((fd = open(FILEPATH, O_CREAT | O_RDWR, 0644)), NULL);
91 	T_ASSERT_POSIX_SUCCESS(close(fd), NULL);
92 
93 	T_ASSERT_POSIX_SUCCESS((dfd = open(DIRPATH, O_RDONLY, 0)), NULL);
94 	T_ASSERT_POSIX_SUCCESS(utimensat(dfd, BASEFILE, tptr, 0), NULL);
95 	T_ASSERT_POSIX_SUCCESS(close(dfd), NULL);
96 
97 	T_ASSERT_POSIX_SUCCESS(stat(FILEPATH, &st), NULL);
98 	T_ASSERT_EQ(st.st_atimespec.tv_sec, tptr[0].tv_sec, NULL);
99 	T_ASSERT_EQ(st.st_atimespec.tv_nsec, tptr[0].tv_nsec, NULL);
100 	T_ASSERT_EQ(st.st_mtimespec.tv_sec, tptr[1].tv_sec, NULL);
101 	T_ASSERT_EQ(st.st_mtimespec.tv_nsec, tptr[1].tv_nsec, NULL);
102 }
103 
104 T_DECL(netbsd_utimensat_fdcwd, "See that utimensat works with fd as AT_FDCWD")
105 {
106 	chtmpdir();
107 
108 	int fd;
109 	struct stat st;
110 
111 	T_ASSERT_POSIX_SUCCESS(mkdir(DIRPATH, 0755), NULL);
112 	T_ASSERT_POSIX_SUCCESS((fd = open(FILEPATH, O_CREAT | O_RDWR, 0644)), NULL);
113 	T_ASSERT_POSIX_SUCCESS(close(fd), NULL);
114 
115 	T_ASSERT_POSIX_SUCCESS(chdir(DIRPATH), NULL);
116 	T_ASSERT_POSIX_SUCCESS(utimensat(AT_FDCWD, BASEFILE, tptr, 0), NULL);
117 
118 	T_ASSERT_POSIX_SUCCESS(stat(BASEFILE, &st), NULL);
119 	T_ASSERT_EQ(st.st_atimespec.tv_sec, tptr[0].tv_sec, NULL);
120 	T_ASSERT_EQ(st.st_atimespec.tv_nsec, tptr[0].tv_nsec, NULL);
121 	T_ASSERT_EQ(st.st_mtimespec.tv_sec, tptr[1].tv_sec, NULL);
122 	T_ASSERT_EQ(st.st_mtimespec.tv_nsec, tptr[1].tv_nsec, NULL);
123 }
124 
125 T_DECL(netbsd_utimensat_fdcwderr, "See that utimensat fails with fd as AT_FDCWD and bad path")
126 {
127 	chtmpdir();
128 
129 	T_ASSERT_POSIX_SUCCESS(mkdir(DIRPATH, 0755), NULL);
130 	T_ASSERT_EQ(utimensat(AT_FDCWD, FILEERR, tptr, 0), -1, NULL);
131 }
132 
133 T_DECL(netbsd_utimensat_fderr1, "See that utimensat fail with bad path")
134 {
135 	chtmpdir();
136 
137 	int dfd;
138 
139 	T_ASSERT_POSIX_SUCCESS(mkdir(DIRPATH, 0755), NULL);
140 	T_ASSERT_POSIX_SUCCESS((dfd = open(DIRPATH, O_RDONLY, 0)), NULL);
141 	T_ASSERT_EQ(utimensat(dfd, FILEERR, tptr, 0), -1, NULL);
142 	T_ASSERT_POSIX_SUCCESS(close(dfd), NULL);
143 }
144 
145 T_DECL(netbsd_utimensat_fderr2, "See that utimensat fails with bad fdat")
146 {
147 	chtmpdir();
148 
149 	int dfd;
150 	int fd;
151 	char cwd[MAXPATHLEN];
152 
153 	T_ASSERT_POSIX_SUCCESS(mkdir(DIRPATH, 0755), NULL);
154 	T_ASSERT_POSIX_SUCCESS((fd = open(FILEPATH, O_CREAT | O_RDWR, 0644)), NULL);
155 	T_ASSERT_POSIX_SUCCESS(close(fd), NULL);
156 
157 	T_ASSERT_POSIX_SUCCESS((dfd = open(getcwd(cwd, MAXPATHLEN), O_RDONLY, 0)), NULL);
158 	T_ASSERT_EQ(utimensat(dfd, BASEFILE, tptr, 0), -1, NULL);
159 	T_ASSERT_POSIX_SUCCESS(close(dfd), NULL);
160 }
161 
162 T_DECL(netbsd_utimensat_fderr3, "See that utimensat fails with fd as -1")
163 {
164 	chtmpdir();
165 
166 	int fd;
167 
168 	T_ASSERT_POSIX_SUCCESS(mkdir(DIRPATH, 0755), NULL);
169 	T_ASSERT_POSIX_SUCCESS((fd = open(FILEPATH, O_CREAT | O_RDWR, 0644)), NULL);
170 	T_ASSERT_POSIX_SUCCESS(close(fd), NULL);
171 
172 	T_ASSERT_EQ(utimensat(-1, FILEPATH, tptr, 0), -1, NULL);
173 }
174 
175 T_DECL(netbsd_utimensat_fdlink, "See that utimensat works on symlink")
176 {
177 	chtmpdir();
178 
179 	int dfd;
180 	struct stat st;
181 
182 	T_ASSERT_POSIX_SUCCESS(mkdir(DIRPATH, 0755), NULL);
183 	T_ASSERT_POSIX_SUCCESS(symlink(FILEPATH, LINK), NULL); /* NB: FILE does not exists */
184 
185 	T_ASSERT_POSIX_SUCCESS((dfd = open(DIRPATH, O_RDONLY, 0)), NULL);
186 
187 	T_ASSERT_EQ(utimensat(dfd, BASELINK, tptr, 0), -1, NULL);
188 	T_ASSERT_EQ(errno, ENOENT, NULL);
189 
190 	T_ASSERT_POSIX_SUCCESS(utimensat(dfd, BASELINK, tptr, AT_SYMLINK_NOFOLLOW), NULL);
191 
192 	T_ASSERT_POSIX_SUCCESS(close(dfd), NULL);
193 
194 	T_ASSERT_POSIX_SUCCESS(lstat(LINK, &st), NULL);
195 	T_ASSERT_EQ(st.st_atimespec.tv_sec, tptr[0].tv_sec, NULL);
196 	T_ASSERT_EQ(st.st_atimespec.tv_nsec, tptr[0].tv_nsec, NULL);
197 	T_ASSERT_EQ(st.st_mtimespec.tv_sec, tptr[1].tv_sec, NULL);
198 	T_ASSERT_EQ(st.st_mtimespec.tv_nsec, tptr[1].tv_nsec, NULL);
199 }
200