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