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

Unified Diff: sandbox/mac/dispatch_source_mach_unittest.cc

Issue 392273002: Create DispatchSourceMach to run a MACH_RECV dispatch source. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: I despise the shared_library build with a deep, deep passion Created 6 years, 5 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 | « sandbox/mac/dispatch_source_mach.cc ('k') | sandbox/mac/mach_message_server.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sandbox/mac/dispatch_source_mach_unittest.cc
diff --git a/sandbox/mac/dispatch_source_mach_unittest.cc b/sandbox/mac/dispatch_source_mach_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..742f14e5206b183ba7ff4f0a7dc6cfddcf1004d3
--- /dev/null
+++ b/sandbox/mac/dispatch_source_mach_unittest.cc
@@ -0,0 +1,119 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "sandbox/mac/dispatch_source_mach.h"
+
+#include <mach/mach.h>
+
+#include "base/logging.h"
+#include "base/mac/scoped_mach_port.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/test/test_timeouts.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace sandbox {
+
+class DispatchSourceMachTest : public testing::Test {
+ public:
+ virtual void SetUp() OVERRIDE {
+ mach_port_t port = MACH_PORT_NULL;
+ ASSERT_EQ(KERN_SUCCESS, mach_port_allocate(mach_task_self(),
+ MACH_PORT_RIGHT_RECEIVE, &port));
+ receive_right_.reset(port);
+
+ ASSERT_EQ(KERN_SUCCESS, mach_port_insert_right(mach_task_self(), port,
+ port, MACH_MSG_TYPE_MAKE_SEND));
+ send_right_.reset(port);
+ }
+
+ mach_port_t port() { return receive_right_.get(); }
+
+ void WaitForSemaphore(dispatch_semaphore_t semaphore) {
+ dispatch_semaphore_wait(semaphore, dispatch_time(
+ DISPATCH_TIME_NOW,
+ TestTimeouts::action_timeout().InSeconds() * NSEC_PER_SEC));
+ }
+
+ private:
+ base::mac::ScopedMachReceiveRight receive_right_;
+ base::mac::ScopedMachSendRight send_right_;
+};
+
+TEST_F(DispatchSourceMachTest, ReceiveAfterResume) {
+ dispatch_semaphore_t signal = dispatch_semaphore_create(0);
+
+ bool __block did_receive = false;
+ DispatchSourceMach source("org.chromium.sandbox.test.ReceiveAfterResume",
+ port(), ^{
+ mach_msg_empty_rcv_t msg = {{0}};
+ msg.header.msgh_size = sizeof(msg);
+ msg.header.msgh_local_port = port();
+ mach_msg_receive(&msg.header);
+ did_receive = true;
+
+ dispatch_semaphore_signal(signal);
+ });
+
+ mach_msg_empty_send_t msg = {{0}};
+ msg.header.msgh_size = sizeof(msg);
+ msg.header.msgh_remote_port = port();
+ msg.header.msgh_bits = MACH_MSGH_BITS_REMOTE(MACH_MSG_TYPE_COPY_SEND);
+ ASSERT_EQ(KERN_SUCCESS, mach_msg_send(&msg.header));
+
+ EXPECT_FALSE(did_receive);
+
+ source.Resume();
+
+ WaitForSemaphore(signal);
+
+ EXPECT_TRUE(did_receive);
+}
+
+TEST_F(DispatchSourceMachTest, NoMessagesAfterDestruction) {
+ scoped_ptr<int> count(new int(0));
+ int* __block count_ptr = count.get();
+
+ scoped_ptr<DispatchSourceMach> source(new DispatchSourceMach(
+ "org.chromium.sandbox.test.NoMessagesAfterDestruction",
+ port(), ^{
+ mach_msg_empty_rcv_t msg = {{0}};
+ msg.header.msgh_size = sizeof(msg);
+ msg.header.msgh_local_port = port();
+ mach_msg_receive(&msg.header);
+ LOG(INFO) << "Receieve " << *count_ptr;
+ ++(*count_ptr);
+ }));
+ source->Resume();
+
+ dispatch_queue_t queue =
+ dispatch_queue_create("org.chromium.sandbox.test.MessageSend", NULL);
+ dispatch_semaphore_t signal = dispatch_semaphore_create(0);
+ for (int i = 0; i < 30; ++i) {
+ dispatch_async(queue, ^{
+ mach_msg_empty_send_t msg = {{0}};
+ msg.header.msgh_size = sizeof(msg);
+ msg.header.msgh_remote_port = port();
+ msg.header.msgh_bits =
+ MACH_MSGH_BITS_REMOTE(MACH_MSG_TYPE_COPY_SEND);
+ mach_msg_send(&msg.header);
+ });
+
+ // After sending five messages, shut down the source and taint the
+ // pointer the handler dereferences. The test will crash if |count_ptr|
+ // is being used after "free".
+ if (i == 5) {
+ scoped_ptr<DispatchSourceMach>* source_ptr = &source;
+ dispatch_async(queue, ^{
+ source_ptr->reset();
+ count_ptr = reinterpret_cast<int*>(0xdeaddead);
+ dispatch_semaphore_signal(signal);
+ });
+ }
+ }
+
+ WaitForSemaphore(signal);
+ dispatch_release(queue);
+}
+
+} // namespace sandbox
« no previous file with comments | « sandbox/mac/dispatch_source_mach.cc ('k') | sandbox/mac/mach_message_server.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698