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

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

Issue 2928133005: Revert of [Chromoting] Add DataChannelManager to manage optional incoming data channels (Closed)
Patch Set: 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
« no previous file with comments | « remoting/protocol/data_channel_manager.cc ('k') | remoting/protocol/fake_message_pipe.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/fake_message_pipe.h"
17 #include "remoting/protocol/fake_message_pipe_wrapper.h"
18 #include "remoting/protocol/named_message_pipe_handler.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20
21 namespace remoting {
22 namespace protocol {
23
24 namespace {
25
26 class FakeNamedMessagePipeHandler final : public NamedMessagePipeHandler {
27 public:
28 FakeNamedMessagePipeHandler(const std::string& name,
29 std::unique_ptr<MessagePipe> pipe,
30 const std::string& expected_data)
31 : NamedMessagePipeHandler(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 FakeNamedMessagePipeHandler* Find(const std::string& name);
39
40 static int instance_count() { return handlers_.size(); }
41
42 // NamedMessagePipeHandler implementation.
43 void OnIncomingMessage(std::unique_ptr<CompoundBuffer> message) override;
44
45 // Public functions for test cases.
46 void Close() { NamedMessagePipeHandler::Close(); }
47
48 bool connected() const { return NamedMessagePipeHandler::connected(); }
49
50 void Send(google::protobuf::MessageLite* message,
51 const base::Closure& done);
52
53 protected:
54 ~FakeNamedMessagePipeHandler() override {
55 EXPECT_EQ(handlers_.erase(pipe_name()), 1U);
56 }
57
58 private:
59 static std::map<std::string, FakeNamedMessagePipeHandler*> handlers_;
60
61 const std::string expected_data_;
62 int received_message_count_ = 0;
63 };
64
65 void FakeNamedMessagePipeHandler::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 FakeNamedMessagePipeHandler* FakeNamedMessagePipeHandler::Find(
77 const std::string& name) {
78 auto it = handlers_.find(name);
79 if (it == handlers_.end()) {
80 return nullptr;
81 }
82 return it->second;
83 }
84
85 void FakeNamedMessagePipeHandler::Send(google::protobuf::MessageLite* message,
86 const base::Closure& done) {
87 if (connected()) {
88 NamedMessagePipeHandler::Send(message, done);
89 return;
90 }
91 #if DCHECK_IS_ON()
92 ASSERT_DEATH_IF_SUPPORTED(
93 NamedMessagePipeHandler::Send(message, done),
94 "connected");
95 #else
96 NamedMessagePipeHandler::Send(message, done);
97 #endif
98 }
99
100 // static
101 std::map<std::string, FakeNamedMessagePipeHandler*>
102 FakeNamedMessagePipeHandler::handlers_;
103
104 void TestDataChannelManagerFullMatch(bool asynchronous) {
105 base::MessageLoop message_loop;
106 DataChannelManager manager;
107 manager.RegisterCreateHandlerCallback("FullMatch", base::Bind(
108 [](const std::string& expected_data,
109 const std::string& name,
110 std::unique_ptr<MessagePipe> pipe) -> void {
111 new FakeNamedMessagePipeHandler(name, std::move(pipe), expected_data);
112 },
113 "FullMatchContent"));
114 manager.RegisterCreateHandlerCallback("AnotherFullMatch", base::Bind(
115 [](const std::string& expected_data,
116 const std::string& name,
117 std::unique_ptr<MessagePipe> pipe) -> void {
118 new FakeNamedMessagePipeHandler(name, std::move(pipe), expected_data);
119 },
120 "AnotherFullMatchContent"));
121
122 FakeMessagePipe pipe1(asynchronous);
123 FakeMessagePipe pipe2(asynchronous);
124 ASSERT_TRUE(manager.OnIncomingDataChannel("FullMatch", pipe1.Wrap()));
125 ASSERT_TRUE(manager.OnIncomingDataChannel("AnotherFullMatch", pipe2.Wrap()));
126 base::RunLoop().RunUntilIdle();
127
128 FakeNamedMessagePipeHandler* handler1 =
129 FakeNamedMessagePipeHandler::Find("FullMatch");
130 FakeNamedMessagePipeHandler* handler2 =
131 FakeNamedMessagePipeHandler::Find("AnotherFullMatch");
132 ASSERT_TRUE(handler1 != nullptr);
133 ASSERT_TRUE(handler2 != nullptr);
134
135 pipe1.OpenPipe();
136 base::RunLoop().RunUntilIdle();
137 {
138 DataChannelManagerTestProto message;
139 int sent = 0;
140 base::Closure sent_callback = base::Bind([](int* sent) {
141 (*sent)++;
142 },
143 base::Unretained(&sent));
144 ASSERT_TRUE(handler1->connected());
145 handler1->Send(&message, sent_callback);
146 ASSERT_FALSE(handler2->connected());
147 handler2->Send(&message, sent_callback);
148
149 base::RunLoop().RunUntilIdle();
150 ASSERT_EQ(sent, 1);
151 }
152
153 pipe2.OpenPipe();
154 base::RunLoop().RunUntilIdle();
155 {
156 DataChannelManagerTestProto message;
157 int sent = 0;
158 base::Closure sent_callback = base::Bind([](int* sent) {
159 (*sent)++;
160 },
161 base::Unretained(&sent));
162 ASSERT_TRUE(handler2->connected());
163
164 handler1->Send(&message, sent_callback);
165 handler2->Send(&message, sent_callback);
166
167 base::RunLoop().RunUntilIdle();
168 ASSERT_EQ(sent, 2);
169 }
170
171 {
172 std::string content;
173 auto message = base::MakeUnique<CompoundBuffer>();
174 content = "FullMatchContent";
175 message->AppendCopyOf(&(content[0]), content.size());
176 pipe1.Receive(std::move(message));
177
178 message = base::MakeUnique<CompoundBuffer>();
179 content = "AnotherFullMatchContent";
180 message->AppendCopyOf(&(content[0]), content.size());
181 pipe2.Receive(std::move(message));
182
183 base::RunLoop().RunUntilIdle();
184 ASSERT_EQ(handler1->received_message_count(), 1);
185 ASSERT_EQ(handler2->received_message_count(), 1);
186 }
187
188 pipe2.ClosePipe();
189 if (asynchronous) {
190 base::RunLoop().RunUntilIdle();
191 }
192 ASSERT_TRUE(FakeNamedMessagePipeHandler::Find("AnotherFullMatch") == nullptr);
193
194 handler1->Close();
195 ASSERT_TRUE(FakeNamedMessagePipeHandler::Find("FullMatch") == nullptr);
196
197 ASSERT_EQ(FakeNamedMessagePipeHandler::instance_count(), 0);
198 }
199
200 void TestDataChannelManagerMultipleRegistrations(bool asynchronous) {
201 base::MessageLoop message_loop;
202 DataChannelManager manager;
203 manager.RegisterCreateHandlerCallback("FullMatch", base::Bind(
204 [](const std::string& expected_data,
205 const std::string& name,
206 std::unique_ptr<MessagePipe> pipe) -> void {
207 new FakeNamedMessagePipeHandler(name, std::move(pipe), expected_data);
208 },
209 "FullMatchContent"));
210 manager.RegisterCreateHandlerCallback("Prefix-", base::Bind(
211 [](const std::string& expected_data,
212 const std::string& name,
213 std::unique_ptr<MessagePipe> pipe) -> void {
214 new FakeNamedMessagePipeHandler(name, std::move(pipe), expected_data);
215 },
216 "PrefixMatchContent"));
217
218 FakeMessagePipe pipe1(asynchronous);
219 FakeMessagePipe pipe2(asynchronous);
220 FakeMessagePipe pipe3(asynchronous);
221 FakeMessagePipe pipe4(asynchronous);
222 ASSERT_TRUE(manager.OnIncomingDataChannel("Prefix-1", pipe1.Wrap()));
223 ASSERT_TRUE(manager.OnIncomingDataChannel("Prefix-2", pipe2.Wrap()));
224 ASSERT_FALSE(manager.OnIncomingDataChannel(
225 "PrefixShouldNotMatch", pipe3.Wrap()));
226 ASSERT_TRUE(manager.OnIncomingDataChannel("FullMatch", pipe4.Wrap()));
227 base::RunLoop().RunUntilIdle();
228
229 FakeNamedMessagePipeHandler* handler1 =
230 FakeNamedMessagePipeHandler::Find("Prefix-1");
231 FakeNamedMessagePipeHandler* handler2 =
232 FakeNamedMessagePipeHandler::Find("Prefix-2");
233 FakeNamedMessagePipeHandler* handler3 =
234 FakeNamedMessagePipeHandler::Find("PrefixShouldNotMatch");
235 FakeNamedMessagePipeHandler* handler4 =
236 FakeNamedMessagePipeHandler::Find("FullMatch");
237 ASSERT_TRUE(handler1 != nullptr);
238 ASSERT_TRUE(handler2 != nullptr);
239 ASSERT_TRUE(handler3 == nullptr);
240 ASSERT_TRUE(handler4 != nullptr);
241
242 handler1->Close();
243 handler2->Close();
244 handler4->Close();
245 base::RunLoop().RunUntilIdle();
246 }
247
248 } // namespace
249
250 TEST(DataChannelManagerTest, FullMatchWithSynchronousPipe) {
251 TestDataChannelManagerFullMatch(false);
252 }
253
254 TEST(DataChannelManagerTest, FullMatchWithAsynchronousPipe) {
255 TestDataChannelManagerFullMatch(true);
256 }
257
258 TEST(DataChannelManagerTest, MultipleRegistrationsWithSynchronousPipe) {
259 TestDataChannelManagerMultipleRegistrations(false);
260 }
261
262 TEST(DataChannelManagerTest, MultipleRegistrationsWithAsynchronousPipe) {
263 TestDataChannelManagerMultipleRegistrations(true);
264 }
265
266 } // namespace protocol
267 } // namespace remoting
OLDNEW
« no previous file with comments | « remoting/protocol/data_channel_manager.cc ('k') | remoting/protocol/fake_message_pipe.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698