Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "mojo/edk/test/mojo_test_base.h" | 5 #include "mojo/edk/test/mojo_test_base.h" |
| 6 | 6 |
| 7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
| 8 #include "base/memory/ref_counted.h" | |
| 8 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 9 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| 11 #include "base/synchronization/waitable_event.h" | |
| 10 #include "mojo/edk/embedder/embedder.h" | 12 #include "mojo/edk/embedder/embedder.h" |
| 11 #include "mojo/edk/system/handle_signals_state.h" | 13 #include "mojo/edk/system/handle_signals_state.h" |
| 12 #include "mojo/public/c/system/buffer.h" | 14 #include "mojo/public/c/system/buffer.h" |
| 13 #include "mojo/public/c/system/data_pipe.h" | 15 #include "mojo/public/c/system/data_pipe.h" |
| 14 #include "mojo/public/c/system/functions.h" | 16 #include "mojo/public/c/system/functions.h" |
| 17 #include "mojo/public/c/system/watcher.h" | |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 19 |
| 17 #if defined(OS_MACOSX) && !defined(OS_IOS) | 20 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 18 #include "base/mac/mach_port_broker.h" | 21 #include "base/mac/mach_port_broker.h" |
| 19 #endif | 22 #endif |
| 20 | 23 |
| 21 namespace mojo { | 24 namespace mojo { |
| 22 namespace edk { | 25 namespace edk { |
| 23 namespace test { | 26 namespace test { |
| 24 | 27 |
| 28 namespace { | |
| 29 | |
| 30 class Waiter { | |
| 31 public: | |
| 32 Waiter() {} | |
| 33 ~Waiter() {} | |
| 34 | |
| 35 MojoResult Wait(MojoHandle handle, | |
| 36 MojoHandleSignals signals, | |
| 37 MojoHandleSignalsState* state) { | |
| 38 MojoHandle watcher; | |
| 39 MojoCreateWatcher(&Context::OnNotification, &watcher); | |
| 40 | |
| 41 context_ = new Context(); | |
| 42 | |
| 43 DCHECK_EQ(MOJO_RESULT_OK, | |
| 44 MojoWatch(watcher, handle, signals, | |
| 45 reinterpret_cast<uintptr_t>( | |
| 46 new scoped_refptr<Context>(context_)))); | |
|
yzshen1
2017/03/14 18:20:44
I feel that it is a little easier to read if we us
Ken Rockot(use gerrit already)
2017/03/14 19:16:18
Done
| |
| 47 | |
| 48 uint32_t num_ready_contexts = 1; | |
| 49 uintptr_t ready_context; | |
| 50 MojoResult ready_result; | |
| 51 MojoHandleSignalsState ready_state; | |
| 52 MojoResult rv = MojoArmWatcher(watcher, &num_ready_contexts, &ready_context, | |
| 53 &ready_result, &ready_state); | |
| 54 if (rv == MOJO_RESULT_FAILED_PRECONDITION) { | |
| 55 MojoClose(watcher); | |
| 56 DCHECK_EQ(1u, num_ready_contexts); | |
| 57 if (state) | |
| 58 *state = ready_state; | |
| 59 return ready_result; | |
| 60 } | |
| 61 | |
| 62 // Wait for the first notification. | |
| 63 context_->event().Wait(); | |
| 64 | |
| 65 ready_result = context_->wait_result(); | |
| 66 DCHECK_NE(MOJO_RESULT_UNKNOWN, ready_result); | |
| 67 | |
| 68 if (state) | |
| 69 *state = context_->wait_state(); | |
| 70 | |
| 71 MojoClose(watcher); | |
| 72 | |
| 73 return ready_result; | |
| 74 } | |
| 75 | |
| 76 private: | |
| 77 class Context : public base::RefCountedThreadSafe<Context> { | |
| 78 public: | |
| 79 Context() | |
| 80 : event_(base::WaitableEvent::ResetPolicy::AUTOMATIC, | |
| 81 base::WaitableEvent::InitialState::NOT_SIGNALED) {} | |
| 82 | |
| 83 base::WaitableEvent& event() { return event_; } | |
| 84 MojoResult wait_result() const { return wait_result_; } | |
| 85 MojoHandleSignalsState wait_state() const { return wait_state_; } | |
| 86 | |
| 87 static void OnNotification(uintptr_t context, | |
| 88 MojoResult result, | |
| 89 MojoHandleSignalsState state, | |
| 90 MojoWatcherNotificationFlags flags) { | |
| 91 auto* context_ref = reinterpret_cast<scoped_refptr<Context>*>(context); | |
| 92 (*context_ref)->Notify(result, state); | |
| 93 if (result == MOJO_RESULT_CANCELLED) | |
| 94 delete context_ref; | |
| 95 } | |
| 96 | |
| 97 private: | |
| 98 friend class base::RefCountedThreadSafe<Context>; | |
| 99 | |
| 100 ~Context() {} | |
| 101 | |
| 102 void Notify(MojoResult result, MojoHandleSignalsState state) { | |
| 103 if (wait_result_ == MOJO_RESULT_UNKNOWN) { | |
| 104 wait_result_ = result; | |
| 105 wait_state_ = state; | |
| 106 } | |
| 107 event_.Signal(); | |
| 108 } | |
| 109 | |
| 110 base::WaitableEvent event_; | |
| 111 MojoResult wait_result_ = MOJO_RESULT_UNKNOWN; | |
| 112 MojoHandleSignalsState wait_state_ = {0, 0}; | |
| 113 | |
| 114 DISALLOW_COPY_AND_ASSIGN(Context); | |
| 115 }; | |
| 116 | |
| 117 scoped_refptr<Context> context_; | |
| 118 | |
| 119 DISALLOW_COPY_AND_ASSIGN(Waiter); | |
| 120 }; | |
| 121 | |
| 122 } // namespace | |
| 25 | 123 |
| 26 #if defined(OS_MACOSX) && !defined(OS_IOS) | 124 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 27 namespace { | 125 namespace { |
| 28 base::MachPortBroker* g_mach_broker = nullptr; | 126 base::MachPortBroker* g_mach_broker = nullptr; |
| 29 } | 127 } |
| 30 #endif | 128 #endif |
| 31 | 129 |
| 32 MojoTestBase::MojoTestBase() { | 130 MojoTestBase::MojoTestBase() { |
| 33 #if defined(OS_MACOSX) && !defined(OS_IOS) | 131 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 34 if (!g_mach_broker) { | 132 if (!g_mach_broker) { |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 123 // static | 221 // static |
| 124 void MojoTestBase::WriteMessage(MojoHandle mp, const std::string& message) { | 222 void MojoTestBase::WriteMessage(MojoHandle mp, const std::string& message) { |
| 125 WriteMessageWithHandles(mp, message, nullptr, 0); | 223 WriteMessageWithHandles(mp, message, nullptr, 0); |
| 126 } | 224 } |
| 127 | 225 |
| 128 // static | 226 // static |
| 129 std::string MojoTestBase::ReadMessageWithHandles( | 227 std::string MojoTestBase::ReadMessageWithHandles( |
| 130 MojoHandle mp, | 228 MojoHandle mp, |
| 131 MojoHandle* handles, | 229 MojoHandle* handles, |
| 132 uint32_t expected_num_handles) { | 230 uint32_t expected_num_handles) { |
| 133 CHECK_EQ(MojoWait(mp, MOJO_HANDLE_SIGNAL_READABLE, MOJO_DEADLINE_INDEFINITE, | 231 CHECK_EQ(WaitForSignals(mp, MOJO_HANDLE_SIGNAL_READABLE), MOJO_RESULT_OK); |
| 134 nullptr), | |
| 135 MOJO_RESULT_OK); | |
| 136 | 232 |
| 137 uint32_t message_size = 0; | 233 uint32_t message_size = 0; |
| 138 uint32_t num_handles = 0; | 234 uint32_t num_handles = 0; |
| 139 CHECK_EQ(MojoReadMessage(mp, nullptr, &message_size, nullptr, &num_handles, | 235 CHECK_EQ(MojoReadMessage(mp, nullptr, &message_size, nullptr, &num_handles, |
| 140 MOJO_READ_MESSAGE_FLAG_NONE), | 236 MOJO_READ_MESSAGE_FLAG_NONE), |
| 141 MOJO_RESULT_RESOURCE_EXHAUSTED); | 237 MOJO_RESULT_RESOURCE_EXHAUSTED); |
| 142 CHECK_EQ(expected_num_handles, num_handles); | 238 CHECK_EQ(expected_num_handles, num_handles); |
| 143 | 239 |
| 144 std::string message(message_size, 'x'); | 240 std::string message(message_size, 'x'); |
| 145 CHECK_EQ(MojoReadMessage(mp, &message[0], &message_size, handles, | 241 CHECK_EQ(MojoReadMessage(mp, &message[0], &message_size, handles, |
| 146 &num_handles, MOJO_READ_MESSAGE_FLAG_NONE), | 242 &num_handles, MOJO_READ_MESSAGE_FLAG_NONE), |
| 147 MOJO_RESULT_OK); | 243 MOJO_RESULT_OK); |
| 148 CHECK_EQ(message_size, message.size()); | 244 CHECK_EQ(message_size, message.size()); |
| 149 CHECK_EQ(num_handles, expected_num_handles); | 245 CHECK_EQ(num_handles, expected_num_handles); |
| 150 | 246 |
| 151 return message; | 247 return message; |
| 152 } | 248 } |
| 153 | 249 |
| 154 // static | 250 // static |
| 155 std::string MojoTestBase::ReadMessageWithOptionalHandle(MojoHandle mp, | 251 std::string MojoTestBase::ReadMessageWithOptionalHandle(MojoHandle mp, |
| 156 MojoHandle* handle) { | 252 MojoHandle* handle) { |
| 157 CHECK_EQ(MojoWait(mp, MOJO_HANDLE_SIGNAL_READABLE, MOJO_DEADLINE_INDEFINITE, | 253 CHECK_EQ(WaitForSignals(mp, MOJO_HANDLE_SIGNAL_READABLE), MOJO_RESULT_OK); |
| 158 nullptr), | |
| 159 MOJO_RESULT_OK); | |
| 160 | 254 |
| 161 uint32_t message_size = 0; | 255 uint32_t message_size = 0; |
| 162 uint32_t num_handles = 0; | 256 uint32_t num_handles = 0; |
| 163 CHECK_EQ(MojoReadMessage(mp, nullptr, &message_size, nullptr, &num_handles, | 257 CHECK_EQ(MojoReadMessage(mp, nullptr, &message_size, nullptr, &num_handles, |
| 164 MOJO_READ_MESSAGE_FLAG_NONE), | 258 MOJO_READ_MESSAGE_FLAG_NONE), |
| 165 MOJO_RESULT_RESOURCE_EXHAUSTED); | 259 MOJO_RESULT_RESOURCE_EXHAUSTED); |
| 166 CHECK(num_handles == 0 || num_handles == 1); | 260 CHECK(num_handles == 0 || num_handles == 1); |
| 167 | 261 |
| 168 CHECK(handle); | 262 CHECK(handle); |
| 169 | 263 |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 184 | 278 |
| 185 // static | 279 // static |
| 186 std::string MojoTestBase::ReadMessage(MojoHandle mp) { | 280 std::string MojoTestBase::ReadMessage(MojoHandle mp) { |
| 187 return ReadMessageWithHandles(mp, nullptr, 0); | 281 return ReadMessageWithHandles(mp, nullptr, 0); |
| 188 } | 282 } |
| 189 | 283 |
| 190 // static | 284 // static |
| 191 void MojoTestBase::ReadMessage(MojoHandle mp, | 285 void MojoTestBase::ReadMessage(MojoHandle mp, |
| 192 char* data, | 286 char* data, |
| 193 size_t num_bytes) { | 287 size_t num_bytes) { |
| 194 CHECK_EQ(MojoWait(mp, MOJO_HANDLE_SIGNAL_READABLE, MOJO_DEADLINE_INDEFINITE, | 288 CHECK_EQ(WaitForSignals(mp, MOJO_HANDLE_SIGNAL_READABLE), MOJO_RESULT_OK); |
| 195 nullptr), | |
| 196 MOJO_RESULT_OK); | |
| 197 | 289 |
| 198 uint32_t message_size = 0; | 290 uint32_t message_size = 0; |
| 199 uint32_t num_handles = 0; | 291 uint32_t num_handles = 0; |
| 200 CHECK_EQ(MojoReadMessage(mp, nullptr, &message_size, nullptr, &num_handles, | 292 CHECK_EQ(MojoReadMessage(mp, nullptr, &message_size, nullptr, &num_handles, |
| 201 MOJO_READ_MESSAGE_FLAG_NONE), | 293 MOJO_READ_MESSAGE_FLAG_NONE), |
| 202 MOJO_RESULT_RESOURCE_EXHAUSTED); | 294 MOJO_RESULT_RESOURCE_EXHAUSTED); |
| 203 CHECK_EQ(num_handles, 0u); | 295 CHECK_EQ(num_handles, 0u); |
| 204 CHECK_EQ(message_size, num_bytes); | 296 CHECK_EQ(message_size, num_bytes); |
| 205 | 297 |
| 206 CHECK_EQ(MojoReadMessage(mp, data, &message_size, nullptr, &num_handles, | 298 CHECK_EQ(MojoReadMessage(mp, data, &message_size, nullptr, &num_handles, |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 281 options.element_num_bytes = 1; | 373 options.element_num_bytes = 1; |
| 282 options.capacity_num_bytes = static_cast<uint32_t>(capacity); | 374 options.capacity_num_bytes = static_cast<uint32_t>(capacity); |
| 283 | 375 |
| 284 MojoCreateDataPipe(&options, p0, p1); | 376 MojoCreateDataPipe(&options, p0, p1); |
| 285 CHECK_NE(*p0, MOJO_HANDLE_INVALID); | 377 CHECK_NE(*p0, MOJO_HANDLE_INVALID); |
| 286 CHECK_NE(*p1, MOJO_HANDLE_INVALID); | 378 CHECK_NE(*p1, MOJO_HANDLE_INVALID); |
| 287 } | 379 } |
| 288 | 380 |
| 289 // static | 381 // static |
| 290 void MojoTestBase::WriteData(MojoHandle producer, const std::string& data) { | 382 void MojoTestBase::WriteData(MojoHandle producer, const std::string& data) { |
| 291 CHECK_EQ(MojoWait(producer, MOJO_HANDLE_SIGNAL_WRITABLE, | 383 CHECK_EQ(WaitForSignals(producer, MOJO_HANDLE_SIGNAL_WRITABLE), |
| 292 MOJO_DEADLINE_INDEFINITE, nullptr), | |
| 293 MOJO_RESULT_OK); | 384 MOJO_RESULT_OK); |
| 294 uint32_t num_bytes = static_cast<uint32_t>(data.size()); | 385 uint32_t num_bytes = static_cast<uint32_t>(data.size()); |
| 295 CHECK_EQ(MojoWriteData(producer, data.data(), &num_bytes, | 386 CHECK_EQ(MojoWriteData(producer, data.data(), &num_bytes, |
| 296 MOJO_WRITE_DATA_FLAG_ALL_OR_NONE), | 387 MOJO_WRITE_DATA_FLAG_ALL_OR_NONE), |
| 297 MOJO_RESULT_OK); | 388 MOJO_RESULT_OK); |
| 298 CHECK_EQ(num_bytes, static_cast<uint32_t>(data.size())); | 389 CHECK_EQ(num_bytes, static_cast<uint32_t>(data.size())); |
| 299 } | 390 } |
| 300 | 391 |
| 301 // static | 392 // static |
| 302 std::string MojoTestBase::ReadData(MojoHandle consumer, size_t size) { | 393 std::string MojoTestBase::ReadData(MojoHandle consumer, size_t size) { |
| 303 CHECK_EQ(MojoWait(consumer, MOJO_HANDLE_SIGNAL_READABLE, | 394 CHECK_EQ(WaitForSignals(consumer, MOJO_HANDLE_SIGNAL_READABLE), |
| 304 MOJO_DEADLINE_INDEFINITE, nullptr), | |
| 305 MOJO_RESULT_OK); | 395 MOJO_RESULT_OK); |
| 306 std::vector<char> buffer(size); | 396 std::vector<char> buffer(size); |
| 307 uint32_t num_bytes = static_cast<uint32_t>(size); | 397 uint32_t num_bytes = static_cast<uint32_t>(size); |
| 308 CHECK_EQ(MojoReadData(consumer, buffer.data(), &num_bytes, | 398 CHECK_EQ(MojoReadData(consumer, buffer.data(), &num_bytes, |
| 309 MOJO_WRITE_DATA_FLAG_ALL_OR_NONE), | 399 MOJO_WRITE_DATA_FLAG_ALL_OR_NONE), |
| 310 MOJO_RESULT_OK); | 400 MOJO_RESULT_OK); |
| 311 CHECK_EQ(num_bytes, static_cast<uint32_t>(size)); | 401 CHECK_EQ(num_bytes, static_cast<uint32_t>(size)); |
| 312 | 402 |
| 313 return std::string(buffer.data(), buffer.size()); | 403 return std::string(buffer.data(), buffer.size()); |
| 314 } | 404 } |
| 315 | 405 |
| 406 // static | |
| 407 MojoHandleSignalsState MojoTestBase::GetSignalsState(MojoHandle handle) { | |
| 408 MojoHandleSignalsState signals_state; | |
| 409 CHECK_EQ(MOJO_RESULT_OK, MojoQueryHandleSignalsState(handle, &signals_state)); | |
| 410 return signals_state; | |
| 411 } | |
| 412 | |
| 413 // static | |
| 414 MojoResult MojoTestBase::WaitForSignals(MojoHandle handle, | |
| 415 MojoHandleSignals signals, | |
| 416 MojoHandleSignalsState* state) { | |
| 417 Waiter waiter; | |
| 418 return waiter.Wait(handle, signals, state); | |
| 419 } | |
| 420 | |
| 316 } // namespace test | 421 } // namespace test |
| 317 } // namespace edk | 422 } // namespace edk |
| 318 } // namespace mojo | 423 } // namespace mojo |
| OLD | NEW |