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
31 #include <cstdio>
32 #include <cstdlib>
33 #include <unistd.h>
34 #include <fcntl.h>
35 #include <cstring>
36 #include <climits>
37 #include <sys/types.h>
38 #include <gtest/gtest.h>
39 #include <dirent.h>
40 #include "It_process_plimits.h"
41
42 static int g_processOs = 8;
43
ItProcessPlimitsPid005(void)44 void ItProcessPlimitsPid005(void)
45 {
46 mode_t mode = 0;
47 std::string path = "/proc/plimits/test";
48 std::string filepath = "/proc/plimits/test/plimits.procs";
49 int pid = getpid();
50 int vprocessId = 90;
51 std::string missPid = std::to_string(vprocessId);
52 std::string runPid = std::to_string(pid);
53
54 int ret = mkdir(path.c_str(), S_IFDIR | mode);
55 ASSERT_EQ(ret, 0);
56
57 int fd = open(filepath.c_str(), O_WRONLY);
58 ASSERT_NE(fd, -1);
59
60 for (int i = 1; i <= g_processOs; i++) {
61 std::string fWrite = std::to_string(i);
62 ret = write(fd, fWrite.c_str(), (fWrite.length()));
63 ASSERT_EQ(ret, -1);
64 }
65
66 ret = write(fd, missPid.c_str(), (missPid.length()));
67 ASSERT_EQ(ret, -1);
68
69 ret = write(fd, path.c_str(), (path.length()));
70 ASSERT_EQ(ret, -1);
71
72 ret = write(fd, runPid.c_str(), (runPid.length()));
73 ASSERT_NE(ret, -1);
74 close(fd);
75 ret = rmdir(path.c_str());
76 ASSERT_EQ(ret, 0);
77 return;
78 }
79