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/system/core_test_base.h" 6 7 #include <vector> 8 9 #include "base/compiler_specific.h" 10 #include "base/logging.h" 11 #include "base/memory/ref_counted.h" 12 #include "mojo/embedder/simple_platform_support.h" 13 #include "mojo/system/constants.h" 14 #include "mojo/system/core.h" 15 #include "mojo/system/dispatcher.h" 16 #include "mojo/system/memory.h" 17 18 namespace mojo { 19 namespace system { 20 namespace test { 21 22 namespace { 23 24 // MockDispatcher -------------------------------------------------------------- 25 26 class MockDispatcher : public Dispatcher { 27 public: 28 explicit MockDispatcher(CoreTestBase::MockHandleInfo* info) : info_(info) { 29 CHECK(info_); 30 info_->IncrementCtorCallCount(); 31 } 32 33 // |Dispatcher| private methods: 34 virtual Type GetType() const OVERRIDE { return kTypeUnknown; } 35 36 private: 37 virtual ~MockDispatcher() { info_->IncrementDtorCallCount(); } 38 39 // |Dispatcher| protected methods: 40 virtual void CloseImplNoLock() OVERRIDE { 41 info_->IncrementCloseCallCount(); 42 lock().AssertAcquired(); 43 } 44 45 virtual MojoResult WriteMessageImplNoLock( 46 UserPointer<const void> bytes, 47 uint32_t num_bytes, 48 std::vector<DispatcherTransport>* transports, 49 MojoWriteMessageFlags /*flags*/) OVERRIDE { 50 info_->IncrementWriteMessageCallCount(); 51 lock().AssertAcquired(); 52 53 if (num_bytes > kMaxMessageNumBytes) 54 return MOJO_RESULT_RESOURCE_EXHAUSTED; 55 56 if (transports) 57 return MOJO_RESULT_UNIMPLEMENTED; 58 59 return MOJO_RESULT_OK; 60 } 61 62 virtual MojoResult ReadMessageImplNoLock( 63 UserPointer<void> bytes, 64 UserPointer<uint32_t> num_bytes, 65 DispatcherVector* dispatchers, 66 uint32_t* num_dispatchers, 67 MojoReadMessageFlags /*flags*/) OVERRIDE { 68 info_->IncrementReadMessageCallCount(); 69 lock().AssertAcquired(); 70 71 if (num_dispatchers) { 72 *num_dispatchers = 1; 73 if (dispatchers) { 74 // Okay to leave an invalid dispatcher. 75 dispatchers->resize(1); 76 } 77 } 78 79 return MOJO_RESULT_OK; 80 } 81 82 virtual MojoResult WriteDataImplNoLock( 83 UserPointer<const void> /*elements*/, 84 UserPointer<uint32_t> /*num_bytes*/, 85 MojoWriteDataFlags /*flags*/) OVERRIDE { 86 info_->IncrementWriteDataCallCount(); 87 lock().AssertAcquired(); 88 return MOJO_RESULT_UNIMPLEMENTED; 89 } 90 91 virtual MojoResult BeginWriteDataImplNoLock( 92 UserPointer<void*> /*buffer*/, 93 UserPointer<uint32_t> /*buffer_num_bytes*/, 94 MojoWriteDataFlags /*flags*/) OVERRIDE { 95 info_->IncrementBeginWriteDataCallCount(); 96 lock().AssertAcquired(); 97 return MOJO_RESULT_UNIMPLEMENTED; 98 } 99 100 virtual MojoResult EndWriteDataImplNoLock( 101 uint32_t /*num_bytes_written*/) OVERRIDE { 102 info_->IncrementEndWriteDataCallCount(); 103 lock().AssertAcquired(); 104 return MOJO_RESULT_UNIMPLEMENTED; 105 } 106 107 virtual MojoResult ReadDataImplNoLock(UserPointer<void> /*elements*/, 108 UserPointer<uint32_t> /*num_bytes*/, 109 MojoReadDataFlags /*flags*/) OVERRIDE { 110 info_->IncrementReadDataCallCount(); 111 lock().AssertAcquired(); 112 return MOJO_RESULT_UNIMPLEMENTED; 113 } 114 115 virtual MojoResult BeginReadDataImplNoLock( 116 UserPointer<const void*> /*buffer*/, 117 UserPointer<uint32_t> /*buffer_num_bytes*/, 118 MojoReadDataFlags /*flags*/) OVERRIDE { 119 info_->IncrementBeginReadDataCallCount(); 120 lock().AssertAcquired(); 121 return MOJO_RESULT_UNIMPLEMENTED; 122 } 123 124 virtual MojoResult EndReadDataImplNoLock( 125 uint32_t /*num_bytes_read*/) OVERRIDE { 126 info_->IncrementEndReadDataCallCount(); 127 lock().AssertAcquired(); 128 return MOJO_RESULT_UNIMPLEMENTED; 129 } 130 131 virtual MojoResult AddWaiterImplNoLock( 132 Waiter* /*waiter*/, 133 MojoHandleSignals /*signals*/, 134 uint32_t /*context*/, 135 HandleSignalsState* signals_state) OVERRIDE { 136 info_->IncrementAddWaiterCallCount(); 137 lock().AssertAcquired(); 138 if (signals_state) 139 *signals_state = HandleSignalsState(); 140 return MOJO_RESULT_FAILED_PRECONDITION; 141 } 142 143 virtual void RemoveWaiterImplNoLock( 144 Waiter* /*waiter*/, 145 HandleSignalsState* signals_state) OVERRIDE { 146 info_->IncrementRemoveWaiterCallCount(); 147 lock().AssertAcquired(); 148 if (signals_state) 149 *signals_state = HandleSignalsState(); 150 } 151 152 virtual void CancelAllWaitersNoLock() OVERRIDE { 153 info_->IncrementCancelAllWaitersCallCount(); 154 lock().AssertAcquired(); 155 } 156 157 virtual scoped_refptr<Dispatcher> 158 CreateEquivalentDispatcherAndCloseImplNoLock() OVERRIDE { 159 return scoped_refptr<Dispatcher>(new MockDispatcher(info_)); 160 } 161 162 CoreTestBase::MockHandleInfo* const info_; 163 164 DISALLOW_COPY_AND_ASSIGN(MockDispatcher); 165 }; 166 167 } // namespace 168 169 // CoreTestBase ---------------------------------------------------------------- 170 171 CoreTestBase::CoreTestBase() { 172 } 173 174 CoreTestBase::~CoreTestBase() { 175 } 176 177 void CoreTestBase::SetUp() { 178 core_ = new Core(scoped_ptr<embedder::PlatformSupport>( 179 new embedder::SimplePlatformSupport())); 180 } 181 182 void CoreTestBase::TearDown() { 183 delete core_; 184 core_ = nullptr; 185 } 186 187 MojoHandle CoreTestBase::CreateMockHandle(CoreTestBase::MockHandleInfo* info) { 188 CHECK(core_); 189 scoped_refptr<MockDispatcher> dispatcher(new MockDispatcher(info)); 190 return core_->AddDispatcher(dispatcher); 191 } 192 193 // CoreTestBase_MockHandleInfo ------------------------------------------------- 194 195 CoreTestBase_MockHandleInfo::CoreTestBase_MockHandleInfo() 196 : ctor_call_count_(0), 197 dtor_call_count_(0), 198 close_call_count_(0), 199 write_message_call_count_(0), 200 read_message_call_count_(0), 201 write_data_call_count_(0), 202 begin_write_data_call_count_(0), 203 end_write_data_call_count_(0), 204 read_data_call_count_(0), 205 begin_read_data_call_count_(0), 206 end_read_data_call_count_(0), 207 add_waiter_call_count_(0), 208 remove_waiter_call_count_(0), 209 cancel_all_waiters_call_count_(0) { 210 } 211 212 CoreTestBase_MockHandleInfo::~CoreTestBase_MockHandleInfo() { 213 } 214 215 unsigned CoreTestBase_MockHandleInfo::GetCtorCallCount() const { 216 base::AutoLock locker(lock_); 217 return ctor_call_count_; 218 } 219 220 unsigned CoreTestBase_MockHandleInfo::GetDtorCallCount() const { 221 base::AutoLock locker(lock_); 222 return dtor_call_count_; 223 } 224 225 unsigned CoreTestBase_MockHandleInfo::GetCloseCallCount() const { 226 base::AutoLock locker(lock_); 227 return close_call_count_; 228 } 229 230 unsigned CoreTestBase_MockHandleInfo::GetWriteMessageCallCount() const { 231 base::AutoLock locker(lock_); 232 return write_message_call_count_; 233 } 234 235 unsigned CoreTestBase_MockHandleInfo::GetReadMessageCallCount() const { 236 base::AutoLock locker(lock_); 237 return read_message_call_count_; 238 } 239 240 unsigned CoreTestBase_MockHandleInfo::GetWriteDataCallCount() const { 241 base::AutoLock locker(lock_); 242 return write_data_call_count_; 243 } 244 245 unsigned CoreTestBase_MockHandleInfo::GetBeginWriteDataCallCount() const { 246 base::AutoLock locker(lock_); 247 return begin_write_data_call_count_; 248 } 249 250 unsigned CoreTestBase_MockHandleInfo::GetEndWriteDataCallCount() const { 251 base::AutoLock locker(lock_); 252 return end_write_data_call_count_; 253 } 254 255 unsigned CoreTestBase_MockHandleInfo::GetReadDataCallCount() const { 256 base::AutoLock locker(lock_); 257 return read_data_call_count_; 258 } 259 260 unsigned CoreTestBase_MockHandleInfo::GetBeginReadDataCallCount() const { 261 base::AutoLock locker(lock_); 262 return begin_read_data_call_count_; 263 } 264 265 unsigned CoreTestBase_MockHandleInfo::GetEndReadDataCallCount() const { 266 base::AutoLock locker(lock_); 267 return end_read_data_call_count_; 268 } 269 270 unsigned CoreTestBase_MockHandleInfo::GetAddWaiterCallCount() const { 271 base::AutoLock locker(lock_); 272 return add_waiter_call_count_; 273 } 274 275 unsigned CoreTestBase_MockHandleInfo::GetRemoveWaiterCallCount() const { 276 base::AutoLock locker(lock_); 277 return remove_waiter_call_count_; 278 } 279 280 unsigned CoreTestBase_MockHandleInfo::GetCancelAllWaitersCallCount() const { 281 base::AutoLock locker(lock_); 282 return cancel_all_waiters_call_count_; 283 } 284 285 void CoreTestBase_MockHandleInfo::IncrementCtorCallCount() { 286 base::AutoLock locker(lock_); 287 ctor_call_count_++; 288 } 289 290 void CoreTestBase_MockHandleInfo::IncrementDtorCallCount() { 291 base::AutoLock locker(lock_); 292 dtor_call_count_++; 293 } 294 295 void CoreTestBase_MockHandleInfo::IncrementCloseCallCount() { 296 base::AutoLock locker(lock_); 297 close_call_count_++; 298 } 299 300 void CoreTestBase_MockHandleInfo::IncrementWriteMessageCallCount() { 301 base::AutoLock locker(lock_); 302 write_message_call_count_++; 303 } 304 305 void CoreTestBase_MockHandleInfo::IncrementReadMessageCallCount() { 306 base::AutoLock locker(lock_); 307 read_message_call_count_++; 308 } 309 310 void CoreTestBase_MockHandleInfo::IncrementWriteDataCallCount() { 311 base::AutoLock locker(lock_); 312 write_data_call_count_++; 313 } 314 315 void CoreTestBase_MockHandleInfo::IncrementBeginWriteDataCallCount() { 316 base::AutoLock locker(lock_); 317 begin_write_data_call_count_++; 318 } 319 320 void CoreTestBase_MockHandleInfo::IncrementEndWriteDataCallCount() { 321 base::AutoLock locker(lock_); 322 end_write_data_call_count_++; 323 } 324 325 void CoreTestBase_MockHandleInfo::IncrementReadDataCallCount() { 326 base::AutoLock locker(lock_); 327 read_data_call_count_++; 328 } 329 330 void CoreTestBase_MockHandleInfo::IncrementBeginReadDataCallCount() { 331 base::AutoLock locker(lock_); 332 begin_read_data_call_count_++; 333 } 334 335 void CoreTestBase_MockHandleInfo::IncrementEndReadDataCallCount() { 336 base::AutoLock locker(lock_); 337 end_read_data_call_count_++; 338 } 339 340 void CoreTestBase_MockHandleInfo::IncrementAddWaiterCallCount() { 341 base::AutoLock locker(lock_); 342 add_waiter_call_count_++; 343 } 344 345 void CoreTestBase_MockHandleInfo::IncrementRemoveWaiterCallCount() { 346 base::AutoLock locker(lock_); 347 remove_waiter_call_count_++; 348 } 349 350 void CoreTestBase_MockHandleInfo::IncrementCancelAllWaitersCallCount() { 351 base::AutoLock locker(lock_); 352 cancel_all_waiters_call_count_++; 353 } 354 355 } // namespace test 356 } // namespace system 357 } // namespace mojo 358