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 <set> | 7 #include <set> |
8 | 8 |
9 #include "ipc/attachment_broker.h" | 9 #include "ipc/attachment_broker.h" |
10 #include "ipc/brokerable_attachment.h" | 10 #include "ipc/brokerable_attachment.h" |
11 #include "ipc/ipc_channel_reader.h" | 11 #include "ipc/ipc_channel_reader.h" |
12 #include "ipc/placeholder_brokerable_attachment.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
14 | 13 |
15 #if USE_ATTACHMENT_BROKER | 14 #if USE_ATTACHMENT_BROKER |
16 namespace IPC { | 15 namespace IPC { |
17 namespace internal { | 16 namespace internal { |
18 | 17 |
19 namespace { | 18 namespace { |
20 | 19 |
21 class MockAttachment : public BrokerableAttachment { | 20 class MockAttachment : public BrokerableAttachment { |
22 public: | 21 public: |
23 MockAttachment() {} | 22 MockAttachment(int internal_state) : internal_state_(internal_state) {} |
24 MockAttachment(BrokerableAttachment::AttachmentId id) | 23 MockAttachment(BrokerableAttachment::AttachmentId id) |
25 : BrokerableAttachment(id) {} | 24 : BrokerableAttachment(id, true), internal_state_(-1) {} |
| 25 |
| 26 void PopulateWithAttachment(const BrokerableAttachment* attachment) override { |
| 27 const MockAttachment* mock_attachment = |
| 28 static_cast<const MockAttachment*>(attachment); |
| 29 internal_state_ = mock_attachment->internal_state_; |
| 30 } |
26 | 31 |
27 #if defined(OS_POSIX) | 32 #if defined(OS_POSIX) |
28 base::PlatformFile TakePlatformFile() override { | 33 base::PlatformFile TakePlatformFile() override { |
29 return base::PlatformFile(); | 34 return base::PlatformFile(); |
30 } | 35 } |
31 #endif // OS_POSIX | 36 #endif // OS_POSIX |
32 | 37 |
33 BrokerableType GetBrokerableType() const override { return WIN_HANDLE; } | 38 BrokerableType GetBrokerableType() const override { return WIN_HANDLE; } |
34 | 39 |
35 private: | 40 private: |
36 ~MockAttachment() override {} | 41 ~MockAttachment() override {} |
| 42 // Internal state differentiates MockAttachments. |
| 43 int internal_state_; |
37 }; | 44 }; |
38 | 45 |
39 class MockAttachmentBroker : public AttachmentBroker { | 46 class MockAttachmentBroker : public AttachmentBroker { |
40 public: | 47 public: |
41 typedef std::set<scoped_refptr<BrokerableAttachment>> AttachmentSet; | 48 typedef std::set<scoped_refptr<BrokerableAttachment>> AttachmentSet; |
42 | 49 |
43 bool SendAttachmentToProcess(const BrokerableAttachment* attachment, | 50 bool SendAttachmentToProcess(const BrokerableAttachment* attachment, |
44 base::ProcessId destination_process) override { | 51 base::ProcessId destination_process) override { |
45 return false; | 52 return false; |
46 } | 53 } |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 Message* last_dispatched_message_; | 98 Message* last_dispatched_message_; |
92 AttachmentBroker* broker_; | 99 AttachmentBroker* broker_; |
93 }; | 100 }; |
94 | 101 |
95 } // namespace | 102 } // namespace |
96 | 103 |
97 TEST(ChannelReaderTest, AttachmentAlreadyBrokered) { | 104 TEST(ChannelReaderTest, AttachmentAlreadyBrokered) { |
98 MockAttachmentBroker broker; | 105 MockAttachmentBroker broker; |
99 MockChannelReader reader; | 106 MockChannelReader reader; |
100 reader.set_broker(&broker); | 107 reader.set_broker(&broker); |
101 scoped_refptr<MockAttachment> attachment(new MockAttachment); | 108 scoped_refptr<MockAttachment> attachment(new MockAttachment(5)); |
102 broker.AddAttachment(attachment); | 109 broker.AddAttachment(attachment); |
103 | 110 |
104 Message* m = new Message; | 111 Message* m = new Message; |
105 PlaceholderBrokerableAttachment* needs_brokering_attachment = | 112 MockAttachment* needs_brokering_attachment = |
106 new PlaceholderBrokerableAttachment(attachment->GetIdentifier()); | 113 new MockAttachment(attachment->GetIdentifier()); |
107 EXPECT_TRUE(m->WriteAttachment(needs_brokering_attachment)); | 114 EXPECT_TRUE(m->WriteAttachment(needs_brokering_attachment)); |
108 reader.AddMessageForDispatch(m); | 115 reader.AddMessageForDispatch(m); |
109 EXPECT_EQ(ChannelReader::DISPATCH_FINISHED, reader.DispatchMessages()); | 116 EXPECT_EQ(ChannelReader::DISPATCH_FINISHED, reader.DispatchMessages()); |
110 EXPECT_EQ(m, reader.get_last_dispatched_message()); | 117 EXPECT_EQ(m, reader.get_last_dispatched_message()); |
111 } | 118 } |
112 | 119 |
113 TEST(ChannelReaderTest, AttachmentNotYetBrokered) { | 120 TEST(ChannelReaderTest, AttachmentNotYetBrokered) { |
114 MockAttachmentBroker broker; | 121 MockAttachmentBroker broker; |
115 MockChannelReader reader; | 122 MockChannelReader reader; |
116 reader.set_broker(&broker); | 123 reader.set_broker(&broker); |
117 scoped_refptr<MockAttachment> attachment(new MockAttachment); | 124 scoped_refptr<MockAttachment> attachment(new MockAttachment(5)); |
118 | 125 |
119 Message* m = new Message; | 126 Message* m = new Message; |
120 PlaceholderBrokerableAttachment* needs_brokering_attachment = | 127 MockAttachment* needs_brokering_attachment = |
121 new PlaceholderBrokerableAttachment(attachment->GetIdentifier()); | 128 new MockAttachment(attachment->GetIdentifier()); |
122 EXPECT_TRUE(m->WriteAttachment(needs_brokering_attachment)); | 129 EXPECT_TRUE(m->WriteAttachment(needs_brokering_attachment)); |
123 reader.AddMessageForDispatch(m); | 130 reader.AddMessageForDispatch(m); |
124 EXPECT_EQ(ChannelReader::DISPATCH_WAITING_ON_BROKER, | 131 EXPECT_EQ(ChannelReader::DISPATCH_WAITING_ON_BROKER, |
125 reader.DispatchMessages()); | 132 reader.DispatchMessages()); |
126 EXPECT_EQ(nullptr, reader.get_last_dispatched_message()); | 133 EXPECT_EQ(nullptr, reader.get_last_dispatched_message()); |
127 | 134 |
128 broker.AddAttachment(attachment); | 135 broker.AddAttachment(attachment); |
129 EXPECT_EQ(m, reader.get_last_dispatched_message()); | 136 EXPECT_EQ(m, reader.get_last_dispatched_message()); |
130 } | 137 } |
131 | 138 |
132 } // namespace internal | 139 } // namespace internal |
133 } // namespace IPC | 140 } // namespace IPC |
134 #endif // USE_ATTACHMENT_BROKER | 141 #endif // USE_ATTACHMENT_BROKER |
OLD | NEW |