15e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org/* 25e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org * libjingle 35e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org * Copyright 2011, Google Inc. 45e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org * 55e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org * Redistribution and use in source and binary forms, with or without 65e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org * modification, are permitted provided that the following conditions are met: 75e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org * 85e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org * 1. Redistributions of source code must retain the above copyright notice, 95e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org * this list of conditions and the following disclaimer. 105e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org * 2. Redistributions in binary form must reproduce the above copyright notice, 115e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org * this list of conditions and the following disclaimer in the documentation 125e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org * and/or other materials provided with the distribution. 135e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org * 3. The name of the author may not be used to endorse or promote products 145e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org * derived from this software without specific prior written permission. 155e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org * 165e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 175e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 185e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 195e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 205e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 215e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 225e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 235e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 245e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 255e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 265e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org */ 270e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 280e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include <string> 290e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 300e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "talk/p2p/base/basicpacketsocketfactory.h" 310e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "talk/p2p/base/relayport.h" 320e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "talk/p2p/base/stunport.h" 330e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "talk/p2p/client/connectivitychecker.h" 340e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "talk/p2p/client/httpportallocator.h" 35cf81adffe15fa8ea0f333432e41f6d504148f18abuildbot@webrtc.org#include "webrtc/base/asynchttprequest.h" 36cf81adffe15fa8ea0f333432e41f6d504148f18abuildbot@webrtc.org#include "webrtc/base/fakenetwork.h" 37cf81adffe15fa8ea0f333432e41f6d504148f18abuildbot@webrtc.org#include "webrtc/base/gunit.h" 38cf81adffe15fa8ea0f333432e41f6d504148f18abuildbot@webrtc.org#include "webrtc/base/scoped_ptr.h" 39cf81adffe15fa8ea0f333432e41f6d504148f18abuildbot@webrtc.org#include "webrtc/base/socketaddress.h" 400e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 410e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgnamespace cricket { 420e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 432a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.orgstatic const rtc::SocketAddress kClientAddr1("11.11.11.11", 0); 442a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.orgstatic const rtc::SocketAddress kClientAddr2("22.22.22.22", 0); 452a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.orgstatic const rtc::SocketAddress kExternalAddr("33.33.33.33", 3333); 462a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.orgstatic const rtc::SocketAddress kStunAddr("44.44.44.44", 4444); 472a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.orgstatic const rtc::SocketAddress kRelayAddr("55.55.55.55", 5555); 482a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.orgstatic const rtc::SocketAddress kProxyAddr("66.66.66.66", 6666); 492a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.orgstatic const rtc::ProxyType kProxyType = rtc::PROXY_HTTPS; 500e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgstatic const char kRelayHost[] = "relay.google.com"; 510e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgstatic const char kRelayToken[] = 520e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org "CAESFwoOb2phQGdvb2dsZS5jb20Q043h47MmGhBTB1rbfIXkhuarDCZe+xF6"; 530e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgstatic const char kBrowserAgent[] = "browser_test"; 540e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgstatic const char kJid[] = "a.b@c"; 550e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgstatic const char kUserName[] = "testuser"; 560e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgstatic const char kPassword[] = "testpassword"; 570e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgstatic const char kMagicCookie[] = "testcookie"; 580e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgstatic const char kRelayUdpPort[] = "4444"; 590e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgstatic const char kRelayTcpPort[] = "5555"; 600e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgstatic const char kRelaySsltcpPort[] = "6666"; 610e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgstatic const char kSessionId[] = "testsession"; 620e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgstatic const char kConnection[] = "testconnection"; 630e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgstatic const int kMinPort = 1000; 640e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgstatic const int kMaxPort = 2000; 650e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 660e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// Fake implementation to mock away real network usage. 670e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgclass FakeRelayPort : public RelayPort { 680e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org public: 692a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org FakeRelayPort(rtc::Thread* thread, 702a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org rtc::PacketSocketFactory* factory, 712a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org rtc::Network* network, const rtc::IPAddress& ip, 720e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org int min_port, int max_port, 730e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org const std::string& username, const std::string& password) 740e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org : RelayPort(thread, factory, network, ip, min_port, max_port, 750e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org username, password) { 760e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org } 770e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 780e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org // Just signal that we are done. 790e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org virtual void PrepareAddress() { 800e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org SignalPortComplete(this); 810e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org } 820e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}; 830e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 840e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// Fake implementation to mock away real network usage. 850e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgclass FakeStunPort : public StunPort { 860e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org public: 872a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org FakeStunPort(rtc::Thread* thread, 882a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org rtc::PacketSocketFactory* factory, 892a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org rtc::Network* network, 902a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org const rtc::IPAddress& ip, 910e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org int min_port, int max_port, 920e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org const std::string& username, const std::string& password, 93459f356846e5dc1e9b2c5182568660f5eaacfe3ebuildbot@webrtc.org const ServerAddresses& server_addr) 940e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org : StunPort(thread, factory, network, ip, min_port, max_port, 950e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org username, password, server_addr) { 960e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org } 970e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 980e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org // Just set external address and signal that we are done. 990e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org virtual void PrepareAddress() { 10058c89b128d6e4dbe00a9a18bb3029f63bd43ce6dmallinath@webrtc.org AddAddress(kExternalAddr, kExternalAddr, rtc::SocketAddress(), "udp", "", 10158c89b128d6e4dbe00a9a18bb3029f63bd43ce6dmallinath@webrtc.org STUN_PORT_TYPE, ICE_TYPE_PREFERENCE_SRFLX, 0, true); 1020e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org SignalPortComplete(this); 1030e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org } 1040e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}; 1050e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 1060e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// Fake implementation to mock away real network usage by responding 1070e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// to http requests immediately. 1080e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgclass FakeHttpPortAllocatorSession : public TestHttpPortAllocatorSession { 1090e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org public: 1100e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org FakeHttpPortAllocatorSession( 1110e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org HttpPortAllocator* allocator, 1120e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org const std::string& content_name, 1130e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org int component, 1140e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org const std::string& ice_ufrag, const std::string& ice_pwd, 1152a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org const std::vector<rtc::SocketAddress>& stun_hosts, 1160e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org const std::vector<std::string>& relay_hosts, 1170e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org const std::string& relay_token, 1180e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org const std::string& agent) 1190e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org : TestHttpPortAllocatorSession(allocator, 1200e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org content_name, 1210e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org component, 1220e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org ice_ufrag, 1230e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org ice_pwd, 1240e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org stun_hosts, 1250e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org relay_hosts, 1260e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org relay_token, 1270e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org agent) { 1280e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org } 1290e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org virtual void SendSessionRequest(const std::string& host, int port) { 1300e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org FakeReceiveSessionResponse(host, port); 1310e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org } 1320e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 1330e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org // Pass results to the real implementation. 1340e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org void FakeReceiveSessionResponse(const std::string& host, int port) { 1352a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org rtc::AsyncHttpRequest* response = CreateAsyncHttpResponse(port); 1360e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org TestHttpPortAllocatorSession::OnRequestDone(response); 1370e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org response->Destroy(true); 1380e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org } 1390e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 1400e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org private: 1410e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org // Helper method for creating a response to a relay session request. 1422a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org rtc::AsyncHttpRequest* CreateAsyncHttpResponse(int port) { 1432a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org rtc::AsyncHttpRequest* request = 1442a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org new rtc::AsyncHttpRequest(kBrowserAgent); 1450e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org std::stringstream ss; 1460e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org ss << "username=" << kUserName << std::endl 1470e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org << "password=" << kPassword << std::endl 1480e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org << "magic_cookie=" << kMagicCookie << std::endl 1490e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org << "relay.ip=" << kRelayAddr.ipaddr().ToString() << std::endl 1500e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org << "relay.udp_port=" << kRelayUdpPort << std::endl 1510e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org << "relay.tcp_port=" << kRelayTcpPort << std::endl 1520e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org << "relay.ssltcp_port=" << kRelaySsltcpPort << std::endl; 1530e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org request->response().document.reset( 1542a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org new rtc::MemoryStream(ss.str().c_str())); 1550e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org request->response().set_success(); 1560e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org request->set_port(port); 1572a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org request->set_secure(port == rtc::HTTP_SECURE_PORT); 1580e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org return request; 1590e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org } 1600e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}; 1610e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 1620e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// Fake implementation for creating fake http sessions. 1630e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgclass FakeHttpPortAllocator : public HttpPortAllocator { 1640e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org public: 1652a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org FakeHttpPortAllocator(rtc::NetworkManager* network_manager, 1660e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org const std::string& user_agent) 1670e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org : HttpPortAllocator(network_manager, user_agent) { 1680e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org } 1690e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 1700e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org virtual PortAllocatorSession* CreateSessionInternal( 1710e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org const std::string& content_name, int component, 1720e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org const std::string& ice_ufrag, const std::string& ice_pwd) { 1732a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org std::vector<rtc::SocketAddress> stun_hosts; 1740e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org stun_hosts.push_back(kStunAddr); 1750e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org std::vector<std::string> relay_hosts; 1760e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org relay_hosts.push_back(kRelayHost); 1770e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org return new FakeHttpPortAllocatorSession(this, 1780e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org content_name, 1790e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org component, 1800e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org ice_ufrag, 1810e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org ice_pwd, 1820e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org stun_hosts, 1830e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org relay_hosts, 1840e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org kRelayToken, 1850e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org kBrowserAgent); 1860e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org } 1870e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}; 1880e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 1890e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgclass ConnectivityCheckerForTest : public ConnectivityChecker { 1900e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org public: 1912a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org ConnectivityCheckerForTest(rtc::Thread* worker, 1920e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org const std::string& jid, 1930e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org const std::string& session_id, 1940e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org const std::string& user_agent, 1950e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org const std::string& relay_token, 1960e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org const std::string& connection) 1970e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org : ConnectivityChecker(worker, 1980e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org jid, 1990e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org session_id, 2000e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org user_agent, 2010e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org relay_token, 2020e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org connection), 2030e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org proxy_initiated_(false) { 2040e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org } 2050e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 2062a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org rtc::FakeNetworkManager* network_manager() const { 2070e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org return network_manager_; 2080e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org } 2090e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 2100e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org FakeHttpPortAllocator* port_allocator() const { 2110e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org return fake_port_allocator_; 2120e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org } 2130e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 2140e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org protected: 2150e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org // Overridden methods for faking a real network. 2162a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org virtual rtc::NetworkManager* CreateNetworkManager() { 2172a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org network_manager_ = new rtc::FakeNetworkManager(); 2180e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org return network_manager_; 2190e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org } 2202a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org virtual rtc::BasicPacketSocketFactory* CreateSocketFactory( 2212a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org rtc::Thread* thread) { 2220e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org // Create socket factory, for simplicity, let it run on the current thread. 2230e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org socket_factory_ = 2242a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org new rtc::BasicPacketSocketFactory(rtc::Thread::Current()); 2250e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org return socket_factory_; 2260e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org } 2270e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org virtual HttpPortAllocator* CreatePortAllocator( 2282a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org rtc::NetworkManager* network_manager, 2290e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org const std::string& user_agent, 2300e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org const std::string& relay_token) { 2310e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org fake_port_allocator_ = 2320e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org new FakeHttpPortAllocator(network_manager, user_agent); 2330e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org return fake_port_allocator_; 2340e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org } 2350e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org virtual StunPort* CreateStunPort( 2360e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org const std::string& username, const std::string& password, 2372a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org const PortConfiguration* config, rtc::Network* network) { 2385e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org return new FakeStunPort(worker(), 2395e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org socket_factory_, 2405e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org network, 2414318280f56990bbbd758f6f5e49b5719100f9951buildbot@webrtc.org#ifdef USE_WEBRTC_DEV_BRANCH 2425e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org network->GetBestIP(), 2434318280f56990bbbd758f6f5e49b5719100f9951buildbot@webrtc.org#else // USE_WEBRTC_DEV_BRANCH 2444318280f56990bbbd758f6f5e49b5719100f9951buildbot@webrtc.org network->ip(), 2454318280f56990bbbd758f6f5e49b5719100f9951buildbot@webrtc.org#endif // USE_WEBRTC_DEV_BRANCH 2465e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org kMinPort, 2475e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org kMaxPort, 2485e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org username, 2495e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org password, 250459f356846e5dc1e9b2c5182568660f5eaacfe3ebuildbot@webrtc.org config->stun_servers); 2510e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org } 2520e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org virtual RelayPort* CreateRelayPort( 2530e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org const std::string& username, const std::string& password, 2542a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org const PortConfiguration* config, rtc::Network* network) { 2555e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org return new FakeRelayPort(worker(), 2565e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org socket_factory_, 2575e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org network, 2584318280f56990bbbd758f6f5e49b5719100f9951buildbot@webrtc.org#ifdef USE_WEBRTC_DEV_BRANCH 2595e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org network->GetBestIP(), 2604318280f56990bbbd758f6f5e49b5719100f9951buildbot@webrtc.org#else // USE_WEBRTC_DEV_BRANCH 2614318280f56990bbbd758f6f5e49b5719100f9951buildbot@webrtc.org network->ip(), 2624318280f56990bbbd758f6f5e49b5719100f9951buildbot@webrtc.org#endif // USE_WEBRTC_DEV_BRANCH 2635e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org kMinPort, 2645e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org kMaxPort, 2655e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org username, 2665e89dbd355e720d82139013c5e56d005208d51c8guoweis@webrtc.org password); 2670e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org } 2680e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org virtual void InitiateProxyDetection() { 2690e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org if (!proxy_initiated_) { 2700e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org proxy_initiated_ = true; 2710e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org proxy_info_.address = kProxyAddr; 2720e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org proxy_info_.type = kProxyType; 2730e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org SetProxyInfo(proxy_info_); 2740e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org } 2750e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org } 2760e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 2772a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org virtual rtc::ProxyInfo GetProxyInfo() const { 2780e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org return proxy_info_; 2790e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org } 2800e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 2810e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org private: 2822a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org rtc::BasicPacketSocketFactory* socket_factory_; 2830e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org FakeHttpPortAllocator* fake_port_allocator_; 2842a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org rtc::FakeNetworkManager* network_manager_; 2852a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org rtc::ProxyInfo proxy_info_; 2860e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org bool proxy_initiated_; 2870e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}; 2880e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 2890e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgclass ConnectivityCheckerTest : public testing::Test { 2900e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org protected: 2910e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org void VerifyNic(const NicInfo& info, 2922a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org const rtc::SocketAddress& local_address) { 2930e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org // Verify that the external address has been set. 2940e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org EXPECT_EQ(kExternalAddr, info.external_address); 2950e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 2960e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org // Verify that the stun server address has been set. 297459f356846e5dc1e9b2c5182568660f5eaacfe3ebuildbot@webrtc.org EXPECT_EQ(1U, info.stun_server_addresses.size()); 298459f356846e5dc1e9b2c5182568660f5eaacfe3ebuildbot@webrtc.org EXPECT_EQ(kStunAddr, *(info.stun_server_addresses.begin())); 2990e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 3000e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org // Verify that the media server address has been set. Don't care 3010e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org // about port since it is different for different protocols. 3020e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org EXPECT_EQ(kRelayAddr.ipaddr(), info.media_server_address.ipaddr()); 3030e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 3040e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org // Verify that local ip matches. 3050e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org EXPECT_EQ(local_address.ipaddr(), info.ip); 3060e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 3070e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org // Verify that we have received responses for our 3080e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org // pings. Unsuccessful ping has rtt value -1, successful >= 0. 3090e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org EXPECT_GE(info.stun.rtt, 0); 3100e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org EXPECT_GE(info.udp.rtt, 0); 3110e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org EXPECT_GE(info.tcp.rtt, 0); 3120e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org EXPECT_GE(info.ssltcp.rtt, 0); 3130e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 3140e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org // If proxy has been set, verify address and type. 3150e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org if (!info.proxy_info.address.IsNil()) { 3160e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org EXPECT_EQ(kProxyAddr, info.proxy_info.address); 3170e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org EXPECT_EQ(kProxyType, info.proxy_info.type); 3180e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org } 3190e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org } 3200e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}; 3210e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 3220e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// Tests a configuration with two network interfaces. Verifies that 4 3230e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// combinations of ip/proxy are created and that all protocols are 3240e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// tested on each combination. 3250e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgTEST_F(ConnectivityCheckerTest, TestStart) { 3262a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org ConnectivityCheckerForTest connectivity_checker(rtc::Thread::Current(), 3270e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org kJid, 3280e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org kSessionId, 3290e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org kBrowserAgent, 3300e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org kRelayToken, 3310e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org kConnection); 3320e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org connectivity_checker.Initialize(); 3330e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org connectivity_checker.set_stun_address(kStunAddr); 3340e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org connectivity_checker.network_manager()->AddInterface(kClientAddr1); 3350e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org connectivity_checker.network_manager()->AddInterface(kClientAddr2); 3360e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 3370e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org connectivity_checker.Start(); 3382a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org rtc::Thread::Current()->ProcessMessages(1000); 3390e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 3400e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org NicMap nics = connectivity_checker.GetResults(); 3410e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 3420e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org // There should be 4 nics in our map. 2 for each interface added, 3430e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org // one with proxy set and one without. 3440e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org EXPECT_EQ(4U, nics.size()); 3450e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 3460e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org // First verify interfaces without proxy. 3472a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org rtc::SocketAddress nilAddress; 3480e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 3490e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org // First lookup the address of the first nic combined with no proxy. 3500e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org NicMap::iterator i = nics.find(NicId(kClientAddr1.ipaddr(), nilAddress)); 3510e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org ASSERT(i != nics.end()); 3520e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org NicInfo info = i->second; 3530e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org VerifyNic(info, kClientAddr1); 3540e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 3550e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org // Then make sure the second device has been tested without proxy. 3560e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org i = nics.find(NicId(kClientAddr2.ipaddr(), nilAddress)); 3570e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org ASSERT(i != nics.end()); 3580e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org info = i->second; 3590e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org VerifyNic(info, kClientAddr2); 3600e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 3610e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org // Now verify both interfaces with proxy. 3620e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org i = nics.find(NicId(kClientAddr1.ipaddr(), kProxyAddr)); 3630e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org ASSERT(i != nics.end()); 3640e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org info = i->second; 3650e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org VerifyNic(info, kClientAddr1); 3660e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 3670e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org i = nics.find(NicId(kClientAddr2.ipaddr(), kProxyAddr)); 3680e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org ASSERT(i != nics.end()); 3690e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org info = i->second; 3700e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org VerifyNic(info, kClientAddr2); 3710e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}; 3720e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 3730e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// Tests that nothing bad happens if thera are no network interfaces 3740e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org// available to check. 3750e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgTEST_F(ConnectivityCheckerTest, TestStartNoNetwork) { 3762a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org ConnectivityCheckerForTest connectivity_checker(rtc::Thread::Current(), 3770e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org kJid, 3780e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org kSessionId, 3790e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org kBrowserAgent, 3800e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org kRelayToken, 3810e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org kConnection); 3820e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org connectivity_checker.Initialize(); 3830e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org connectivity_checker.Start(); 3842a86ce22ccc387dfa6f8a98ce3eba5c1e6f9e538buildbot@webrtc.org rtc::Thread::Current()->ProcessMessages(1000); 3850e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 3860e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org NicMap nics = connectivity_checker.GetResults(); 3870e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 3880e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org // Verify that no nics where checked. 3890e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org EXPECT_EQ(0U, nics.size()); 3900e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org} 3910e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org 3920e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org} // namespace cricket 393