1 /*
2  * Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification,
5  * are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice, this list of
8  * conditions and the following disclaimer.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11  * of conditions and the following disclaimer in the documentation and/or other materials
12  * provided with the distribution.
13  *
14  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
15  * to endorse or promote products derived from this software without specific prior written
16  * permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 #include <cstdio>
31 #include <cstdlib>
32 #include <climits>
33 #include <sys/types.h>
34 #include <gtest/gtest.h>
35 #include <dirent.h>
36 #include <unistd.h>
37 #include <vector>
38 #include <cstring>
39 #include "It_process_plimits.h"
40 
GetFileMode(const char* filePath)41 static std::string GetFileMode(const char* filePath)
42 {
43     const int MODE_COUNT = 11;
44     char fileProperty[MODE_COUNT] = "----------";
45     char fileMode[MODE_COUNT] = "-rwxrwxrwx";
46     struct stat buf;
47     stat(filePath, &buf);
48     unsigned int off = 256;
49     const int LOOP_VARY = 10;
50     for (int i = 1; i < LOOP_VARY; i++) {
51         if (buf.st_mode & (off >> (i - 1))) {
52             fileProperty[i] = fileMode[i];
53         }
54     }
55     return fileProperty;
56 }
57 
IsFilePropertyR1(const char* filePath)58 static int IsFilePropertyR1(const char* filePath)
59 {
60     std::string fileOrg = "-r--r--r--";
61     std::string fileProperty = GetFileMode(filePath);
62     return strcmp(fileProperty.c_str(), fileOrg.c_str());
63 }
64 
ItProcessPlimits008(void)65 void ItProcessPlimits008(void)
66 {
67     std::string filePath = "/proc/plimits/";
68     std::vector<std::string> fileName;
69     fileName.push_back("plimits.procs");
70     fileName.push_back("plimits.limiters");
71     fileName.push_back("pids.max");
72     fileName.push_back("sched.period");
73     fileName.push_back("sched.quota");
74     fileName.push_back("sched.stat");
75     fileName.push_back("memory.failcnt");
76     fileName.push_back("memory.limit");
77     fileName.push_back("memory.peak");
78     fileName.push_back("memory.usage");
79     fileName.push_back("memory.stat");
80 
81     for (auto iter = fileName.begin(); iter != fileName.end(); ++iter) {
82         std::string fileFullPath = filePath + *iter;
83         int ret = IsFilePropertyR1(fileFullPath.c_str());
84         ASSERT_EQ(ret, 0);
85     }
86 
87     return;
88 }
89