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 #include "private/bionic_time_conversions.h"
27 #include "utils.h"
28
29 // http://b/11383777
TEST(sys_time,utimes_nullptr)30 TEST(sys_time, utimes_nullptr) {
31 TemporaryFile tf;
32 ASSERT_EQ(0, utimes(tf.path, nullptr));
33 }
34
TEST(sys_time,utimes_EINVAL)35 TEST(sys_time, utimes_EINVAL) {
36 TemporaryFile tf;
37
38 timeval tv[2] = {};
39
40 tv[0].tv_usec = -123;
41 ASSERT_EQ(-1, utimes(tf.path, tv));
42 ASSERT_ERRNO(EINVAL);
43 tv[0].tv_usec = 1234567;
44 ASSERT_EQ(-1, utimes(tf.path, tv));
45 ASSERT_ERRNO(EINVAL);
46
47 tv[0].tv_usec = 0;
48
49 tv[1].tv_usec = -123;
50 ASSERT_EQ(-1, utimes(tf.path, tv));
51 ASSERT_ERRNO(EINVAL);
52 tv[1].tv_usec = 1234567;
53 ASSERT_EQ(-1, utimes(tf.path, tv));
54 ASSERT_ERRNO(EINVAL);
55 }
56
TEST(sys_time,futimes_nullptr)57 TEST(sys_time, futimes_nullptr) {
58 TemporaryFile tf;
59 ASSERT_EQ(0, futimes(tf.fd, nullptr));
60 }
61
TEST(sys_time,futimes_EINVAL)62 TEST(sys_time, futimes_EINVAL) {
63 TemporaryFile tf;
64
65 timeval tv[2] = {};
66
67 tv[0].tv_usec = -123;
68 ASSERT_EQ(-1, futimes(tf.fd, tv));
69 ASSERT_ERRNO(EINVAL);
70 tv[0].tv_usec = 1234567;
71 ASSERT_EQ(-1, futimes(tf.fd, tv));
72 ASSERT_ERRNO(EINVAL);
73
74 tv[0].tv_usec = 0;
75
76 tv[1].tv_usec = -123;
77 ASSERT_EQ(-1, futimes(tf.fd, tv));
78 ASSERT_ERRNO(EINVAL);
79 tv[1].tv_usec = 1234567;
80 ASSERT_EQ(-1, futimes(tf.fd, tv));
81 ASSERT_ERRNO(EINVAL);
82 }
83
TEST(sys_time,futimesat_nullptr)84 TEST(sys_time, futimesat_nullptr) {
85 TemporaryFile tf;
86 ASSERT_EQ(0, futimesat(AT_FDCWD, tf.path, nullptr));
87 }
88
TEST(sys_time,futimesat_EINVAL)89 TEST(sys_time, futimesat_EINVAL) {
90 TemporaryFile tf;
91
92 timeval tv[2] = {};
93
94 tv[0].tv_usec = -123;
95 ASSERT_EQ(-1, futimesat(AT_FDCWD, tf.path, tv));
96 ASSERT_ERRNO(EINVAL);
97 tv[0].tv_usec = 1234567;
98 ASSERT_EQ(-1, futimesat(AT_FDCWD, tf.path, tv));
99 ASSERT_ERRNO(EINVAL);
100
101 tv[0].tv_usec = 0;
102
103 tv[1].tv_usec = -123;
104 ASSERT_EQ(-1, futimesat(AT_FDCWD, tf.path, tv));
105 ASSERT_ERRNO(EINVAL);
106 tv[1].tv_usec = 1234567;
107 ASSERT_EQ(-1, futimesat(AT_FDCWD, tf.path, tv));
108 ASSERT_ERRNO(EINVAL);
109 }
110
TEST(sys_time,lutimes_nullptr)111 TEST(sys_time, lutimes_nullptr) {
112 TemporaryFile tf;
113 ASSERT_EQ(0, lutimes(tf.path, nullptr));
114 }
115
TEST(sys_time,lutimes_EINVAL)116 TEST(sys_time, lutimes_EINVAL) {
117 TemporaryFile tf;
118
119 timeval tv[2] = {};
120
121 tv[0].tv_usec = -123;
122 ASSERT_EQ(-1, lutimes(tf.path, tv));
123 ASSERT_ERRNO(EINVAL);
124 tv[0].tv_usec = 1234567;
125 ASSERT_EQ(-1, lutimes(tf.path, tv));
126 ASSERT_ERRNO(EINVAL);
127
128 tv[0].tv_usec = 0;
129
130 tv[1].tv_usec = -123;
131 ASSERT_EQ(-1, lutimes(tf.path, tv));
132 ASSERT_ERRNO(EINVAL);
133 tv[1].tv_usec = 1234567;
134 ASSERT_EQ(-1, lutimes(tf.path, tv));
135 ASSERT_ERRNO(EINVAL);
136 }
137
138 // Musl doesn't define __NR_gettimeofday on 32-bit architectures.
139 #if !defined(__NR_gettimeofday)
140 #define __NR_gettimeofday __NR_gettimeofday_time32
141 #endif
142
TEST(sys_time,gettimeofday)143 TEST(sys_time, gettimeofday) {
144 // Try to ensure that our vdso gettimeofday is working.
145 timeval tv1;
146 ASSERT_EQ(0, gettimeofday(&tv1, nullptr));
147 timeval tv2;
148 ASSERT_EQ(0, syscall(__NR_gettimeofday, &tv2, nullptr));
149
150 // What's the difference between the two?
151 // To try to avoid flakiness we'll accept answers within 10,000us (0.01s).
152 ASSERT_LT(to_us(tv2) - to_us(tv1), 10'000);
153 }
154