1 /* $NetBSD: t_utimensat.c,v 1.6 2017/01/10 15:13:56 christos Exp $ */
4 * Copyright (c) 2012 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_utimensat.c,v 1.6 2017/01/10 15:13:56 christos Exp $");
34 #include <sys/param.h>
45 #include <darwintest.h>
46 #include <darwintest_utils.h>
49 #define FILEPATH "dir/utimensat"
50 #define BASEFILE "utimensat"
51 #define LINK "dir/symlink"
52 #define BASELINK "symlink"
53 #define FILEERR "dir/symlink"
55 static const struct timespec tptr
[] = {
56 { 0x12345678, 987654321 },
57 { 0x15263748, 123456789 },
60 static void chtmpdir(void)
63 T_ASSERT_POSIX_ZERO(chdir(dt_tmpdir()), NULL
);
65 // <rdar://problem/31780295> dt_tmpdir() should guarantee a clean directory for each run
70 // Skip the test if the current working directory is not on APFS.
71 struct statfs sfs
= { 0 };
72 T_QUIET
; T_ASSERT_POSIX_SUCCESS(statfs(".", &sfs
), NULL
);
73 if (memcmp(&sfs
.f_fstypename
[0], "apfs", strlen("apfs")) != 0) {
74 T_SKIP("utimensat is APFS-only, but working directory is non-APFS");
80 T_DECL(netbsd_utimensat_fd
, "See that utimensat works with fd")
88 T_ASSERT_POSIX_ZERO(mkdir(DIRPATH
, 0755), NULL
);
89 T_ASSERT_POSIX_SUCCESS((fd
= open(FILEPATH
, O_CREAT
|O_RDWR
, 0644)), NULL
);
90 T_ASSERT_POSIX_ZERO(close(fd
), NULL
);
92 T_ASSERT_POSIX_SUCCESS((dfd
= open(DIRPATH
, O_RDONLY
, 0)), NULL
);
93 T_ASSERT_POSIX_ZERO(utimensat(dfd
, BASEFILE
, tptr
, 0), NULL
);
94 T_ASSERT_POSIX_ZERO(close(dfd
), NULL
);
96 T_ASSERT_POSIX_ZERO(stat(FILEPATH
, &st
), NULL
);
97 T_ASSERT_EQ(st
.st_atimespec
.tv_sec
, tptr
[0].tv_sec
, NULL
);
98 T_ASSERT_EQ(st
.st_atimespec
.tv_nsec
, tptr
[0].tv_nsec
, NULL
);
99 T_ASSERT_EQ(st
.st_mtimespec
.tv_sec
, tptr
[1].tv_sec
, NULL
);
100 T_ASSERT_EQ(st
.st_mtimespec
.tv_nsec
, tptr
[1].tv_nsec
, NULL
);
103 T_DECL(netbsd_utimensat_fdcwd
, "See that utimensat works with fd as AT_FDCWD")
110 T_ASSERT_POSIX_ZERO(mkdir(DIRPATH
, 0755), NULL
);
111 T_ASSERT_POSIX_SUCCESS((fd
= open(FILEPATH
, O_CREAT
|O_RDWR
, 0644)), NULL
);
112 T_ASSERT_POSIX_ZERO(close(fd
), NULL
);
114 T_ASSERT_POSIX_ZERO(chdir(DIRPATH
), NULL
);
115 T_ASSERT_POSIX_ZERO(utimensat(AT_FDCWD
, BASEFILE
, tptr
, 0), NULL
);
117 T_ASSERT_POSIX_ZERO(stat(BASEFILE
, &st
), NULL
);
118 T_ASSERT_EQ(st
.st_atimespec
.tv_sec
, tptr
[0].tv_sec
, NULL
);
119 T_ASSERT_EQ(st
.st_atimespec
.tv_nsec
, tptr
[0].tv_nsec
, NULL
);
120 T_ASSERT_EQ(st
.st_mtimespec
.tv_sec
, tptr
[1].tv_sec
, NULL
);
121 T_ASSERT_EQ(st
.st_mtimespec
.tv_nsec
, tptr
[1].tv_nsec
, NULL
);
124 T_DECL(netbsd_utimensat_fdcwderr
, "See that utimensat fails with fd as AT_FDCWD and bad path")
128 T_ASSERT_POSIX_ZERO(mkdir(DIRPATH
, 0755), NULL
);
129 T_ASSERT_EQ(utimensat(AT_FDCWD
, FILEERR
, tptr
, 0), -1, NULL
);
132 T_DECL(netbsd_utimensat_fderr1
, "See that utimensat fail with bad path")
138 T_ASSERT_POSIX_ZERO(mkdir(DIRPATH
, 0755), NULL
);
139 T_ASSERT_POSIX_SUCCESS((dfd
= open(DIRPATH
, O_RDONLY
, 0)), NULL
);
140 T_ASSERT_EQ(utimensat(dfd
, FILEERR
, tptr
, 0), -1, NULL
);
141 T_ASSERT_POSIX_ZERO(close(dfd
), NULL
);
144 T_DECL(netbsd_utimensat_fderr2
, "See that utimensat fails with bad fdat")
150 char cwd
[MAXPATHLEN
];
152 T_ASSERT_POSIX_ZERO(mkdir(DIRPATH
, 0755), NULL
);
153 T_ASSERT_POSIX_SUCCESS((fd
= open(FILEPATH
, O_CREAT
|O_RDWR
, 0644)), NULL
);
154 T_ASSERT_POSIX_ZERO(close(fd
), NULL
);
156 T_ASSERT_POSIX_SUCCESS((dfd
= open(getcwd(cwd
, MAXPATHLEN
), O_RDONLY
, 0)), NULL
);
157 T_ASSERT_EQ(utimensat(dfd
, BASEFILE
, tptr
, 0), -1, NULL
);
158 T_ASSERT_POSIX_ZERO(close(dfd
), NULL
);
161 T_DECL(netbsd_utimensat_fderr3
, "See that utimensat fails with fd as -1")
167 T_ASSERT_POSIX_ZERO(mkdir(DIRPATH
, 0755), NULL
);
168 T_ASSERT_POSIX_SUCCESS((fd
= open(FILEPATH
, O_CREAT
|O_RDWR
, 0644)), NULL
);
169 T_ASSERT_POSIX_ZERO(close(fd
), NULL
);
171 T_ASSERT_EQ(utimensat(-1, FILEPATH
, tptr
, 0), -1, NULL
);
174 T_DECL(netbsd_utimensat_fdlink
, "See that utimensat works on symlink")
181 T_ASSERT_POSIX_ZERO(mkdir(DIRPATH
, 0755), NULL
);
182 T_ASSERT_POSIX_ZERO(symlink(FILEPATH
, LINK
), NULL
); /* NB: FILE does not exists */
184 T_ASSERT_POSIX_SUCCESS((dfd
= open(DIRPATH
, O_RDONLY
, 0)), NULL
);
186 T_ASSERT_EQ(utimensat(dfd
, BASELINK
, tptr
, 0), -1, NULL
);
187 T_ASSERT_EQ(errno
, ENOENT
, NULL
);
189 T_ASSERT_POSIX_ZERO(utimensat(dfd
, BASELINK
, tptr
, AT_SYMLINK_NOFOLLOW
), NULL
);
191 T_ASSERT_POSIX_ZERO(close(dfd
), NULL
);
193 T_ASSERT_POSIX_ZERO(lstat(LINK
, &st
), NULL
);
194 T_ASSERT_EQ(st
.st_atimespec
.tv_sec
, tptr
[0].tv_sec
, NULL
);
195 T_ASSERT_EQ(st
.st_atimespec
.tv_nsec
, tptr
[0].tv_nsec
, NULL
);
196 T_ASSERT_EQ(st
.st_mtimespec
.tv_sec
, tptr
[1].tv_sec
, NULL
);
197 T_ASSERT_EQ(st
.st_mtimespec
.tv_nsec
, tptr
[1].tv_nsec
, NULL
);