blob: d3da2b27f74b45af2572452cc0aa7d815f5b1f76 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2009, Google, Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#ifndef TALK_P2P_BASE_FAKESESSION_H_
29#define TALK_P2P_BASE_FAKESESSION_H_
30
31#include <map>
32#include <string>
33#include <vector>
34
35#include "talk/base/buffer.h"
36#include "talk/base/sigslot.h"
37#include "talk/base/sslfingerprint.h"
38#include "talk/base/messagequeue.h"
39#include "talk/p2p/base/session.h"
40#include "talk/p2p/base/transport.h"
41#include "talk/p2p/base/transportchannel.h"
42#include "talk/p2p/base/transportchannelimpl.h"
43
44namespace cricket {
45
46class FakeTransport;
47
48struct PacketMessageData : public talk_base::MessageData {
49 PacketMessageData(const char* data, size_t len) : packet(data, len) {
50 }
51 talk_base::Buffer packet;
52};
53
54// Fake transport channel class, which can be passed to anything that needs a
55// transport channel. Can be informed of another FakeTransportChannel via
56// SetDestination.
57class FakeTransportChannel : public TransportChannelImpl,
58 public talk_base::MessageHandler {
59 public:
60 explicit FakeTransportChannel(Transport* transport,
61 const std::string& content_name,
62 int component)
63 : TransportChannelImpl(content_name, component),
64 transport_(transport),
65 dest_(NULL),
66 state_(STATE_INIT),
67 async_(false),
68 identity_(NULL),
69 do_dtls_(false),
mallinath@webrtc.orga5506692013-08-12 21:18:15 +000070 role_(ICEROLE_UNKNOWN),
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071 tiebreaker_(0),
72 ice_proto_(ICEPROTO_HYBRID),
73 remote_ice_mode_(ICEMODE_FULL),
74 dtls_fingerprint_("", NULL, 0) {
75 }
76 ~FakeTransportChannel() {
77 Reset();
78 }
79
mallinath@webrtc.orga5506692013-08-12 21:18:15 +000080 uint64 IceTiebreaker() const { return tiebreaker_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000081 TransportProtocol protocol() const { return ice_proto_; }
82 IceMode remote_ice_mode() const { return remote_ice_mode_; }
83 const std::string& ice_ufrag() const { return ice_ufrag_; }
84 const std::string& ice_pwd() const { return ice_pwd_; }
85 const std::string& remote_ice_ufrag() const { return remote_ice_ufrag_; }
86 const std::string& remote_ice_pwd() const { return remote_ice_pwd_; }
87 const talk_base::SSLFingerprint& dtls_fingerprint() const {
88 return dtls_fingerprint_;
89 }
90
91 void SetAsync(bool async) {
92 async_ = async;
93 }
94
95 virtual Transport* GetTransport() {
96 return transport_;
97 }
98
mallinath@webrtc.orga5506692013-08-12 21:18:15 +000099 virtual void SetIceRole(IceRole role) { role_ = role; }
100 virtual IceRole GetIceRole() const { return role_; }
101 virtual void SetIceTiebreaker(uint64 tiebreaker) { tiebreaker_ = tiebreaker; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000102 virtual void SetIceProtocolType(IceProtocolType type) { ice_proto_ = type; }
103 virtual void SetIceCredentials(const std::string& ice_ufrag,
104 const std::string& ice_pwd) {
105 ice_ufrag_ = ice_ufrag;
106 ice_pwd_ = ice_pwd;
107 }
108 virtual void SetRemoteIceCredentials(const std::string& ice_ufrag,
109 const std::string& ice_pwd) {
110 remote_ice_ufrag_ = ice_ufrag;
111 remote_ice_pwd_ = ice_pwd;
112 }
113
114 virtual void SetRemoteIceMode(IceMode mode) { remote_ice_mode_ = mode; }
115 virtual bool SetRemoteFingerprint(const std::string& alg, const uint8* digest,
116 size_t digest_len) {
117 dtls_fingerprint_ = talk_base::SSLFingerprint(alg, digest, digest_len);
118 return true;
119 }
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000120 virtual bool SetSslRole(talk_base::SSLRole role) {
121 ssl_role_ = role;
122 return true;
123 }
124 virtual bool GetSslRole(talk_base::SSLRole* role) const {
125 *role = ssl_role_;
126 return true;
127 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000128
129 virtual void Connect() {
130 if (state_ == STATE_INIT) {
131 state_ = STATE_CONNECTING;
132 }
133 }
134 virtual void Reset() {
135 if (state_ != STATE_INIT) {
136 state_ = STATE_INIT;
137 if (dest_) {
138 dest_->state_ = STATE_INIT;
139 dest_->dest_ = NULL;
140 dest_ = NULL;
141 }
142 }
143 }
144
145 void SetWritable(bool writable) {
146 set_writable(writable);
147 }
148
149 void SetDestination(FakeTransportChannel* dest) {
150 if (state_ == STATE_CONNECTING && dest) {
151 // This simulates the delivery of candidates.
152 dest_ = dest;
153 dest_->dest_ = this;
154 if (identity_ && dest_->identity_) {
155 do_dtls_ = true;
156 dest_->do_dtls_ = true;
157 NegotiateSrtpCiphers();
158 }
159 state_ = STATE_CONNECTED;
160 dest_->state_ = STATE_CONNECTED;
161 set_writable(true);
162 dest_->set_writable(true);
163 } else if (state_ == STATE_CONNECTED && !dest) {
164 // Simulates loss of connectivity, by asymmetrically forgetting dest_.
165 dest_ = NULL;
166 state_ = STATE_CONNECTING;
167 set_writable(false);
168 }
169 }
170
171 virtual int SendPacket(const char* data, size_t len, int flags) {
172 if (state_ != STATE_CONNECTED) {
173 return -1;
174 }
175
176 if (flags != PF_SRTP_BYPASS && flags != 0) {
177 return -1;
178 }
179
180 PacketMessageData* packet = new PacketMessageData(data, len);
181 if (async_) {
182 talk_base::Thread::Current()->Post(this, 0, packet);
183 } else {
184 talk_base::Thread::Current()->Send(this, 0, packet);
185 }
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000186 return static_cast<int>(len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000187 }
188 virtual int SetOption(talk_base::Socket::Option opt, int value) {
189 return true;
190 }
191 virtual int GetError() {
192 return 0;
193 }
194
195 virtual void OnSignalingReady() {
196 }
197 virtual void OnCandidate(const Candidate& candidate) {
198 }
199
200 virtual void OnMessage(talk_base::Message* msg) {
201 PacketMessageData* data = static_cast<PacketMessageData*>(
202 msg->pdata);
203 dest_->SignalReadPacket(dest_, data->packet.data(),
204 data->packet.length(), 0);
205 delete data;
206 }
207
208 bool SetLocalIdentity(talk_base::SSLIdentity* identity) {
209 identity_ = identity;
210
211 return true;
212 }
213
214 bool IsDtlsActive() const {
215 return do_dtls_;
216 }
217
218 bool SetSrtpCiphers(const std::vector<std::string>& ciphers) {
219 srtp_ciphers_ = ciphers;
220 return true;
221 }
222
223 virtual bool GetSrtpCipher(std::string* cipher) {
224 if (!chosen_srtp_cipher_.empty()) {
225 *cipher = chosen_srtp_cipher_;
226 return true;
227 }
228 return false;
229 }
230
231 virtual bool ExportKeyingMaterial(const std::string& label,
232 const uint8* context,
233 size_t context_len,
234 bool use_context,
235 uint8* result,
236 size_t result_len) {
237 if (!chosen_srtp_cipher_.empty()) {
238 memset(result, 0xff, result_len);
239 return true;
240 }
241
242 return false;
243 }
244
245 virtual void NegotiateSrtpCiphers() {
246 for (std::vector<std::string>::const_iterator it1 = srtp_ciphers_.begin();
247 it1 != srtp_ciphers_.end(); ++it1) {
248 for (std::vector<std::string>::const_iterator it2 =
249 dest_->srtp_ciphers_.begin();
250 it2 != dest_->srtp_ciphers_.end(); ++it2) {
251 if (*it1 == *it2) {
252 chosen_srtp_cipher_ = *it1;
253 dest_->chosen_srtp_cipher_ = *it2;
254 return;
255 }
256 }
257 }
258 }
259
260 virtual bool GetStats(ConnectionInfos* infos) OVERRIDE {
261 ConnectionInfo info;
262 infos->clear();
263 infos->push_back(info);
264 return true;
265 }
266
267 private:
268 enum State { STATE_INIT, STATE_CONNECTING, STATE_CONNECTED };
269 Transport* transport_;
270 FakeTransportChannel* dest_;
271 State state_;
272 bool async_;
273 talk_base::SSLIdentity* identity_;
274 bool do_dtls_;
275 std::vector<std::string> srtp_ciphers_;
276 std::string chosen_srtp_cipher_;
mallinath@webrtc.orga5506692013-08-12 21:18:15 +0000277 IceRole role_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000278 uint64 tiebreaker_;
279 IceProtocolType ice_proto_;
280 std::string ice_ufrag_;
281 std::string ice_pwd_;
282 std::string remote_ice_ufrag_;
283 std::string remote_ice_pwd_;
284 IceMode remote_ice_mode_;
285 talk_base::SSLFingerprint dtls_fingerprint_;
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000286 talk_base::SSLRole ssl_role_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000287};
288
289// Fake transport class, which can be passed to anything that needs a Transport.
290// Can be informed of another FakeTransport via SetDestination (low-tech way
291// of doing candidates)
292class FakeTransport : public Transport {
293 public:
294 typedef std::map<int, FakeTransportChannel*> ChannelMap;
295 FakeTransport(talk_base::Thread* signaling_thread,
296 talk_base::Thread* worker_thread,
297 const std::string& content_name,
298 PortAllocator* alllocator = NULL)
299 : Transport(signaling_thread, worker_thread,
300 content_name, "test_type", NULL),
301 dest_(NULL),
302 async_(false),
303 identity_(NULL) {
304 }
305 ~FakeTransport() {
306 DestroyAllChannels();
307 }
308
309 const ChannelMap& channels() const { return channels_; }
310
311 void SetAsync(bool async) { async_ = async; }
312 void SetDestination(FakeTransport* dest) {
313 dest_ = dest;
314 for (ChannelMap::iterator it = channels_.begin(); it != channels_.end();
315 ++it) {
316 it->second->SetLocalIdentity(identity_);
317 SetChannelDestination(it->first, it->second);
318 }
319 }
320
321 void SetWritable(bool writable) {
322 for (ChannelMap::iterator it = channels_.begin(); it != channels_.end();
323 ++it) {
324 it->second->SetWritable(writable);
325 }
326 }
327
328 void set_identity(talk_base::SSLIdentity* identity) {
329 identity_ = identity;
330 }
331
332 using Transport::local_description;
333 using Transport::remote_description;
334
335 protected:
336 virtual TransportChannelImpl* CreateTransportChannel(int component) {
337 if (channels_.find(component) != channels_.end()) {
338 return NULL;
339 }
340 FakeTransportChannel* channel =
341 new FakeTransportChannel(this, content_name(), component);
342 channel->SetAsync(async_);
343 SetChannelDestination(component, channel);
344 channels_[component] = channel;
345 return channel;
346 }
347 virtual void DestroyTransportChannel(TransportChannelImpl* channel) {
348 channels_.erase(channel->component());
349 delete channel;
350 }
351
352 private:
353 FakeTransportChannel* GetFakeChannel(int component) {
354 ChannelMap::iterator it = channels_.find(component);
355 return (it != channels_.end()) ? it->second : NULL;
356 }
357 void SetChannelDestination(int component,
358 FakeTransportChannel* channel) {
359 FakeTransportChannel* dest_channel = NULL;
360 if (dest_) {
361 dest_channel = dest_->GetFakeChannel(component);
362 if (dest_channel) {
363 dest_channel->SetLocalIdentity(dest_->identity_);
364 }
365 }
366 channel->SetDestination(dest_channel);
367 }
368
369 // Note, this is distinct from the Channel map owned by Transport.
370 // This map just tracks the FakeTransportChannels created by this class.
371 ChannelMap channels_;
372 FakeTransport* dest_;
373 bool async_;
374 talk_base::SSLIdentity* identity_;
375};
376
377// Fake session class, which can be passed into a BaseChannel object for
378// test purposes. Can be connected to other FakeSessions via Connect().
379class FakeSession : public BaseSession {
380 public:
381 explicit FakeSession()
382 : BaseSession(talk_base::Thread::Current(),
383 talk_base::Thread::Current(),
384 NULL, "", "", true),
385 fail_create_channel_(false) {
386 }
387 explicit FakeSession(bool initiator)
388 : BaseSession(talk_base::Thread::Current(),
389 talk_base::Thread::Current(),
390 NULL, "", "", initiator),
391 fail_create_channel_(false) {
392 }
393
394 FakeTransport* GetTransport(const std::string& content_name) {
395 return static_cast<FakeTransport*>(
396 BaseSession::GetTransport(content_name));
397 }
398
399 void Connect(FakeSession* dest) {
400 // Simulate the exchange of candidates.
401 CompleteNegotiation();
402 dest->CompleteNegotiation();
403 for (TransportMap::const_iterator it = transport_proxies().begin();
404 it != transport_proxies().end(); ++it) {
405 static_cast<FakeTransport*>(it->second->impl())->SetDestination(
406 dest->GetTransport(it->first));
407 }
408 }
409
410 virtual TransportChannel* CreateChannel(
411 const std::string& content_name,
412 const std::string& channel_name,
413 int component) {
414 if (fail_create_channel_) {
415 return NULL;
416 }
417 return BaseSession::CreateChannel(content_name, channel_name, component);
418 }
419
420 void set_fail_channel_creation(bool fail_channel_creation) {
421 fail_create_channel_ = fail_channel_creation;
422 }
423
424 // TODO: Hoist this into Session when we re-work the Session code.
425 void set_ssl_identity(talk_base::SSLIdentity* identity) {
426 for (TransportMap::const_iterator it = transport_proxies().begin();
427 it != transport_proxies().end(); ++it) {
428 // We know that we have a FakeTransport*
429
430 static_cast<FakeTransport*>(it->second->impl())->set_identity
431 (identity);
432 }
433 }
434
435 protected:
436 virtual Transport* CreateTransport(const std::string& content_name) {
437 return new FakeTransport(signaling_thread(), worker_thread(), content_name);
438 }
439
440 void CompleteNegotiation() {
441 for (TransportMap::const_iterator it = transport_proxies().begin();
442 it != transport_proxies().end(); ++it) {
443 it->second->CompleteNegotiation();
444 it->second->ConnectChannels();
445 }
446 }
447
448 private:
449 bool fail_create_channel_;
450};
451
452} // namespace cricket
453
454#endif // TALK_P2P_BASE_FAKESESSION_H_