• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef BASE_TEST_TEST_SHARED_MEMORY_UTIL_H_
6 #define BASE_TEST_TEST_SHARED_MEMORY_UTIL_H_
7 
8 #include "base/memory/platform_shared_memory_region.h"
9 #include "base/memory/read_only_shared_memory_region.h"
10 #include "base/memory/shared_memory_handle.h"
11 #include "base/memory/shared_memory_mapping.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 
14 namespace base {
15 
16 // Check that the shared memory |handle| cannot be used to perform
17 // a writable mapping with low-level system APIs like mmap(). Return true
18 // in case of success (i.e. writable mappings are _not_ allowed), or false
19 // otherwise.
20 bool CheckReadOnlySharedMemoryHandleForTesting(SharedMemoryHandle handle);
21 
22 bool CheckReadOnlyPlatformSharedMemoryRegionForTesting(
23     subtle::PlatformSharedMemoryRegion region);
24 
25 // Creates a scoped mapping from a PlatformSharedMemoryRegion. It's useful for
26 // PlatformSharedMemoryRegion testing to not leak mapped memory.
27 // WritableSharedMemoryMapping is used for wrapping because it has max
28 // capabilities but the actual permission depends on the |region|'s mode.
29 // This must not be used in production where PlatformSharedMemoryRegion should
30 // be wrapped with {Writable,Unsafe,ReadOnly}SharedMemoryRegion.
31 WritableSharedMemoryMapping MapAtForTesting(
32     subtle::PlatformSharedMemoryRegion* region,
33     off_t offset,
34     size_t size);
35 
36 WritableSharedMemoryMapping MapForTesting(
37     subtle::PlatformSharedMemoryRegion* region);
38 
39 template <typename SharedMemoryRegionType>
40 std::pair<SharedMemoryRegionType, WritableSharedMemoryMapping>
CreateMappedRegion(size_t size)41 CreateMappedRegion(size_t size) {
42   SharedMemoryRegionType region = SharedMemoryRegionType::Create(size);
43   WritableSharedMemoryMapping mapping = region.Map();
44   return {std::move(region), std::move(mapping)};
45 }
46 
47 // Template specialization of CreateMappedRegion<>() for
48 // the ReadOnlySharedMemoryRegion. We need this because
49 // ReadOnlySharedMemoryRegion::Create() has a different return type.
50 template <>
51 std::pair<ReadOnlySharedMemoryRegion, WritableSharedMemoryMapping>
52 CreateMappedRegion(size_t size);
53 
54 }  // namespace base
55 
56 #endif  // BASE_TEST_TEST_SHARED_MEMORY_UTIL_H_
57