OSDN Git Service

Merge "Expand whitelist"
[android-x86/bionic.git] / tests / sys_time_test.cpp
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 <sys/syscall.h>
21 #include <sys/time.h>
22
23 #include "TemporaryFile.h"
24
25 // http://b/11383777
26 TEST(sys_time, utimes_nullptr) {
27   TemporaryFile tf;
28   ASSERT_EQ(0, utimes(tf.filename, nullptr));
29 }
30
31 TEST(sys_time, utimes_EINVAL) {
32   TemporaryFile tf;
33
34   timeval tv[2] = {};
35
36   tv[0].tv_usec = -123;
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);
42
43   tv[0].tv_usec = 0;
44
45   tv[1].tv_usec = -123;
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);
51 }
52
53 TEST(sys_time, futimes_nullptr) {
54   TemporaryFile tf;
55   ASSERT_EQ(0, futimes(tf.fd, nullptr));
56 }
57
58 TEST(sys_time, futimes_EINVAL) {
59   TemporaryFile tf;
60
61   timeval tv[2] = {};
62
63   tv[0].tv_usec = -123;
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);
69
70   tv[0].tv_usec = 0;
71
72   tv[1].tv_usec = -123;
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);
78 }
79
80 TEST(sys_time, futimesat_nullptr) {
81   TemporaryFile tf;
82   ASSERT_EQ(0, futimesat(AT_FDCWD, tf.filename, nullptr));
83 }
84
85 TEST(sys_time, futimesat_EINVAL) {
86   TemporaryFile tf;
87
88   timeval tv[2] = {};
89
90   tv[0].tv_usec = -123;
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);
96
97   tv[0].tv_usec = 0;
98
99   tv[1].tv_usec = -123;
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);
105 }
106
107 TEST(sys_time, lutimes_nullptr) {
108   TemporaryFile tf;
109   ASSERT_EQ(0, lutimes(tf.filename, nullptr));
110 }
111
112 TEST(sys_time, lutimes_EINVAL) {
113   TemporaryFile tf;
114
115   timeval tv[2] = {};
116
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);
123
124   tv[0].tv_usec = 0;
125
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);
132 }
133
134 TEST(sys_time, gettimeofday) {
135   // Try to ensure that our vdso gettimeofday is working.
136   timeval tv1;
137   ASSERT_EQ(0, gettimeofday(&tv1, NULL));
138   timeval tv2;
139   ASSERT_EQ(0, syscall(__NR_gettimeofday, &tv2, NULL));
140
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) {
145     --tv2.tv_sec;
146     tv2.tv_usec += 1000000;
147   }
148
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);
152 }