| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 <string.h> | 5 #include <string.h> |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/memory/shared_memory.h" |
| 11 #include "base/strings/string_piece.h" | 12 #include "base/strings/string_piece.h" |
| 12 #include "mojo/edk/test/mojo_test_base.h" | 13 #include "mojo/edk/test/mojo_test_base.h" |
| 13 #include "mojo/public/c/system/types.h" | 14 #include "mojo/public/c/system/types.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 16 |
| 16 namespace mojo { | 17 namespace mojo { |
| 17 namespace edk { | 18 namespace edk { |
| 18 namespace { | 19 namespace { |
| 19 | 20 |
| 20 using SharedBufferTest = test::MojoTestBase; | 21 using SharedBufferTest = test::MojoTestBase; |
| 21 | 22 |
| 22 TEST_F(SharedBufferTest, CreateSharedBuffer) { | 23 TEST_F(SharedBufferTest, CreateSharedBuffer) { |
| 23 const std::string message = "hello"; | 24 const std::string message = "hello"; |
| 24 MojoHandle h = CreateBuffer(message.size()); | 25 MojoHandle h = CreateBuffer(message.size()); |
| 25 WriteToBuffer(h, 0, message); | 26 WriteToBuffer(h, 0, message); |
| 26 ExpectBufferContents(h, 0, message); | 27 ExpectBufferContents(h, 0, message); |
| 27 } | 28 } |
| 28 | 29 |
| 29 TEST_F(SharedBufferTest, DuplicateSharedBuffer) { | 30 TEST_F(SharedBufferTest, DuplicateSharedBuffer) { |
| 30 const std::string message = "hello"; | 31 const std::string message = "hello"; |
| 31 MojoHandle h = CreateBuffer(message.size()); | 32 MojoHandle h = CreateBuffer(message.size()); |
| 32 WriteToBuffer(h, 0, message); | 33 WriteToBuffer(h, 0, message); |
| 33 | 34 |
| 34 MojoHandle dupe = DuplicateBuffer(h); | 35 MojoHandle dupe = DuplicateBuffer(h, false); |
| 35 ExpectBufferContents(dupe, 0, message); | 36 ExpectBufferContents(dupe, 0, message); |
| 36 } | 37 } |
| 37 | 38 |
| 38 TEST_F(SharedBufferTest, PassSharedBufferLocal) { | 39 TEST_F(SharedBufferTest, PassSharedBufferLocal) { |
| 39 const std::string message = "hello"; | 40 const std::string message = "hello"; |
| 40 MojoHandle h = CreateBuffer(message.size()); | 41 MojoHandle h = CreateBuffer(message.size()); |
| 41 WriteToBuffer(h, 0, message); | 42 WriteToBuffer(h, 0, message); |
| 42 | 43 |
| 43 MojoHandle dupe = DuplicateBuffer(h); | 44 MojoHandle dupe = DuplicateBuffer(h, false); |
| 44 MojoHandle p0, p1; | 45 MojoHandle p0, p1; |
| 45 CreateMessagePipe(&p0, &p1); | 46 CreateMessagePipe(&p0, &p1); |
| 46 | 47 |
| 47 WriteMessageWithHandles(p0, "...", &dupe, 1); | 48 WriteMessageWithHandles(p0, "...", &dupe, 1); |
| 48 EXPECT_EQ("...", ReadMessageWithHandles(p1, &dupe, 1)); | 49 EXPECT_EQ("...", ReadMessageWithHandles(p1, &dupe, 1)); |
| 49 | 50 |
| 50 ExpectBufferContents(dupe, 0, message); | 51 ExpectBufferContents(dupe, 0, message); |
| 51 } | 52 } |
| 52 | 53 |
| 53 #if !defined(OS_IOS) | 54 #if !defined(OS_IOS) |
| (...skipping 12 matching lines...) Expand all Loading... |
| 66 // Android multi-process tests are not executing the new process. This is flaky. | 67 // Android multi-process tests are not executing the new process. This is flaky. |
| 67 #define MAYBE_PassSharedBufferCrossProcess DISABLED_PassSharedBufferCrossProcess | 68 #define MAYBE_PassSharedBufferCrossProcess DISABLED_PassSharedBufferCrossProcess |
| 68 #else | 69 #else |
| 69 #define MAYBE_PassSharedBufferCrossProcess PassSharedBufferCrossProcess | 70 #define MAYBE_PassSharedBufferCrossProcess PassSharedBufferCrossProcess |
| 70 #endif | 71 #endif |
| 71 TEST_F(SharedBufferTest, MAYBE_PassSharedBufferCrossProcess) { | 72 TEST_F(SharedBufferTest, MAYBE_PassSharedBufferCrossProcess) { |
| 72 const std::string message = "hello"; | 73 const std::string message = "hello"; |
| 73 MojoHandle b = CreateBuffer(message.size()); | 74 MojoHandle b = CreateBuffer(message.size()); |
| 74 | 75 |
| 75 RUN_CHILD_ON_PIPE(CopyToBufferClient, h) | 76 RUN_CHILD_ON_PIPE(CopyToBufferClient, h) |
| 76 MojoHandle dupe = DuplicateBuffer(b); | 77 MojoHandle dupe = DuplicateBuffer(b, false); |
| 77 WriteMessageWithHandles(h, message, &dupe, 1); | 78 WriteMessageWithHandles(h, message, &dupe, 1); |
| 78 WriteMessage(h, "quit"); | 79 WriteMessage(h, "quit"); |
| 79 END_CHILD() | 80 END_CHILD() |
| 80 | 81 |
| 81 ExpectBufferContents(b, 0, message); | 82 ExpectBufferContents(b, 0, message); |
| 82 } | 83 } |
| 83 | 84 |
| 84 // Creates a new buffer, maps it, writes a message contents to it, unmaps it, | 85 // Creates a new buffer, maps it, writes a message contents to it, unmaps it, |
| 85 // and finally passes it back to the parent. | 86 // and finally passes it back to the parent. |
| 86 DEFINE_TEST_CLIENT_TEST_WITH_PIPE(CreateBufferClient, SharedBufferTest, h) { | 87 DEFINE_TEST_CLIENT_TEST_WITH_PIPE(CreateBufferClient, SharedBufferTest, h) { |
| (...skipping 26 matching lines...) Expand all Loading... |
| 113 DEFINE_TEST_CLIENT_TEST_WITH_PIPE(CreateAndPassBuffer, SharedBufferTest, h) { | 114 DEFINE_TEST_CLIENT_TEST_WITH_PIPE(CreateAndPassBuffer, SharedBufferTest, h) { |
| 114 // Receive a pipe handle over the primordial pipe. This will be connected to | 115 // Receive a pipe handle over the primordial pipe. This will be connected to |
| 115 // another child process. | 116 // another child process. |
| 116 MojoHandle other_child; | 117 MojoHandle other_child; |
| 117 std::string message = ReadMessageWithHandles(h, &other_child, 1); | 118 std::string message = ReadMessageWithHandles(h, &other_child, 1); |
| 118 | 119 |
| 119 // Create a new shared buffer. | 120 // Create a new shared buffer. |
| 120 MojoHandle b = CreateBuffer(message.size()); | 121 MojoHandle b = CreateBuffer(message.size()); |
| 121 | 122 |
| 122 // Send a copy of the buffer to the parent and the other child. | 123 // Send a copy of the buffer to the parent and the other child. |
| 123 MojoHandle dupe = DuplicateBuffer(b); | 124 MojoHandle dupe = DuplicateBuffer(b, false); |
| 124 WriteMessageWithHandles(h, "", &b, 1); | 125 WriteMessageWithHandles(h, "", &b, 1); |
| 125 WriteMessageWithHandles(other_child, "", &dupe, 1); | 126 WriteMessageWithHandles(other_child, "", &dupe, 1); |
| 126 | 127 |
| 127 EXPECT_EQ("quit", ReadMessage(h)); | 128 EXPECT_EQ("quit", ReadMessage(h)); |
| 128 WriteMessage(h, "ok"); | 129 WriteMessage(h, "ok"); |
| 129 } | 130 } |
| 130 | 131 |
| 131 DEFINE_TEST_CLIENT_TEST_WITH_PIPE(ReceiveAndEditBuffer, SharedBufferTest, h) { | 132 DEFINE_TEST_CLIENT_TEST_WITH_PIPE(ReceiveAndEditBuffer, SharedBufferTest, h) { |
| 132 // Receive a pipe handle over the primordial pipe. This will be connected to | 133 // Receive a pipe handle over the primordial pipe. This will be connected to |
| 133 // another child process (running CreateAndPassBuffer). | 134 // another child process (running CreateAndPassBuffer). |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 247 EXPECT_EQ("ok", ReadMessage(child2)); | 248 EXPECT_EQ("ok", ReadMessage(child2)); |
| 248 END_CHILD() | 249 END_CHILD() |
| 249 WriteMessage(child1, "quit"); | 250 WriteMessage(child1, "quit"); |
| 250 EXPECT_EQ("ok", ReadMessage(child1)); | 251 EXPECT_EQ("ok", ReadMessage(child1)); |
| 251 END_CHILD() | 252 END_CHILD() |
| 252 | 253 |
| 253 // The second grandchild should have written this message. | 254 // The second grandchild should have written this message. |
| 254 ExpectBufferContents(b, 0, message); | 255 ExpectBufferContents(b, 0, message); |
| 255 } | 256 } |
| 256 | 257 |
| 258 DEFINE_TEST_CLIENT_TEST_WITH_PIPE(ReadAndMapWriteSharedBuffer, |
| 259 SharedBufferTest, h) { |
| 260 // Receive the shared buffer. |
| 261 MojoHandle b; |
| 262 EXPECT_EQ("hello", ReadMessageWithHandles(h, &b, 1)); |
| 263 |
| 264 // Read from the bufer. |
| 265 ExpectBufferContents(b, 0, "hello"); |
| 266 |
| 267 // Extract the shared memory handle and try to map it writable. |
| 268 base::SharedMemoryHandle shm_handle; |
| 269 bool read_only = false; |
| 270 ASSERT_EQ(MOJO_RESULT_OK, |
| 271 PassSharedMemoryHandle(b, &shm_handle, nullptr, &read_only)); |
| 272 base::SharedMemory shared_memory(shm_handle, false); |
| 273 EXPECT_TRUE(read_only); |
| 274 EXPECT_FALSE(shared_memory.Map(1234)); |
| 275 |
| 276 EXPECT_EQ("quit", ReadMessage(h)); |
| 277 WriteMessage(h, "ok"); |
| 278 } |
| 279 |
| 280 #if defined(OS_ANDROID) |
| 281 // Android multi-process tests are not executing the new process. This is flaky. |
| 282 #define MAYBE_CreateAndPassReadOnlyBuffer DISABLED_CreateAndPassReadOnlyBuffer |
| 283 #else |
| 284 #define MAYBE_CreateAndPassReadOnlyBuffer CreateAndPassReadOnlyBuffer |
| 285 #endif |
| 286 TEST_F(SharedBufferTest, MAYBE_CreateAndPassReadOnlyBuffer) { |
| 287 RUN_CHILD_ON_PIPE(ReadAndMapWriteSharedBuffer, h) |
| 288 // Create a new shared buffer. |
| 289 MojoHandle b = CreateBuffer(1234); |
| 290 WriteToBuffer(b, 0, "hello"); |
| 291 |
| 292 // Send a read-only copy of the buffer to the child. |
| 293 MojoHandle dupe = DuplicateBuffer(b, true /* read_only */); |
| 294 WriteMessageWithHandles(h, "hello", &dupe, 1); |
| 295 |
| 296 WriteMessage(h, "quit"); |
| 297 EXPECT_EQ("ok", ReadMessage(h)); |
| 298 END_CHILD() |
| 299 } |
| 300 |
| 301 DEFINE_TEST_CLIENT_TEST_WITH_PIPE(CreateAndPassReadOnlyBuffer, |
| 302 SharedBufferTest, h) { |
| 303 // Create a new shared buffer. |
| 304 MojoHandle b = CreateBuffer(1234); |
| 305 WriteToBuffer(b, 0, "hello"); |
| 306 |
| 307 // Send a read-only copy of the buffer to the parent. |
| 308 MojoHandle dupe = DuplicateBuffer(b, true /* read_only */); |
| 309 WriteMessageWithHandles(h, "", &dupe, 1); |
| 310 |
| 311 EXPECT_EQ("quit", ReadMessage(h)); |
| 312 WriteMessage(h, "ok"); |
| 313 } |
| 314 |
| 315 #if defined(OS_ANDROID) || (defined(OS_POSIX) && !defined(OS_MACOSX)) |
| 316 // Android multi-process tests are not executing the new process. This is flaky. |
| 317 // Non-OSX posix uses a sync broker to create shared memory. Creating read-only |
| 318 // duplicates in child processes is not currently supported via the sync broker. |
| 319 #define MAYBE_CreateAndPassFromChildReadOnlyBuffer \ |
| 320 DISABLED_CreateAndPassFromChildReadOnlyBuffer |
| 321 #else |
| 322 #define MAYBE_CreateAndPassFromChildReadOnlyBuffer \ |
| 323 CreateAndPassFromChildReadOnlyBuffer |
| 324 #endif |
| 325 TEST_F(SharedBufferTest, MAYBE_CreateAndPassFromChildReadOnlyBuffer) { |
| 326 RUN_CHILD_ON_PIPE(CreateAndPassReadOnlyBuffer, h) |
| 327 MojoHandle b; |
| 328 EXPECT_EQ("", ReadMessageWithHandles(h, &b, 1)); |
| 329 ExpectBufferContents(b, 0, "hello"); |
| 330 |
| 331 // Extract the shared memory handle and try to map it writable. |
| 332 base::SharedMemoryHandle shm_handle; |
| 333 bool read_only = false; |
| 334 ASSERT_EQ(MOJO_RESULT_OK, |
| 335 PassSharedMemoryHandle(b, &shm_handle, nullptr, &read_only)); |
| 336 base::SharedMemory shared_memory(shm_handle, false); |
| 337 EXPECT_TRUE(read_only); |
| 338 EXPECT_FALSE(shared_memory.Map(1234)); |
| 339 |
| 340 WriteMessage(h, "quit"); |
| 341 EXPECT_EQ("ok", ReadMessage(h)); |
| 342 END_CHILD() |
| 343 } |
| 344 |
| 257 #endif // !defined(OS_IOS) | 345 #endif // !defined(OS_IOS) |
| 258 | 346 |
| 259 } // namespace | 347 } // namespace |
| 260 } // namespace edk | 348 } // namespace edk |
| 261 } // namespace mojo | 349 } // namespace mojo |
| OLD | NEW |