def test_http_if_no_other_choice(self):
     """Finally, we use the host configured for HTTP."""
     settings = {
         "http": {"host": "httphost", "port": 3128},
     }
     proxy = tunnel_server.build_proxy(settings)
     self.assertEqual(proxy.type(), proxy.HttpProxy)
     self.assertEqual(proxy.hostName(), "httphost")
     self.assertEqual(proxy.port(), 3128)
 def test_https_beats_http(self):
     """HTTPS wins over HTTP, since all of SD traffic is https."""
     settings = {
         "http": {"host": "httphost", "port": 3128},
         "https": {"host": "httpshost", "port": 3129},
     }
     proxy = tunnel_server.build_proxy(settings)
     self.assertEqual(proxy.type(), proxy.HttpProxy)
     self.assertEqual(proxy.hostName(), "httpshost")
     self.assertEqual(proxy.port(), 3129)
 def test_socks_is_preferred(self):
     """Socks overrides all protocols."""
     settings = {
         "http": {"host": "httphost", "port": 3128},
         "https": {"host": "httpshost", "port": 3129},
         "socks": {"host": "sockshost", "port": 1080},
     }
     proxy = tunnel_server.build_proxy(settings)
     self.assertEqual(proxy.type(), proxy.Socks5Proxy)
     self.assertEqual(proxy.hostName(), "sockshost")
     self.assertEqual(proxy.port(), 1080)
 def test_proxy_authentication_error(self):
     """The proxy credentials were wrong on purpose."""
     settings = {"http": self.get_proxy_settings()}
     settings["http"]["password"] = "******"
     proxy = tunnel_server.build_proxy(settings)
     tunnel_server.QNetworkProxy.setApplicationProxy(proxy)
     fake_protocol = FakeServerTunnelProtocol()
     client = tunnel_server.RemoteSocket(fake_protocol)
     self.addCleanup(client.stop)
     url = urlparse(self.dest_url)
     yield self.assertFailure(client.connect(url.netloc),
                              tunnel_server.ProxyAuthenticationError)
    def setUp(self):
        """Initialize this testcase."""
        yield super(RemoteSocketTestCase, self).setUp()
        self.ws = MockWebServer()
        self.addCleanup(self.ws.stop)
        self.dest_url = self.ws.get_iri().encode("utf-8") + SIMPLERESOURCE

        self.addCleanup(tunnel_server.QNetworkProxy.setApplicationProxy,
                        tunnel_server.QNetworkProxy.applicationProxy())
        settings = {"http": self.get_proxy_settings()}
        proxy = tunnel_server.build_proxy(settings)
        tunnel_server.QNetworkProxy.setApplicationProxy(proxy)
 def test_proxy_nobody_listens(self):
     """The proxy settings point to a proxy that's unreachable."""
     settings = dict(http={
         "host": "127.0.0.1",
         "port": 83,  # unused port according to /etc/services
     })
     proxy = tunnel_server.build_proxy(settings)
     tunnel_server.QNetworkProxy.setApplicationProxy(proxy)
     fake_protocol = FakeServerTunnelProtocol()
     client = tunnel_server.RemoteSocket(fake_protocol)
     self.addCleanup(client.stop)
     url = urlparse(self.dest_url)
     yield self.assertFailure(client.connect(url.netloc),
                              tunnel_server.ConnectionError)
    def test_use_credentials(self):
        """The credentials are used if present."""
        fake_protocol = FakeServerTunnelProtocol()
        client = tunnel_server.RemoteSocket(fake_protocol)
        proxy = tunnel_server.build_proxy(FAKE_SETTINGS)
        authenticator = QAuthenticator()

        client.proxyAuthenticationRequired.emit(proxy, authenticator)
        self.assertEqual(proxy.user(), "")
        self.assertEqual(proxy.password(), "")
        fake_protocol.proxy_credentials = FAKE_CREDS

        client.proxyAuthenticationRequired.emit(proxy, authenticator)
        self.assertEqual(authenticator.user(), FAKE_CREDS["username"])
        self.assertEqual(authenticator.password(), FAKE_CREDS["password"])
    def test_creds_are_not_logged(self):
        """The proxy credentials are not logged."""
        log = []
        self.patch(tunnel_server.logger, "info",
                   lambda text, *args: log.append(text % args))
        proxy = tunnel_server.build_proxy(FAKE_AUTH_SETTINGS)
        authenticator = QAuthenticator()
        username = FAKE_AUTH_SETTINGS["http"]["username"]
        password = FAKE_AUTH_SETTINGS["http"]["password"]
        self.proto.proxy_credentials = {
            "username": username,
            "password": password,
        }
        self.proto.proxy_domain = proxy.hostName()

        self.proto.proxy_auth_required(proxy, authenticator)

        for line in log:
            self.assertNotIn(username, line)
            self.assertNotIn(password, line)
 def test_use_noproxy_as_fallback(self):
     """If nothing useful, revert to no proxy."""
     settings = {}
     proxy = tunnel_server.build_proxy(settings)
     self.assertEqual(proxy.type(), proxy.DefaultProxy)