| 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 28 matching lines...) Expand all Loading... |
| 182 static_cast<int>(strlen(kDataBuffer)))); | 248 static_cast<int>(strlen(kDataBuffer)))); |
| 183 | 249 |
| 184 HANDLE h = | 250 HANDLE h = |
| 185 CreateFile(temp_path_.value().c_str(), GENERIC_READ | GENERIC_WRITE, 0, | 251 CreateFile(temp_path_.value().c_str(), GENERIC_READ | GENERIC_WRITE, 0, |
| 186 nullptr, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); | 252 nullptr, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); |
| 187 EXPECT_NE(h, INVALID_HANDLE_VALUE); | 253 EXPECT_NE(h, INVALID_HANDLE_VALUE); |
| 188 return h; | 254 return h; |
| 189 } | 255 } |
| 190 | 256 |
| 191 void SendMessageWithAttachment(HANDLE h) { | 257 void SendMessageWithAttachment(HANDLE h) { |
| 192 IPC::Message* message = | 258 IPC::HandleWin handle_win(h, IPC::HandleWin::FILE_READ_WRITE); |
| 193 new IPC::Message(0, 2, IPC::Message::PRIORITY_NORMAL); | 259 IPC::Message* message = new TestHandleWinMsg(100, handle_win, 200); |
| 194 message->WriteInt(message_index_++); | |
| 195 scoped_refptr<IPC::internal::HandleAttachmentWin> attachment( | |
| 196 new IPC::internal::HandleAttachmentWin(h, IPC::HandleWin::DUPLICATE)); | |
| 197 ASSERT_TRUE(message->WriteAttachment(attachment)); | |
| 198 sender()->Send(message); | 260 sender()->Send(message); |
| 199 } | 261 } |
| 200 | 262 |
| 201 ProxyListener* get_proxy_listener() { return &proxy_listener_; } | 263 ProxyListener* get_proxy_listener() { return &proxy_listener_; } |
| 202 IPC::AttachmentBrokerUnprivilegedWin* get_broker() { return broker_.get(); } | 264 IPC::AttachmentBrokerUnprivilegedWin* get_broker() { return broker_.get(); } |
| 203 MockObserver* get_observer() { return &observer_; } | 265 MockObserver* get_observer() { return &observer_; } |
| 204 | 266 |
| 205 private: | 267 private: |
| 206 base::ScopedTempDir temp_dir_; | 268 base::ScopedTempDir temp_dir_; |
| 207 base::FilePath temp_path_; | 269 base::FilePath temp_path_; |
| 208 int message_index_; | |
| 209 ProxyListener proxy_listener_; | 270 ProxyListener proxy_listener_; |
| 210 scoped_ptr<IPC::AttachmentBrokerUnprivilegedWin> broker_; | 271 scoped_ptr<IPC::AttachmentBrokerUnprivilegedWin> broker_; |
| 211 MockObserver observer_; | 272 MockObserver observer_; |
| 212 }; | 273 }; |
| 213 | 274 |
| 214 // A broker which always sets the current process as the destination process | 275 // A broker which always sets the current process as the destination process |
| 215 // for attachments. | 276 // for attachments. |
| 216 class MockBroker : public IPC::AttachmentBrokerUnprivilegedWin { | 277 class MockBroker : public IPC::AttachmentBrokerUnprivilegedWin { |
| 217 public: | 278 public: |
| 218 MockBroker() {} | 279 MockBroker() {} |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 255 | 316 |
| 256 CommonSetUp(); | 317 CommonSetUp(); |
| 257 ResultListener result_listener; | 318 ResultListener result_listener; |
| 258 get_proxy_listener()->set_listener(&result_listener); | 319 get_proxy_listener()->set_listener(&result_listener); |
| 259 | 320 |
| 260 HANDLE h = CreateTempFile(); | 321 HANDLE h = CreateTempFile(); |
| 261 HANDLE h2; | 322 HANDLE h2; |
| 262 BOOL result = ::DuplicateHandle(GetCurrentProcess(), h, GetCurrentProcess(), | 323 BOOL result = ::DuplicateHandle(GetCurrentProcess(), h, GetCurrentProcess(), |
| 263 &h2, 0, FALSE, DUPLICATE_CLOSE_SOURCE); | 324 &h2, 0, FALSE, DUPLICATE_CLOSE_SOURCE); |
| 264 ASSERT_TRUE(result); | 325 ASSERT_TRUE(result); |
| 265 SendMessageWithAttachment(h2); | 326 IPC::HandleWin handle_win(h2, IPC::HandleWin::DUPLICATE); |
| 327 IPC::Message* message = new TestHandleWinMsg(100, handle_win, 200); |
| 328 sender()->Send(message); |
| 266 base::MessageLoop::current()->Run(); | 329 base::MessageLoop::current()->Run(); |
| 267 | 330 |
| 268 // Check the result. | 331 // Check the result. |
| 269 ASSERT_EQ(ProxyListener::MESSAGE_RECEIVED, | 332 ASSERT_EQ(ProxyListener::MESSAGE_RECEIVED, |
| 270 get_proxy_listener()->get_reason()); | 333 get_proxy_listener()->get_reason()); |
| 271 ASSERT_EQ(result_listener.get_result(), RESULT_SUCCESS); | 334 ASSERT_EQ(result_listener.get_result(), RESULT_SUCCESS); |
| 272 | 335 |
| 273 CommonTearDown(); | 336 CommonTearDown(); |
| 274 } | 337 } |
| 275 | 338 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 301 EXPECT_NE(h2, h); | 364 EXPECT_NE(h2, h); |
| 302 EXPECT_NE(h2, nullptr); | 365 EXPECT_NE(h2, nullptr); |
| 303 | 366 |
| 304 // But it still points to the same file. | 367 // But it still points to the same file. |
| 305 std::string contents = ReadFromFile(h); | 368 std::string contents = ReadFromFile(h); |
| 306 EXPECT_EQ(contents, std::string(kDataBuffer)); | 369 EXPECT_EQ(contents, std::string(kDataBuffer)); |
| 307 | 370 |
| 308 CommonTearDown(); | 371 CommonTearDown(); |
| 309 } | 372 } |
| 310 | 373 |
| 311 // Similar to SendHandle, except this test uses the HandleWin class. | 374 // Similar to SendHandle, but sends a message with two instances of the same |
| 312 TEST_F(IPCAttachmentBrokerPrivilegedWinTest, DISABLED_SendHandleWin) { | 375 // handle. |
| 313 Init("SendHandleWin"); | 376 TEST_F(IPCAttachmentBrokerPrivilegedWinTest, DISABLED_SendTwoHandles) { |
| 377 Init("SendTwoHandles"); |
| 314 | 378 |
| 315 CommonSetUp(); | 379 CommonSetUp(); |
| 316 ResultListener result_listener; | 380 ResultListener result_listener; |
| 317 get_proxy_listener()->set_listener(&result_listener); | 381 get_proxy_listener()->set_listener(&result_listener); |
| 318 | 382 |
| 319 HANDLE h = CreateTempFile(); | 383 HANDLE h = CreateTempFile(); |
| 320 IPC::HandleWin handle_win(h, IPC::HandleWin::FILE_READ_WRITE); | 384 IPC::HandleWin handle_win1(h, IPC::HandleWin::FILE_READ_WRITE); |
| 321 IPC::Message* message = new IPC::Message(0, 2, IPC::Message::PRIORITY_NORMAL); | 385 IPC::HandleWin handle_win2(h, IPC::HandleWin::FILE_READ_WRITE); |
| 322 message->WriteInt(0); | 386 IPC::Message* message = new TestTwoHandleWinMsg(handle_win1, handle_win2); |
| 323 IPC::ParamTraits<IPC::HandleWin>::Write(message, handle_win); | |
| 324 sender()->Send(message); | 387 sender()->Send(message); |
| 325 base::MessageLoop::current()->Run(); | 388 base::MessageLoop::current()->Run(); |
| 326 | 389 |
| 327 // Check the result. | 390 // Check the result. |
| 328 ASSERT_EQ(ProxyListener::MESSAGE_RECEIVED, | 391 ASSERT_EQ(ProxyListener::MESSAGE_RECEIVED, |
| 329 get_proxy_listener()->get_reason()); | 392 get_proxy_listener()->get_reason()); |
| 330 ASSERT_EQ(result_listener.get_result(), RESULT_SUCCESS); | 393 ASSERT_EQ(result_listener.get_result(), RESULT_SUCCESS); |
| 331 | 394 |
| 332 CommonTearDown(); | 395 CommonTearDown(); |
| 333 } | 396 } |
| 334 | 397 |
| 335 using OnMessageReceivedCallback = | 398 // Similar to SendHandle, but sends the same message twice. |
| 336 void (*)(MockObserver* observer, | 399 TEST_F(IPCAttachmentBrokerPrivilegedWinTest, DISABLED_SendHandleTwice) { |
| 337 IPC::AttachmentBrokerPrivilegedWin* broker, | 400 Init("SendHandleTwice"); |
| 338 IPC::Sender* sender); | 401 |
| 402 CommonSetUp(); |
| 403 ResultListener result_listener; |
| 404 get_proxy_listener()->set_listener(&result_listener); |
| 405 |
| 406 HANDLE h = CreateTempFile(); |
| 407 SendMessageWithAttachment(h); |
| 408 SendMessageWithAttachment(h); |
| 409 base::MessageLoop::current()->Run(); |
| 410 |
| 411 // Check the result. |
| 412 ASSERT_EQ(ProxyListener::MESSAGE_RECEIVED, |
| 413 get_proxy_listener()->get_reason()); |
| 414 ASSERT_EQ(result_listener.get_result(), RESULT_SUCCESS); |
| 415 |
| 416 CommonTearDown(); |
| 417 } |
| 418 |
| 419 using OnMessageReceivedCallback = void (*)(IPC::Sender* sender, |
| 420 const IPC::Message& message); |
| 339 | 421 |
| 340 int CommonPrivilegedProcessMain(OnMessageReceivedCallback callback, | 422 int CommonPrivilegedProcessMain(OnMessageReceivedCallback callback, |
| 341 const char* channel_name) { | 423 const char* channel_name) { |
| 424 LOG(INFO) << "Privileged process start."; |
| 342 base::MessageLoopForIO main_message_loop; | 425 base::MessageLoopForIO main_message_loop; |
| 343 ProxyListener listener; | 426 ProxyListener listener; |
| 344 | 427 |
| 345 // Set up IPC channel. | 428 // Set up IPC channel. |
| 346 IPC::AttachmentBrokerPrivilegedWin broker; | 429 IPC::AttachmentBrokerPrivilegedWin broker; |
| 347 IPC::AttachmentBroker::SetGlobal(&broker); | 430 IPC::AttachmentBroker::SetGlobal(&broker); |
| 348 scoped_ptr<IPC::Channel> channel(IPC::Channel::CreateClient( | 431 scoped_ptr<IPC::Channel> channel(IPC::Channel::CreateClient( |
| 349 IPCTestBase::GetChannelName(channel_name), &listener)); | 432 IPCTestBase::GetChannelName(channel_name), &listener)); |
| 350 broker.RegisterCommunicationChannel(channel.get()); | 433 broker.RegisterCommunicationChannel(channel.get()); |
| 351 CHECK(channel->Connect()); | 434 CHECK(channel->Connect()); |
| 352 | 435 |
| 353 MockObserver observer; | |
| 354 broker.AddObserver(&observer); | |
| 355 | |
| 356 while (true) { | 436 while (true) { |
| 437 LOG(INFO) << "Privileged process spinning run loop."; |
| 357 base::MessageLoop::current()->Run(); | 438 base::MessageLoop::current()->Run(); |
| 358 ProxyListener::Reason reason = listener.get_reason(); | 439 ProxyListener::Reason reason = listener.get_reason(); |
| 359 if (reason == ProxyListener::CHANNEL_ERROR) | 440 if (reason == ProxyListener::CHANNEL_ERROR) |
| 360 break; | 441 break; |
| 361 | 442 |
| 362 callback(&observer, &broker, channel.get()); | 443 while (listener.has_message()) { |
| 444 LOG(INFO) << "Privileged process running callback."; |
| 445 callback(channel.get(), listener.get_first_message()); |
| 446 LOG(INFO) << "Privileged process finishing callback."; |
| 447 listener.pop_first_message(); |
| 448 } |
| 363 } | 449 } |
| 364 | 450 |
| 451 LOG(INFO) << "Privileged process end."; |
| 365 return 0; | 452 return 0; |
| 366 } | 453 } |
| 367 | 454 |
| 368 void SendHandleCallback(MockObserver* observer, | 455 void SendHandleCallback(IPC::Sender* sender, const IPC::Message& message) { |
| 369 IPC::AttachmentBrokerPrivilegedWin* broker, | 456 bool success = CheckContentsOfTestMessage(message); |
| 370 IPC::Sender* sender) { | |
| 371 IPC::BrokerableAttachment::AttachmentId* id = observer->get_id(); | |
| 372 scoped_refptr<IPC::BrokerableAttachment> received_attachment; | |
| 373 broker->GetAttachmentWithId(*id, &received_attachment); | |
| 374 | |
| 375 // Check that it's the expected handle. | |
| 376 bool success = CheckContentsOfBrokeredAttachment(received_attachment); | |
| 377 | |
| 378 SendControlMessage(sender, success); | 457 SendControlMessage(sender, success); |
| 379 } | 458 } |
| 380 | 459 |
| 381 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendHandle) { | 460 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendHandle) { |
| 382 return CommonPrivilegedProcessMain(&SendHandleCallback, "SendHandle"); | 461 return CommonPrivilegedProcessMain(&SendHandleCallback, "SendHandle"); |
| 383 } | 462 } |
| 384 | 463 |
| 385 void SendHandleWithoutPermissionsCallback( | 464 void SendHandleWithoutPermissionsCallback(IPC::Sender* sender, |
| 386 MockObserver* observer, | 465 const IPC::Message& message) { |
| 387 IPC::AttachmentBrokerPrivilegedWin* broker, | 466 HANDLE h = GetHandleFromTestHandleWinMsg(message); |
| 388 IPC::Sender* sender) { | |
| 389 IPC::BrokerableAttachment::AttachmentId* id = observer->get_id(); | |
| 390 scoped_refptr<IPC::BrokerableAttachment> received_attachment; | |
| 391 broker->GetAttachmentWithId(*id, &received_attachment); | |
| 392 | |
| 393 // Check that it's the expected handle. | |
| 394 HANDLE h = GetHandleFromBrokeredAttachment(received_attachment); | |
| 395 if (h != nullptr) { | 467 if (h != nullptr) { |
| 396 SetFilePointer(h, 0, nullptr, FILE_BEGIN); | 468 SetFilePointer(h, 0, nullptr, FILE_BEGIN); |
| 397 | 469 |
| 398 char buffer[100]; | 470 char buffer[100]; |
| 399 DWORD bytes_read; | 471 DWORD bytes_read; |
| 400 BOOL success = | 472 BOOL success = |
| 401 ::ReadFile(h, buffer, static_cast<DWORD>(strlen(kDataBuffer)), | 473 ::ReadFile(h, buffer, static_cast<DWORD>(strlen(kDataBuffer)), |
| 402 &bytes_read, nullptr); | 474 &bytes_read, nullptr); |
| 403 if (!success && GetLastError() == ERROR_ACCESS_DENIED) { | 475 if (!success && GetLastError() == ERROR_ACCESS_DENIED) { |
| 404 SendControlMessage(sender, true); | 476 SendControlMessage(sender, true); |
| 405 return; | 477 return; |
| 406 } | 478 } |
| 407 } | 479 } |
| 408 | 480 |
| 409 SendControlMessage(sender, false); | 481 SendControlMessage(sender, false); |
| 410 } | 482 } |
| 411 | 483 |
| 412 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendHandleWithoutPermissions) { | 484 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendHandleWithoutPermissions) { |
| 413 return CommonPrivilegedProcessMain(&SendHandleWithoutPermissionsCallback, | 485 return CommonPrivilegedProcessMain(&SendHandleWithoutPermissionsCallback, |
| 414 "SendHandleWithoutPermissions"); | 486 "SendHandleWithoutPermissions"); |
| 415 } | 487 } |
| 416 | 488 |
| 417 void SendHandleToSelfCallback(MockObserver* observer, | 489 void SendHandleToSelfCallback(IPC::Sender* sender, const IPC::Message&) { |
| 418 IPC::AttachmentBrokerPrivilegedWin* broker, | |
| 419 IPC::Sender* sender) { | |
| 420 // Do nothing special. The default behavior already runs the | 490 // Do nothing special. The default behavior already runs the |
| 421 // AttachmentBrokerPrivilegedWin. | 491 // AttachmentBrokerPrivilegedWin. |
| 422 } | 492 } |
| 423 | 493 |
| 424 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendHandleToSelf) { | 494 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendHandleToSelf) { |
| 425 return CommonPrivilegedProcessMain(&SendHandleToSelfCallback, | 495 return CommonPrivilegedProcessMain(&SendHandleToSelfCallback, |
| 426 "SendHandleToSelf"); | 496 "SendHandleToSelf"); |
| 427 } | 497 } |
| 428 | 498 |
| 429 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendHandleWin) { | 499 void SendTwoHandlesCallback(IPC::Sender* sender, const IPC::Message& message) { |
| 430 return CommonPrivilegedProcessMain(&SendHandleCallback, "SendHandleWin"); | 500 // Check for two handles. |
| 501 HANDLE h1 = GetHandleFromTestTwoHandleWinMsg(message, 0); |
| 502 HANDLE h2 = GetHandleFromTestTwoHandleWinMsg(message, 1); |
| 503 if (h1 == nullptr || h2 == nullptr) { |
| 504 SendControlMessage(sender, false); |
| 505 return; |
| 506 } |
| 507 |
| 508 // Check that their contents are correct. |
| 509 std::string contents1 = ReadFromFile(h1); |
| 510 std::string contents2 = ReadFromFile(h2); |
| 511 if (contents1 != std::string(kDataBuffer) || |
| 512 contents2 != std::string(kDataBuffer)) { |
| 513 SendControlMessage(sender, false); |
| 514 return; |
| 515 } |
| 516 |
| 517 // Check that the handles point to the same file. |
| 518 const char text[] = "katy perry"; |
| 519 DWORD bytes_written = 0; |
| 520 SetFilePointer(h1, 0, nullptr, FILE_BEGIN); |
| 521 BOOL success = ::WriteFile(h1, text, static_cast<DWORD>(strlen(text)), |
| 522 &bytes_written, nullptr); |
| 523 if (!success) { |
| 524 SendControlMessage(sender, false); |
| 525 return; |
| 526 } |
| 527 |
| 528 SetFilePointer(h2, 0, nullptr, FILE_BEGIN); |
| 529 char buffer[100]; |
| 530 DWORD bytes_read; |
| 531 success = ::ReadFile(h2, buffer, static_cast<DWORD>(strlen(text)), |
| 532 &bytes_read, nullptr); |
| 533 if (!success) { |
| 534 SendControlMessage(sender, false); |
| 535 return; |
| 536 } |
| 537 success = std::string(buffer, bytes_read) == std::string(text); |
| 538 SendControlMessage(sender, success); |
| 539 } |
| 540 |
| 541 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendTwoHandles) { |
| 542 return CommonPrivilegedProcessMain(&SendTwoHandlesCallback, "SendTwoHandles"); |
| 543 } |
| 544 |
| 545 void SendHandleTwiceCallback(IPC::Sender* sender, const IPC::Message& message) { |
| 546 // We expect the same message twice. |
| 547 static int i = 0; |
| 548 static bool success = true; |
| 549 success &= CheckContentsOfTestMessage(message); |
| 550 if (i == 0) { |
| 551 LOG(INFO) << "Received first message."; |
| 552 ++i; |
| 553 } else { |
| 554 LOG(INFO) << "Received second message."; |
| 555 SendControlMessage(sender, success); |
| 556 } |
| 557 } |
| 558 |
| 559 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendHandleTwice) { |
| 560 return CommonPrivilegedProcessMain(&SendHandleTwiceCallback, |
| 561 "SendHandleTwice"); |
| 431 } | 562 } |
| 432 | 563 |
| 433 } // namespace | 564 } // namespace |
| OLD | NEW |