OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <string.h> |
| 6 |
| 7 #include <string> |
| 8 #include <utility> |
| 9 |
| 10 #include "base/logging.h" |
| 11 #include "base/strings/string_piece.h" |
| 12 #include "mojo/edk/test/mojo_test_base.h" |
| 13 #include "mojo/public/c/system/types.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 |
| 16 namespace mojo { |
| 17 namespace edk { |
| 18 namespace { |
| 19 |
| 20 using SharedBufferTest = test::MojoTestBase; |
| 21 |
| 22 TEST_F(SharedBufferTest, CreateSharedBuffer) { |
| 23 const std::string message = "hello"; |
| 24 MojoHandle h = CreateBuffer(message.size()); |
| 25 WriteToBuffer(h, 0, message); |
| 26 ExpectBufferContents(h, 0, message); |
| 27 } |
| 28 |
| 29 TEST_F(SharedBufferTest, DuplicateSharedBuffer) { |
| 30 const std::string message = "hello"; |
| 31 MojoHandle h = CreateBuffer(message.size()); |
| 32 WriteToBuffer(h, 0, message); |
| 33 |
| 34 MojoHandle dupe = DuplicateBuffer(h); |
| 35 ExpectBufferContents(dupe, 0, message); |
| 36 } |
| 37 |
| 38 TEST_F(SharedBufferTest, PassSharedBufferLocal) { |
| 39 const std::string message = "hello"; |
| 40 MojoHandle h = CreateBuffer(message.size()); |
| 41 WriteToBuffer(h, 0, message); |
| 42 |
| 43 MojoHandle dupe = DuplicateBuffer(h); |
| 44 MojoHandle p0, p1; |
| 45 CreateMessagePipe(&p0, &p1); |
| 46 |
| 47 WriteMessageWithHandles(p0, "...", &dupe, 1); |
| 48 EXPECT_EQ("...", ReadMessageWithHandles(p1, &dupe, 1)); |
| 49 |
| 50 ExpectBufferContents(dupe, 0, message); |
| 51 } |
| 52 |
| 53 // Reads a single message with a shared buffer handle, maps the buffer, copies |
| 54 // the message contents into it, then exits. |
| 55 DEFINE_TEST_CLIENT_TEST_WITH_PIPE(CopyToBufferClient, SharedBufferTest, h) { |
| 56 MojoHandle b; |
| 57 std::string message = ReadMessageWithHandles(h, &b, 1); |
| 58 WriteToBuffer(b, 0, message); |
| 59 |
| 60 EXPECT_EQ("quit", ReadMessage(h)); |
| 61 } |
| 62 |
| 63 TEST_F(SharedBufferTest, PassSharedBufferCrossProcess) { |
| 64 const std::string message = "hello"; |
| 65 MojoHandle b = CreateBuffer(message.size()); |
| 66 |
| 67 RUN_CHILD_ON_PIPE(CopyToBufferClient, h) |
| 68 MojoHandle dupe = DuplicateBuffer(b); |
| 69 WriteMessageWithHandles(h, message, &dupe, 1); |
| 70 WriteMessage(h, "quit"); |
| 71 END_CHILD() |
| 72 |
| 73 ExpectBufferContents(b, 0, message); |
| 74 } |
| 75 |
| 76 // Creates a new buffer, maps it, writes a message contents to it, unmaps it, |
| 77 // and finally passes it back to the parent. |
| 78 DEFINE_TEST_CLIENT_TEST_WITH_PIPE(CreateBufferClient, SharedBufferTest, h) { |
| 79 std::string message = ReadMessage(h); |
| 80 MojoHandle b = CreateBuffer(message.size()); |
| 81 WriteToBuffer(b, 0, message); |
| 82 WriteMessageWithHandles(h, "have a buffer", &b, 1); |
| 83 |
| 84 EXPECT_EQ("quit", ReadMessage(h)); |
| 85 } |
| 86 |
| 87 TEST_F(SharedBufferTest, PassSharedBufferFromChild) { |
| 88 const std::string message = "hello"; |
| 89 MojoHandle b; |
| 90 RUN_CHILD_ON_PIPE(CreateBufferClient, h) |
| 91 WriteMessage(h, message); |
| 92 ReadMessageWithHandles(h, &b, 1); |
| 93 WriteMessage(h, "quit"); |
| 94 END_CHILD() |
| 95 |
| 96 ExpectBufferContents(b, 0, message); |
| 97 } |
| 98 |
| 99 DEFINE_TEST_CLIENT_TEST_WITH_PIPE(CreateAndPassBuffer, SharedBufferTest, h) { |
| 100 // Receive a pipe handle over the primordial pipe. This will be connected to |
| 101 // another child process. |
| 102 MojoHandle other_child; |
| 103 std::string message = ReadMessageWithHandles(h, &other_child, 1); |
| 104 |
| 105 // Create a new shared buffer. |
| 106 MojoHandle b = CreateBuffer(message.size()); |
| 107 |
| 108 // Send a copy of the buffer to the parent and the other child. |
| 109 MojoHandle dupe = DuplicateBuffer(b); |
| 110 WriteMessageWithHandles(h, "", &b, 1); |
| 111 WriteMessageWithHandles(other_child, "", &dupe, 1); |
| 112 |
| 113 EXPECT_EQ("quit", ReadMessage(h)); |
| 114 } |
| 115 |
| 116 DEFINE_TEST_CLIENT_TEST_WITH_PIPE(ReceiveAndEditBuffer, SharedBufferTest, h) { |
| 117 // Receive a pipe handle over the primordial pipe. This will be connected to |
| 118 // another child process (running CreateAndPassBuffer). |
| 119 MojoHandle other_child; |
| 120 std::string message = ReadMessageWithHandles(h, &other_child, 1); |
| 121 |
| 122 // Receive a shared buffer from the other child. |
| 123 MojoHandle b; |
| 124 ReadMessageWithHandles(other_child, &b, 1); |
| 125 |
| 126 // Write the message from the parent into the buffer and exit. |
| 127 WriteToBuffer(b, 0, message); |
| 128 |
| 129 EXPECT_EQ("quit", ReadMessage(h)); |
| 130 } |
| 131 |
| 132 TEST_F(SharedBufferTest, PassSharedBufferFromChildToChild) { |
| 133 const std::string message = "hello"; |
| 134 MojoHandle p0, p1; |
| 135 CreateMessagePipe(&p0, &p1); |
| 136 |
| 137 MojoHandle b; |
| 138 RUN_CHILD_ON_PIPE(CreateAndPassBuffer, h0) |
| 139 RUN_CHILD_ON_PIPE(ReceiveAndEditBuffer, h1) |
| 140 // Send one end of the pipe to each child. The first child will create |
| 141 // and pass a buffer to the second child and back to us. The second child |
| 142 // will write our message into the buffer. |
| 143 WriteMessageWithHandles(h0, message, &p0, 1); |
| 144 WriteMessageWithHandles(h1, message, &p1, 1); |
| 145 |
| 146 // Receive the buffer back from the first child. |
| 147 ReadMessageWithHandles(h0, &b, 1); |
| 148 |
| 149 WriteMessage(h1, "quit"); |
| 150 WriteMessage(h0, "quit"); |
| 151 END_CHILD() |
| 152 END_CHILD() |
| 153 |
| 154 // The second child should have written this message. |
| 155 ExpectBufferContents(b, 0, message); |
| 156 } |
| 157 |
| 158 } // namespace |
| 159 } // namespace edk |
| 160 } // namespace mojo |
OLD | NEW |