Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(15)

Side by Side Diff: remoting/protocol/data_channel_manager_unittest.cc

Issue 2907073003: [Chromoting] Add DataChannelManager to manage optional incoming data channels (Closed)
Patch Set: Resolve review comments Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 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 "remoting/protocol/data_channel_manager.h"
6
7 #include <map>
8 #include <utility>
9
10 #include "base/bind.h"
11 #include "base/memory/ptr_util.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/run_loop.h"
14 #include "remoting/base/compound_buffer.h"
15 #include "remoting/proto/test.pb.h"
16 #include "remoting/protocol/data_channel_handler.h"
17 #include "remoting/protocol/fake_message_pipe.h"
18 #include "remoting/protocol/fake_message_pipe_wrapper.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20
21 namespace remoting {
22 namespace protocol {
23
24 namespace {
25
26 class FakeDataChannelHandler final : public DataChannelHandler {
27 public:
28 FakeDataChannelHandler(const std::string& name,
29 std::unique_ptr<MessagePipe> pipe,
30 const std::string& expected_data)
31 : DataChannelHandler(name, std::move(pipe)),
32 expected_data_(expected_data) {
33 handlers_[name] = this;
34 }
35
36 int received_message_count() const { return received_message_count_; }
37
38 static FakeDataChannelHandler* Find(const std::string& name);
39
40 static int instance_count() { return handlers_.size(); }
41
42 // DataChannelHandler implementation.
43 void OnIncomingMessage(std::unique_ptr<CompoundBuffer> message) override;
44
45 // Public functions for test cases.
46 void Close() { DataChannelHandler::Close(); }
47
48 bool closing() const { return DataChannelHandler::closing(); }
49
50 bool Send(google::protobuf::MessageLite* message,
51 const base::Closure& done);
52
53 protected:
54 ~FakeDataChannelHandler() override {
55 EXPECT_EQ(handlers_.erase(pipe_name()), 1U);
56 }
57
58 private:
59 static std::map<std::string, FakeDataChannelHandler*> handlers_;
60
61 const std::string expected_data_;
62 int received_message_count_ = 0;
63 };
64
65 void FakeDataChannelHandler::OnIncomingMessage(
66 std::unique_ptr<CompoundBuffer> message) {
67 ASSERT_TRUE(message != nullptr);
68 std::string content;
69 content.resize(expected_data_.size());
70 message->CopyTo(&(content[0]), content.size());
71 ASSERT_EQ(content, expected_data_);
72 received_message_count_++;
73 }
74
75 // static
76 FakeDataChannelHandler* FakeDataChannelHandler::Find(const std::string& name) {
77 auto it = handlers_.find(name);
78 if (it == handlers_.end()) {
79 return nullptr;
80 }
81 return it->second;
82 }
83
84 bool FakeDataChannelHandler::Send(google::protobuf::MessageLite* message,
85 const base::Closure& done) {
86 return DataChannelHandler::Send(message, done);
87 }
88
89 // static
90 std::map<std::string, FakeDataChannelHandler*>
91 FakeDataChannelHandler::handlers_;
92
93 void TestDataChannelManagerFullMatch(bool asynchronous) {
94 base::MessageLoop message_loop;
95 DataChannelManager manager;
96 ASSERT_TRUE(manager.RegisterCreateHandlerCallback("FullMatch", base::Bind(
97 [](const std::string& expected_data,
98 const std::string& name,
99 std::unique_ptr<MessagePipe> pipe) -> void {
100 new FakeDataChannelHandler(name, std::move(pipe), expected_data);
101 },
102 "FullMatchContent")));
103 ASSERT_TRUE(manager.RegisterCreateHandlerCallback("AnotherFullMatch", base::Bi nd(
104 [](const std::string& expected_data,
105 const std::string& name,
106 std::unique_ptr<MessagePipe> pipe) -> void {
107 new FakeDataChannelHandler(name, std::move(pipe), expected_data);
108 },
109 "AnotherFullMatchContent")));
110
111 FakeMessagePipe pipe1(asynchronous);
112 FakeMessagePipe pipe2(asynchronous);
113 ASSERT_TRUE(manager.OnIncomingDataChannel("FullMatch", pipe1.Wrap()));
114 ASSERT_TRUE(manager.OnIncomingDataChannel("AnotherFullMatch", pipe2.Wrap()));
115 base::RunLoop().RunUntilIdle();
116
117 FakeDataChannelHandler* handler1 = FakeDataChannelHandler::Find("FullMatch");
118 FakeDataChannelHandler* handler2 =
119 FakeDataChannelHandler::Find("AnotherFullMatch");
120 ASSERT_TRUE(handler1 != nullptr);
121 ASSERT_TRUE(handler2 != nullptr);
122
123 pipe1.OpenPipe();
124 base::RunLoop().RunUntilIdle();
125 {
126 DataChannelManagerTestProto message;
127 int sent = 0;
128 base::Closure sent_callback = base::Bind([](int* sent) {
129 (*sent)++;
130 },
131 base::Unretained(&sent));
132 ASSERT_TRUE(handler1->Send(&message, sent_callback));
133 ASSERT_FALSE(handler2->Send(&message, sent_callback));
134
135 base::RunLoop().RunUntilIdle();
136 ASSERT_EQ(sent, 1);
137 }
138
139 pipe2.OpenPipe();
140 base::RunLoop().RunUntilIdle();
141 {
142 DataChannelManagerTestProto message;
143 int sent = 0;
144 base::Closure sent_callback = base::Bind([](int* sent) {
145 (*sent)++;
146 },
147 base::Unretained(&sent));
148 ASSERT_TRUE(handler1->Send(&message, sent_callback));
149 ASSERT_TRUE(handler2->Send(&message, sent_callback));
150
151 base::RunLoop().RunUntilIdle();
152 ASSERT_EQ(sent, 2);
153 }
154
155 {
156 std::string content;
157 auto message = base::MakeUnique<CompoundBuffer>();
158 content = "FullMatchContent";
159 message->AppendCopyOf(&(content[0]), content.size());
160 pipe1.Receive(std::move(message));
161
162 message = base::MakeUnique<CompoundBuffer>();
163 content = "AnotherFullMatchContent";
164 message->AppendCopyOf(&(content[0]), content.size());
165 pipe2.Receive(std::move(message));
166
167 base::RunLoop().RunUntilIdle();
168 ASSERT_EQ(handler1->received_message_count(), 1);
169 ASSERT_EQ(handler2->received_message_count(), 1);
170 }
171
172 pipe2.ClosePipe();
173 if (!asynchronous) {
174 ASSERT_TRUE(handler2->closing());
175 }
176 base::RunLoop().RunUntilIdle();
177 ASSERT_TRUE(FakeDataChannelHandler::Find("AnotherFullMatch") == nullptr);
178
179 handler1->Close();
180 ASSERT_TRUE(handler1->closing());
181 base::RunLoop().RunUntilIdle();
182 ASSERT_TRUE(FakeDataChannelHandler::Find("FullMatch") == nullptr);
183
184 ASSERT_EQ(FakeDataChannelHandler::instance_count(), 0);
185 }
186
187 void TestDataChannelManagerMultipleRegistrations(bool asynchronous) {
188 base::MessageLoop message_loop;
189 DataChannelManager manager;
190 ASSERT_TRUE(manager.RegisterCreateHandlerCallback("FullMatch", base::Bind(
191 [](const std::string& expected_data,
192 const std::string& name,
193 std::unique_ptr<MessagePipe> pipe) -> void {
194 new FakeDataChannelHandler(name, std::move(pipe), expected_data);
195 },
196 "FullMatchContent")));
197 ASSERT_TRUE(manager.RegisterCreateHandlerCallback("Prefix-", base::Bind(
198 [](const std::string& expected_data,
199 const std::string& name,
200 std::unique_ptr<MessagePipe> pipe) -> void {
201 new FakeDataChannelHandler(name, std::move(pipe), expected_data);
202 },
203 "PrefixMatchContent")));
204
205 FakeMessagePipe pipe1(asynchronous);
206 FakeMessagePipe pipe2(asynchronous);
207 FakeMessagePipe pipe3(asynchronous);
208 FakeMessagePipe pipe4(asynchronous);
209 ASSERT_TRUE(manager.OnIncomingDataChannel("Prefix-1", pipe1.Wrap()));
210 ASSERT_TRUE(manager.OnIncomingDataChannel("Prefix-2", pipe2.Wrap()));
211 ASSERT_FALSE(manager.OnIncomingDataChannel(
212 "PrefixShouldNotMatch", pipe3.Wrap()));
213 ASSERT_TRUE(manager.OnIncomingDataChannel("FullMatch", pipe4.Wrap()));
214 base::RunLoop().RunUntilIdle();
215
216 FakeDataChannelHandler* handler1 = FakeDataChannelHandler::Find("Prefix-1");
217 FakeDataChannelHandler* handler2 = FakeDataChannelHandler::Find("Prefix-2");
218 FakeDataChannelHandler* handler3 =
219 FakeDataChannelHandler::Find("PrefixShouldNotMatch");
220 FakeDataChannelHandler* handler4 = FakeDataChannelHandler::Find("FullMatch");
221 ASSERT_TRUE(handler1 != nullptr);
222 ASSERT_TRUE(handler2 != nullptr);
223 ASSERT_TRUE(handler3 == nullptr);
224 ASSERT_TRUE(handler4 != nullptr);
225
226 handler1->Close();
227 handler2->Close();
228 handler4->Close();
229 base::RunLoop().RunUntilIdle();
230 }
231
232 } // namespace
233
234 TEST(DataChannelManagerTest, FullMatchWithSynchronousPipe) {
235 TestDataChannelManagerFullMatch(false);
236 }
237
238 TEST(DataChannelManagerTest, FullMatchWithAsynchronousPipe) {
239 TestDataChannelManagerFullMatch(true);
240 }
241
242 TEST(DataChannelManagerTest, MultipleRegistrationsWithSynchronousPipe) {
243 TestDataChannelManagerMultipleRegistrations(false);
244 }
245
246 TEST(DataChannelManagerTest, MultipleRegistrationsWithAsynchronousPipe) {
247 TestDataChannelManagerMultipleRegistrations(true);
248 }
249
250 #if DCHECK_IS_ON()
251 TEST(DataChannelManagerTest, MultipleMatchesIsNotAllowed) {
252 DataChannelManager manager;
253 ASSERT_TRUE(manager.RegisterCreateHandlerCallback("Pipe", base::Bind(
254 [](const std::string& expected_data,
255 const std::string& name,
256 std::unique_ptr<MessagePipe> pipe) -> void {
257 new FakeDataChannelHandler(name, std::move(pipe), expected_data);
258 },
259 "")));
260 ASSERT_TRUE(manager.RegisterCreateHandlerCallback("Pipe-", base::Bind(
261 [](const std::string& expected_data,
262 const std::string& name,
263 std::unique_ptr<MessagePipe> pipe) -> void {
264 new FakeDataChannelHandler(name, std::move(pipe), expected_data);
265 },
266 "")));
267 FakeMessagePipe pipe(false);
268 ASSERT_DEATH_IF_SUPPORTED(
269 manager.OnIncomingDataChannel("Pipe-1", pipe.Wrap()), "!matched");
270 }
271 #endif
272
273 } // namespace protocol
274 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698