1//===-- Unittests for shm_open/shm_unlink ---------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "hdr/fcntl_macros.h"
10#include "src/__support/OSUtil/syscall.h"
11#include "src/fcntl/fcntl.h"
12#include "src/sys/mman/mmap.h"
13#include "src/sys/mman/munmap.h"
14#include "src/sys/mman/shm_open.h"
15#include "src/sys/mman/shm_unlink.h"
16#include "src/unistd/close.h"
17#include "src/unistd/ftruncate.h"
18#include "test/UnitTest/ErrnoCheckingTest.h"
19#include "test/UnitTest/ErrnoSetterMatcher.h"
20#include "test/UnitTest/Test.h"
21#include <sys/syscall.h>
22
23using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
24using LlvmLibcShmTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
25// since shm_open/shm_unlink are wrappers around open/unlink, we only focus on
26// testing basic cases and name conversions.
27
28TEST_F(LlvmLibcShmTest, Basic) {
29 const char *name = "/test_shm_open";
30 int fd;
31 ASSERT_THAT(fd = LIBC_NAMESPACE::shm_open(name, O_CREAT | O_RDWR, 0666),
32 returns(GE(0)).with_errno(EQ(0)));
33
34 // check that FD_CLOEXEC is set by default.
35 long flag = LIBC_NAMESPACE::fcntl(fd, F_GETFD);
36 ASSERT_GE(static_cast<int>(flag), 0);
37 EXPECT_NE(static_cast<int>(flag) & FD_CLOEXEC, 0);
38
39 // allocate space using ftruncate
40 ASSERT_THAT(LIBC_NAMESPACE::ftruncate(fd, 4096), Succeeds());
41 // map the shared memory
42 void *addr = LIBC_NAMESPACE::mmap(nullptr, 4096, PROT_READ | PROT_WRITE,
43 MAP_SHARED, fd, 0);
44 ASSERT_NE(addr, MAP_FAILED);
45 // just write random data to the shared memory
46 char data[] = "Despite its name, LLVM has little to do with traditional "
47 "virtual machines.";
48 for (size_t i = 0; i < sizeof(data); ++i)
49 static_cast<char *>(addr)[i] = data[i];
50
51 // close fd does not affect the mapping
52 ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds());
53 for (size_t i = 0; i < sizeof(data); ++i)
54 EXPECT_EQ(static_cast<char *>(addr)[i], data[i]);
55
56 // unmap the shared memory
57 ASSERT_THAT(LIBC_NAMESPACE::munmap(addr, 4096), Succeeds());
58 // remove the shared memory
59 ASSERT_THAT(LIBC_NAMESPACE::shm_unlink(name), Succeeds());
60}
61
62TEST_F(LlvmLibcShmTest, NameConversion) {
63 const char *name = "////test_shm_open";
64 int fd;
65 ASSERT_THAT(fd = LIBC_NAMESPACE::shm_open(name, O_CREAT | O_RDWR, 0666),
66 returns(GE(0)).with_errno(EQ(0)));
67 ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds());
68 ASSERT_THAT(LIBC_NAMESPACE::shm_unlink(name), Succeeds());
69
70 ASSERT_THAT(LIBC_NAMESPACE::shm_open("/123/123", O_CREAT | O_RDWR, 0666),
71 Fails(EINVAL));
72
73 ASSERT_THAT(LIBC_NAMESPACE::shm_open("/.", O_CREAT | O_RDWR, 0666),
74 Fails(EINVAL));
75
76 ASSERT_THAT(LIBC_NAMESPACE::shm_open("/..", O_CREAT | O_RDWR, 0666),
77 Fails(EINVAL));
78}
79

Provided by KDAB

Privacy Policy
Improve your Profiling and Debugging skills
Find out more

source code of libc/test/src/sys/mman/linux/shm_test.cpp