1 /*
2  * Copyright (C) 2024 HiHope Open Source Organization.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <iostream>
17 #include <unistd.h>
18 #include <gtest/gtest.h>
19 #include <sys/timex.h>
20 #include <sys/times.h>
21 #include "securec.h"
22 
23 using namespace testing::ext;
24 
25 class HatsAdjTimexApiTest : public testing::Test {
26 public:
27 static void SetUpTestCase();
28 static void TearDownTestCase();
29 void SetUp();
30 void TearDown();
31 private:
32 };
SetUp()33 void HatsAdjTimexApiTest::SetUp()
34 {
35 }
TearDown()36 void HatsAdjTimexApiTest::TearDown()
37 {
38 }
SetUpTestCase()39 void HatsAdjTimexApiTest::SetUpTestCase()
40 {
41 }
TearDownTestCase()42 void HatsAdjTimexApiTest::TearDownTestCase()
43 {
44 }
45 
46 /*
47  * @tc.number : SUB_KERNEL_SYSCALL_ADJTIMEX_0100
48  * @tc.name   : AdjTimexGetCurrentTimeSuccess_0001
49  * @tc.desc   : Test the adjtimex get current time success.
50  * @tc.size   : MediumTest
51  * @tc.type   : Function
52  * @tc.level  : Level 1
53  */
HWTEST_F(HatsAdjTimexApiTest, AdjTimexGetCurrentTimeSuccess_0001, Function | MediumTest | Level1)54 HWTEST_F(HatsAdjTimexApiTest, AdjTimexGetCurrentTimeSuccess_0001, Function | MediumTest | Level1)
55 {
56     struct timex tx;
57     tx.modes = 0;
58     int result = adjtimex(&tx);
59     EXPECT_GE(result, 0);
60 
61     struct timeval now;
62     gettimeofday(&now, nullptr);
63 
64     EXPECT_NEAR(tx.time.tv_sec, now.tv_sec, 1);
65 }
66 
67 /*
68  * @tc.number : SUB_KERNEL_SYSCALL_CLOCK_ADJTIMEX_0200
69  * @tc.name   : ClockAdjTimexGetCurrentTimeSuccess_0002
70  * @tc.desc   : Test the clock_adjtimex get current time success.
71  * @tc.size   : MediumTest
72  * @tc.type   : Function
73  * @tc.level  : Level 1
74  */
HWTEST_F(HatsAdjTimexApiTest, ClockAdjTimexGetCurrentTimeSuccess_0002, Function | MediumTest | Level1)75 HWTEST_F(HatsAdjTimexApiTest, ClockAdjTimexGetCurrentTimeSuccess_0002, Function | MediumTest | Level1)
76 {
77     struct timex tx;
78     tx.modes = 0;
79     clockid_t clock_id = CLOCK_REALTIME;
80     int result = clock_adjtime(clock_id, &tx);
81     EXPECT_GE(result, 0);
82 
83     struct timespec now;
84     clock_gettime(clock_id, &now);
85 
86     EXPECT_NEAR(tx.time.tv_sec, now.tv_sec, 1);
87 }
88