OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "build/build_config.h" |
| 6 |
| 7 #include <set> |
| 8 |
| 9 #include "ipc/attachment_broker.h" |
| 10 #include "ipc/brokerable_attachment.h" |
| 11 #include "ipc/ipc_channel_reader.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 #if USE_ATTACHMENT_BROKER |
| 15 namespace IPC { |
| 16 namespace internal { |
| 17 |
| 18 namespace { |
| 19 |
| 20 class MockAttachment : public BrokerableAttachment { |
| 21 public: |
| 22 MockAttachment(int internal_state) : internal_state_(internal_state) {} |
| 23 MockAttachment(BrokerableAttachment::AttachmentId 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 } |
| 31 |
| 32 #if defined(OS_POSIX) |
| 33 base::PlatformFile TakePlatformFile() override { |
| 34 return base::PlatformFile(); |
| 35 } |
| 36 #endif // OS_POSIX |
| 37 |
| 38 BrokerableType GetBrokerableType() const override { return WIN_HANDLE; } |
| 39 |
| 40 private: |
| 41 ~MockAttachment() override {} |
| 42 // Internal state differentiates MockAttachments. |
| 43 int internal_state_; |
| 44 }; |
| 45 |
| 46 class MockAttachmentBroker : public AttachmentBroker { |
| 47 public: |
| 48 typedef std::set<scoped_refptr<BrokerableAttachment>> AttachmentSet; |
| 49 |
| 50 bool SendAttachmentToProcess(const BrokerableAttachment* attachment, |
| 51 base::ProcessId destination_process) override { |
| 52 return false; |
| 53 } |
| 54 |
| 55 bool OnMessageReceived(const Message& message) override { return false; } |
| 56 |
| 57 bool GetAttachmentWithId( |
| 58 BrokerableAttachment::AttachmentId id, |
| 59 scoped_refptr<BrokerableAttachment>* attachment) override { |
| 60 for (AttachmentSet::iterator it = attachments_.begin(); |
| 61 it != attachments_.end(); ++it) { |
| 62 if ((*it)->GetIdentifier() == id) { |
| 63 *attachment = *it; |
| 64 attachments_.erase(it); |
| 65 return true; |
| 66 } |
| 67 } |
| 68 return false; |
| 69 } |
| 70 |
| 71 void AddAttachment(scoped_refptr<BrokerableAttachment> attachment) { |
| 72 attachments_.insert(attachment); |
| 73 NotifyObservers(attachment->GetIdentifier()); |
| 74 } |
| 75 |
| 76 private: |
| 77 // A set of attachmetns that have been brokered and are available to |
| 78 // consumers. |
| 79 AttachmentSet attachments_; |
| 80 }; |
| 81 |
| 82 class MockChannelReader : public ChannelReader { |
| 83 public: |
| 84 MockChannelReader() |
| 85 : ChannelReader(nullptr), last_dispatched_message_(nullptr) {} |
| 86 |
| 87 ReadState ReadData(char* buffer, int buffer_len, int* bytes_read) override { |
| 88 return READ_FAILED; |
| 89 } |
| 90 |
| 91 bool ShouldDispatchInputMessage(Message* msg) override { return true; } |
| 92 |
| 93 bool GetNonBrokeredAttachments(Message* msg) override { return true; } |
| 94 |
| 95 bool DidEmptyInputBuffers() override { return true; } |
| 96 |
| 97 void HandleInternalMessage(const Message& msg) override {} |
| 98 |
| 99 void DispatchMessage(Message* m) override { last_dispatched_message_ = m; } |
| 100 |
| 101 AttachmentBroker* GetAttachmentBroker() override { return broker_; } |
| 102 |
| 103 // This instance takes ownership of |m|. |
| 104 void AddMessageForDispatch(Message* m) { |
| 105 get_queued_messages()->push_back(m); |
| 106 } |
| 107 |
| 108 Message* get_last_dispatched_message() { return last_dispatched_message_; } |
| 109 |
| 110 void set_broker(AttachmentBroker* broker) { broker_ = broker; } |
| 111 |
| 112 private: |
| 113 Message* last_dispatched_message_; |
| 114 AttachmentBroker* broker_; |
| 115 }; |
| 116 |
| 117 } // namespace |
| 118 |
| 119 TEST(ChannelReaderTest, AttachmentAlreadyBrokered) { |
| 120 MockAttachmentBroker broker; |
| 121 MockChannelReader reader; |
| 122 reader.set_broker(&broker); |
| 123 scoped_refptr<MockAttachment> attachment(new MockAttachment(5)); |
| 124 broker.AddAttachment(attachment); |
| 125 |
| 126 Message* m = new Message; |
| 127 MockAttachment* needs_brokering_attachment = |
| 128 new MockAttachment(attachment->GetIdentifier()); |
| 129 EXPECT_TRUE(m->WriteAttachment(needs_brokering_attachment)); |
| 130 reader.AddMessageForDispatch(m); |
| 131 EXPECT_EQ(ChannelReader::DISPATCH_FINISHED, reader.DispatchMessages()); |
| 132 EXPECT_EQ(m, reader.get_last_dispatched_message()); |
| 133 } |
| 134 |
| 135 TEST(ChannelReaderTest, AttachmentNotYetBrokered) { |
| 136 MockAttachmentBroker broker; |
| 137 MockChannelReader reader; |
| 138 reader.set_broker(&broker); |
| 139 scoped_refptr<MockAttachment> attachment(new MockAttachment(5)); |
| 140 |
| 141 Message* m = new Message; |
| 142 MockAttachment* needs_brokering_attachment = |
| 143 new MockAttachment(attachment->GetIdentifier()); |
| 144 EXPECT_TRUE(m->WriteAttachment(needs_brokering_attachment)); |
| 145 reader.AddMessageForDispatch(m); |
| 146 EXPECT_EQ(ChannelReader::DISPATCH_WAITING_ON_BROKER, |
| 147 reader.DispatchMessages()); |
| 148 EXPECT_EQ(nullptr, reader.get_last_dispatched_message()); |
| 149 |
| 150 broker.AddAttachment(attachment); |
| 151 EXPECT_EQ(m, reader.get_last_dispatched_message()); |
| 152 } |
| 153 |
| 154 } // namespace internal |
| 155 } // namespace IPC |
| 156 #endif // USE_ATTACHMENT_BROKER |
OLD | NEW |