OLD | NEW |
(Empty) | |
| 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/system/core_impl.h" |
| 13 #include "mojo/system/dispatcher.h" |
| 14 #include "mojo/system/memory.h" |
| 15 |
| 16 namespace mojo { |
| 17 namespace system { |
| 18 namespace test { |
| 19 |
| 20 namespace { |
| 21 |
| 22 // MockDispatcher -------------------------------------------------------------- |
| 23 |
| 24 class MockDispatcher : public Dispatcher { |
| 25 public: |
| 26 explicit MockDispatcher(CoreTestBase::MockHandleInfo* info) |
| 27 : info_(info) { |
| 28 CHECK(info_); |
| 29 info_->IncrementCtorCallCount(); |
| 30 } |
| 31 |
| 32 private: |
| 33 friend class base::RefCountedThreadSafe<MockDispatcher>; |
| 34 virtual ~MockDispatcher() { |
| 35 info_->IncrementDtorCallCount(); |
| 36 } |
| 37 |
| 38 // |Dispatcher| implementation/overrides: |
| 39 virtual MojoResult CloseImplNoLock() OVERRIDE { |
| 40 info_->IncrementCloseCallCount(); |
| 41 lock().AssertAcquired(); |
| 42 return MOJO_RESULT_OK; |
| 43 } |
| 44 |
| 45 virtual MojoResult WriteMessageImplNoLock( |
| 46 const void* bytes, |
| 47 uint32_t num_bytes, |
| 48 const MojoHandle* handles, |
| 49 uint32_t num_handles, |
| 50 MojoWriteMessageFlags /*flags*/) OVERRIDE { |
| 51 info_->IncrementWriteMessageCallCount(); |
| 52 lock().AssertAcquired(); |
| 53 |
| 54 if (!VerifyUserPointer(bytes, num_bytes, 1)) |
| 55 return MOJO_RESULT_INVALID_ARGUMENT; |
| 56 if (!VerifyUserPointer(handles, num_handles, sizeof(handles[0]))) |
| 57 return MOJO_RESULT_INVALID_ARGUMENT; |
| 58 |
| 59 return MOJO_RESULT_OK; |
| 60 } |
| 61 |
| 62 virtual MojoResult ReadMessageImplNoLock( |
| 63 void* bytes, |
| 64 uint32_t* num_bytes, |
| 65 MojoHandle* handles, |
| 66 uint32_t* num_handles, |
| 67 MojoReadMessageFlags /*flags*/) OVERRIDE { |
| 68 info_->IncrementReadMessageCallCount(); |
| 69 lock().AssertAcquired(); |
| 70 |
| 71 if (num_bytes && !VerifyUserPointer(bytes, *num_bytes, 1)) |
| 72 return MOJO_RESULT_INVALID_ARGUMENT; |
| 73 if (num_handles && |
| 74 !VerifyUserPointer(handles, *num_handles, sizeof(handles[0]))) |
| 75 return MOJO_RESULT_INVALID_ARGUMENT; |
| 76 |
| 77 return MOJO_RESULT_OK; |
| 78 } |
| 79 |
| 80 virtual MojoResult AddWaiterImplNoLock(Waiter* /*waiter*/, |
| 81 MojoWaitFlags /*flags*/, |
| 82 MojoResult /*wake_result*/) OVERRIDE { |
| 83 info_->IncrementAddWaiterCallCount(); |
| 84 lock().AssertAcquired(); |
| 85 return MOJO_RESULT_FAILED_PRECONDITION; |
| 86 } |
| 87 |
| 88 virtual void RemoveWaiterImplNoLock(Waiter* /*waiter*/) OVERRIDE { |
| 89 info_->IncrementRemoveWaiterCallCount(); |
| 90 lock().AssertAcquired(); |
| 91 } |
| 92 |
| 93 virtual void CancelAllWaitersNoLock() OVERRIDE { |
| 94 info_->IncrementCancelAllWaitersCallCount(); |
| 95 lock().AssertAcquired(); |
| 96 } |
| 97 |
| 98 CoreTestBase::MockHandleInfo* const info_; |
| 99 |
| 100 DISALLOW_COPY_AND_ASSIGN(MockDispatcher); |
| 101 }; |
| 102 |
| 103 } // namespace |
| 104 |
| 105 // CoreTestBase ---------------------------------------------------------------- |
| 106 |
| 107 CoreTestBase::CoreTestBase() { |
| 108 } |
| 109 |
| 110 CoreTestBase::~CoreTestBase() { |
| 111 } |
| 112 |
| 113 void CoreTestBase::SetUp() { |
| 114 core_ = new CoreImpl(); |
| 115 } |
| 116 |
| 117 void CoreTestBase::TearDown() { |
| 118 delete core_; |
| 119 core_ = NULL; |
| 120 } |
| 121 |
| 122 MojoHandle CoreTestBase::CreateMockHandle(CoreTestBase::MockHandleInfo* info) { |
| 123 CHECK(core_); |
| 124 scoped_refptr<MockDispatcher> dispatcher(new MockDispatcher(info)); |
| 125 base::AutoLock locker(core_->handle_table_lock_); |
| 126 return core_->AddDispatcherNoLock(dispatcher); |
| 127 } |
| 128 |
| 129 // CoreTestBase_MockHandleInfo ------------------------------------------------- |
| 130 |
| 131 CoreTestBase_MockHandleInfo::CoreTestBase_MockHandleInfo() |
| 132 : ctor_call_count_(0), |
| 133 dtor_call_count_(0), |
| 134 close_call_count_(0), |
| 135 write_message_call_count_(0), |
| 136 read_message_call_count_(0), |
| 137 add_waiter_call_count_(0), |
| 138 remove_waiter_call_count_(0), |
| 139 cancel_all_waiters_call_count_(0) { |
| 140 } |
| 141 |
| 142 CoreTestBase_MockHandleInfo::~CoreTestBase_MockHandleInfo() { |
| 143 } |
| 144 |
| 145 unsigned CoreTestBase_MockHandleInfo::GetCtorCallCount() const { |
| 146 base::AutoLock locker(lock_); |
| 147 return ctor_call_count_; |
| 148 } |
| 149 |
| 150 unsigned CoreTestBase_MockHandleInfo::GetDtorCallCount() const { |
| 151 base::AutoLock locker(lock_); |
| 152 return dtor_call_count_; |
| 153 } |
| 154 |
| 155 unsigned CoreTestBase_MockHandleInfo::GetCloseCallCount() const { |
| 156 base::AutoLock locker(lock_); |
| 157 return close_call_count_; |
| 158 } |
| 159 |
| 160 unsigned CoreTestBase_MockHandleInfo::GetWriteMessageCallCount() const { |
| 161 base::AutoLock locker(lock_); |
| 162 return write_message_call_count_; |
| 163 } |
| 164 |
| 165 unsigned CoreTestBase_MockHandleInfo::GetReadMessageCallCount() const { |
| 166 base::AutoLock locker(lock_); |
| 167 return read_message_call_count_; |
| 168 } |
| 169 |
| 170 unsigned CoreTestBase_MockHandleInfo::GetAddWaiterCallCount() const { |
| 171 base::AutoLock locker(lock_); |
| 172 return add_waiter_call_count_; |
| 173 } |
| 174 |
| 175 unsigned CoreTestBase_MockHandleInfo::GetRemoveWaiterCallCount() const { |
| 176 base::AutoLock locker(lock_); |
| 177 return remove_waiter_call_count_; |
| 178 } |
| 179 |
| 180 unsigned CoreTestBase_MockHandleInfo::GetCancelAllWaitersCallCount() const { |
| 181 base::AutoLock locker(lock_); |
| 182 return cancel_all_waiters_call_count_; |
| 183 } |
| 184 |
| 185 void CoreTestBase_MockHandleInfo::IncrementCtorCallCount() { |
| 186 base::AutoLock locker(lock_); |
| 187 ctor_call_count_++; |
| 188 } |
| 189 |
| 190 void CoreTestBase_MockHandleInfo::IncrementDtorCallCount() { |
| 191 base::AutoLock locker(lock_); |
| 192 dtor_call_count_++; |
| 193 } |
| 194 |
| 195 void CoreTestBase_MockHandleInfo::IncrementCloseCallCount() { |
| 196 base::AutoLock locker(lock_); |
| 197 close_call_count_++; |
| 198 } |
| 199 |
| 200 void CoreTestBase_MockHandleInfo::IncrementWriteMessageCallCount() { |
| 201 base::AutoLock locker(lock_); |
| 202 write_message_call_count_++; |
| 203 } |
| 204 |
| 205 void CoreTestBase_MockHandleInfo::IncrementReadMessageCallCount() { |
| 206 base::AutoLock locker(lock_); |
| 207 read_message_call_count_++; |
| 208 } |
| 209 |
| 210 void CoreTestBase_MockHandleInfo::IncrementAddWaiterCallCount() { |
| 211 base::AutoLock locker(lock_); |
| 212 add_waiter_call_count_++; |
| 213 } |
| 214 |
| 215 void CoreTestBase_MockHandleInfo::IncrementRemoveWaiterCallCount() { |
| 216 base::AutoLock locker(lock_); |
| 217 remove_waiter_call_count_++; |
| 218 } |
| 219 |
| 220 void CoreTestBase_MockHandleInfo::IncrementCancelAllWaitersCallCount() { |
| 221 base::AutoLock locker(lock_); |
| 222 cancel_all_waiters_call_count_++; |
| 223 } |
| 224 |
| 225 } // namespace test |
| 226 } // namespace system |
| 227 } // namespace mojo |
OLD | NEW |