Chromium Code Reviews| 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 "base/mac/dispatch_source_mach.h" | 5 #include "base/mac/dispatch_source_mach.h" |
| 6 | 6 |
| 7 #include <mach/mach.h> | 7 #include <mach/mach.h> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/mac/scoped_mach_port.h" | 10 #include "base/mac/scoped_mach_port.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/test/test_timeouts.h" | 12 #include "base/test/test_timeouts.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 14 |
| 15 namespace base { | 15 namespace base { |
| 16 | 16 |
| 17 class DispatchSourceMachTest : public testing::Test { | 17 class DispatchSourceMachTest : public testing::Test { |
| 18 public: | 18 public: |
| 19 void SetUp() override { | 19 void SetUp() override { |
| 20 mach_port_t port = MACH_PORT_NULL; | 20 mach_port_t port = MACH_PORT_NULL; |
| 21 ASSERT_EQ(KERN_SUCCESS, mach_port_allocate(mach_task_self(), | 21 ASSERT_EQ(KERN_SUCCESS, mach_port_allocate(mach_task_self(), |
| 22 MACH_PORT_RIGHT_RECEIVE, &port)); | 22 MACH_PORT_RIGHT_RECEIVE, &port)); |
| 23 receive_right_.reset(port); | 23 receive_right_.reset(port); |
| 24 | 24 |
| 25 ASSERT_EQ(KERN_SUCCESS, mach_port_insert_right(mach_task_self(), port, | 25 ASSERT_EQ(KERN_SUCCESS, mach_port_insert_right(mach_task_self(), port, |
| 26 port, MACH_MSG_TYPE_MAKE_SEND)); | 26 port, MACH_MSG_TYPE_MAKE_SEND)); |
| 27 send_right_.reset(port); | 27 send_right_.reset(port); |
| 28 } | 28 } |
| 29 | 29 |
| 30 mach_port_t port() { return receive_right_.get(); } | 30 mach_port_t GetPort() { return receive_right_.get(); } |
| 31 | 31 |
| 32 void WaitForSemaphore(dispatch_semaphore_t semaphore) { | 32 void WaitForSemaphore(dispatch_semaphore_t semaphore) { |
| 33 dispatch_semaphore_wait(semaphore, dispatch_time( | 33 dispatch_semaphore_wait(semaphore, dispatch_time( |
| 34 DISPATCH_TIME_NOW, | 34 DISPATCH_TIME_NOW, |
| 35 TestTimeouts::action_timeout().InSeconds() * NSEC_PER_SEC)); | 35 TestTimeouts::action_timeout().InSeconds() * NSEC_PER_SEC)); |
| 36 } | 36 } |
| 37 | 37 |
| 38 private: | 38 private: |
| 39 base::mac::ScopedMachReceiveRight receive_right_; | 39 base::mac::ScopedMachReceiveRight receive_right_; |
| 40 base::mac::ScopedMachSendRight send_right_; | 40 base::mac::ScopedMachSendRight send_right_; |
| 41 }; | 41 }; |
| 42 | 42 |
| 43 TEST_F(DispatchSourceMachTest, ReceiveAfterResume) { | 43 TEST_F(DispatchSourceMachTest, ReceiveAfterResume) { |
| 44 dispatch_semaphore_t signal = dispatch_semaphore_create(0); | 44 dispatch_semaphore_t signal = dispatch_semaphore_create(0); |
| 45 mach_port_t port = GetPort(); | |
|
Nico
2015/05/14 02:49:25
this is a noop, right?
Robert Sesek
2015/05/14 14:52:18
For this test, yes. The change is not implicitly c
| |
| 45 | 46 |
| 46 bool __block did_receive = false; | 47 bool __block did_receive = false; |
| 47 DispatchSourceMach source("org.chromium.base.test.ReceiveAfterResume", | 48 DispatchSourceMach source("org.chromium.base.test.ReceiveAfterResume", |
| 48 port(), ^{ | 49 port, ^{ |
| 49 mach_msg_empty_rcv_t msg = {{0}}; | 50 mach_msg_empty_rcv_t msg = {{0}}; |
| 50 msg.header.msgh_size = sizeof(msg); | 51 msg.header.msgh_size = sizeof(msg); |
| 51 msg.header.msgh_local_port = port(); | 52 msg.header.msgh_local_port = port; |
| 52 mach_msg_receive(&msg.header); | 53 mach_msg_receive(&msg.header); |
| 53 did_receive = true; | 54 did_receive = true; |
| 54 | 55 |
| 55 dispatch_semaphore_signal(signal); | 56 dispatch_semaphore_signal(signal); |
| 56 }); | 57 }); |
| 57 | 58 |
| 58 mach_msg_empty_send_t msg = {{0}}; | 59 mach_msg_empty_send_t msg = {{0}}; |
| 59 msg.header.msgh_size = sizeof(msg); | 60 msg.header.msgh_size = sizeof(msg); |
| 60 msg.header.msgh_remote_port = port(); | 61 msg.header.msgh_remote_port = port; |
| 61 msg.header.msgh_bits = MACH_MSGH_BITS_REMOTE(MACH_MSG_TYPE_COPY_SEND); | 62 msg.header.msgh_bits = MACH_MSGH_BITS_REMOTE(MACH_MSG_TYPE_COPY_SEND); |
| 62 ASSERT_EQ(KERN_SUCCESS, mach_msg_send(&msg.header)); | 63 ASSERT_EQ(KERN_SUCCESS, mach_msg_send(&msg.header)); |
| 63 | 64 |
| 64 EXPECT_FALSE(did_receive); | 65 EXPECT_FALSE(did_receive); |
| 65 | 66 |
| 66 source.Resume(); | 67 source.Resume(); |
| 67 | 68 |
| 68 WaitForSemaphore(signal); | 69 WaitForSemaphore(signal); |
| 70 dispatch_release(signal); | |
| 69 | 71 |
| 70 EXPECT_TRUE(did_receive); | 72 EXPECT_TRUE(did_receive); |
| 71 } | 73 } |
| 72 | 74 |
| 73 TEST_F(DispatchSourceMachTest, NoMessagesAfterDestruction) { | 75 TEST_F(DispatchSourceMachTest, NoMessagesAfterDestruction) { |
| 76 mach_port_t port = GetPort(); | |
| 77 | |
| 74 scoped_ptr<int> count(new int(0)); | 78 scoped_ptr<int> count(new int(0)); |
| 75 int* __block count_ptr = count.get(); | 79 int* __block count_ptr = count.get(); |
| 76 | 80 |
| 77 scoped_ptr<DispatchSourceMach> source(new DispatchSourceMach( | 81 scoped_ptr<DispatchSourceMach> source(new DispatchSourceMach( |
| 78 "org.chromium.base.test.NoMessagesAfterDestruction", | 82 "org.chromium.base.test.NoMessagesAfterDestruction", |
| 79 port(), ^{ | 83 port, ^{ |
| 80 mach_msg_empty_rcv_t msg = {{0}}; | 84 mach_msg_empty_rcv_t msg = {{0}}; |
| 81 msg.header.msgh_size = sizeof(msg); | 85 msg.header.msgh_size = sizeof(msg); |
| 82 msg.header.msgh_local_port = port(); | 86 msg.header.msgh_local_port = port; |
| 83 mach_msg_receive(&msg.header); | 87 mach_msg_receive(&msg.header); |
| 84 LOG(INFO) << "Receieve " << *count_ptr; | 88 LOG(INFO) << "Receieve " << *count_ptr; |
| 85 ++(*count_ptr); | 89 ++(*count_ptr); |
| 86 })); | 90 })); |
| 87 source->Resume(); | 91 source->Resume(); |
| 88 | 92 |
| 89 dispatch_queue_t queue = | 93 dispatch_queue_t queue = |
| 90 dispatch_queue_create("org.chromium.base.test.MessageSend", NULL); | 94 dispatch_queue_create("org.chromium.base.test.MessageSend", NULL); |
| 91 dispatch_semaphore_t signal = dispatch_semaphore_create(0); | 95 dispatch_semaphore_t signal = dispatch_semaphore_create(0); |
| 92 for (int i = 0; i < 30; ++i) { | 96 for (int i = 0; i < 30; ++i) { |
| 93 dispatch_async(queue, ^{ | 97 dispatch_async(queue, ^{ |
| 94 mach_msg_empty_send_t msg = {{0}}; | 98 mach_msg_empty_send_t msg = {{0}}; |
| 95 msg.header.msgh_size = sizeof(msg); | 99 msg.header.msgh_size = sizeof(msg); |
| 96 msg.header.msgh_remote_port = port(); | 100 msg.header.msgh_remote_port = port; |
| 97 msg.header.msgh_bits = | 101 msg.header.msgh_bits = |
| 98 MACH_MSGH_BITS_REMOTE(MACH_MSG_TYPE_COPY_SEND); | 102 MACH_MSGH_BITS_REMOTE(MACH_MSG_TYPE_COPY_SEND); |
| 99 mach_msg_send(&msg.header); | 103 mach_msg_send(&msg.header); |
| 100 }); | 104 }); |
| 101 | 105 |
| 102 // After sending five messages, shut down the source and taint the | 106 // After sending five messages, shut down the source and taint the |
| 103 // pointer the handler dereferences. The test will crash if |count_ptr| | 107 // pointer the handler dereferences. The test will crash if |count_ptr| |
| 104 // is being used after "free". | 108 // is being used after "free". |
| 105 if (i == 5) { | 109 if (i == 5) { |
| 106 scoped_ptr<DispatchSourceMach>* source_ptr = &source; | 110 scoped_ptr<DispatchSourceMach>* source_ptr = &source; |
| 107 dispatch_async(queue, ^{ | 111 dispatch_async(queue, ^{ |
| 108 source_ptr->reset(); | 112 source_ptr->reset(); |
| 109 count_ptr = reinterpret_cast<int*>(0xdeaddead); | 113 count_ptr = reinterpret_cast<int*>(0xdeaddead); |
| 110 dispatch_semaphore_signal(signal); | 114 dispatch_semaphore_signal(signal); |
| 111 }); | 115 }); |
| 112 } | 116 } |
| 113 } | 117 } |
| 114 | 118 |
| 115 WaitForSemaphore(signal); | 119 WaitForSemaphore(signal); |
| 120 dispatch_release(signal); | |
| 121 | |
| 116 dispatch_release(queue); | 122 dispatch_release(queue); |
| 117 } | 123 } |
| 118 | 124 |
| 119 } // namespace base | 125 } // namespace base |
| OLD | NEW |