1 /*
2 * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
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 #include "gtest/gtest.h"
16 #include <securec.h>
17
18 #include "softbus_adapter_timer.h"
19 #include "softbus_def.h"
20 #include "softbus_error_code.h"
21
22 using namespace std;
23 using namespace testing::ext;
24
25 namespace OHOS {
26 const int32_t TIMER_TIMEOUT = 1000;
27
28 class SoftbusTimeTest : public testing::Test {
29 protected:
30 static void SetUpTestCase(void);
31 static void TearDownTestCase(void);
32 void SetUp();
33 void TearDown();
34 };
35
SetUpTestCase(void)36 void SoftbusTimeTest::SetUpTestCase(void) { }
37
TearDownTestCase(void)38 void SoftbusTimeTest::TearDownTestCase(void) { }
39
SetUp()40 void SoftbusTimeTest::SetUp() { }
41
TearDown()42 void SoftbusTimeTest::TearDown() { }
43
44 /*
45 * @tc.name: SoftBusTimerTest001
46 * @tc.desc: soft bus timer test
47 * @tc.type: FUNC
48 * @tc.require:
49 */
50 HWTEST_F(SoftbusTimeTest, SoftBusTimerTest001, TestSize.Level1)
51 {
52 void *timerId = nullptr;
53 SoftBusSysTime times = { 0 };
54 int32_t ret;
55
56 ret = SoftBusStartTimer(nullptr, TIMER_TIMEOUT);
57 EXPECT_EQ(SOFTBUS_ERR, ret);
58 SoftBusCreateTimer(nullptr, TIMER_TYPE_ONCE);
59 SoftBusCreateTimer(&timerId, TIMER_TYPE_ONCE);
60 ret = SoftBusStartTimer(timerId, TIMER_TIMEOUT);
61 EXPECT_NE(SOFTBUS_ERR, ret);
62 ret = SoftBusDeleteTimer(nullptr);
63 EXPECT_EQ(SOFTBUS_ERR, ret);
64 ret = SoftBusDeleteTimer(timerId);
65 EXPECT_EQ(SOFTBUS_OK, ret);
66 ret = SoftBusGetTime(nullptr);
67 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
68 ret = SoftBusGetTime(×);
69 EXPECT_EQ(SOFTBUS_OK, ret);
70 }
71
72 /*
73 * @tc.name: SoftBusTimerTest002
74 * @tc.desc: test SoftBusFormatTimestamp
75 * @tc.type: FUNC
76 * @tc.require:
77 */
78 HWTEST_F(SoftbusTimeTest, SoftBusTimerTest002, TestSize.Level1)
79 {
80 uint64_t timestamp1 = 946656000000;
81 const char *formated1 = SoftBusFormatTimestamp(timestamp1);
82 EXPECT_STREQ(formated1, "2000-01-01 00:00:00.000");
83
84 uint64_t timestamp2 = 1705984496789;
85 const char *formated2 = SoftBusFormatTimestamp(timestamp2);
86 EXPECT_STREQ(formated2, "2024-01-23 12:34:56.789");
87 }
88
89 #ifdef SOFTBUS_STANDARD_OS
90 /*
91 * @tc.name: SoftBusStartTimerWithFfrt001
92 * @tc.desc: SoftBusStartTimerWithFfrt will return SOFTBUS_INVALID_PARAM when timerHandle=nullptr
93 * @tc.type: FUNC
94 * @tc.require:
95 */
96 HWTEST_F(SoftbusTimeTest, SoftBusStartTimerWithFfrt001, TestSize.Level1)
97 {
98 uint64_t timeout = 1;
99 int32_t timerHandle = -1;
100 int32_t ret = SoftBusStartTimerWithFfrt(nullptr, timeout, 0);
101 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
102 SoftBusStopTimerWithFfrt(timerHandle);
103 }
104 #endif
105
106 /*
107 * @tc.name: SoftBusGetRealTime001
108 * @tc.desc: SoftBusGetRealTime will return SOFTBUS_INVALID_PARAM when sysTime=nullptr
109 * @tc.type: FUNC
110 * @tc.require:
111 */
112 HWTEST_F(SoftbusTimeTest, SoftBusGetRealTime001, TestSize.Level1)
113 {
114 int32_t ret = SoftBusGetRealTime(nullptr);
115 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
116 }
117 } // namespace OHOS
118