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/edk/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/edk/embedder/simple_platform_support.h" | |
13 #include "mojo/edk/system/configuration.h" | |
14 #include "mojo/edk/system/core.h" | |
15 #include "mojo/edk/system/dispatcher.h" | |
16 #include "mojo/edk/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 Type GetType() const override { return kTypeUnknown; } | |
35 | |
36 private: | |
37 ~MockDispatcher() override { info_->IncrementDtorCallCount(); } | |
38 | |
39 // |Dispatcher| protected methods: | |
40 void CloseImplNoLock() override { | |
41 info_->IncrementCloseCallCount(); | |
42 lock().AssertAcquired(); | |
43 } | |
44 | |
45 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 > GetConfiguration().max_message_num_bytes) | |
54 return MOJO_RESULT_RESOURCE_EXHAUSTED; | |
55 | |
56 if (transports) | |
57 return MOJO_RESULT_UNIMPLEMENTED; | |
58 | |
59 return MOJO_RESULT_OK; | |
60 } | |
61 | |
62 MojoResult ReadMessageImplNoLock(UserPointer<void> bytes, | |
63 UserPointer<uint32_t> num_bytes, | |
64 DispatcherVector* dispatchers, | |
65 uint32_t* num_dispatchers, | |
66 MojoReadMessageFlags /*flags*/) override { | |
67 info_->IncrementReadMessageCallCount(); | |
68 lock().AssertAcquired(); | |
69 | |
70 if (num_dispatchers) { | |
71 *num_dispatchers = 1; | |
72 if (dispatchers) { | |
73 // Okay to leave an invalid dispatcher. | |
74 dispatchers->resize(1); | |
75 } | |
76 } | |
77 | |
78 return MOJO_RESULT_OK; | |
79 } | |
80 | |
81 MojoResult WriteDataImplNoLock(UserPointer<const void> /*elements*/, | |
82 UserPointer<uint32_t> /*num_bytes*/, | |
83 MojoWriteDataFlags /*flags*/) override { | |
84 info_->IncrementWriteDataCallCount(); | |
85 lock().AssertAcquired(); | |
86 return MOJO_RESULT_UNIMPLEMENTED; | |
87 } | |
88 | |
89 MojoResult BeginWriteDataImplNoLock( | |
90 UserPointer<void*> /*buffer*/, | |
91 UserPointer<uint32_t> /*buffer_num_bytes*/, | |
92 MojoWriteDataFlags /*flags*/) override { | |
93 info_->IncrementBeginWriteDataCallCount(); | |
94 lock().AssertAcquired(); | |
95 return MOJO_RESULT_UNIMPLEMENTED; | |
96 } | |
97 | |
98 MojoResult EndWriteDataImplNoLock(uint32_t /*num_bytes_written*/) override { | |
99 info_->IncrementEndWriteDataCallCount(); | |
100 lock().AssertAcquired(); | |
101 return MOJO_RESULT_UNIMPLEMENTED; | |
102 } | |
103 | |
104 MojoResult ReadDataImplNoLock(UserPointer<void> /*elements*/, | |
105 UserPointer<uint32_t> /*num_bytes*/, | |
106 MojoReadDataFlags /*flags*/) override { | |
107 info_->IncrementReadDataCallCount(); | |
108 lock().AssertAcquired(); | |
109 return MOJO_RESULT_UNIMPLEMENTED; | |
110 } | |
111 | |
112 MojoResult BeginReadDataImplNoLock(UserPointer<const void*> /*buffer*/, | |
113 UserPointer<uint32_t> /*buffer_num_bytes*/, | |
114 MojoReadDataFlags /*flags*/) override { | |
115 info_->IncrementBeginReadDataCallCount(); | |
116 lock().AssertAcquired(); | |
117 return MOJO_RESULT_UNIMPLEMENTED; | |
118 } | |
119 | |
120 MojoResult EndReadDataImplNoLock(uint32_t /*num_bytes_read*/) override { | |
121 info_->IncrementEndReadDataCallCount(); | |
122 lock().AssertAcquired(); | |
123 return MOJO_RESULT_UNIMPLEMENTED; | |
124 } | |
125 | |
126 MojoResult AddAwakableImplNoLock(Awakable* awakable, | |
127 MojoHandleSignals /*signals*/, | |
128 uint32_t /*context*/, | |
129 HandleSignalsState* signals_state) override { | |
130 info_->IncrementAddAwakableCallCount(); | |
131 lock().AssertAcquired(); | |
132 if (signals_state) | |
133 *signals_state = HandleSignalsState(); | |
134 if (info_->IsAddAwakableAllowed()) { | |
135 info_->AwakableWasAdded(awakable); | |
136 return MOJO_RESULT_OK; | |
137 } | |
138 | |
139 return MOJO_RESULT_FAILED_PRECONDITION; | |
140 } | |
141 | |
142 void RemoveAwakableImplNoLock(Awakable* /*awakable*/, | |
143 HandleSignalsState* signals_state) override { | |
144 info_->IncrementRemoveAwakableCallCount(); | |
145 lock().AssertAcquired(); | |
146 if (signals_state) | |
147 *signals_state = HandleSignalsState(); | |
148 } | |
149 | |
150 void CancelAllAwakablesNoLock() override { | |
151 info_->IncrementCancelAllAwakablesCallCount(); | |
152 lock().AssertAcquired(); | |
153 } | |
154 | |
155 scoped_refptr<Dispatcher> CreateEquivalentDispatcherAndCloseImplNoLock() | |
156 override { | |
157 return scoped_refptr<Dispatcher>(new MockDispatcher(info_)); | |
158 } | |
159 | |
160 CoreTestBase::MockHandleInfo* const info_; | |
161 | |
162 DISALLOW_COPY_AND_ASSIGN(MockDispatcher); | |
163 }; | |
164 | |
165 } // namespace | |
166 | |
167 // CoreTestBase ---------------------------------------------------------------- | |
168 | |
169 CoreTestBase::CoreTestBase() { | |
170 } | |
171 | |
172 CoreTestBase::~CoreTestBase() { | |
173 } | |
174 | |
175 void CoreTestBase::SetUp() { | |
176 core_ = new Core(make_scoped_ptr(new embedder::SimplePlatformSupport())); | |
177 } | |
178 | |
179 void CoreTestBase::TearDown() { | |
180 delete core_; | |
181 core_ = nullptr; | |
182 } | |
183 | |
184 MojoHandle CoreTestBase::CreateMockHandle(CoreTestBase::MockHandleInfo* info) { | |
185 CHECK(core_); | |
186 scoped_refptr<MockDispatcher> dispatcher(new MockDispatcher(info)); | |
187 return core_->AddDispatcher(dispatcher); | |
188 } | |
189 | |
190 // CoreTestBase_MockHandleInfo ------------------------------------------------- | |
191 | |
192 CoreTestBase_MockHandleInfo::CoreTestBase_MockHandleInfo() | |
193 : ctor_call_count_(0), | |
194 dtor_call_count_(0), | |
195 close_call_count_(0), | |
196 write_message_call_count_(0), | |
197 read_message_call_count_(0), | |
198 write_data_call_count_(0), | |
199 begin_write_data_call_count_(0), | |
200 end_write_data_call_count_(0), | |
201 read_data_call_count_(0), | |
202 begin_read_data_call_count_(0), | |
203 end_read_data_call_count_(0), | |
204 add_awakable_call_count_(0), | |
205 remove_awakable_call_count_(0), | |
206 cancel_all_awakables_call_count_(0), | |
207 add_awakable_allowed_(false) { | |
208 } | |
209 | |
210 CoreTestBase_MockHandleInfo::~CoreTestBase_MockHandleInfo() { | |
211 } | |
212 | |
213 unsigned CoreTestBase_MockHandleInfo::GetCtorCallCount() const { | |
214 base::AutoLock locker(lock_); | |
215 return ctor_call_count_; | |
216 } | |
217 | |
218 unsigned CoreTestBase_MockHandleInfo::GetDtorCallCount() const { | |
219 base::AutoLock locker(lock_); | |
220 return dtor_call_count_; | |
221 } | |
222 | |
223 unsigned CoreTestBase_MockHandleInfo::GetCloseCallCount() const { | |
224 base::AutoLock locker(lock_); | |
225 return close_call_count_; | |
226 } | |
227 | |
228 unsigned CoreTestBase_MockHandleInfo::GetWriteMessageCallCount() const { | |
229 base::AutoLock locker(lock_); | |
230 return write_message_call_count_; | |
231 } | |
232 | |
233 unsigned CoreTestBase_MockHandleInfo::GetReadMessageCallCount() const { | |
234 base::AutoLock locker(lock_); | |
235 return read_message_call_count_; | |
236 } | |
237 | |
238 unsigned CoreTestBase_MockHandleInfo::GetWriteDataCallCount() const { | |
239 base::AutoLock locker(lock_); | |
240 return write_data_call_count_; | |
241 } | |
242 | |
243 unsigned CoreTestBase_MockHandleInfo::GetBeginWriteDataCallCount() const { | |
244 base::AutoLock locker(lock_); | |
245 return begin_write_data_call_count_; | |
246 } | |
247 | |
248 unsigned CoreTestBase_MockHandleInfo::GetEndWriteDataCallCount() const { | |
249 base::AutoLock locker(lock_); | |
250 return end_write_data_call_count_; | |
251 } | |
252 | |
253 unsigned CoreTestBase_MockHandleInfo::GetReadDataCallCount() const { | |
254 base::AutoLock locker(lock_); | |
255 return read_data_call_count_; | |
256 } | |
257 | |
258 unsigned CoreTestBase_MockHandleInfo::GetBeginReadDataCallCount() const { | |
259 base::AutoLock locker(lock_); | |
260 return begin_read_data_call_count_; | |
261 } | |
262 | |
263 unsigned CoreTestBase_MockHandleInfo::GetEndReadDataCallCount() const { | |
264 base::AutoLock locker(lock_); | |
265 return end_read_data_call_count_; | |
266 } | |
267 | |
268 unsigned CoreTestBase_MockHandleInfo::GetAddAwakableCallCount() const { | |
269 base::AutoLock locker(lock_); | |
270 return add_awakable_call_count_; | |
271 } | |
272 | |
273 unsigned CoreTestBase_MockHandleInfo::GetRemoveAwakableCallCount() const { | |
274 base::AutoLock locker(lock_); | |
275 return remove_awakable_call_count_; | |
276 } | |
277 | |
278 unsigned CoreTestBase_MockHandleInfo::GetCancelAllAwakablesCallCount() const { | |
279 base::AutoLock locker(lock_); | |
280 return cancel_all_awakables_call_count_; | |
281 } | |
282 | |
283 size_t CoreTestBase_MockHandleInfo::GetAddedAwakableSize() const { | |
284 base::AutoLock locker(lock_); | |
285 return added_awakables_.size(); | |
286 } | |
287 | |
288 Awakable* CoreTestBase_MockHandleInfo::GetAddedAwakableAt(unsigned i) const { | |
289 base::AutoLock locker(lock_); | |
290 return added_awakables_[i]; | |
291 } | |
292 | |
293 void CoreTestBase_MockHandleInfo::IncrementCtorCallCount() { | |
294 base::AutoLock locker(lock_); | |
295 ctor_call_count_++; | |
296 } | |
297 | |
298 void CoreTestBase_MockHandleInfo::IncrementDtorCallCount() { | |
299 base::AutoLock locker(lock_); | |
300 dtor_call_count_++; | |
301 } | |
302 | |
303 void CoreTestBase_MockHandleInfo::IncrementCloseCallCount() { | |
304 base::AutoLock locker(lock_); | |
305 close_call_count_++; | |
306 } | |
307 | |
308 void CoreTestBase_MockHandleInfo::IncrementWriteMessageCallCount() { | |
309 base::AutoLock locker(lock_); | |
310 write_message_call_count_++; | |
311 } | |
312 | |
313 void CoreTestBase_MockHandleInfo::IncrementReadMessageCallCount() { | |
314 base::AutoLock locker(lock_); | |
315 read_message_call_count_++; | |
316 } | |
317 | |
318 void CoreTestBase_MockHandleInfo::IncrementWriteDataCallCount() { | |
319 base::AutoLock locker(lock_); | |
320 write_data_call_count_++; | |
321 } | |
322 | |
323 void CoreTestBase_MockHandleInfo::IncrementBeginWriteDataCallCount() { | |
324 base::AutoLock locker(lock_); | |
325 begin_write_data_call_count_++; | |
326 } | |
327 | |
328 void CoreTestBase_MockHandleInfo::IncrementEndWriteDataCallCount() { | |
329 base::AutoLock locker(lock_); | |
330 end_write_data_call_count_++; | |
331 } | |
332 | |
333 void CoreTestBase_MockHandleInfo::IncrementReadDataCallCount() { | |
334 base::AutoLock locker(lock_); | |
335 read_data_call_count_++; | |
336 } | |
337 | |
338 void CoreTestBase_MockHandleInfo::IncrementBeginReadDataCallCount() { | |
339 base::AutoLock locker(lock_); | |
340 begin_read_data_call_count_++; | |
341 } | |
342 | |
343 void CoreTestBase_MockHandleInfo::IncrementEndReadDataCallCount() { | |
344 base::AutoLock locker(lock_); | |
345 end_read_data_call_count_++; | |
346 } | |
347 | |
348 void CoreTestBase_MockHandleInfo::IncrementAddAwakableCallCount() { | |
349 base::AutoLock locker(lock_); | |
350 add_awakable_call_count_++; | |
351 } | |
352 | |
353 void CoreTestBase_MockHandleInfo::IncrementRemoveAwakableCallCount() { | |
354 base::AutoLock locker(lock_); | |
355 remove_awakable_call_count_++; | |
356 } | |
357 | |
358 void CoreTestBase_MockHandleInfo::IncrementCancelAllAwakablesCallCount() { | |
359 base::AutoLock locker(lock_); | |
360 cancel_all_awakables_call_count_++; | |
361 } | |
362 | |
363 void CoreTestBase_MockHandleInfo::AllowAddAwakable(bool alllow) { | |
364 base::AutoLock locker(lock_); | |
365 add_awakable_allowed_ = alllow; | |
366 } | |
367 | |
368 bool CoreTestBase_MockHandleInfo::IsAddAwakableAllowed() const { | |
369 base::AutoLock locker(lock_); | |
370 return add_awakable_allowed_; | |
371 } | |
372 | |
373 void CoreTestBase_MockHandleInfo::AwakableWasAdded(Awakable* awakable) { | |
374 base::AutoLock locker(lock_); | |
375 added_awakables_.push_back(awakable); | |
376 } | |
377 | |
378 } // namespace test | |
379 } // namespace system | |
380 } // namespace mojo | |
OLD | NEW |