1 /*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <gtest/gtest.h>
18
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <sys/syscall.h>
22 #include <sys/time.h>
23
24 #include <android-base/file.h>
25
26 // http://b/11383777
TEST(sys_time,utimes_nullptr)27 TEST(sys_time, utimes_nullptr) {
28 TemporaryFile tf;
29 ASSERT_EQ(0, utimes(tf.path, nullptr));
30 }
31
TEST(sys_time,utimes_EINVAL)32 TEST(sys_time, utimes_EINVAL) {
33 TemporaryFile tf;
34
35 timeval tv[2] = {};
36
37 tv[0].tv_usec = -123;
38 ASSERT_EQ(-1, utimes(tf.path, tv));
39 ASSERT_EQ(EINVAL, errno);
40 tv[0].tv_usec = 1234567;
41 ASSERT_EQ(-1, utimes(tf.path, tv));
42 ASSERT_EQ(EINVAL, errno);
43
44 tv[0].tv_usec = 0;
45
46 tv[1].tv_usec = -123;
47 ASSERT_EQ(-1, utimes(tf.path, tv));
48 ASSERT_EQ(EINVAL, errno);
49 tv[1].tv_usec = 1234567;
50 ASSERT_EQ(-1, utimes(tf.path, tv));
51 ASSERT_EQ(EINVAL, errno);
52 }
53
TEST(sys_time,futimes_nullptr)54 TEST(sys_time, futimes_nullptr) {
55 TemporaryFile tf;
56 ASSERT_EQ(0, futimes(tf.fd, nullptr));
57 }
58
TEST(sys_time,futimes_EINVAL)59 TEST(sys_time, futimes_EINVAL) {
60 TemporaryFile tf;
61
62 timeval tv[2] = {};
63
64 tv[0].tv_usec = -123;
65 ASSERT_EQ(-1, futimes(tf.fd, tv));
66 ASSERT_EQ(EINVAL, errno);
67 tv[0].tv_usec = 1234567;
68 ASSERT_EQ(-1, futimes(tf.fd, tv));
69 ASSERT_EQ(EINVAL, errno);
70
71 tv[0].tv_usec = 0;
72
73 tv[1].tv_usec = -123;
74 ASSERT_EQ(-1, futimes(tf.fd, tv));
75 ASSERT_EQ(EINVAL, errno);
76 tv[1].tv_usec = 1234567;
77 ASSERT_EQ(-1, futimes(tf.fd, tv));
78 ASSERT_EQ(EINVAL, errno);
79 }
80
TEST(sys_time,futimesat_nullptr)81 TEST(sys_time, futimesat_nullptr) {
82 TemporaryFile tf;
83 ASSERT_EQ(0, futimesat(AT_FDCWD, tf.path, nullptr));
84 }
85
TEST(sys_time,futimesat_EINVAL)86 TEST(sys_time, futimesat_EINVAL) {
87 TemporaryFile tf;
88
89 timeval tv[2] = {};
90
91 tv[0].tv_usec = -123;
92 ASSERT_EQ(-1, futimesat(AT_FDCWD, tf.path, tv));
93 ASSERT_EQ(EINVAL, errno);
94 tv[0].tv_usec = 1234567;
95 ASSERT_EQ(-1, futimesat(AT_FDCWD, tf.path, tv));
96 ASSERT_EQ(EINVAL, errno);
97
98 tv[0].tv_usec = 0;
99
100 tv[1].tv_usec = -123;
101 ASSERT_EQ(-1, futimesat(AT_FDCWD, tf.path, tv));
102 ASSERT_EQ(EINVAL, errno);
103 tv[1].tv_usec = 1234567;
104 ASSERT_EQ(-1, futimesat(AT_FDCWD, tf.path, tv));
105 ASSERT_EQ(EINVAL, errno);
106 }
107
TEST(sys_time,lutimes_nullptr)108 TEST(sys_time, lutimes_nullptr) {
109 TemporaryFile tf;
110 ASSERT_EQ(0, lutimes(tf.path, nullptr));
111 }
112
TEST(sys_time,lutimes_EINVAL)113 TEST(sys_time, lutimes_EINVAL) {
114 TemporaryFile tf;
115
116 timeval tv[2] = {};
117
118 tv[0].tv_usec = -123;
119 ASSERT_EQ(-1, lutimes(tf.path, tv));
120 ASSERT_EQ(EINVAL, errno);
121 tv[0].tv_usec = 1234567;
122 ASSERT_EQ(-1, lutimes(tf.path, tv));
123 ASSERT_EQ(EINVAL, errno);
124
125 tv[0].tv_usec = 0;
126
127 tv[1].tv_usec = -123;
128 ASSERT_EQ(-1, lutimes(tf.path, tv));
129 ASSERT_EQ(EINVAL, errno);
130 tv[1].tv_usec = 1234567;
131 ASSERT_EQ(-1, lutimes(tf.path, tv));
132 ASSERT_EQ(EINVAL, errno);
133 }
134
TEST(sys_time,gettimeofday)135 TEST(sys_time, gettimeofday) {
136 // Try to ensure that our vdso gettimeofday is working.
137 timeval tv1;
138 ASSERT_EQ(0, gettimeofday(&tv1, nullptr));
139 timeval tv2;
140 ASSERT_EQ(0, syscall(__NR_gettimeofday, &tv2, nullptr));
141
142 // What's the difference between the two?
143 tv2.tv_sec -= tv1.tv_sec;
144 tv2.tv_usec -= tv1.tv_usec;
145 if (tv2.tv_usec < 0) {
146 --tv2.tv_sec;
147 tv2.tv_usec += 1000000;
148 }
149
150 // To try to avoid flakiness we'll accept answers within 10,000us (0.01s).
151 ASSERT_EQ(0, tv2.tv_sec);
152 ASSERT_LT(tv2.tv_usec, 10'000);
153 }
154