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" |
11 #include "base/message_loop_proxy.h" | 11 #include "base/message_loop_proxy.h" |
12 #include "base/threading/platform_thread.h" | 12 #include "base/threading/platform_thread.h" |
13 #include "base/threading/simple_thread.h" | 13 #include "base/threading/simple_thread.h" |
14 #include "ipc/ipc_test_sink.h" | 14 #include "ipc/ipc_test_sink.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
16 | 16 |
17 namespace { | 17 namespace { |
18 | 18 |
19 class NaClIPCAdapterTest : public testing::Test { | 19 class NaClIPCAdapterTest : public testing::Test { |
20 public: | 20 public: |
21 NaClIPCAdapterTest() {} | 21 NaClIPCAdapterTest() {} |
22 | 22 |
23 // testing::Test implementation. | 23 // testing::Test implementation. |
24 virtual void SetUp() OVERRIDE { | 24 virtual void SetUp() OVERRIDE { |
25 sink_ = new IPC::TestSink; | 25 sink_ = new IPC::TestSink; |
26 | 26 |
27 // Takes ownership of the sink_ pointer. | 27 // Takes ownership of the sink_ pointer. Note we provide the current message |
| 28 // loop instead of using a real IO thread. This should work OK since we do |
| 29 // not need real IPC for the tests. |
28 adapter_ = new NaClIPCAdapter(scoped_ptr<IPC::Channel>(sink_), | 30 adapter_ = new NaClIPCAdapter(scoped_ptr<IPC::Channel>(sink_), |
29 base::MessageLoopProxy::current()); | 31 base::MessageLoopProxy::current()); |
30 } | 32 } |
31 virtual void TearDown() OVERRIDE { | 33 virtual void TearDown() OVERRIDE { |
32 sink_ = NULL; // This pointer is actually owned by the IPCAdapter. | 34 sink_ = NULL; // This pointer is actually owned by the IPCAdapter. |
33 adapter_ = NULL; | 35 adapter_ = NULL; |
| 36 // The adapter destructor has to post a task to destroy the Channel on the |
| 37 // IO thread. For the purposes of the test, we just need to make sure that |
| 38 // task gets run, or it will appear as a leak. |
| 39 message_loop_.RunAllPending(); |
34 } | 40 } |
35 | 41 |
36 protected: | 42 protected: |
37 MessageLoop message_loop_; | 43 MessageLoop message_loop_; |
38 | 44 |
39 scoped_refptr<NaClIPCAdapter> adapter_; | 45 scoped_refptr<NaClIPCAdapter> adapter_; |
40 | 46 |
41 // Messages sent from nacl to the adapter end up here. Note that we create | 47 // 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 | 48 // 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 | 49 // it alive as long as the adapter is alive. This means that when the |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 header->type = type; | 107 header->type = type; |
102 header->flags = 0; | 108 header->flags = 0; |
103 header->num_fds = 0; | 109 header->num_fds = 0; |
104 *reinterpret_cast<int*>( | 110 *reinterpret_cast<int*>( |
105 &buf[sizeof(NaClIPCAdapter::NaClMessageHeader)]) = value; | 111 &buf[sizeof(NaClIPCAdapter::NaClMessageHeader)]) = value; |
106 | 112 |
107 int result = adapter_->Send(buf, buf_size); | 113 int result = adapter_->Send(buf, buf_size); |
108 EXPECT_EQ(buf_size, result); | 114 EXPECT_EQ(buf_size, result); |
109 | 115 |
110 // Check that the message came out the other end in the test sink | 116 // Check that the message came out the other end in the test sink |
111 // (messages are posted to we have to pump). | 117 // (messages are posted, so we have to pump). |
112 message_loop_.RunAllPending(); | 118 message_loop_.RunAllPending(); |
113 ASSERT_EQ(1u, sink_->message_count()); | 119 ASSERT_EQ(1u, sink_->message_count()); |
114 const IPC::Message* msg = sink_->GetMessageAt(0); | 120 const IPC::Message* msg = sink_->GetMessageAt(0); |
115 | 121 |
116 EXPECT_EQ(sizeof(int), msg->payload_size()); | 122 EXPECT_EQ(sizeof(int), msg->payload_size()); |
117 EXPECT_EQ(header->routing, msg->routing_id()); | 123 EXPECT_EQ(header->routing, msg->routing_id()); |
118 EXPECT_EQ(header->type, msg->type()); | 124 EXPECT_EQ(header->type, msg->type()); |
119 | 125 |
120 // Now test the partial send case. We should be able to break the message | 126 // Now test the partial send case. We should be able to break the message |
121 // into two parts and it should still work. | 127 // into two parts and it should still work. |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
280 EXPECT_EQ(-1, result); | 286 EXPECT_EQ(-1, result); |
281 | 287 |
282 // Test the "previously had an error" case. BlockingReceive should return | 288 // Test the "previously had an error" case. BlockingReceive should return |
283 // immediately if there was an error. | 289 // immediately if there was an error. |
284 result = adapter_->BlockingReceive(buf, kBufSize); | 290 result = adapter_->BlockingReceive(buf, kBufSize); |
285 EXPECT_EQ(-1, result); | 291 EXPECT_EQ(-1, result); |
286 | 292 |
287 thread.Join(); | 293 thread.Join(); |
288 } | 294 } |
289 | 295 |
OLD | NEW |