| Elliott Hughes | f8fcfbc | 2013-10-22 13:28:46 -0700 | [diff] [blame] | 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> | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 20 | #include <sys/syscall.h> | 
| Elliott Hughes | f8fcfbc | 2013-10-22 13:28:46 -0700 | [diff] [blame] | 21 | #include <sys/time.h> | 
|  | 22 |  | 
| Elliott Hughes | 27586eb | 2013-10-28 13:21:06 -0700 | [diff] [blame] | 23 | #include "TemporaryFile.h" | 
|  | 24 |  | 
| Elliott Hughes | f8fcfbc | 2013-10-22 13:28:46 -0700 | [diff] [blame] | 25 | TEST(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 | } | 
| Elliott Hughes | 27586eb | 2013-10-28 13:21:06 -0700 | [diff] [blame] | 44 |  | 
|  | 45 | // http://b/11383777 | 
|  | 46 | TEST(sys_time, utimes_NULL) { | 
|  | 47 | TemporaryFile tf; | 
|  | 48 | ASSERT_EQ(0, utimes(tf.filename, NULL)); | 
|  | 49 | } | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 50 |  | 
|  | 51 | TEST(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 | } |