disable two ClientCertStoreChromeOSTest.* unit_tests on Valgrind bots
[chromium-blink-merge.git] / ipc / ipc_channel_reader_unittest.cc
blob005898015bf6d40e607b2113838908ac37b73a5c
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.
5 #include "build/build_config.h"
7 #include <set>
9 #include "ipc/attachment_broker.h"
10 #include "ipc/brokerable_attachment.h"
11 #include "ipc/ipc_channel_reader.h"
12 #include "ipc/placeholder_brokerable_attachment.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 #if USE_ATTACHMENT_BROKER
16 namespace IPC {
17 namespace internal {
19 namespace {
21 class MockAttachment : public BrokerableAttachment {
22 public:
23 MockAttachment() {}
24 MockAttachment(BrokerableAttachment::AttachmentId id)
25 : BrokerableAttachment(id) {}
27 #if defined(OS_POSIX)
28 base::PlatformFile TakePlatformFile() override {
29 return base::PlatformFile();
31 #endif // OS_POSIX
33 BrokerableType GetBrokerableType() const override { return WIN_HANDLE; }
35 private:
36 ~MockAttachment() override {}
39 class MockAttachmentBroker : public AttachmentBroker {
40 public:
41 typedef std::set<scoped_refptr<BrokerableAttachment>> AttachmentSet;
43 bool SendAttachmentToProcess(const BrokerableAttachment* attachment,
44 base::ProcessId destination_process) override {
45 return false;
48 bool OnMessageReceived(const Message& message) override { return false; }
50 void AddAttachment(scoped_refptr<BrokerableAttachment> attachment) {
51 get_attachments()->push_back(attachment);
52 NotifyObservers(attachment->GetIdentifier());
56 class MockChannelReader : public ChannelReader {
57 public:
58 MockChannelReader()
59 : ChannelReader(nullptr), last_dispatched_message_(nullptr) {}
61 ReadState ReadData(char* buffer, int buffer_len, int* bytes_read) override {
62 return READ_FAILED;
65 bool ShouldDispatchInputMessage(Message* msg) override { return true; }
67 bool GetNonBrokeredAttachments(Message* msg) override { return true; }
69 bool DidEmptyInputBuffers() override { return true; }
71 void HandleInternalMessage(const Message& msg) override {}
73 void DispatchMessage(Message* m) override { last_dispatched_message_ = m; }
75 base::ProcessId GetSenderPID() override { return base::kNullProcessId; }
77 bool IsAttachmentBrokerEndpoint() override { return false; }
79 AttachmentBroker* GetAttachmentBroker() override { return broker_; }
81 // This instance takes ownership of |m|.
82 void AddMessageForDispatch(Message* m) {
83 get_queued_messages()->push_back(m);
86 Message* get_last_dispatched_message() { return last_dispatched_message_; }
88 void set_broker(AttachmentBroker* broker) { broker_ = broker; }
90 private:
91 Message* last_dispatched_message_;
92 AttachmentBroker* broker_;
95 } // namespace
97 TEST(ChannelReaderTest, AttachmentAlreadyBrokered) {
98 MockAttachmentBroker broker;
99 MockChannelReader reader;
100 reader.set_broker(&broker);
101 scoped_refptr<MockAttachment> attachment(new MockAttachment);
102 broker.AddAttachment(attachment);
104 Message* m = new Message;
105 PlaceholderBrokerableAttachment* needs_brokering_attachment =
106 new PlaceholderBrokerableAttachment(attachment->GetIdentifier());
107 EXPECT_TRUE(m->WriteAttachment(needs_brokering_attachment));
108 reader.AddMessageForDispatch(m);
109 EXPECT_EQ(ChannelReader::DISPATCH_FINISHED, reader.DispatchMessages());
110 EXPECT_EQ(m, reader.get_last_dispatched_message());
113 TEST(ChannelReaderTest, AttachmentNotYetBrokered) {
114 MockAttachmentBroker broker;
115 MockChannelReader reader;
116 reader.set_broker(&broker);
117 scoped_refptr<MockAttachment> attachment(new MockAttachment);
119 Message* m = new Message;
120 PlaceholderBrokerableAttachment* needs_brokering_attachment =
121 new PlaceholderBrokerableAttachment(attachment->GetIdentifier());
122 EXPECT_TRUE(m->WriteAttachment(needs_brokering_attachment));
123 reader.AddMessageForDispatch(m);
124 EXPECT_EQ(ChannelReader::DISPATCH_WAITING_ON_BROKER,
125 reader.DispatchMessages());
126 EXPECT_EQ(nullptr, reader.get_last_dispatched_message());
128 broker.AddAttachment(attachment);
129 EXPECT_EQ(m, reader.get_last_dispatched_message());
132 } // namespace internal
133 } // namespace IPC
134 #endif // USE_ATTACHMENT_BROKER