1 // Copyright 2017 The Chromium Authors 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_mapping.h" 11 #include "testing/gtest/include/gtest/gtest.h" 12 13 namespace base { 14 15 // Check that the shared memory |region| cannot be used to perform a writable 16 // mapping with low-level system APIs like mmap(). Return true in case of 17 // success (i.e. writable mappings are _not_ allowed), or false otherwise. 18 bool CheckReadOnlyPlatformSharedMemoryRegionForTesting( 19 subtle::PlatformSharedMemoryRegion region); 20 21 // Creates a scoped mapping from a PlatformSharedMemoryRegion. It's useful for 22 // PlatformSharedMemoryRegion testing to not leak mapped memory. 23 // WritableSharedMemoryMapping is used for wrapping because it has max 24 // capabilities but the actual permission depends on the |region|'s mode. 25 // This must not be used in production where PlatformSharedMemoryRegion should 26 // be wrapped with {Writable,Unsafe,ReadOnly}SharedMemoryRegion. 27 WritableSharedMemoryMapping MapAtForTesting( 28 subtle::PlatformSharedMemoryRegion* region, 29 uint64_t offset, 30 size_t size); 31 32 WritableSharedMemoryMapping MapForTesting( 33 subtle::PlatformSharedMemoryRegion* region); 34 35 template <typename SharedMemoryRegionType> 36 std::pair<SharedMemoryRegionType, WritableSharedMemoryMapping> CreateMappedRegion(size_t size)37CreateMappedRegion(size_t size) { 38 SharedMemoryRegionType region = SharedMemoryRegionType::Create(size); 39 WritableSharedMemoryMapping mapping = region.Map(); 40 return {std::move(region), std::move(mapping)}; 41 } 42 43 // Template specialization of CreateMappedRegion<>() for 44 // the ReadOnlySharedMemoryRegion. We need this because 45 // ReadOnlySharedMemoryRegion::Create() has a different return type. 46 template <> 47 std::pair<ReadOnlySharedMemoryRegion, WritableSharedMemoryMapping> 48 CreateMappedRegion(size_t size); 49 50 } // namespace base 51 52 #endif // BASE_TEST_TEST_SHARED_MEMORY_UTIL_H_ 53