OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2009 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2009 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #ifndef WEBRTC_P2P_BASE_FAKESESSION_H_ | 11 #ifndef WEBRTC_P2P_BASE_FAKETRANSPORTCONTROLLER_H_ |
12 #define WEBRTC_P2P_BASE_FAKESESSION_H_ | 12 #define WEBRTC_P2P_BASE_FAKETRANSPORTCONTROLLER_H_ |
13 | 13 |
14 #include <map> | 14 #include <map> |
15 #include <string> | 15 #include <string> |
16 #include <vector> | 16 #include <vector> |
17 | 17 |
18 #include "webrtc/p2p/base/session.h" | |
19 #include "webrtc/p2p/base/transport.h" | 18 #include "webrtc/p2p/base/transport.h" |
20 #include "webrtc/p2p/base/transportchannel.h" | 19 #include "webrtc/p2p/base/transportchannel.h" |
20 #include "webrtc/p2p/base/transportcontroller.h" | |
21 #include "webrtc/p2p/base/transportchannelimpl.h" | 21 #include "webrtc/p2p/base/transportchannelimpl.h" |
22 #include "webrtc/base/bind.h" | |
22 #include "webrtc/base/buffer.h" | 23 #include "webrtc/base/buffer.h" |
23 #include "webrtc/base/fakesslidentity.h" | 24 #include "webrtc/base/fakesslidentity.h" |
24 #include "webrtc/base/messagequeue.h" | 25 #include "webrtc/base/messagequeue.h" |
25 #include "webrtc/base/sigslot.h" | 26 #include "webrtc/base/sigslot.h" |
26 #include "webrtc/base/sslfingerprint.h" | 27 #include "webrtc/base/sslfingerprint.h" |
28 #include "webrtc/base/thread.h" | |
27 | 29 |
28 namespace cricket { | 30 namespace cricket { |
29 | 31 |
30 class FakeTransport; | 32 class FakeTransport; |
31 | 33 |
32 struct PacketMessageData : public rtc::MessageData { | 34 struct PacketMessageData : public rtc::MessageData { |
33 PacketMessageData(const char* data, size_t len) : packet(data, len) { | 35 PacketMessageData(const char* data, size_t len) : packet(data, len) {} |
34 } | |
35 rtc::Buffer packet; | 36 rtc::Buffer packet; |
36 }; | 37 }; |
37 | 38 |
38 // Fake transport channel class, which can be passed to anything that needs a | 39 // Fake transport channel class, which can be passed to anything that needs a |
39 // transport channel. Can be informed of another FakeTransportChannel via | 40 // transport channel. Can be informed of another FakeTransportChannel via |
40 // SetDestination. | 41 // SetDestination. |
41 // TODO(hbos): Move implementation to .cc file, this and other classes in file. | 42 // TODO(hbos): Move implementation to .cc file, this and other classes in file. |
42 class FakeTransportChannel : public TransportChannelImpl, | 43 class FakeTransportChannel : public TransportChannelImpl, |
43 public rtc::MessageHandler { | 44 public rtc::MessageHandler { |
44 public: | 45 public: |
45 explicit FakeTransportChannel(Transport* transport, | 46 explicit FakeTransportChannel(Transport* transport, |
46 const std::string& content_name, | 47 const std::string& name, |
47 int component) | 48 int component) |
48 : TransportChannelImpl(content_name, component), | 49 : TransportChannelImpl(name, component), |
49 transport_(transport), | 50 transport_(transport), |
50 dest_(nullptr), | 51 dtls_fingerprint_("", nullptr, 0) {} |
51 state_(STATE_INIT), | 52 ~FakeTransportChannel() { Reset(); } |
52 async_(false), | |
53 do_dtls_(false), | |
54 role_(ICEROLE_UNKNOWN), | |
55 tiebreaker_(0), | |
56 remote_ice_mode_(ICEMODE_FULL), | |
57 dtls_fingerprint_("", nullptr, 0), | |
58 ssl_role_(rtc::SSL_CLIENT), | |
59 connection_count_(0) { | |
60 } | |
61 ~FakeTransportChannel() { | |
62 Reset(); | |
63 } | |
64 | 53 |
65 uint64 IceTiebreaker() const { return tiebreaker_; } | 54 uint64 IceTiebreaker() const { return tiebreaker_; } |
66 IceMode remote_ice_mode() const { return remote_ice_mode_; } | 55 IceMode remote_ice_mode() const { return remote_ice_mode_; } |
67 const std::string& ice_ufrag() const { return ice_ufrag_; } | 56 const std::string& ice_ufrag() const { return ice_ufrag_; } |
68 const std::string& ice_pwd() const { return ice_pwd_; } | 57 const std::string& ice_pwd() const { return ice_pwd_; } |
69 const std::string& remote_ice_ufrag() const { return remote_ice_ufrag_; } | 58 const std::string& remote_ice_ufrag() const { return remote_ice_ufrag_; } |
70 const std::string& remote_ice_pwd() const { return remote_ice_pwd_; } | 59 const std::string& remote_ice_pwd() const { return remote_ice_pwd_; } |
71 const rtc::SSLFingerprint& dtls_fingerprint() const { | 60 const rtc::SSLFingerprint& dtls_fingerprint() const { |
72 return dtls_fingerprint_; | 61 return dtls_fingerprint_; |
73 } | 62 } |
74 | 63 |
75 void SetAsync(bool async) { | 64 // If async, will send packets by "Post"-ing to message queue instead of |
76 async_ = async; | 65 // synchronously "Send"-ing. |
77 } | 66 void SetAsync(bool async) { async_ = async; } |
78 | 67 |
79 Transport* GetTransport() override { | 68 Transport* GetTransport() override { return transport_; } |
80 return transport_; | |
81 } | |
82 | 69 |
83 TransportChannelState GetState() const override { | 70 TransportChannelState GetState() const override { |
84 if (connection_count_ == 0) { | 71 if (connection_count_ == 0) { |
85 return TransportChannelState::STATE_FAILED; | 72 return had_connection_ ? TransportChannelState::STATE_FAILED |
73 : TransportChannelState::STATE_INIT; | |
86 } | 74 } |
87 | 75 |
88 if (connection_count_ == 1) { | 76 if (connection_count_ == 1) { |
89 return TransportChannelState::STATE_COMPLETED; | 77 return TransportChannelState::STATE_COMPLETED; |
90 } | 78 } |
91 | 79 |
92 return TransportChannelState::STATE_FAILED; | 80 return TransportChannelState::STATE_CONNECTING; |
93 } | 81 } |
94 | 82 |
95 void SetIceRole(IceRole role) override { role_ = role; } | 83 void SetIceRole(IceRole role) override { role_ = role; } |
96 IceRole GetIceRole() const override { return role_; } | 84 IceRole GetIceRole() const override { return role_; } |
97 void SetIceTiebreaker(uint64 tiebreaker) override { | 85 void SetIceTiebreaker(uint64 tiebreaker) override { |
98 tiebreaker_ = tiebreaker; | 86 tiebreaker_ = tiebreaker; |
99 } | 87 } |
100 void SetIceCredentials(const std::string& ice_ufrag, | 88 void SetIceCredentials(const std::string& ice_ufrag, |
101 const std::string& ice_pwd) override { | 89 const std::string& ice_pwd) override { |
102 ice_ufrag_ = ice_ufrag; | 90 ice_ufrag_ = ice_ufrag; |
103 ice_pwd_ = ice_pwd; | 91 ice_pwd_ = ice_pwd; |
104 } | 92 } |
105 void SetRemoteIceCredentials(const std::string& ice_ufrag, | 93 void SetRemoteIceCredentials(const std::string& ice_ufrag, |
106 const std::string& ice_pwd) override { | 94 const std::string& ice_pwd) override { |
107 remote_ice_ufrag_ = ice_ufrag; | 95 remote_ice_ufrag_ = ice_ufrag; |
108 remote_ice_pwd_ = ice_pwd; | 96 remote_ice_pwd_ = ice_pwd; |
109 } | 97 } |
110 | 98 |
111 void SetRemoteIceMode(IceMode mode) override { remote_ice_mode_ = mode; } | 99 void SetRemoteIceMode(IceMode mode) override { remote_ice_mode_ = mode; } |
112 bool SetRemoteFingerprint(const std::string& alg, const uint8* digest, | 100 bool SetRemoteFingerprint(const std::string& alg, |
101 const uint8* digest, | |
113 size_t digest_len) override { | 102 size_t digest_len) override { |
114 dtls_fingerprint_ = rtc::SSLFingerprint(alg, digest, digest_len); | 103 dtls_fingerprint_ = rtc::SSLFingerprint(alg, digest, digest_len); |
115 return true; | 104 return true; |
116 } | 105 } |
117 bool SetSslRole(rtc::SSLRole role) override { | 106 bool SetSslRole(rtc::SSLRole role) override { |
118 ssl_role_ = role; | 107 ssl_role_ = role; |
119 return true; | 108 return true; |
120 } | 109 } |
121 bool GetSslRole(rtc::SSLRole* role) const override { | 110 bool GetSslRole(rtc::SSLRole* role) const override { |
122 *role = ssl_role_; | 111 *role = ssl_role_; |
123 return true; | 112 return true; |
124 } | 113 } |
125 | 114 |
126 void Connect() override { | 115 void Connect() override { |
127 if (state_ == STATE_INIT) { | 116 if (state_ == STATE_INIT) { |
128 state_ = STATE_CONNECTING; | 117 state_ = STATE_CONNECTING; |
129 } | 118 } |
130 } | 119 } |
131 virtual void Reset() { | 120 |
121 void MaybeStartGathering() override { | |
122 if (gathering_state_ == kIceGatheringNew) { | |
123 gathering_state_ = kIceGatheringGathering; | |
124 SignalGatheringState(this); | |
125 } | |
126 } | |
127 | |
128 IceGatheringState gathering_state() const override { | |
129 return gathering_state_; | |
130 } | |
131 | |
132 void Reset() { | |
132 if (state_ != STATE_INIT) { | 133 if (state_ != STATE_INIT) { |
133 state_ = STATE_INIT; | 134 state_ = STATE_INIT; |
134 if (dest_) { | 135 if (dest_) { |
135 dest_->state_ = STATE_INIT; | 136 dest_->state_ = STATE_INIT; |
136 dest_->dest_ = NULL; | 137 dest_->dest_ = nullptr; |
137 dest_ = NULL; | 138 dest_ = nullptr; |
138 } | 139 } |
139 } | 140 } |
140 } | 141 } |
141 | 142 |
142 void SetWritable(bool writable) { | 143 void SetWritable(bool writable) { set_writable(writable); } |
143 set_writable(writable); | |
144 } | |
145 | 144 |
146 void SetDestination(FakeTransportChannel* dest) { | 145 void SetDestination(FakeTransportChannel* dest) { |
147 if (state_ == STATE_CONNECTING && dest) { | 146 if (state_ == STATE_CONNECTING && dest) { |
148 // This simulates the delivery of candidates. | 147 // This simulates the delivery of candidates. |
149 dest_ = dest; | 148 dest_ = dest; |
150 dest_->dest_ = this; | 149 dest_->dest_ = this; |
151 if (certificate_ && dest_->certificate_) { | 150 if (local_cert_ && dest_->local_cert_) { |
152 do_dtls_ = true; | 151 do_dtls_ = true; |
153 dest_->do_dtls_ = true; | 152 dest_->do_dtls_ = true; |
154 NegotiateSrtpCiphers(); | 153 NegotiateSrtpCiphers(); |
155 } | 154 } |
156 state_ = STATE_CONNECTED; | 155 state_ = STATE_CONNECTED; |
157 dest_->state_ = STATE_CONNECTED; | 156 dest_->state_ = STATE_CONNECTED; |
158 set_writable(true); | 157 set_writable(true); |
159 dest_->set_writable(true); | 158 dest_->set_writable(true); |
160 } else if (state_ == STATE_CONNECTED && !dest) { | 159 } else if (state_ == STATE_CONNECTED && !dest) { |
161 // Simulates loss of connectivity, by asymmetrically forgetting dest_. | 160 // Simulates loss of connectivity, by asymmetrically forgetting dest_. |
162 dest_ = NULL; | 161 dest_ = nullptr; |
163 state_ = STATE_CONNECTING; | 162 state_ = STATE_CONNECTING; |
164 set_writable(false); | 163 set_writable(false); |
165 } | 164 } |
166 } | 165 } |
167 | 166 |
168 void SetConnectionCount(size_t connection_count) { | 167 void SetConnectionCount(size_t connection_count) { |
169 size_t old_connection_count = connection_count_; | 168 size_t old_connection_count = connection_count_; |
170 connection_count_ = connection_count; | 169 connection_count_ = connection_count; |
170 if (connection_count) | |
171 had_connection_ = true; | |
171 if (connection_count_ < old_connection_count) | 172 if (connection_count_ < old_connection_count) |
172 SignalConnectionRemoved(this); | 173 SignalConnectionRemoved(this); |
173 } | 174 } |
174 | 175 |
175 void SetReceiving(bool receiving) { | 176 void SetCandidatesGatheringComplete() { |
176 set_receiving(receiving); | 177 if (gathering_state_ != kIceGatheringComplete) { |
178 gathering_state_ = kIceGatheringComplete; | |
179 SignalGatheringState(this); | |
180 } | |
177 } | 181 } |
178 | 182 |
179 void SetReceivingTimeout(int timeout) override {} | 183 void SetReceiving(bool receiving) { set_receiving(receiving); } |
180 | 184 |
181 int SendPacket(const char* data, size_t len, | 185 void SetReceivingTimeout(int timeout) override { |
182 const rtc::PacketOptions& options, int flags) override { | 186 receiving_timeout_ = timeout; |
187 } | |
188 | |
189 int receiving_timeout() const { return receiving_timeout_; } | |
190 | |
191 int SendPacket(const char* data, | |
192 size_t len, | |
193 const rtc::PacketOptions& options, | |
194 int flags) override { | |
183 if (state_ != STATE_CONNECTED) { | 195 if (state_ != STATE_CONNECTED) { |
184 return -1; | 196 return -1; |
185 } | 197 } |
186 | 198 |
187 if (flags != PF_SRTP_BYPASS && flags != 0) { | 199 if (flags != PF_SRTP_BYPASS && flags != 0) { |
188 return -1; | 200 return -1; |
189 } | 201 } |
190 | 202 |
191 PacketMessageData* packet = new PacketMessageData(data, len); | 203 PacketMessageData* packet = new PacketMessageData(data, len); |
192 if (async_) { | 204 if (async_) { |
193 rtc::Thread::Current()->Post(this, 0, packet); | 205 rtc::Thread::Current()->Post(this, 0, packet); |
194 } else { | 206 } else { |
195 rtc::Thread::Current()->Send(this, 0, packet); | 207 rtc::Thread::Current()->Send(this, 0, packet); |
196 } | 208 } |
197 return static_cast<int>(len); | 209 return static_cast<int>(len); |
198 } | 210 } |
199 int SetOption(rtc::Socket::Option opt, int value) override { | 211 int SetOption(rtc::Socket::Option opt, int value) override { return true; } |
200 return true; | 212 bool GetOption(rtc::Socket::Option opt, int* value) override { return true; } |
213 int GetError() override { return 0; } | |
214 | |
215 void AddRemoteCandidate(const Candidate& candidate) override { | |
216 remote_candidates_.push_back(candidate); | |
201 } | 217 } |
202 bool GetOption(rtc::Socket::Option opt, int* value) override { | 218 const Candidates& remote_candidates() const { return remote_candidates_; } |
203 return true; | |
204 } | |
205 int GetError() override { | |
206 return 0; | |
207 } | |
208 | |
209 void OnSignalingReady() override { | |
210 } | |
211 void OnCandidate(const Candidate& candidate) override { | |
212 } | |
213 | 219 |
214 void OnMessage(rtc::Message* msg) override { | 220 void OnMessage(rtc::Message* msg) override { |
215 PacketMessageData* data = static_cast<PacketMessageData*>( | 221 PacketMessageData* data = static_cast<PacketMessageData*>(msg->pdata); |
216 msg->pdata); | |
217 dest_->SignalReadPacket(dest_, data->packet.data<char>(), | 222 dest_->SignalReadPacket(dest_, data->packet.data<char>(), |
218 data->packet.size(), rtc::CreatePacketTime(0), 0); | 223 data->packet.size(), rtc::CreatePacketTime(0), 0); |
219 delete data; | 224 delete data; |
220 } | 225 } |
221 | 226 |
222 bool SetLocalCertificate( | 227 bool SetLocalCertificate( |
223 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) override { | 228 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) { |
224 certificate_ = certificate; | 229 local_cert_ = certificate; |
225 return true; | 230 return true; |
226 } | 231 } |
227 | 232 |
228 void SetRemoteSSLCertificate(rtc::FakeSSLCertificate* cert) { | 233 void SetRemoteSSLCertificate(rtc::FakeSSLCertificate* cert) { |
229 remote_cert_ = cert; | 234 remote_cert_ = cert; |
230 } | 235 } |
231 | 236 |
232 bool IsDtlsActive() const override { | 237 bool IsDtlsActive() const override { return do_dtls_; } |
233 return do_dtls_; | |
234 } | |
235 | 238 |
236 bool SetSrtpCiphers(const std::vector<std::string>& ciphers) override { | 239 bool SetSrtpCiphers(const std::vector<std::string>& ciphers) override { |
237 srtp_ciphers_ = ciphers; | 240 srtp_ciphers_ = ciphers; |
238 return true; | 241 return true; |
239 } | 242 } |
240 | 243 |
241 bool GetSrtpCipher(std::string* cipher) override { | 244 bool GetSrtpCipher(std::string* cipher) override { |
242 if (!chosen_srtp_cipher_.empty()) { | 245 if (!chosen_srtp_cipher_.empty()) { |
243 *cipher = chosen_srtp_cipher_; | 246 *cipher = chosen_srtp_cipher_; |
244 return true; | 247 return true; |
245 } | 248 } |
246 return false; | 249 return false; |
247 } | 250 } |
248 | 251 |
249 bool GetSslCipher(std::string* cipher) override { | 252 bool GetSslCipher(std::string* cipher) override { return false; } |
250 return false; | |
251 } | |
252 | 253 |
253 rtc::scoped_refptr<rtc::RTCCertificate> | 254 rtc::scoped_refptr<rtc::RTCCertificate> GetLocalCertificate() const { |
254 GetLocalCertificate() const override { | 255 return local_cert_; |
255 return certificate_; | |
256 } | 256 } |
257 | 257 |
258 bool GetRemoteSSLCertificate(rtc::SSLCertificate** cert) const override { | 258 bool GetRemoteSSLCertificate(rtc::SSLCertificate** cert) const override { |
259 if (!remote_cert_) | 259 if (!remote_cert_) |
260 return false; | 260 return false; |
261 | 261 |
262 *cert = remote_cert_->GetReference(); | 262 *cert = remote_cert_->GetReference(); |
263 return true; | 263 return true; |
264 } | 264 } |
265 | 265 |
266 bool ExportKeyingMaterial(const std::string& label, | 266 bool ExportKeyingMaterial(const std::string& label, |
267 const uint8* context, | 267 const uint8* context, |
268 size_t context_len, | 268 size_t context_len, |
269 bool use_context, | 269 bool use_context, |
270 uint8* result, | 270 uint8* result, |
271 size_t result_len) override { | 271 size_t result_len) override { |
272 if (!chosen_srtp_cipher_.empty()) { | 272 if (!chosen_srtp_cipher_.empty()) { |
273 memset(result, 0xff, result_len); | 273 memset(result, 0xff, result_len); |
274 return true; | 274 return true; |
275 } | 275 } |
276 | 276 |
277 return false; | 277 return false; |
278 } | 278 } |
279 | 279 |
280 virtual void NegotiateSrtpCiphers() { | 280 void NegotiateSrtpCiphers() { |
281 for (std::vector<std::string>::const_iterator it1 = srtp_ciphers_.begin(); | 281 for (std::vector<std::string>::const_iterator it1 = srtp_ciphers_.begin(); |
282 it1 != srtp_ciphers_.end(); ++it1) { | 282 it1 != srtp_ciphers_.end(); ++it1) { |
283 for (std::vector<std::string>::const_iterator it2 = | 283 for (std::vector<std::string>::const_iterator it2 = |
284 dest_->srtp_ciphers_.begin(); | 284 dest_->srtp_ciphers_.begin(); |
285 it2 != dest_->srtp_ciphers_.end(); ++it2) { | 285 it2 != dest_->srtp_ciphers_.end(); ++it2) { |
286 if (*it1 == *it2) { | 286 if (*it1 == *it2) { |
287 chosen_srtp_cipher_ = *it1; | 287 chosen_srtp_cipher_ = *it1; |
288 dest_->chosen_srtp_cipher_ = *it2; | 288 dest_->chosen_srtp_cipher_ = *it2; |
289 return; | 289 return; |
290 } | 290 } |
291 } | 291 } |
292 } | 292 } |
293 } | 293 } |
294 | 294 |
295 bool GetStats(ConnectionInfos* infos) override { | 295 bool GetStats(ConnectionInfos* infos) override { |
296 ConnectionInfo info; | 296 ConnectionInfo info; |
297 infos->clear(); | 297 infos->clear(); |
298 infos->push_back(info); | 298 infos->push_back(info); |
299 return true; | 299 return true; |
300 } | 300 } |
301 | 301 |
302 void set_ssl_max_protocol_version(rtc::SSLProtocolVersion version) { | |
303 ssl_max_version_ = version; | |
304 } | |
305 rtc::SSLProtocolVersion ssl_max_protocol_version() const { | |
306 return ssl_max_version_; | |
307 } | |
308 | |
302 private: | 309 private: |
303 enum State { STATE_INIT, STATE_CONNECTING, STATE_CONNECTED }; | 310 enum State { STATE_INIT, STATE_CONNECTING, STATE_CONNECTED }; |
304 Transport* transport_; | 311 Transport* transport_; |
305 FakeTransportChannel* dest_; | 312 FakeTransportChannel* dest_ = nullptr; |
306 State state_; | 313 State state_ = STATE_INIT; |
307 bool async_; | 314 bool async_ = false; |
308 rtc::scoped_refptr<rtc::RTCCertificate> certificate_; | 315 Candidates remote_candidates_; |
309 rtc::FakeSSLCertificate* remote_cert_; | 316 rtc::scoped_refptr<rtc::RTCCertificate> local_cert_; |
310 bool do_dtls_; | 317 rtc::FakeSSLCertificate* remote_cert_ = nullptr; |
318 bool do_dtls_ = false; | |
311 std::vector<std::string> srtp_ciphers_; | 319 std::vector<std::string> srtp_ciphers_; |
312 std::string chosen_srtp_cipher_; | 320 std::string chosen_srtp_cipher_; |
313 IceRole role_; | 321 int receiving_timeout_ = -1; |
314 uint64 tiebreaker_; | 322 IceRole role_ = ICEROLE_UNKNOWN; |
323 uint64 tiebreaker_ = 0; | |
315 std::string ice_ufrag_; | 324 std::string ice_ufrag_; |
316 std::string ice_pwd_; | 325 std::string ice_pwd_; |
317 std::string remote_ice_ufrag_; | 326 std::string remote_ice_ufrag_; |
318 std::string remote_ice_pwd_; | 327 std::string remote_ice_pwd_; |
319 IceMode remote_ice_mode_; | 328 IceMode remote_ice_mode_ = ICEMODE_FULL; |
329 rtc::SSLProtocolVersion ssl_max_version_ = rtc::SSL_PROTOCOL_DTLS_10; | |
320 rtc::SSLFingerprint dtls_fingerprint_; | 330 rtc::SSLFingerprint dtls_fingerprint_; |
321 rtc::SSLRole ssl_role_; | 331 rtc::SSLRole ssl_role_ = rtc::SSL_CLIENT; |
322 size_t connection_count_; | 332 size_t connection_count_ = 0; |
333 IceGatheringState gathering_state_ = kIceGatheringNew; | |
334 bool had_connection_ = false; | |
323 }; | 335 }; |
324 | 336 |
325 // Fake transport class, which can be passed to anything that needs a Transport. | 337 // Fake transport class, which can be passed to anything that needs a Transport. |
326 // Can be informed of another FakeTransport via SetDestination (low-tech way | 338 // Can be informed of another FakeTransport via SetDestination (low-tech way |
327 // of doing candidates) | 339 // of doing candidates) |
328 class FakeTransport : public Transport { | 340 class FakeTransport : public Transport { |
329 public: | 341 public: |
330 typedef std::map<int, FakeTransportChannel*> ChannelMap; | 342 typedef std::map<int, FakeTransportChannel*> ChannelMap; |
331 FakeTransport(rtc::Thread* signaling_thread, | 343 |
332 rtc::Thread* worker_thread, | 344 explicit FakeTransport(const std::string& name) : Transport(name, nullptr) {} |
333 const std::string& content_name, | 345 |
334 PortAllocator* alllocator = nullptr) | 346 // Note that we only have a constructor with the allocator parameter so it can |
335 : Transport(signaling_thread, worker_thread, | 347 // be wrapped by a DtlsTransport. |
336 content_name, nullptr), | 348 FakeTransport(const std::string& name, PortAllocator* allocator) |
337 dest_(nullptr), | 349 : Transport(name, nullptr) {} |
338 async_(false) { | 350 |
339 } | 351 ~FakeTransport() { DestroyAllChannels(); } |
340 ~FakeTransport() { | |
341 DestroyAllChannels(); | |
342 } | |
343 | 352 |
344 const ChannelMap& channels() const { return channels_; } | 353 const ChannelMap& channels() const { return channels_; } |
345 | 354 |
355 // If async, will send packets by "Post"-ing to message queue instead of | |
356 // synchronously "Send"-ing. | |
346 void SetAsync(bool async) { async_ = async; } | 357 void SetAsync(bool async) { async_ = async; } |
347 void SetDestination(FakeTransport* dest) { | 358 void SetDestination(FakeTransport* dest) { |
348 dest_ = dest; | 359 dest_ = dest; |
349 for (ChannelMap::iterator it = channels_.begin(); it != channels_.end(); | 360 for (const auto& kv : channels_) { |
350 ++it) { | 361 kv.second->SetLocalCertificate(certificate_); |
351 it->second->SetLocalCertificate(certificate_); | 362 SetChannelDestination(kv.first, kv.second); |
352 SetChannelDestination(it->first, it->second); | |
353 } | 363 } |
354 } | 364 } |
355 | 365 |
356 void SetWritable(bool writable) { | 366 void SetWritable(bool writable) { |
357 for (ChannelMap::iterator it = channels_.begin(); it != channels_.end(); | 367 for (const auto& kv : channels_) { |
358 ++it) { | 368 kv.second->SetWritable(writable); |
359 it->second->SetWritable(writable); | |
360 } | 369 } |
361 } | 370 } |
362 | 371 |
363 void set_certificate( | 372 void SetLocalCertificate( |
364 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) { | 373 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) override { |
365 certificate_ = certificate; | 374 certificate_ = certificate; |
366 } | 375 } |
376 bool GetLocalCertificate( | |
377 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) override { | |
378 if (!certificate_) | |
379 return false; | |
380 | |
381 *certificate = certificate_; | |
382 return true; | |
383 } | |
384 | |
385 bool GetSslRole(rtc::SSLRole* role) const override { | |
386 if (channels_.empty()) { | |
387 return false; | |
388 } | |
389 return channels_.begin()->second->GetSslRole(role); | |
390 } | |
391 | |
392 bool SetSslMaxProtocolVersion(rtc::SSLProtocolVersion version) override { | |
393 ssl_max_version_ = version; | |
394 for (const auto& kv : channels_) { | |
395 kv.second->set_ssl_max_protocol_version(ssl_max_version_); | |
396 } | |
397 return true; | |
398 } | |
399 rtc::SSLProtocolVersion ssl_max_protocol_version() const { | |
400 return ssl_max_version_; | |
401 } | |
367 | 402 |
368 using Transport::local_description; | 403 using Transport::local_description; |
369 using Transport::remote_description; | 404 using Transport::remote_description; |
370 | 405 |
371 protected: | 406 protected: |
372 TransportChannelImpl* CreateTransportChannel(int component) override { | 407 TransportChannelImpl* CreateTransportChannel(int component) override { |
373 if (channels_.find(component) != channels_.end()) { | 408 if (channels_.find(component) != channels_.end()) { |
374 return NULL; | 409 return nullptr; |
375 } | 410 } |
376 FakeTransportChannel* channel = | 411 FakeTransportChannel* channel = |
377 new FakeTransportChannel(this, content_name(), component); | 412 new FakeTransportChannel(this, name(), component); |
413 channel->set_ssl_max_protocol_version(ssl_max_version_); | |
378 channel->SetAsync(async_); | 414 channel->SetAsync(async_); |
379 SetChannelDestination(component, channel); | 415 SetChannelDestination(component, channel); |
380 channels_[component] = channel; | 416 channels_[component] = channel; |
381 return channel; | 417 return channel; |
382 } | 418 } |
419 | |
383 void DestroyTransportChannel(TransportChannelImpl* channel) override { | 420 void DestroyTransportChannel(TransportChannelImpl* channel) override { |
384 channels_.erase(channel->component()); | 421 channels_.erase(channel->component()); |
385 delete channel; | 422 delete channel; |
386 } | 423 } |
387 void SetCertificate_w( | |
388 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) override { | |
389 certificate_ = certificate; | |
390 } | |
391 bool GetCertificate_w( | |
392 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) override { | |
393 if (!certificate_) | |
394 return false; | |
395 | |
396 *certificate = certificate_; | |
397 return true; | |
398 } | |
399 | 424 |
400 private: | 425 private: |
401 FakeTransportChannel* GetFakeChannel(int component) { | 426 FakeTransportChannel* GetFakeChannel(int component) { |
402 ChannelMap::iterator it = channels_.find(component); | 427 auto it = channels_.find(component); |
403 return (it != channels_.end()) ? it->second : NULL; | 428 return (it != channels_.end()) ? it->second : nullptr; |
404 } | 429 } |
405 void SetChannelDestination(int component, | 430 |
406 FakeTransportChannel* channel) { | 431 void SetChannelDestination(int component, FakeTransportChannel* channel) { |
407 FakeTransportChannel* dest_channel = NULL; | 432 FakeTransportChannel* dest_channel = nullptr; |
408 if (dest_) { | 433 if (dest_) { |
409 dest_channel = dest_->GetFakeChannel(component); | 434 dest_channel = dest_->GetFakeChannel(component); |
410 if (dest_channel) | 435 if (dest_channel) { |
411 dest_channel->SetLocalCertificate(dest_->certificate_); | 436 dest_channel->SetLocalCertificate(dest_->certificate_); |
437 } | |
412 } | 438 } |
413 channel->SetDestination(dest_channel); | 439 channel->SetDestination(dest_channel); |
414 } | 440 } |
415 | 441 |
416 // Note, this is distinct from the Channel map owned by Transport. | 442 // Note, this is distinct from the Channel map owned by Transport. |
417 // This map just tracks the FakeTransportChannels created by this class. | 443 // This map just tracks the FakeTransportChannels created by this class. |
444 // It's mainly needed so that we can access a FakeTransportChannel directly, | |
445 // even if wrapped by a DtlsTransportChannelWrapper. | |
418 ChannelMap channels_; | 446 ChannelMap channels_; |
419 FakeTransport* dest_; | 447 FakeTransport* dest_ = nullptr; |
420 bool async_; | 448 bool async_ = false; |
421 rtc::scoped_refptr<rtc::RTCCertificate> certificate_; | 449 rtc::scoped_refptr<rtc::RTCCertificate> certificate_; |
450 rtc::SSLProtocolVersion ssl_max_version_ = rtc::SSL_PROTOCOL_DTLS_10; | |
422 }; | 451 }; |
423 | 452 |
424 // Fake session class, which can be passed into a BaseChannel object for | 453 // Fake TransportController class, which can be passed into a BaseChannel object |
425 // test purposes. Can be connected to other FakeSessions via Connect(). | 454 // for test purposes. Can be connected to other FakeTransportControllers via |
426 class FakeSession : public BaseSession { | 455 // Connect(). |
456 // | |
457 // This fake is unusual in that for the most part, it's implemented with the | |
458 // real TransportController code, but with fake TransportChannels underneath. | |
459 class FakeTransportController : public TransportController { | |
427 public: | 460 public: |
428 explicit FakeSession() | 461 FakeTransportController() |
429 : BaseSession(rtc::Thread::Current(), | 462 : TransportController(rtc::Thread::Current(), |
430 rtc::Thread::Current(), | 463 rtc::Thread::Current(), |
431 NULL, "", "", true), | 464 nullptr), |
432 fail_create_channel_(false) { | 465 fail_create_channel_(false) {} |
433 } | 466 |
434 explicit FakeSession(bool initiator) | 467 explicit FakeTransportController(IceRole role) |
435 : BaseSession(rtc::Thread::Current(), | 468 : TransportController(rtc::Thread::Current(), |
436 rtc::Thread::Current(), | 469 rtc::Thread::Current(), |
437 NULL, "", "", initiator), | 470 nullptr), |
438 fail_create_channel_(false) { | 471 fail_create_channel_(false) { |
439 } | 472 SetIceRole(role); |
440 FakeSession(rtc::Thread* worker_thread, bool initiator) | |
441 : BaseSession(rtc::Thread::Current(), | |
442 worker_thread, | |
443 NULL, "", "", initiator), | |
444 fail_create_channel_(false) { | |
445 } | 473 } |
446 | 474 |
447 FakeTransport* GetTransport(const std::string& content_name) { | 475 explicit FakeTransportController(rtc::Thread* worker_thread) |
448 return static_cast<FakeTransport*>( | 476 : TransportController(rtc::Thread::Current(), worker_thread, nullptr), |
449 BaseSession::GetTransport(content_name)); | 477 fail_create_channel_(false) {} |
478 | |
479 FakeTransportController(rtc::Thread* worker_thread, IceRole role) | |
480 : TransportController(rtc::Thread::Current(), worker_thread, nullptr), | |
481 fail_create_channel_(false) { | |
482 SetIceRole(role); | |
450 } | 483 } |
451 | 484 |
452 void Connect(FakeSession* dest) { | 485 FakeTransport* GetTransport_w(const std::string& transport_name) { |
453 // Simulate the exchange of candidates. | 486 return static_cast<FakeTransport*>( |
454 CompleteNegotiation(); | 487 TransportController::GetTransport_w(transport_name)); |
455 dest->CompleteNegotiation(); | |
456 for (TransportMap::const_iterator it = transport_proxies().begin(); | |
457 it != transport_proxies().end(); ++it) { | |
458 static_cast<FakeTransport*>(it->second->impl())->SetDestination( | |
459 dest->GetTransport(it->first)); | |
460 } | |
461 } | 488 } |
462 | 489 |
463 TransportChannel* CreateChannel(const std::string& content_name, | 490 void Connect(FakeTransportController* dest) { |
464 int component) override { | 491 worker_thread()->Invoke<void>( |
492 rtc::Bind(&FakeTransportController::Connect_w, this, dest)); | |
493 } | |
494 | |
495 TransportChannel* CreateTransportChannel_w(const std::string& transport_name, | |
496 int component) override { | |
465 if (fail_create_channel_) { | 497 if (fail_create_channel_) { |
466 return NULL; | 498 return nullptr; |
467 } | 499 } |
468 return BaseSession::CreateChannel(content_name, component); | 500 return TransportController::CreateTransportChannel_w(transport_name, |
501 component); | |
469 } | 502 } |
470 | 503 |
471 void set_fail_channel_creation(bool fail_channel_creation) { | 504 void set_fail_channel_creation(bool fail_channel_creation) { |
472 fail_create_channel_ = fail_channel_creation; | 505 fail_create_channel_ = fail_channel_creation; |
473 } | 506 } |
474 | 507 |
475 // TODO: Hoist this into Session when we re-work the Session code. | 508 protected: |
476 void set_ssl_rtccertificate( | 509 Transport* CreateTransport_w(const std::string& transport_name) override { |
477 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) { | 510 return new FakeTransport(transport_name); |
478 for (TransportMap::const_iterator it = transport_proxies().begin(); | 511 } |
479 it != transport_proxies().end(); ++it) { | |
480 // We know that we have a FakeTransport* | |
481 | 512 |
482 static_cast<FakeTransport*>(it->second->impl())->set_certificate | 513 void Connect_w(FakeTransportController* dest) { |
483 (certificate); | 514 // Simulate the exchange of candidates. |
515 ConnectChannels_w(); | |
516 dest->ConnectChannels_w(); | |
517 for (auto& kv : transports()) { | |
518 FakeTransport* transport = static_cast<FakeTransport*>(kv.second); | |
519 transport->SetDestination(dest->GetTransport_w(kv.first)); | |
484 } | 520 } |
485 } | 521 } |
486 | 522 |
487 protected: | 523 void ConnectChannels_w() { |
488 Transport* CreateTransport(const std::string& content_name) override { | 524 for (auto& kv : transports()) { |
489 return new FakeTransport(signaling_thread(), worker_thread(), content_name); | 525 FakeTransport* transport = static_cast<FakeTransport*>(kv.second); |
490 } | 526 transport->ConnectChannels(); |
491 | 527 transport->MaybeStartGathering(); |
pthatcher1
2015/09/19 00:08:05
Do we want the FakeTransportController to auto-gat
Taylor Brandstetter
2015/09/19 00:59:23
This is something that only gets called in a test
| |
492 void CompleteNegotiation() { | |
493 for (TransportMap::const_iterator it = transport_proxies().begin(); | |
494 it != transport_proxies().end(); ++it) { | |
495 it->second->CompleteNegotiation(); | |
496 it->second->ConnectChannels(); | |
497 } | 528 } |
498 } | 529 } |
499 | 530 |
500 private: | 531 private: |
501 bool fail_create_channel_; | 532 bool fail_create_channel_; |
502 }; | 533 }; |
503 | 534 |
504 } // namespace cricket | 535 } // namespace cricket |
505 | 536 |
506 #endif // WEBRTC_P2P_BASE_FAKESESSION_H_ | 537 #endif // WEBRTC_P2P_BASE_FAKETRANSPORTCONTROLLER_H_ |
OLD | NEW |