OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "build/build_config.h" | 5 #include "build/build_config.h" |
6 | 6 |
7 #include <windows.h> | 7 #include <windows.h> |
8 | 8 |
9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
11 #include "base/files/scoped_temp_dir.h" | 11 #include "base/files/scoped_temp_dir.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "ipc/attachment_broker_privileged_win.h" | 13 #include "ipc/attachment_broker_privileged_win.h" |
14 #include "ipc/attachment_broker_unprivileged_win.h" | 14 #include "ipc/attachment_broker_unprivileged_win.h" |
15 #include "ipc/handle_attachment_win.h" | 15 #include "ipc/handle_attachment_win.h" |
16 #include "ipc/handle_win.h" | 16 #include "ipc/handle_win.h" |
17 #include "ipc/ipc_listener.h" | 17 #include "ipc/ipc_listener.h" |
18 #include "ipc/ipc_message.h" | 18 #include "ipc/ipc_message.h" |
19 #include "ipc/ipc_test_base.h" | 19 #include "ipc/ipc_test_base.h" |
| 20 #include "ipc/ipc_test_messages.h" |
20 | 21 |
21 namespace { | 22 namespace { |
22 | 23 |
23 const char kDataBuffer[] = "This is some test data to write to the file."; | 24 const char kDataBuffer[] = "This is some test data to write to the file."; |
24 | 25 |
25 // Returns the contents of the file represented by |h| as a std::string. | 26 // Returns the contents of the file represented by |h| as a std::string. |
26 std::string ReadFromFile(HANDLE h) { | 27 std::string ReadFromFile(HANDLE h) { |
27 SetFilePointer(h, 0, nullptr, FILE_BEGIN); | 28 SetFilePointer(h, 0, nullptr, FILE_BEGIN); |
28 char buffer[100]; | 29 char buffer[100]; |
29 DWORD bytes_read; | 30 DWORD bytes_read; |
30 BOOL success = ::ReadFile(h, buffer, static_cast<DWORD>(strlen(kDataBuffer)), | 31 BOOL success = ::ReadFile(h, buffer, static_cast<DWORD>(strlen(kDataBuffer)), |
31 &bytes_read, nullptr); | 32 &bytes_read, nullptr); |
32 return success ? std::string(buffer, bytes_read) : std::string(); | 33 return success ? std::string(buffer, bytes_read) : std::string(); |
33 } | 34 } |
34 | 35 |
35 HANDLE GetHandleFromBrokeredAttachment( | 36 HANDLE GetHandleFromBrokeredAttachment( |
36 const scoped_refptr<IPC::BrokerableAttachment>& attachment) { | 37 const scoped_refptr<IPC::BrokerableAttachment>& attachment) { |
37 if (attachment->GetType() != | 38 if (attachment->GetType() != |
38 IPC::BrokerableAttachment::TYPE_BROKERABLE_ATTACHMENT) | 39 IPC::BrokerableAttachment::TYPE_BROKERABLE_ATTACHMENT) { |
| 40 LOG(INFO) << "Attachment type not TYPE_BROKERABLE_ATTACHMENT."; |
39 return nullptr; | 41 return nullptr; |
40 if (attachment->GetBrokerableType() != IPC::BrokerableAttachment::WIN_HANDLE) | 42 } |
| 43 |
| 44 if (attachment->GetBrokerableType() != |
| 45 IPC::BrokerableAttachment::WIN_HANDLE) { |
| 46 LOG(INFO) << "Brokerable type not WIN_HANDLE."; |
41 return nullptr; | 47 return nullptr; |
| 48 } |
| 49 |
42 IPC::internal::HandleAttachmentWin* received_handle_attachment = | 50 IPC::internal::HandleAttachmentWin* received_handle_attachment = |
43 static_cast<IPC::internal::HandleAttachmentWin*>(attachment.get()); | 51 static_cast<IPC::internal::HandleAttachmentWin*>(attachment.get()); |
44 return received_handle_attachment->get_handle(); | 52 return received_handle_attachment->get_handle(); |
45 } | 53 } |
46 | 54 |
47 // Returns true if |attachment| is a file HANDLE whose contents is | 55 // |message| must be deserializable as a TestHandleWinMsg. Returns the HANDLE, |
48 // |kDataBuffer|. | 56 // or nullptr if deserialization failed. |
49 bool CheckContentsOfBrokeredAttachment( | 57 HANDLE GetHandleFromTestHandleWinMsg(const IPC::Message& message) { |
50 const scoped_refptr<IPC::BrokerableAttachment>& attachment) { | 58 // Expect a message with a brokered attachment. |
51 HANDLE h = GetHandleFromBrokeredAttachment(attachment); | 59 if (!message.HasBrokerableAttachments()) { |
52 if (h == nullptr) | 60 LOG(INFO) << "Message missing brokerable attachment."; |
| 61 return nullptr; |
| 62 } |
| 63 |
| 64 TestHandleWinMsg::Schema::Param p; |
| 65 bool success = TestHandleWinMsg::Read(&message, &p); |
| 66 if (!success) { |
| 67 LOG(INFO) << "Failed to deserialize message."; |
| 68 return nullptr; |
| 69 } |
| 70 |
| 71 IPC::HandleWin handle_win = base::get<1>(p); |
| 72 return handle_win.get_handle(); |
| 73 } |
| 74 |
| 75 // |message| must be deserializable as a TestTwoHandleWinMsg. Returns the |
| 76 // HANDLE, or nullptr if deserialization failed. |
| 77 HANDLE GetHandleFromTestTwoHandleWinMsg(const IPC::Message& message, |
| 78 int index) { |
| 79 // Expect a message with a brokered attachment. |
| 80 if (!message.HasBrokerableAttachments()) { |
| 81 LOG(INFO) << "Message missing brokerable attachment."; |
| 82 return nullptr; |
| 83 } |
| 84 |
| 85 TestTwoHandleWinMsg::Schema::Param p; |
| 86 bool success = TestTwoHandleWinMsg::Read(&message, &p); |
| 87 if (!success) { |
| 88 LOG(INFO) << "Failed to deserialize message."; |
| 89 return nullptr; |
| 90 } |
| 91 |
| 92 IPC::HandleWin handle_win; |
| 93 if (index == 0) |
| 94 handle_win = base::get<0>(p); |
| 95 else if (index == 1) |
| 96 handle_win = base::get<1>(p); |
| 97 return handle_win.get_handle(); |
| 98 } |
| 99 |
| 100 // |message| must be deserializable as a TestHandleWinMsg. Returns true if the |
| 101 // attached file HANDLE has contents |kDataBuffer|. |
| 102 bool CheckContentsOfTestMessage(const IPC::Message& message) { |
| 103 HANDLE h = GetHandleFromTestHandleWinMsg(message); |
| 104 if (h == nullptr) { |
| 105 LOG(INFO) << "Failed to get handle from TestHandleWinMsg."; |
53 return false; | 106 return false; |
| 107 } |
54 | 108 |
55 std::string contents = ReadFromFile(h); | 109 std::string contents = ReadFromFile(h); |
56 return contents == std::string(kDataBuffer); | 110 bool success = (contents == std::string(kDataBuffer)); |
| 111 if (!success) { |
| 112 LOG(INFO) << "Expected contents: " << std::string(kDataBuffer); |
| 113 LOG(INFO) << "Read contents: " << contents; |
| 114 } |
| 115 return success; |
57 } | 116 } |
58 | 117 |
59 enum TestResult { | 118 enum TestResult { |
60 RESULT_UNKNOWN, | 119 RESULT_UNKNOWN, |
61 RESULT_SUCCESS, | 120 RESULT_SUCCESS, |
62 RESULT_FAILURE, | 121 RESULT_FAILURE, |
63 }; | 122 }; |
64 | 123 |
65 // Once the test is finished, send a control message to the parent process with | 124 // Once the test is finished, send a control message to the parent process with |
66 // the result. The message may require the runloop to be run before its | 125 // the result. The message may require the runloop to be run before its |
(...skipping 14 matching lines...) Expand all Loading... |
81 IPC::BrokerableAttachment::AttachmentId* get_id() { return &id_; } | 140 IPC::BrokerableAttachment::AttachmentId* get_id() { return &id_; } |
82 | 141 |
83 private: | 142 private: |
84 IPC::BrokerableAttachment::AttachmentId id_; | 143 IPC::BrokerableAttachment::AttachmentId id_; |
85 }; | 144 }; |
86 | 145 |
87 // Forwards all messages to |listener_|. Quits the message loop after a | 146 // Forwards all messages to |listener_|. Quits the message loop after a |
88 // message is received, or the channel has an error. | 147 // message is received, or the channel has an error. |
89 class ProxyListener : public IPC::Listener { | 148 class ProxyListener : public IPC::Listener { |
90 public: | 149 public: |
91 ProxyListener() : reason_(MESSAGE_RECEIVED) {} | 150 ProxyListener() : listener_(nullptr), reason_(MESSAGE_RECEIVED) {} |
92 ~ProxyListener() override {} | 151 ~ProxyListener() override {} |
93 | 152 |
94 // The reason for exiting the message loop. | 153 // The reason for exiting the message loop. |
95 enum Reason { MESSAGE_RECEIVED, CHANNEL_ERROR }; | 154 enum Reason { MESSAGE_RECEIVED, CHANNEL_ERROR }; |
96 | 155 |
97 bool OnMessageReceived(const IPC::Message& message) override { | 156 bool OnMessageReceived(const IPC::Message& message) override { |
98 bool result = listener_->OnMessageReceived(message); | 157 bool result = false; |
| 158 if (listener_) |
| 159 result = listener_->OnMessageReceived(message); |
99 reason_ = MESSAGE_RECEIVED; | 160 reason_ = MESSAGE_RECEIVED; |
100 base::MessageLoop::current()->Quit(); | 161 messages_.push_back(message); |
| 162 base::MessageLoop::current()->QuitNow(); |
101 return result; | 163 return result; |
102 } | 164 } |
103 | 165 |
104 void OnChannelError() override { | 166 void OnChannelError() override { |
105 reason_ = CHANNEL_ERROR; | 167 reason_ = CHANNEL_ERROR; |
106 base::MessageLoop::current()->Quit(); | 168 base::MessageLoop::current()->QuitNow(); |
107 } | 169 } |
108 | 170 |
109 void set_listener(IPC::Listener* listener) { listener_ = listener; } | 171 void set_listener(IPC::Listener* listener) { listener_ = listener; } |
110 Reason get_reason() { return reason_; } | 172 Reason get_reason() { return reason_; } |
| 173 IPC::Message get_first_message() { return messages_[0]; } |
| 174 void pop_first_message() { messages_.erase(messages_.begin()); } |
| 175 bool has_message() { return !messages_.empty(); } |
111 | 176 |
112 private: | 177 private: |
113 IPC::Listener* listener_; | 178 IPC::Listener* listener_; |
114 Reason reason_; | 179 Reason reason_; |
| 180 std::vector<IPC::Message> messages_; |
115 }; | 181 }; |
116 | 182 |
117 // Waits for a result to be sent over the channel. Quits the message loop | 183 // Waits for a result to be sent over the channel. Quits the message loop |
118 // after a message is received, or the channel has an error. | 184 // after a message is received, or the channel has an error. |
119 class ResultListener : public IPC::Listener { | 185 class ResultListener : public IPC::Listener { |
120 public: | 186 public: |
121 ResultListener() : result_(RESULT_UNKNOWN) {} | 187 ResultListener() : result_(RESULT_UNKNOWN) {} |
122 ~ResultListener() override {} | 188 ~ResultListener() override {} |
123 | 189 |
124 bool OnMessageReceived(const IPC::Message& message) override { | 190 bool OnMessageReceived(const IPC::Message& message) override { |
125 base::PickleIterator iter(message); | 191 base::PickleIterator iter(message); |
126 | 192 |
127 int result; | 193 int result; |
128 EXPECT_TRUE(iter.ReadInt(&result)); | 194 EXPECT_TRUE(iter.ReadInt(&result)); |
129 result_ = static_cast<TestResult>(result); | 195 result_ = static_cast<TestResult>(result); |
130 return true; | 196 return true; |
131 } | 197 } |
132 | 198 |
133 TestResult get_result() { return result_; } | 199 TestResult get_result() { return result_; } |
134 | 200 |
135 private: | 201 private: |
136 TestResult result_; | 202 TestResult result_; |
137 }; | 203 }; |
138 | 204 |
139 // The parent process acts as an unprivileged process. The forked process acts | 205 // The parent process acts as an unprivileged process. The forked process acts |
140 // as the privileged process. | 206 // as the privileged process. |
141 class IPCAttachmentBrokerPrivilegedWinTest : public IPCTestBase { | 207 class IPCAttachmentBrokerPrivilegedWinTest : public IPCTestBase { |
142 public: | 208 public: |
143 IPCAttachmentBrokerPrivilegedWinTest() : message_index_(0) {} | 209 IPCAttachmentBrokerPrivilegedWinTest() {} |
144 ~IPCAttachmentBrokerPrivilegedWinTest() override {} | 210 ~IPCAttachmentBrokerPrivilegedWinTest() override {} |
145 | 211 |
146 void SetUp() override { | 212 void SetUp() override { |
147 IPCTestBase::SetUp(); | 213 IPCTestBase::SetUp(); |
148 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 214 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
149 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &temp_path_)); | 215 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &temp_path_)); |
150 } | 216 } |
151 | 217 |
152 void TearDown() override { IPCTestBase::TearDown(); } | 218 void TearDown() override { IPCTestBase::TearDown(); } |
153 | 219 |
(...skipping 27 matching lines...) Expand all Loading... |
181 static_cast<int>(strlen(kDataBuffer)))); | 247 static_cast<int>(strlen(kDataBuffer)))); |
182 | 248 |
183 HANDLE h = | 249 HANDLE h = |
184 CreateFile(temp_path_.value().c_str(), GENERIC_READ | GENERIC_WRITE, 0, | 250 CreateFile(temp_path_.value().c_str(), GENERIC_READ | GENERIC_WRITE, 0, |
185 nullptr, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); | 251 nullptr, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); |
186 EXPECT_NE(h, INVALID_HANDLE_VALUE); | 252 EXPECT_NE(h, INVALID_HANDLE_VALUE); |
187 return h; | 253 return h; |
188 } | 254 } |
189 | 255 |
190 void SendMessageWithAttachment(HANDLE h) { | 256 void SendMessageWithAttachment(HANDLE h) { |
191 IPC::Message* message = | 257 IPC::HandleWin handle_win(h, IPC::HandleWin::FILE_READ_WRITE); |
192 new IPC::Message(0, 2, IPC::Message::PRIORITY_NORMAL); | 258 IPC::Message* message = new TestHandleWinMsg(100, handle_win, 200); |
193 message->WriteInt(message_index_++); | |
194 scoped_refptr<IPC::internal::HandleAttachmentWin> attachment( | |
195 new IPC::internal::HandleAttachmentWin(h, IPC::HandleWin::DUPLICATE)); | |
196 ASSERT_TRUE(message->WriteAttachment(attachment)); | |
197 sender()->Send(message); | 259 sender()->Send(message); |
198 } | 260 } |
199 | 261 |
200 ProxyListener* get_proxy_listener() { return &proxy_listener_; } | 262 ProxyListener* get_proxy_listener() { return &proxy_listener_; } |
201 IPC::AttachmentBrokerUnprivilegedWin* get_broker() { return broker_.get(); } | 263 IPC::AttachmentBrokerUnprivilegedWin* get_broker() { return broker_.get(); } |
202 MockObserver* get_observer() { return &observer_; } | 264 MockObserver* get_observer() { return &observer_; } |
203 | 265 |
204 private: | 266 private: |
205 base::ScopedTempDir temp_dir_; | 267 base::ScopedTempDir temp_dir_; |
206 base::FilePath temp_path_; | 268 base::FilePath temp_path_; |
207 int message_index_; | |
208 ProxyListener proxy_listener_; | 269 ProxyListener proxy_listener_; |
209 scoped_ptr<IPC::AttachmentBrokerUnprivilegedWin> broker_; | 270 scoped_ptr<IPC::AttachmentBrokerUnprivilegedWin> broker_; |
210 MockObserver observer_; | 271 MockObserver observer_; |
211 }; | 272 }; |
212 | 273 |
213 // A broker which always sets the current process as the destination process | 274 // A broker which always sets the current process as the destination process |
214 // for attachments. | 275 // for attachments. |
215 class MockBroker : public IPC::AttachmentBrokerUnprivilegedWin { | 276 class MockBroker : public IPC::AttachmentBrokerUnprivilegedWin { |
216 public: | 277 public: |
217 MockBroker() {} | 278 MockBroker() {} |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 | 314 |
254 CommonSetUp(); | 315 CommonSetUp(); |
255 ResultListener result_listener; | 316 ResultListener result_listener; |
256 get_proxy_listener()->set_listener(&result_listener); | 317 get_proxy_listener()->set_listener(&result_listener); |
257 | 318 |
258 HANDLE h = CreateTempFile(); | 319 HANDLE h = CreateTempFile(); |
259 HANDLE h2; | 320 HANDLE h2; |
260 BOOL result = ::DuplicateHandle(GetCurrentProcess(), h, GetCurrentProcess(), | 321 BOOL result = ::DuplicateHandle(GetCurrentProcess(), h, GetCurrentProcess(), |
261 &h2, 0, FALSE, DUPLICATE_CLOSE_SOURCE); | 322 &h2, 0, FALSE, DUPLICATE_CLOSE_SOURCE); |
262 ASSERT_TRUE(result); | 323 ASSERT_TRUE(result); |
263 SendMessageWithAttachment(h2); | 324 IPC::HandleWin handle_win(h2, IPC::HandleWin::DUPLICATE); |
| 325 IPC::Message* message = new TestHandleWinMsg(100, handle_win, 200); |
| 326 sender()->Send(message); |
264 base::MessageLoop::current()->Run(); | 327 base::MessageLoop::current()->Run(); |
265 | 328 |
266 // Check the result. | 329 // Check the result. |
267 ASSERT_EQ(ProxyListener::MESSAGE_RECEIVED, | 330 ASSERT_EQ(ProxyListener::MESSAGE_RECEIVED, |
268 get_proxy_listener()->get_reason()); | 331 get_proxy_listener()->get_reason()); |
269 ASSERT_EQ(result_listener.get_result(), RESULT_SUCCESS); | 332 ASSERT_EQ(result_listener.get_result(), RESULT_SUCCESS); |
270 | 333 |
271 CommonTearDown(); | 334 CommonTearDown(); |
272 } | 335 } |
273 | 336 |
(...skipping 25 matching lines...) Expand all Loading... |
299 EXPECT_NE(h2, h); | 362 EXPECT_NE(h2, h); |
300 EXPECT_NE(h2, nullptr); | 363 EXPECT_NE(h2, nullptr); |
301 | 364 |
302 // But it still points to the same file. | 365 // But it still points to the same file. |
303 std::string contents = ReadFromFile(h); | 366 std::string contents = ReadFromFile(h); |
304 EXPECT_EQ(contents, std::string(kDataBuffer)); | 367 EXPECT_EQ(contents, std::string(kDataBuffer)); |
305 | 368 |
306 CommonTearDown(); | 369 CommonTearDown(); |
307 } | 370 } |
308 | 371 |
309 // Similar to SendHandle, except this test uses the HandleWin class. | 372 // Similar to SendHandle, but sends a message with the same handle twice. |
310 TEST_F(IPCAttachmentBrokerPrivilegedWinTest, SendHandleWin) { | 373 TEST_F(IPCAttachmentBrokerPrivilegedWinTest, SendTwoHandles) { |
311 Init("SendHandleWin"); | 374 Init("SendTwoHandles"); |
312 | 375 |
313 CommonSetUp(); | 376 CommonSetUp(); |
314 ResultListener result_listener; | 377 ResultListener result_listener; |
315 get_proxy_listener()->set_listener(&result_listener); | 378 get_proxy_listener()->set_listener(&result_listener); |
316 | 379 |
317 HANDLE h = CreateTempFile(); | 380 HANDLE h = CreateTempFile(); |
318 IPC::HandleWin handle_win(h, IPC::HandleWin::FILE_READ_WRITE); | 381 IPC::HandleWin handle_win1(h, IPC::HandleWin::FILE_READ_WRITE); |
319 IPC::Message* message = new IPC::Message(0, 2, IPC::Message::PRIORITY_NORMAL); | 382 IPC::HandleWin handle_win2(h, IPC::HandleWin::FILE_READ_WRITE); |
320 message->WriteInt(0); | 383 IPC::Message* message = new TestTwoHandleWinMsg(handle_win1, handle_win2); |
321 IPC::ParamTraits<IPC::HandleWin>::Write(message, handle_win); | |
322 sender()->Send(message); | 384 sender()->Send(message); |
323 base::MessageLoop::current()->Run(); | 385 base::MessageLoop::current()->Run(); |
324 | 386 |
325 // Check the result. | 387 // Check the result. |
326 ASSERT_EQ(ProxyListener::MESSAGE_RECEIVED, | 388 ASSERT_EQ(ProxyListener::MESSAGE_RECEIVED, |
327 get_proxy_listener()->get_reason()); | 389 get_proxy_listener()->get_reason()); |
328 ASSERT_EQ(result_listener.get_result(), RESULT_SUCCESS); | 390 ASSERT_EQ(result_listener.get_result(), RESULT_SUCCESS); |
329 | 391 |
330 CommonTearDown(); | 392 CommonTearDown(); |
331 } | 393 } |
332 | 394 |
333 using OnMessageReceivedCallback = | 395 // Similar to SendHandle, but sends the same message twice. |
334 void (*)(MockObserver* observer, | 396 TEST_F(IPCAttachmentBrokerPrivilegedWinTest, SendHandleTwice) { |
335 IPC::AttachmentBrokerPrivilegedWin* broker, | 397 Init("SendHandleTwice"); |
336 IPC::Sender* sender); | 398 |
| 399 CommonSetUp(); |
| 400 ResultListener result_listener; |
| 401 get_proxy_listener()->set_listener(&result_listener); |
| 402 |
| 403 HANDLE h = CreateTempFile(); |
| 404 SendMessageWithAttachment(h); |
| 405 SendMessageWithAttachment(h); |
| 406 base::MessageLoop::current()->Run(); |
| 407 |
| 408 // Check the result. |
| 409 ASSERT_EQ(ProxyListener::MESSAGE_RECEIVED, |
| 410 get_proxy_listener()->get_reason()); |
| 411 ASSERT_EQ(result_listener.get_result(), RESULT_SUCCESS); |
| 412 |
| 413 CommonTearDown(); |
| 414 } |
| 415 |
| 416 using OnMessageReceivedCallback = void (*)(IPC::Sender* sender, |
| 417 const IPC::Message& message); |
337 | 418 |
338 int CommonPrivilegedProcessMain(OnMessageReceivedCallback callback, | 419 int CommonPrivilegedProcessMain(OnMessageReceivedCallback callback, |
339 const char* channel_name) { | 420 const char* channel_name) { |
| 421 LOG(INFO) << "Privileged process start."; |
340 base::MessageLoopForIO main_message_loop; | 422 base::MessageLoopForIO main_message_loop; |
341 ProxyListener listener; | 423 ProxyListener listener; |
342 | 424 |
343 // Set up IPC channel. | 425 // Set up IPC channel. |
344 IPC::AttachmentBrokerPrivilegedWin broker; | 426 IPC::AttachmentBrokerPrivilegedWin broker; |
345 listener.set_listener(&broker); | |
346 scoped_ptr<IPC::Channel> channel(IPC::Channel::CreateClient( | 427 scoped_ptr<IPC::Channel> channel(IPC::Channel::CreateClient( |
347 IPCTestBase::GetChannelName(channel_name), &listener, &broker)); | 428 IPCTestBase::GetChannelName(channel_name), &listener, &broker)); |
348 broker.RegisterCommunicationChannel(channel.get()); | 429 broker.RegisterCommunicationChannel(channel.get()); |
349 CHECK(channel->Connect()); | 430 CHECK(channel->Connect()); |
350 | 431 |
351 MockObserver observer; | |
352 broker.AddObserver(&observer); | |
353 | |
354 while (true) { | 432 while (true) { |
| 433 LOG(INFO) << "Privileged process spinning run loop."; |
355 base::MessageLoop::current()->Run(); | 434 base::MessageLoop::current()->Run(); |
356 ProxyListener::Reason reason = listener.get_reason(); | 435 ProxyListener::Reason reason = listener.get_reason(); |
357 if (reason == ProxyListener::CHANNEL_ERROR) | 436 if (reason == ProxyListener::CHANNEL_ERROR) |
358 break; | 437 break; |
359 | 438 |
360 callback(&observer, &broker, channel.get()); | 439 while (listener.has_message()) { |
| 440 LOG(INFO) << "Privileged process running callback."; |
| 441 callback(channel.get(), listener.get_first_message()); |
| 442 LOG(INFO) << "Privileged process finishing callback."; |
| 443 listener.pop_first_message(); |
| 444 } |
361 } | 445 } |
362 | 446 |
| 447 LOG(INFO) << "Privileged process end."; |
363 return 0; | 448 return 0; |
364 } | 449 } |
365 | 450 |
366 void SendHandleCallback(MockObserver* observer, | 451 void SendHandleCallback(IPC::Sender* sender, const IPC::Message& message) { |
367 IPC::AttachmentBrokerPrivilegedWin* broker, | 452 bool success = CheckContentsOfTestMessage(message); |
368 IPC::Sender* sender) { | |
369 IPC::BrokerableAttachment::AttachmentId* id = observer->get_id(); | |
370 scoped_refptr<IPC::BrokerableAttachment> received_attachment; | |
371 broker->GetAttachmentWithId(*id, &received_attachment); | |
372 | |
373 // Check that it's the expected handle. | |
374 bool success = CheckContentsOfBrokeredAttachment(received_attachment); | |
375 | |
376 SendControlMessage(sender, success); | 453 SendControlMessage(sender, success); |
377 } | 454 } |
378 | 455 |
379 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendHandle) { | 456 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendHandle) { |
380 return CommonPrivilegedProcessMain(&SendHandleCallback, "SendHandle"); | 457 return CommonPrivilegedProcessMain(&SendHandleCallback, "SendHandle"); |
381 } | 458 } |
382 | 459 |
383 void SendHandleWithoutPermissionsCallback( | 460 void SendHandleWithoutPermissionsCallback(IPC::Sender* sender, |
384 MockObserver* observer, | 461 const IPC::Message& message) { |
385 IPC::AttachmentBrokerPrivilegedWin* broker, | 462 HANDLE h = GetHandleFromTestHandleWinMsg(message); |
386 IPC::Sender* sender) { | |
387 IPC::BrokerableAttachment::AttachmentId* id = observer->get_id(); | |
388 scoped_refptr<IPC::BrokerableAttachment> received_attachment; | |
389 broker->GetAttachmentWithId(*id, &received_attachment); | |
390 | |
391 // Check that it's the expected handle. | |
392 HANDLE h = GetHandleFromBrokeredAttachment(received_attachment); | |
393 if (h != nullptr) { | 463 if (h != nullptr) { |
394 SetFilePointer(h, 0, nullptr, FILE_BEGIN); | 464 SetFilePointer(h, 0, nullptr, FILE_BEGIN); |
395 | 465 |
396 char buffer[100]; | 466 char buffer[100]; |
397 DWORD bytes_read; | 467 DWORD bytes_read; |
398 BOOL success = | 468 BOOL success = |
399 ::ReadFile(h, buffer, static_cast<DWORD>(strlen(kDataBuffer)), | 469 ::ReadFile(h, buffer, static_cast<DWORD>(strlen(kDataBuffer)), |
400 &bytes_read, nullptr); | 470 &bytes_read, nullptr); |
401 if (!success && GetLastError() == ERROR_ACCESS_DENIED) { | 471 if (!success && GetLastError() == ERROR_ACCESS_DENIED) { |
402 SendControlMessage(sender, true); | 472 SendControlMessage(sender, true); |
403 return; | 473 return; |
404 } | 474 } |
405 } | 475 } |
406 | 476 |
407 SendControlMessage(sender, false); | 477 SendControlMessage(sender, false); |
408 } | 478 } |
409 | 479 |
410 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendHandleWithoutPermissions) { | 480 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendHandleWithoutPermissions) { |
411 return CommonPrivilegedProcessMain(&SendHandleWithoutPermissionsCallback, | 481 return CommonPrivilegedProcessMain(&SendHandleWithoutPermissionsCallback, |
412 "SendHandleWithoutPermissions"); | 482 "SendHandleWithoutPermissions"); |
413 } | 483 } |
414 | 484 |
415 void SendHandleToSelfCallback(MockObserver* observer, | 485 void SendHandleToSelfCallback(IPC::Sender* sender, const IPC::Message&) { |
416 IPC::AttachmentBrokerPrivilegedWin* broker, | |
417 IPC::Sender* sender) { | |
418 // Do nothing special. The default behavior already runs the | 486 // Do nothing special. The default behavior already runs the |
419 // AttachmentBrokerPrivilegedWin. | 487 // AttachmentBrokerPrivilegedWin. |
420 } | 488 } |
421 | 489 |
422 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendHandleToSelf) { | 490 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendHandleToSelf) { |
423 return CommonPrivilegedProcessMain(&SendHandleToSelfCallback, | 491 return CommonPrivilegedProcessMain(&SendHandleToSelfCallback, |
424 "SendHandleToSelf"); | 492 "SendHandleToSelf"); |
425 } | 493 } |
426 | 494 |
427 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendHandleWin) { | 495 void SendTwoHandlesCallback(IPC::Sender* sender, const IPC::Message& message) { |
428 return CommonPrivilegedProcessMain(&SendHandleCallback, "SendHandleWin"); | 496 // Check for two handles. |
| 497 HANDLE h1 = GetHandleFromTestTwoHandleWinMsg(message, 0); |
| 498 HANDLE h2 = GetHandleFromTestTwoHandleWinMsg(message, 1); |
| 499 if (h1 == nullptr || h2 == nullptr) { |
| 500 SendControlMessage(sender, false); |
| 501 return; |
| 502 } |
| 503 |
| 504 // Check that their contents are correct. |
| 505 std::string contents1 = ReadFromFile(h1); |
| 506 std::string contents2 = ReadFromFile(h2); |
| 507 if (contents1 != std::string(kDataBuffer) || |
| 508 contents2 != std::string(kDataBuffer)) { |
| 509 SendControlMessage(sender, false); |
| 510 return; |
| 511 } |
| 512 |
| 513 // Check that the handles point to the same file. |
| 514 const char text[] = "katy perry"; |
| 515 DWORD bytes_written = 0; |
| 516 SetFilePointer(h1, 0, nullptr, FILE_BEGIN); |
| 517 BOOL success = ::WriteFile(h1, text, static_cast<DWORD>(strlen(text)), |
| 518 &bytes_written, nullptr); |
| 519 if (!success) { |
| 520 SendControlMessage(sender, false); |
| 521 return; |
| 522 } |
| 523 |
| 524 SetFilePointer(h2, 0, nullptr, FILE_BEGIN); |
| 525 char buffer[100]; |
| 526 DWORD bytes_read; |
| 527 success = ::ReadFile(h2, buffer, static_cast<DWORD>(strlen(text)), |
| 528 &bytes_read, nullptr); |
| 529 if (!success) { |
| 530 SendControlMessage(sender, false); |
| 531 return; |
| 532 } |
| 533 success = std::string(buffer, bytes_read) == std::string(text); |
| 534 SendControlMessage(sender, success); |
| 535 } |
| 536 |
| 537 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendTwoHandles) { |
| 538 return CommonPrivilegedProcessMain(&SendTwoHandlesCallback, "SendTwoHandles"); |
| 539 } |
| 540 |
| 541 void SendHandleTwiceCallback(IPC::Sender* sender, const IPC::Message& message) { |
| 542 // We expect the same message twice. |
| 543 static int i = 0; |
| 544 static bool success = true; |
| 545 success &= CheckContentsOfTestMessage(message); |
| 546 if (i == 0) { |
| 547 LOG(INFO) << "Received first message."; |
| 548 ++i; |
| 549 } else { |
| 550 LOG(INFO) << "Received second message."; |
| 551 SendControlMessage(sender, success); |
| 552 } |
| 553 } |
| 554 |
| 555 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendHandleTwice) { |
| 556 return CommonPrivilegedProcessMain(&SendHandleTwiceCallback, |
| 557 "SendHandleTwice"); |
429 } | 558 } |
430 | 559 |
431 } // namespace | 560 } // namespace |
OLD | NEW |