]> git.saurik.com Git - apple/xnu.git/blame - tests/netbsd_utimensat.c
xnu-7195.81.3.tar.gz
[apple/xnu.git] / tests / netbsd_utimensat.c
CommitLineData
5ba3f43e
A
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
a39ff7e2
A
48#define DIRPATH "dir"
49#define FILEPATH "dir/utimensat"
5ba3f43e
A
50#define BASEFILE "utimensat"
51#define LINK "dir/symlink"
52#define BASELINK "symlink"
53#define FILEERR "dir/symlink"
54
0a7de745 55static const struct timespec tptr[] = {
5ba3f43e
A
56 { 0x12345678, 987654321 },
57 { 0x15263748, 123456789 },
58};
59
0a7de745
A
60static void
61chtmpdir(void)
5ba3f43e
A
62{
63 T_SETUPBEGIN;
64 T_ASSERT_POSIX_ZERO(chdir(dt_tmpdir()), NULL);
65
66 // <rdar://problem/31780295> dt_tmpdir() should guarantee a clean directory for each run
a39ff7e2 67 unlink(FILEPATH);
5ba3f43e 68 unlink(LINK);
a39ff7e2
A
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 }
5ba3f43e
A
77
78 T_SETUPEND;
79}
80
81T_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
a39ff7e2 89 T_ASSERT_POSIX_ZERO(mkdir(DIRPATH, 0755), NULL);
0a7de745 90 T_ASSERT_POSIX_SUCCESS((fd = open(FILEPATH, O_CREAT | O_RDWR, 0644)), NULL);
5ba3f43e
A
91 T_ASSERT_POSIX_ZERO(close(fd), NULL);
92
a39ff7e2 93 T_ASSERT_POSIX_SUCCESS((dfd = open(DIRPATH, O_RDONLY, 0)), NULL);
5ba3f43e
A
94 T_ASSERT_POSIX_ZERO(utimensat(dfd, BASEFILE, tptr, 0), NULL);
95 T_ASSERT_POSIX_ZERO(close(dfd), NULL);
96
a39ff7e2 97 T_ASSERT_POSIX_ZERO(stat(FILEPATH, &st), NULL);
5ba3f43e
A
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
104T_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
a39ff7e2 111 T_ASSERT_POSIX_ZERO(mkdir(DIRPATH, 0755), NULL);
0a7de745 112 T_ASSERT_POSIX_SUCCESS((fd = open(FILEPATH, O_CREAT | O_RDWR, 0644)), NULL);
5ba3f43e
A
113 T_ASSERT_POSIX_ZERO(close(fd), NULL);
114
a39ff7e2 115 T_ASSERT_POSIX_ZERO(chdir(DIRPATH), NULL);
5ba3f43e
A
116 T_ASSERT_POSIX_ZERO(utimensat(AT_FDCWD, BASEFILE, tptr, 0), NULL);
117
118 T_ASSERT_POSIX_ZERO(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
125T_DECL(netbsd_utimensat_fdcwderr, "See that utimensat fails with fd as AT_FDCWD and bad path")
126{
127 chtmpdir();
128
a39ff7e2 129 T_ASSERT_POSIX_ZERO(mkdir(DIRPATH, 0755), NULL);
5ba3f43e
A
130 T_ASSERT_EQ(utimensat(AT_FDCWD, FILEERR, tptr, 0), -1, NULL);
131}
132
133T_DECL(netbsd_utimensat_fderr1, "See that utimensat fail with bad path")
134{
135 chtmpdir();
136
137 int dfd;
138
a39ff7e2
A
139 T_ASSERT_POSIX_ZERO(mkdir(DIRPATH, 0755), NULL);
140 T_ASSERT_POSIX_SUCCESS((dfd = open(DIRPATH, O_RDONLY, 0)), NULL);
5ba3f43e
A
141 T_ASSERT_EQ(utimensat(dfd, FILEERR, tptr, 0), -1, NULL);
142 T_ASSERT_POSIX_ZERO(close(dfd), NULL);
143}
144
145T_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
a39ff7e2 153 T_ASSERT_POSIX_ZERO(mkdir(DIRPATH, 0755), NULL);
0a7de745 154 T_ASSERT_POSIX_SUCCESS((fd = open(FILEPATH, O_CREAT | O_RDWR, 0644)), NULL);
5ba3f43e
A
155 T_ASSERT_POSIX_ZERO(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_ZERO(close(dfd), NULL);
160}
161
162T_DECL(netbsd_utimensat_fderr3, "See that utimensat fails with fd as -1")
163{
164 chtmpdir();
165
166 int fd;
167
a39ff7e2 168 T_ASSERT_POSIX_ZERO(mkdir(DIRPATH, 0755), NULL);
0a7de745 169 T_ASSERT_POSIX_SUCCESS((fd = open(FILEPATH, O_CREAT | O_RDWR, 0644)), NULL);
5ba3f43e
A
170 T_ASSERT_POSIX_ZERO(close(fd), NULL);
171
a39ff7e2 172 T_ASSERT_EQ(utimensat(-1, FILEPATH, tptr, 0), -1, NULL);
5ba3f43e
A
173}
174
175T_DECL(netbsd_utimensat_fdlink, "See that utimensat works on symlink")
176{
177 chtmpdir();
178
179 int dfd;
180 struct stat st;
181
a39ff7e2
A
182 T_ASSERT_POSIX_ZERO(mkdir(DIRPATH, 0755), NULL);
183 T_ASSERT_POSIX_ZERO(symlink(FILEPATH, LINK), NULL); /* NB: FILE does not exists */
5ba3f43e 184
a39ff7e2 185 T_ASSERT_POSIX_SUCCESS((dfd = open(DIRPATH, O_RDONLY, 0)), NULL);
5ba3f43e
A
186
187 T_ASSERT_EQ(utimensat(dfd, BASELINK, tptr, 0), -1, NULL);
188 T_ASSERT_EQ(errno, ENOENT, NULL);
189
190 T_ASSERT_POSIX_ZERO(utimensat(dfd, BASELINK, tptr, AT_SYMLINK_NOFOLLOW), NULL);
191
192 T_ASSERT_POSIX_ZERO(close(dfd), NULL);
193
194 T_ASSERT_POSIX_ZERO(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}