OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/nacl/nacl_ipc_adapter.h" |
| 6 |
| 7 #include <string.h> |
| 8 |
| 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/message_loop.h" |
| 11 #include "base/message_loop_proxy.h" |
| 12 #include "base/threading/platform_thread.h" |
| 13 #include "base/threading/simple_thread.h" |
| 14 #include "ipc/ipc_test_sink.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 |
| 17 namespace { |
| 18 |
| 19 class NaClIPCAdapterTest : public testing::Test { |
| 20 public: |
| 21 NaClIPCAdapterTest() {} |
| 22 |
| 23 // testing::Test implementation. |
| 24 virtual void SetUp() OVERRIDE { |
| 25 sink_ = new IPC::TestSink; |
| 26 |
| 27 // Takes ownership of the sink_ pointer. |
| 28 adapter_ = new NaClIPCAdapter(scoped_ptr<IPC::Channel>(sink_), |
| 29 base::MessageLoopProxy::current()); |
| 30 } |
| 31 virtual void TearDown() OVERRIDE { |
| 32 sink_ = NULL; // This pointer is actually owned by the IPCAdapter. |
| 33 adapter_ = NULL; |
| 34 } |
| 35 |
| 36 protected: |
| 37 MessageLoop message_loop_; |
| 38 |
| 39 scoped_refptr<NaClIPCAdapter> adapter_; |
| 40 |
| 41 // Messages sent from nacl to the adapter end up here. Note that we create |
| 42 // this pointer and pass ownership of it to the IPC adapter, who will keep |
| 43 // it alive as long as the adapter is alive. This means that when the |
| 44 // adapter goes away, this pointer will become invalid. |
| 45 // |
| 46 // In real life the adapter needs to take ownership so the channel can be |
| 47 // destroyed on the right thread. |
| 48 IPC::TestSink* sink_; |
| 49 }; |
| 50 |
| 51 } // namespace |
| 52 |
| 53 // Tests a simple message getting rewritten sent from native code to NaCl. |
| 54 TEST_F(NaClIPCAdapterTest, SimpleReceiveRewriting) { |
| 55 int routing_id = 0x89898989; |
| 56 int type = 0x55555555; |
| 57 IPC::Message input(routing_id, type, IPC::Message::PRIORITY_NORMAL); |
| 58 |
| 59 int value = 0x12345678; |
| 60 input.WriteInt(value); |
| 61 adapter_->OnMessageReceived(input); |
| 62 |
| 63 // Buffer just need to be big enough for our message with one int. |
| 64 const int kBufSize = 64; |
| 65 char buf[kBufSize]; |
| 66 |
| 67 int bytes_read = adapter_->BlockingReceive(buf, kBufSize); |
| 68 EXPECT_EQ(sizeof(NaClIPCAdapter::NaClMessageHeader) + sizeof(int), |
| 69 bytes_read); |
| 70 |
| 71 const NaClIPCAdapter::NaClMessageHeader* output_header = |
| 72 reinterpret_cast<const NaClIPCAdapter::NaClMessageHeader*>(buf); |
| 73 EXPECT_EQ(sizeof(int), output_header->payload_size); |
| 74 EXPECT_EQ(routing_id, output_header->routing); |
| 75 EXPECT_EQ(type, output_header->type); |
| 76 EXPECT_EQ(IPC::Message::PRIORITY_NORMAL, output_header->flags); |
| 77 EXPECT_EQ(0, output_header->num_fds); |
| 78 EXPECT_EQ(0, output_header->pad); |
| 79 |
| 80 // Validate the payload. |
| 81 EXPECT_EQ(value, |
| 82 *reinterpret_cast<const int*>(&buf[ |
| 83 sizeof(NaClIPCAdapter::NaClMessageHeader)])); |
| 84 } |
| 85 |
| 86 // Tests a simple message getting rewritten sent from NaCl to native code. |
| 87 TEST_F(NaClIPCAdapterTest, SendRewriting) { |
| 88 int routing_id = 0x89898989; |
| 89 int type = 0x55555555; |
| 90 int value = 0x12345678; |
| 91 |
| 92 // Send a message with one int inside it. |
| 93 const int buf_size = sizeof(NaClIPCAdapter::NaClMessageHeader) + sizeof(int); |
| 94 char buf[buf_size] = {0}; |
| 95 |
| 96 NaClIPCAdapter::NaClMessageHeader* header = |
| 97 reinterpret_cast<NaClIPCAdapter::NaClMessageHeader*>(buf); |
| 98 header->payload_size = sizeof(int); |
| 99 header->routing = routing_id; |
| 100 header->type = type; |
| 101 header->flags = 0; |
| 102 header->num_fds = 0; |
| 103 *reinterpret_cast<int*>( |
| 104 &buf[sizeof(NaClIPCAdapter::NaClMessageHeader)]) = value; |
| 105 |
| 106 int result = adapter_->Send(buf, buf_size); |
| 107 EXPECT_EQ(buf_size, result); |
| 108 |
| 109 // Check that the message came out the other end in the test sink |
| 110 // (messages are posted to we have to pump). |
| 111 message_loop_.RunAllPending(); |
| 112 ASSERT_EQ(1, sink_->message_count()); |
| 113 const IPC::Message* msg = sink_->GetMessageAt(0); |
| 114 |
| 115 EXPECT_EQ(sizeof(int), msg->payload_size()); |
| 116 EXPECT_EQ(header->routing, msg->routing_id()); |
| 117 EXPECT_EQ(header->type, msg->type()); |
| 118 |
| 119 // Now test the partial send case. We should be able to break the message |
| 120 // into two parts and it should still work. |
| 121 sink_->ClearMessages(); |
| 122 int first_chunk_size = 7; |
| 123 result = adapter_->Send(buf, first_chunk_size); |
| 124 EXPECT_EQ(first_chunk_size, result); |
| 125 |
| 126 // First partial send should not have made any messages. |
| 127 message_loop_.RunAllPending(); |
| 128 ASSERT_EQ(0, sink_->message_count()); |
| 129 |
| 130 int second_chunk_size = buf_size - first_chunk_size; |
| 131 result = adapter_->Send(&buf[first_chunk_size], second_chunk_size); |
| 132 EXPECT_EQ(second_chunk_size, result); |
| 133 |
| 134 // Second partial send should have generated one message. |
| 135 message_loop_.RunAllPending(); |
| 136 ASSERT_EQ(1, sink_->message_count()); |
| 137 msg = sink_->GetMessageAt(0); |
| 138 EXPECT_EQ(sizeof(int), msg->payload_size()); |
| 139 EXPECT_EQ(header->routing, msg->routing_id()); |
| 140 EXPECT_EQ(header->type, msg->type()); |
| 141 } |
| 142 |
| 143 // Tests when a buffer is too small to receive the entire message. |
| 144 TEST_F(NaClIPCAdapterTest, PartialReceive) { |
| 145 int routing_id_1 = 0x89898989; |
| 146 int type_1 = 0x55555555; |
| 147 IPC::Message input_1(routing_id_1, type_1, IPC::Message::PRIORITY_NORMAL); |
| 148 int value_1 = 0x12121212; |
| 149 input_1.WriteInt(value_1); |
| 150 adapter_->OnMessageReceived(input_1); |
| 151 |
| 152 int routing_id_2 = 0x90909090; |
| 153 int type_2 = 0x66666666; |
| 154 IPC::Message input_2(routing_id_2, type_2, IPC::Message::PRIORITY_NORMAL); |
| 155 int value_2 = 0x23232323; |
| 156 input_2.WriteInt(value_2); |
| 157 adapter_->OnMessageReceived(input_2); |
| 158 |
| 159 const int kBufSize = 64; |
| 160 char buf[kBufSize]; |
| 161 |
| 162 // Read part of the first message. |
| 163 int bytes_requested = 7; |
| 164 int bytes_read = adapter_->BlockingReceive(buf, bytes_requested); |
| 165 ASSERT_EQ(bytes_requested, bytes_read); |
| 166 |
| 167 // Read the rest, this should give us the rest of the first message only. |
| 168 bytes_read += adapter_->BlockingReceive(&buf[bytes_requested], |
| 169 kBufSize - bytes_requested); |
| 170 EXPECT_EQ(sizeof(NaClIPCAdapter::NaClMessageHeader) + sizeof(int), |
| 171 bytes_read); |
| 172 |
| 173 // Make sure we got the right message. |
| 174 const NaClIPCAdapter::NaClMessageHeader* output_header = |
| 175 reinterpret_cast<const NaClIPCAdapter::NaClMessageHeader*>(buf); |
| 176 EXPECT_EQ(sizeof(int), output_header->payload_size); |
| 177 EXPECT_EQ(routing_id_1, output_header->routing); |
| 178 EXPECT_EQ(type_1, output_header->type); |
| 179 |
| 180 // Read the second message to make sure we went on to it. |
| 181 bytes_read = adapter_->BlockingReceive(buf, kBufSize); |
| 182 EXPECT_EQ(sizeof(NaClIPCAdapter::NaClMessageHeader) + sizeof(int), |
| 183 bytes_read); |
| 184 output_header = |
| 185 reinterpret_cast<const NaClIPCAdapter::NaClMessageHeader*>(buf); |
| 186 EXPECT_EQ(sizeof(int), output_header->payload_size); |
| 187 EXPECT_EQ(routing_id_2, output_header->routing); |
| 188 EXPECT_EQ(type_2, output_header->type); |
| 189 } |
| 190 |
| 191 // Tests sending messages that are too large. We test sends that are too |
| 192 // small implicitly here and in the success case because in that case it |
| 193 // succeeds and buffers the data. |
| 194 TEST_F(NaClIPCAdapterTest, SendOverflow) { |
| 195 int routing_id = 0x89898989; |
| 196 int type = 0x55555555; |
| 197 int value = 0x12345678; |
| 198 |
| 199 // Make a message with one int inside it. Reserve some extra space so |
| 200 // we can test what happens when we send too much data. |
| 201 const int buf_size = sizeof(NaClIPCAdapter::NaClMessageHeader) + sizeof(int); |
| 202 const int big_buf_size = buf_size + 4; |
| 203 char buf[big_buf_size] = {0}; |
| 204 |
| 205 NaClIPCAdapter::NaClMessageHeader* header = |
| 206 reinterpret_cast<NaClIPCAdapter::NaClMessageHeader*>(buf); |
| 207 header->payload_size = sizeof(int); |
| 208 header->routing = routing_id; |
| 209 header->type = type; |
| 210 header->flags = 0; |
| 211 header->num_fds = 0; |
| 212 *reinterpret_cast<int*>( |
| 213 &buf[sizeof(NaClIPCAdapter::NaClMessageHeader)]) = value; |
| 214 |
| 215 // Send too much data and make sure that the send fails. |
| 216 int result = adapter_->Send(buf, big_buf_size); |
| 217 EXPECT_EQ(-1, result); |
| 218 message_loop_.RunAllPending(); |
| 219 ASSERT_EQ(0, sink_->message_count()); |
| 220 |
| 221 // Send too much data in two chunks and make sure that the send fails. |
| 222 int first_chunk_size = 7; |
| 223 result = adapter_->Send(buf, first_chunk_size); |
| 224 EXPECT_EQ(first_chunk_size, result); |
| 225 |
| 226 // First partial send should not have made any messages. |
| 227 message_loop_.RunAllPending(); |
| 228 ASSERT_EQ(0, sink_->message_count()); |
| 229 |
| 230 int second_chunk_size = big_buf_size - first_chunk_size; |
| 231 result = adapter_->Send(&buf[first_chunk_size], second_chunk_size); |
| 232 EXPECT_EQ(-1, result); |
| 233 message_loop_.RunAllPending(); |
| 234 ASSERT_EQ(0, sink_->message_count()); |
| 235 } |
| 236 |
| 237 // Tests that when the IPC channel reports an error, that waiting reads are |
| 238 // unblocked and return a -1 error code. |
| 239 TEST_F(NaClIPCAdapterTest, ReadWithChannelError) { |
| 240 // Have a background thread that waits a bit and calls the channel error |
| 241 // handler. This should wake up any waiting threads and immediately return |
| 242 // -1. There is an inherent race condition in that we can't be sure if the |
| 243 // other thread is actually waiting when this happens. This is OK, since the |
| 244 // behavior (which we also explicitly test later) is to return -1 if the |
| 245 // channel has already had an error when you start waiting. |
| 246 class MyThread : public base::SimpleThread { |
| 247 public: |
| 248 explicit MyThread(NaClIPCAdapter* adapter) |
| 249 : SimpleThread("NaClIPCAdapterThread"), |
| 250 adapter_(adapter) {} |
| 251 virtual void Run() { |
| 252 base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(1)); |
| 253 adapter_->OnChannelError(); |
| 254 } |
| 255 private: |
| 256 scoped_refptr<NaClIPCAdapter> adapter_; |
| 257 }; |
| 258 MyThread thread(adapter_); |
| 259 |
| 260 // IMPORTANT: do not return early from here down (including ASSERT_*) because |
| 261 // the thread needs to joined or it will assert. |
| 262 thread.Start(); |
| 263 |
| 264 // Request data. This will normally (modulo races) block until data is |
| 265 // received or there is an error, and the thread above will wake us up |
| 266 // after 1s. |
| 267 const int kBufSize = 64; |
| 268 char buf[kBufSize]; |
| 269 int result = adapter_->BlockingReceive(buf, kBufSize); |
| 270 EXPECT_EQ(-1, result); |
| 271 |
| 272 // Test the "previously had an error" case. BlockingReceive should return |
| 273 // immediately if there was an error. |
| 274 result = adapter_->BlockingReceive(buf, kBufSize); |
| 275 EXPECT_EQ(-1, result); |
| 276 |
| 277 thread.Join(); |
| 278 } |
| 279 |
OLD | NEW |