1 /*
2
3 * Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 #include <climits>
32 #include <cstdio>
33 #include <cstdlib>
34 #include <unistd.h>
35 #include <fcntl.h>
36 #include <vector>
37 #include <string>
38 #include <gtest/gtest.h>
39 #include <sys/stat.h>
40 #include <sys/types.h>
41 #include <dirent.h>
42 #include "It_process_plimits.h"
43
44 using namespace std;
45 using namespace testing::ext;
46 namespace OHOS {
47 class ProcessPlimitsTest : public testing::Test {
48 public:
SetUpTestCase(void)49 static void SetUpTestCase(void) {}
TearDownTestCase(void)50 static void TearDownTestCase(void) {}
51
52 protected:
53 virtual void SetUp();
54 virtual void TearDown();
55
56 private:
57 inline bool IsFile(const std::string &file);
58 inline bool IsDir(const std::string &path);
59 inline bool IsSpecialDir(const std::string &path);
60 };
61
62 #if defined(LOSCFG_USER_TEST_SMOKE)
63 /**
64 * @tc.name: plimits_Test_001
65 * @tc.desc: plimits function test case
66 * @tc.type: FUNC
67 * @tc.require: issueI6GVPL
68 * @tc.author:
69 */
HWTEST_F(ProcessPlimitsTest, ItProcessPlimits001, TestSize.Level0)70 HWTEST_F(ProcessPlimitsTest, ItProcessPlimits001, TestSize.Level0)
71 {
72 ItProcessPlimits001();
73 }
74
75 /**
76 * @tc.name: plimits_Test_002
77 * @tc.desc: plimits function test case
78 * @tc.type: FUNC
79 * @tc.require: issueI6GVPL
80 * @tc.author:
81 */
HWTEST_F(ProcessPlimitsTest, ItProcessPlimits002, TestSize.Level0)82 HWTEST_F(ProcessPlimitsTest, ItProcessPlimits002, TestSize.Level0)
83 {
84 ItProcessPlimits002();
85 }
86
87 /**
88 * @tc.name: plimits_Test_003
89 * @tc.desc: plimits function test case
90 * @tc.type: FUNC
91 * @tc.require: issueI6GVPL
92 * @tc.author:
93 */
HWTEST_F(ProcessPlimitsTest, ItProcessPlimits003, TestSize.Level0)94 HWTEST_F(ProcessPlimitsTest, ItProcessPlimits003, TestSize.Level0)
95 {
96 ItProcessPlimits003();
97 }
98
99 /**
100 * @tc.name: plimits_Test_004
101 * @tc.desc: plimits function test case
102 * @tc.type: FUNC
103 * @tc.require: issueI6GVPL
104 * @tc.author:
105 */
HWTEST_F(ProcessPlimitsTest, ItProcessPlimits004, TestSize.Level0)106 HWTEST_F(ProcessPlimitsTest, ItProcessPlimits004, TestSize.Level0)
107 {
108 ItProcessPlimits004();
109 }
110
111 /**
112 * @tc.name: plimits_Test_005
113 * @tc.desc: plimits function test case
114 * @tc.type: FUNC
115 * @tc.require: issueI6GVPL
116 * @tc.author:
117 */
HWTEST_F(ProcessPlimitsTest, ItProcessPlimits005, TestSize.Level0)118 HWTEST_F(ProcessPlimitsTest, ItProcessPlimits005, TestSize.Level0)
119 {
120 ItProcessPlimits005();
121 }
122
123 /**
124 * @tc.name: plimits_Test_006
125 * @tc.desc: plimits function test case
126 * @tc.type: FUNC
127 * @tc.require: issueI6GVPL
128 * @tc.author:
129 */
HWTEST_F(ProcessPlimitsTest, ItProcessPlimits006, TestSize.Level0)130 HWTEST_F(ProcessPlimitsTest, ItProcessPlimits006, TestSize.Level0)
131 {
132 ItProcessPlimits006();
133 }
134
135 /**
136 * @tc.name: plimits_Test_007
137 * @tc.desc: plimits function test case
138 * @tc.type: FUNC
139 * @tc.require: issueI6GVPL
140 * @tc.author:
141 */
HWTEST_F(ProcessPlimitsTest, ItProcessPlimits007, TestSize.Level0)142 HWTEST_F(ProcessPlimitsTest, ItProcessPlimits007, TestSize.Level0)
143 {
144 ItProcessPlimits007();
145 }
146
147 /**
148 * @tc.name: plimits_Test_008
149 * @tc.desc: plimits function test case
150 * @tc.type: FUNC
151 * @tc.require: issueI6GVPL
152 * @tc.author:
153 */
HWTEST_F(ProcessPlimitsTest, ItProcessPlimits008, TestSize.Level0)154 HWTEST_F(ProcessPlimitsTest, ItProcessPlimits008, TestSize.Level0)
155 {
156 ItProcessPlimits008();
157 }
158
159 /**
160 * @tc.name: plimits_pid_Test_001
161 * @tc.desc: pid plimit function test case
162 * @tc.type: FUNC
163 * @tc.require: issueI6GVPL
164 * @tc.author:
165 */
HWTEST_F(ProcessPlimitsTest, ItProcessPlimitsPid001, TestSize.Level0)166 HWTEST_F(ProcessPlimitsTest, ItProcessPlimitsPid001, TestSize.Level0)
167 {
168 ItProcessPlimitsPid001();
169 }
170
171 /**
172 * @tc.name: plimits_pid_Test_002
173 * @tc.desc: pid plimit function test case
174 * @tc.type: FUNC
175 * @tc.require: issueI6GVPL
176 * @tc.author:
177 */
HWTEST_F(ProcessPlimitsTest, ItProcessPlimitsPid002, TestSize.Level0)178 HWTEST_F(ProcessPlimitsTest, ItProcessPlimitsPid002, TestSize.Level0)
179 {
180 ItProcessPlimitsPid002();
181 }
182
183 /**
184 * @tc.name: plimits_pid_Test_003
185 * @tc.desc: pid plimit function test case
186 * @tc.type: FUNC
187 * @tc.require: issueI6GVPL
188 * @tc.author:
189 */
HWTEST_F(ProcessPlimitsTest, ItProcessPlimitsPid003, TestSize.Level0)190 HWTEST_F(ProcessPlimitsTest, ItProcessPlimitsPid003, TestSize.Level0)
191 {
192 ItProcessPlimitsPid003();
193 }
194
195 /**
196 * @tc.name: plimits_pid_Test_004
197 * @tc.desc: pid plimit function test case
198 * @tc.type: FUNC
199 * @tc.require: issueI6GVPL
200 * @tc.author:
201 */
HWTEST_F(ProcessPlimitsTest, ItProcessPlimitsPid004, TestSize.Level0)202 HWTEST_F(ProcessPlimitsTest, ItProcessPlimitsPid004, TestSize.Level0)
203 {
204 ItProcessPlimitsPid004();
205 }
206
207 /**
208 * @tc.name: plimits_pid_Test_005
209 * @tc.desc: pid plimit function test case
210 * @tc.type: FUNC
211 * @tc.require: issueI6GVPL
212 * @tc.author:
213 */
HWTEST_F(ProcessPlimitsTest, ItProcessPlimitsPid005, TestSize.Level0)214 HWTEST_F(ProcessPlimitsTest, ItProcessPlimitsPid005, TestSize.Level0)
215 {
216 ItProcessPlimitsPid005();
217 }
218
219 /**
220 * @tc.name: plimits_pid_Test_006
221 * @tc.desc: pid plimit function test case
222 * @tc.type: FUNC
223 * @tc.require: issueI6GVPL
224 * @tc.author:
225 */
HWTEST_F(ProcessPlimitsTest, ItProcessPlimitsPid006, TestSize.Level0)226 HWTEST_F(ProcessPlimitsTest, ItProcessPlimitsPid006, TestSize.Level0)
227 {
228 ItProcessPlimitsPid006();
229 }
230
231 /**
232 * @tc.name: plimits_Mem_Test_001
233 * @tc.desc: mem plimit function test case
234 * @tc.require: issueI6GVPL
235 * @tc.author:
236 */
HWTEST_F(ProcessPlimitsTest, ItProcessPlimitsMemory001, TestSize.Level0)237 HWTEST_F(ProcessPlimitsTest, ItProcessPlimitsMemory001, TestSize.Level0)
238 {
239 ItProcessPlimitsMemory001();
240 }
241
242 /**
243 * @tc.name: plimits_Mem_Test_002
244 * @tc.desc: mem plimit function test case
245 * @tc.require: issueI6GVPL
246 * @tc.author:
247 */
HWTEST_F(ProcessPlimitsTest, ItProcessPlimitsMemory002, TestSize.Level0)248 HWTEST_F(ProcessPlimitsTest, ItProcessPlimitsMemory002, TestSize.Level0)
249 {
250 ItProcessPlimitsMemory002();
251 }
252
253 /**
254 * @tc.name: plimits_Dev_Test_001
255 * @tc.desc: devices plimit function test case
256 * @tc.require: issueI6GVPL
257 * @tc.author:
258 */
HWTEST_F(ProcessPlimitsTest, ItProcessPlimitsDevices001, TestSize.Level0)259 HWTEST_F(ProcessPlimitsTest, ItProcessPlimitsDevices001, TestSize.Level0)
260 {
261 ItProcessPlimitsDevices001();
262 }
263
264 /**
265 * @tc.name: plimits_Dev_Test_002
266 * @tc.desc: devices plimit function test case
267 * @tc.require: issueI6GVPL
268 * @tc.author:
269 */
HWTEST_F(ProcessPlimitsTest, ItProcessPlimitsDevices002, TestSize.Level0)270 HWTEST_F(ProcessPlimitsTest, ItProcessPlimitsDevices002, TestSize.Level0)
271 {
272 ItProcessPlimitsDevices002();
273 }
274
275 /**
276 * @tc.name: plimits_Dev_Test_003
277 * @tc.desc: devices plimit function test case
278 * @tc.require: issueI6GVPL
279 * @tc.author:
280 */
HWTEST_F(ProcessPlimitsTest, ItProcessPlimitsDevices003, TestSize.Level0)281 HWTEST_F(ProcessPlimitsTest, ItProcessPlimitsDevices003, TestSize.Level0)
282 {
283 ItProcessPlimitsDevices003();
284 }
285
286 /**
287 * @tc.name: plimits_Dev_Test_004
288 * @tc.desc: devices plimit function test case
289 * @tc.require: issueI6GVPL
290 * @tc.author:
291 */
HWTEST_F(ProcessPlimitsTest, ItProcessPlimitsDevices004, TestSize.Level0)292 HWTEST_F(ProcessPlimitsTest, ItProcessPlimitsDevices004, TestSize.Level0)
293 {
294 ItProcessPlimitsDevices004();
295 }
296
297 /**
298 * @tc.name: plimits_Dev_Test_005
299 * @tc.desc: devices plimit function test case
300 * @tc.require: issueI6GVPL
301 * @tc.author:
302 */
HWTEST_F(ProcessPlimitsTest, ItProcessPlimitsDevices005, TestSize.Level0)303 HWTEST_F(ProcessPlimitsTest, ItProcessPlimitsDevices005, TestSize.Level0)
304 {
305 ItProcessPlimitsDevices005();
306 }
307
308 /**
309 * @tc.name: plimits_Dev_Test_006
310 * @tc.desc: devices plimit function test case
311 * @tc.require: issueI6GVPL
312 * @tc.author:
313 */
HWTEST_F(ProcessPlimitsTest, ItProcessPlimitsDevices006, TestSize.Level0)314 HWTEST_F(ProcessPlimitsTest, ItProcessPlimitsDevices006, TestSize.Level0)
315 {
316 ItProcessPlimitsDevices006();
317 }
318
319 /**
320 * @tc.name: plimits_Dev_Test_007
321 * @tc.desc: devices plimit function test case
322 * @tc.require: issueI6GVPL
323 * @tc.author:
324 */
HWTEST_F(ProcessPlimitsTest, ItProcessPlimitsDevices007, TestSize.Level0)325 HWTEST_F(ProcessPlimitsTest, ItProcessPlimitsDevices007, TestSize.Level0)
326 {
327 ItProcessPlimitsDevices007();
328 }
329
330 /**
331 * @tc.name: plimits_Dev_Test_008
332 * @tc.desc: devices plimit function test case
333 * @tc.require: issueI6GVPL
334 * @tc.author:
335 */
HWTEST_F(ProcessPlimitsTest, ItProcessPlimitsDevices008, TestSize.Level0)336 HWTEST_F(ProcessPlimitsTest, ItProcessPlimitsDevices008, TestSize.Level0)
337 {
338 ItProcessPlimitsDevices008();
339 }
340
341 /**
342 * @tc.name: plimits_Dev_Test_009
343 * @tc.desc: devices plimit function test case
344 * @tc.require: issueI6GVPL
345 * @tc.author:
346 */
HWTEST_F(ProcessPlimitsTest, ItProcessPlimitsDevices009, TestSize.Level0)347 HWTEST_F(ProcessPlimitsTest, ItProcessPlimitsDevices009, TestSize.Level0)
348 {
349 ItProcessPlimitsDevices009();
350 }
351
352 /**
353 * @tc.name: plimits_Ipc_Test_002
354 * @tc.desc: ipc plimit function test case
355 * @tc.require: issueI6GVPL
356 * @tc.author:
357 */
HWTEST_F(ProcessPlimitsTest, ItProcessPlimitsIpc002, TestSize.Level0)358 HWTEST_F(ProcessPlimitsTest, ItProcessPlimitsIpc002, TestSize.Level0)
359 {
360 ItProcessPlimitsIpc002();
361 }
362
363 /**
364 * @tc.name: plimits_Ipc_Test_003
365 * @tc.desc: ipc plimit function test case
366 * @tc.require: issueI6GVPL
367 * @tc.author:
368 */
HWTEST_F(ProcessPlimitsTest, ItProcessPlimitsIpc003, TestSize.Level0)369 HWTEST_F(ProcessPlimitsTest, ItProcessPlimitsIpc003, TestSize.Level0)
370 {
371 ItProcessPlimitsIpc003();
372 }
373
374 /**
375 * @tc.name: plimits_Ipc_Test_004
376 * @tc.desc: ipc plimit function test case
377 * @tc.require: issueI6GVPL
378 * @tc.author:
379 */
HWTEST_F(ProcessPlimitsTest, ItProcessPlimitsIpc004, TestSize.Level0)380 HWTEST_F(ProcessPlimitsTest, ItProcessPlimitsIpc004, TestSize.Level0)
381 {
382 ItProcessPlimitsIpc004();
383 }
384
385 /**
386 * @tc.name: plimits_Ipc_Test_005
387 * @tc.desc: ipc plimit function test case
388 * @tc.require: issueI6GVPL
389 * @tc.author:
390 */
HWTEST_F(ProcessPlimitsTest, ItProcessPlimitsIpc005, TestSize.Level0)391 HWTEST_F(ProcessPlimitsTest, ItProcessPlimitsIpc005, TestSize.Level0)
392 {
393 ItProcessPlimitsIpc005();
394 }
395
396 /**
397 * @tc.name: plimits_Ipc_Test_006
398 * @tc.desc: ipc plimit function test case
399 * @tc.require: issueI6GVPL
400 * @tc.author:
401 */
HWTEST_F(ProcessPlimitsTest, ItProcessPlimitsIpc006, TestSize.Level0)402 HWTEST_F(ProcessPlimitsTest, ItProcessPlimitsIpc006, TestSize.Level0)
403 {
404 ItProcessPlimitsIpc006();
405 }
406
407 /**
408 * @tc.name: plimits_Ipc_Test_007
409 * @tc.desc: ipc plimit function test case
410 * @tc.require: issueI6GVPL
411 * @tc.author:
412 */
HWTEST_F(ProcessPlimitsTest, ItProcessPlimitsIpc007, TestSize.Level0)413 HWTEST_F(ProcessPlimitsTest, ItProcessPlimitsIpc007, TestSize.Level0)
414 {
415 ItProcessPlimitsIpc007();
416 }
417
418 /**
419 * @tc.name: plimits_Ipc_Test_008
420 * @tc.desc: ipc plimit function test case
421 * @tc.require: issueI6GVPL
422 * @tc.author:
423 */
HWTEST_F(ProcessPlimitsTest, ItProcessPlimitsIpc008, TestSize.Level0)424 HWTEST_F(ProcessPlimitsTest, ItProcessPlimitsIpc008, TestSize.Level0)
425 {
426 ItProcessPlimitsIpc008();
427 }
428
429 /**
430 * @tc.name: plimits_Ipc_Test_009
431 * @tc.desc: ipc plimit function test case
432 * @tc.require: issueI6GVPL
433 * @tc.author:
434 */
HWTEST_F(ProcessPlimitsTest, ItProcessPlimitsIpc009, TestSize.Level0)435 HWTEST_F(ProcessPlimitsTest, ItProcessPlimitsIpc009, TestSize.Level0)
436 {
437 ItProcessPlimitsIpc009();
438 }
439
440 /**
441 * @tc.name: plimits_Ipc_Test_010
442 * @tc.desc: ipc plimit function test case
443 * @tc.require: issueI6GVPL
444 * @tc.author:
445 */
HWTEST_F(ProcessPlimitsTest, ItProcessPlimitsIpc010, TestSize.Level0)446 HWTEST_F(ProcessPlimitsTest, ItProcessPlimitsIpc010, TestSize.Level0)
447 {
448 ItProcessPlimitsIpc010();
449 }
450
451 /**
452 * @tc.name: plimits_Ipc_Test_011
453 * @tc.desc: ipc plimit function test case
454 * @tc.require: issueI6GVPL
455 * @tc.author:
456 */
HWTEST_F(ProcessPlimitsTest, ItProcessPlimitsIpc011, TestSize.Level0)457 HWTEST_F(ProcessPlimitsTest, ItProcessPlimitsIpc011, TestSize.Level0)
458 {
459 ItProcessPlimitsIpc011();
460 }
461
462 /**
463 * @tc.name: plimits_Ipc_Test_012
464 * @tc.desc: ipc plimit function test case
465 * @tc.require: issueI6GVPL
466 * @tc.author:
467 */
HWTEST_F(ProcessPlimitsTest, ItProcessPlimitsIpc012, TestSize.Level0)468 HWTEST_F(ProcessPlimitsTest, ItProcessPlimitsIpc012, TestSize.Level0)
469 {
470 ItProcessPlimitsIpc012();
471 }
472
473 /**
474 * @tc.name: plimits_Ipc_Test_013
475 * @tc.desc: ipc plimit function test case
476 * @tc.require: issueI6GVPL
477 * @tc.author:
478 */
HWTEST_F(ProcessPlimitsTest, ItProcessPlimitsIpc013, TestSize.Level0)479 HWTEST_F(ProcessPlimitsTest, ItProcessPlimitsIpc013, TestSize.Level0)
480 {
481 ItProcessPlimitsIpc013();
482 }
483
484 /**
485 * @tc.name: plimits_Sched_Test_001
486 * @tc.desc: sched plimit function test case
487 * @tc.require: issueI6GVPL
488 * @tc.author:
489 */
HWTEST_F(ProcessPlimitsTest, IItProcessPlimitsSched001, TestSize.Level0)490 HWTEST_F(ProcessPlimitsTest, IItProcessPlimitsSched001, TestSize.Level0)
491 {
492 ItProcessPlimitsSched001();
493 }
494
495 /**
496 * @tc.name: plimits_Sched_Test_002
497 * @tc.desc: sched plimit function test case
498 * @tc.require: issueI6GVPL
499 * @tc.author:
500 */
HWTEST_F(ProcessPlimitsTest, IItProcessPlimitsSched002, TestSize.Level0)501 HWTEST_F(ProcessPlimitsTest, IItProcessPlimitsSched002, TestSize.Level0)
502 {
503 ItProcessPlimitsSched002();
504 }
505
506 /**
507 * @tc.name: plimits_Sched_Test_003
508 * @tc.desc: sched plimit function test case
509 * @tc.require: issueI6GVPL
510 * @tc.author:
511 */
HWTEST_F(ProcessPlimitsTest, IItProcessPlimitsSched003, TestSize.Level0)512 HWTEST_F(ProcessPlimitsTest, IItProcessPlimitsSched003, TestSize.Level0)
513 {
514 ItProcessPlimitsSched003();
515 }
516
517 /**
518 * @tc.name: plimits_Sched_Test_004
519 * @tc.desc: sched plimit function test case
520 * @tc.require: issueI6GVPL
521 * @tc.author:
522 */
HWTEST_F(ProcessPlimitsTest, IItProcessPlimitsSched004, TestSize.Level0)523 HWTEST_F(ProcessPlimitsTest, IItProcessPlimitsSched004, TestSize.Level0)
524 {
525 ItProcessPlimitsSched004();
526 }
527 #endif
528 } // namespace OHOS
529
530
531 namespace OHOS {
SetUp()532 void ProcessPlimitsTest::SetUp()
533 {
534 (void)rmdir("/proc/plimits/test");
535 }
536
TearDown()537 void ProcessPlimitsTest::TearDown()
538 {
539 (void)rmdir("/proc/plimits/test");
540 }
541
IsFile(const std::string &file)542 bool ProcessPlimitsTest::IsFile(const std::string &file)
543 {
544 struct stat statbuf;
545 return (lstat(file.c_str(), &statbuf) == 0) && S_ISREG(statbuf.st_mode);
546 }
547
IsDir(const std::string &path)548 bool ProcessPlimitsTest::IsDir(const std::string &path)
549 {
550 struct stat statbuf;
551 return (lstat(path.c_str(), &statbuf) == 0) && S_ISDIR(statbuf.st_mode);
552 }
553
IsSpecialDir(const std::string &path)554 bool ProcessPlimitsTest::IsSpecialDir(const std::string &path)
555 {
556 return strcmp(path.c_str(), ".") == 0 || strcmp(path.c_str(), "..") == 0;
557 }
558 } // namespace OHOS
559
ReadFile(const char *filepath, char *buf)560 int ReadFile(const char *filepath, char *buf)
561 {
562 FILE *fpid = nullptr;
563 fpid = fopen(filepath, "r");
564 if (fpid == nullptr) {
565 return -1;
566 }
567 size_t trd = fread(buf, 1, 512, fpid);
568 (void)fclose(fpid);
569 return trd;
570 }
571
WriteFile(const char *filepath, const char *buf)572 int WriteFile(const char *filepath, const char *buf)
573 {
574 int fd = open(filepath, O_WRONLY);
575 if (fd == -1) {
576 return -1;
577 }
578 size_t twd = write(fd, buf, strlen(buf));
579 if (twd == -1) {
580 (void)close(fd);
581 return -1;
582 }
583 (void)close(fd);
584 return twd;
585 }
586
GetLine(char *buf, int count, int maxLen, char **array)587 int GetLine(char *buf, int count, int maxLen, char **array)
588 {
589 char *head = buf;
590 char *tail = buf;
591 char index = 0;
592 if ((buf == NULL) || (strlen(buf) == 0)) {
593 return 0;
594 }
595 while (*tail != '\0') {
596 if (*tail != '\n') {
597 tail++;
598 continue;
599 }
600 if (index >= count) {
601 return index + 1;
602 }
603
604 array[index] = head;
605 index++;
606 *tail = '\0';
607 if (strlen(head) > maxLen) {
608 return index + 1;
609 }
610 tail++;
611 head = tail;
612 tail++;
613 }
614 return (index + 1);
615 }
616
RmdirTest(std::string path)617 int RmdirTest(std::string path)
618 {
619 int ret;
620 RmdirControlFile(path);
621 ret = rmdir(path.c_str());
622 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
623 return 0;
624 }
625
WaitForCpupStable(int expectedCpupPercent)626 int WaitForCpupStable(int expectedCpupPercent)
627 {
628 int sleepTime;
629 if (expectedCpupPercent >= QUOTA_PERCENT_100) {
630 sleepTime = WAIT_CPUP_STABLE_FOR_100;
631 } else {
632 sleepTime = WAIT_CPUP_STABLE;
633 }
634 return sleep(sleepTime);
635 }
636
SampleRound(void)637 static int SampleRound(void)
638 {
639 return STATISTIC_TIMES;
640 }
641
GetProcessInfo(pid_t pid)642 static vector<string> GetProcessInfo(pid_t pid)
643 {
644 vector<string> contentArr;
645 char buf[TEST_BUFFER_SIZE + 1] = {0};
646 string strpid = to_string(pid);
647
648 ifstream infile;
649 infile.open("/proc/process");
650 while (!infile.eof()) {
651 infile.getline(buf, TEST_BUFFER_SIZE);
652 regex e("^\\s+"+strpid);
653 int matchResult = regex_search(buf, e);
654 if (matchResult == 1) {
655 istringstream str(buf);
656 string out;
657 while (str >> out) {
658 contentArr.push_back(out);
659 }
660 break;
661 }
662 (void)memset_s(buf, TEST_BUFFER_SIZE, 0, TEST_BUFFER_SIZE);
663 }
664 infile.close();
665 (void)memset_s(buf, TEST_BUFFER_SIZE, 0, TEST_BUFFER_SIZE);
666 return contentArr;
667 }
668
669
SigQuit(int s)670 static void SigQuit(int s)
671 {
672 exit(0);
673 }
674
ChildRunCpup()675 static int ChildRunCpup()
676 {
677 struct sigaction act;
678 act.sa_handler = SigQuit;
679 (void)sigaction(SIGUSR1, &act, NULL);
680
681 unsigned long x = 1;
682 unsigned long y = 1;
683 while (1) {
684 y++;
685 x *= y;
686 }
687
688 return 0;
689 }
690
ForkChilds(int num, int *pidArray)691 int ForkChilds(int num, int *pidArray)
692 {
693 pid_t childPid;
694 pid_t pidArrayLocal[PROCESS_LIMIT_AMOUNT];
695
696 for (int idx = 0; idx < num; idx++) {
697 childPid = fork();
698 if (childPid == 0) {
699 (void)ChildRunCpup();
700 } else if (childPid > 0) {
701 pidArrayLocal[idx] = childPid;
702 *pidArray = childPid;
703 pidArray++;
704 } else {
705 return -errno;
706 }
707 }
708 return 0;
709 }
710
GetCpup(pid_t pid)711 static double GetCpup(pid_t pid)
712 {
713 auto content = GetProcessInfo(pid);
714 double cpup10s = atof(content[CPUP10S_INDEX].c_str());
715 return cpup10s;
716 }
717
CollectCpupData(int childAmount, int sampleSeconds, int *pidArray, vector<vector<double>> &cpupValuesArray)718 static int CollectCpupData(int childAmount, int sampleSeconds, int *pidArray, vector<vector<double>> &cpupValuesArray)
719 {
720 double cpup10s;
721 for (int i = 0; i < sampleSeconds; i++) {
722 for (int j = 0; j < childAmount;j++) {
723 cpup10s = GetCpup(pidArray[j]);
724 cpupValuesArray[j].push_back(cpup10s);
725 }
726 sleep(1);
727 }
728 return 0;
729 }
730
CalcAverageCpup(int num, vector<vector<double>> &cpupValuesArray, double *cpupAverageArray)731 static int CalcAverageCpup(int num, vector<vector<double>> &cpupValuesArray, double *cpupAverageArray)
732 {
733 double cpup10sAverage;
734 for (int idx = 0; idx < num; idx++) {
735 auto size = cpupValuesArray[idx].size();
736 cpup10sAverage = std::accumulate(cpupValuesArray[idx].begin(), cpupValuesArray[idx].end(), 0.0) / size;
737 cpupAverageArray[idx] = cpup10sAverage;
738 }
739 return 0;
740 }
741
CreatePlimitGroup(const char* groupName, char *childPidFiles, unsigned long long periodUs, unsigned long long quotaUs)742 int CreatePlimitGroup(const char* groupName, char *childPidFiles,
743 unsigned long long periodUs, unsigned long long quotaUs)
744 {
745 int ret;
746 mode_t mode = 0777;
747 char dirpath[TEST_BUFFER_SIZE];
748 char procspath[TEST_BUFFER_SIZE];
749 char periodpath[TEST_BUFFER_SIZE];
750 char quotapath[TEST_BUFFER_SIZE];
751 char periodValue[TEST_BUFFER_SIZE];
752 char quotaValue[TEST_BUFFER_SIZE];
753
754 if (sprintf_s(dirpath, TEST_BUFFER_SIZE, "/proc/plimits/%s", groupName) < 0) {
755 return -1;
756 }
757 if (sprintf_s(procspath, TEST_BUFFER_SIZE, "%s/plimits.procs", dirpath) < 0) {
758 return -1;
759 }
760 if (sprintf_s(periodpath, TEST_BUFFER_SIZE, "%s/sched.period", dirpath) < 0) {
761 return -1;
762 }
763 if (sprintf_s(quotapath, TEST_BUFFER_SIZE, "%s/sched.quota", dirpath) < 0) {
764 return -1;
765 }
766
767 ret = access(dirpath, 0);
768 ICUNIT_ASSERT_EQUAL(ret, -1, ret);
769
770 ret = mkdir(dirpath, mode);
771 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
772
773 if (sprintf_s(periodValue, TEST_BUFFER_SIZE, "%llu", periodUs) < 0) {
774 return -1;
775 }
776 if (sprintf_s(quotaValue, TEST_BUFFER_SIZE, "%llu", quotaUs) < 0) {
777 return -1;
778 }
779 ret = WriteFile(periodpath, periodValue);
780 if (ret < 0) {
781 printf("%s %d\n", __FUNCTION__, __LINE__);
782 return ret;
783 }
784 ret = WriteFile(quotapath, quotaValue);
785 if (ret < 0) {
786 printf("%s %d\n", __FUNCTION__, __LINE__);
787 return ret;
788 }
789 if (sprintf_s(childPidFiles, TEST_BUFFER_SIZE, "%s", procspath) < 0) {
790 return -1;
791 }
792 return 0;
793 }
794
CreatePlimitGroupWithoutLimit(const char* groupName, char *childPidFiles)795 int CreatePlimitGroupWithoutLimit(const char* groupName, char *childPidFiles)
796 {
797 int ret;
798 mode_t mode = 0777;
799 char dirpath[TEST_BUFFER_SIZE];
800 char procspath[TEST_BUFFER_SIZE];
801
802 if (sprintf_s(dirpath, TEST_BUFFER_SIZE, "/proc/plimits/%s", groupName) < 0) {
803 return -1;
804 }
805 if (sprintf_s(procspath, TEST_BUFFER_SIZE, "%s/plimits.procs", dirpath) < 0) {
806 return -1;
807 }
808
809 ret = access(dirpath, 0);
810 ICUNIT_ASSERT_EQUAL(ret, -1, ret);
811 ret = mkdir(dirpath, mode);
812 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
813 if (sprintf_s(childPidFiles, TEST_BUFFER_SIZE, "%s", procspath) < 0) {
814 return -1;
815 }
816 return 0;
817 }
818
TerminateChildProcess(int *childPidArray, int childAmount, int sig)819 int TerminateChildProcess(int *childPidArray, int childAmount, int sig)
820 {
821 int idx;
822 for (idx = 0; idx < childAmount; idx++) {
823 (void)kill(childPidArray[idx], SIGUSR1);
824
825 int status;
826 (void)waitpid(childPidArray[idx], &status, 0);
827 }
828
829 (void)signal(SIGUSR1, SIG_DFL);
830 return 0;
831 }
832
CalcCpupUsage(int childAmount, int *childPidArray, int expectedCpupPercent)833 double CalcCpupUsage(int childAmount, int *childPidArray, int expectedCpupPercent)
834 {
835 int idx;
836 int sampleSeconds = SampleRound();
837 vector<vector<double>> cpupValuesArray(PROCESS_LIMIT_AMOUNT);
838 (void)CollectCpupData(childAmount, sampleSeconds, &childPidArray[0], cpupValuesArray);
839
840 double actualCpup10sArray[PROCESS_LIMIT_AMOUNT];
841 (void)CalcAverageCpup(childAmount, cpupValuesArray, &actualCpup10sArray[0]);
842
843 double sumAllChildsCpup = 0;
844 for (idx = 0; idx < childAmount; idx++) {
845 sumAllChildsCpup += actualCpup10sArray[idx];
846 }
847 return sumAllChildsCpup;
848 }
849
CheckCpupUsage(double sumAllChildsCpup, int expectedCpupPercent)850 double CheckCpupUsage(double sumAllChildsCpup, int expectedCpupPercent)
851 {
852 if (expectedCpupPercent <= 0.0) {
853 return 500.0; /* 500.0: errno */
854 }
855 double errorRate = fabs(sumAllChildsCpup / expectedCpupPercent - 1.0);
856 return errorRate;
857 }
858
checkCpupUsageGreaterThan(double sumAllChildsCpup, int expectedCpupPercent)859 int checkCpupUsageGreaterThan(double sumAllChildsCpup, int expectedCpupPercent)
860 {
861 if (sumAllChildsCpup > expectedCpupPercent) {
862 return 0;
863 } else {
864 return -1;
865 }
866 }
867
TestCpupInPlimit(int childAmount, const char* groupName, unsigned long long periodUs, unsigned long long quotaUs, int expectedCpupPercent)868 double TestCpupInPlimit(int childAmount, const char* groupName,
869 unsigned long long periodUs, unsigned long long quotaUs, int expectedCpupPercent)
870 {
871 char dirpath[TEST_BUFFER_SIZE];
872 pid_t childPidArray[PROCESS_LIMIT_AMOUNT];
873 char procspath[TEST_BUFFER_SIZE];
874 double sumAllChildsCpup = 0;
875
876 int ret = CreatePlimitGroup(groupName, procspath, periodUs, quotaUs);
877 if (ret < 0) {
878 printf("%s %d, ret=%d\n", __FUNCTION__, __LINE__, ret);
879 return 100.0; /* 100.0: errno */
880 }
881 ret = ForkChilds(childAmount, &childPidArray[0]);
882 if (ret != 0) {
883 printf("%s %d, ret=%d\n", __FUNCTION__, __LINE__, ret);
884 return 200.0; /* 200.0: errno */
885 }
886 (void)WaitForCpupStable(expectedCpupPercent);
887 sumAllChildsCpup = CalcCpupUsage(childAmount, &childPidArray[0], 0);
888 double errorRate = CheckCpupUsage(sumAllChildsCpup, expectedCpupPercent);
889
890 (void)TerminateChildProcess(&childPidArray[0], childAmount, SIGUSR1);
891
892 if (sprintf_s(dirpath, TEST_BUFFER_SIZE, "/proc/plimits/%s", groupName) < 0) {
893 return 300.0; /* 300.0: errno */
894 }
895 ret = rmdir(dirpath);
896 if (ret != 0) {
897 printf("%s %d, ret=%d\n", __FUNCTION__, __LINE__, errno);
898 return 400.0; /* 400.0: errno */
899 }
900 return errorRate;
901 }
902