2008-07-01 20:59:13 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2008, 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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2007-11-18 05:12:35 +01:00
|
|
|
#include "test.hpp"
|
|
|
|
#include "libtorrent/socket.hpp"
|
2009-12-16 11:42:34 +01:00
|
|
|
#include "libtorrent/socket_io.hpp" // print_endpoint
|
2007-11-18 05:12:35 +01:00
|
|
|
#include "libtorrent/connection_queue.hpp"
|
|
|
|
#include "libtorrent/http_connection.hpp"
|
2007-11-27 01:06:59 +01:00
|
|
|
#include "setup_transfer.hpp"
|
|
|
|
|
|
|
|
#include <fstream>
|
2009-12-15 14:11:07 +01:00
|
|
|
#include <iostream>
|
2007-11-27 01:06:59 +01:00
|
|
|
#include <boost/optional.hpp>
|
2007-11-18 05:12:35 +01:00
|
|
|
|
|
|
|
using namespace libtorrent;
|
|
|
|
|
|
|
|
io_service ios;
|
|
|
|
connection_queue cq(ios);
|
|
|
|
|
|
|
|
int connect_handler_called = 0;
|
|
|
|
int handler_called = 0;
|
|
|
|
int data_size = 0;
|
|
|
|
int http_status = 0;
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code g_error_code;
|
2007-11-27 01:06:59 +01:00
|
|
|
char data_buffer[4000];
|
2007-11-18 05:12:35 +01:00
|
|
|
|
|
|
|
void print_http_header(http_parser const& p)
|
|
|
|
{
|
2007-11-27 01:06:59 +01:00
|
|
|
std::cerr << " < " << p.status_code() << " " << p.message() << std::endl;
|
2007-11-18 05:12:35 +01:00
|
|
|
|
2011-02-16 08:41:44 +01:00
|
|
|
for (std::multimap<std::string, std::string>::const_iterator i
|
2007-11-18 05:12:35 +01:00
|
|
|
= p.headers().begin(), end(p.headers().end()); i != end; ++i)
|
|
|
|
{
|
2007-11-27 01:06:59 +01:00
|
|
|
std::cerr << " < " << i->first << ": " << i->second << std::endl;
|
2007-11-18 05:12:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void http_connect_handler(http_connection& c)
|
|
|
|
{
|
|
|
|
++connect_handler_called;
|
|
|
|
TEST_CHECK(c.socket().is_open());
|
2008-11-05 04:17:41 +01:00
|
|
|
error_code ec;
|
2009-12-15 14:11:07 +01:00
|
|
|
std::cerr << "connected to: " << print_endpoint(c.socket().remote_endpoint(ec))
|
|
|
|
<< std::endl;
|
2010-08-03 11:08:37 +02:00
|
|
|
// this is not necessarily true when using a proxy and proxying hostnames
|
|
|
|
// TEST_CHECK(c.socket().remote_endpoint(ec).address() == address::from_string("127.0.0.1", ec));
|
2007-11-18 05:12:35 +01:00
|
|
|
}
|
|
|
|
|
2008-05-17 02:27:26 +02:00
|
|
|
void http_handler(error_code const& ec, http_parser const& parser
|
|
|
|
, char const* data, int size, http_connection& c)
|
2007-11-18 05:12:35 +01:00
|
|
|
{
|
|
|
|
++handler_called;
|
|
|
|
data_size = size;
|
2008-05-03 18:05:42 +02:00
|
|
|
g_error_code = ec;
|
2011-01-06 05:08:57 +01:00
|
|
|
TORRENT_ASSERT(size == 0 || parser.finished());
|
2007-11-18 05:12:35 +01:00
|
|
|
|
2007-11-27 01:06:59 +01:00
|
|
|
if (parser.header_finished())
|
|
|
|
{
|
|
|
|
http_status = parser.status_code();
|
|
|
|
if (http_status == 200)
|
|
|
|
{
|
|
|
|
TEST_CHECK(memcmp(data, data_buffer, size) == 0);
|
|
|
|
}
|
|
|
|
}
|
2007-11-18 05:12:35 +01:00
|
|
|
print_http_header(parser);
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset_globals()
|
|
|
|
{
|
|
|
|
connect_handler_called = 0;
|
|
|
|
handler_called = 0;
|
|
|
|
data_size = 0;
|
|
|
|
http_status = 0;
|
2008-05-03 18:05:42 +02:00
|
|
|
g_error_code = error_code();
|
2007-11-18 05:12:35 +01:00
|
|
|
}
|
|
|
|
|
2008-01-27 23:39:50 +01:00
|
|
|
void run_test(std::string const& url, int size, int status, int connected
|
2008-05-03 18:05:42 +02:00
|
|
|
, boost::optional<error_code> ec, proxy_settings const& ps)
|
2007-11-18 05:12:35 +01:00
|
|
|
{
|
|
|
|
reset_globals();
|
|
|
|
|
|
|
|
std::cerr << " ===== TESTING: " << url << " =====" << std::endl;
|
|
|
|
|
2009-07-26 04:17:21 +02:00
|
|
|
std::cerr << " expecting: size: " << size
|
|
|
|
<< " status: " << status
|
|
|
|
<< " connected: " << connected
|
|
|
|
<< " error: " << (ec?ec->message():"no error") << std::endl;
|
|
|
|
|
2007-11-18 05:12:35 +01:00
|
|
|
boost::shared_ptr<http_connection> h(new http_connection(ios, cq
|
|
|
|
, &::http_handler, true, &::http_connect_handler));
|
2008-11-14 08:59:54 +01:00
|
|
|
h->get(url, seconds(1), 0, &ps);
|
2007-11-18 05:12:35 +01:00
|
|
|
ios.reset();
|
2008-11-05 04:17:41 +01:00
|
|
|
error_code e;
|
|
|
|
ios.run(e);
|
2007-11-18 05:12:35 +01:00
|
|
|
|
|
|
|
std::cerr << "connect_handler_called: " << connect_handler_called << std::endl;
|
|
|
|
std::cerr << "handler_called: " << handler_called << std::endl;
|
|
|
|
std::cerr << "status: " << http_status << std::endl;
|
|
|
|
std::cerr << "size: " << data_size << std::endl;
|
2008-05-03 18:05:42 +02:00
|
|
|
std::cerr << "error_code: " << g_error_code.message() << std::endl;
|
2007-11-19 04:48:45 +01:00
|
|
|
TEST_CHECK(connect_handler_called == connected);
|
2007-11-18 05:12:35 +01:00
|
|
|
TEST_CHECK(handler_called == 1);
|
|
|
|
TEST_CHECK(data_size == size || size == -1);
|
2008-05-03 18:05:42 +02:00
|
|
|
TEST_CHECK(!ec || g_error_code == *ec);
|
2007-11-19 04:48:45 +01:00
|
|
|
TEST_CHECK(http_status == status || status == -1);
|
2007-11-18 05:12:35 +01:00
|
|
|
}
|
|
|
|
|
2010-01-24 00:57:11 +01:00
|
|
|
void run_suite(std::string const& protocol, proxy_settings const& ps, int port)
|
2007-11-18 05:12:35 +01:00
|
|
|
{
|
2008-01-27 23:39:50 +01:00
|
|
|
if (ps.type != proxy_settings::none)
|
|
|
|
{
|
|
|
|
start_proxy(ps.port, ps.type);
|
|
|
|
}
|
|
|
|
char const* test_name[] = {"no", "SOCKS4", "SOCKS5"
|
|
|
|
, "SOCKS5 password protected", "HTTP", "HTTP password protected"};
|
|
|
|
std::cout << "\n\n********************** using " << test_name[ps.type]
|
|
|
|
<< " proxy **********************\n" << std::endl;
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
typedef boost::optional<error_code> err;
|
2008-05-19 00:14:55 +02:00
|
|
|
// this requires the hosts file to be modified
|
|
|
|
// run_test(protocol + "://test.dns.ts:8001/test_file", 3216, 200, 1, error_code(), ps);
|
|
|
|
|
2010-01-24 00:57:11 +01:00
|
|
|
char url[256];
|
|
|
|
snprintf(url, sizeof(url), "%s://127.0.0.1:%d/", protocol.c_str(), port);
|
|
|
|
std::string url_base(url);
|
|
|
|
|
|
|
|
run_test(url_base + "relative/redirect", 3216, 200, 2, error_code(), ps);
|
|
|
|
run_test(url_base + "redirect", 3216, 200, 2, error_code(), ps);
|
|
|
|
run_test(url_base + "infinite_redirect", 0, 301, 6, error_code(), ps);
|
|
|
|
run_test(url_base + "test_file", 3216, 200, 1, error_code(), ps);
|
|
|
|
run_test(url_base + "test_file.gz", 3216, 200, 1, error_code(), ps);
|
|
|
|
run_test(url_base + "non-existing-file", -1, 404, 1, err(), ps);
|
2010-08-28 22:32:58 +02:00
|
|
|
|
|
|
|
// only run the tests to handle NX_DOMAIN if we have a proper internet
|
|
|
|
// connection that doesn't inject false DNS responses (like Comcast does)
|
|
|
|
hostent* h = gethostbyname("non-existent-domain.se");
|
|
|
|
if (h == 0 && h_errno == HOST_NOT_FOUND)
|
|
|
|
{
|
|
|
|
// if we're going through an http proxy, we won't get the same error as if the hostname
|
|
|
|
// resolution failed
|
|
|
|
if ((ps.type == proxy_settings::http || ps.type == proxy_settings::http_pw) && protocol != "https")
|
|
|
|
run_test(protocol + "://non-existent-domain.se/non-existing-file", -1, 502, 1, err(), ps);
|
|
|
|
else
|
|
|
|
run_test(protocol + "://non-existent-domain.se/non-existing-file", -1, -1, 0, err(), ps);
|
|
|
|
}
|
2008-01-27 23:39:50 +01:00
|
|
|
|
|
|
|
if (ps.type != proxy_settings::none)
|
|
|
|
stop_proxy(ps.port);
|
|
|
|
}
|
|
|
|
|
|
|
|
int test_main()
|
|
|
|
{
|
2007-11-27 01:06:59 +01:00
|
|
|
std::srand(std::time(0));
|
|
|
|
std::generate(data_buffer, data_buffer + sizeof(data_buffer), &std::rand);
|
2009-11-09 17:48:16 +01:00
|
|
|
error_code ec;
|
|
|
|
file test_file("test_file", file::write_only, ec);
|
|
|
|
TEST_CHECK(!ec);
|
|
|
|
if (ec) fprintf(stderr, "file error: %s\n", ec.message().c_str());
|
|
|
|
file::iovec_t b = { data_buffer, 3216};
|
|
|
|
test_file.writev(0, &b, 1, ec);
|
|
|
|
TEST_CHECK(!ec);
|
|
|
|
if (ec) fprintf(stderr, "file error: %s\n", ec.message().c_str());
|
2008-11-14 08:59:54 +01:00
|
|
|
test_file.close();
|
2008-01-30 19:32:13 +01:00
|
|
|
std::system("gzip -9 -c test_file > test_file.gz");
|
|
|
|
|
2008-01-27 23:39:50 +01:00
|
|
|
proxy_settings ps;
|
|
|
|
ps.hostname = "127.0.0.1";
|
|
|
|
ps.port = 8034;
|
|
|
|
ps.username = "testuser";
|
|
|
|
ps.password = "testpass";
|
2011-01-06 05:08:57 +01:00
|
|
|
int port = 0;
|
2008-01-27 23:39:50 +01:00
|
|
|
|
2011-01-06 05:08:57 +01:00
|
|
|
port = start_web_server();
|
2008-01-27 23:39:50 +01:00
|
|
|
for (int i = 0; i < 5; ++i)
|
|
|
|
{
|
|
|
|
ps.type = (proxy_settings::proxy_type)i;
|
2010-01-24 00:57:11 +01:00
|
|
|
run_suite("http", ps, port);
|
2008-01-27 23:39:50 +01:00
|
|
|
}
|
2010-01-24 00:57:11 +01:00
|
|
|
stop_web_server();
|
2008-01-27 23:39:50 +01:00
|
|
|
|
|
|
|
#ifdef TORRENT_USE_OPENSSL
|
2010-01-24 00:57:11 +01:00
|
|
|
port = start_web_server(true);
|
2008-01-27 23:39:50 +01:00
|
|
|
for (int i = 0; i < 5; ++i)
|
|
|
|
{
|
|
|
|
ps.type = (proxy_settings::proxy_type)i;
|
2010-01-24 00:57:11 +01:00
|
|
|
run_suite("https", ps, port);
|
2008-01-27 23:39:50 +01:00
|
|
|
}
|
2010-01-24 00:57:11 +01:00
|
|
|
stop_web_server();
|
2008-01-27 23:39:50 +01:00
|
|
|
#endif
|
|
|
|
|
2011-01-06 05:08:57 +01:00
|
|
|
// test chunked encoding
|
|
|
|
port = start_web_server(false, true);
|
|
|
|
ps.type = proxy_settings::none;
|
|
|
|
run_suite("http", ps, port);
|
|
|
|
|
|
|
|
stop_web_server();
|
|
|
|
|
2007-11-27 01:06:59 +01:00
|
|
|
std::remove("test_file");
|
2007-11-18 05:12:35 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|