1 // Copyright 2013 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 "mojo/edk/system/core_test_base.h" 6 7 #include <stddef.h> 8 #include <stdint.h> 9 10 #include <vector> 11 12 #include "base/logging.h" 13 #include "base/macros.h" 14 #include "base/memory/ref_counted.h" 15 #include "mojo/edk/embedder/embedder_internal.h" 16 #include "mojo/edk/system/configuration.h" 17 #include "mojo/edk/system/core.h" 18 #include "mojo/edk/system/dispatcher.h" 19 #include "mojo/edk/system/message_for_transit.h" 20 21 namespace mojo { 22 namespace edk { 23 namespace test { 24 25 namespace { 26 27 // MockDispatcher -------------------------------------------------------------- 28 29 class MockDispatcher : public Dispatcher { 30 public: 31 static scoped_refptr<MockDispatcher> Create( 32 CoreTestBase::MockHandleInfo* info) { 33 return make_scoped_refptr(new MockDispatcher(info)); 34 } 35 36 // Dispatcher: 37 Type GetType() const override { return Type::UNKNOWN; } 38 39 MojoResult Close() override { 40 info_->IncrementCloseCallCount(); 41 return MOJO_RESULT_OK; 42 } 43 44 MojoResult WriteMessage( 45 std::unique_ptr<MessageForTransit> message, 46 MojoWriteMessageFlags /*flags*/) override { 47 info_->IncrementWriteMessageCallCount(); 48 49 if (message->num_bytes() > GetConfiguration().max_message_num_bytes) 50 return MOJO_RESULT_RESOURCE_EXHAUSTED; 51 52 if (message->num_handles()) 53 return MOJO_RESULT_UNIMPLEMENTED; 54 55 return MOJO_RESULT_OK; 56 } 57 58 MojoResult ReadMessage(std::unique_ptr<MessageForTransit>* message, 59 uint32_t* num_bytes, 60 MojoHandle* handle, 61 uint32_t* num_handles, 62 MojoReadMessageFlags /*flags*/, 63 bool ignore_num_bytes) override { 64 info_->IncrementReadMessageCallCount(); 65 66 if (num_handles) 67 *num_handles = 1; 68 69 return MOJO_RESULT_OK; 70 } 71 72 MojoResult WriteData(const void* elements, 73 uint32_t* num_bytes, 74 MojoWriteDataFlags flags) override { 75 info_->IncrementWriteDataCallCount(); 76 return MOJO_RESULT_UNIMPLEMENTED; 77 } 78 79 MojoResult BeginWriteData(void** buffer, 80 uint32_t* buffer_num_bytes, 81 MojoWriteDataFlags flags) override { 82 info_->IncrementBeginWriteDataCallCount(); 83 return MOJO_RESULT_UNIMPLEMENTED; 84 } 85 86 MojoResult EndWriteData(uint32_t num_bytes_written) override { 87 info_->IncrementEndWriteDataCallCount(); 88 return MOJO_RESULT_UNIMPLEMENTED; 89 } 90 91 MojoResult ReadData(void* elements, 92 uint32_t* num_bytes, 93 MojoReadDataFlags flags) override { 94 info_->IncrementReadDataCallCount(); 95 return MOJO_RESULT_UNIMPLEMENTED; 96 } 97 98 MojoResult BeginReadData(const void** buffer, 99 uint32_t* buffer_num_bytes, 100 MojoReadDataFlags flags) override { 101 info_->IncrementBeginReadDataCallCount(); 102 return MOJO_RESULT_UNIMPLEMENTED; 103 } 104 105 MojoResult EndReadData(uint32_t num_bytes_read) override { 106 info_->IncrementEndReadDataCallCount(); 107 return MOJO_RESULT_UNIMPLEMENTED; 108 } 109 110 MojoResult AddAwakable(Awakable* awakable, 111 MojoHandleSignals /*signals*/, 112 uintptr_t /*context*/, 113 HandleSignalsState* signals_state) override { 114 info_->IncrementAddAwakableCallCount(); 115 if (signals_state) 116 *signals_state = HandleSignalsState(); 117 if (info_->IsAddAwakableAllowed()) { 118 info_->AwakableWasAdded(awakable); 119 return MOJO_RESULT_OK; 120 } 121 122 return MOJO_RESULT_FAILED_PRECONDITION; 123 } 124 125 void RemoveAwakable(Awakable* /*awakable*/, 126 HandleSignalsState* signals_state) override { 127 info_->IncrementRemoveAwakableCallCount(); 128 if (signals_state) 129 *signals_state = HandleSignalsState(); 130 } 131 132 private: 133 explicit MockDispatcher(CoreTestBase::MockHandleInfo* info) : info_(info) { 134 CHECK(info_); 135 info_->IncrementCtorCallCount(); 136 } 137 138 ~MockDispatcher() override { info_->IncrementDtorCallCount(); } 139 140 CoreTestBase::MockHandleInfo* const info_; 141 142 DISALLOW_COPY_AND_ASSIGN(MockDispatcher); 143 }; 144 145 } // namespace 146 147 // CoreTestBase ---------------------------------------------------------------- 148 149 CoreTestBase::CoreTestBase() { 150 } 151 152 CoreTestBase::~CoreTestBase() { 153 } 154 155 MojoHandle CoreTestBase::CreateMockHandle(CoreTestBase::MockHandleInfo* info) { 156 scoped_refptr<MockDispatcher> dispatcher = MockDispatcher::Create(info); 157 return core()->AddDispatcher(dispatcher); 158 } 159 160 Core* CoreTestBase::core() { 161 return mojo::edk::internal::g_core; 162 } 163 164 // CoreTestBase_MockHandleInfo ------------------------------------------------- 165 166 CoreTestBase_MockHandleInfo::CoreTestBase_MockHandleInfo() 167 : ctor_call_count_(0), 168 dtor_call_count_(0), 169 close_call_count_(0), 170 write_message_call_count_(0), 171 read_message_call_count_(0), 172 write_data_call_count_(0), 173 begin_write_data_call_count_(0), 174 end_write_data_call_count_(0), 175 read_data_call_count_(0), 176 begin_read_data_call_count_(0), 177 end_read_data_call_count_(0), 178 add_awakable_call_count_(0), 179 remove_awakable_call_count_(0), 180 add_awakable_allowed_(false) { 181 } 182 183 CoreTestBase_MockHandleInfo::~CoreTestBase_MockHandleInfo() { 184 } 185 186 unsigned CoreTestBase_MockHandleInfo::GetCtorCallCount() const { 187 base::AutoLock locker(lock_); 188 return ctor_call_count_; 189 } 190 191 unsigned CoreTestBase_MockHandleInfo::GetDtorCallCount() const { 192 base::AutoLock locker(lock_); 193 return dtor_call_count_; 194 } 195 196 unsigned CoreTestBase_MockHandleInfo::GetCloseCallCount() const { 197 base::AutoLock locker(lock_); 198 return close_call_count_; 199 } 200 201 unsigned CoreTestBase_MockHandleInfo::GetWriteMessageCallCount() const { 202 base::AutoLock locker(lock_); 203 return write_message_call_count_; 204 } 205 206 unsigned CoreTestBase_MockHandleInfo::GetReadMessageCallCount() const { 207 base::AutoLock locker(lock_); 208 return read_message_call_count_; 209 } 210 211 unsigned CoreTestBase_MockHandleInfo::GetWriteDataCallCount() const { 212 base::AutoLock locker(lock_); 213 return write_data_call_count_; 214 } 215 216 unsigned CoreTestBase_MockHandleInfo::GetBeginWriteDataCallCount() const { 217 base::AutoLock locker(lock_); 218 return begin_write_data_call_count_; 219 } 220 221 unsigned CoreTestBase_MockHandleInfo::GetEndWriteDataCallCount() const { 222 base::AutoLock locker(lock_); 223 return end_write_data_call_count_; 224 } 225 226 unsigned CoreTestBase_MockHandleInfo::GetReadDataCallCount() const { 227 base::AutoLock locker(lock_); 228 return read_data_call_count_; 229 } 230 231 unsigned CoreTestBase_MockHandleInfo::GetBeginReadDataCallCount() const { 232 base::AutoLock locker(lock_); 233 return begin_read_data_call_count_; 234 } 235 236 unsigned CoreTestBase_MockHandleInfo::GetEndReadDataCallCount() const { 237 base::AutoLock locker(lock_); 238 return end_read_data_call_count_; 239 } 240 241 unsigned CoreTestBase_MockHandleInfo::GetAddAwakableCallCount() const { 242 base::AutoLock locker(lock_); 243 return add_awakable_call_count_; 244 } 245 246 unsigned CoreTestBase_MockHandleInfo::GetRemoveAwakableCallCount() const { 247 base::AutoLock locker(lock_); 248 return remove_awakable_call_count_; 249 } 250 251 size_t CoreTestBase_MockHandleInfo::GetAddedAwakableSize() const { 252 base::AutoLock locker(lock_); 253 return added_awakables_.size(); 254 } 255 256 Awakable* CoreTestBase_MockHandleInfo::GetAddedAwakableAt(unsigned i) const { 257 base::AutoLock locker(lock_); 258 return added_awakables_[i]; 259 } 260 261 void CoreTestBase_MockHandleInfo::IncrementCtorCallCount() { 262 base::AutoLock locker(lock_); 263 ctor_call_count_++; 264 } 265 266 void CoreTestBase_MockHandleInfo::IncrementDtorCallCount() { 267 base::AutoLock locker(lock_); 268 dtor_call_count_++; 269 } 270 271 void CoreTestBase_MockHandleInfo::IncrementCloseCallCount() { 272 base::AutoLock locker(lock_); 273 close_call_count_++; 274 } 275 276 void CoreTestBase_MockHandleInfo::IncrementWriteMessageCallCount() { 277 base::AutoLock locker(lock_); 278 write_message_call_count_++; 279 } 280 281 void CoreTestBase_MockHandleInfo::IncrementReadMessageCallCount() { 282 base::AutoLock locker(lock_); 283 read_message_call_count_++; 284 } 285 286 void CoreTestBase_MockHandleInfo::IncrementWriteDataCallCount() { 287 base::AutoLock locker(lock_); 288 write_data_call_count_++; 289 } 290 291 void CoreTestBase_MockHandleInfo::IncrementBeginWriteDataCallCount() { 292 base::AutoLock locker(lock_); 293 begin_write_data_call_count_++; 294 } 295 296 void CoreTestBase_MockHandleInfo::IncrementEndWriteDataCallCount() { 297 base::AutoLock locker(lock_); 298 end_write_data_call_count_++; 299 } 300 301 void CoreTestBase_MockHandleInfo::IncrementReadDataCallCount() { 302 base::AutoLock locker(lock_); 303 read_data_call_count_++; 304 } 305 306 void CoreTestBase_MockHandleInfo::IncrementBeginReadDataCallCount() { 307 base::AutoLock locker(lock_); 308 begin_read_data_call_count_++; 309 } 310 311 void CoreTestBase_MockHandleInfo::IncrementEndReadDataCallCount() { 312 base::AutoLock locker(lock_); 313 end_read_data_call_count_++; 314 } 315 316 void CoreTestBase_MockHandleInfo::IncrementAddAwakableCallCount() { 317 base::AutoLock locker(lock_); 318 add_awakable_call_count_++; 319 } 320 321 void CoreTestBase_MockHandleInfo::IncrementRemoveAwakableCallCount() { 322 base::AutoLock locker(lock_); 323 remove_awakable_call_count_++; 324 } 325 326 void CoreTestBase_MockHandleInfo::AllowAddAwakable(bool alllow) { 327 base::AutoLock locker(lock_); 328 add_awakable_allowed_ = alllow; 329 } 330 331 bool CoreTestBase_MockHandleInfo::IsAddAwakableAllowed() const { 332 base::AutoLock locker(lock_); 333 return add_awakable_allowed_; 334 } 335 336 void CoreTestBase_MockHandleInfo::AwakableWasAdded(Awakable* awakable) { 337 base::AutoLock locker(lock_); 338 added_awakables_.push_back(awakable); 339 } 340 341 } // namespace test 342 } // namespace edk 343 } // namespace mojo 344