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 <memory> |
| 10 |
9 #include "base/logging.h" | 11 #include "base/logging.h" |
10 #include "base/mac/scoped_mach_port.h" | 12 #include "base/mac/scoped_mach_port.h" |
11 #include "base/memory/scoped_ptr.h" | |
12 #include "base/test/test_timeouts.h" | 13 #include "base/test/test_timeouts.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
14 | 15 |
15 namespace base { | 16 namespace base { |
16 | 17 |
17 class DispatchSourceMachTest : public testing::Test { | 18 class DispatchSourceMachTest : public testing::Test { |
18 public: | 19 public: |
19 void SetUp() override { | 20 void SetUp() override { |
20 mach_port_t port = MACH_PORT_NULL; | 21 mach_port_t port = MACH_PORT_NULL; |
21 ASSERT_EQ(KERN_SUCCESS, mach_port_allocate(mach_task_self(), | 22 ASSERT_EQ(KERN_SUCCESS, mach_port_allocate(mach_task_self(), |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 | 69 |
69 WaitForSemaphore(signal); | 70 WaitForSemaphore(signal); |
70 dispatch_release(signal); | 71 dispatch_release(signal); |
71 | 72 |
72 EXPECT_TRUE(did_receive); | 73 EXPECT_TRUE(did_receive); |
73 } | 74 } |
74 | 75 |
75 TEST_F(DispatchSourceMachTest, NoMessagesAfterDestruction) { | 76 TEST_F(DispatchSourceMachTest, NoMessagesAfterDestruction) { |
76 mach_port_t port = GetPort(); | 77 mach_port_t port = GetPort(); |
77 | 78 |
78 scoped_ptr<int> count(new int(0)); | 79 std::unique_ptr<int> count(new int(0)); |
79 int* __block count_ptr = count.get(); | 80 int* __block count_ptr = count.get(); |
80 | 81 |
81 scoped_ptr<DispatchSourceMach> source(new DispatchSourceMach( | 82 std::unique_ptr<DispatchSourceMach> source(new DispatchSourceMach( |
82 "org.chromium.base.test.NoMessagesAfterDestruction", | 83 "org.chromium.base.test.NoMessagesAfterDestruction", port, ^{ |
83 port, ^{ | 84 mach_msg_empty_rcv_t msg = {{0}}; |
84 mach_msg_empty_rcv_t msg = {{0}}; | 85 msg.header.msgh_size = sizeof(msg); |
85 msg.header.msgh_size = sizeof(msg); | 86 msg.header.msgh_local_port = port; |
86 msg.header.msgh_local_port = port; | 87 mach_msg_receive(&msg.header); |
87 mach_msg_receive(&msg.header); | 88 LOG(INFO) << "Receieve " << *count_ptr; |
88 LOG(INFO) << "Receieve " << *count_ptr; | 89 ++(*count_ptr); |
89 ++(*count_ptr); | |
90 })); | 90 })); |
91 source->Resume(); | 91 source->Resume(); |
92 | 92 |
93 dispatch_queue_t queue = | 93 dispatch_queue_t queue = |
94 dispatch_queue_create("org.chromium.base.test.MessageSend", NULL); | 94 dispatch_queue_create("org.chromium.base.test.MessageSend", NULL); |
95 dispatch_semaphore_t signal = dispatch_semaphore_create(0); | 95 dispatch_semaphore_t signal = dispatch_semaphore_create(0); |
96 for (int i = 0; i < 30; ++i) { | 96 for (int i = 0; i < 30; ++i) { |
97 dispatch_async(queue, ^{ | 97 dispatch_async(queue, ^{ |
98 mach_msg_empty_send_t msg = {{0}}; | 98 mach_msg_empty_send_t msg = {{0}}; |
99 msg.header.msgh_size = sizeof(msg); | 99 msg.header.msgh_size = sizeof(msg); |
100 msg.header.msgh_remote_port = port; | 100 msg.header.msgh_remote_port = port; |
101 msg.header.msgh_bits = | 101 msg.header.msgh_bits = |
102 MACH_MSGH_BITS_REMOTE(MACH_MSG_TYPE_COPY_SEND); | 102 MACH_MSGH_BITS_REMOTE(MACH_MSG_TYPE_COPY_SEND); |
103 mach_msg_send(&msg.header); | 103 mach_msg_send(&msg.header); |
104 }); | 104 }); |
105 | 105 |
106 // After sending five messages, shut down the source and taint the | 106 // After sending five messages, shut down the source and taint the |
107 // pointer the handler dereferences. The test will crash if |count_ptr| | 107 // pointer the handler dereferences. The test will crash if |count_ptr| |
108 // is being used after "free". | 108 // is being used after "free". |
109 if (i == 5) { | 109 if (i == 5) { |
110 scoped_ptr<DispatchSourceMach>* source_ptr = &source; | 110 std::unique_ptr<DispatchSourceMach>* source_ptr = &source; |
111 dispatch_async(queue, ^{ | 111 dispatch_async(queue, ^{ |
112 source_ptr->reset(); | 112 source_ptr->reset(); |
113 count_ptr = reinterpret_cast<int*>(0xdeaddead); | 113 count_ptr = reinterpret_cast<int*>(0xdeaddead); |
114 dispatch_semaphore_signal(signal); | 114 dispatch_semaphore_signal(signal); |
115 }); | 115 }); |
116 } | 116 } |
117 } | 117 } |
118 | 118 |
119 WaitForSemaphore(signal); | 119 WaitForSemaphore(signal); |
120 dispatch_release(signal); | 120 dispatch_release(signal); |
121 | 121 |
122 dispatch_release(queue); | 122 dispatch_release(queue); |
123 } | 123 } |
124 | 124 |
125 } // namespace base | 125 } // namespace base |
OLD | NEW |