19762338dSopenharmony_ci/*
29762338dSopenharmony_ci * Copyright (C) 2024 HiHope Open Source Organization.
39762338dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
49762338dSopenharmony_ci * you may not use this file except in compliance with the License.
59762338dSopenharmony_ci * You may obtain a copy of the License at
69762338dSopenharmony_ci *
79762338dSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
89762338dSopenharmony_ci *
99762338dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
109762338dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
119762338dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
129762338dSopenharmony_ci * See the License for the specific language governing permissions and
139762338dSopenharmony_ci * limitations under the License.
149762338dSopenharmony_ci */
159762338dSopenharmony_ci
169762338dSopenharmony_ci#include <cerrno>
179762338dSopenharmony_ci#include <cstdio>
189762338dSopenharmony_ci#include <cstdlib>
199762338dSopenharmony_ci#include <string>
209762338dSopenharmony_ci#include <vector>
219762338dSopenharmony_ci#include <unistd.h>
229762338dSopenharmony_ci#include <gtest/gtest.h>
239762338dSopenharmony_ci#include <sys/resource.h>
249762338dSopenharmony_ci#include <sys/stat.h>
259762338dSopenharmony_ci#include <sys/types.h>
269762338dSopenharmony_ci
279762338dSopenharmony_ci
289762338dSopenharmony_ciusing namespace testing::ext;
299762338dSopenharmony_ciusing namespace std;
309762338dSopenharmony_ci
319762338dSopenharmony_ciclass Prlimit64ApiTest : public testing::Test {
329762338dSopenharmony_cipublic:
339762338dSopenharmony_ci    static void SetUpTestCase();
349762338dSopenharmony_ci    static void TearDownTestCase();
359762338dSopenharmony_ci    void SetUp();
369762338dSopenharmony_ci    void TearDown();
379762338dSopenharmony_ciprivate:
389762338dSopenharmony_ci};
399762338dSopenharmony_civoid Prlimit64ApiTest::SetUp()
409762338dSopenharmony_ci{
419762338dSopenharmony_ci}
429762338dSopenharmony_civoid Prlimit64ApiTest::TearDown()
439762338dSopenharmony_ci{
449762338dSopenharmony_ci}
459762338dSopenharmony_civoid Prlimit64ApiTest::SetUpTestCase()
469762338dSopenharmony_ci{
479762338dSopenharmony_ci}
489762338dSopenharmony_civoid Prlimit64ApiTest::TearDownTestCase()
499762338dSopenharmony_ci{
509762338dSopenharmony_ci}
519762338dSopenharmony_ci
529762338dSopenharmony_ci/*
539762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_PRLIMIT64_0100
549762338dSopenharmony_ci * @tc.name   : PrlimitSetGetPIDResourceLimitsSuccess_0001
559762338dSopenharmony_ci * @tc.desc   : Prlimit64 get and set pid resource limits success.
569762338dSopenharmony_ci * @tc.size   : MediumTest
579762338dSopenharmony_ci * @tc.type   : Function
589762338dSopenharmony_ci * @tc.level  : Level 1
599762338dSopenharmony_ci */
609762338dSopenharmony_ciHWTEST_F(Prlimit64ApiTest, PrlimitSetGetPIDResourceLimitsSuccess_0001, Function | MediumTest | Level1)
619762338dSopenharmony_ci{
629762338dSopenharmony_ci    int ret;
639762338dSopenharmony_ci    pid_t pid = getpid();
649762338dSopenharmony_ci    struct rlimit limit;
659762338dSopenharmony_ci    struct rlimit limit2;
669762338dSopenharmony_ci    ret = prlimit64(pid, RLIMIT_NPROC, nullptr, &limit);
679762338dSopenharmony_ci    EXPECT_EQ(ret, 0);
689762338dSopenharmony_ci    ret = prlimit64(pid, RLIMIT_NPROC, &limit, nullptr);
699762338dSopenharmony_ci    EXPECT_EQ(ret, 0);
709762338dSopenharmony_ci    ret = prlimit64(0, RLIMIT_NPROC, &limit, &limit2);
719762338dSopenharmony_ci    EXPECT_EQ(ret, 0);
729762338dSopenharmony_ci    ret = prlimit64(pid, RLIMIT_NOFILE, nullptr, &limit);
739762338dSopenharmony_ci    EXPECT_EQ(ret, 0);
749762338dSopenharmony_ci    ret = prlimit64(0, RLIMIT_CORE, &limit, &limit2);
759762338dSopenharmony_ci    EXPECT_EQ(ret, 0);
769762338dSopenharmony_ci    ret = prlimit64(0, RLIMIT_LOCKS, &limit, &limit2);
779762338dSopenharmony_ci    EXPECT_EQ(ret, 0);
789762338dSopenharmony_ci    ret = prlimit64(0, RLIMIT_MEMLOCK, &limit, &limit2);
799762338dSopenharmony_ci    EXPECT_EQ(ret, 0);
809762338dSopenharmony_ci    ret = prlimit64(0, RLIMIT_MSGQUEUE, &limit, &limit2);
819762338dSopenharmony_ci    EXPECT_EQ(ret, 0);
829762338dSopenharmony_ci    ret = prlimit64(0, RLIMIT_NOFILE, &limit, &limit2);
839762338dSopenharmony_ci    EXPECT_EQ(ret, 0);
849762338dSopenharmony_ci    ret = prlimit64(0, RLIMIT_RTPRIO, &limit, &limit2);
859762338dSopenharmony_ci    EXPECT_EQ(ret, 0);
869762338dSopenharmony_ci    ret = prlimit64(0, RLIMIT_RTTIME, &limit, &limit2);
879762338dSopenharmony_ci    EXPECT_EQ(ret, 0);
889762338dSopenharmony_ci    ret = prlimit64(0, RLIMIT_SIGPENDING, &limit, &limit2);
899762338dSopenharmony_ci    EXPECT_EQ(ret, 0);
909762338dSopenharmony_ci    ret = prlimit64(0, RLIMIT_STACK, &limit, &limit2);
919762338dSopenharmony_ci    EXPECT_EQ(ret, 0);
929762338dSopenharmony_ci    ret = prlimit64(0, RLIMIT_NICE, &limit, &limit2);
939762338dSopenharmony_ci    EXPECT_EQ(ret, 0);
949762338dSopenharmony_ci}
959762338dSopenharmony_ci
969762338dSopenharmony_ci/*
979762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_PRLIMIT64_0200
989762338dSopenharmony_ci * @tc.name   : PrlimitSetGetPIDResourceLimitsFail_0002
999762338dSopenharmony_ci * @tc.desc   : Prlimit64 set and get pid resource limits fail.
1009762338dSopenharmony_ci * @tc.size   : MediumTest
1019762338dSopenharmony_ci * @tc.type   : Function
1029762338dSopenharmony_ci * @tc.level  : Level 2
1039762338dSopenharmony_ci */
1049762338dSopenharmony_ciHWTEST_F(Prlimit64ApiTest, PrlimitSetGetPIDResourceLimitsFail_0002, Function | MediumTest | Level2)
1059762338dSopenharmony_ci{
1069762338dSopenharmony_ci    int ret;
1079762338dSopenharmony_ci    struct rlimit limit;
1089762338dSopenharmony_ci    pid_t pid = getpid();
1099762338dSopenharmony_ci
1109762338dSopenharmony_ci    errno = 0;
1119762338dSopenharmony_ci    ret = prlimit64(-2, RLIMIT_NPROC, nullptr, &limit);
1129762338dSopenharmony_ci    EXPECT_EQ(ret, -1);
1139762338dSopenharmony_ci    EXPECT_EQ(errno, ESRCH);
1149762338dSopenharmony_ci
1159762338dSopenharmony_ci    errno = 0;
1169762338dSopenharmony_ci    ret = prlimit64(pid, -1, nullptr, &limit);
1179762338dSopenharmony_ci    EXPECT_EQ(ret, -1);
1189762338dSopenharmony_ci    EXPECT_EQ(errno, EINVAL);
1199762338dSopenharmony_ci}
120