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/ipc_listener.h" | 17 #include "ipc/ipc_listener.h" |
17 #include "ipc/ipc_message.h" | 18 #include "ipc/ipc_message.h" |
18 #include "ipc/ipc_test_base.h" | 19 #include "ipc/ipc_test_base.h" |
19 | 20 |
20 namespace { | 21 namespace { |
21 | 22 |
22 const char kDataBuffer[] = "This is some test data to write to the file."; | 23 const char kDataBuffer[] = "This is some test data to write to the file."; |
23 | 24 |
24 // Returns the contents of the file represented by |h| as a std::string. | 25 // Returns the contents of the file represented by |h| as a std::string. |
25 std::string ReadFromFile(HANDLE h) { | 26 std::string ReadFromFile(HANDLE h) { |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
184 nullptr, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); | 185 nullptr, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); |
185 EXPECT_NE(h, INVALID_HANDLE_VALUE); | 186 EXPECT_NE(h, INVALID_HANDLE_VALUE); |
186 return h; | 187 return h; |
187 } | 188 } |
188 | 189 |
189 void SendMessageWithAttachment(HANDLE h) { | 190 void SendMessageWithAttachment(HANDLE h) { |
190 IPC::Message* message = | 191 IPC::Message* message = |
191 new IPC::Message(0, 2, IPC::Message::PRIORITY_NORMAL); | 192 new IPC::Message(0, 2, IPC::Message::PRIORITY_NORMAL); |
192 message->WriteInt(message_index_++); | 193 message->WriteInt(message_index_++); |
193 scoped_refptr<IPC::internal::HandleAttachmentWin> attachment( | 194 scoped_refptr<IPC::internal::HandleAttachmentWin> attachment( |
194 new IPC::internal::HandleAttachmentWin(h)); | 195 new IPC::internal::HandleAttachmentWin(h, IPC::HandleWin::DUPLICATE)); |
195 ASSERT_TRUE(message->WriteAttachment(attachment)); | 196 ASSERT_TRUE(message->WriteAttachment(attachment)); |
196 sender()->Send(message); | 197 sender()->Send(message); |
197 } | 198 } |
198 | 199 |
199 ProxyListener* get_proxy_listener() { return &proxy_listener_; } | 200 ProxyListener* get_proxy_listener() { return &proxy_listener_; } |
200 IPC::AttachmentBrokerUnprivilegedWin* get_broker() { return broker_.get(); } | 201 IPC::AttachmentBrokerUnprivilegedWin* get_broker() { return broker_.get(); } |
201 MockObserver* get_observer() { return &observer_; } | 202 MockObserver* get_observer() { return &observer_; } |
202 | 203 |
203 private: | 204 private: |
204 base::ScopedTempDir temp_dir_; | 205 base::ScopedTempDir temp_dir_; |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
298 EXPECT_NE(h2, h); | 299 EXPECT_NE(h2, h); |
299 EXPECT_NE(h2, nullptr); | 300 EXPECT_NE(h2, nullptr); |
300 | 301 |
301 // But it still points to the same file. | 302 // But it still points to the same file. |
302 std::string contents = ReadFromFile(h); | 303 std::string contents = ReadFromFile(h); |
303 EXPECT_EQ(contents, std::string(kDataBuffer)); | 304 EXPECT_EQ(contents, std::string(kDataBuffer)); |
304 | 305 |
305 CommonTearDown(); | 306 CommonTearDown(); |
306 } | 307 } |
307 | 308 |
| 309 // Similar to SendHandle, except this test uses the HandleWin class. |
| 310 TEST_F(IPCAttachmentBrokerPrivilegedWinTest, SendHandleWin) { |
| 311 Init("SendHandleWin"); |
| 312 |
| 313 CommonSetUp(); |
| 314 ResultListener result_listener; |
| 315 get_proxy_listener()->set_listener(&result_listener); |
| 316 |
| 317 HANDLE h = CreateTempFile(); |
| 318 IPC::HandleWin handle_win(h, IPC::HandleWin::FILE_READ_WRITE); |
| 319 IPC::Message* message = new IPC::Message(0, 2, IPC::Message::PRIORITY_NORMAL); |
| 320 message->WriteInt(0); |
| 321 IPC::ParamTraits<IPC::HandleWin>::Write(message, handle_win); |
| 322 sender()->Send(message); |
| 323 base::MessageLoop::current()->Run(); |
| 324 |
| 325 // Check the result. |
| 326 ASSERT_EQ(ProxyListener::MESSAGE_RECEIVED, |
| 327 get_proxy_listener()->get_reason()); |
| 328 ASSERT_EQ(result_listener.get_result(), RESULT_SUCCESS); |
| 329 |
| 330 CommonTearDown(); |
| 331 } |
| 332 |
308 using OnMessageReceivedCallback = | 333 using OnMessageReceivedCallback = |
309 void (*)(MockObserver* observer, | 334 void (*)(MockObserver* observer, |
310 IPC::AttachmentBrokerPrivilegedWin* broker, | 335 IPC::AttachmentBrokerPrivilegedWin* broker, |
311 IPC::Sender* sender); | 336 IPC::Sender* sender); |
312 | 337 |
313 int CommonPrivilegedProcessMain(OnMessageReceivedCallback callback, | 338 int CommonPrivilegedProcessMain(OnMessageReceivedCallback callback, |
314 const char* channel_name) { | 339 const char* channel_name) { |
315 base::MessageLoopForIO main_message_loop; | 340 base::MessageLoopForIO main_message_loop; |
316 ProxyListener listener; | 341 ProxyListener listener; |
317 | 342 |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
392 IPC::Sender* sender) { | 417 IPC::Sender* sender) { |
393 // Do nothing special. The default behavior already runs the | 418 // Do nothing special. The default behavior already runs the |
394 // AttachmentBrokerPrivilegedWin. | 419 // AttachmentBrokerPrivilegedWin. |
395 } | 420 } |
396 | 421 |
397 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendHandleToSelf) { | 422 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendHandleToSelf) { |
398 return CommonPrivilegedProcessMain(&SendHandleToSelfCallback, | 423 return CommonPrivilegedProcessMain(&SendHandleToSelfCallback, |
399 "SendHandleToSelf"); | 424 "SendHandleToSelf"); |
400 } | 425 } |
401 | 426 |
| 427 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendHandleWin) { |
| 428 return CommonPrivilegedProcessMain(&SendHandleCallback, "SendHandleWin"); |
| 429 } |
| 430 |
402 } // namespace | 431 } // namespace |
OLD | NEW |