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