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