OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2010 The WebRTC Project Authors. All rights reserved. | |
3 * | |
4 * Use of this source code is governed by a BSD-style license | |
5 * that can be found in the LICENSE file in the root of the source | |
6 * tree. An additional intellectual property rights grant can be found | |
7 * in the file PATENTS. All contributing project authors may | |
8 * be found in the AUTHORS file in the root of the source tree. | |
9 */ | |
10 | |
11 #ifndef WEBRTC_P2P_BASE_FAKEPORTALLOCATOR_H_ | |
12 #define WEBRTC_P2P_BASE_FAKEPORTALLOCATOR_H_ | |
13 | |
14 #include <memory> | |
15 #include <string> | |
16 #include <vector> | |
17 | |
18 #include "webrtc/p2p/base/basicpacketsocketfactory.h" | |
19 #include "webrtc/p2p/base/portallocator.h" | |
20 #include "webrtc/p2p/base/udpport.h" | |
21 #include "webrtc/base/scoped_ptr.h" | |
22 | |
23 namespace rtc { | |
24 class SocketFactory; | |
25 class Thread; | |
26 } | |
27 | |
28 namespace cricket { | |
29 | |
30 class TestUDPPort : public UDPPort { | |
31 public: | |
32 static TestUDPPort* Create(rtc::Thread* thread, | |
33 rtc::PacketSocketFactory* factory, | |
34 rtc::Network* network, | |
35 const rtc::IPAddress& ip, | |
36 uint16_t min_port, | |
37 uint16_t max_port, | |
38 const std::string& username, | |
39 const std::string& password, | |
40 const std::string& origin, | |
41 bool emit_localhost_for_anyaddress) { | |
42 TestUDPPort* port = new TestUDPPort(thread, factory, network, ip, min_port, | |
43 max_port, username, password, origin, | |
44 emit_localhost_for_anyaddress); | |
45 if (!port->Init()) { | |
46 delete port; | |
47 port = nullptr; | |
48 } | |
49 return port; | |
50 } | |
51 void SendBindingResponse(StunMessage* request, | |
52 const rtc::SocketAddress& addr) override { | |
53 UDPPort::SendBindingResponse(request, addr); | |
54 sent_binding_response_ = true; | |
55 } | |
56 bool sent_binding_response() { return sent_binding_response_; } | |
57 void set_sent_binding_response(bool response) { | |
58 sent_binding_response_ = response; | |
59 } | |
60 | |
61 protected: | |
62 TestUDPPort(rtc::Thread* thread, | |
63 rtc::PacketSocketFactory* factory, | |
64 rtc::Network* network, | |
65 const rtc::IPAddress& ip, | |
66 uint16_t min_port, | |
67 uint16_t max_port, | |
68 const std::string& username, | |
69 const std::string& password, | |
70 const std::string& origin, | |
71 bool emit_localhost_for_anyaddress) | |
72 : UDPPort(thread, | |
73 factory, | |
74 network, | |
75 ip, | |
76 min_port, | |
77 max_port, | |
78 username, | |
79 password, | |
80 origin, | |
81 emit_localhost_for_anyaddress) {} | |
82 | |
83 bool sent_binding_response_ = false; | |
84 }; | |
85 | |
86 class FakePortAllocatorSession : public PortAllocatorSession { | |
87 public: | |
88 FakePortAllocatorSession(PortAllocator* allocator, | |
89 rtc::Thread* worker_thread, | |
90 rtc::PacketSocketFactory* factory, | |
91 const std::string& content_name, | |
92 int component, | |
93 const std::string& ice_ufrag, | |
94 const std::string& ice_pwd) | |
95 : PortAllocatorSession(content_name, | |
96 component, | |
97 ice_ufrag, | |
98 ice_pwd, | |
99 allocator->flags()), | |
100 worker_thread_(worker_thread), | |
101 factory_(factory), | |
102 network_("network", "unittest", rtc::IPAddress(INADDR_LOOPBACK), 8), | |
103 port_(), | |
104 running_(false), | |
105 port_config_count_(0), | |
106 stun_servers_(allocator->stun_servers()), | |
107 turn_servers_(allocator->turn_servers()), | |
108 candidate_filter_(allocator->candidate_filter()) { | |
109 network_.AddIP(rtc::IPAddress(INADDR_LOOPBACK)); | |
110 } | |
111 | |
112 void StartGettingPorts() override { | |
113 if (!port_) { | |
114 port_.reset(TestUDPPort::Create(worker_thread_, factory_, &network_, | |
115 network_.GetBestIP(), 0, 0, username(), | |
116 password(), std::string(), false)); | |
117 AddPort(port_.get()); | |
118 } | |
119 ++port_config_count_; | |
120 running_ = true; | |
121 } | |
122 | |
123 void StopGettingPorts() override { running_ = false; } | |
124 bool IsGettingPorts() override { return running_; } | |
125 void ClearGettingPorts() override {} | |
126 std::vector<PortInterface*> ReadyPorts() const override { | |
127 return ready_ports_; | |
128 } | |
129 std::vector<Candidate> ReadyCandidates() const override { | |
130 return candidates_; | |
131 } | |
132 bool CandidatesAllocationDone() const override { return allocation_done_; } | |
133 | |
134 int port_config_count() { return port_config_count_; } | |
135 | |
136 const ServerAddresses& stun_servers() const { return stun_servers_; } | |
137 | |
138 const std::vector<RelayServerConfig>& turn_servers() const { | |
139 return turn_servers_; | |
140 } | |
141 | |
142 uint32_t candidate_filter() const { return candidate_filter_; } | |
143 | |
144 void AddPort(cricket::Port* port) { | |
145 port->set_component(component()); | |
146 port->set_generation(generation()); | |
147 port->SignalPortComplete.connect(this, | |
148 &FakePortAllocatorSession::OnPortComplete); | |
149 port->PrepareAddress(); | |
150 ready_ports_.push_back(port); | |
151 SignalPortReady(this, port); | |
152 } | |
153 void OnPortComplete(cricket::Port* port) { | |
154 const std::vector<Candidate>& candidates = port->Candidates(); | |
155 candidates_.insert(candidates_.end(), candidates.begin(), candidates.end()); | |
156 SignalCandidatesReady(this, candidates); | |
157 | |
158 allocation_done_ = true; | |
159 SignalCandidatesAllocationDone(this); | |
160 } | |
161 | |
162 int transport_info_update_count() const { | |
163 return transport_info_update_count_; | |
164 } | |
165 | |
166 protected: | |
167 void UpdateIceParametersInternal() override { | |
168 // Since this class is a fake and this method only is overridden for tests, | |
169 // we don't need to actually update the transport info. | |
170 ++transport_info_update_count_; | |
171 } | |
172 | |
173 private: | |
174 rtc::Thread* worker_thread_; | |
175 rtc::PacketSocketFactory* factory_; | |
176 rtc::Network network_; | |
177 std::unique_ptr<cricket::Port> port_; | |
178 bool running_; | |
179 int port_config_count_; | |
180 std::vector<Candidate> candidates_; | |
181 std::vector<PortInterface*> ready_ports_; | |
182 bool allocation_done_ = false; | |
183 ServerAddresses stun_servers_; | |
184 std::vector<RelayServerConfig> turn_servers_; | |
185 uint32_t candidate_filter_; | |
186 int transport_info_update_count_ = 0; | |
187 }; | |
188 | |
189 class FakePortAllocator : public cricket::PortAllocator { | |
190 public: | |
191 FakePortAllocator(rtc::Thread* worker_thread, | |
192 rtc::PacketSocketFactory* factory) | |
193 : worker_thread_(worker_thread), factory_(factory) { | |
194 if (factory_ == NULL) { | |
195 owned_factory_.reset(new rtc::BasicPacketSocketFactory(worker_thread_)); | |
196 factory_ = owned_factory_.get(); | |
197 } | |
198 } | |
199 | |
200 void SetNetworkIgnoreMask(int network_ignore_mask) override {} | |
201 | |
202 cricket::PortAllocatorSession* CreateSessionInternal( | |
203 const std::string& content_name, | |
204 int component, | |
205 const std::string& ice_ufrag, | |
206 const std::string& ice_pwd) override { | |
207 return new FakePortAllocatorSession(this, worker_thread_, factory_, | |
208 content_name, component, ice_ufrag, | |
209 ice_pwd); | |
210 } | |
211 | |
212 private: | |
213 rtc::Thread* worker_thread_; | |
214 rtc::PacketSocketFactory* factory_; | |
215 std::unique_ptr<rtc::BasicPacketSocketFactory> owned_factory_; | |
216 }; | |
217 | |
218 } // namespace cricket | |
219 | |
220 #endif // WEBRTC_P2P_BASE_FAKEPORTALLOCATOR_H_ | |
OLD | NEW |