| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "mojo/edk/system/shared_buffer_dispatcher.h" | 5 #include "mojo/edk/system/shared_buffer_dispatcher.h" |
| 6 | 6 |
| 7 #include <limits> | 7 #include <limits> |
| 8 | 8 |
| 9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
| 10 #include "mojo/edk/embedder/platform_shared_buffer.h" | 10 #include "mojo/edk/embedder/platform_shared_buffer.h" |
| 11 #include "mojo/edk/embedder/simple_platform_support.h" | 11 #include "mojo/edk/embedder/simple_platform_support.h" |
| 12 #include "mojo/edk/system/dispatcher.h" | 12 #include "mojo/edk/system/dispatcher.h" |
| 13 #include "mojo/public/cpp/system/macros.h" | 13 #include "mojo/public/cpp/system/macros.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 15 |
| 16 namespace mojo { | 16 namespace mojo { |
| 17 namespace system { | 17 namespace edk { |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 // NOTE(vtl): There's currently not much to test for in | 20 // NOTE(vtl): There's currently not much to test for in |
| 21 // |SharedBufferDispatcher::ValidateCreateOptions()|, but the tests should be | 21 // |SharedBufferDispatcher::ValidateCreateOptions()|, but the tests should be |
| 22 // expanded if/when options are added, so I've kept the general form of the | 22 // expanded if/when options are added, so I've kept the general form of the |
| 23 // tests from data_pipe_unittest.cc. | 23 // tests from data_pipe_unittest.cc. |
| 24 | 24 |
| 25 const uint32_t kSizeOfCreateOptions = sizeof(MojoCreateSharedBufferOptions); | 25 const uint32_t kSizeOfCreateOptions = sizeof(MojoCreateSharedBufferOptions); |
| 26 | 26 |
| 27 // Does a cursory sanity check of |validated_options|. Calls | 27 // Does a cursory sanity check of |validated_options|. Calls |
| (...skipping 10 matching lines...) Expand all Loading... |
| 38 MakeUserPointer(&validated_options), &revalidated_options)); | 38 MakeUserPointer(&validated_options), &revalidated_options)); |
| 39 EXPECT_EQ(validated_options.struct_size, revalidated_options.struct_size); | 39 EXPECT_EQ(validated_options.struct_size, revalidated_options.struct_size); |
| 40 EXPECT_EQ(validated_options.flags, revalidated_options.flags); | 40 EXPECT_EQ(validated_options.flags, revalidated_options.flags); |
| 41 } | 41 } |
| 42 | 42 |
| 43 class SharedBufferDispatcherTest : public testing::Test { | 43 class SharedBufferDispatcherTest : public testing::Test { |
| 44 public: | 44 public: |
| 45 SharedBufferDispatcherTest() {} | 45 SharedBufferDispatcherTest() {} |
| 46 ~SharedBufferDispatcherTest() override {} | 46 ~SharedBufferDispatcherTest() override {} |
| 47 | 47 |
| 48 embedder::PlatformSupport* platform_support() { return &platform_support_; } | 48 PlatformSupport* platform_support() { return &platform_support_; } |
| 49 | 49 |
| 50 private: | 50 private: |
| 51 embedder::SimplePlatformSupport platform_support_; | 51 SimplePlatformSupport platform_support_; |
| 52 | 52 |
| 53 MOJO_DISALLOW_COPY_AND_ASSIGN(SharedBufferDispatcherTest); | 53 MOJO_DISALLOW_COPY_AND_ASSIGN(SharedBufferDispatcherTest); |
| 54 }; | 54 }; |
| 55 | 55 |
| 56 // Tests valid inputs to |ValidateCreateOptions()|. | 56 // Tests valid inputs to |ValidateCreateOptions()|. |
| 57 TEST_F(SharedBufferDispatcherTest, ValidateCreateOptionsValid) { | 57 TEST_F(SharedBufferDispatcherTest, ValidateCreateOptionsValid) { |
| 58 // Default options. | 58 // Default options. |
| 59 { | 59 { |
| 60 MojoCreateSharedBufferOptions validated_options = {}; | 60 MojoCreateSharedBufferOptions validated_options = {}; |
| 61 EXPECT_EQ(MOJO_RESULT_OK, SharedBufferDispatcher::ValidateCreateOptions( | 61 EXPECT_EQ(MOJO_RESULT_OK, SharedBufferDispatcher::ValidateCreateOptions( |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 TEST_F(SharedBufferDispatcherTest, CreateAndMapBuffer) { | 115 TEST_F(SharedBufferDispatcherTest, CreateAndMapBuffer) { |
| 116 scoped_refptr<SharedBufferDispatcher> dispatcher; | 116 scoped_refptr<SharedBufferDispatcher> dispatcher; |
| 117 EXPECT_EQ(MOJO_RESULT_OK, SharedBufferDispatcher::Create( | 117 EXPECT_EQ(MOJO_RESULT_OK, SharedBufferDispatcher::Create( |
| 118 platform_support(), | 118 platform_support(), |
| 119 SharedBufferDispatcher::kDefaultCreateOptions, | 119 SharedBufferDispatcher::kDefaultCreateOptions, |
| 120 100, &dispatcher)); | 120 100, &dispatcher)); |
| 121 ASSERT_TRUE(dispatcher); | 121 ASSERT_TRUE(dispatcher); |
| 122 EXPECT_EQ(Dispatcher::Type::SHARED_BUFFER, dispatcher->GetType()); | 122 EXPECT_EQ(Dispatcher::Type::SHARED_BUFFER, dispatcher->GetType()); |
| 123 | 123 |
| 124 // Make a couple of mappings. | 124 // Make a couple of mappings. |
| 125 scoped_ptr<embedder::PlatformSharedBufferMapping> mapping1; | 125 scoped_ptr<PlatformSharedBufferMapping> mapping1; |
| 126 EXPECT_EQ(MOJO_RESULT_OK, dispatcher->MapBuffer( | 126 EXPECT_EQ(MOJO_RESULT_OK, dispatcher->MapBuffer( |
| 127 0, 100, MOJO_MAP_BUFFER_FLAG_NONE, &mapping1)); | 127 0, 100, MOJO_MAP_BUFFER_FLAG_NONE, &mapping1)); |
| 128 ASSERT_TRUE(mapping1); | 128 ASSERT_TRUE(mapping1); |
| 129 ASSERT_TRUE(mapping1->GetBase()); | 129 ASSERT_TRUE(mapping1->GetBase()); |
| 130 EXPECT_EQ(100u, mapping1->GetLength()); | 130 EXPECT_EQ(100u, mapping1->GetLength()); |
| 131 // Write something. | 131 // Write something. |
| 132 static_cast<char*>(mapping1->GetBase())[50] = 'x'; | 132 static_cast<char*>(mapping1->GetBase())[50] = 'x'; |
| 133 | 133 |
| 134 scoped_ptr<embedder::PlatformSharedBufferMapping> mapping2; | 134 scoped_ptr<PlatformSharedBufferMapping> mapping2; |
| 135 EXPECT_EQ(MOJO_RESULT_OK, dispatcher->MapBuffer( | 135 EXPECT_EQ(MOJO_RESULT_OK, dispatcher->MapBuffer( |
| 136 50, 50, MOJO_MAP_BUFFER_FLAG_NONE, &mapping2)); | 136 50, 50, MOJO_MAP_BUFFER_FLAG_NONE, &mapping2)); |
| 137 ASSERT_TRUE(mapping2); | 137 ASSERT_TRUE(mapping2); |
| 138 ASSERT_TRUE(mapping2->GetBase()); | 138 ASSERT_TRUE(mapping2->GetBase()); |
| 139 EXPECT_EQ(50u, mapping2->GetLength()); | 139 EXPECT_EQ(50u, mapping2->GetLength()); |
| 140 EXPECT_EQ('x', static_cast<char*>(mapping2->GetBase())[0]); | 140 EXPECT_EQ('x', static_cast<char*>(mapping2->GetBase())[0]); |
| 141 | 141 |
| 142 EXPECT_EQ(MOJO_RESULT_OK, dispatcher->Close()); | 142 EXPECT_EQ(MOJO_RESULT_OK, dispatcher->Close()); |
| 143 | 143 |
| 144 // Check that we can still read/write to mappings after the dispatcher has | 144 // Check that we can still read/write to mappings after the dispatcher has |
| 145 // gone away. | 145 // gone away. |
| 146 static_cast<char*>(mapping2->GetBase())[1] = 'y'; | 146 static_cast<char*>(mapping2->GetBase())[1] = 'y'; |
| 147 EXPECT_EQ('y', static_cast<char*>(mapping1->GetBase())[51]); | 147 EXPECT_EQ('y', static_cast<char*>(mapping1->GetBase())[51]); |
| 148 } | 148 } |
| 149 | 149 |
| 150 TEST_F(SharedBufferDispatcherTest, DuplicateBufferHandle) { | 150 TEST_F(SharedBufferDispatcherTest, DuplicateBufferHandle) { |
| 151 scoped_refptr<SharedBufferDispatcher> dispatcher1; | 151 scoped_refptr<SharedBufferDispatcher> dispatcher1; |
| 152 EXPECT_EQ(MOJO_RESULT_OK, SharedBufferDispatcher::Create( | 152 EXPECT_EQ(MOJO_RESULT_OK, SharedBufferDispatcher::Create( |
| 153 platform_support(), | 153 platform_support(), |
| 154 SharedBufferDispatcher::kDefaultCreateOptions, | 154 SharedBufferDispatcher::kDefaultCreateOptions, |
| 155 100, &dispatcher1)); | 155 100, &dispatcher1)); |
| 156 | 156 |
| 157 // Map and write something. | 157 // Map and write something. |
| 158 scoped_ptr<embedder::PlatformSharedBufferMapping> mapping; | 158 scoped_ptr<PlatformSharedBufferMapping> mapping; |
| 159 EXPECT_EQ(MOJO_RESULT_OK, dispatcher1->MapBuffer( | 159 EXPECT_EQ(MOJO_RESULT_OK, dispatcher1->MapBuffer( |
| 160 0, 100, MOJO_MAP_BUFFER_FLAG_NONE, &mapping)); | 160 0, 100, MOJO_MAP_BUFFER_FLAG_NONE, &mapping)); |
| 161 static_cast<char*>(mapping->GetBase())[0] = 'x'; | 161 static_cast<char*>(mapping->GetBase())[0] = 'x'; |
| 162 mapping.reset(); | 162 mapping.reset(); |
| 163 | 163 |
| 164 // Duplicate |dispatcher1| and then close it. | 164 // Duplicate |dispatcher1| and then close it. |
| 165 scoped_refptr<Dispatcher> dispatcher2; | 165 scoped_refptr<Dispatcher> dispatcher2; |
| 166 EXPECT_EQ(MOJO_RESULT_OK, dispatcher1->DuplicateBufferHandle( | 166 EXPECT_EQ(MOJO_RESULT_OK, dispatcher1->DuplicateBufferHandle( |
| 167 NullUserPointer(), &dispatcher2)); | 167 NullUserPointer(), &dispatcher2)); |
| 168 ASSERT_TRUE(dispatcher2); | 168 ASSERT_TRUE(dispatcher2); |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 251 EXPECT_FALSE(dispatcher); | 251 EXPECT_FALSE(dispatcher); |
| 252 } | 252 } |
| 253 | 253 |
| 254 TEST_F(SharedBufferDispatcherTest, MapBufferInvalidArguments) { | 254 TEST_F(SharedBufferDispatcherTest, MapBufferInvalidArguments) { |
| 255 scoped_refptr<SharedBufferDispatcher> dispatcher; | 255 scoped_refptr<SharedBufferDispatcher> dispatcher; |
| 256 EXPECT_EQ(MOJO_RESULT_OK, SharedBufferDispatcher::Create( | 256 EXPECT_EQ(MOJO_RESULT_OK, SharedBufferDispatcher::Create( |
| 257 platform_support(), | 257 platform_support(), |
| 258 SharedBufferDispatcher::kDefaultCreateOptions, | 258 SharedBufferDispatcher::kDefaultCreateOptions, |
| 259 100, &dispatcher)); | 259 100, &dispatcher)); |
| 260 | 260 |
| 261 scoped_ptr<embedder::PlatformSharedBufferMapping> mapping; | 261 scoped_ptr<PlatformSharedBufferMapping> mapping; |
| 262 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, | 262 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, |
| 263 dispatcher->MapBuffer(0, 101, MOJO_MAP_BUFFER_FLAG_NONE, &mapping)); | 263 dispatcher->MapBuffer(0, 101, MOJO_MAP_BUFFER_FLAG_NONE, &mapping)); |
| 264 EXPECT_FALSE(mapping); | 264 EXPECT_FALSE(mapping); |
| 265 | 265 |
| 266 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, | 266 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, |
| 267 dispatcher->MapBuffer(1, 100, MOJO_MAP_BUFFER_FLAG_NONE, &mapping)); | 267 dispatcher->MapBuffer(1, 100, MOJO_MAP_BUFFER_FLAG_NONE, &mapping)); |
| 268 EXPECT_FALSE(mapping); | 268 EXPECT_FALSE(mapping); |
| 269 | 269 |
| 270 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, | 270 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, |
| 271 dispatcher->MapBuffer(0, 0, MOJO_MAP_BUFFER_FLAG_NONE, &mapping)); | 271 dispatcher->MapBuffer(0, 0, MOJO_MAP_BUFFER_FLAG_NONE, &mapping)); |
| 272 EXPECT_FALSE(mapping); | 272 EXPECT_FALSE(mapping); |
| 273 | 273 |
| 274 EXPECT_EQ(MOJO_RESULT_OK, dispatcher->Close()); | 274 EXPECT_EQ(MOJO_RESULT_OK, dispatcher->Close()); |
| 275 } | 275 } |
| 276 | 276 |
| 277 } // namespace | 277 } // namespace |
| 278 } // namespace system | 278 } // namespace edk |
| 279 } // namespace mojo | 279 } // namespace mojo |
| OLD | NEW |