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/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 #endif |
| 96 } |
| 97 |
| 98 // static |
| 99 std::map<std::string, FakeNamedMessagePipeHandler*> |
| 100 FakeNamedMessagePipeHandler::handlers_; |
| 101 |
| 102 void TestDataChannelManagerFullMatch(bool asynchronous) { |
| 103 base::MessageLoop message_loop; |
| 104 DataChannelManager manager; |
| 105 manager.RegisterCreateHandlerCallback("FullMatch", base::Bind( |
| 106 [](const std::string& expected_data, |
| 107 const std::string& name, |
| 108 std::unique_ptr<MessagePipe> pipe) -> void { |
| 109 new FakeNamedMessagePipeHandler(name, std::move(pipe), expected_data); |
| 110 }, |
| 111 "FullMatchContent")); |
| 112 manager.RegisterCreateHandlerCallback("AnotherFullMatch", base::Bind( |
| 113 [](const std::string& expected_data, |
| 114 const std::string& name, |
| 115 std::unique_ptr<MessagePipe> pipe) -> void { |
| 116 new FakeNamedMessagePipeHandler(name, std::move(pipe), expected_data); |
| 117 }, |
| 118 "AnotherFullMatchContent")); |
| 119 |
| 120 FakeMessagePipe pipe1(asynchronous); |
| 121 FakeMessagePipe pipe2(asynchronous); |
| 122 ASSERT_TRUE(manager.OnIncomingDataChannel("FullMatch", pipe1.Wrap())); |
| 123 ASSERT_TRUE(manager.OnIncomingDataChannel("AnotherFullMatch", pipe2.Wrap())); |
| 124 base::RunLoop().RunUntilIdle(); |
| 125 |
| 126 FakeNamedMessagePipeHandler* handler1 = |
| 127 FakeNamedMessagePipeHandler::Find("FullMatch"); |
| 128 FakeNamedMessagePipeHandler* handler2 = |
| 129 FakeNamedMessagePipeHandler::Find("AnotherFullMatch"); |
| 130 ASSERT_TRUE(handler1 != nullptr); |
| 131 ASSERT_TRUE(handler2 != nullptr); |
| 132 |
| 133 pipe1.OpenPipe(); |
| 134 base::RunLoop().RunUntilIdle(); |
| 135 { |
| 136 DataChannelManagerTestProto message; |
| 137 int sent = 0; |
| 138 base::Closure sent_callback = base::Bind([](int* sent) { |
| 139 (*sent)++; |
| 140 }, |
| 141 base::Unretained(&sent)); |
| 142 ASSERT_TRUE(handler1->connected()); |
| 143 handler1->Send(&message, sent_callback); |
| 144 ASSERT_FALSE(handler2->connected()); |
| 145 handler2->Send(&message, sent_callback); |
| 146 |
| 147 base::RunLoop().RunUntilIdle(); |
| 148 ASSERT_EQ(sent, 1); |
| 149 } |
| 150 |
| 151 pipe2.OpenPipe(); |
| 152 base::RunLoop().RunUntilIdle(); |
| 153 { |
| 154 DataChannelManagerTestProto message; |
| 155 int sent = 0; |
| 156 base::Closure sent_callback = base::Bind([](int* sent) { |
| 157 (*sent)++; |
| 158 }, |
| 159 base::Unretained(&sent)); |
| 160 ASSERT_TRUE(handler2->connected()); |
| 161 |
| 162 handler1->Send(&message, sent_callback); |
| 163 handler2->Send(&message, sent_callback); |
| 164 |
| 165 base::RunLoop().RunUntilIdle(); |
| 166 ASSERT_EQ(sent, 2); |
| 167 } |
| 168 |
| 169 { |
| 170 std::string content; |
| 171 auto message = base::MakeUnique<CompoundBuffer>(); |
| 172 content = "FullMatchContent"; |
| 173 message->AppendCopyOf(&(content[0]), content.size()); |
| 174 pipe1.Receive(std::move(message)); |
| 175 |
| 176 message = base::MakeUnique<CompoundBuffer>(); |
| 177 content = "AnotherFullMatchContent"; |
| 178 message->AppendCopyOf(&(content[0]), content.size()); |
| 179 pipe2.Receive(std::move(message)); |
| 180 |
| 181 base::RunLoop().RunUntilIdle(); |
| 182 ASSERT_EQ(handler1->received_message_count(), 1); |
| 183 ASSERT_EQ(handler2->received_message_count(), 1); |
| 184 } |
| 185 |
| 186 pipe2.ClosePipe(); |
| 187 if (asynchronous) { |
| 188 base::RunLoop().RunUntilIdle(); |
| 189 } |
| 190 ASSERT_TRUE(FakeNamedMessagePipeHandler::Find("AnotherFullMatch") == nullptr); |
| 191 |
| 192 handler1->Close(); |
| 193 ASSERT_TRUE(FakeNamedMessagePipeHandler::Find("FullMatch") == nullptr); |
| 194 |
| 195 ASSERT_EQ(FakeNamedMessagePipeHandler::instance_count(), 0); |
| 196 } |
| 197 |
| 198 void TestDataChannelManagerMultipleRegistrations(bool asynchronous) { |
| 199 base::MessageLoop message_loop; |
| 200 DataChannelManager manager; |
| 201 manager.RegisterCreateHandlerCallback("FullMatch", base::Bind( |
| 202 [](const std::string& expected_data, |
| 203 const std::string& name, |
| 204 std::unique_ptr<MessagePipe> pipe) -> void { |
| 205 new FakeNamedMessagePipeHandler(name, std::move(pipe), expected_data); |
| 206 }, |
| 207 "FullMatchContent")); |
| 208 manager.RegisterCreateHandlerCallback("Prefix-", base::Bind( |
| 209 [](const std::string& expected_data, |
| 210 const std::string& name, |
| 211 std::unique_ptr<MessagePipe> pipe) -> void { |
| 212 new FakeNamedMessagePipeHandler(name, std::move(pipe), expected_data); |
| 213 }, |
| 214 "PrefixMatchContent")); |
| 215 |
| 216 FakeMessagePipe pipe1(asynchronous); |
| 217 FakeMessagePipe pipe2(asynchronous); |
| 218 FakeMessagePipe pipe3(asynchronous); |
| 219 FakeMessagePipe pipe4(asynchronous); |
| 220 ASSERT_TRUE(manager.OnIncomingDataChannel("Prefix-1", pipe1.Wrap())); |
| 221 ASSERT_TRUE(manager.OnIncomingDataChannel("Prefix-2", pipe2.Wrap())); |
| 222 ASSERT_FALSE(manager.OnIncomingDataChannel( |
| 223 "PrefixShouldNotMatch", pipe3.Wrap())); |
| 224 ASSERT_TRUE(manager.OnIncomingDataChannel("FullMatch", pipe4.Wrap())); |
| 225 base::RunLoop().RunUntilIdle(); |
| 226 |
| 227 FakeNamedMessagePipeHandler* handler1 = |
| 228 FakeNamedMessagePipeHandler::Find("Prefix-1"); |
| 229 FakeNamedMessagePipeHandler* handler2 = |
| 230 FakeNamedMessagePipeHandler::Find("Prefix-2"); |
| 231 FakeNamedMessagePipeHandler* handler3 = |
| 232 FakeNamedMessagePipeHandler::Find("PrefixShouldNotMatch"); |
| 233 FakeNamedMessagePipeHandler* handler4 = |
| 234 FakeNamedMessagePipeHandler::Find("FullMatch"); |
| 235 ASSERT_TRUE(handler1 != nullptr); |
| 236 ASSERT_TRUE(handler2 != nullptr); |
| 237 ASSERT_TRUE(handler3 == nullptr); |
| 238 ASSERT_TRUE(handler4 != nullptr); |
| 239 |
| 240 handler1->Close(); |
| 241 handler2->Close(); |
| 242 handler4->Close(); |
| 243 base::RunLoop().RunUntilIdle(); |
| 244 } |
| 245 |
| 246 } // namespace |
| 247 |
| 248 TEST(DataChannelManagerTest, FullMatchWithSynchronousPipe) { |
| 249 TestDataChannelManagerFullMatch(false); |
| 250 } |
| 251 |
| 252 TEST(DataChannelManagerTest, FullMatchWithAsynchronousPipe) { |
| 253 TestDataChannelManagerFullMatch(true); |
| 254 } |
| 255 |
| 256 TEST(DataChannelManagerTest, MultipleRegistrationsWithSynchronousPipe) { |
| 257 TestDataChannelManagerMultipleRegistrations(false); |
| 258 } |
| 259 |
| 260 TEST(DataChannelManagerTest, MultipleRegistrationsWithAsynchronousPipe) { |
| 261 TestDataChannelManagerMultipleRegistrations(true); |
| 262 } |
| 263 |
| 264 } // namespace protocol |
| 265 } // namespace remoting |
OLD | NEW |