xref: /xnu-10063.121.3/tests/netbsd_utimensat.c (revision 2c2f96dc2b9a4408a43d3150ae9c105355ca3daa)
1*2c2f96dcSApple OSS Distributions /*	$NetBSD: t_utimensat.c,v 1.6 2017/01/10 15:13:56 christos Exp $ */
2*2c2f96dcSApple OSS Distributions 
3*2c2f96dcSApple OSS Distributions /*-
4*2c2f96dcSApple OSS Distributions  * Copyright (c) 2012 The NetBSD Foundation, Inc.
5*2c2f96dcSApple OSS Distributions  * All rights reserved.
6*2c2f96dcSApple OSS Distributions  *
7*2c2f96dcSApple OSS Distributions  * This code is derived from software contributed to The NetBSD Foundation
8*2c2f96dcSApple OSS Distributions  * by Emmanuel Dreyfus.
9*2c2f96dcSApple OSS Distributions  *
10*2c2f96dcSApple OSS Distributions  * Redistribution and use in source and binary forms, with or without
11*2c2f96dcSApple OSS Distributions  * modification, are permitted provided that the following conditions
12*2c2f96dcSApple OSS Distributions  * are met:
13*2c2f96dcSApple OSS Distributions  * 1. Redistributions of source code must retain the above copyright
14*2c2f96dcSApple OSS Distributions  *    notice, this list of conditions and the following disclaimer.
15*2c2f96dcSApple OSS Distributions  * 2. Redistributions in binary form must reproduce the above copyright
16*2c2f96dcSApple OSS Distributions  *    notice, this list of conditions and the following disclaimer in the
17*2c2f96dcSApple OSS Distributions  *    documentation and/or other materials provided with the distribution.
18*2c2f96dcSApple OSS Distributions  *
19*2c2f96dcSApple OSS Distributions  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*2c2f96dcSApple OSS Distributions  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*2c2f96dcSApple OSS Distributions  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*2c2f96dcSApple OSS Distributions  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*2c2f96dcSApple OSS Distributions  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*2c2f96dcSApple OSS Distributions  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*2c2f96dcSApple OSS Distributions  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*2c2f96dcSApple OSS Distributions  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*2c2f96dcSApple OSS Distributions  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*2c2f96dcSApple OSS Distributions  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*2c2f96dcSApple OSS Distributions  * POSSIBILITY OF SUCH DAMAGE.
30*2c2f96dcSApple OSS Distributions  */
31*2c2f96dcSApple OSS Distributions #include <sys/cdefs.h>
32*2c2f96dcSApple OSS Distributions __RCSID("$NetBSD: t_utimensat.c,v 1.6 2017/01/10 15:13:56 christos Exp $");
33*2c2f96dcSApple OSS Distributions 
34*2c2f96dcSApple OSS Distributions #include <sys/param.h>
35*2c2f96dcSApple OSS Distributions #include <sys/stat.h>
36*2c2f96dcSApple OSS Distributions #include <sys/time.h>
37*2c2f96dcSApple OSS Distributions #include <errno.h>
38*2c2f96dcSApple OSS Distributions #include <fcntl.h>
39*2c2f96dcSApple OSS Distributions #include <limits.h>
40*2c2f96dcSApple OSS Distributions #include <paths.h>
41*2c2f96dcSApple OSS Distributions #include <stdio.h>
42*2c2f96dcSApple OSS Distributions #include <string.h>
43*2c2f96dcSApple OSS Distributions #include <unistd.h>
44*2c2f96dcSApple OSS Distributions 
45*2c2f96dcSApple OSS Distributions #include <darwintest.h>
46*2c2f96dcSApple OSS Distributions #include <darwintest_utils.h>
47*2c2f96dcSApple OSS Distributions 
48*2c2f96dcSApple OSS Distributions #define DIRPATH "dir"
49*2c2f96dcSApple OSS Distributions #define FILEPATH "dir/utimensat"
50*2c2f96dcSApple OSS Distributions #define BASEFILE "utimensat"
51*2c2f96dcSApple OSS Distributions #define LINK "dir/symlink"
52*2c2f96dcSApple OSS Distributions #define BASELINK "symlink"
53*2c2f96dcSApple OSS Distributions #define FILEERR "dir/symlink"
54*2c2f96dcSApple OSS Distributions 
55*2c2f96dcSApple OSS Distributions static const struct timespec tptr[] = {
56*2c2f96dcSApple OSS Distributions 	{ 0x12345678, 987654321 },
57*2c2f96dcSApple OSS Distributions 	{ 0x15263748, 123456789 },
58*2c2f96dcSApple OSS Distributions };
59*2c2f96dcSApple OSS Distributions 
60*2c2f96dcSApple OSS Distributions static void
chtmpdir(void)61*2c2f96dcSApple OSS Distributions chtmpdir(void)
62*2c2f96dcSApple OSS Distributions {
63*2c2f96dcSApple OSS Distributions 	T_SETUPBEGIN;
64*2c2f96dcSApple OSS Distributions 	T_ASSERT_POSIX_ZERO(chdir(dt_tmpdir()), NULL);
65*2c2f96dcSApple OSS Distributions 
66*2c2f96dcSApple OSS Distributions 	// <rdar://problem/31780295> dt_tmpdir() should guarantee a clean directory for each run
67*2c2f96dcSApple OSS Distributions 	unlink(FILEPATH);
68*2c2f96dcSApple OSS Distributions 	unlink(LINK);
69*2c2f96dcSApple OSS Distributions 	rmdir(DIRPATH);
70*2c2f96dcSApple OSS Distributions 
71*2c2f96dcSApple OSS Distributions 	// Skip the test if the current working directory is not on APFS.
72*2c2f96dcSApple OSS Distributions 	struct statfs sfs = { 0 };
73*2c2f96dcSApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(statfs(".", &sfs), NULL);
74*2c2f96dcSApple OSS Distributions 	if (memcmp(&sfs.f_fstypename[0], "apfs", strlen("apfs")) != 0) {
75*2c2f96dcSApple OSS Distributions 		T_SKIP("utimensat is APFS-only, but working directory is non-APFS");
76*2c2f96dcSApple OSS Distributions 	}
77*2c2f96dcSApple OSS Distributions 
78*2c2f96dcSApple OSS Distributions 	T_SETUPEND;
79*2c2f96dcSApple OSS Distributions }
80*2c2f96dcSApple OSS Distributions 
81*2c2f96dcSApple OSS Distributions T_DECL(netbsd_utimensat_fd, "See that utimensat works with fd")
82*2c2f96dcSApple OSS Distributions {
83*2c2f96dcSApple OSS Distributions 	chtmpdir();
84*2c2f96dcSApple OSS Distributions 
85*2c2f96dcSApple OSS Distributions 	int dfd;
86*2c2f96dcSApple OSS Distributions 	int fd;
87*2c2f96dcSApple OSS Distributions 	struct stat st;
88*2c2f96dcSApple OSS Distributions 
89*2c2f96dcSApple OSS Distributions 	T_ASSERT_POSIX_ZERO(mkdir(DIRPATH, 0755), NULL);
90*2c2f96dcSApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS((fd = open(FILEPATH, O_CREAT | O_RDWR, 0644)), NULL);
91*2c2f96dcSApple OSS Distributions 	T_ASSERT_POSIX_ZERO(close(fd), NULL);
92*2c2f96dcSApple OSS Distributions 
93*2c2f96dcSApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS((dfd = open(DIRPATH, O_RDONLY, 0)), NULL);
94*2c2f96dcSApple OSS Distributions 	T_ASSERT_POSIX_ZERO(utimensat(dfd, BASEFILE, tptr, 0), NULL);
95*2c2f96dcSApple OSS Distributions 	T_ASSERT_POSIX_ZERO(close(dfd), NULL);
96*2c2f96dcSApple OSS Distributions 
97*2c2f96dcSApple OSS Distributions 	T_ASSERT_POSIX_ZERO(stat(FILEPATH, &st), NULL);
98*2c2f96dcSApple OSS Distributions 	T_ASSERT_EQ(st.st_atimespec.tv_sec, tptr[0].tv_sec, NULL);
99*2c2f96dcSApple OSS Distributions 	T_ASSERT_EQ(st.st_atimespec.tv_nsec, tptr[0].tv_nsec, NULL);
100*2c2f96dcSApple OSS Distributions 	T_ASSERT_EQ(st.st_mtimespec.tv_sec, tptr[1].tv_sec, NULL);
101*2c2f96dcSApple OSS Distributions 	T_ASSERT_EQ(st.st_mtimespec.tv_nsec, tptr[1].tv_nsec, NULL);
102*2c2f96dcSApple OSS Distributions }
103*2c2f96dcSApple OSS Distributions 
104*2c2f96dcSApple OSS Distributions T_DECL(netbsd_utimensat_fdcwd, "See that utimensat works with fd as AT_FDCWD")
105*2c2f96dcSApple OSS Distributions {
106*2c2f96dcSApple OSS Distributions 	chtmpdir();
107*2c2f96dcSApple OSS Distributions 
108*2c2f96dcSApple OSS Distributions 	int fd;
109*2c2f96dcSApple OSS Distributions 	struct stat st;
110*2c2f96dcSApple OSS Distributions 
111*2c2f96dcSApple OSS Distributions 	T_ASSERT_POSIX_ZERO(mkdir(DIRPATH, 0755), NULL);
112*2c2f96dcSApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS((fd = open(FILEPATH, O_CREAT | O_RDWR, 0644)), NULL);
113*2c2f96dcSApple OSS Distributions 	T_ASSERT_POSIX_ZERO(close(fd), NULL);
114*2c2f96dcSApple OSS Distributions 
115*2c2f96dcSApple OSS Distributions 	T_ASSERT_POSIX_ZERO(chdir(DIRPATH), NULL);
116*2c2f96dcSApple OSS Distributions 	T_ASSERT_POSIX_ZERO(utimensat(AT_FDCWD, BASEFILE, tptr, 0), NULL);
117*2c2f96dcSApple OSS Distributions 
118*2c2f96dcSApple OSS Distributions 	T_ASSERT_POSIX_ZERO(stat(BASEFILE, &st), NULL);
119*2c2f96dcSApple OSS Distributions 	T_ASSERT_EQ(st.st_atimespec.tv_sec, tptr[0].tv_sec, NULL);
120*2c2f96dcSApple OSS Distributions 	T_ASSERT_EQ(st.st_atimespec.tv_nsec, tptr[0].tv_nsec, NULL);
121*2c2f96dcSApple OSS Distributions 	T_ASSERT_EQ(st.st_mtimespec.tv_sec, tptr[1].tv_sec, NULL);
122*2c2f96dcSApple OSS Distributions 	T_ASSERT_EQ(st.st_mtimespec.tv_nsec, tptr[1].tv_nsec, NULL);
123*2c2f96dcSApple OSS Distributions }
124*2c2f96dcSApple OSS Distributions 
125*2c2f96dcSApple OSS Distributions T_DECL(netbsd_utimensat_fdcwderr, "See that utimensat fails with fd as AT_FDCWD and bad path")
126*2c2f96dcSApple OSS Distributions {
127*2c2f96dcSApple OSS Distributions 	chtmpdir();
128*2c2f96dcSApple OSS Distributions 
129*2c2f96dcSApple OSS Distributions 	T_ASSERT_POSIX_ZERO(mkdir(DIRPATH, 0755), NULL);
130*2c2f96dcSApple OSS Distributions 	T_ASSERT_EQ(utimensat(AT_FDCWD, FILEERR, tptr, 0), -1, NULL);
131*2c2f96dcSApple OSS Distributions }
132*2c2f96dcSApple OSS Distributions 
133*2c2f96dcSApple OSS Distributions T_DECL(netbsd_utimensat_fderr1, "See that utimensat fail with bad path")
134*2c2f96dcSApple OSS Distributions {
135*2c2f96dcSApple OSS Distributions 	chtmpdir();
136*2c2f96dcSApple OSS Distributions 
137*2c2f96dcSApple OSS Distributions 	int dfd;
138*2c2f96dcSApple OSS Distributions 
139*2c2f96dcSApple OSS Distributions 	T_ASSERT_POSIX_ZERO(mkdir(DIRPATH, 0755), NULL);
140*2c2f96dcSApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS((dfd = open(DIRPATH, O_RDONLY, 0)), NULL);
141*2c2f96dcSApple OSS Distributions 	T_ASSERT_EQ(utimensat(dfd, FILEERR, tptr, 0), -1, NULL);
142*2c2f96dcSApple OSS Distributions 	T_ASSERT_POSIX_ZERO(close(dfd), NULL);
143*2c2f96dcSApple OSS Distributions }
144*2c2f96dcSApple OSS Distributions 
145*2c2f96dcSApple OSS Distributions T_DECL(netbsd_utimensat_fderr2, "See that utimensat fails with bad fdat")
146*2c2f96dcSApple OSS Distributions {
147*2c2f96dcSApple OSS Distributions 	chtmpdir();
148*2c2f96dcSApple OSS Distributions 
149*2c2f96dcSApple OSS Distributions 	int dfd;
150*2c2f96dcSApple OSS Distributions 	int fd;
151*2c2f96dcSApple OSS Distributions 	char cwd[MAXPATHLEN];
152*2c2f96dcSApple OSS Distributions 
153*2c2f96dcSApple OSS Distributions 	T_ASSERT_POSIX_ZERO(mkdir(DIRPATH, 0755), NULL);
154*2c2f96dcSApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS((fd = open(FILEPATH, O_CREAT | O_RDWR, 0644)), NULL);
155*2c2f96dcSApple OSS Distributions 	T_ASSERT_POSIX_ZERO(close(fd), NULL);
156*2c2f96dcSApple OSS Distributions 
157*2c2f96dcSApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS((dfd = open(getcwd(cwd, MAXPATHLEN), O_RDONLY, 0)), NULL);
158*2c2f96dcSApple OSS Distributions 	T_ASSERT_EQ(utimensat(dfd, BASEFILE, tptr, 0), -1, NULL);
159*2c2f96dcSApple OSS Distributions 	T_ASSERT_POSIX_ZERO(close(dfd), NULL);
160*2c2f96dcSApple OSS Distributions }
161*2c2f96dcSApple OSS Distributions 
162*2c2f96dcSApple OSS Distributions T_DECL(netbsd_utimensat_fderr3, "See that utimensat fails with fd as -1")
163*2c2f96dcSApple OSS Distributions {
164*2c2f96dcSApple OSS Distributions 	chtmpdir();
165*2c2f96dcSApple OSS Distributions 
166*2c2f96dcSApple OSS Distributions 	int fd;
167*2c2f96dcSApple OSS Distributions 
168*2c2f96dcSApple OSS Distributions 	T_ASSERT_POSIX_ZERO(mkdir(DIRPATH, 0755), NULL);
169*2c2f96dcSApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS((fd = open(FILEPATH, O_CREAT | O_RDWR, 0644)), NULL);
170*2c2f96dcSApple OSS Distributions 	T_ASSERT_POSIX_ZERO(close(fd), NULL);
171*2c2f96dcSApple OSS Distributions 
172*2c2f96dcSApple OSS Distributions 	T_ASSERT_EQ(utimensat(-1, FILEPATH, tptr, 0), -1, NULL);
173*2c2f96dcSApple OSS Distributions }
174*2c2f96dcSApple OSS Distributions 
175*2c2f96dcSApple OSS Distributions T_DECL(netbsd_utimensat_fdlink, "See that utimensat works on symlink")
176*2c2f96dcSApple OSS Distributions {
177*2c2f96dcSApple OSS Distributions 	chtmpdir();
178*2c2f96dcSApple OSS Distributions 
179*2c2f96dcSApple OSS Distributions 	int dfd;
180*2c2f96dcSApple OSS Distributions 	struct stat st;
181*2c2f96dcSApple OSS Distributions 
182*2c2f96dcSApple OSS Distributions 	T_ASSERT_POSIX_ZERO(mkdir(DIRPATH, 0755), NULL);
183*2c2f96dcSApple OSS Distributions 	T_ASSERT_POSIX_ZERO(symlink(FILEPATH, LINK), NULL); /* NB: FILE does not exists */
184*2c2f96dcSApple OSS Distributions 
185*2c2f96dcSApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS((dfd = open(DIRPATH, O_RDONLY, 0)), NULL);
186*2c2f96dcSApple OSS Distributions 
187*2c2f96dcSApple OSS Distributions 	T_ASSERT_EQ(utimensat(dfd, BASELINK, tptr, 0), -1, NULL);
188*2c2f96dcSApple OSS Distributions 	T_ASSERT_EQ(errno, ENOENT, NULL);
189*2c2f96dcSApple OSS Distributions 
190*2c2f96dcSApple OSS Distributions 	T_ASSERT_POSIX_ZERO(utimensat(dfd, BASELINK, tptr, AT_SYMLINK_NOFOLLOW), NULL);
191*2c2f96dcSApple OSS Distributions 
192*2c2f96dcSApple OSS Distributions 	T_ASSERT_POSIX_ZERO(close(dfd), NULL);
193*2c2f96dcSApple OSS Distributions 
194*2c2f96dcSApple OSS Distributions 	T_ASSERT_POSIX_ZERO(lstat(LINK, &st), NULL);
195*2c2f96dcSApple OSS Distributions 	T_ASSERT_EQ(st.st_atimespec.tv_sec, tptr[0].tv_sec, NULL);
196*2c2f96dcSApple OSS Distributions 	T_ASSERT_EQ(st.st_atimespec.tv_nsec, tptr[0].tv_nsec, NULL);
197*2c2f96dcSApple OSS Distributions 	T_ASSERT_EQ(st.st_mtimespec.tv_sec, tptr[1].tv_sec, NULL);
198*2c2f96dcSApple OSS Distributions 	T_ASSERT_EQ(st.st_mtimespec.tv_nsec, tptr[1].tv_nsec, NULL);
199*2c2f96dcSApple OSS Distributions }
200