• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
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 <cstdlib>
17 #include <filesystem>
18 
19 #include <gtest/hwext/gtest-ext.h>
20 #include <gtest/hwext/gtest-tag.h>
21 
22 #include <sys/stat.h>
23 
24 #include "hidebug_util.h"
25 
26 using namespace testing::ext;
27 
28 namespace OHOS {
29 namespace HiviewDFX {
30 class HidebugUtilTest : public ::testing::Test {
31 protected:
SetUp()32     void SetUp() override {}
TearDown()33     void TearDown() override
34     {
35         system("param set hiviewdfx.debugenv.hidebug_test 0");
36         system("param set libc.hook_mode 0");
37     }
38 };
39 
40 /**
41  * @tc.name: GetRealNanoSecondsTimestampTest
42  * @tc.desc: test GetRealNanoSecondsTimestamp.
43  * @tc.type: FUNC
44  */
45 HWTEST_F(HidebugUtilTest, GetRealNanoSecondsTimestampTest, TestSize.Level1)
46 {
47     EXPECT_GE(GetRealNanoSecondsTimestamp(), 0);
48 }
49 
50 /**
51  * @tc.name: GetElapsedNanoSecondsSinceBootTest
52  * @tc.desc: test GetElapsedNanoSecondsSinceBoot.
53  * @tc.type: FUNC
54  */
55 HWTEST_F(HidebugUtilTest, GetElapsedNanoSecondsSinceBootTest, TestSize.Level1)
56 {
57     EXPECT_GE(GetElapsedNanoSecondsSinceBoot(), 0);
58 }
59 
60 /**
61  * @tc.name: GetProcessDirTest
62  * @tc.desc: test GetProcessDir.
63  * @tc.type: FUNC
64  */
65 HWTEST_F(HidebugUtilTest, GetProcessDirTest, TestSize.Level1)
66 {
67     std::string cacheDir = GetProcessDir(DirectoryType::CACHE);
68     GTEST_LOG_(INFO) << "DumpCatcherCommandTest001: cacheDir." << cacheDir;
69     std::string fileDir = GetProcessDir(DirectoryType::FILE);
70     GTEST_LOG_(INFO) << "DumpCatcherCommandTest001: cacheDir." << fileDir;
71     std::string otherDir = GetProcessDir(static_cast<DirectoryType>(3));
72     GTEST_LOG_(INFO) << "DumpCatcherCommandTest001: cacheDir." << fileDir;
73     EXPECT_GE(fileDir.length(), 0);
74 }
75 
76 /**
77  * @tc.name: SplitStrTest
78  * @tc.desc: test SplitStr.
79  * @tc.type: FUNC
80  */
81 HWTEST_F(HidebugUtilTest, SplitStrTest, TestSize.Level1)
82 {
83     auto split1 = SplitStr("/test/", '/');
84     EXPECT_EQ(split1.size(), 3);
85     EXPECT_STREQ(split1[1].c_str(), "test");
__anon4d6366ea0102(std::string& s) 86     auto split2 = SplitStr("/test/", '/', [](std::string& s) {
87         return !s.empty();
88     });
89     EXPECT_EQ(split2.size(), 1);
90     EXPECT_STREQ(split2[0].c_str(), "test");
91 }
92 
93 /**
94  * @tc.name: IsLegalPathTest
95  * @tc.desc: test IsLegalPath.
96  * @tc.type: FUNC
97  */
98 HWTEST_F(HidebugUtilTest, IsLegalPathTest, TestSize.Level1)
99 {
100     EXPECT_FALSE(IsLegalPath("./"));
101     EXPECT_FALSE(IsLegalPath(""));
102     EXPECT_TRUE(IsLegalPath("/test"));
103 }
104 
105 /**
106  * @tc.name: SmartFileTest
107  * @tc.desc: test SmartFile.
108  * @tc.type: FUNC
109  */
110 HWTEST_F(HidebugUtilTest, SmartFileTest, TestSize.Level1)
111 {
112     ASSERT_EQ(SmartFile::OpenFile("./", "r"), nullptr);
113     constexpr auto testFile = "/data/test/testSmartFile.txt";
114     ASSERT_EQ(GetFileSize(testFile), 0);
115     ASSERT_EQ(SmartFile::OpenFile(testFile, "r"), nullptr);
116     auto testFilePtr = SmartFile::OpenFile(testFile, "w+");
117     ASSERT_NE(SmartFile::OpenFile(testFile, "r"), nullptr);
118     int writeDATA = 10;
119     ASSERT_TRUE(testFilePtr->Write(&writeDATA, sizeof(writeDATA), 1));
120     testFilePtr = nullptr;
121     ASSERT_GT(GetFileSize(testFile), 0);
122     testFilePtr = SmartFile::OpenFile(testFile, "r");
123     ASSERT_FALSE(testFilePtr->Write(&writeDATA, sizeof(writeDATA), 1));
124     int readData = 0;
125     ASSERT_GT(testFilePtr->Read(&readData, sizeof(readData), 1), 0);
126     ASSERT_EQ(readData, writeDATA);
127     remove(testFile);
128 }
129 
130 /**
131  * @tc.name: CreateDirectoryTest
132  * @tc.desc: test CreateDirectory.
133  * @tc.type: FUNC
134  */
135 HWTEST_F(HidebugUtilTest, CreateDirectoryTest, TestSize.Level1)
136 {
137     constexpr auto testFileDir = "/data/test/testCreateDirectory";
138     ASSERT_TRUE(CreateDirectory(testFileDir, 0000));
139     std::string filePath = std::string(testFileDir) + std::string("/") + std::string(256, 'a');
140     ASSERT_FALSE(CreateDirectory(filePath, 0000));
141     remove(testFileDir);
142 }
143 
144 /**
145  * @tc.name: CreateFileTest
146  * @tc.desc: test CreateFile.
147  * @tc.type: FUNC
148  */
149 HWTEST_F(HidebugUtilTest, CreateFileTest, TestSize.Level1)
150 {
151     constexpr auto testFile = "/data/test/test/test.txt";
152     ASSERT_FALSE(CreateFile(testFile));
153     constexpr auto testFileDir = "/data/test/test";
154     ASSERT_TRUE(CreateDirectory(testFileDir, 0755));
155     ASSERT_TRUE(CreateFile(testFile));
156     ASSERT_TRUE(CreateFile(testFile));
157     remove(testFile);
158     remove(testFileDir);
159 }
160 
161 /**
162  * @tc.name: XAttrTest
163  * @tc.desc: test XAttr.
164  * @tc.type: FUNC
165  */
166 HWTEST_F(HidebugUtilTest, XAttrTest, TestSize.Level1)
167 {
168     constexpr auto key = "user.test";
169     constexpr auto value = "test";
170     constexpr auto testFile = "/data/test/testXAttr.txt";
171     ASSERT_FALSE(SetXAttr(testFile, key, value));
172     std::string readValue;
173     ASSERT_FALSE(GetXAttr(testFile, key, readValue, 128));
174     ASSERT_TRUE(CreateFile(testFile));
175     ASSERT_TRUE(SetXAttr(testFile, key, value));
176     ASSERT_TRUE(GetXAttr(testFile, key, readValue, 128));
177     ASSERT_STREQ(readValue.c_str(), "test");
178     remove(testFile);
179 }
180 
181 /**
182  * @tc.name: IsBetaVersionTest
183  * @tc.desc: test IsBetaVersion.
184  * @tc.type: FUNC
185  */
186 HWTEST_F(HidebugUtilTest, IsBetaVersionTest, TestSize.Level1)
187 {
188     IsBetaVersion();
189     ASSERT_TRUE(true);
190 }
191 
192 /**
193  * @tc.name: IsDebuggableHapTest
194  * @tc.desc: test IsDebuggableHap.
195  * @tc.type: FUNC
196  */
197 HWTEST_F(HidebugUtilTest, IsDebuggableHapTest, TestSize.Level1)
198 {
199     unsetenv("HAP_DEBUGGABLE");
200     EXPECT_FALSE(IsDebuggableHap());
201     setenv("HAP_DEBUGGABLE", "true", 1);
202     EXPECT_TRUE(IsDebuggableHap());
203 }
204 
205 /**
206  * @tc.name: IsDeveloperOptionsEnabledTest
207  * @tc.desc: test IsDeveloperOptionsEnabled.
208  * @tc.type: FUNC
209  */
210 HWTEST_F(HidebugUtilTest, IsDeveloperOptionsEnabledTest, TestSize.Level1)
211 {
212     IsDeveloperOptionsEnabled();
213     ASSERT_TRUE(true);
214 }
215 }
216 }