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
25TEST(sys_time, utimes) {
26  timeval tv[2];
27  memset(&tv, 0, sizeof(tv));
28
29  tv[0].tv_usec = -123;
30  ASSERT_EQ(-1, utimes("/", tv));
31  ASSERT_EQ(EINVAL, errno);
32  tv[0].tv_usec = 1234567;
33  ASSERT_EQ(-1, utimes("/", tv));
34  ASSERT_EQ(EINVAL, errno);
35  tv[0].tv_usec = 0;
36
37  tv[1].tv_usec = -123;
38  ASSERT_EQ(-1, utimes("/", tv));
39  ASSERT_EQ(EINVAL, errno);
40  tv[1].tv_usec = 1234567;
41  ASSERT_EQ(-1, utimes("/", tv));
42  ASSERT_EQ(EINVAL, errno);
43}
44
45// http://b/11383777
46TEST(sys_time, utimes_NULL) {
47  TemporaryFile tf;
48  ASSERT_EQ(0, utimes(tf.filename, NULL));
49}
50
51TEST(sys_time, gettimeofday) {
52  // Try to ensure that our vdso gettimeofday is working.
53  timeval tv1;
54  ASSERT_EQ(0, gettimeofday(&tv1, NULL));
55  timeval tv2;
56  ASSERT_EQ(0, syscall(__NR_gettimeofday, &tv2, NULL));
57
58  // What's the difference between the two?
59  tv2.tv_sec -= tv1.tv_sec;
60  tv2.tv_usec -= tv1.tv_usec;
61  if (tv2.tv_usec < 0) {
62    --tv2.tv_sec;
63    tv2.tv_usec += 1000000;
64  }
65
66  // Should be less than (a very generous, to try to avoid flakiness) 1000us.
67  ASSERT_EQ(0, tv2.tv_sec);
68  ASSERT_LT(tv2.tv_usec, 1000);
69}
70