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