1 /*
2  * Copyright (C) 2024 HiHope Open Source Organization.
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 <cstdio>
17 #include <cstdlib>
18 #include <fcntl.h>
19 #include <string>
20 #include <unistd.h>
21 #include <vector>
22 #include <gtest/gtest.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <sys/ipc.h>
26 #include <sys/shm.h>
27 #include "securec.h"
28 
29 using namespace testing::ext;
30 using namespace std;
31 
32 class ShmctlApiTest : public testing::Test {
33 public:
34     static void SetUpTestCase();
35     static void TearDownTestCase();
36     void SetUp();
37     void TearDown();
38 private:
39 };
SetUp()40 void ShmctlApiTest::SetUp()
41 {
42 }
TearDown()43 void ShmctlApiTest::TearDown()
44 {
45 }
SetUpTestCase()46 void ShmctlApiTest::SetUpTestCase()
47 {
48 }
TearDownTestCase()49 void ShmctlApiTest::TearDownTestCase()
50 {
51 }
52 
53 const int MEM_SIZE = 1024;
54 
55 /*
56  * @tc.number : SUB_KERNEL_SYSCALL_SHMCTL_0100
57  * @tc.name   : ShmctlCmdTestSuccess_0001
58  * @tc.desc   : shmctl command test success.
59  * @tc.size   : MediumTest
60  * @tc.type   : Function
61  * @tc.level  : Level 1
62  */
HWTEST_F(ShmctlApiTest, ShmctlCmdTestSuccess_0001, Function | MediumTest | Level1)63 HWTEST_F(ShmctlApiTest, ShmctlCmdTestSuccess_0001, Function | MediumTest | Level1)
64 {
65     key_t key = ftok("shmfile", 66);
66 
67     int shmid = shmget(key, MEM_SIZE, 0666 | IPC_CREAT);
68     EXPECT_TRUE(shmid != 0);
69 
70     struct shmid_ds ds;
71     int ret = shmctl(shmid, IPC_STAT, &ds);
72     EXPECT_TRUE(ret == 0);
73 
74     ret = shmctl(shmid, IPC_SET, &ds);
75     EXPECT_TRUE(ret == 0);
76 
77     ret = shmctl(shmid, IPC_RMID, nullptr);
78     EXPECT_TRUE(ret == 0);
79 }
80 
81 /*
82  * @tc.number : SUB_KERNEL_SYSCALL_SHMCTL_0200
83  * @tc.name   : ShmctlInvalidCommandFailed_0002
84  * @tc.desc   : shmctl use invalid commands failed.
85  * @tc.size   : MediumTest
86  * @tc.type   : Function
87  * @tc.level  : Level 2
88  */
HWTEST_F(ShmctlApiTest, ShmctlInvalidCommandFailed_0002, Function | MediumTest | Level2)89 HWTEST_F(ShmctlApiTest, ShmctlInvalidCommandFailed_0002, Function | MediumTest | Level2)
90 {
91     key_t key = ftok("shmfile", 66);
92 
93     int shmid = shmget(key, MEM_SIZE, 0666 | IPC_CREAT);
94     EXPECT_TRUE(shmid != 0);
95 
96     errno = 0;
97     int ret = shmctl(shmid, 999, nullptr);
98     EXPECT_TRUE(ret == -1);
99     EXPECT_TRUE(errno == EINVAL);
100 
101     ret = shmctl(shmid, IPC_RMID, nullptr);
102     EXPECT_TRUE(ret == 0);
103 }
104 
105 /*
106  * @tc.number : SUB_KERNEL_SYSCALL_SHMCTL_0300
107  * @tc.name   : ShmctlInvalidIdFailed_0003
108  * @tc.desc   : shmctl use invalid id failed.
109  * @tc.size   : MediumTest
110  * @tc.type   : Function
111  * @tc.level  : Level 2
112  */
HWTEST_F(ShmctlApiTest, ShmctlInvalidIdFailed_0003, Function | MediumTest | Level2)113 HWTEST_F(ShmctlApiTest, ShmctlInvalidIdFailed_0003, Function | MediumTest | Level2)
114 {
115     key_t key = ftok("shmfile", 66);
116 
117     int shmid = shmget(key, MEM_SIZE, 0666 | IPC_CREAT);
118     EXPECT_TRUE(shmid != 0);
119 
120     errno = 0;
121     int ret = shmctl(-1, IPC_STAT, nullptr);
122     EXPECT_TRUE(ret == -1);
123     EXPECT_TRUE(errno == EINVAL);
124 
125     ret = shmctl(shmid, IPC_RMID, nullptr);
126     EXPECT_TRUE(ret == 0);
127 }
128