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