| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/nacl/nacl_ipc_adapter.h" | 5 #include "chrome/nacl/nacl_ipc_adapter.h" |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 | 8 |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 // not need real IPC for the tests. | 30 // not need real IPC for the tests. |
| 31 adapter_ = new NaClIPCAdapter(scoped_ptr<IPC::Channel>(sink_), | 31 adapter_ = new NaClIPCAdapter(scoped_ptr<IPC::Channel>(sink_), |
| 32 base::MessageLoopProxy::current()); | 32 base::MessageLoopProxy::current()); |
| 33 } | 33 } |
| 34 virtual void TearDown() OVERRIDE { | 34 virtual void TearDown() OVERRIDE { |
| 35 sink_ = NULL; // This pointer is actually owned by the IPCAdapter. | 35 sink_ = NULL; // This pointer is actually owned by the IPCAdapter. |
| 36 adapter_ = NULL; | 36 adapter_ = NULL; |
| 37 // The adapter destructor has to post a task to destroy the Channel on the | 37 // The adapter destructor has to post a task to destroy the Channel on the |
| 38 // IO thread. For the purposes of the test, we just need to make sure that | 38 // IO thread. For the purposes of the test, we just need to make sure that |
| 39 // task gets run, or it will appear as a leak. | 39 // task gets run, or it will appear as a leak. |
| 40 message_loop_.RunAllPending(); | 40 message_loop_.RunUntilIdle(); |
| 41 } | 41 } |
| 42 | 42 |
| 43 protected: | 43 protected: |
| 44 int BlockingReceive(void* buf, size_t buf_size) { | 44 int BlockingReceive(void* buf, size_t buf_size) { |
| 45 NaClImcMsgIoVec iov = {buf, buf_size}; | 45 NaClImcMsgIoVec iov = {buf, buf_size}; |
| 46 NaClImcTypedMsgHdr msg = {&iov, 1}; | 46 NaClImcTypedMsgHdr msg = {&iov, 1}; |
| 47 return adapter_->BlockingReceive(&msg); | 47 return adapter_->BlockingReceive(&msg); |
| 48 } | 48 } |
| 49 | 49 |
| 50 int Send(void* buf, size_t buf_size) { | 50 int Send(void* buf, size_t buf_size) { |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 header->flags = 0; | 121 header->flags = 0; |
| 122 header->num_fds = 0; | 122 header->num_fds = 0; |
| 123 *reinterpret_cast<int*>( | 123 *reinterpret_cast<int*>( |
| 124 &buf[sizeof(NaClIPCAdapter::NaClMessageHeader)]) = value; | 124 &buf[sizeof(NaClIPCAdapter::NaClMessageHeader)]) = value; |
| 125 | 125 |
| 126 int result = Send(buf, buf_size); | 126 int result = Send(buf, buf_size); |
| 127 EXPECT_EQ(buf_size, result); | 127 EXPECT_EQ(buf_size, result); |
| 128 | 128 |
| 129 // Check that the message came out the other end in the test sink | 129 // Check that the message came out the other end in the test sink |
| 130 // (messages are posted, so we have to pump). | 130 // (messages are posted, so we have to pump). |
| 131 message_loop_.RunAllPending(); | 131 message_loop_.RunUntilIdle(); |
| 132 ASSERT_EQ(1u, sink_->message_count()); | 132 ASSERT_EQ(1u, sink_->message_count()); |
| 133 const IPC::Message* msg = sink_->GetMessageAt(0); | 133 const IPC::Message* msg = sink_->GetMessageAt(0); |
| 134 | 134 |
| 135 EXPECT_EQ(sizeof(int), msg->payload_size()); | 135 EXPECT_EQ(sizeof(int), msg->payload_size()); |
| 136 EXPECT_EQ(header->routing, msg->routing_id()); | 136 EXPECT_EQ(header->routing, msg->routing_id()); |
| 137 EXPECT_EQ(header->type, msg->type()); | 137 EXPECT_EQ(header->type, msg->type()); |
| 138 | 138 |
| 139 // Now test the partial send case. We should be able to break the message | 139 // Now test the partial send case. We should be able to break the message |
| 140 // into two parts and it should still work. | 140 // into two parts and it should still work. |
| 141 sink_->ClearMessages(); | 141 sink_->ClearMessages(); |
| 142 int first_chunk_size = 7; | 142 int first_chunk_size = 7; |
| 143 result = Send(buf, first_chunk_size); | 143 result = Send(buf, first_chunk_size); |
| 144 EXPECT_EQ(first_chunk_size, result); | 144 EXPECT_EQ(first_chunk_size, result); |
| 145 | 145 |
| 146 // First partial send should not have made any messages. | 146 // First partial send should not have made any messages. |
| 147 message_loop_.RunAllPending(); | 147 message_loop_.RunUntilIdle(); |
| 148 ASSERT_EQ(0u, sink_->message_count()); | 148 ASSERT_EQ(0u, sink_->message_count()); |
| 149 | 149 |
| 150 // Second partial send should do the same. | 150 // Second partial send should do the same. |
| 151 int second_chunk_size = 2; | 151 int second_chunk_size = 2; |
| 152 result = Send(&buf[first_chunk_size], second_chunk_size); | 152 result = Send(&buf[first_chunk_size], second_chunk_size); |
| 153 EXPECT_EQ(second_chunk_size, result); | 153 EXPECT_EQ(second_chunk_size, result); |
| 154 message_loop_.RunAllPending(); | 154 message_loop_.RunUntilIdle(); |
| 155 ASSERT_EQ(0u, sink_->message_count()); | 155 ASSERT_EQ(0u, sink_->message_count()); |
| 156 | 156 |
| 157 // Send the rest of the message in a third chunk. | 157 // Send the rest of the message in a third chunk. |
| 158 int third_chunk_size = buf_size - first_chunk_size - second_chunk_size; | 158 int third_chunk_size = buf_size - first_chunk_size - second_chunk_size; |
| 159 result = Send(&buf[first_chunk_size + second_chunk_size], | 159 result = Send(&buf[first_chunk_size + second_chunk_size], |
| 160 third_chunk_size); | 160 third_chunk_size); |
| 161 EXPECT_EQ(third_chunk_size, result); | 161 EXPECT_EQ(third_chunk_size, result); |
| 162 | 162 |
| 163 // Last send should have generated one message. | 163 // Last send should have generated one message. |
| 164 message_loop_.RunAllPending(); | 164 message_loop_.RunUntilIdle(); |
| 165 ASSERT_EQ(1u, sink_->message_count()); | 165 ASSERT_EQ(1u, sink_->message_count()); |
| 166 msg = sink_->GetMessageAt(0); | 166 msg = sink_->GetMessageAt(0); |
| 167 EXPECT_EQ(sizeof(int), msg->payload_size()); | 167 EXPECT_EQ(sizeof(int), msg->payload_size()); |
| 168 EXPECT_EQ(header->routing, msg->routing_id()); | 168 EXPECT_EQ(header->routing, msg->routing_id()); |
| 169 EXPECT_EQ(header->type, msg->type()); | 169 EXPECT_EQ(header->type, msg->type()); |
| 170 } | 170 } |
| 171 | 171 |
| 172 // Tests when a buffer is too small to receive the entire message. | 172 // Tests when a buffer is too small to receive the entire message. |
| 173 TEST_F(NaClIPCAdapterTest, PartialReceive) { | 173 TEST_F(NaClIPCAdapterTest, PartialReceive) { |
| 174 int routing_id_1 = 0x89898989; | 174 int routing_id_1 = 0x89898989; |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 237 header->routing = routing_id; | 237 header->routing = routing_id; |
| 238 header->type = type; | 238 header->type = type; |
| 239 header->flags = 0; | 239 header->flags = 0; |
| 240 header->num_fds = 0; | 240 header->num_fds = 0; |
| 241 *reinterpret_cast<int*>( | 241 *reinterpret_cast<int*>( |
| 242 &buf[sizeof(NaClIPCAdapter::NaClMessageHeader)]) = value; | 242 &buf[sizeof(NaClIPCAdapter::NaClMessageHeader)]) = value; |
| 243 | 243 |
| 244 // Send too much data and make sure that the send fails. | 244 // Send too much data and make sure that the send fails. |
| 245 int result = Send(buf, big_buf_size); | 245 int result = Send(buf, big_buf_size); |
| 246 EXPECT_EQ(-1, result); | 246 EXPECT_EQ(-1, result); |
| 247 message_loop_.RunAllPending(); | 247 message_loop_.RunUntilIdle(); |
| 248 ASSERT_EQ(0u, sink_->message_count()); | 248 ASSERT_EQ(0u, sink_->message_count()); |
| 249 | 249 |
| 250 // Send too much data in two chunks and make sure that the send fails. | 250 // Send too much data in two chunks and make sure that the send fails. |
| 251 int first_chunk_size = 7; | 251 int first_chunk_size = 7; |
| 252 result = Send(buf, first_chunk_size); | 252 result = Send(buf, first_chunk_size); |
| 253 EXPECT_EQ(first_chunk_size, result); | 253 EXPECT_EQ(first_chunk_size, result); |
| 254 | 254 |
| 255 // First partial send should not have made any messages. | 255 // First partial send should not have made any messages. |
| 256 message_loop_.RunAllPending(); | 256 message_loop_.RunUntilIdle(); |
| 257 ASSERT_EQ(0u, sink_->message_count()); | 257 ASSERT_EQ(0u, sink_->message_count()); |
| 258 | 258 |
| 259 int second_chunk_size = big_buf_size - first_chunk_size; | 259 int second_chunk_size = big_buf_size - first_chunk_size; |
| 260 result = Send(&buf[first_chunk_size], second_chunk_size); | 260 result = Send(&buf[first_chunk_size], second_chunk_size); |
| 261 EXPECT_EQ(-1, result); | 261 EXPECT_EQ(-1, result); |
| 262 message_loop_.RunAllPending(); | 262 message_loop_.RunUntilIdle(); |
| 263 ASSERT_EQ(0u, sink_->message_count()); | 263 ASSERT_EQ(0u, sink_->message_count()); |
| 264 } | 264 } |
| 265 | 265 |
| 266 // Tests that when the IPC channel reports an error, that waiting reads are | 266 // Tests that when the IPC channel reports an error, that waiting reads are |
| 267 // unblocked and return a -1 error code. | 267 // unblocked and return a -1 error code. |
| 268 TEST_F(NaClIPCAdapterTest, ReadWithChannelError) { | 268 TEST_F(NaClIPCAdapterTest, ReadWithChannelError) { |
| 269 // Have a background thread that waits a bit and calls the channel error | 269 // Have a background thread that waits a bit and calls the channel error |
| 270 // handler. This should wake up any waiting threads and immediately return | 270 // handler. This should wake up any waiting threads and immediately return |
| 271 // -1. There is an inherent race condition in that we can't be sure if the | 271 // -1. There is an inherent race condition in that we can't be sure if the |
| 272 // other thread is actually waiting when this happens. This is OK, since the | 272 // other thread is actually waiting when this happens. This is OK, since the |
| (...skipping 26 matching lines...) Expand all Loading... |
| 299 EXPECT_EQ(-1, result); | 299 EXPECT_EQ(-1, result); |
| 300 | 300 |
| 301 // Test the "previously had an error" case. BlockingReceive should return | 301 // Test the "previously had an error" case. BlockingReceive should return |
| 302 // immediately if there was an error. | 302 // immediately if there was an error. |
| 303 result = BlockingReceive(buf, kBufSize); | 303 result = BlockingReceive(buf, kBufSize); |
| 304 EXPECT_EQ(-1, result); | 304 EXPECT_EQ(-1, result); |
| 305 | 305 |
| 306 thread.Join(); | 306 thread.Join(); |
| 307 } | 307 } |
| 308 | 308 |
| OLD | NEW |