1 // Copyright 2014 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/test/fake_port_allocator.h"
6
7 #include "remoting/test/fake_network_dispatcher.h"
8 #include "remoting/test/fake_network_manager.h"
9 #include "remoting/test/fake_socket_factory.h"
10
11 namespace remoting {
12
13 namespace {
14
15 class FakePortAllocatorSession
16 : public cricket::HttpPortAllocatorSessionBase {
17 public:
18 FakePortAllocatorSession(
19 cricket::HttpPortAllocatorBase* allocator,
20 const std::string& content_name,
21 int component,
22 const std::string& ice_username_fragment,
23 const std::string& ice_password,
24 const std::vector<rtc::SocketAddress>& stun_hosts,
25 const std::vector<std::string>& relay_hosts,
26 const std::string& relay);
27 virtual ~FakePortAllocatorSession();
28
29 // cricket::HttpPortAllocatorBase overrides.
30 virtual void ConfigReady(cricket::PortConfiguration* config) OVERRIDE;
31 virtual void SendSessionRequest(const std::string& host, int port) OVERRIDE;
32
33 private:
34 DISALLOW_COPY_AND_ASSIGN(FakePortAllocatorSession);
35 };
36
FakePortAllocatorSession(cricket::HttpPortAllocatorBase * allocator,const std::string & content_name,int component,const std::string & ice_username_fragment,const std::string & ice_password,const std::vector<rtc::SocketAddress> & stun_hosts,const std::vector<std::string> & relay_hosts,const std::string & relay)37 FakePortAllocatorSession::FakePortAllocatorSession(
38 cricket::HttpPortAllocatorBase* allocator,
39 const std::string& content_name,
40 int component,
41 const std::string& ice_username_fragment,
42 const std::string& ice_password,
43 const std::vector<rtc::SocketAddress>& stun_hosts,
44 const std::vector<std::string>& relay_hosts,
45 const std::string& relay)
46 : HttpPortAllocatorSessionBase(allocator,
47 content_name,
48 component,
49 ice_username_fragment,
50 ice_password,
51 stun_hosts,
52 relay_hosts,
53 relay,
54 std::string()) {
55 set_flags(cricket::PORTALLOCATOR_DISABLE_TCP |
56 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG |
57 cricket::PORTALLOCATOR_ENABLE_IPV6 |
58 cricket::PORTALLOCATOR_DISABLE_STUN |
59 cricket::PORTALLOCATOR_DISABLE_RELAY);
60 }
61
~FakePortAllocatorSession()62 FakePortAllocatorSession::~FakePortAllocatorSession() {
63 }
64
ConfigReady(cricket::PortConfiguration * config)65 void FakePortAllocatorSession::ConfigReady(
66 cricket::PortConfiguration* config) {
67 // Filter out non-UDP relay ports, so that we don't try using TCP.
68 for (cricket::PortConfiguration::RelayList::iterator relay =
69 config->relays.begin(); relay != config->relays.end(); ++relay) {
70 cricket::PortList filtered_ports;
71 for (cricket::PortList::iterator port =
72 relay->ports.begin(); port != relay->ports.end(); ++port) {
73 if (port->proto == cricket::PROTO_UDP) {
74 filtered_ports.push_back(*port);
75 }
76 }
77 relay->ports = filtered_ports;
78 }
79 cricket::BasicPortAllocatorSession::ConfigReady(config);
80 }
81
SendSessionRequest(const std::string & host,int port)82 void FakePortAllocatorSession::SendSessionRequest(
83 const std::string& host,
84 int port) {
85 ReceiveSessionResponse(std::string());
86 }
87
88 } // namespace
89
90 // static
Create(scoped_refptr<FakeNetworkDispatcher> fake_network_dispatcher)91 scoped_ptr<FakePortAllocator> FakePortAllocator::Create(
92 scoped_refptr<FakeNetworkDispatcher> fake_network_dispatcher) {
93 scoped_ptr<FakePacketSocketFactory> socket_factory(
94 new FakePacketSocketFactory(fake_network_dispatcher.get()));
95 scoped_ptr<rtc::NetworkManager> network_manager(
96 new FakeNetworkManager(socket_factory->GetAddress()));
97
98 return scoped_ptr<FakePortAllocator>(
99 new FakePortAllocator(network_manager.Pass(), socket_factory.Pass()));
100 }
101
FakePortAllocator(scoped_ptr<rtc::NetworkManager> network_manager,scoped_ptr<FakePacketSocketFactory> socket_factory)102 FakePortAllocator::FakePortAllocator(
103 scoped_ptr<rtc::NetworkManager> network_manager,
104 scoped_ptr<FakePacketSocketFactory> socket_factory)
105 : HttpPortAllocatorBase(network_manager.get(),
106 socket_factory.get(),
107 std::string()),
108 network_manager_(network_manager.Pass()),
109 socket_factory_(socket_factory.Pass()) {}
110
~FakePortAllocator()111 FakePortAllocator::~FakePortAllocator() {
112 }
113
CreateSessionInternal(const std::string & content_name,int component,const std::string & ice_username_fragment,const std::string & ice_password)114 cricket::PortAllocatorSession* FakePortAllocator::CreateSessionInternal(
115 const std::string& content_name,
116 int component,
117 const std::string& ice_username_fragment,
118 const std::string& ice_password) {
119 return new FakePortAllocatorSession(
120 this, content_name, component, ice_username_fragment, ice_password,
121 stun_hosts(), relay_hosts(), relay_token());
122 }
123
124 } // namespace remoting
125