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