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