2015-11-01 18:26:22 +01:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2010, Arvid Norberg
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "test.hpp"
|
|
|
|
#include "settings.hpp"
|
|
|
|
#include "setup_swarm.hpp"
|
|
|
|
#include "simulator/simulator.hpp"
|
|
|
|
#include "simulator/http_server.hpp"
|
2015-12-22 06:44:03 +01:00
|
|
|
#include "simulator/http_proxy.hpp"
|
2015-11-22 03:43:08 +01:00
|
|
|
#include "simulator/socks_server.hpp"
|
2015-11-01 18:26:22 +01:00
|
|
|
#include "libtorrent/alert_types.hpp"
|
|
|
|
#include "libtorrent/aux_/proxy_settings.hpp"
|
|
|
|
#include "libtorrent/http_connection.hpp"
|
|
|
|
#include "libtorrent/resolver.hpp"
|
2015-11-07 04:28:51 +01:00
|
|
|
#include "libtorrent/io.hpp"
|
2016-05-23 14:15:39 +02:00
|
|
|
|
2015-11-27 03:18:58 +01:00
|
|
|
#include "make_proxy_settings.hpp"
|
2015-11-07 04:28:51 +01:00
|
|
|
|
2016-05-23 14:15:39 +02:00
|
|
|
#include <iostream>
|
2016-06-19 01:24:27 +02:00
|
|
|
#include "libtorrent/aux_/disable_warnings_push.hpp"
|
2015-11-07 04:28:51 +01:00
|
|
|
#include <boost/crc.hpp>
|
2016-06-19 01:24:27 +02:00
|
|
|
#include "libtorrent/aux_/disable_warnings_pop.hpp"
|
2015-11-01 18:26:22 +01:00
|
|
|
|
2017-04-12 20:05:53 +02:00
|
|
|
using namespace lt;
|
2015-11-01 18:26:22 +01:00
|
|
|
using namespace sim;
|
2015-11-07 04:28:51 +01:00
|
|
|
namespace io = lt::detail;
|
2015-11-01 18:26:22 +01:00
|
|
|
|
|
|
|
using chrono::duration_cast;
|
|
|
|
|
|
|
|
struct sim_config : sim::default_config
|
|
|
|
{
|
|
|
|
chrono::high_resolution_clock::duration hostname_lookup(
|
|
|
|
asio::ip::address const& requestor
|
|
|
|
, std::string hostname
|
|
|
|
, std::vector<asio::ip::address>& result
|
2016-07-10 02:10:38 +02:00
|
|
|
, boost::system::error_code& ec) override
|
2015-11-01 18:26:22 +01:00
|
|
|
{
|
2015-11-01 23:22:40 +01:00
|
|
|
if (hostname == "try-next.com")
|
|
|
|
{
|
|
|
|
result.push_back(address_v4::from_string("10.0.0.10"));
|
|
|
|
result.push_back(address_v4::from_string("10.0.0.9"));
|
|
|
|
result.push_back(address_v4::from_string("10.0.0.8"));
|
|
|
|
result.push_back(address_v4::from_string("10.0.0.7"));
|
|
|
|
result.push_back(address_v4::from_string("10.0.0.6"));
|
|
|
|
result.push_back(address_v4::from_string("10.0.0.5"));
|
|
|
|
result.push_back(address_v4::from_string("10.0.0.4"));
|
|
|
|
result.push_back(address_v4::from_string("10.0.0.3"));
|
2015-11-02 05:58:38 +01:00
|
|
|
|
|
|
|
// this is the IP that works, all other should fail
|
2015-11-01 23:22:40 +01:00
|
|
|
result.push_back(address_v4::from_string("10.0.0.2"));
|
|
|
|
return duration_cast<chrono::high_resolution_clock::duration>(chrono::milliseconds(100));
|
|
|
|
}
|
|
|
|
|
2015-11-24 05:24:55 +01:00
|
|
|
if (hostname == "test-hostname.com")
|
|
|
|
{
|
|
|
|
result.push_back(address_v4::from_string("10.0.0.2"));
|
|
|
|
return duration_cast<chrono::high_resolution_clock::duration>(chrono::milliseconds(100));
|
|
|
|
}
|
|
|
|
|
2017-05-04 23:32:47 +02:00
|
|
|
if (hostname == "dual-stack.test-hostname.com")
|
|
|
|
{
|
|
|
|
result.push_back(address_v4::from_string("10.0.0.2"));
|
|
|
|
result.push_back(address_v6::from_string("ff::dead:beef"));
|
|
|
|
return duration_cast<chrono::high_resolution_clock::duration>(chrono::milliseconds(100));
|
|
|
|
}
|
|
|
|
|
2015-11-01 18:26:22 +01:00
|
|
|
return default_config::hostname_lookup(requestor, hostname, result, ec);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-11-02 05:58:38 +01:00
|
|
|
// takes a string of data and chunks it up using HTTP chunked encoding
|
|
|
|
std::string chunk_string(std::string s)
|
|
|
|
{
|
|
|
|
size_t i = 10;
|
|
|
|
std::string ret;
|
|
|
|
while (!s.empty())
|
|
|
|
{
|
|
|
|
i = std::min(i, s.size());
|
|
|
|
char header[50];
|
2016-05-17 15:24:06 +02:00
|
|
|
std::snprintf(header, sizeof(header), "%x\r\n", int(i));
|
2015-11-02 05:58:38 +01:00
|
|
|
ret += header;
|
|
|
|
ret += s.substr(0, i);
|
|
|
|
s.erase(s.begin(), s.begin() + i);
|
|
|
|
i *= 2;
|
|
|
|
}
|
|
|
|
ret += "0\r\n\r\n";
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-08-31 18:45:45 +02:00
|
|
|
std::shared_ptr<http_connection> test_request(io_service& ios
|
2015-11-01 18:26:22 +01:00
|
|
|
, resolver& res
|
|
|
|
, std::string const& url
|
|
|
|
, char const* expected_data
|
2017-09-04 00:13:08 +02:00
|
|
|
, int const expected_size
|
|
|
|
, int const expected_status
|
2015-11-22 19:12:11 +01:00
|
|
|
, error_condition expected_error
|
2015-11-01 18:26:22 +01:00
|
|
|
, lt::aux::proxy_settings const& ps
|
|
|
|
, int* connect_handler_called
|
|
|
|
, int* handler_called
|
|
|
|
, std::string const& auth = std::string())
|
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
std::printf(" ===== TESTING: %s =====\n", url.c_str());
|
2015-11-01 18:26:22 +01:00
|
|
|
|
2016-08-31 18:45:45 +02:00
|
|
|
auto h = std::make_shared<http_connection>(ios
|
2015-11-01 18:26:22 +01:00
|
|
|
, res
|
|
|
|
, [=](error_code const& ec, http_parser const& parser
|
2017-09-04 00:13:08 +02:00
|
|
|
, span<char const> data, http_connection&)
|
2015-11-01 18:26:22 +01:00
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::printf("RESPONSE: %s\n", url.c_str());
|
2015-11-01 18:26:22 +01:00
|
|
|
++*handler_called;
|
2015-11-01 23:22:40 +01:00
|
|
|
|
2015-11-23 04:58:01 +01:00
|
|
|
// this is pretty gross. Since boost.asio is a header-only library, when this test is
|
|
|
|
// build against shared libraries of libtorrent and simulator, there will be multiple
|
|
|
|
// (distinct) error categories in boost.asio. The traditional comparison of error_code
|
|
|
|
// and error_condition may hence fail.
|
|
|
|
const bool error_ok = ec == expected_error
|
|
|
|
|| (strcmp(ec.category().name(), expected_error.category().name()) == 0
|
|
|
|
&& ec.value() == expected_error.value());
|
|
|
|
|
|
|
|
if (!error_ok)
|
2015-11-01 23:22:40 +01:00
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::printf("ERROR: %s (expected: %s)\n"
|
2015-11-01 23:22:40 +01:00
|
|
|
, ec.message().c_str()
|
2015-11-22 19:12:11 +01:00
|
|
|
, expected_error.message().c_str());
|
2015-11-01 23:22:40 +01:00
|
|
|
}
|
|
|
|
|
2015-11-01 18:26:22 +01:00
|
|
|
const int http_status = parser.status_code();
|
|
|
|
if (expected_size != -1)
|
|
|
|
{
|
2017-09-04 00:13:08 +02:00
|
|
|
TEST_EQUAL(int(data.size()), expected_size);
|
2015-11-01 18:26:22 +01:00
|
|
|
}
|
2015-11-23 04:58:01 +01:00
|
|
|
TEST_CHECK(error_ok);
|
2015-11-01 18:26:22 +01:00
|
|
|
if (expected_status != -1)
|
|
|
|
{
|
|
|
|
TEST_EQUAL(http_status, expected_status);
|
|
|
|
}
|
|
|
|
if (http_status == 200)
|
|
|
|
{
|
|
|
|
TEST_CHECK(expected_data
|
2017-09-04 00:13:08 +02:00
|
|
|
&& int(data.size()) == expected_size
|
|
|
|
&& memcmp(expected_data, data.data(), data.size()) == 0);
|
2015-11-01 18:26:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
, true, 1024*1024
|
|
|
|
, [=](http_connection& c)
|
|
|
|
{
|
|
|
|
++*connect_handler_called;
|
|
|
|
TEST_CHECK(c.socket().is_open());
|
2016-05-17 15:24:06 +02:00
|
|
|
std::printf("CONNECTED: %s\n", url.c_str());
|
2015-11-01 18:26:22 +01:00
|
|
|
});
|
|
|
|
|
2017-05-08 13:48:42 +02:00
|
|
|
h->get(url, seconds(1), 0, &ps, 5, "test/user-agent", boost::optional<address>()
|
2017-07-15 02:59:20 +02:00
|
|
|
, resolver_flags{}, auth);
|
2015-11-01 18:26:22 +01:00
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
void print_http_header(std::map<std::string, std::string> const& headers)
|
|
|
|
{
|
|
|
|
for (std::map<std::string, std::string>::const_iterator i
|
|
|
|
= headers.begin(), end(headers.end()); i != end; ++i)
|
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::printf("%s: %s\n", i->first.c_str(), i->second.c_str());
|
2015-11-01 18:26:22 +01:00
|
|
|
}
|
|
|
|
}
|
2015-11-01 23:22:40 +01:00
|
|
|
|
2015-11-22 20:08:32 +01:00
|
|
|
void run_test(lt::aux::proxy_settings ps, std::string url, int expect_size, int expect_status
|
2015-11-22 19:12:11 +01:00
|
|
|
, boost::system::error_condition expect_error, std::vector<int> expect_counters);
|
2015-11-01 23:22:40 +01:00
|
|
|
|
|
|
|
enum expect_counters
|
|
|
|
{
|
|
|
|
connect_handler = 0,
|
|
|
|
handler = 1,
|
|
|
|
test_file_req = 2,
|
|
|
|
redirect_req = 3,
|
|
|
|
rel_redirect_req = 4,
|
|
|
|
inf_redirect_req = 5,
|
2015-11-02 05:58:38 +01:00
|
|
|
chunked_req = 6,
|
2015-11-07 04:28:51 +01:00
|
|
|
test_file_gz_req = 7,
|
2015-11-01 23:22:40 +01:00
|
|
|
|
|
|
|
num_counters
|
|
|
|
};
|
|
|
|
|
2015-11-22 20:08:32 +01:00
|
|
|
void run_suite(lt::aux::proxy_settings ps)
|
2015-11-01 23:22:40 +01:00
|
|
|
{
|
|
|
|
std::string url_base = "http://10.0.0.2:8080";
|
|
|
|
|
2015-11-22 20:08:32 +01:00
|
|
|
run_test(ps, url_base + "/test_file", 1337, 200, error_condition(), { 1, 1, 1});
|
2015-11-22 03:43:08 +01:00
|
|
|
|
2015-11-24 05:24:55 +01:00
|
|
|
// positive test with a successful hostname
|
|
|
|
run_test(ps, "http://test-hostname.com:8080/test_file", 1337, 200, error_condition(), { 1, 1, 1});
|
|
|
|
|
2015-11-22 20:08:32 +01:00
|
|
|
run_test(ps, url_base + "/non-existent", 0, 404, error_condition(), { 1, 1 });
|
|
|
|
run_test(ps, url_base + "/redirect", 1337, 200, error_condition(), { 2, 1, 1, 1 });
|
|
|
|
run_test(ps, url_base + "/relative/redirect", 1337, 200, error_condition(), {2, 1, 1, 0, 1});
|
2015-11-22 19:12:11 +01:00
|
|
|
|
2015-11-22 20:08:32 +01:00
|
|
|
run_test(ps, url_base + "/infinite/redirect", 0, 301
|
2015-11-22 19:12:11 +01:00
|
|
|
, error_condition(asio::error::eof, asio::error::get_misc_category()), {6, 1, 0, 0, 0, 6});
|
|
|
|
|
2015-11-22 20:08:32 +01:00
|
|
|
run_test(ps, url_base + "/chunked_encoding", 1337, 200, error_condition(), { 1, 1, 0, 0, 0, 0, 1});
|
2015-11-01 23:22:40 +01:00
|
|
|
|
|
|
|
// we are on an IPv4 host, we can't connect to IPv6 addresses, make sure that
|
|
|
|
// error is correctly propagated
|
2015-11-22 03:43:08 +01:00
|
|
|
// with socks5 we would be able to do this, assuming the socks server
|
|
|
|
// supported it, but the current socks implementation in libsimulator does
|
|
|
|
// not support IPv6
|
2015-12-22 06:44:03 +01:00
|
|
|
if (ps.type != settings_pack::socks5
|
|
|
|
&& ps.type != settings_pack::http)
|
2015-11-22 03:43:08 +01:00
|
|
|
{
|
2017-05-04 23:32:47 +02:00
|
|
|
const auto expected_code = ps.type == settings_pack::socks4 ?
|
|
|
|
boost::system::errc::address_family_not_supported :
|
|
|
|
boost::system::errc::address_not_available;
|
|
|
|
|
2015-11-22 20:08:32 +01:00
|
|
|
run_test(ps, "http://[ff::dead:beef]:8080/test_file", 0, -1
|
2017-05-04 23:32:47 +02:00
|
|
|
, error_condition(expected_code, generic_category())
|
2015-11-22 03:43:08 +01:00
|
|
|
, {0,1});
|
|
|
|
}
|
2015-11-01 23:22:40 +01:00
|
|
|
|
2015-11-02 05:58:38 +01:00
|
|
|
// there is no node at 10.0.0.10, this should fail with connection refused
|
2015-12-22 06:44:03 +01:00
|
|
|
if (ps.type != settings_pack::http)
|
|
|
|
{
|
|
|
|
run_test(ps, "http://10.0.0.10:8080/test_file", 0, -1,
|
|
|
|
error_condition(boost::system::errc::connection_refused, generic_category())
|
|
|
|
, {0,1});
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
run_test(ps, "http://10.0.0.10:8080/test_file", 0, 503,
|
|
|
|
error_condition(), {1,1});
|
|
|
|
}
|
2015-11-02 05:58:38 +01:00
|
|
|
|
2015-11-24 05:24:55 +01:00
|
|
|
// the try-next test in his case would test the socks proxy itself, whether
|
|
|
|
// it has robust retry behavior (which the simple test proxy that comes with
|
|
|
|
// libsimulator doesn't).
|
2015-11-22 20:08:32 +01:00
|
|
|
if (ps.proxy_hostnames == false)
|
|
|
|
{
|
|
|
|
// this hostname will resolve to multiple IPs, all but one that we cannot
|
|
|
|
// connect to and the second one where we'll get the test file response. Make
|
|
|
|
// sure the http_connection correcly tries the second IP if the first one
|
|
|
|
// fails.
|
|
|
|
run_test(ps, "http://try-next.com:8080/test_file", 1337, 200
|
|
|
|
, error_condition(), { 1, 1, 1});
|
|
|
|
}
|
2015-11-01 23:22:40 +01:00
|
|
|
|
2015-12-22 06:44:03 +01:00
|
|
|
// the http proxy does not support hostname lookups yet
|
|
|
|
if (ps.type != settings_pack::http)
|
|
|
|
{
|
|
|
|
const error_condition expected_error = ps.proxy_hostnames
|
|
|
|
? error_condition(boost::system::errc::host_unreachable, generic_category())
|
|
|
|
: error_condition(asio::error::host_not_found, boost::asio::error::get_netdb_category());
|
2015-11-24 05:24:55 +01:00
|
|
|
|
2015-12-22 06:44:03 +01:00
|
|
|
// make sure hostname lookup failures are passed through correctly
|
|
|
|
run_test(ps, "http://non-existent.com/test_file", 0, -1
|
|
|
|
, expected_error, { 0, 1 });
|
|
|
|
}
|
2015-11-24 05:24:55 +01:00
|
|
|
|
2015-11-07 04:28:51 +01:00
|
|
|
// make sure we handle gzipped content correctly
|
2015-11-22 20:08:32 +01:00
|
|
|
run_test(ps, url_base + "/test_file.gz", 1337, 200, error_condition(), { 1, 1, 0, 0, 0, 0, 0, 1});
|
2015-11-01 23:22:40 +01:00
|
|
|
|
2015-11-22 03:43:08 +01:00
|
|
|
// TODO: 2 test basic-auth
|
|
|
|
// TODO: 2 test https
|
2015-11-01 23:22:40 +01:00
|
|
|
}
|
|
|
|
|
2015-11-22 20:08:32 +01:00
|
|
|
void run_test(lt::aux::proxy_settings ps, std::string url, int expect_size, int expect_status
|
2015-11-22 19:12:11 +01:00
|
|
|
, boost::system::error_condition expect_error, std::vector<int> expect_counters)
|
2015-11-01 18:26:22 +01:00
|
|
|
{
|
|
|
|
using sim::asio::ip::address_v4;
|
|
|
|
sim_config network_cfg;
|
|
|
|
sim::simulation sim{network_cfg};
|
|
|
|
|
2015-11-01 23:22:40 +01:00
|
|
|
// allow sparse expected counters
|
|
|
|
expect_counters.resize(num_counters, 0);
|
|
|
|
|
2015-11-01 18:26:22 +01:00
|
|
|
sim::asio::io_service web_server(sim, address_v4::from_string("10.0.0.2"));
|
|
|
|
sim::asio::io_service ios(sim, address_v4::from_string("10.0.0.1"));
|
2015-11-22 03:43:08 +01:00
|
|
|
sim::asio::io_service proxy_ios(sim, address_v4::from_string("50.50.50.50"));
|
2015-11-01 18:26:22 +01:00
|
|
|
lt::resolver res(ios);
|
|
|
|
|
|
|
|
sim::http_server http(web_server, 8080);
|
2015-11-22 20:08:32 +01:00
|
|
|
sim::socks_server socks(proxy_ios, 4444, ps.type == settings_pack::socks4 ? 4 : 5);
|
2015-12-22 06:44:03 +01:00
|
|
|
sim::http_proxy http_p(proxy_ios, 4445);
|
2015-11-01 18:26:22 +01:00
|
|
|
|
|
|
|
char data_buffer[4000];
|
|
|
|
std::generate(data_buffer, data_buffer + sizeof(data_buffer), &std::rand);
|
|
|
|
|
2015-11-01 23:22:40 +01:00
|
|
|
std::vector<int> counters(num_counters, 0);
|
|
|
|
|
2015-11-01 18:26:22 +01:00
|
|
|
http.register_handler("/test_file"
|
2015-11-22 14:13:23 +01:00
|
|
|
, [&data_buffer,&counters](std::string method, std::string req
|
2015-11-01 18:26:22 +01:00
|
|
|
, std::map<std::string, std::string>& headers)
|
|
|
|
{
|
2015-11-01 23:22:40 +01:00
|
|
|
++counters[test_file_req];
|
2015-11-01 18:26:22 +01:00
|
|
|
print_http_header(headers);
|
|
|
|
TEST_EQUAL(method, "GET");
|
|
|
|
return sim::send_response(200, "OK", 1337).append(data_buffer, 1337);
|
|
|
|
});
|
|
|
|
|
2015-11-02 05:58:38 +01:00
|
|
|
http.register_handler("/chunked_encoding"
|
2015-11-22 14:13:23 +01:00
|
|
|
, [&data_buffer,&counters](std::string method, std::string req
|
2015-11-02 05:58:38 +01:00
|
|
|
, std::map<std::string, std::string>& headers)
|
|
|
|
{
|
|
|
|
++counters[chunked_req];
|
|
|
|
print_http_header(headers);
|
|
|
|
TEST_EQUAL(method, "GET");
|
|
|
|
|
|
|
|
// there's no content length with chunked encoding
|
|
|
|
return "HTTP/1.1 200 OK\r\nTransfer-encoding: Chunked\r\n\r\n"
|
|
|
|
+ chunk_string(std::string(data_buffer, 1337));
|
|
|
|
});
|
|
|
|
|
2015-11-07 04:28:51 +01:00
|
|
|
http.register_handler("/test_file.gz"
|
2015-11-22 14:13:23 +01:00
|
|
|
, [&data_buffer,&counters](std::string method, std::string req
|
2015-11-07 04:28:51 +01:00
|
|
|
, std::map<std::string, std::string>& headers)
|
|
|
|
{
|
|
|
|
++counters[test_file_gz_req];
|
|
|
|
print_http_header(headers);
|
|
|
|
TEST_EQUAL(method, "GET");
|
|
|
|
|
|
|
|
char const* extra_headers[4] = {"Content-Encoding: gzip\r\n", "", "", ""};
|
|
|
|
unsigned char const gzheader[] = {
|
|
|
|
0x1f , 0x8b , 0x08 , 0x00 // ID, compression=deflate, flags=0
|
|
|
|
, 0x00 , 0x00 , 0x00 , 0x00 // mtime=0
|
|
|
|
, 0x00, 0x01 // extra headers, OS
|
|
|
|
, 0x01 // last block, uncompressed
|
|
|
|
, 0x39 , 0x05, 0xc6 , 0xfa // length = 1337 (little endian 16 bit and inverted)
|
|
|
|
};
|
|
|
|
unsigned char trailer[8] = { 0, 0, 0, 0, 0x39, 0x05, 0x00, 0x00 };
|
|
|
|
boost::crc_32_type crc;
|
|
|
|
crc.process_bytes(data_buffer, 1337);
|
2016-06-18 20:01:38 +02:00
|
|
|
std::uint32_t checksum = crc.checksum();
|
2015-11-07 04:28:51 +01:00
|
|
|
trailer[0] = checksum >> 24;
|
|
|
|
trailer[1] = (checksum >> 16) & 0xff;
|
|
|
|
trailer[2] = (checksum >> 8) & 0xff;
|
|
|
|
trailer[3] = (checksum) & 0xff;
|
|
|
|
|
|
|
|
std::string ret = sim::send_response(200, "OK", 1337 + sizeof(gzheader)
|
|
|
|
+ sizeof(trailer), extra_headers);
|
|
|
|
ret.append(std::string((char const*)gzheader, sizeof(gzheader)));
|
|
|
|
ret.append(data_buffer, 1337);
|
|
|
|
ret.append(std::string((char const*)trailer, sizeof(trailer)));
|
|
|
|
return ret;
|
|
|
|
});
|
|
|
|
|
2015-11-01 23:22:40 +01:00
|
|
|
http.register_handler("/redirect"
|
2015-11-22 14:13:23 +01:00
|
|
|
, [&data_buffer,&counters](std::string method, std::string req
|
2016-06-19 01:24:27 +02:00
|
|
|
, std::map<std::string, std::string>&)
|
2015-11-01 23:22:40 +01:00
|
|
|
{
|
|
|
|
++counters[redirect_req];
|
|
|
|
TEST_EQUAL(method, "GET");
|
|
|
|
return "HTTP/1.1 301 Moved Temporarily\r\n"
|
|
|
|
"Location: /test_file\r\n"
|
|
|
|
"\r\n";
|
|
|
|
});
|
|
|
|
|
|
|
|
http.register_handler("/relative/redirect"
|
2015-11-22 14:13:23 +01:00
|
|
|
, [&data_buffer,&counters](std::string method, std::string req
|
2016-06-19 01:24:27 +02:00
|
|
|
, std::map<std::string, std::string>&)
|
2015-11-01 23:22:40 +01:00
|
|
|
{
|
|
|
|
++counters[rel_redirect_req];
|
|
|
|
TEST_EQUAL(method, "GET");
|
|
|
|
return "HTTP/1.1 301 Moved Temporarily\r\n"
|
|
|
|
"Location: ../test_file\r\n"
|
|
|
|
"\r\n";
|
|
|
|
});
|
2015-11-01 18:26:22 +01:00
|
|
|
|
2015-11-01 23:22:40 +01:00
|
|
|
http.register_handler("/infinite/redirect"
|
2015-11-22 14:13:23 +01:00
|
|
|
, [&data_buffer,&counters](std::string method, std::string req
|
2016-06-19 01:24:27 +02:00
|
|
|
, std::map<std::string, std::string>&)
|
2015-11-01 23:22:40 +01:00
|
|
|
{
|
|
|
|
++counters[inf_redirect_req];
|
|
|
|
TEST_EQUAL(method, "GET");
|
|
|
|
return "HTTP/1.1 301 Moved Temporarily\r\n"
|
|
|
|
"Location: /infinite/redirect\r\n"
|
|
|
|
"\r\n";
|
|
|
|
});
|
2015-11-01 18:26:22 +01:00
|
|
|
|
2015-11-01 23:22:40 +01:00
|
|
|
auto c = test_request(ios, res, url, data_buffer, expect_size
|
|
|
|
, expect_status, expect_error, ps, &counters[connect_handler]
|
|
|
|
, &counters[handler]);
|
2015-11-01 18:26:22 +01:00
|
|
|
|
|
|
|
error_code e;
|
|
|
|
sim.run(e);
|
|
|
|
|
|
|
|
if (e) std::cerr << " run failed: " << e.message() << std::endl;
|
2015-11-01 23:22:40 +01:00
|
|
|
TEST_EQUAL(e, error_code());
|
2015-11-01 18:26:22 +01:00
|
|
|
|
2015-11-01 23:22:40 +01:00
|
|
|
TEST_EQUAL(counters.size(), expect_counters.size());
|
2015-12-12 06:15:24 +01:00
|
|
|
for (int i = 0; i < int(counters.size()); ++i)
|
2015-11-01 23:22:40 +01:00
|
|
|
{
|
2016-10-10 02:23:45 +02:00
|
|
|
if (counters[i] != expect_counters[i]) std::printf("i=%d\n", i);
|
2015-11-01 23:22:40 +01:00
|
|
|
TEST_EQUAL(counters[i], expect_counters[i]);
|
|
|
|
}
|
2015-11-01 18:26:22 +01:00
|
|
|
}
|
|
|
|
|
2015-11-22 03:43:08 +01:00
|
|
|
TORRENT_TEST(http_connection)
|
|
|
|
{
|
2015-11-22 20:08:32 +01:00
|
|
|
lt::aux::proxy_settings ps = make_proxy_settings(settings_pack::none);
|
|
|
|
run_suite(ps);
|
2015-11-22 03:43:08 +01:00
|
|
|
}
|
|
|
|
|
2015-12-22 06:44:03 +01:00
|
|
|
TORRENT_TEST(http_connection_http)
|
|
|
|
{
|
|
|
|
lt::aux::proxy_settings ps = make_proxy_settings(settings_pack::http);
|
|
|
|
ps.proxy_hostnames = true;
|
|
|
|
run_suite(ps);
|
|
|
|
}
|
|
|
|
|
2015-11-22 03:43:08 +01:00
|
|
|
TORRENT_TEST(http_connection_socks4)
|
|
|
|
{
|
2015-11-22 20:08:32 +01:00
|
|
|
lt::aux::proxy_settings ps = make_proxy_settings(settings_pack::socks4);
|
|
|
|
run_suite(ps);
|
2015-11-22 03:43:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(http_connection_socks5)
|
|
|
|
{
|
2015-11-22 20:08:32 +01:00
|
|
|
lt::aux::proxy_settings ps = make_proxy_settings(settings_pack::socks5);
|
|
|
|
run_suite(ps);
|
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(http_connection_socks5_proxy_names)
|
|
|
|
{
|
|
|
|
lt::aux::proxy_settings ps = make_proxy_settings(settings_pack::socks5);
|
|
|
|
ps.proxy_hostnames = true;
|
|
|
|
run_suite(ps);
|
2015-11-22 03:43:08 +01:00
|
|
|
}
|
|
|
|
|
2017-05-04 23:32:47 +02:00
|
|
|
// tests the error scenario of a http server listening on two sockets (ipv4/ipv6) which
|
|
|
|
// both accept the incoming connection but never send anything back. we test that
|
|
|
|
// both ip addresses get tried in turn and that the connection attempts time out as expected.
|
|
|
|
TORRENT_TEST(http_connection_timeout_server_stalls)
|
|
|
|
{
|
|
|
|
sim_config network_cfg;
|
|
|
|
sim::simulation sim{network_cfg};
|
|
|
|
// server has two ip addresses (ipv4/ipv6)
|
|
|
|
sim::asio::io_service server_ios(sim, address_v4::from_string("10.0.0.2"));
|
|
|
|
sim::asio::io_service server_ios_ipv6(sim, address_v6::from_string("ff::dead:beef"));
|
|
|
|
// same for client
|
|
|
|
sim::asio::io_service client_ios(sim, {
|
|
|
|
address_v4::from_string("10.0.0.1"),
|
|
|
|
address_v6::from_string("ff::abad:cafe")
|
|
|
|
});
|
|
|
|
lt::resolver resolver(client_ios);
|
|
|
|
|
|
|
|
const unsigned short http_port = 8080;
|
|
|
|
sim::http_server http(server_ios, http_port);
|
|
|
|
sim::http_server http_ipv6(server_ios_ipv6, http_port);
|
|
|
|
|
|
|
|
http.register_stall_handler("/timeout");
|
|
|
|
http_ipv6.register_stall_handler("/timeout");
|
|
|
|
|
|
|
|
char data_buffer[4000];
|
|
|
|
std::generate(data_buffer, data_buffer + sizeof(data_buffer), &std::rand);
|
|
|
|
|
|
|
|
int connect_counter = 0;
|
|
|
|
int handler_counter = 0;
|
|
|
|
|
|
|
|
error_condition timed_out(boost::system::errc::timed_out, boost::system::generic_category());
|
|
|
|
|
|
|
|
auto c = test_request(client_ios, resolver
|
|
|
|
, "http://dual-stack.test-hostname.com:8080/timeout", data_buffer, -1, -1
|
|
|
|
, timed_out, lt::aux::proxy_settings()
|
|
|
|
, &connect_counter, &handler_counter);
|
|
|
|
|
|
|
|
error_code e;
|
|
|
|
sim.run(e);
|
|
|
|
TEST_CHECK(!e);
|
2017-05-08 13:48:42 +02:00
|
|
|
TEST_EQUAL(connect_counter, 2); // both endpoints are connected to
|
|
|
|
TEST_EQUAL(handler_counter, 1); // the handler only gets called once with error_code == timed_out
|
2017-05-04 23:32:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// tests the error scenario of a http server listening on two sockets (ipv4/ipv6) neither of which
|
|
|
|
// accept incoming connections. we test that both ip addresses get tried in turn and that the
|
|
|
|
// connection attempts time out as expected.
|
|
|
|
TORRENT_TEST(http_connection_timeout_server_does_not_accept)
|
|
|
|
{
|
|
|
|
sim_config network_cfg;
|
|
|
|
sim::simulation sim{network_cfg};
|
|
|
|
// server has two ip addresses (ipv4/ipv6)
|
|
|
|
sim::asio::io_service server_ios(sim, {
|
|
|
|
address_v4::from_string("10.0.0.2"),
|
|
|
|
address_v6::from_string("ff::dead:beef")
|
|
|
|
});
|
|
|
|
// same for client
|
|
|
|
sim::asio::io_service client_ios(sim, {
|
|
|
|
address_v4::from_string("10.0.0.1"),
|
|
|
|
address_v6::from_string("ff::abad:cafe")
|
|
|
|
});
|
|
|
|
lt::resolver resolver(client_ios);
|
|
|
|
|
|
|
|
const unsigned short http_port = 8080;
|
|
|
|
|
|
|
|
// listen on two sockets, but don't accept connections
|
|
|
|
asio::ip::tcp::acceptor server_socket_ipv4(server_ios);
|
|
|
|
server_socket_ipv4.open(tcp::v4());
|
|
|
|
server_socket_ipv4.bind(tcp::endpoint(address_v4::any(), http_port));
|
|
|
|
server_socket_ipv4.listen();
|
|
|
|
|
|
|
|
asio::ip::tcp::acceptor server_socket_ipv6(server_ios);
|
|
|
|
server_socket_ipv6.open(tcp::v6());
|
|
|
|
server_socket_ipv6.bind(tcp::endpoint(address_v6::any(), http_port));
|
|
|
|
server_socket_ipv6.listen();
|
|
|
|
|
|
|
|
int connect_counter = 0;
|
|
|
|
int handler_counter = 0;
|
|
|
|
|
|
|
|
error_condition timed_out(boost::system::errc::timed_out, boost::system::generic_category());
|
|
|
|
|
|
|
|
char data_buffer[4000];
|
|
|
|
std::generate(data_buffer, data_buffer + sizeof(data_buffer), &std::rand);
|
|
|
|
|
|
|
|
auto c = test_request(client_ios, resolver
|
|
|
|
, "http://dual-stack.test-hostname.com:8080/timeout_server_does_not_accept", data_buffer, -1, -1
|
|
|
|
, timed_out, lt::aux::proxy_settings()
|
|
|
|
, &connect_counter, &handler_counter);
|
|
|
|
|
|
|
|
error_code e;
|
|
|
|
sim.run(e);
|
|
|
|
TEST_CHECK(!e);
|
2017-05-08 13:48:42 +02:00
|
|
|
TEST_EQUAL(connect_counter, 0); // no connection takes place
|
|
|
|
TEST_EQUAL(handler_counter, 1); // the handler only gets called once with error_code == timed_out
|
2017-05-04 23:32:47 +02:00
|
|
|
}
|
|
|
|
|
2015-12-22 06:44:03 +01:00
|
|
|
void test_proxy_failure(lt::settings_pack::proxy_type_t proxy_type)
|
2015-11-22 03:43:08 +01:00
|
|
|
{
|
|
|
|
using sim::asio::ip::address_v4;
|
|
|
|
sim_config network_cfg;
|
|
|
|
sim::simulation sim{network_cfg};
|
|
|
|
|
|
|
|
sim::asio::io_service web_server(sim, address_v4::from_string("10.0.0.2"));
|
|
|
|
sim::asio::io_service ios(sim, address_v4::from_string("10.0.0.1"));
|
|
|
|
lt::resolver res(ios);
|
|
|
|
|
|
|
|
sim::http_server http(web_server, 8080);
|
|
|
|
|
2015-12-22 06:44:03 +01:00
|
|
|
lt::aux::proxy_settings ps = make_proxy_settings(proxy_type);
|
2015-11-22 03:43:08 +01:00
|
|
|
|
|
|
|
char data_buffer[4000];
|
|
|
|
std::generate(data_buffer, data_buffer + sizeof(data_buffer), &std::rand);
|
|
|
|
|
|
|
|
http.register_handler("/test_file"
|
2015-11-22 14:13:23 +01:00
|
|
|
, [&data_buffer](std::string method, std::string req
|
2015-11-22 03:43:08 +01:00
|
|
|
, std::map<std::string, std::string>& headers)
|
|
|
|
{
|
|
|
|
print_http_header(headers);
|
2016-06-19 01:24:27 +02:00
|
|
|
// we're not supposed to get here
|
|
|
|
TEST_CHECK(false);
|
2015-11-22 03:43:08 +01:00
|
|
|
return sim::send_response(200, "OK", 1337).append(data_buffer, 1337);
|
|
|
|
});
|
|
|
|
|
|
|
|
int connect_counter = 0;
|
|
|
|
int handler_counter = 0;
|
|
|
|
auto c = test_request(ios, res, "http://10.0.0.2:8080/test_file"
|
|
|
|
, data_buffer, -1, -1
|
2015-11-22 19:12:11 +01:00
|
|
|
, error_condition(boost::system::errc::connection_refused, boost::system::generic_category())
|
2015-11-22 03:43:08 +01:00
|
|
|
, ps, &connect_counter, &handler_counter);
|
|
|
|
|
|
|
|
error_code e;
|
|
|
|
sim.run(e);
|
|
|
|
|
|
|
|
if (e) std::cerr << " run failed: " << e.message() << std::endl;
|
|
|
|
TEST_EQUAL(e, error_code());
|
|
|
|
}
|
|
|
|
|
2015-12-22 06:44:03 +01:00
|
|
|
// if we set up to user a proxy that does not exist, expect failure!
|
|
|
|
// if this doesn't fail, the other tests are invalid because the proxy may not
|
|
|
|
// be exercised!
|
|
|
|
TORRENT_TEST(http_connection_socks_error)
|
|
|
|
{
|
|
|
|
test_proxy_failure(settings_pack::socks5);
|
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_TEST(http_connection_http_error)
|
|
|
|
{
|
|
|
|
test_proxy_failure(settings_pack::http);
|
|
|
|
}
|
|
|
|
|
2017-05-08 17:04:17 +02:00
|
|
|
// Requests a proxied SSL connection. This test just ensures that the correct CONNECT request
|
|
|
|
// is sent to the proxy server.
|
|
|
|
TORRENT_TEST(http_connection_ssl_proxy)
|
|
|
|
{
|
|
|
|
using sim::asio::ip::address_v4;
|
|
|
|
sim_config network_cfg;
|
|
|
|
sim::simulation sim{network_cfg};
|
|
|
|
|
|
|
|
sim::asio::io_service client_ios(sim, address_v4::from_string("10.0.0.1"));
|
|
|
|
sim::asio::io_service proxy_ios(sim, address_v4::from_string("50.50.50.50"));
|
|
|
|
lt::resolver res(client_ios);
|
|
|
|
|
|
|
|
sim::http_server http_proxy(proxy_ios, 4445);
|
|
|
|
|
|
|
|
lt::aux::proxy_settings ps = make_proxy_settings(settings_pack::http);
|
|
|
|
|
|
|
|
int client_counter = 0;
|
|
|
|
int proxy_counter = 0;
|
|
|
|
|
|
|
|
http_proxy.register_handler("10.0.0.2:8080"
|
|
|
|
, [&proxy_counter](std::string method, std::string req, std::map<std::string, std::string>& headers)
|
|
|
|
{
|
|
|
|
proxy_counter++;
|
|
|
|
TEST_EQUAL(method, "CONNECT");
|
|
|
|
return sim::send_response(403, "Not supported", 1337);
|
|
|
|
});
|
|
|
|
|
2017-05-13 03:46:45 +02:00
|
|
|
auto h = std::make_shared<http_connection>(client_ios
|
2017-05-08 17:04:17 +02:00
|
|
|
, res
|
|
|
|
, [&client_counter](error_code const& ec, http_parser const& parser
|
2017-09-04 00:13:08 +02:00
|
|
|
, span<char const>, http_connection& c)
|
2017-05-08 17:04:17 +02:00
|
|
|
{
|
|
|
|
client_counter++;
|
|
|
|
TEST_EQUAL(ec, boost::asio::error::operation_not_supported);
|
|
|
|
});
|
|
|
|
|
|
|
|
h->start("10.0.0.2", 8080, seconds(1), 0, &ps, true /*ssl*/);
|
|
|
|
|
|
|
|
error_code e;
|
|
|
|
sim.run(e);
|
|
|
|
|
|
|
|
TEST_EQUAL(client_counter, 1);
|
|
|
|
TEST_EQUAL(proxy_counter, 1);
|
|
|
|
if (e) std::cerr << " run failed: " << e.message() << std::endl;
|
|
|
|
TEST_EQUAL(e, error_code());
|
|
|
|
}
|
|
|
|
|
2015-12-22 06:44:03 +01:00
|
|
|
// TODO: test http proxy with password
|
2015-11-22 03:43:08 +01:00
|
|
|
// TODO: test socks5 with password
|
2015-11-24 05:39:26 +01:00
|
|
|
// TODO: test SSL
|
|
|
|
// TODO: test keepalive
|
2015-11-22 03:43:08 +01:00
|
|
|
|