Home | History | Annotate | Download | only in base
      1 // Copyright 2018 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 #include <string>
      6 
      7 #include "mojo/public/cpp/base/shared_memory_mojom_traits.h"
      8 #include "mojo/public/cpp/test_support/test_utils.h"
      9 #include "mojo/public/mojom/base/shared_memory.mojom.h"
     10 #include "testing/gtest/include/gtest/gtest.h"
     11 
     12 TEST(SharedMemoryMojomTest, ReadOnly) {
     13   auto region = base::ReadOnlySharedMemoryRegion::Create(64);
     14   const std::string kTestData = "Hello, world!";
     15   memcpy(region.mapping.memory(), kTestData.data(), kTestData.size());
     16 
     17   base::ReadOnlySharedMemoryRegion read_only_out;
     18   ASSERT_TRUE(mojo::test::SerializeAndDeserialize<
     19               mojo_base::mojom::ReadOnlySharedMemoryRegion>(&region.region,
     20                                                             &read_only_out));
     21   base::ReadOnlySharedMemoryMapping mapping = read_only_out.Map();
     22   EXPECT_EQ(0, memcmp(mapping.memory(), kTestData.data(), kTestData.size()));
     23 }
     24 
     25 TEST(SharedMemoryMojomTest, Writable) {
     26   auto region = base::WritableSharedMemoryRegion::Create(64);
     27   auto mapping = region.Map();
     28   const std::string kTestData = "Hello, world!";
     29   memcpy(mapping.memory(), kTestData.data(), kTestData.size());
     30 
     31   base::WritableSharedMemoryRegion writable_out;
     32   ASSERT_TRUE(mojo::test::SerializeAndDeserialize<
     33               mojo_base::mojom::WritableSharedMemoryRegion>(&region,
     34                                                             &writable_out));
     35 
     36   mapping = writable_out.Map();
     37   EXPECT_EQ(0, memcmp(mapping.memory(), kTestData.data(), kTestData.size()));
     38 }
     39 
     40 TEST(SharedMemoryMojomTest, Unsafe) {
     41   auto region = base::UnsafeSharedMemoryRegion::Create(64);
     42   auto mapping = region.Map();
     43   const std::string kTestData = "Hello, world!";
     44   memcpy(mapping.memory(), kTestData.data(), kTestData.size());
     45 
     46   base::UnsafeSharedMemoryRegion unsafe_out;
     47   ASSERT_TRUE(
     48       mojo::test::SerializeAndDeserialize<
     49           mojo_base::mojom::UnsafeSharedMemoryRegion>(&region, &unsafe_out));
     50 
     51   mapping = unsafe_out.Map();
     52   EXPECT_EQ(0, memcmp(mapping.memory(), kTestData.data(), kTestData.size()));
     53 }
     54