| 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) |
| 39 return nullptr; | 40 return nullptr; |
| 40 if (attachment->GetBrokerableType() != IPC::BrokerableAttachment::WIN_HANDLE) | 41 if (attachment->GetBrokerableType() != IPC::BrokerableAttachment::WIN_HANDLE) |
| 41 return nullptr; | 42 return nullptr; |
| 42 IPC::internal::HandleAttachmentWin* received_handle_attachment = | 43 IPC::internal::HandleAttachmentWin* received_handle_attachment = |
| 43 static_cast<IPC::internal::HandleAttachmentWin*>(attachment.get()); | 44 static_cast<IPC::internal::HandleAttachmentWin*>(attachment.get()); |
| 44 return received_handle_attachment->get_handle(); | 45 return received_handle_attachment->get_handle(); |
| 45 } | 46 } |
| 46 | 47 |
| 47 // Returns true if |attachment| is a file HANDLE whose contents is | 48 // |message| must be deserializable as a TestHandleWinMsg. Returns the HANDLE, |
| 48 // |kDataBuffer|. | 49 // or nullptr if deserialization failed. |
| 49 bool CheckContentsOfBrokeredAttachment( | 50 HANDLE GetHandleFromTestHandleWinMsg(const IPC::Message& message) { |
| 50 const scoped_refptr<IPC::BrokerableAttachment>& attachment) { | 51 // Expect a message with a brokered attachment. |
| 51 HANDLE h = GetHandleFromBrokeredAttachment(attachment); | 52 if (!message.HasBrokerableAttachments()) |
| 53 return nullptr; |
| 54 |
| 55 TestHandleWinMsg::Schema::Param p; |
| 56 bool success = TestHandleWinMsg::Read(&message, &p); |
| 57 if (!success) |
| 58 return nullptr; |
| 59 |
| 60 IPC::HandleWin handle_win = base::get<1>(p); |
| 61 return handle_win.get_handle(); |
| 62 } |
| 63 |
| 64 // |message| must be deserializable as a TestTwoHandleWinMsg. Returns the |
| 65 // HANDLE, or nullptr if deserialization failed. |
| 66 HANDLE GetHandleFromTestTwoHandleWinMsg(const IPC::Message& message, |
| 67 int index) { |
| 68 // Expect a message with a brokered attachment. |
| 69 if (!message.HasBrokerableAttachments()) |
| 70 return nullptr; |
| 71 |
| 72 TestTwoHandleWinMsg::Schema::Param p; |
| 73 bool success = TestTwoHandleWinMsg::Read(&message, &p); |
| 74 if (!success) |
| 75 return nullptr; |
| 76 |
| 77 IPC::HandleWin handle_win; |
| 78 if (index == 0) |
| 79 handle_win = base::get<0>(p); |
| 80 else if (index == 1) |
| 81 handle_win = base::get<1>(p); |
| 82 return handle_win.get_handle(); |
| 83 } |
| 84 |
| 85 // |message| must be deserializable as a TestHandleWinMsg. Returns true if the |
| 86 // attached file HANDLE has contents |kDataBuffer|. |
| 87 bool CheckContentsOfTestMessage(const IPC::Message& message) { |
| 88 HANDLE h = GetHandleFromTestHandleWinMsg(message); |
| 52 if (h == nullptr) | 89 if (h == nullptr) |
| 53 return false; | 90 return false; |
| 54 | 91 |
| 55 std::string contents = ReadFromFile(h); | 92 std::string contents = ReadFromFile(h); |
| 56 return contents == std::string(kDataBuffer); | 93 return contents == std::string(kDataBuffer); |
| 57 } | 94 } |
| 58 | 95 |
| 59 enum TestResult { | 96 enum TestResult { |
| 60 RESULT_UNKNOWN, | 97 RESULT_UNKNOWN, |
| 61 RESULT_SUCCESS, | 98 RESULT_SUCCESS, |
| (...skipping 19 matching lines...) Expand all Loading... |
| 81 IPC::BrokerableAttachment::AttachmentId* get_id() { return &id_; } | 118 IPC::BrokerableAttachment::AttachmentId* get_id() { return &id_; } |
| 82 | 119 |
| 83 private: | 120 private: |
| 84 IPC::BrokerableAttachment::AttachmentId id_; | 121 IPC::BrokerableAttachment::AttachmentId id_; |
| 85 }; | 122 }; |
| 86 | 123 |
| 87 // Forwards all messages to |listener_|. Quits the message loop after a | 124 // Forwards all messages to |listener_|. Quits the message loop after a |
| 88 // message is received, or the channel has an error. | 125 // message is received, or the channel has an error. |
| 89 class ProxyListener : public IPC::Listener { | 126 class ProxyListener : public IPC::Listener { |
| 90 public: | 127 public: |
| 91 ProxyListener() : reason_(MESSAGE_RECEIVED) {} | 128 ProxyListener() : listener_(nullptr), reason_(MESSAGE_RECEIVED) {} |
| 92 ~ProxyListener() override {} | 129 ~ProxyListener() override {} |
| 93 | 130 |
| 94 // The reason for exiting the message loop. | 131 // The reason for exiting the message loop. |
| 95 enum Reason { MESSAGE_RECEIVED, CHANNEL_ERROR }; | 132 enum Reason { MESSAGE_RECEIVED, CHANNEL_ERROR }; |
| 96 | 133 |
| 97 bool OnMessageReceived(const IPC::Message& message) override { | 134 bool OnMessageReceived(const IPC::Message& message) override { |
| 98 bool result = listener_->OnMessageReceived(message); | 135 bool result = false; |
| 136 if (listener_) |
| 137 result = listener_->OnMessageReceived(message); |
| 99 reason_ = MESSAGE_RECEIVED; | 138 reason_ = MESSAGE_RECEIVED; |
| 100 base::MessageLoop::current()->Quit(); | 139 message_ = message; |
| 140 base::MessageLoop::current()->QuitNow(); |
| 101 return result; | 141 return result; |
| 102 } | 142 } |
| 103 | 143 |
| 104 void OnChannelError() override { | 144 void OnChannelError() override { |
| 105 reason_ = CHANNEL_ERROR; | 145 reason_ = CHANNEL_ERROR; |
| 106 base::MessageLoop::current()->Quit(); | 146 base::MessageLoop::current()->QuitNow(); |
| 107 } | 147 } |
| 108 | 148 |
| 109 void set_listener(IPC::Listener* listener) { listener_ = listener; } | 149 void set_listener(IPC::Listener* listener) { listener_ = listener; } |
| 110 Reason get_reason() { return reason_; } | 150 Reason get_reason() { return reason_; } |
| 151 IPC::Message get_message() { return message_; } |
| 111 | 152 |
| 112 private: | 153 private: |
| 113 IPC::Listener* listener_; | 154 IPC::Listener* listener_; |
| 114 Reason reason_; | 155 Reason reason_; |
| 156 IPC::Message message_; |
| 115 }; | 157 }; |
| 116 | 158 |
| 117 // Waits for a result to be sent over the channel. Quits the message loop | 159 // 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. | 160 // after a message is received, or the channel has an error. |
| 119 class ResultListener : public IPC::Listener { | 161 class ResultListener : public IPC::Listener { |
| 120 public: | 162 public: |
| 121 ResultListener() : result_(RESULT_UNKNOWN) {} | 163 ResultListener() : result_(RESULT_UNKNOWN) {} |
| 122 ~ResultListener() override {} | 164 ~ResultListener() override {} |
| 123 | 165 |
| 124 bool OnMessageReceived(const IPC::Message& message) override { | 166 bool OnMessageReceived(const IPC::Message& message) override { |
| 125 base::PickleIterator iter(message); | 167 base::PickleIterator iter(message); |
| 126 | 168 |
| 127 int result; | 169 int result; |
| 128 EXPECT_TRUE(iter.ReadInt(&result)); | 170 EXPECT_TRUE(iter.ReadInt(&result)); |
| 129 result_ = static_cast<TestResult>(result); | 171 result_ = static_cast<TestResult>(result); |
| 130 return true; | 172 return true; |
| 131 } | 173 } |
| 132 | 174 |
| 133 TestResult get_result() { return result_; } | 175 TestResult get_result() { return result_; } |
| 134 | 176 |
| 135 private: | 177 private: |
| 136 TestResult result_; | 178 TestResult result_; |
| 137 }; | 179 }; |
| 138 | 180 |
| 139 // The parent process acts as an unprivileged process. The forked process acts | 181 // The parent process acts as an unprivileged process. The forked process acts |
| 140 // as the privileged process. | 182 // as the privileged process. |
| 141 class IPCAttachmentBrokerPrivilegedWinTest : public IPCTestBase { | 183 class IPCAttachmentBrokerPrivilegedWinTest : public IPCTestBase { |
| 142 public: | 184 public: |
| 143 IPCAttachmentBrokerPrivilegedWinTest() : message_index_(0) {} | 185 IPCAttachmentBrokerPrivilegedWinTest() {} |
| 144 ~IPCAttachmentBrokerPrivilegedWinTest() override {} | 186 ~IPCAttachmentBrokerPrivilegedWinTest() override {} |
| 145 | 187 |
| 146 void SetUp() override { | 188 void SetUp() override { |
| 147 IPCTestBase::SetUp(); | 189 IPCTestBase::SetUp(); |
| 148 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 190 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 149 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &temp_path_)); | 191 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &temp_path_)); |
| 150 } | 192 } |
| 151 | 193 |
| 152 void TearDown() override { IPCTestBase::TearDown(); } | 194 void TearDown() override { IPCTestBase::TearDown(); } |
| 153 | 195 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 181 static_cast<int>(strlen(kDataBuffer)))); | 223 static_cast<int>(strlen(kDataBuffer)))); |
| 182 | 224 |
| 183 HANDLE h = | 225 HANDLE h = |
| 184 CreateFile(temp_path_.value().c_str(), GENERIC_READ | GENERIC_WRITE, 0, | 226 CreateFile(temp_path_.value().c_str(), GENERIC_READ | GENERIC_WRITE, 0, |
| 185 nullptr, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); | 227 nullptr, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); |
| 186 EXPECT_NE(h, INVALID_HANDLE_VALUE); | 228 EXPECT_NE(h, INVALID_HANDLE_VALUE); |
| 187 return h; | 229 return h; |
| 188 } | 230 } |
| 189 | 231 |
| 190 void SendMessageWithAttachment(HANDLE h) { | 232 void SendMessageWithAttachment(HANDLE h) { |
| 191 IPC::Message* message = | 233 IPC::HandleWin handle_win(h, IPC::HandleWin::FILE_READ_WRITE); |
| 192 new IPC::Message(0, 2, IPC::Message::PRIORITY_NORMAL); | 234 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); | 235 sender()->Send(message); |
| 198 } | 236 } |
| 199 | 237 |
| 200 ProxyListener* get_proxy_listener() { return &proxy_listener_; } | 238 ProxyListener* get_proxy_listener() { return &proxy_listener_; } |
| 201 IPC::AttachmentBrokerUnprivilegedWin* get_broker() { return broker_.get(); } | 239 IPC::AttachmentBrokerUnprivilegedWin* get_broker() { return broker_.get(); } |
| 202 MockObserver* get_observer() { return &observer_; } | 240 MockObserver* get_observer() { return &observer_; } |
| 203 | 241 |
| 204 private: | 242 private: |
| 205 base::ScopedTempDir temp_dir_; | 243 base::ScopedTempDir temp_dir_; |
| 206 base::FilePath temp_path_; | 244 base::FilePath temp_path_; |
| 207 int message_index_; | |
| 208 ProxyListener proxy_listener_; | 245 ProxyListener proxy_listener_; |
| 209 scoped_ptr<IPC::AttachmentBrokerUnprivilegedWin> broker_; | 246 scoped_ptr<IPC::AttachmentBrokerUnprivilegedWin> broker_; |
| 210 MockObserver observer_; | 247 MockObserver observer_; |
| 211 }; | 248 }; |
| 212 | 249 |
| 213 // A broker which always sets the current process as the destination process | 250 // A broker which always sets the current process as the destination process |
| 214 // for attachments. | 251 // for attachments. |
| 215 class MockBroker : public IPC::AttachmentBrokerUnprivilegedWin { | 252 class MockBroker : public IPC::AttachmentBrokerUnprivilegedWin { |
| 216 public: | 253 public: |
| 217 MockBroker() {} | 254 MockBroker() {} |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 253 | 290 |
| 254 CommonSetUp(); | 291 CommonSetUp(); |
| 255 ResultListener result_listener; | 292 ResultListener result_listener; |
| 256 get_proxy_listener()->set_listener(&result_listener); | 293 get_proxy_listener()->set_listener(&result_listener); |
| 257 | 294 |
| 258 HANDLE h = CreateTempFile(); | 295 HANDLE h = CreateTempFile(); |
| 259 HANDLE h2; | 296 HANDLE h2; |
| 260 BOOL result = ::DuplicateHandle(GetCurrentProcess(), h, GetCurrentProcess(), | 297 BOOL result = ::DuplicateHandle(GetCurrentProcess(), h, GetCurrentProcess(), |
| 261 &h2, 0, FALSE, DUPLICATE_CLOSE_SOURCE); | 298 &h2, 0, FALSE, DUPLICATE_CLOSE_SOURCE); |
| 262 ASSERT_TRUE(result); | 299 ASSERT_TRUE(result); |
| 263 SendMessageWithAttachment(h2); | 300 IPC::HandleWin handle_win(h2, IPC::HandleWin::DUPLICATE); |
| 301 IPC::Message* message = new TestHandleWinMsg(100, handle_win, 200); |
| 302 sender()->Send(message); |
| 264 base::MessageLoop::current()->Run(); | 303 base::MessageLoop::current()->Run(); |
| 265 | 304 |
| 266 // Check the result. | 305 // Check the result. |
| 267 ASSERT_EQ(ProxyListener::MESSAGE_RECEIVED, | 306 ASSERT_EQ(ProxyListener::MESSAGE_RECEIVED, |
| 268 get_proxy_listener()->get_reason()); | 307 get_proxy_listener()->get_reason()); |
| 269 ASSERT_EQ(result_listener.get_result(), RESULT_SUCCESS); | 308 ASSERT_EQ(result_listener.get_result(), RESULT_SUCCESS); |
| 270 | 309 |
| 271 CommonTearDown(); | 310 CommonTearDown(); |
| 272 } | 311 } |
| 273 | 312 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 299 EXPECT_NE(h2, h); | 338 EXPECT_NE(h2, h); |
| 300 EXPECT_NE(h2, nullptr); | 339 EXPECT_NE(h2, nullptr); |
| 301 | 340 |
| 302 // But it still points to the same file. | 341 // But it still points to the same file. |
| 303 std::string contents = ReadFromFile(h); | 342 std::string contents = ReadFromFile(h); |
| 304 EXPECT_EQ(contents, std::string(kDataBuffer)); | 343 EXPECT_EQ(contents, std::string(kDataBuffer)); |
| 305 | 344 |
| 306 CommonTearDown(); | 345 CommonTearDown(); |
| 307 } | 346 } |
| 308 | 347 |
| 309 // Similar to SendHandle, except this test uses the HandleWin class. | 348 // Similar to SendHandle, but sends a message with the same handle twice. |
| 310 TEST_F(IPCAttachmentBrokerPrivilegedWinTest, SendHandleWin) { | 349 TEST_F(IPCAttachmentBrokerPrivilegedWinTest, SendTwoHandles) { |
| 311 Init("SendHandleWin"); | 350 Init("SendTwoHandles"); |
| 312 | 351 |
| 313 CommonSetUp(); | 352 CommonSetUp(); |
| 314 ResultListener result_listener; | 353 ResultListener result_listener; |
| 315 get_proxy_listener()->set_listener(&result_listener); | 354 get_proxy_listener()->set_listener(&result_listener); |
| 316 | 355 |
| 317 HANDLE h = CreateTempFile(); | 356 HANDLE h = CreateTempFile(); |
| 318 IPC::HandleWin handle_win(h, IPC::HandleWin::FILE_READ_WRITE); | 357 IPC::HandleWin handle_win1(h, IPC::HandleWin::FILE_READ_WRITE); |
| 319 IPC::Message* message = new IPC::Message(0, 2, IPC::Message::PRIORITY_NORMAL); | 358 IPC::HandleWin handle_win2(h, IPC::HandleWin::FILE_READ_WRITE); |
| 320 message->WriteInt(0); | 359 IPC::Message* message = new TestTwoHandleWinMsg(handle_win1, handle_win2); |
| 321 IPC::ParamTraits<IPC::HandleWin>::Write(message, handle_win); | |
| 322 sender()->Send(message); | 360 sender()->Send(message); |
| 323 base::MessageLoop::current()->Run(); | 361 base::MessageLoop::current()->Run(); |
| 324 | 362 |
| 325 // Check the result. | 363 // Check the result. |
| 326 ASSERT_EQ(ProxyListener::MESSAGE_RECEIVED, | 364 ASSERT_EQ(ProxyListener::MESSAGE_RECEIVED, |
| 327 get_proxy_listener()->get_reason()); | 365 get_proxy_listener()->get_reason()); |
| 328 ASSERT_EQ(result_listener.get_result(), RESULT_SUCCESS); | 366 ASSERT_EQ(result_listener.get_result(), RESULT_SUCCESS); |
| 329 | 367 |
| 330 CommonTearDown(); | 368 CommonTearDown(); |
| 331 } | 369 } |
| 332 | 370 |
| 333 using OnMessageReceivedCallback = | 371 // Similar to SendHandle, but sends the same message twice. |
| 334 void (*)(MockObserver* observer, | 372 TEST_F(IPCAttachmentBrokerPrivilegedWinTest, SendHandleTwice) { |
| 335 IPC::AttachmentBrokerPrivilegedWin* broker, | 373 Init("SendHandleTwice"); |
| 336 IPC::Sender* sender); | 374 |
| 375 CommonSetUp(); |
| 376 ResultListener result_listener; |
| 377 get_proxy_listener()->set_listener(&result_listener); |
| 378 |
| 379 HANDLE h = CreateTempFile(); |
| 380 SendMessageWithAttachment(h); |
| 381 SendMessageWithAttachment(h); |
| 382 base::MessageLoop::current()->Run(); |
| 383 |
| 384 // Check the result. |
| 385 ASSERT_EQ(ProxyListener::MESSAGE_RECEIVED, |
| 386 get_proxy_listener()->get_reason()); |
| 387 ASSERT_EQ(result_listener.get_result(), RESULT_SUCCESS); |
| 388 |
| 389 CommonTearDown(); |
| 390 } |
| 391 |
| 392 using OnMessageReceivedCallback = void (*)(IPC::Sender* sender, |
| 393 const IPC::Message& message); |
| 337 | 394 |
| 338 int CommonPrivilegedProcessMain(OnMessageReceivedCallback callback, | 395 int CommonPrivilegedProcessMain(OnMessageReceivedCallback callback, |
| 339 const char* channel_name) { | 396 const char* channel_name) { |
| 340 base::MessageLoopForIO main_message_loop; | 397 base::MessageLoopForIO main_message_loop; |
| 341 ProxyListener listener; | 398 ProxyListener listener; |
| 342 | 399 |
| 343 // Set up IPC channel. | 400 // Set up IPC channel. |
| 344 IPC::AttachmentBrokerPrivilegedWin broker; | 401 IPC::AttachmentBrokerPrivilegedWin broker; |
| 345 listener.set_listener(&broker); | |
| 346 scoped_ptr<IPC::Channel> channel(IPC::Channel::CreateClient( | 402 scoped_ptr<IPC::Channel> channel(IPC::Channel::CreateClient( |
| 347 IPCTestBase::GetChannelName(channel_name), &listener, &broker)); | 403 IPCTestBase::GetChannelName(channel_name), &listener, &broker)); |
| 348 broker.RegisterCommunicationChannel(channel.get()); | 404 broker.RegisterCommunicationChannel(channel.get()); |
| 349 CHECK(channel->Connect()); | 405 CHECK(channel->Connect()); |
| 350 | 406 |
| 351 MockObserver observer; | |
| 352 broker.AddObserver(&observer); | |
| 353 | |
| 354 while (true) { | 407 while (true) { |
| 355 base::MessageLoop::current()->Run(); | 408 base::MessageLoop::current()->Run(); |
| 356 ProxyListener::Reason reason = listener.get_reason(); | 409 ProxyListener::Reason reason = listener.get_reason(); |
| 357 if (reason == ProxyListener::CHANNEL_ERROR) | 410 if (reason == ProxyListener::CHANNEL_ERROR) |
| 358 break; | 411 break; |
| 359 | 412 |
| 360 callback(&observer, &broker, channel.get()); | 413 callback(channel.get(), listener.get_message()); |
| 361 } | 414 } |
| 362 | 415 |
| 363 return 0; | 416 return 0; |
| 364 } | 417 } |
| 365 | 418 |
| 366 void SendHandleCallback(MockObserver* observer, | 419 void SendHandleCallback(IPC::Sender* sender, const IPC::Message& message) { |
| 367 IPC::AttachmentBrokerPrivilegedWin* broker, | 420 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); | 421 SendControlMessage(sender, success); |
| 377 } | 422 } |
| 378 | 423 |
| 379 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendHandle) { | 424 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendHandle) { |
| 380 return CommonPrivilegedProcessMain(&SendHandleCallback, "SendHandle"); | 425 return CommonPrivilegedProcessMain(&SendHandleCallback, "SendHandle"); |
| 381 } | 426 } |
| 382 | 427 |
| 383 void SendHandleWithoutPermissionsCallback( | 428 void SendHandleWithoutPermissionsCallback(IPC::Sender* sender, |
| 384 MockObserver* observer, | 429 const IPC::Message& message) { |
| 385 IPC::AttachmentBrokerPrivilegedWin* broker, | 430 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) { | 431 if (h != nullptr) { |
| 394 SetFilePointer(h, 0, nullptr, FILE_BEGIN); | 432 SetFilePointer(h, 0, nullptr, FILE_BEGIN); |
| 395 | 433 |
| 396 char buffer[100]; | 434 char buffer[100]; |
| 397 DWORD bytes_read; | 435 DWORD bytes_read; |
| 398 BOOL success = | 436 BOOL success = |
| 399 ::ReadFile(h, buffer, static_cast<DWORD>(strlen(kDataBuffer)), | 437 ::ReadFile(h, buffer, static_cast<DWORD>(strlen(kDataBuffer)), |
| 400 &bytes_read, nullptr); | 438 &bytes_read, nullptr); |
| 401 if (!success && GetLastError() == ERROR_ACCESS_DENIED) { | 439 if (!success && GetLastError() == ERROR_ACCESS_DENIED) { |
| 402 SendControlMessage(sender, true); | 440 SendControlMessage(sender, true); |
| 403 return; | 441 return; |
| 404 } | 442 } |
| 405 } | 443 } |
| 406 | 444 |
| 407 SendControlMessage(sender, false); | 445 SendControlMessage(sender, false); |
| 408 } | 446 } |
| 409 | 447 |
| 410 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendHandleWithoutPermissions) { | 448 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendHandleWithoutPermissions) { |
| 411 return CommonPrivilegedProcessMain(&SendHandleWithoutPermissionsCallback, | 449 return CommonPrivilegedProcessMain(&SendHandleWithoutPermissionsCallback, |
| 412 "SendHandleWithoutPermissions"); | 450 "SendHandleWithoutPermissions"); |
| 413 } | 451 } |
| 414 | 452 |
| 415 void SendHandleToSelfCallback(MockObserver* observer, | 453 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 | 454 // Do nothing special. The default behavior already runs the |
| 419 // AttachmentBrokerPrivilegedWin. | 455 // AttachmentBrokerPrivilegedWin. |
| 420 } | 456 } |
| 421 | 457 |
| 422 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendHandleToSelf) { | 458 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendHandleToSelf) { |
| 423 return CommonPrivilegedProcessMain(&SendHandleToSelfCallback, | 459 return CommonPrivilegedProcessMain(&SendHandleToSelfCallback, |
| 424 "SendHandleToSelf"); | 460 "SendHandleToSelf"); |
| 425 } | 461 } |
| 426 | 462 |
| 427 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendHandleWin) { | 463 void SendTwoHandlesCallback(IPC::Sender* sender, const IPC::Message& message) { |
| 428 return CommonPrivilegedProcessMain(&SendHandleCallback, "SendHandleWin"); | 464 // Check for two handles. |
| 465 HANDLE h1 = GetHandleFromTestTwoHandleWinMsg(message, 0); |
| 466 HANDLE h2 = GetHandleFromTestTwoHandleWinMsg(message, 1); |
| 467 if (h1 == nullptr || h2 == nullptr) { |
| 468 SendControlMessage(sender, false); |
| 469 return; |
| 470 } |
| 471 |
| 472 // Check that their contents are correct. |
| 473 std::string contents1 = ReadFromFile(h1); |
| 474 std::string contents2 = ReadFromFile(h2); |
| 475 if (contents1 != std::string(kDataBuffer) || |
| 476 contents2 != std::string(kDataBuffer)) { |
| 477 SendControlMessage(sender, false); |
| 478 return; |
| 479 } |
| 480 |
| 481 // Check that the handles point to the same file. |
| 482 const char text[] = "katy perry"; |
| 483 DWORD bytes_written = 0; |
| 484 SetFilePointer(h1, 0, nullptr, FILE_BEGIN); |
| 485 BOOL success = ::WriteFile(h1, text, static_cast<DWORD>(strlen(text)), |
| 486 &bytes_written, nullptr); |
| 487 if (!success) { |
| 488 SendControlMessage(sender, false); |
| 489 return; |
| 490 } |
| 491 |
| 492 SetFilePointer(h2, 0, nullptr, FILE_BEGIN); |
| 493 char buffer[100]; |
| 494 DWORD bytes_read; |
| 495 success = ::ReadFile(h2, buffer, static_cast<DWORD>(strlen(text)), |
| 496 &bytes_read, nullptr); |
| 497 if (!success) { |
| 498 SendControlMessage(sender, false); |
| 499 return; |
| 500 } |
| 501 success = std::string(buffer, bytes_read) == std::string(text); |
| 502 SendControlMessage(sender, success); |
| 503 } |
| 504 |
| 505 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendTwoHandles) { |
| 506 return CommonPrivilegedProcessMain(&SendTwoHandlesCallback, "SendTwoHandles"); |
| 507 } |
| 508 |
| 509 void SendHandleTwiceCallback(IPC::Sender* sender, const IPC::Message& message) { |
| 510 // We expect the same message twice. |
| 511 static int i = 0; |
| 512 static bool success = true; |
| 513 success &= CheckContentsOfTestMessage(message); |
| 514 if (i == 0) { |
| 515 ++i; |
| 516 } else { |
| 517 SendControlMessage(sender, success); |
| 518 } |
| 519 } |
| 520 |
| 521 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendHandleTwice) { |
| 522 return CommonPrivilegedProcessMain(&SendHandleTwiceCallback, |
| 523 "SendHandleTwice"); |
| 429 } | 524 } |
| 430 | 525 |
| 431 } // namespace | 526 } // namespace |
| OLD | NEW |