1 /**
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
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 <fcntl.h>
17 #include <sys/mman.h>
18 #include <sys/stat.h>
19 #include <unistd.h>
20 #include <stdio.h>
21 #include "functionalext.h"
22
23 /**
24 * @tc.name : shm_open_0100
25 * @tc.desc : Test if shm_open can successfully create and open a shared memory object.
26 * @tc.level : level 0
27 */
shm_open_0100null28 void shm_open_0100()
29 {
30 const char* shm_name = "/test_shm";
31 int fd = shm_open(shm_name, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
32 EXPECT_NE("shm_open_0100", fd, -1);
33 close(fd);
34 shm_unlink(shm_name);
35 }
36
37 /**
38 * @tc.name : shm_open_0200
39 * @tc.desc : Test if shm_open can successfully open an existing shared memory object without the O_CREAT flag.
40 * @tc.level : level 0
41 */
shm_open_0200null42 void shm_open_0200()
43 {
44 const char* shm_name = "/test_shm_0200";
45 int fd1 = shm_open(shm_name, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
46 EXPECT_NE("shm_open_0200", fd1, -1);
47 close(fd1);
48 int fd2 = shm_open(shm_name, O_RDWR, S_IRUSR | S_IWUSR);
49 EXPECT_NE("shm_open_0200", fd2, -1);
50 close(fd2);
51 shm_unlink(shm_name);
52 }
53
54
55 /**
56 * @tc.name : shm_open_0300
57 * @tc.desc : Test if shm_open returns an error when the shared memory
58 * object already exists and O_CREAT and O_EXCL flags are set.
59 * @tc.level : level 0
60 */
shm_open_0300null61 void shm_open_0300()
62 {
63 const char* shm_name = "/test_shm_0300";
64 int fd1 = shm_open(shm_name, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
65 EXPECT_NE("shm_open_0200", fd1, -1);
66 close(fd1);
67 int fd2 = shm_open(shm_name, O_CREAT | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR);
68 EXPECT_EQ("shm_open_0300", fd2, -1);
69 if (fd2 != -1) {
70 close(fd2);
71 }
72 shm_unlink(shm_name);
73 }
74
75 /**
76 * @tc.name : shm_open_0400
77 * @tc.desc : Test if shm_open returns an error when trying to open a non-existent
78 * shared memory object without the O_CREAT flag.
79 * @tc.level : level 0
80 */
shm_open_0400null81 void shm_open_0400()
82 {
83 const char* shm_name = "/test_shm_0400";
84 int fd = shm_open(shm_name, O_RDWR, S_IRUSR | S_IWUSR);
85 EXPECT_EQ("shm_open_0400", fd, -1);
86 if (fd != -1) {
87 close(fd);
88 }
89 }
90
91
mainnull92 int main()
93 {
94 shm_open_0100();
95 shm_open_0200();
96 shm_open_0300();
97 shm_open_0400();
98 return t_status;
99 }
100