OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "mojo/edk/system/raw_channel.h" | 5 #include "mojo/edk/system/raw_channel.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 #include <stdio.h> | 8 #include <stdio.h> |
9 | 9 |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
14 #include "base/files/file_util.h" | 14 #include "base/files/file_util.h" |
15 #include "base/files/scoped_file.h" | 15 #include "base/files/scoped_file.h" |
16 #include "base/files/scoped_temp_dir.h" | 16 #include "base/files/scoped_temp_dir.h" |
17 #include "base/location.h" | 17 #include "base/location.h" |
18 #include "base/logging.h" | 18 #include "base/logging.h" |
19 #include "base/memory/scoped_ptr.h" | 19 #include "base/memory/scoped_ptr.h" |
20 #include "base/memory/scoped_vector.h" | 20 #include "base/memory/scoped_vector.h" |
21 #include "base/rand_util.h" | 21 #include "base/rand_util.h" |
| 22 #include "base/synchronization/lock.h" |
22 #include "base/synchronization/waitable_event.h" | 23 #include "base/synchronization/waitable_event.h" |
23 #include "base/test/test_io_thread.h" | 24 #include "base/test/test_io_thread.h" |
24 #include "base/threading/simple_thread.h" | 25 #include "base/threading/simple_thread.h" |
25 #include "build/build_config.h" // TODO(vtl): Remove this. | 26 #include "build/build_config.h" // TODO(vtl): Remove this. |
26 #include "mojo/edk/embedder/platform_channel_pair.h" | 27 #include "mojo/edk/embedder/platform_channel_pair.h" |
27 #include "mojo/edk/embedder/platform_handle.h" | 28 #include "mojo/edk/embedder/platform_handle.h" |
28 #include "mojo/edk/embedder/scoped_platform_handle.h" | 29 #include "mojo/edk/embedder/scoped_platform_handle.h" |
29 #include "mojo/edk/system/message_in_transit.h" | 30 #include "mojo/edk/system/message_in_transit.h" |
30 #include "mojo/edk/system/mutex.h" | |
31 #include "mojo/edk/system/test_utils.h" | 31 #include "mojo/edk/system/test_utils.h" |
32 #include "mojo/edk/system/transport_data.h" | 32 #include "mojo/edk/system/transport_data.h" |
33 #include "mojo/edk/test/test_utils.h" | 33 #include "mojo/edk/test/test_utils.h" |
34 #include "mojo/public/cpp/system/macros.h" | 34 #include "mojo/public/cpp/system/macros.h" |
35 #include "testing/gtest/include/gtest/gtest.h" | 35 #include "testing/gtest/include/gtest/gtest.h" |
36 | 36 |
37 namespace mojo { | 37 namespace mojo { |
38 namespace system { | 38 namespace system { |
39 namespace { | 39 namespace { |
40 | 40 |
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
226 // |RawChannel::Delegate| implementation (called on the I/O thread): | 226 // |RawChannel::Delegate| implementation (called on the I/O thread): |
227 void OnReadMessage( | 227 void OnReadMessage( |
228 const MessageInTransit::View& message_view, | 228 const MessageInTransit::View& message_view, |
229 embedder::ScopedPlatformHandleVectorPtr platform_handles) override { | 229 embedder::ScopedPlatformHandleVectorPtr platform_handles) override { |
230 EXPECT_FALSE(platform_handles); | 230 EXPECT_FALSE(platform_handles); |
231 | 231 |
232 size_t position; | 232 size_t position; |
233 size_t expected_size; | 233 size_t expected_size; |
234 bool should_signal = false; | 234 bool should_signal = false; |
235 { | 235 { |
236 MutexLocker locker(&mutex_); | 236 base::AutoLock locker(lock_); |
237 CHECK_LT(position_, expected_sizes_.size()); | 237 CHECK_LT(position_, expected_sizes_.size()); |
238 position = position_; | 238 position = position_; |
239 expected_size = expected_sizes_[position]; | 239 expected_size = expected_sizes_[position]; |
240 position_++; | 240 position_++; |
241 if (position_ >= expected_sizes_.size()) | 241 if (position_ >= expected_sizes_.size()) |
242 should_signal = true; | 242 should_signal = true; |
243 } | 243 } |
244 | 244 |
245 EXPECT_EQ(expected_size, message_view.num_bytes()) << position; | 245 EXPECT_EQ(expected_size, message_view.num_bytes()) << position; |
246 if (message_view.num_bytes() == expected_size) { | 246 if (message_view.num_bytes() == expected_size) { |
247 EXPECT_TRUE( | 247 EXPECT_TRUE( |
248 CheckMessageData(message_view.bytes(), message_view.num_bytes())) | 248 CheckMessageData(message_view.bytes(), message_view.num_bytes())) |
249 << position; | 249 << position; |
250 } | 250 } |
251 | 251 |
252 if (should_signal) | 252 if (should_signal) |
253 done_event_.Signal(); | 253 done_event_.Signal(); |
254 } | 254 } |
255 void OnError(Error error) override { | 255 void OnError(Error error) override { |
256 // We'll get a read (shutdown) error when the connection is closed. | 256 // We'll get a read (shutdown) error when the connection is closed. |
257 CHECK_EQ(error, ERROR_READ_SHUTDOWN); | 257 CHECK_EQ(error, ERROR_READ_SHUTDOWN); |
258 } | 258 } |
259 | 259 |
260 // Waits for all the messages (of sizes |expected_sizes_|) to be seen. | 260 // Waits for all the messages (of sizes |expected_sizes_|) to be seen. |
261 void Wait() { done_event_.Wait(); } | 261 void Wait() { done_event_.Wait(); } |
262 | 262 |
263 void SetExpectedSizes(const std::vector<uint32_t>& expected_sizes) { | 263 void SetExpectedSizes(const std::vector<uint32_t>& expected_sizes) { |
264 MutexLocker locker(&mutex_); | 264 base::AutoLock locker(lock_); |
265 CHECK_EQ(position_, expected_sizes_.size()); | 265 CHECK_EQ(position_, expected_sizes_.size()); |
266 expected_sizes_ = expected_sizes; | 266 expected_sizes_ = expected_sizes; |
267 position_ = 0; | 267 position_ = 0; |
268 } | 268 } |
269 | 269 |
270 private: | 270 private: |
271 base::WaitableEvent done_event_; | 271 base::WaitableEvent done_event_; |
272 | 272 |
273 Mutex mutex_; | 273 base::Lock lock_; // Protects the following members. |
274 std::vector<uint32_t> expected_sizes_ MOJO_GUARDED_BY(mutex_); | 274 std::vector<uint32_t> expected_sizes_; |
275 size_t position_ MOJO_GUARDED_BY(mutex_); | 275 size_t position_; |
276 | 276 |
277 MOJO_DISALLOW_COPY_AND_ASSIGN(ReadCheckerRawChannelDelegate); | 277 MOJO_DISALLOW_COPY_AND_ASSIGN(ReadCheckerRawChannelDelegate); |
278 }; | 278 }; |
279 | 279 |
280 // Tests reading (writing using our own custom writer). | 280 // Tests reading (writing using our own custom writer). |
281 TEST_F(RawChannelTest, OnReadMessage) { | 281 TEST_F(RawChannelTest, OnReadMessage) { |
282 ReadCheckerRawChannelDelegate delegate; | 282 ReadCheckerRawChannelDelegate delegate; |
283 scoped_ptr<RawChannel> rc(RawChannel::Create(handles[0].Pass())); | 283 scoped_ptr<RawChannel> rc(RawChannel::Create(handles[0].Pass())); |
284 io_thread()->PostTaskAndWait( | 284 io_thread()->PostTaskAndWait( |
285 FROM_HERE, | 285 FROM_HERE, |
(...skipping 563 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
849 FROM_HERE, | 849 FROM_HERE, |
850 base::Bind(&RawChannel::Shutdown, base::Unretained(rc_read.get()))); | 850 base::Bind(&RawChannel::Shutdown, base::Unretained(rc_read.get()))); |
851 io_thread()->PostTaskAndWait( | 851 io_thread()->PostTaskAndWait( |
852 FROM_HERE, | 852 FROM_HERE, |
853 base::Bind(&RawChannel::Shutdown, base::Unretained(rc_write.get()))); | 853 base::Bind(&RawChannel::Shutdown, base::Unretained(rc_write.get()))); |
854 } | 854 } |
855 | 855 |
856 } // namespace | 856 } // namespace |
857 } // namespace system | 857 } // namespace system |
858 } // namespace mojo | 858 } // namespace mojo |
OLD | NEW |