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