• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 /* This files contains unit test of process module. */
17 
18 #include "process_dfx_test.h"
19 
20 #include <memory>
21 
22 #include "dfx_process.h"
23 
24 using namespace OHOS::HiviewDFX;
25 using namespace testing::ext;
26 using namespace std;
27 
SetUpTestCase(void)28 void ProcessDfxTest::SetUpTestCase(void)
29 {
30 }
31 
32 
TearDownTestCase(void)33 void ProcessDfxTest::TearDownTestCase(void)
34 {
35 }
36 
SetUp(void)37 void ProcessDfxTest::SetUp(void)
38 {
39 }
40 
TearDown(void)41 void ProcessDfxTest::TearDown(void)
42 {
43 }
44 
GetAccountmgrPid()45 pid_t ProcessDfxTest::GetAccountmgrPid()
46 {
47     std::string procCMD = "pgrep 'accountmgr'";
48     GTEST_LOG_(INFO) << "threadCMD = " << procCMD;
49     FILE *procFileInfo = nullptr;
50     procFileInfo = popen(procCMD.c_str(), "r");
51     if (procFileInfo == nullptr) {
52         perror("popen execute failed");
53         return 0;
54     }
55     std::string pidLog;
56     pid_t accountmgrPid = 0;
57     char result_buf_shell[NAME_LEN] = { 0, };
58     if (fgets(result_buf_shell, sizeof(result_buf_shell), procFileInfo) != nullptr) {
59         pidLog = result_buf_shell;
60         accountmgrPid = atoi(pidLog.c_str());
61     }
62     pclose(procFileInfo);
63     return accountmgrPid;
64 }
65 
66 namespace {
67 /**
68  * @tc.name: ProcessDfxRequestTest001
69  * @tc.desc: test cinit process maps node
70  * @tc.type: FUNC
71  */
72 HWTEST_F (ProcessDfxTest, ProcessDfxRequestTest001, TestSize.Level2)
73 {
74     GTEST_LOG_(INFO) << "ProcessDfxRequestTest001: start.";
75     std::shared_ptr<DfxProcess> processDfx = std::make_shared<DfxProcess>();
76     auto dfx = false;
77     if (processDfx != nullptr) {
78         dfx = processDfx->InitProcessMaps();
79     }
80     EXPECT_EQ(true, dfx != true) << "ProcessDfxRequestTest001 Failed";
81     GTEST_LOG_(INFO) << "ProcessDfxRequestTest001: end.";
82 }
83 
84 /**
85  * @tc.name: ProcessDfxRequestTest002
86  * @tc.desc: test init process threads
87  * @tc.type: FUNC
88  */
89 HWTEST_F (ProcessDfxTest, ProcessDfxRequestTest002, TestSize.Level2)
90 {
91     GTEST_LOG_(INFO) << "ProcessDfxRequestTest002: start.";
92     pid_t accountmgrPid = ProcessDfxTest::GetAccountmgrPid();
93     if (accountmgrPid == 0) {
94         GTEST_LOG_(INFO) << "ProcessDfxRequestTest002: get pid failed.";
95         return;
96     }
97     std::shared_ptr<DfxProcess> processDfx = std::make_shared<DfxProcess>();
98     pid_t pid = accountmgrPid;
99     pid_t tid = accountmgrPid;
100     std::shared_ptr<DfxThread> keyThread = std::make_shared<DfxThread>(pid, tid, tid);
101     auto dfx = false;
102     if (processDfx != nullptr && keyThread != nullptr) {
103         dfx = processDfx->InitProcessThreads(keyThread);
104     }
105     EXPECT_EQ(true, dfx == true) << "ProcessDfxRequestTest002 Failed";
106     GTEST_LOG_(INFO) << "ProcessDfxRequestTest002: end.";
107 }
108 
109 /**
110  * @tc.name: ProcessDfxRequestTest003
111  * @tc.desc: test init other threads
112  * @tc.type: FUNC
113  */
114 HWTEST_F (ProcessDfxTest, ProcessDfxRequestTest003, TestSize.Level2)
115 {
116     GTEST_LOG_(INFO) << "ProcessDfxRequestTest003: start.";
117     std::shared_ptr<DfxProcess> processDfx = std::make_shared<DfxProcess>();
118     auto dfx = false;
119     if (processDfx != nullptr) {
120         dfx = processDfx->InitOtherThreads();
121     }
122     EXPECT_EQ(true, dfx != true) << "ProcessDfxRequestTest003 Failed";
123     GTEST_LOG_(INFO) << "ProcessDfxRequestTest003: end.";
124 }
125 
126 /**
127  * @tc.name: ProcessDfxRequestTest004
128  * @tc.desc: test get pid
129  * @tc.type: FUNC
130  */
131 HWTEST_F (ProcessDfxTest, ProcessDfxRequestTest004, TestSize.Level2)
132 {
133     GTEST_LOG_(INFO) << "ProcessDfxRequestTest004: start.";
134     std::shared_ptr<DfxProcess> processDfx = std::make_shared<DfxProcess>();
135     pid_t pid = 1;
136     auto getpid = 0;
137     if (processDfx != nullptr) {
138         processDfx->SetPid(pid);
139         sleep(1);
140         getpid = processDfx->GetPid();
141     }
142     EXPECT_EQ(true, getpid == pid) << "ProcessDfxRequestTest004 Failed";
143     GTEST_LOG_(INFO) << "ProcessDfxRequestTest004: end.";
144 }
145 
146 /**
147  * @tc.name: ProcessDfxRequestTest005
148  * @tc.desc: test get pid
149  * @tc.type: FUNC
150  */
151 HWTEST_F (ProcessDfxTest, ProcessDfxRequestTest005, TestSize.Level2)
152 {
153     GTEST_LOG_(INFO) << "ProcessDfxRequestTest005: start.";
154     std::shared_ptr<DfxProcess> processDfx = std::make_shared<DfxProcess>();
155     pid_t pid = 164;
156     auto getpid = 0;
157     if (processDfx != nullptr) {
158         processDfx->SetPid(pid);
159         sleep(1);
160         getpid = processDfx->GetPid();
161     }
162     EXPECT_EQ(true, getpid == pid) << "ProcessDfxRequestTest005 Failed";
163     GTEST_LOG_(INFO) << "ProcessDfxRequestTest005: end.";
164 }
165 
166 /**
167  * @tc.name: ProcessDfxRequestTest006
168  * @tc.desc: test get pid
169  * @tc.type: FUNC
170  */
171 HWTEST_F (ProcessDfxTest, ProcessDfxRequestTest006, TestSize.Level2)
172 {
173     GTEST_LOG_(INFO) << "ProcessDfxRequestTest006: start.";
174     std::shared_ptr<DfxProcess> processDfx = std::make_shared<DfxProcess>();
175     pid_t pid = 10000;
176     auto getpid = 0;
177     if (processDfx != nullptr) {
178         processDfx->SetPid(pid);
179         getpid = processDfx->GetPid();
180     }
181     EXPECT_EQ(true, getpid == pid) << "ProcessDfxRequestTest006 Failed";
182     GTEST_LOG_(INFO) << "ProcessDfxRequestTest006: end.";
183 }
184 
185 /**
186  * @tc.name: ProcessDfxRequestTest007
187  * @tc.desc: test get uid
188  * @tc.type: FUNC
189  */
190 HWTEST_F (ProcessDfxTest, ProcessDfxRequestTest007, TestSize.Level2)
191 {
192     GTEST_LOG_(INFO) << "ProcessDfxRequestTest007: start.";
193     std::shared_ptr<DfxProcess> processDfx = std::make_shared<DfxProcess>();
194     uid_t uid = 1;
195     uid_t getuid = 0;
196     if (processDfx != nullptr) {
197         processDfx->SetUid(uid);
198         getuid = processDfx->GetUid();
199     }
200     EXPECT_EQ(true, getuid == uid) << "ProcessDfxRequestTest007 Failed";
201     GTEST_LOG_(INFO) << "ProcessDfxRequestTest007: end.";
202 }
203 
204 /**
205  * @tc.name: ProcessDfxRequestTest008
206  * @tc.desc: test get uid
207  * @tc.type: FUNC
208  */
209 HWTEST_F (ProcessDfxTest, ProcessDfxRequestTest008, TestSize.Level2)
210 {
211     GTEST_LOG_(INFO) << "ProcessDfxRequestTest008: start.";
212     std::shared_ptr<DfxProcess> processDfx = std::make_shared<DfxProcess>();
213     uid_t uid = 164;
214     uid_t getuid = 0;
215     if (processDfx != nullptr) {
216         processDfx->SetUid(uid);
217         getuid = processDfx->GetUid();
218     }
219     EXPECT_EQ(true, getuid == uid) << "ProcessDfxRequestTest008 Failed";
220     GTEST_LOG_(INFO) << "ProcessDfxRequestTest008: end.";
221 }
222 
223 /**
224  * @tc.name: ProcessDfxRequestTest009
225  * @tc.desc: test get uid
226  * @tc.type: FUNC
227  */
228 HWTEST_F (ProcessDfxTest, ProcessDfxRequestTest009, TestSize.Level2)
229 {
230     GTEST_LOG_(INFO) << "ProcessDfxRequestTest009: start.";
231     std::shared_ptr<DfxProcess> processDfx = std::make_shared<DfxProcess>();
232     uid_t uid = -1;
233     uid_t getuid = 0;
234     if (processDfx != nullptr) {
235         processDfx->SetUid(uid);
236         getuid = processDfx->GetUid();
237     }
238     EXPECT_EQ(true, getuid == uid) << "ProcessDfxRequestTest009 Failed";
239     GTEST_LOG_(INFO) << "ProcessDfxRequestTest009: end.";
240 }
241 
242 /**
243  * @tc.name: ProcessDfxRequestTest010
244  * @tc.desc: test get process name
245  * @tc.type: FUNC
246  */
247 HWTEST_F (ProcessDfxTest, ProcessDfxRequestTest010, TestSize.Level2)
248 {
249     GTEST_LOG_(INFO) << "ProcessDfxRequestTest010: start.";
250     std::shared_ptr<DfxProcess> processDfx = std::make_shared<DfxProcess>();
251     std::string input = "processName";
252     std::string output = "";
253     if (processDfx != nullptr) {
254         processDfx->SetProcessName(input);
255         output =  processDfx->GetProcessName();
256     }
257     EXPECT_EQ(true, input == output) << "ProcessDfxRequestTest010 Failed";
258     GTEST_LOG_(INFO) << "ProcessDfxRequestTest010: end.";
259 }
260 
261 /**
262  * @tc.name: ProcessDfxRequestTest011
263  * @tc.desc: test get map
264  * @tc.type: FUNC
265  */
266 HWTEST_F (ProcessDfxTest, ProcessDfxRequestTest011, TestSize.Level2)
267 {
268     GTEST_LOG_(INFO) << "ProcessDfxRequestTest011: start.";
269     std::shared_ptr<DfxProcess> processDfx = std::make_shared<DfxProcess>();
270     std::shared_ptr<DfxElfMaps> maps = std::make_shared<DfxElfMaps>();
271     std::shared_ptr<DfxElfMaps> output = std::make_shared<DfxElfMaps>();
272     if (processDfx != nullptr && maps != nullptr) {
273         processDfx->SetMaps(maps);
274         output = processDfx->GetMaps();
275     }
276     EXPECT_EQ(true, output == maps) << "ProcessDfxRequestTest011 Failed";
277     GTEST_LOG_(INFO) << "ProcessDfxRequestTest011: end.";
278 }
279 
280 /**
281  * @tc.name: ProcessDfxRequestTest012
282  * @tc.desc: test get threads
283  * @tc.type: FUNC
284  */
285 HWTEST_F (ProcessDfxTest, ProcessDfxRequestTest012, TestSize.Level2)
286 {
287     GTEST_LOG_(INFO) << "ProcessDfxRequestTest012: start.";
288     std::shared_ptr<DfxProcess> processDfx = std::make_shared<DfxProcess>();
289     pid_t pid = 100;
290     pid_t tid = 100;
291     std::shared_ptr<DfxThread> thread = std::make_shared<DfxThread>(pid, tid, tid);
292     std::vector<std::shared_ptr<DfxThread>> input;
293     std::vector<std::shared_ptr<DfxThread>> output;
294     if (processDfx != nullptr && thread != nullptr) {
295         processDfx->SetThreads(input);
296         input.push_back(thread);
297         output = processDfx->GetThreads();
298     }
299     EXPECT_EQ(false, input.size() == output.size()) << "ProcessDfxRequestTest012 Failed";
300     GTEST_LOG_(INFO) << "ProcessDfxRequestTest012: end.";
301 }
302 }
303