| 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 | 8 | 
| 9 #include <vector> | 9 #include <vector> | 
| 10 | 10 | 
| (...skipping 23 matching lines...) Expand all  Loading... | 
| 34 namespace system { | 34 namespace system { | 
| 35 namespace { | 35 namespace { | 
| 36 | 36 | 
| 37 scoped_ptr<MessageInTransit> MakeTestMessage(uint32_t num_bytes) { | 37 scoped_ptr<MessageInTransit> MakeTestMessage(uint32_t num_bytes) { | 
| 38   std::vector<unsigned char> bytes(num_bytes, 0); | 38   std::vector<unsigned char> bytes(num_bytes, 0); | 
| 39   for (size_t i = 0; i < num_bytes; i++) | 39   for (size_t i = 0; i < num_bytes; i++) | 
| 40     bytes[i] = static_cast<unsigned char>(i + num_bytes); | 40     bytes[i] = static_cast<unsigned char>(i + num_bytes); | 
| 41   return make_scoped_ptr( | 41   return make_scoped_ptr( | 
| 42       new MessageInTransit(MessageInTransit::kTypeMessagePipeEndpoint, | 42       new MessageInTransit(MessageInTransit::kTypeMessagePipeEndpoint, | 
| 43                            MessageInTransit::kSubtypeMessagePipeEndpointData, | 43                            MessageInTransit::kSubtypeMessagePipeEndpointData, | 
| 44                            num_bytes, bytes.empty() ? nullptr : &bytes[0])); | 44                            num_bytes, | 
|  | 45                            bytes.empty() ? nullptr : &bytes[0])); | 
| 45 } | 46 } | 
| 46 | 47 | 
| 47 bool CheckMessageData(const void* bytes, uint32_t num_bytes) { | 48 bool CheckMessageData(const void* bytes, uint32_t num_bytes) { | 
| 48   const unsigned char* b = static_cast<const unsigned char*>(bytes); | 49   const unsigned char* b = static_cast<const unsigned char*>(bytes); | 
| 49   for (uint32_t i = 0; i < num_bytes; i++) { | 50   for (uint32_t i = 0; i < num_bytes; i++) { | 
| 50     if (b[i] != static_cast<unsigned char>(i + num_bytes)) | 51     if (b[i] != static_cast<unsigned char>(i + num_bytes)) | 
| 51       return false; | 52       return false; | 
| 52   } | 53   } | 
| 53   return true; | 54   return true; | 
| 54 } | 55 } | 
| 55 | 56 | 
| 56 void InitOnIOThread(RawChannel* raw_channel, RawChannel::Delegate* delegate) { | 57 void InitOnIOThread(RawChannel* raw_channel, RawChannel::Delegate* delegate) { | 
| 57   CHECK(raw_channel->Init(delegate)); | 58   CHECK(raw_channel->Init(delegate)); | 
| 58 } | 59 } | 
| 59 | 60 | 
| 60 bool WriteTestMessageToHandle(const embedder::PlatformHandle& handle, | 61 bool WriteTestMessageToHandle(const embedder::PlatformHandle& handle, | 
| 61                               uint32_t num_bytes) { | 62                               uint32_t num_bytes) { | 
| 62   scoped_ptr<MessageInTransit> message(MakeTestMessage(num_bytes)); | 63   scoped_ptr<MessageInTransit> message(MakeTestMessage(num_bytes)); | 
| 63 | 64 | 
| 64   size_t write_size = 0; | 65   size_t write_size = 0; | 
| 65   mojo::test::BlockingWrite(handle, message->main_buffer(), | 66   mojo::test::BlockingWrite( | 
| 66                             message->main_buffer_size(), &write_size); | 67       handle, message->main_buffer(), message->main_buffer_size(), &write_size); | 
| 67   return write_size == message->main_buffer_size(); | 68   return write_size == message->main_buffer_size(); | 
| 68 } | 69 } | 
| 69 | 70 | 
| 70 // ----------------------------------------------------------------------------- | 71 // ----------------------------------------------------------------------------- | 
| 71 | 72 | 
| 72 class RawChannelTest : public testing::Test { | 73 class RawChannelTest : public testing::Test { | 
| 73  public: | 74  public: | 
| 74   RawChannelTest() : io_thread_(base::TestIOThread::kManualStart) {} | 75   RawChannelTest() : io_thread_(base::TestIOThread::kManualStart) {} | 
| 75   ~RawChannelTest() override {} | 76   ~RawChannelTest() override {} | 
| 76 | 77 | 
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 127  public: | 128  public: | 
| 128   explicit TestMessageReaderAndChecker(embedder::PlatformHandle handle) | 129   explicit TestMessageReaderAndChecker(embedder::PlatformHandle handle) | 
| 129       : handle_(handle) {} | 130       : handle_(handle) {} | 
| 130   ~TestMessageReaderAndChecker() { CHECK(bytes_.empty()); } | 131   ~TestMessageReaderAndChecker() { CHECK(bytes_.empty()); } | 
| 131 | 132 | 
| 132   bool ReadAndCheckNextMessage(uint32_t expected_size) { | 133   bool ReadAndCheckNextMessage(uint32_t expected_size) { | 
| 133     unsigned char buffer[4096]; | 134     unsigned char buffer[4096]; | 
| 134 | 135 | 
| 135     for (size_t i = 0; i < kMessageReaderMaxPollIterations;) { | 136     for (size_t i = 0; i < kMessageReaderMaxPollIterations;) { | 
| 136       size_t read_size = 0; | 137       size_t read_size = 0; | 
| 137       CHECK(mojo::test::NonBlockingRead(handle_, buffer, sizeof(buffer), | 138       CHECK(mojo::test::NonBlockingRead( | 
| 138                                         &read_size)); | 139           handle_, buffer, sizeof(buffer), &read_size)); | 
| 139 | 140 | 
| 140       // Append newly-read data to |bytes_|. | 141       // Append newly-read data to |bytes_|. | 
| 141       bytes_.insert(bytes_.end(), buffer, buffer + read_size); | 142       bytes_.insert(bytes_.end(), buffer, buffer + read_size); | 
| 142 | 143 | 
| 143       // If we have the header.... | 144       // If we have the header.... | 
| 144       size_t message_size; | 145       size_t message_size; | 
| 145       if (MessageInTransit::GetNextMessageSize( | 146       if (MessageInTransit::GetNextMessageSize( | 
| 146               bytes_.empty() ? nullptr : &bytes_[0], bytes_.size(), | 147               bytes_.empty() ? nullptr : &bytes_[0], | 
|  | 148               bytes_.size(), | 
| 147               &message_size)) { | 149               &message_size)) { | 
| 148         // If we've read the whole message.... | 150         // If we've read the whole message.... | 
| 149         if (bytes_.size() >= message_size) { | 151         if (bytes_.size() >= message_size) { | 
| 150           bool rv = true; | 152           bool rv = true; | 
| 151           MessageInTransit::View message_view(message_size, &bytes_[0]); | 153           MessageInTransit::View message_view(message_size, &bytes_[0]); | 
| 152           CHECK_EQ(message_view.main_buffer_size(), message_size); | 154           CHECK_EQ(message_view.main_buffer_size(), message_size); | 
| 153 | 155 | 
| 154           if (message_view.num_bytes() != expected_size) { | 156           if (message_view.num_bytes() != expected_size) { | 
| 155             LOG(ERROR) << "Wrong size: " << message_size << " instead of " | 157             LOG(ERROR) << "Wrong size: " << message_size << " instead of " | 
| 156                        << expected_size << " bytes."; | 158                        << expected_size << " bytes."; | 
| (...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 369   DISALLOW_COPY_AND_ASSIGN(ReadCountdownRawChannelDelegate); | 371   DISALLOW_COPY_AND_ASSIGN(ReadCountdownRawChannelDelegate); | 
| 370 }; | 372 }; | 
| 371 | 373 | 
| 372 TEST_F(RawChannelTest, WriteMessageAndOnReadMessage) { | 374 TEST_F(RawChannelTest, WriteMessageAndOnReadMessage) { | 
| 373   static const size_t kNumWriterThreads = 10; | 375   static const size_t kNumWriterThreads = 10; | 
| 374   static const size_t kNumWriteMessagesPerThread = 4000; | 376   static const size_t kNumWriteMessagesPerThread = 4000; | 
| 375 | 377 | 
| 376   WriteOnlyRawChannelDelegate writer_delegate; | 378   WriteOnlyRawChannelDelegate writer_delegate; | 
| 377   scoped_ptr<RawChannel> writer_rc(RawChannel::Create(handles[0].Pass())); | 379   scoped_ptr<RawChannel> writer_rc(RawChannel::Create(handles[0].Pass())); | 
| 378   io_thread()->PostTaskAndWait(FROM_HERE, | 380   io_thread()->PostTaskAndWait(FROM_HERE, | 
| 379                                base::Bind(&InitOnIOThread, writer_rc.get(), | 381                                base::Bind(&InitOnIOThread, | 
|  | 382                                           writer_rc.get(), | 
| 380                                           base::Unretained(&writer_delegate))); | 383                                           base::Unretained(&writer_delegate))); | 
| 381 | 384 | 
| 382   ReadCountdownRawChannelDelegate reader_delegate(kNumWriterThreads * | 385   ReadCountdownRawChannelDelegate reader_delegate(kNumWriterThreads * | 
| 383                                                   kNumWriteMessagesPerThread); | 386                                                   kNumWriteMessagesPerThread); | 
| 384   scoped_ptr<RawChannel> reader_rc(RawChannel::Create(handles[1].Pass())); | 387   scoped_ptr<RawChannel> reader_rc(RawChannel::Create(handles[1].Pass())); | 
| 385   io_thread()->PostTaskAndWait(FROM_HERE, | 388   io_thread()->PostTaskAndWait(FROM_HERE, | 
| 386                                base::Bind(&InitOnIOThread, reader_rc.get(), | 389                                base::Bind(&InitOnIOThread, | 
|  | 390                                           reader_rc.get(), | 
| 387                                           base::Unretained(&reader_delegate))); | 391                                           base::Unretained(&reader_delegate))); | 
| 388 | 392 | 
| 389   { | 393   { | 
| 390     ScopedVector<RawChannelWriterThread> writer_threads; | 394     ScopedVector<RawChannelWriterThread> writer_threads; | 
| 391     for (size_t i = 0; i < kNumWriterThreads; i++) { | 395     for (size_t i = 0; i < kNumWriterThreads; i++) { | 
| 392       writer_threads.push_back(new RawChannelWriterThread( | 396       writer_threads.push_back(new RawChannelWriterThread( | 
| 393           writer_rc.get(), kNumWriteMessagesPerThread)); | 397           writer_rc.get(), kNumWriteMessagesPerThread)); | 
| 394     } | 398     } | 
| 395     for (size_t i = 0; i < writer_threads.size(); i++) | 399     for (size_t i = 0; i < writer_threads.size(); i++) | 
| 396       writer_threads[i]->Start(); | 400       writer_threads[i]->Start(); | 
| (...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 675 | 679 | 
| 676   EXPECT_FALSE(rc->WriteMessage(MakeTestMessage(1))); | 680   EXPECT_FALSE(rc->WriteMessage(MakeTestMessage(1))); | 
| 677 | 681 | 
| 678   // Wait for the delegate, which will shut the |RawChannel| down. | 682   // Wait for the delegate, which will shut the |RawChannel| down. | 
| 679   delegate.Wait(); | 683   delegate.Wait(); | 
| 680 } | 684 } | 
| 681 | 685 | 
| 682 }  // namespace | 686 }  // namespace | 
| 683 }  // namespace system | 687 }  // namespace system | 
| 684 }  // namespace mojo | 688 }  // namespace mojo | 
| OLD | NEW | 
|---|