blob: ff9271f8c02975fcbcfb71ffff8d13732249c1d9 [file] [log] [blame]
Elliott Hughesf8fcfbc2013-10-22 13:28:46 -07001/*
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>
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -080020#include <fcntl.h>
Elliott Hughes625993d2014-07-15 16:53:13 -070021#include <sys/syscall.h>
Elliott Hughesf8fcfbc2013-10-22 13:28:46 -070022#include <sys/time.h>
23
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -080024#include <android-base/file.h>
Elliott Hughes27586eb2013-10-28 13:21:06 -070025
Elliott Hughes95646e62023-09-21 14:11:19 -070026#include "utils.h"
27
Elliott Hughes449eff02016-06-08 19:51:20 -070028// http://b/11383777
29TEST(sys_time, utimes_nullptr) {
30 TemporaryFile tf;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -080031 ASSERT_EQ(0, utimes(tf.path, nullptr));
Elliott Hughes449eff02016-06-08 19:51:20 -070032}
33
34TEST(sys_time, utimes_EINVAL) {
35 TemporaryFile tf;
36
37 timeval tv[2] = {};
Elliott Hughesf8fcfbc2013-10-22 13:28:46 -070038
39 tv[0].tv_usec = -123;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -080040 ASSERT_EQ(-1, utimes(tf.path, tv));
Elliott Hughes95646e62023-09-21 14:11:19 -070041 ASSERT_ERRNO(EINVAL);
Elliott Hughesf8fcfbc2013-10-22 13:28:46 -070042 tv[0].tv_usec = 1234567;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -080043 ASSERT_EQ(-1, utimes(tf.path, tv));
Elliott Hughes95646e62023-09-21 14:11:19 -070044 ASSERT_ERRNO(EINVAL);
Elliott Hughes449eff02016-06-08 19:51:20 -070045
Elliott Hughesf8fcfbc2013-10-22 13:28:46 -070046 tv[0].tv_usec = 0;
47
48 tv[1].tv_usec = -123;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -080049 ASSERT_EQ(-1, utimes(tf.path, tv));
Elliott Hughes95646e62023-09-21 14:11:19 -070050 ASSERT_ERRNO(EINVAL);
Elliott Hughesf8fcfbc2013-10-22 13:28:46 -070051 tv[1].tv_usec = 1234567;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -080052 ASSERT_EQ(-1, utimes(tf.path, tv));
Elliott Hughes95646e62023-09-21 14:11:19 -070053 ASSERT_ERRNO(EINVAL);
Elliott Hughesf8fcfbc2013-10-22 13:28:46 -070054}
Elliott Hughes27586eb2013-10-28 13:21:06 -070055
Elliott Hughes449eff02016-06-08 19:51:20 -070056TEST(sys_time, futimes_nullptr) {
Elliott Hughes27586eb2013-10-28 13:21:06 -070057 TemporaryFile tf;
Elliott Hughes449eff02016-06-08 19:51:20 -070058 ASSERT_EQ(0, futimes(tf.fd, nullptr));
59}
60
61TEST(sys_time, futimes_EINVAL) {
62 TemporaryFile tf;
63
64 timeval tv[2] = {};
65
66 tv[0].tv_usec = -123;
67 ASSERT_EQ(-1, futimes(tf.fd, tv));
Elliott Hughes95646e62023-09-21 14:11:19 -070068 ASSERT_ERRNO(EINVAL);
Elliott Hughes449eff02016-06-08 19:51:20 -070069 tv[0].tv_usec = 1234567;
70 ASSERT_EQ(-1, futimes(tf.fd, tv));
Elliott Hughes95646e62023-09-21 14:11:19 -070071 ASSERT_ERRNO(EINVAL);
Elliott Hughes449eff02016-06-08 19:51:20 -070072
73 tv[0].tv_usec = 0;
74
75 tv[1].tv_usec = -123;
76 ASSERT_EQ(-1, futimes(tf.fd, tv));
Elliott Hughes95646e62023-09-21 14:11:19 -070077 ASSERT_ERRNO(EINVAL);
Elliott Hughes449eff02016-06-08 19:51:20 -070078 tv[1].tv_usec = 1234567;
79 ASSERT_EQ(-1, futimes(tf.fd, tv));
Elliott Hughes95646e62023-09-21 14:11:19 -070080 ASSERT_ERRNO(EINVAL);
Elliott Hughes449eff02016-06-08 19:51:20 -070081}
82
83TEST(sys_time, futimesat_nullptr) {
84 TemporaryFile tf;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -080085 ASSERT_EQ(0, futimesat(AT_FDCWD, tf.path, nullptr));
Elliott Hughes449eff02016-06-08 19:51:20 -070086}
87
88TEST(sys_time, futimesat_EINVAL) {
89 TemporaryFile tf;
90
91 timeval tv[2] = {};
92
93 tv[0].tv_usec = -123;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -080094 ASSERT_EQ(-1, futimesat(AT_FDCWD, tf.path, tv));
Elliott Hughes95646e62023-09-21 14:11:19 -070095 ASSERT_ERRNO(EINVAL);
Elliott Hughes449eff02016-06-08 19:51:20 -070096 tv[0].tv_usec = 1234567;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -080097 ASSERT_EQ(-1, futimesat(AT_FDCWD, tf.path, tv));
Elliott Hughes95646e62023-09-21 14:11:19 -070098 ASSERT_ERRNO(EINVAL);
Elliott Hughes449eff02016-06-08 19:51:20 -070099
100 tv[0].tv_usec = 0;
101
102 tv[1].tv_usec = -123;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800103 ASSERT_EQ(-1, futimesat(AT_FDCWD, tf.path, tv));
Elliott Hughes95646e62023-09-21 14:11:19 -0700104 ASSERT_ERRNO(EINVAL);
Elliott Hughes449eff02016-06-08 19:51:20 -0700105 tv[1].tv_usec = 1234567;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800106 ASSERT_EQ(-1, futimesat(AT_FDCWD, tf.path, tv));
Elliott Hughes95646e62023-09-21 14:11:19 -0700107 ASSERT_ERRNO(EINVAL);
Elliott Hughes449eff02016-06-08 19:51:20 -0700108}
109
110TEST(sys_time, lutimes_nullptr) {
111 TemporaryFile tf;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800112 ASSERT_EQ(0, lutimes(tf.path, nullptr));
Elliott Hughes449eff02016-06-08 19:51:20 -0700113}
114
115TEST(sys_time, lutimes_EINVAL) {
116 TemporaryFile tf;
117
118 timeval tv[2] = {};
119
120 tv[0].tv_usec = -123;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800121 ASSERT_EQ(-1, lutimes(tf.path, tv));
Elliott Hughes95646e62023-09-21 14:11:19 -0700122 ASSERT_ERRNO(EINVAL);
Elliott Hughes449eff02016-06-08 19:51:20 -0700123 tv[0].tv_usec = 1234567;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800124 ASSERT_EQ(-1, lutimes(tf.path, tv));
Elliott Hughes95646e62023-09-21 14:11:19 -0700125 ASSERT_ERRNO(EINVAL);
Elliott Hughes449eff02016-06-08 19:51:20 -0700126
127 tv[0].tv_usec = 0;
128
129 tv[1].tv_usec = -123;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800130 ASSERT_EQ(-1, lutimes(tf.path, tv));
Elliott Hughes95646e62023-09-21 14:11:19 -0700131 ASSERT_ERRNO(EINVAL);
Elliott Hughes449eff02016-06-08 19:51:20 -0700132 tv[1].tv_usec = 1234567;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800133 ASSERT_EQ(-1, lutimes(tf.path, tv));
Elliott Hughes95646e62023-09-21 14:11:19 -0700134 ASSERT_ERRNO(EINVAL);
Elliott Hughes27586eb2013-10-28 13:21:06 -0700135}
Elliott Hughes625993d2014-07-15 16:53:13 -0700136
Colin Cross35d469b2021-08-16 15:26:28 -0700137// Musl doesn't define __NR_gettimeofday on 32-bit architectures.
138#if !defined(__NR_gettimeofday)
139#define __NR_gettimeofday __NR_gettimeofday_time32
140#endif
141
Elliott Hughes625993d2014-07-15 16:53:13 -0700142TEST(sys_time, gettimeofday) {
143 // Try to ensure that our vdso gettimeofday is working.
144 timeval tv1;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700145 ASSERT_EQ(0, gettimeofday(&tv1, nullptr));
Elliott Hughes625993d2014-07-15 16:53:13 -0700146 timeval tv2;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700147 ASSERT_EQ(0, syscall(__NR_gettimeofday, &tv2, nullptr));
Elliott Hughes625993d2014-07-15 16:53:13 -0700148
149 // What's the difference between the two?
150 tv2.tv_sec -= tv1.tv_sec;
151 tv2.tv_usec -= tv1.tv_usec;
152 if (tv2.tv_usec < 0) {
153 --tv2.tv_sec;
154 tv2.tv_usec += 1000000;
155 }
156
Elliott Hughes0159b642019-01-18 08:20:55 -0800157 // To try to avoid flakiness we'll accept answers within 10,000us (0.01s).
Elliott Hughes625993d2014-07-15 16:53:13 -0700158 ASSERT_EQ(0, tv2.tv_sec);
Elliott Hughes0159b642019-01-18 08:20:55 -0800159 ASSERT_LT(tv2.tv_usec, 10'000);
Elliott Hughes625993d2014-07-15 16:53:13 -0700160}