• 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 = processDfx->InitProcessMaps();
77     EXPECT_EQ(false, dfx) << "ProcessDfxRequestTest001 Failed";
78     GTEST_LOG_(INFO) << "ProcessDfxRequestTest001: end.";
79 }
80 
81 /**
82  * @tc.name: ProcessDfxRequestTest002
83  * @tc.desc: test init process threads
84  * @tc.type: FUNC
85  */
86 HWTEST_F (ProcessDfxTest, ProcessDfxRequestTest002, TestSize.Level2)
87 {
88     GTEST_LOG_(INFO) << "ProcessDfxRequestTest002: start.";
89     pid_t accountmgrPid = ProcessDfxTest::GetAccountmgrPid();
90     if (accountmgrPid == 0) {
91         GTEST_LOG_(INFO) << "ProcessDfxRequestTest002: get pid failed.";
92         return;
93     }
94     std::shared_ptr<DfxProcess> processDfx = std::make_shared<DfxProcess>();
95     pid_t pid = accountmgrPid;
96     pid_t tid = accountmgrPid;
97     std::shared_ptr<DfxThread> keyThread = std::make_shared<DfxThread>(pid, tid, tid);
98     auto dfx = processDfx->InitProcessThreads(keyThread);
99     EXPECT_EQ(true, dfx) << "ProcessDfxRequestTest002 Failed";
100     GTEST_LOG_(INFO) << "ProcessDfxRequestTest002: end.";
101 }
102 
103 /**
104  * @tc.name: ProcessDfxRequestTest003
105  * @tc.desc: test init other threads
106  * @tc.type: FUNC
107  */
108 HWTEST_F (ProcessDfxTest, ProcessDfxRequestTest003, TestSize.Level2)
109 {
110     GTEST_LOG_(INFO) << "ProcessDfxRequestTest003: start.";
111     std::shared_ptr<DfxProcess> processDfx = std::make_shared<DfxProcess>();
112     auto dfx = processDfx->InitOtherThreads();
113     EXPECT_EQ(false, dfx) << "ProcessDfxRequestTest003 Failed";
114     GTEST_LOG_(INFO) << "ProcessDfxRequestTest003: end.";
115 }
116 
117 /**
118  * @tc.name: ProcessDfxRequestTest004
119  * @tc.desc: test get pid
120  * @tc.type: FUNC
121  */
122 HWTEST_F (ProcessDfxTest, ProcessDfxRequestTest004, TestSize.Level2)
123 {
124     GTEST_LOG_(INFO) << "ProcessDfxRequestTest004: start.";
125     std::shared_ptr<DfxProcess> processDfx = std::make_shared<DfxProcess>();
126     pid_t pid = 1;
127     processDfx->SetPid(pid);
128     sleep(1);
129     auto getpid = processDfx->GetPid();
130     EXPECT_EQ(true, getpid == pid) << "ProcessDfxRequestTest004 Failed";
131     GTEST_LOG_(INFO) << "ProcessDfxRequestTest004: end.";
132 }
133 
134 /**
135  * @tc.name: ProcessDfxRequestTest005
136  * @tc.desc: test get pid
137  * @tc.type: FUNC
138  */
139 HWTEST_F (ProcessDfxTest, ProcessDfxRequestTest005, TestSize.Level2)
140 {
141     GTEST_LOG_(INFO) << "ProcessDfxRequestTest005: start.";
142     std::shared_ptr<DfxProcess> processDfx = std::make_shared<DfxProcess>();
143     pid_t pid = 164;
144     processDfx->SetPid(pid);
145     sleep(1);
146     auto getpid = processDfx->GetPid();
147     EXPECT_EQ(true, getpid == pid) << "ProcessDfxRequestTest005 Failed";
148     GTEST_LOG_(INFO) << "ProcessDfxRequestTest005: end.";
149 }
150 
151 /**
152  * @tc.name: ProcessDfxRequestTest006
153  * @tc.desc: test get pid
154  * @tc.type: FUNC
155  */
156 HWTEST_F (ProcessDfxTest, ProcessDfxRequestTest006, TestSize.Level2)
157 {
158     GTEST_LOG_(INFO) << "ProcessDfxRequestTest006: start.";
159     std::shared_ptr<DfxProcess> processDfx = std::make_shared<DfxProcess>();
160     pid_t pid = 10000;
161     processDfx->SetPid(pid);
162     auto getpid = processDfx->GetPid();
163     EXPECT_EQ(true, getpid == pid) << "ProcessDfxRequestTest006 Failed";
164     GTEST_LOG_(INFO) << "ProcessDfxRequestTest006: end.";
165 }
166 
167 /**
168  * @tc.name: ProcessDfxRequestTest007
169  * @tc.desc: test get uid
170  * @tc.type: FUNC
171  */
172 HWTEST_F (ProcessDfxTest, ProcessDfxRequestTest007, TestSize.Level2)
173 {
174     GTEST_LOG_(INFO) << "ProcessDfxRequestTest007: start.";
175     std::shared_ptr<DfxProcess> processDfx = std::make_shared<DfxProcess>();
176     uid_t uid = 1;
177     processDfx->SetUid(uid);
178     auto getuid = processDfx->GetUid();
179     EXPECT_EQ(true, getuid == uid) << "ProcessDfxRequestTest007 Failed";
180     GTEST_LOG_(INFO) << "ProcessDfxRequestTest007: end.";
181 }
182 
183 /**
184  * @tc.name: ProcessDfxRequestTest008
185  * @tc.desc: test get uid
186  * @tc.type: FUNC
187  */
188 HWTEST_F (ProcessDfxTest, ProcessDfxRequestTest008, TestSize.Level2)
189 {
190     GTEST_LOG_(INFO) << "ProcessDfxRequestTest008: start.";
191     std::shared_ptr<DfxProcess> processDfx = std::make_shared<DfxProcess>();
192     uid_t uid = 164;
193     processDfx->SetUid(uid);
194     auto getuid = processDfx->GetUid();
195     EXPECT_EQ(true, getuid == uid) << "ProcessDfxRequestTest008 Failed";
196     GTEST_LOG_(INFO) << "ProcessDfxRequestTest008: end.";
197 }
198 
199 /**
200  * @tc.name: ProcessDfxRequestTest009
201  * @tc.desc: test get uid
202  * @tc.type: FUNC
203  */
204 HWTEST_F (ProcessDfxTest, ProcessDfxRequestTest009, TestSize.Level2)
205 {
206     GTEST_LOG_(INFO) << "ProcessDfxRequestTest009: start.";
207     std::shared_ptr<DfxProcess> processDfx = std::make_shared<DfxProcess>();
208     uid_t uid = -1;
209     processDfx->SetUid(uid);
210     auto getuid = processDfx->GetUid();
211     EXPECT_EQ(true, getuid == uid) << "ProcessDfxRequestTest009 Failed";
212     GTEST_LOG_(INFO) << "ProcessDfxRequestTest009: end.";
213 }
214 
215 /**
216  * @tc.name: ProcessDfxRequestTest010
217  * @tc.desc: test get process name
218  * @tc.type: FUNC
219  */
220 HWTEST_F (ProcessDfxTest, ProcessDfxRequestTest010, TestSize.Level2)
221 {
222     GTEST_LOG_(INFO) << "ProcessDfxRequestTest010: start.";
223     std::shared_ptr<DfxProcess> processDfx = std::make_shared<DfxProcess>();
224     std::string input = "processName";
225     processDfx->SetProcessName(input);
226     auto output = processDfx->GetProcessName();
227     EXPECT_EQ(true, input == output) << "ProcessDfxRequestTest010 Failed";
228     GTEST_LOG_(INFO) << "ProcessDfxRequestTest010: end.";
229 }
230 
231 /**
232  * @tc.name: ProcessDfxRequestTest011
233  * @tc.desc: test get map
234  * @tc.type: FUNC
235  */
236 HWTEST_F (ProcessDfxTest, ProcessDfxRequestTest011, TestSize.Level2)
237 {
238     GTEST_LOG_(INFO) << "ProcessDfxRequestTest011: start.";
239     std::shared_ptr<DfxProcess> processDfx = std::make_shared<DfxProcess>();
240     std::shared_ptr<DfxElfMaps> maps = std::make_shared<DfxElfMaps>();
241     processDfx->SetMaps(maps);
242     auto output = processDfx->GetMaps();
243     EXPECT_EQ(true, output == maps) << "ProcessDfxRequestTest011 Failed";
244     GTEST_LOG_(INFO) << "ProcessDfxRequestTest011: end.";
245 }
246 
247 /**
248  * @tc.name: ProcessDfxRequestTest012
249  * @tc.desc: test get threads
250  * @tc.type: FUNC
251  */
252 HWTEST_F (ProcessDfxTest, ProcessDfxRequestTest012, TestSize.Level2)
253 {
254     GTEST_LOG_(INFO) << "ProcessDfxRequestTest012: start.";
255     std::shared_ptr<DfxProcess> processDfx = std::make_shared<DfxProcess>();
256     pid_t pid = 100;
257     pid_t tid = 100;
258     std::shared_ptr<DfxThread> thread = std::make_shared<DfxThread>(pid, tid, tid);
259     std::vector<std::shared_ptr<DfxThread>> input;
260     processDfx->SetThreads(input);
261     input.push_back(thread);
262     auto output = processDfx->GetThreads();
263     EXPECT_EQ(false, input.size() == output.size()) << "ProcessDfxRequestTest012 Failed";
264     GTEST_LOG_(INFO) << "ProcessDfxRequestTest012: end.";
265 }
266 }
267