OLD | NEW |
---|---|
(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/process_stats.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 void OnIncomingMessage(std::unique_ptr<CompoundBuffer> message) override { | |
joedow
2017/05/30 16:24:18
nit: I think it is cleaner to break the impls out
Hzj_jie
2017/05/31 00:11:53
Done.
| |
37 ASSERT_TRUE(message != nullptr); | |
38 std::string content; | |
39 content.resize(expected_data_.size()); | |
40 message->CopyTo(&(content[0]), content.size()); | |
41 ASSERT_EQ(content, expected_data_); | |
42 received_message_count_++; | |
43 } | |
44 | |
45 int received_message_count() const { return received_message_count_; } | |
46 | |
47 static FakeDataChannelHandler* Find(const std::string& name) { | |
48 auto it = handlers_.find(name); | |
49 if (it == handlers_.end()) { | |
50 return nullptr; | |
51 } | |
52 return it->second; | |
53 } | |
54 | |
55 static int instance_count() { return handlers_.size(); } | |
56 | |
57 protected: | |
58 ~FakeDataChannelHandler() override { | |
59 EXPECT_EQ(handlers_.erase(pipe_name()), 1U); | |
60 } | |
61 | |
62 private: | |
63 static std::map<std::string, FakeDataChannelHandler*> handlers_; | |
64 | |
65 const std::string expected_data_; | |
66 int received_message_count_ = 0; | |
67 }; | |
68 | |
69 // static | |
70 std::map<std::string, FakeDataChannelHandler*> | |
71 FakeDataChannelHandler::handlers_; | |
72 | |
73 void TestDataChannelManager(bool asynchronous) { | |
joedow
2017/05/30 16:24:18
This is a *big* test method that does a lot of stu
Hzj_jie
2017/05/31 00:11:53
Done.
| |
74 base::MessageLoop message_loop; | |
75 DataChannelManager manager; | |
76 ASSERT_TRUE(manager.RegisterHandlerFactory("FullMatch", base::Bind( | |
77 [](const std::string& expected_data, | |
78 const std::string& name, | |
79 std::unique_ptr<MessagePipe> pipe) -> void { | |
80 new FakeDataChannelHandler(name, std::move(pipe), expected_data); | |
81 }, | |
82 "FullMatchContent"))); | |
83 | |
84 ASSERT_TRUE(manager.RegisterHandlerFactory("Prefix.*", base::Bind( | |
85 [](const std::string& expected_data, | |
86 const std::string& name, | |
87 std::unique_ptr<MessagePipe> pipe) -> void { | |
88 new FakeDataChannelHandler(name, std::move(pipe), expected_data); | |
89 }, | |
90 "PrefixMatchContent"))); | |
91 | |
92 FakeMessagePipe pipe1(asynchronous); | |
93 FakeMessagePipe pipe2(asynchronous); | |
94 FakeMessagePipe pipe3(asynchronous); | |
95 FakeMessagePipe pipe4(asynchronous); | |
96 FakeMessagePipe pipe5(asynchronous); | |
97 ASSERT_TRUE(manager.OnIncomingDataChannel("FullMatch", pipe1.Wrap())); | |
98 ASSERT_TRUE(manager.OnIncomingDataChannel("Prefix1", pipe2.Wrap())); | |
99 ASSERT_TRUE(manager.OnIncomingDataChannel("Prefix2", pipe3.Wrap())); | |
100 ASSERT_TRUE(manager.OnIncomingDataChannel("Prefix3", pipe4.Wrap())); | |
101 ASSERT_FALSE(manager.OnIncomingDataChannel("Unknown", pipe5.Wrap())); | |
102 pipe1.OpenPipe(); | |
103 pipe2.OpenPipe(); | |
104 pipe3.OpenPipe(); | |
105 | |
106 base::RunLoop().RunUntilIdle(); | |
107 | |
108 FakeDataChannelHandler* handler1 = FakeDataChannelHandler::Find("FullMatch"); | |
109 FakeDataChannelHandler* handler2 = FakeDataChannelHandler::Find("Prefix1"); | |
110 FakeDataChannelHandler* handler3 = FakeDataChannelHandler::Find("Prefix2"); | |
111 FakeDataChannelHandler* handler4 = FakeDataChannelHandler::Find("Prefix3"); | |
112 FakeDataChannelHandler* handler5 = FakeDataChannelHandler::Find("Unknown"); | |
113 ASSERT_TRUE(handler1 != nullptr); | |
114 ASSERT_TRUE(handler2 != nullptr); | |
115 ASSERT_TRUE(handler3 != nullptr); | |
116 ASSERT_TRUE(handler4 != nullptr); | |
117 ASSERT_TRUE(handler5 == nullptr); | |
118 | |
119 { | |
120 ProcessResourceUsage usage; | |
joedow
2017/05/30 16:24:18
Can you avoid bringing in external dependencies in
Hzj_jie
2017/05/31 00:11:53
This is not doable since proto is automatically ge
joedow
2017/06/01 17:25:26
Thanks!
Hzj_jie
2017/06/01 19:32:08
Done.
| |
121 int sent = 0; | |
122 base::Closure sent_callback = base::Bind([](int* sent) { | |
123 (*sent)++; | |
124 }, | |
125 base::Unretained(&sent)); | |
126 ASSERT_TRUE(handler1->Send(&usage, sent_callback)); | |
127 ASSERT_TRUE(handler2->Send(&usage, sent_callback)); | |
128 ASSERT_TRUE(handler3->Send(&usage, sent_callback)); | |
129 ASSERT_FALSE(handler4->Send(&usage, sent_callback)); | |
130 | |
131 base::RunLoop().RunUntilIdle(); | |
132 ASSERT_EQ(sent, 3); | |
133 } | |
134 | |
135 { | |
136 std::string content; | |
137 auto message = base::MakeUnique<CompoundBuffer>(); | |
138 content = "FullMatchContent"; | |
139 message->AppendCopyOf(&(content[0]), content.size()); | |
140 pipe1.Receive(std::move(message)); | |
141 | |
142 message = base::MakeUnique<CompoundBuffer>(); | |
143 content = "PrefixMatchContent"; | |
144 message->AppendCopyOf(&(content[0]), content.size()); | |
145 pipe2.Receive(std::move(message)); | |
146 | |
147 message = base::MakeUnique<CompoundBuffer>(); | |
148 content = "PrefixMatchContent"; | |
149 message->AppendCopyOf(&(content[0]), content.size()); | |
150 pipe3.Receive(std::move(message)); | |
151 | |
152 base::RunLoop().RunUntilIdle(); | |
153 ASSERT_EQ(handler1->received_message_count(), 1); | |
154 ASSERT_EQ(handler2->received_message_count(), 1); | |
155 ASSERT_EQ(handler3->received_message_count(), 1); | |
156 } | |
157 | |
158 pipe2.ClosePipe(); | |
159 base::RunLoop().RunUntilIdle(); | |
160 ASSERT_TRUE(FakeDataChannelHandler::Find("Prefix1") == nullptr); | |
161 | |
162 handler3->Close(); | |
163 base::RunLoop().RunUntilIdle(); | |
164 ASSERT_TRUE(FakeDataChannelHandler::Find("Prefix2") == nullptr); | |
165 | |
166 pipe1.ClosePipe(); | |
167 handler4->Close(); | |
168 base::RunLoop().RunUntilIdle(); | |
169 ASSERT_EQ(FakeDataChannelHandler::instance_count(), 0); | |
170 } | |
171 | |
172 } // namespace | |
173 | |
174 TEST(DataChannelManagerTest, SynchronousPipe) { | |
175 TestDataChannelManager(false); | |
176 } | |
177 | |
178 TEST(DataChannelManagerTest, AsynchronousPipe) { | |
179 TestDataChannelManager(true); | |
180 } | |
181 | |
182 } // namespace protocol | |
183 } // namespace remoting | |
OLD | NEW |