1 /*
2  * Copyright (c) 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 
16 #include <gtest/gtest.h>
17 #include <ctime>
18 #include <securec.h>
19 #include <string>
20 #include <vector>
21 #include "dfx_define.h"
22 #include "dfx_util.h"
23 #include "dfx_dump_res.h"
24 #include "dfx_log.h"
25 #include "string_util.h"
26 
27 using namespace OHOS::HiviewDFX;
28 using namespace testing::ext;
29 using namespace std;
30 
31 namespace OHOS {
32 namespace HiviewDFX {
33 class CommonTest : public testing::Test {
34 public:
SetUpTestCase(void)35     static void SetUpTestCase(void) {}
TearDownTestCase(void)36     static void TearDownTestCase(void) {}
SetUp()37     void SetUp() {}
TearDown()38     void TearDown() {}
39 };
40 
41 namespace {
42 #ifdef LOG_DOMAIN
43 #undef LOG_DOMAIN
44 #define LOG_DOMAIN 0xD002D11
45 #endif
46 
47 #ifdef LOG_TAG
48 #undef LOG_TAG
49 #define LOG_TAG "DfxCommonTest"
50 #endif
51 
52 /**
53  * @tc.name: DfxUtilTest001
54  * @tc.desc: test DfxUtil GetCurrentTimeStr
55  * @tc.type: FUNC
56  */
57 HWTEST_F(CommonTest, DfxUtilTest001, TestSize.Level2)
58 {
59     GTEST_LOG_(INFO) << "DfxUtilTest001: start.";
60     time_t now = time(nullptr);
61     std::string timeStr = GetCurrentTimeStr(static_cast<uint64_t>(now));
62     GTEST_LOG_(INFO) << timeStr;
63     ASSERT_NE(timeStr, "invalid timestamp\n");
64     now += 100000; // 100000 : test time offset
65     timeStr = GetCurrentTimeStr(static_cast<uint64_t>(now));
66     GTEST_LOG_(INFO) << timeStr;
67     ASSERT_NE(timeStr, "invalid timestamp\n");
68     GTEST_LOG_(INFO) << "DfxUtilTest001: end.";
69 }
70 
71 /**
72  * @tc.name: DfxUtilTest002
73  * @tc.desc: test DfxUtil TrimAndDupStr
74  * @tc.type: FUNC
75  */
76 HWTEST_F(CommonTest, DfxUtilTest002, TestSize.Level2)
77 {
78     GTEST_LOG_(INFO) << "DfxUtilTest002: start.";
79     std::string testStr = " abcd  ";
80     std::string resStr;
81     bool ret = TrimAndDupStr(testStr, resStr);
82     ASSERT_EQ(ret, true);
83     ASSERT_EQ(resStr, "abcd");
84     GTEST_LOG_(INFO) << "DfxUtilTest002: end.";
85 }
86 
87 /**
88  * @tc.name: DfxDumpResTest001
89  * @tc.desc: test DfxDumpRes functions
90  * @tc.type: FUNC
91  */
92 HWTEST_F(CommonTest, DfxDumpResTest001, TestSize.Level2)
93 {
94     GTEST_LOG_(INFO) << "DfxDumpResTest001: start.";
95     int32_t res = DUMP_ESUCCESS;
96     GTEST_LOG_(INFO) << DfxDumpRes::ToString(res);
97     GTEST_LOG_(INFO) << "DfxDumpResTest001: end.";
98 }
99 
100 /**
101  * @tc.name: DfxLogTest001
102  * @tc.desc: test DfxLog functions
103  * @tc.type: FUNC
104  */
105 HWTEST_F(CommonTest, DfxLogTest001, TestSize.Level2)
106 {
107     GTEST_LOG_(INFO) << "DfxLogTest001: start.";
108     InitDebugFd(STDERR_FILENO);
109     EXPECT_FALSE(CheckDebugLevel());
110     GTEST_LOG_(INFO) << "DfxLogTest001: end.";
111 }
112 
113 /**
114  * @tc.name: StringUtilTest001
115  * @tc.desc: test StartsWith functions
116  * @tc.type: FUNC
117  */
118 HWTEST_F(CommonTest, StringUtilTest001, TestSize.Level2)
119 {
120     GTEST_LOG_(INFO) << "StringUtilTest001: start.";
121     std::string start = "[anon:ArkTS Code2024]";
122     bool ret = StartsWith(start, "[anon:ArkTS Code");
123     EXPECT_TRUE(ret);
124     std::string end = "test.hap";
125     ret = EndsWith(end, ".hap");
126     EXPECT_TRUE(ret);
127     GTEST_LOG_(INFO) << "StringUtilTest001: end.";
128 }
129 }
130 } // namespace HiviewDFX
131 } // namespace OHOS