Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1559)

Unified Diff: mojo/edk/system/shared_buffer_unittest.cc

Issue 1689053003: Support read-only duplicates of Mojo shared buffers. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@mojo-shm-interop
Patch Set: Rebase and fix comment. Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « mojo/edk/system/shared_buffer_dispatcher_unittest.cc ('k') | mojo/edk/test/mojo_test_base.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojo/edk/system/shared_buffer_unittest.cc
diff --git a/mojo/edk/system/shared_buffer_unittest.cc b/mojo/edk/system/shared_buffer_unittest.cc
index 210c2f55f0f16dc6274e35d3339cbbd1e82dd729..9451ae865b3b6d8545960c3121594c9a99354b69 100644
--- a/mojo/edk/system/shared_buffer_unittest.cc
+++ b/mojo/edk/system/shared_buffer_unittest.cc
@@ -8,6 +8,7 @@
#include <utility>
#include "base/logging.h"
+#include "base/memory/shared_memory.h"
#include "base/strings/string_piece.h"
#include "mojo/edk/test/mojo_test_base.h"
#include "mojo/public/c/system/types.h"
@@ -31,7 +32,7 @@ TEST_F(SharedBufferTest, DuplicateSharedBuffer) {
MojoHandle h = CreateBuffer(message.size());
WriteToBuffer(h, 0, message);
- MojoHandle dupe = DuplicateBuffer(h);
+ MojoHandle dupe = DuplicateBuffer(h, false);
ExpectBufferContents(dupe, 0, message);
}
@@ -40,7 +41,7 @@ TEST_F(SharedBufferTest, PassSharedBufferLocal) {
MojoHandle h = CreateBuffer(message.size());
WriteToBuffer(h, 0, message);
- MojoHandle dupe = DuplicateBuffer(h);
+ MojoHandle dupe = DuplicateBuffer(h, false);
MojoHandle p0, p1;
CreateMessagePipe(&p0, &p1);
@@ -73,7 +74,7 @@ TEST_F(SharedBufferTest, MAYBE_PassSharedBufferCrossProcess) {
MojoHandle b = CreateBuffer(message.size());
RUN_CHILD_ON_PIPE(CopyToBufferClient, h)
- MojoHandle dupe = DuplicateBuffer(b);
+ MojoHandle dupe = DuplicateBuffer(b, false);
WriteMessageWithHandles(h, message, &dupe, 1);
WriteMessage(h, "quit");
END_CHILD()
@@ -120,7 +121,7 @@ DEFINE_TEST_CLIENT_TEST_WITH_PIPE(CreateAndPassBuffer, SharedBufferTest, h) {
MojoHandle b = CreateBuffer(message.size());
// Send a copy of the buffer to the parent and the other child.
- MojoHandle dupe = DuplicateBuffer(b);
+ MojoHandle dupe = DuplicateBuffer(b, false);
WriteMessageWithHandles(h, "", &b, 1);
WriteMessageWithHandles(other_child, "", &dupe, 1);
@@ -254,6 +255,93 @@ TEST_F(SharedBufferTest, MAYBE_PassHandleBetweenCousins) {
ExpectBufferContents(b, 0, message);
}
+DEFINE_TEST_CLIENT_TEST_WITH_PIPE(ReadAndMapWriteSharedBuffer,
+ SharedBufferTest, h) {
+ // Receive the shared buffer.
+ MojoHandle b;
+ EXPECT_EQ("hello", ReadMessageWithHandles(h, &b, 1));
+
+ // Read from the bufer.
+ ExpectBufferContents(b, 0, "hello");
+
+ // Extract the shared memory handle and try to map it writable.
+ base::SharedMemoryHandle shm_handle;
+ bool read_only = false;
+ ASSERT_EQ(MOJO_RESULT_OK,
+ PassSharedMemoryHandle(b, &shm_handle, nullptr, &read_only));
+ base::SharedMemory shared_memory(shm_handle, false);
+ EXPECT_TRUE(read_only);
+ EXPECT_FALSE(shared_memory.Map(1234));
+
+ EXPECT_EQ("quit", ReadMessage(h));
+ WriteMessage(h, "ok");
+}
+
+#if defined(OS_ANDROID)
+// Android multi-process tests are not executing the new process. This is flaky.
+#define MAYBE_CreateAndPassReadOnlyBuffer DISABLED_CreateAndPassReadOnlyBuffer
+#else
+#define MAYBE_CreateAndPassReadOnlyBuffer CreateAndPassReadOnlyBuffer
+#endif
+TEST_F(SharedBufferTest, MAYBE_CreateAndPassReadOnlyBuffer) {
+ RUN_CHILD_ON_PIPE(ReadAndMapWriteSharedBuffer, h)
+ // Create a new shared buffer.
+ MojoHandle b = CreateBuffer(1234);
+ WriteToBuffer(b, 0, "hello");
+
+ // Send a read-only copy of the buffer to the child.
+ MojoHandle dupe = DuplicateBuffer(b, true /* read_only */);
+ WriteMessageWithHandles(h, "hello", &dupe, 1);
+
+ WriteMessage(h, "quit");
+ EXPECT_EQ("ok", ReadMessage(h));
+ END_CHILD()
+}
+
+DEFINE_TEST_CLIENT_TEST_WITH_PIPE(CreateAndPassReadOnlyBuffer,
+ SharedBufferTest, h) {
+ // Create a new shared buffer.
+ MojoHandle b = CreateBuffer(1234);
+ WriteToBuffer(b, 0, "hello");
+
+ // Send a read-only copy of the buffer to the parent.
+ MojoHandle dupe = DuplicateBuffer(b, true /* read_only */);
+ WriteMessageWithHandles(h, "", &dupe, 1);
+
+ EXPECT_EQ("quit", ReadMessage(h));
+ WriteMessage(h, "ok");
+}
+
+#if defined(OS_ANDROID) || (defined(OS_POSIX) && !defined(OS_MACOSX))
+// Android multi-process tests are not executing the new process. This is flaky.
+// Non-OSX posix uses a sync broker to create shared memory. Creating read-only
+// duplicates in child processes is not currently supported via the sync broker.
+#define MAYBE_CreateAndPassFromChildReadOnlyBuffer \
+ DISABLED_CreateAndPassFromChildReadOnlyBuffer
+#else
+#define MAYBE_CreateAndPassFromChildReadOnlyBuffer \
+ CreateAndPassFromChildReadOnlyBuffer
+#endif
+TEST_F(SharedBufferTest, MAYBE_CreateAndPassFromChildReadOnlyBuffer) {
+ RUN_CHILD_ON_PIPE(CreateAndPassReadOnlyBuffer, h)
+ MojoHandle b;
+ EXPECT_EQ("", ReadMessageWithHandles(h, &b, 1));
+ ExpectBufferContents(b, 0, "hello");
+
+ // Extract the shared memory handle and try to map it writable.
+ base::SharedMemoryHandle shm_handle;
+ bool read_only = false;
+ ASSERT_EQ(MOJO_RESULT_OK,
+ PassSharedMemoryHandle(b, &shm_handle, nullptr, &read_only));
+ base::SharedMemory shared_memory(shm_handle, false);
+ EXPECT_TRUE(read_only);
+ EXPECT_FALSE(shared_memory.Map(1234));
+
+ WriteMessage(h, "quit");
+ EXPECT_EQ("ok", ReadMessage(h));
+ END_CHILD()
+}
+
#endif // !defined(OS_IOS)
} // namespace
« no previous file with comments | « mojo/edk/system/shared_buffer_dispatcher_unittest.cc ('k') | mojo/edk/test/mojo_test_base.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698