1 /******************************************************************************
2 *
3 * Copyright (C) 2015 Google, Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19 #include <gtest/gtest.h>
20
21 #include "AllocationTestHarness.h"
22
23 extern "C" {
24 #include "osi/include/time.h"
25 }
26
27 // Generous upper bound: 10 seconds
28 static const uint32_t TEST_TIME_DELTA_UPPER_BOUND_MS = 10 * 1000;
29
30 class TimeTest : public AllocationTestHarness {};
31
32 //
33 // Test that the return value of time_get_os_boottime_ms() is not zero.
34 //
35 // NOTE: For now this test is disabled, because the return value
36 // of time_get_os_boottime_ms() is 32-bits integer that could wrap-around
37 // in 49.7 days. It should be re-enabled if/after the wrap-around issue
38 // is resolved (e.g., if the return value is 64-bits integer).
39 //
40 #if 0
41 TEST_F(TimeTest, test_time_get_os_boottime_ms_not_zero) {
42 uint32_t t1 = time_get_os_boottime_ms();
43 ASSERT_TRUE(t1 > 0);
44 }
45 #endif
46
47 //
48 // Test that the return value of time_get_os_boottime_ms()
49 // is monotonically increasing within reasonable boundries.
50 //
TEST_F(TimeTest,test_time_get_os_boottime_ms_increases_upper_bound)51 TEST_F(TimeTest, test_time_get_os_boottime_ms_increases_upper_bound) {
52 uint32_t t1 = time_get_os_boottime_ms();
53 uint32_t t2 = time_get_os_boottime_ms();
54 ASSERT_TRUE((t2 - t1) < TEST_TIME_DELTA_UPPER_BOUND_MS);
55 }
56
57 //
58 // Test that the return value of time_get_os_boottime_ms()
59 // is increasing.
60 //
TEST_F(TimeTest,test_time_get_os_boottime_ms_increases_lower_bound)61 TEST_F(TimeTest, test_time_get_os_boottime_ms_increases_lower_bound) {
62 static const uint32_t TEST_TIME_SLEEP_MS = 100;
63 struct timespec delay;
64
65 delay.tv_sec = TEST_TIME_SLEEP_MS / 1000;
66 delay.tv_nsec = 1000 * 1000 * (TEST_TIME_SLEEP_MS % 1000);
67
68 // Take two timestamps with sleep in-between
69 uint32_t t1 = time_get_os_boottime_ms();
70 int err = nanosleep(&delay, &delay);
71 uint32_t t2 = time_get_os_boottime_ms();
72
73 ASSERT_TRUE(err == 0);
74 ASSERT_TRUE((t2 - t1) >= TEST_TIME_SLEEP_MS);
75 ASSERT_TRUE((t2 - t1) < TEST_TIME_DELTA_UPPER_BOUND_MS);
76 }
77