• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 "csignal"
18 #include "proc_util.h"
19 
20 using namespace testing::ext;
21 
22 namespace OHOS {
23 namespace HiviewDFX {
24 /**
25  * @tc.name: ParseStatTest001
26  * @tc.desc: test path invalid.
27  * @tc.type: FUNC
28  */
29 HWTEST(ProcUtilTest, ParseStatTest001, TestSize.Level2)
30 {
31     ProcessInfo info;
32     ASSERT_FALSE(ParseStat("", info));
33 }
34 
35 /**
36  * @tc.name: ParseStatTest002
37  * @tc.desc: parse file.
38  * @tc.type: FUNC
39  */
40 HWTEST(ProcUtilTest, ParseStatTest002, TestSize.Level2)
41 {
42     ProcessInfo info;
43     const std::string path = "/data/test/resource/proc/ParseStatTest002/stat";
44     ASSERT_TRUE(ParseStat(path, info));
45     ASSERT_EQ(info.pid, 1716);
46     ASSERT_EQ(info.comm, std::string("at-spi-bus-laun"));
47     ASSERT_EQ(info.state, ThreadState::RUNNING);
48     ASSERT_EQ(info.ppid, 1695);
49     ASSERT_EQ(info.utime, 0);
50     ASSERT_EQ(info.stime, 0);
51     ASSERT_EQ(info.cutime, 0);
52     ASSERT_EQ(info.cstime, 0);
53     ASSERT_EQ(info.priority, 20);
54     ASSERT_EQ(info.nice, 0);
55     ASSERT_EQ(info.numThreads, 4);
56     ASSERT_EQ(info.starttime, 1326);
57     ASSERT_EQ(info.rss, 1312);
58     ASSERT_EQ(info.signal, 0);
59     ASSERT_EQ(info.blocked, 0);
60     ASSERT_EQ(info.sigignore, 4096);
61     ASSERT_EQ(info.sigcatch, 81920);
62 }
63 
64 /**
65  * @tc.name: ParseStatTest003
66  * @tc.desc: parse file.
67  * @tc.type: FUNC
68  */
69 HWTEST(ProcUtilTest, ParseStatTest003, TestSize.Level2)
70 {
71     ProcessInfo info;
72     const std::string path = "/data/test/resource/proc/ParseStatTest003/stat";
73     ASSERT_TRUE(ParseStat(path, info));
74     ASSERT_EQ(info.pid, 111);
75     ASSERT_EQ(info.comm, std::string("222"));
76     ASSERT_EQ(info.state, ThreadState::SLEEP);
77     ASSERT_EQ(info.ppid, 444);
78     ASSERT_EQ(info.utime, 1414);
79     ASSERT_EQ(info.stime, 1515);
80     ASSERT_EQ(info.cutime, 1616);
81     ASSERT_EQ(info.cstime, 1717);
82     ASSERT_EQ(info.priority, 1818);
83     ASSERT_EQ(info.nice, 1919);
84     ASSERT_EQ(info.numThreads, 2020);
85     ASSERT_EQ(info.starttime, 2222);
86     ASSERT_EQ(info.rss, 2424);
87     ASSERT_EQ(info.signal, 3131);
88     ASSERT_EQ(info.blocked, 3232);
89     ASSERT_EQ(info.sigignore, 3333);
90     ASSERT_EQ(info.sigcatch, 3434);
91 }
92 
93 /**
94  * @tc.name: ParseStatTest004
95  * @tc.desc: parse file.param < 0
96  * @tc.type: FUNC
97  */
98 HWTEST(ProcUtilTest, ParseStatTest004, TestSize.Level2)
99 {
100     ProcessInfo info;
101     const std::string path = "/data/test/resource/proc/ParseStatTest004/stat";
102     ASSERT_TRUE(ParseStat(path, info));
103     ASSERT_EQ(info.pid, -111);
104     ASSERT_EQ(info.comm, std::string("222"));
105     ASSERT_EQ(info.state, ThreadState::DISK_SLEEP);
106     ASSERT_EQ(info.ppid, -444);
107     ASSERT_EQ(info.utime, 1414);
108     ASSERT_EQ(info.stime, 1515);
109     ASSERT_EQ(info.cutime, -1616);
110     ASSERT_EQ(info.cstime, -1717);
111     ASSERT_EQ(info.priority, -1818);
112     ASSERT_EQ(info.nice, -1919);
113     ASSERT_EQ(info.numThreads, -2020);
114     ASSERT_EQ(info.starttime, 2222);
115     ASSERT_EQ(info.rss, -2424);
116     ASSERT_EQ(info.signal, 3131);
117     ASSERT_EQ(info.blocked, 3232);
118     ASSERT_EQ(info.sigignore, 3333);
119     ASSERT_EQ(info.sigcatch, 3434);
120 }
121 
122 /**
123  * @tc.name: ParseStatTest005
124  * @tc.desc: parse file. comm len > 16
125  * @tc.type: FUNC
126  */
127 HWTEST(ProcUtilTest, ParseStatTest005, TestSize.Level2)
128 {
129     ProcessInfo info;
130     const std::string path = "/data/test/resource/proc/ParseStatTest005/stat";
131     ASSERT_FALSE(ParseStat(path, info));
132 }
133 
134 /**
135  * @tc.name: ParseStatTest006
136  * @tc.desc: parse file. comm contains ')'
137  * @tc.type: FUNC
138  */
139 HWTEST(ProcUtilTest, ParseStatTest006, TestSize.Level2)
140 {
141     ProcessInfo info;
142     const std::string path = "/data/test/resource/proc/ParseStatTest006/stat";
143     ASSERT_TRUE(ParseStat(path, info));
144     ASSERT_EQ(info.pid, -111);
145     ASSERT_EQ(info.comm, std::string("222)"));
146     ASSERT_EQ(info.state, ThreadState::ZOMBIE);
147     ASSERT_EQ(info.ppid, -444);
148     ASSERT_EQ(info.utime, 1414);
149     ASSERT_EQ(info.stime, 1515);
150     ASSERT_EQ(info.cutime, -1616);
151     ASSERT_EQ(info.cstime, -1717);
152     ASSERT_EQ(info.priority, -1818);
153     ASSERT_EQ(info.nice, -1919);
154     ASSERT_EQ(info.numThreads, -2020);
155     ASSERT_EQ(info.starttime, 2222);
156     ASSERT_EQ(info.rss, -2424);
157     ASSERT_EQ(info.signal, 3131);
158     ASSERT_EQ(info.blocked, 3232);
159     ASSERT_EQ(info.sigignore, 3333);
160     ASSERT_EQ(info.sigcatch, 3434);
161 }
162 
163 /**
164  * @tc.name: ParseStatTest007
165  * @tc.desc: parse file. comm contains ') '
166  * @tc.type: FUNC
167  */
168 HWTEST(ProcUtilTest, ParseStatTest007, TestSize.Level2)
169 {
170     ProcessInfo info;
171     const std::string path = "/data/test/resource/proc/ParseStatTest007/stat";
172     ASSERT_TRUE(ParseStat(path, info));
173     ASSERT_EQ(info.pid, -111);
174     ASSERT_EQ(info.comm, std::string("222) "));
175     ASSERT_EQ(info.state, ThreadState::IDLE);
176     ASSERT_EQ(info.ppid, -444);
177     ASSERT_EQ(info.utime, 1414);
178     ASSERT_EQ(info.stime, 1515);
179     ASSERT_EQ(info.cutime, -1616);
180     ASSERT_EQ(info.cstime, -1717);
181     ASSERT_EQ(info.priority, -1818);
182     ASSERT_EQ(info.nice, -1919);
183     ASSERT_EQ(info.numThreads, -2020);
184     ASSERT_EQ(info.starttime, 2222);
185     ASSERT_EQ(info.rss, -2424);
186     ASSERT_EQ(info.signal, 3131);
187     ASSERT_EQ(info.blocked, 3232);
188     ASSERT_EQ(info.sigignore, 3333);
189     ASSERT_EQ(info.sigcatch, 3434);
190 }
191 
192 /**
193  * @tc.name: ParseStatTest008
194  * @tc.desc: parse file. comm len = 16
195  * @tc.type: FUNC
196  */
197 HWTEST(ProcUtilTest, ParseStatTest008, TestSize.Level2)
198 {
199     ProcessInfo info;
200     const std::string path = "/data/test/resource/proc/ParseStatTest008/stat";
201     ASSERT_TRUE(ParseStat(path, info));
202     ASSERT_EQ(info.pid, -111);
203     ASSERT_EQ(info.comm, std::string("0123456789abcdef"));
204     ASSERT_EQ(info.state, ThreadState::STOPPED);
205     ASSERT_EQ(info.ppid, -444);
206     ASSERT_EQ(info.utime, 1414);
207     ASSERT_EQ(info.stime, 1515);
208     ASSERT_EQ(info.cutime, -1616);
209     ASSERT_EQ(info.cstime, -1717);
210     ASSERT_EQ(info.priority, -1818);
211     ASSERT_EQ(info.nice, -1919);
212     ASSERT_EQ(info.numThreads, -2020);
213     ASSERT_EQ(info.starttime, 2222);
214     ASSERT_EQ(info.rss, -2424);
215     ASSERT_EQ(info.signal, 3131);
216     ASSERT_EQ(info.blocked, 3232);
217     ASSERT_EQ(info.sigignore, 3333);
218     ASSERT_EQ(info.sigcatch, 3434);
219 }
220 
221 /**
222  * @tc.name: ParseStatTest009
223  * @tc.desc: parse file. read /proc/self/stat
224  * @tc.type: FUNC
225  */
226 HWTEST(ProcUtilTest, ParseStatTest009, TestSize.Level2)
227 {
228     ProcessInfo info;
229     const std::string path = "/proc/self/stat";
230     ASSERT_TRUE(ParseStat(path, info));
231     ASSERT_EQ(info.pid, getpid());
232     ASSERT_EQ(info.comm, std::string("test_common"));
233 }
234 
235 /**
236  * @tc.name: GetFirstNumberSeqTest010
237  * @tc.desc: get first number sequence.
238  * @tc.type: FUNC
239  */
240 HWTEST(ProcUtilTest, GetFirstNumberSeqTest010, TestSize.Level2)
241 {
242     std::string str = "abc";
243     std::string result = GetFirstNumberSeq(str);
244     ASSERT_EQ(result, "");
245 
246     std::string data = "123456";
247     result = GetFirstNumberSeq(data);
248     ASSERT_EQ(result, data);
249 
250     result = GetFirstNumberSeq("abc123456");
251     ASSERT_EQ(result, data);
252 
253     result = GetFirstNumberSeq("abc123456   ");
254     ASSERT_EQ(result, data);
255 
256     result = GetFirstNumberSeq("abc123456 adc");
257     ASSERT_EQ(result, data);
258 
259     result = GetFirstNumberSeq("abc123456  123");
260     ASSERT_EQ(result, data);
261 
262     result = GetFirstNumberSeq("123456  123");
263     ASSERT_EQ(result, data);
264 }
265 
266 /**
267  * @tc.name: IsSigDumpMaskTest011
268  * @tc.desc: is sig dump mask.
269  * @tc.type: FUNC
270  */
271 HWTEST(ProcUtilTest, IsSigDumpMaskTest011, TestSize.Level2)
272 {
273     uint64_t sigBlk = 0x00000000;
274     ASSERT_FALSE(IsSigDumpMask(sigBlk));
275 
276     sigBlk = 0x00000004;
277     ASSERT_FALSE(IsSigDumpMask(sigBlk));
278     // SigBlk: 0000000400000000
279     sigBlk = 0x0000000400000000;
280     ASSERT_TRUE(IsSigDumpMask(sigBlk));
281 
282     sigBlk = 0x4000000000;
283     ASSERT_FALSE(IsSigDumpMask(sigBlk));
284 }
285 
286 /**
287  * @tc.name: ParsePidStatus013
288  * @tc.desc: is hap.
289  * @tc.type: FUNC
290  */
291 HWTEST(ProcUtilTest, ParsePidStatus013, TestSize.Level2)
292 {
293     pid_t pid = 99999; // 99999 : Invalid PID
294     long uid = 0;
295     uint64_t sigBlk = 0;
296     auto result = GetUidAndSigBlk(pid, uid, sigBlk);
297     EXPECT_TRUE(uid == 0);
298     EXPECT_TRUE(sigBlk == 0);
299 
300     pid = getpid();
301     result = GetUidAndSigBlk(pid, uid, sigBlk);
302     EXPECT_TRUE(sigBlk == 0);
303 
304     sigset_t sigSet;
305     sigemptyset(&sigSet);
306     sigaddset(&sigSet, SIGQUIT);
307     sigprocmask(SIG_BLOCK, &sigSet, nullptr);
308 
309     result = GetUidAndSigBlk(pid, uid, sigBlk);
310     EXPECT_TRUE(sigBlk ==  (1 << (SIGQUIT -1)));
311 
312     sigprocmask(SIG_UNBLOCK, &sigSet, nullptr);
313 }
314 } // namespace HiviewDFX
315 } // namespace OHOS
316