]> git.saurik.com Git - apple/xnu.git/blame - tools/tests/darwintests/utimensat.c
xnu-4570.1.46.tar.gz
[apple/xnu.git] / tools / tests / darwintests / utimensat.c
CommitLineData
5ba3f43e
A
1#include <sys/cdefs.h>
2#include <sys/param.h>
3#include <sys/stat.h>
4#include <sys/time.h>
5#include <errno.h>
6#include <fcntl.h>
7#include <limits.h>
8#include <paths.h>
9#include <stdio.h>
10#include <string.h>
11#include <unistd.h>
12
13#include <darwintest.h>
14#include <darwintest_utils.h>
15
16#define FILENAME "utimensat"
17
18static const struct timespec tptr[][2] = {
19 { { 0x12345678, 987654321 }, { 0x15263748, 123456789 }, },
20
21 { { 0, UTIME_NOW }, { 0x15263748, 123456789 }, },
22 { { 0x12345678, 987654321 }, { 0, UTIME_NOW }, },
23 { { 0, UTIME_NOW }, { 0, UTIME_NOW }, },
24
25 { { 0, UTIME_OMIT }, { 0x15263748, 123456789 }, },
26 { { 0x12345678, 987654321 }, { 0, UTIME_OMIT }, },
27 { { 0, UTIME_OMIT }, { 0, UTIME_OMIT }, },
28
29 { { 0, UTIME_NOW }, { 0, UTIME_OMIT }, },
30 { { 0, UTIME_OMIT }, { 0, UTIME_NOW }, },
31};
32
33T_DECL(utimensat, "Try various versions of utimensat")
34{
35 T_SETUPBEGIN;
36 T_ASSERT_POSIX_ZERO(chdir(dt_tmpdir()), NULL);
37 T_SETUPEND;
38
39 struct stat pre_st, post_st;
40 int fd;
41
42 T_ASSERT_POSIX_SUCCESS((fd = open(FILENAME, O_CREAT|O_RDWR, 0644)), NULL);
43 T_ASSERT_POSIX_ZERO(close(fd), NULL);
44
45 for (size_t i = 0; i < sizeof(tptr)/sizeof(tptr[0]); i++) {
46 T_LOG("=== {%ld, %ld} {%ld, %ld} ===",
47 tptr[i][0].tv_sec, tptr[i][0].tv_nsec,
48 tptr[i][1].tv_sec, tptr[i][1].tv_nsec);
49
50 struct timespec now;
51 clock_gettime(CLOCK_REALTIME, &now);
52
53 T_ASSERT_POSIX_ZERO(stat(FILENAME, &pre_st), NULL);
54 T_ASSERT_POSIX_ZERO(utimensat(AT_FDCWD, FILENAME, tptr[i], 0), NULL);
55 T_ASSERT_POSIX_ZERO(stat(FILENAME, &post_st), NULL);
56
57 if (tptr[i][0].tv_nsec == UTIME_NOW) {
58 T_ASSERT_GE(post_st.st_atimespec.tv_sec, now.tv_sec, NULL);
59 } else if (tptr[i][0].tv_nsec == UTIME_OMIT) {
60 T_ASSERT_EQ(post_st.st_atimespec.tv_sec, pre_st.st_atimespec.tv_sec, NULL);
61 T_ASSERT_EQ(post_st.st_atimespec.tv_nsec, pre_st.st_atimespec.tv_nsec, NULL);
62 } else {
63 T_ASSERT_EQ(post_st.st_atimespec.tv_sec, tptr[i][0].tv_sec, NULL);
64 T_ASSERT_EQ(post_st.st_atimespec.tv_nsec, tptr[i][0].tv_nsec, NULL);
65 }
66
67 if (tptr[i][1].tv_nsec == UTIME_NOW) {
68 T_ASSERT_GE(post_st.st_mtimespec.tv_sec, now.tv_sec, NULL);
69 } else if (tptr[i][1].tv_nsec == UTIME_OMIT) {
70 T_ASSERT_EQ(post_st.st_mtimespec.tv_sec, pre_st.st_mtimespec.tv_sec, NULL);
71 T_ASSERT_EQ(post_st.st_mtimespec.tv_nsec, pre_st.st_mtimespec.tv_nsec, NULL);
72 } else {
73 T_ASSERT_EQ(post_st.st_mtimespec.tv_sec, tptr[i][1].tv_sec, NULL);
74 T_ASSERT_EQ(post_st.st_mtimespec.tv_nsec, tptr[i][1].tv_nsec, NULL);
75 }
76 }
77}