1 /* $NetBSD: t_utimensat.c,v 1.5 2013/03/17 04:46:06 jmmv 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.5 2013/03/17 04:46:06 jmmv Exp $");
42 #include <sys/param.h>
46 #define FILE "dir/utimensat"
47 #define BASEFILE "utimensat"
48 #define LINK "dir/symlink"
49 #define BASELINK "symlink"
50 #define FILEERR "dir/symlink"
52 const struct timespec tptr
[] = {
53 { 0x12345678, 987654321 },
54 { 0x15263748, 123456789 },
58 ATF_TC_HEAD(utimensat_fd
, tc
)
60 atf_tc_set_md_var(tc
, "descr", "See that utimensat works with fd");
62 ATF_TC_BODY(utimensat_fd
, tc
)
68 ATF_REQUIRE(mkdir(DIR, 0755) == 0);
69 ATF_REQUIRE((fd
= open(FILE, O_CREAT
|O_RDWR
, 0644)) != -1);
70 ATF_REQUIRE(close(fd
) == 0);
72 ATF_REQUIRE((dfd
= open(DIR, O_RDONLY
, 0)) != -1);
73 ATF_REQUIRE(utimensat(dfd
, BASEFILE
, tptr
, 0) == 0);
74 ATF_REQUIRE(close(dfd
) == 0);
76 ATF_REQUIRE(stat(FILE, &st
) == 0);
77 ATF_REQUIRE(st
.st_atimespec
.tv_sec
== tptr
[0].tv_sec
);
78 ATF_REQUIRE(st
.st_atimespec
.tv_nsec
== tptr
[0].tv_nsec
);
79 ATF_REQUIRE(st
.st_mtimespec
.tv_sec
== tptr
[1].tv_sec
);
80 ATF_REQUIRE(st
.st_mtimespec
.tv_nsec
== tptr
[1].tv_nsec
);
83 ATF_TC(utimensat_fdcwd
);
84 ATF_TC_HEAD(utimensat_fdcwd
, tc
)
86 atf_tc_set_md_var(tc
, "descr",
87 "See that utimensat works with fd as AT_FDCWD");
89 ATF_TC_BODY(utimensat_fdcwd
, tc
)
94 ATF_REQUIRE(mkdir(DIR, 0755) == 0);
95 ATF_REQUIRE((fd
= open(FILE, O_CREAT
|O_RDWR
, 0644)) != -1);
96 ATF_REQUIRE(close(fd
) == 0);
98 ATF_REQUIRE(chdir(DIR) == 0);
99 ATF_REQUIRE(utimensat(AT_FDCWD
, BASEFILE
, tptr
, 0) == 0);
101 ATF_REQUIRE(stat(BASEFILE
, &st
) == 0);
102 ATF_REQUIRE(st
.st_atimespec
.tv_sec
== tptr
[0].tv_sec
);
103 ATF_REQUIRE(st
.st_atimespec
.tv_nsec
== tptr
[0].tv_nsec
);
104 ATF_REQUIRE(st
.st_mtimespec
.tv_sec
== tptr
[1].tv_sec
);
105 ATF_REQUIRE(st
.st_mtimespec
.tv_nsec
== tptr
[1].tv_nsec
);
108 ATF_TC(utimensat_fdcwderr
);
109 ATF_TC_HEAD(utimensat_fdcwderr
, tc
)
111 atf_tc_set_md_var(tc
, "descr",
112 "See that utimensat fails with fd as AT_FDCWD and bad path");
114 ATF_TC_BODY(utimensat_fdcwderr
, tc
)
116 ATF_REQUIRE(mkdir(DIR, 0755) == 0);
117 ATF_REQUIRE(utimensat(AT_FDCWD
, FILEERR
, tptr
, 0) == -1);
120 ATF_TC(utimensat_fderr1
);
121 ATF_TC_HEAD(utimensat_fderr1
, tc
)
123 atf_tc_set_md_var(tc
, "descr", "See that utimensat fail with bad path");
125 ATF_TC_BODY(utimensat_fderr1
, tc
)
129 ATF_REQUIRE(mkdir(DIR, 0755) == 0);
130 ATF_REQUIRE((dfd
= open(DIR, O_RDONLY
, 0)) != -1);
131 ATF_REQUIRE(utimensat(dfd
, FILEERR
, tptr
, 0) == -1);
132 ATF_REQUIRE(close(dfd
) == 0);
135 ATF_TC(utimensat_fderr2
);
136 ATF_TC_HEAD(utimensat_fderr2
, tc
)
138 atf_tc_set_md_var(tc
, "descr", "See that utimensat fails with bad fdat");
140 ATF_TC_BODY(utimensat_fderr2
, tc
)
144 char cwd
[MAXPATHLEN
];
146 ATF_REQUIRE(mkdir(DIR, 0755) == 0);
147 ATF_REQUIRE((fd
= open(FILE, O_CREAT
|O_RDWR
, 0644)) != -1);
148 ATF_REQUIRE(close(fd
) == 0);
150 ATF_REQUIRE((dfd
= open(getcwd(cwd
, MAXPATHLEN
), O_RDONLY
, 0)) != -1);
151 ATF_REQUIRE(utimensat(dfd
, BASEFILE
, tptr
, 0) == -1);
152 ATF_REQUIRE(close(dfd
) == 0);
155 ATF_TC(utimensat_fderr3
);
156 ATF_TC_HEAD(utimensat_fderr3
, tc
)
158 atf_tc_set_md_var(tc
, "descr", "See that utimensat fails with fd as -1");
160 ATF_TC_BODY(utimensat_fderr3
, tc
)
164 ATF_REQUIRE(mkdir(DIR, 0755) == 0);
165 ATF_REQUIRE((fd
= open(FILE, O_CREAT
|O_RDWR
, 0644)) != -1);
166 ATF_REQUIRE(close(fd
) == 0);
168 ATF_REQUIRE(utimensat(-1, FILE, tptr
, 0) == -1);
171 ATF_TC(utimensat_fdlink
);
172 ATF_TC_HEAD(utimensat_fdlink
, tc
)
174 atf_tc_set_md_var(tc
, "descr", "See that utimensat works on symlink");
176 ATF_TC_BODY(utimensat_fdlink
, tc
)
181 ATF_REQUIRE(mkdir(DIR, 0755) == 0);
182 ATF_REQUIRE(symlink(FILE, LINK
) == 0); /* NB: FILE does not exists */
184 ATF_REQUIRE((dfd
= open(DIR, O_RDONLY
, 0)) != -1);
186 ATF_REQUIRE(utimensat(dfd
, BASELINK
, tptr
, 0) == -1);
187 ATF_REQUIRE(errno
= ENOENT
);
189 ATF_REQUIRE(utimensat(dfd
, BASELINK
, tptr
, AT_SYMLINK_NOFOLLOW
) == 0);
191 ATF_REQUIRE(close(dfd
) == 0);
193 ATF_REQUIRE(lstat(LINK
, &st
) == 0);
194 ATF_REQUIRE(st
.st_atimespec
.tv_sec
== tptr
[0].tv_sec
);
195 ATF_REQUIRE(st
.st_atimespec
.tv_nsec
== tptr
[0].tv_nsec
);
196 ATF_REQUIRE(st
.st_mtimespec
.tv_sec
== tptr
[1].tv_sec
);
197 ATF_REQUIRE(st
.st_mtimespec
.tv_nsec
== tptr
[1].tv_nsec
);
203 ATF_TP_ADD_TC(tp
, utimensat_fd
);
204 ATF_TP_ADD_TC(tp
, utimensat_fdcwd
);
205 ATF_TP_ADD_TC(tp
, utimensat_fdcwderr
);
206 ATF_TP_ADD_TC(tp
, utimensat_fderr1
);
207 ATF_TP_ADD_TC(tp
, utimensat_fderr2
);
208 ATF_TP_ADD_TC(tp
, utimensat_fderr3
);
209 ATF_TP_ADD_TC(tp
, utimensat_fdlink
);
211 return atf_no_error();