blob: b0e52aac9568b387bbbca2b702d90533cc14e724 [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 Hugheseabc47b2024-11-08 22:05:00 +000026#include "private/bionic_time_conversions.h"
Elliott Hughes95646e62023-09-21 14:11:19 -070027#include "utils.h"
28
Elliott Hughes449eff02016-06-08 19:51:20 -070029// http://b/11383777
30TEST(sys_time, utimes_nullptr) {
31 TemporaryFile tf;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -080032 ASSERT_EQ(0, utimes(tf.path, nullptr));
Elliott Hughes449eff02016-06-08 19:51:20 -070033}
34
35TEST(sys_time, utimes_EINVAL) {
36 TemporaryFile tf;
37
38 timeval tv[2] = {};
Elliott Hughesf8fcfbc2013-10-22 13:28:46 -070039
40 tv[0].tv_usec = -123;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -080041 ASSERT_EQ(-1, utimes(tf.path, tv));
Elliott Hughes95646e62023-09-21 14:11:19 -070042 ASSERT_ERRNO(EINVAL);
Elliott Hughesf8fcfbc2013-10-22 13:28:46 -070043 tv[0].tv_usec = 1234567;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -080044 ASSERT_EQ(-1, utimes(tf.path, tv));
Elliott Hughes95646e62023-09-21 14:11:19 -070045 ASSERT_ERRNO(EINVAL);
Elliott Hughes449eff02016-06-08 19:51:20 -070046
Elliott Hughesf8fcfbc2013-10-22 13:28:46 -070047 tv[0].tv_usec = 0;
48
49 tv[1].tv_usec = -123;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -080050 ASSERT_EQ(-1, utimes(tf.path, tv));
Elliott Hughes95646e62023-09-21 14:11:19 -070051 ASSERT_ERRNO(EINVAL);
Elliott Hughesf8fcfbc2013-10-22 13:28:46 -070052 tv[1].tv_usec = 1234567;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -080053 ASSERT_EQ(-1, utimes(tf.path, tv));
Elliott Hughes95646e62023-09-21 14:11:19 -070054 ASSERT_ERRNO(EINVAL);
Elliott Hughesf8fcfbc2013-10-22 13:28:46 -070055}
Elliott Hughes27586eb2013-10-28 13:21:06 -070056
Elliott Hughes449eff02016-06-08 19:51:20 -070057TEST(sys_time, futimes_nullptr) {
Elliott Hughes27586eb2013-10-28 13:21:06 -070058 TemporaryFile tf;
Elliott Hughes449eff02016-06-08 19:51:20 -070059 ASSERT_EQ(0, futimes(tf.fd, nullptr));
60}
61
62TEST(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));
Elliott Hughes95646e62023-09-21 14:11:19 -070069 ASSERT_ERRNO(EINVAL);
Elliott Hughes449eff02016-06-08 19:51:20 -070070 tv[0].tv_usec = 1234567;
71 ASSERT_EQ(-1, futimes(tf.fd, tv));
Elliott Hughes95646e62023-09-21 14:11:19 -070072 ASSERT_ERRNO(EINVAL);
Elliott Hughes449eff02016-06-08 19:51:20 -070073
74 tv[0].tv_usec = 0;
75
76 tv[1].tv_usec = -123;
77 ASSERT_EQ(-1, futimes(tf.fd, tv));
Elliott Hughes95646e62023-09-21 14:11:19 -070078 ASSERT_ERRNO(EINVAL);
Elliott Hughes449eff02016-06-08 19:51:20 -070079 tv[1].tv_usec = 1234567;
80 ASSERT_EQ(-1, futimes(tf.fd, tv));
Elliott Hughes95646e62023-09-21 14:11:19 -070081 ASSERT_ERRNO(EINVAL);
Elliott Hughes449eff02016-06-08 19:51:20 -070082}
83
84TEST(sys_time, futimesat_nullptr) {
85 TemporaryFile tf;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -080086 ASSERT_EQ(0, futimesat(AT_FDCWD, tf.path, nullptr));
Elliott Hughes449eff02016-06-08 19:51:20 -070087}
88
89TEST(sys_time, futimesat_EINVAL) {
90 TemporaryFile tf;
91
92 timeval tv[2] = {};
93
94 tv[0].tv_usec = -123;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -080095 ASSERT_EQ(-1, futimesat(AT_FDCWD, tf.path, tv));
Elliott Hughes95646e62023-09-21 14:11:19 -070096 ASSERT_ERRNO(EINVAL);
Elliott Hughes449eff02016-06-08 19:51:20 -070097 tv[0].tv_usec = 1234567;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -080098 ASSERT_EQ(-1, futimesat(AT_FDCWD, tf.path, tv));
Elliott Hughes95646e62023-09-21 14:11:19 -070099 ASSERT_ERRNO(EINVAL);
Elliott Hughes449eff02016-06-08 19:51:20 -0700100
101 tv[0].tv_usec = 0;
102
103 tv[1].tv_usec = -123;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800104 ASSERT_EQ(-1, futimesat(AT_FDCWD, tf.path, tv));
Elliott Hughes95646e62023-09-21 14:11:19 -0700105 ASSERT_ERRNO(EINVAL);
Elliott Hughes449eff02016-06-08 19:51:20 -0700106 tv[1].tv_usec = 1234567;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800107 ASSERT_EQ(-1, futimesat(AT_FDCWD, tf.path, tv));
Elliott Hughes95646e62023-09-21 14:11:19 -0700108 ASSERT_ERRNO(EINVAL);
Elliott Hughes449eff02016-06-08 19:51:20 -0700109}
110
111TEST(sys_time, lutimes_nullptr) {
112 TemporaryFile tf;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800113 ASSERT_EQ(0, lutimes(tf.path, nullptr));
Elliott Hughes449eff02016-06-08 19:51:20 -0700114}
115
116TEST(sys_time, lutimes_EINVAL) {
117 TemporaryFile tf;
118
119 timeval tv[2] = {};
120
121 tv[0].tv_usec = -123;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800122 ASSERT_EQ(-1, lutimes(tf.path, tv));
Elliott Hughes95646e62023-09-21 14:11:19 -0700123 ASSERT_ERRNO(EINVAL);
Elliott Hughes449eff02016-06-08 19:51:20 -0700124 tv[0].tv_usec = 1234567;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800125 ASSERT_EQ(-1, lutimes(tf.path, tv));
Elliott Hughes95646e62023-09-21 14:11:19 -0700126 ASSERT_ERRNO(EINVAL);
Elliott Hughes449eff02016-06-08 19:51:20 -0700127
128 tv[0].tv_usec = 0;
129
130 tv[1].tv_usec = -123;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800131 ASSERT_EQ(-1, lutimes(tf.path, tv));
Elliott Hughes95646e62023-09-21 14:11:19 -0700132 ASSERT_ERRNO(EINVAL);
Elliott Hughes449eff02016-06-08 19:51:20 -0700133 tv[1].tv_usec = 1234567;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800134 ASSERT_EQ(-1, lutimes(tf.path, tv));
Elliott Hughes95646e62023-09-21 14:11:19 -0700135 ASSERT_ERRNO(EINVAL);
Elliott Hughes27586eb2013-10-28 13:21:06 -0700136}
Elliott Hughes625993d2014-07-15 16:53:13 -0700137
Colin Cross35d469b2021-08-16 15:26:28 -0700138// 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
Elliott Hughes625993d2014-07-15 16:53:13 -0700143TEST(sys_time, gettimeofday) {
144 // Try to ensure that our vdso gettimeofday is working.
145 timeval tv1;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700146 ASSERT_EQ(0, gettimeofday(&tv1, nullptr));
Elliott Hughes625993d2014-07-15 16:53:13 -0700147 timeval tv2;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700148 ASSERT_EQ(0, syscall(__NR_gettimeofday, &tv2, nullptr));
Elliott Hughes625993d2014-07-15 16:53:13 -0700149
150 // What's the difference between the two?
Elliott Hughes0159b642019-01-18 08:20:55 -0800151 // To try to avoid flakiness we'll accept answers within 10,000us (0.01s).
Elliott Hugheseabc47b2024-11-08 22:05:00 +0000152 ASSERT_LT(to_us(tv2) - to_us(tv1), 10'000);
Elliott Hughes625993d2014-07-15 16:53:13 -0700153}