2 * Copyright (C) 2013 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <gtest/gtest.h>
20 #include <sys/syscall.h>
23 #include "TemporaryFile.h"
26 TEST(sys_time, utimes_nullptr) {
28 ASSERT_EQ(0, utimes(tf.filename, nullptr));
31 TEST(sys_time, utimes_EINVAL) {
37 ASSERT_EQ(-1, utimes(tf.filename, tv));
38 ASSERT_EQ(EINVAL, errno);
39 tv[0].tv_usec = 1234567;
40 ASSERT_EQ(-1, utimes(tf.filename, tv));
41 ASSERT_EQ(EINVAL, errno);
46 ASSERT_EQ(-1, utimes(tf.filename, tv));
47 ASSERT_EQ(EINVAL, errno);
48 tv[1].tv_usec = 1234567;
49 ASSERT_EQ(-1, utimes(tf.filename, tv));
50 ASSERT_EQ(EINVAL, errno);
53 TEST(sys_time, futimes_nullptr) {
55 ASSERT_EQ(0, futimes(tf.fd, nullptr));
58 TEST(sys_time, futimes_EINVAL) {
64 ASSERT_EQ(-1, futimes(tf.fd, tv));
65 ASSERT_EQ(EINVAL, errno);
66 tv[0].tv_usec = 1234567;
67 ASSERT_EQ(-1, futimes(tf.fd, tv));
68 ASSERT_EQ(EINVAL, errno);
73 ASSERT_EQ(-1, futimes(tf.fd, tv));
74 ASSERT_EQ(EINVAL, errno);
75 tv[1].tv_usec = 1234567;
76 ASSERT_EQ(-1, futimes(tf.fd, tv));
77 ASSERT_EQ(EINVAL, errno);
80 TEST(sys_time, futimesat_nullptr) {
82 ASSERT_EQ(0, futimesat(AT_FDCWD, tf.filename, nullptr));
85 TEST(sys_time, futimesat_EINVAL) {
91 ASSERT_EQ(-1, futimesat(AT_FDCWD, tf.filename, tv));
92 ASSERT_EQ(EINVAL, errno);
93 tv[0].tv_usec = 1234567;
94 ASSERT_EQ(-1, futimesat(AT_FDCWD, tf.filename, tv));
95 ASSERT_EQ(EINVAL, errno);
100 ASSERT_EQ(-1, futimesat(AT_FDCWD, tf.filename, tv));
101 ASSERT_EQ(EINVAL, errno);
102 tv[1].tv_usec = 1234567;
103 ASSERT_EQ(-1, futimesat(AT_FDCWD, tf.filename, tv));
104 ASSERT_EQ(EINVAL, errno);
107 TEST(sys_time, lutimes_nullptr) {
109 ASSERT_EQ(0, lutimes(tf.filename, nullptr));
112 TEST(sys_time, lutimes_EINVAL) {
117 tv[0].tv_usec = -123;
118 ASSERT_EQ(-1, lutimes(tf.filename, tv));
119 ASSERT_EQ(EINVAL, errno);
120 tv[0].tv_usec = 1234567;
121 ASSERT_EQ(-1, lutimes(tf.filename, tv));
122 ASSERT_EQ(EINVAL, errno);
126 tv[1].tv_usec = -123;
127 ASSERT_EQ(-1, lutimes(tf.filename, tv));
128 ASSERT_EQ(EINVAL, errno);
129 tv[1].tv_usec = 1234567;
130 ASSERT_EQ(-1, lutimes(tf.filename, tv));
131 ASSERT_EQ(EINVAL, errno);
134 TEST(sys_time, gettimeofday) {
135 // Try to ensure that our vdso gettimeofday is working.
137 ASSERT_EQ(0, gettimeofday(&tv1, NULL));
139 ASSERT_EQ(0, syscall(__NR_gettimeofday, &tv2, NULL));
141 // What's the difference between the two?
142 tv2.tv_sec -= tv1.tv_sec;
143 tv2.tv_usec -= tv1.tv_usec;
144 if (tv2.tv_usec < 0) {
146 tv2.tv_usec += 1000000;
149 // Should be less than (a very generous, to try to avoid flakiness) 2ms (2000us).
150 ASSERT_EQ(0, tv2.tv_sec);
151 ASSERT_LT(tv2.tv_usec, 2000);