test_shared_memory_util.h 2.1 KB

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