move time_now_string out to the unit test library (basically unused in libtorrent itself)

This commit is contained in:
Arvid Norberg 2015-04-24 03:48:08 +00:00
parent 91e66f65d3
commit 3ea67e2bf5
21 changed files with 202 additions and 100 deletions

View File

@ -359,7 +359,7 @@ if(build_tests)
FILE(GLOB tests RELATIVE "${PROJECT_SOURCE_DIR}" "test/test_*.cpp")
add_library(test_common STATIC test/main.cpp test/setup_transfer.cpp
test/dht_server.cpp test/udp_tracker.cpp test/peer_server.cpp
test/web_seed_suite.cpp)
test/web_seed_suite.cpp test/test_utils.cpp)
enable_testing()
foreach(s ${tests})

View File

@ -40,10 +40,6 @@ POSSIBILITY OF SUCH DAMAGE.
namespace libtorrent { namespace aux
{
// TODO: 3 this is only used for debug logging. It should probably not
// be included unconditionally
TORRENT_EXTRA_EXPORT char const* time_now_string();
std::string log_time();
// returns the current time, as represented by time_point. The

View File

@ -394,8 +394,8 @@ int snprintf(char* buf, int len, char const* fmt, ...)
// at the highest warning level, clang actually warns about functions
// that could be marked noreturn. There seems to be versions of GCC
// that declare being
#if defined __clang__ && defined __cplusplus && __cplusplus >= 199711L
#define TORRENT_NO_RETURN [[noreturn]]
#if defined __clang__ || defined __GNUC__
#define TORRENT_NO_RETURN __attribute((noreturn))
#else
#define TORRENT_NO_RETURN
#endif

View File

@ -34,6 +34,9 @@ POSSIBILITY OF SUCH DAMAGE.
#define TORRENT_TIME_HPP_INCLUDED
#include "libtorrent/config.hpp"
#include "libtorrent/aux_/disable_warnings_push.hpp"
#include <boost/cstdint.hpp>
#if defined BOOST_ASIO_HAS_STD_CHRONO
@ -42,6 +45,8 @@ POSSIBILITY OF SUCH DAMAGE.
#include <boost/chrono.hpp>
#endif
#include "libtorrent/aux_/disable_warnings_pop.hpp"
namespace libtorrent {
#if defined BOOST_ASIO_HAS_STD_CHRONO

View File

@ -36,11 +36,12 @@ POSSIBILITY OF SUCH DAMAGE.
namespace libtorrent { namespace dht
{
// TODO: 3 replace this logging with alerts
log_event::log_event(log& log)
: log_(log)
{
if (log_.enabled())
log_ << libtorrent::aux::time_now_string() << " [" << log.id() << "] ";
log_ << libtorrent::aux::log_time() << " [" << log.id() << "] ";
}
log_event::~log_event()

View File

@ -222,7 +222,7 @@ namespace libtorrent { namespace
#ifndef TORRENT_DISABLE_LOGGING
std::stringstream log_line;
log_line << aux::time_now_string() << " <== LT_TEX [ "
log_line << " <== LT_TEX [ "
"added: ";
#endif
@ -327,7 +327,7 @@ namespace libtorrent { namespace
#ifndef TORRENT_DISABLE_LOGGING
std::stringstream log_line;
log_line << aux::time_now_string() << " ==> LT_TEX [ "
log_line << " ==> LT_TEX [ "
"added: ";
#endif
entry tex;

View File

@ -39,10 +39,6 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/time.hpp"
#include "libtorrent/aux_/time.hpp"
#ifndef _WIN32
#include <unistd.h>
#endif
namespace libtorrent { namespace aux
{
// used to cache the current time
@ -54,22 +50,6 @@ namespace libtorrent { namespace aux
time_point const& time_now() { return aux::g_current_time; }
TORRENT_EXTRA_EXPORT char const* time_now_string()
{
static const time_point start = clock_type::now();
static char ret[200];
int t = total_milliseconds(clock_type::now() - start);
int h = t / 1000 / 60 / 60;
t -= h * 60 * 60 * 1000;
int m = t / 1000 / 60;
t -= m * 60 * 1000;
int s = t / 1000;
t -= s * 1000;
int ms = t;
snprintf(ret, sizeof(ret), "%02d:%02d:%02d.%03d", h, m, s, ms);
return ret;
}
std::string log_time()
{
static const time_point start = clock_type::now();

View File

@ -56,6 +56,7 @@ lib libtorrent_test
peer_server.cpp
web_seed_suite.cpp
swarm_suite.cpp
test_utils.cpp
: # requirements
# this is used to determine whether

View File

@ -128,7 +128,8 @@ EXTRA_DIST = Jamfile \
EXTRA_PROGRAMS = $(test_programs)
noinst_HEADERS = test.hpp setup_transfer.hpp dht_server.hpp \
peer_server.hpp udp_tracker.hpp web_seed_suite.hpp swarm_suite.hpp
peer_server.hpp udp_tracker.hpp web_seed_suite.hpp swarm_suite.hpp \
test_utils.hpp
libtest_la_SOURCES = main.cpp \
setup_transfer.cpp \
@ -136,7 +137,8 @@ libtest_la_SOURCES = main.cpp \
udp_tracker.cpp \
peer_server.cpp \
web_seed_suite.cpp \
swarm_suite.cpp
swarm_suite.cpp \
test_utils.cpp
test_bitfield_SOURCES = test_bitfield.cpp
test_crc32_SOURCES = test_crc32.cpp

View File

@ -39,6 +39,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/socket.hpp"
#include "libtorrent/aux_/time.hpp"
#include "dht_server.hpp"
#include "test_utils.hpp"
#include <boost/detail/atomic_count.hpp>
#include <boost/shared_ptr.hpp>
@ -86,7 +87,7 @@ struct dht_server
return;
}
fprintf(stderr, "%s: DHT initialized on port %d\n", aux::time_now_string(), m_port);
fprintf(stderr, "%s: DHT initialized on port %d\n", time_now_string(), m_port);
m_thread.reset(new thread(boost::bind(&dht_server::thread_fun, this)));
}
@ -171,8 +172,8 @@ int num_dht_hits()
void stop_dht()
{
fprintf(stderr, "%s: stop_dht()\n", aux::time_now_string());
fprintf(stderr, "%s: stop_dht()\n", time_now_string());
g_dht.reset();
fprintf(stderr, "%s: stop_dht() done\n", aux::time_now_string());
fprintf(stderr, "%s: stop_dht() done\n", time_now_string());
}

View File

@ -39,6 +39,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/socket.hpp"
#include "libtorrent/aux_/time.hpp"
#include "peer_server.hpp"
#include "test_utils.hpp"
#include <boost/detail/atomic_count.hpp>
#include <boost/shared_ptr.hpp>
@ -88,7 +89,7 @@ struct peer_server
return;
}
fprintf(stderr, "%s: PEER peer initialized on port %d\n", aux::time_now_string(), m_port);
fprintf(stderr, "%s: PEER peer initialized on port %d\n", time_now_string(), m_port);
m_thread.reset(new thread(boost::bind(&peer_server::thread_fun, this)));
}
@ -135,7 +136,7 @@ struct peer_server
return;
}
fprintf(stderr, "%s: PEER incoming peer connection\n", aux::time_now_string());
fprintf(stderr, "%s: PEER incoming peer connection\n", time_now_string());
++m_peer_requests;
socket.close(ec);
}
@ -159,8 +160,8 @@ int num_peer_hits()
void stop_peer()
{
fprintf(stderr, "%s: PEER stop_peer()\n", aux::time_now_string());
fprintf(stderr, "%s: PEER stop_peer()\n", time_now_string());
g_peer.reset();
fprintf(stderr, "%s: PEER stop_peer() done\n", aux::time_now_string());
fprintf(stderr, "%s: PEER stop_peer() done\n", time_now_string());
}

View File

@ -47,6 +47,8 @@ POSSIBILITY OF SUCH DAMAGE.
#include <boost/make_shared.hpp>
#include "test.hpp"
#include "test_utils.hpp"
#include "libtorrent/assert.hpp"
#include "libtorrent/alert_types.hpp"
#include "libtorrent/create_torrent.hpp"
@ -156,7 +158,7 @@ alert const* wait_for_alert(lt::session& ses, int type, char const* name)
for (std::vector<alert*>::iterator i = alerts.begin()
, end(alerts.end()); i != end; ++i)
{
fprintf(stderr, "%s: %s: [%s] %s\n", aux::time_now_string(), name
fprintf(stderr, "%s: %s: [%s] %s\n", time_now_string(), name
, (*i)->what(), (*i)->message().c_str());
if ((*i)->type() == type)
{
@ -265,14 +267,14 @@ bool print_alerts(lt::session& ses, char const* name
if (predicate && predicate(*i)) ret = true;
if (peer_disconnected_alert const* p = alert_cast<peer_disconnected_alert>(*i))
{
fprintf(stderr, "%s: %s: [%s] (%s): %s\n", aux::time_now_string(), name, (*i)->what(), print_endpoint(p->ip).c_str(), p->message().c_str());
fprintf(stderr, "%s: %s: [%s] (%s): %s\n", time_now_string(), name, (*i)->what(), print_endpoint(p->ip).c_str(), p->message().c_str());
}
else if ((*i)->message() != "block downloading"
&& (*i)->message() != "block finished"
&& (*i)->message() != "piece finished"
&& !no_output)
{
fprintf(stderr, "%s: %s: [%s] %s\n", aux::time_now_string(), name, (*i)->what(), (*i)->message().c_str());
fprintf(stderr, "%s: %s: [%s] %s\n", time_now_string(), name, (*i)->what(), (*i)->message().c_str());
}
TEST_CHECK(alert_cast<fastresume_rejected_alert>(*i) == 0 || allow_failed_fastresume);
@ -280,7 +282,7 @@ bool print_alerts(lt::session& ses, char const* name
peer_error_alert const* pea = alert_cast<peer_error_alert>(*i);
if (pea)
{
fprintf(stderr, "%s: peer error: %s\n", aux::time_now_string(), pea->error.message().c_str());
fprintf(stderr, "%s: peer error: %s\n", time_now_string(), pea->error.message().c_str());
TEST_CHECK((!handles.empty() && h.status().is_seeding)
|| pea->error.message() == "connecting to peer"
|| pea->error.message() == "closing connection to ourself"
@ -543,13 +545,13 @@ int start_proxy(int proxy_type)
char buf[512];
snprintf(buf, sizeof(buf), "%s --port %d%s", cmd, port, auth);
fprintf(stderr, "%s starting proxy on port %d (%s %s)...\n", aux::time_now_string(), port, type, auth);
fprintf(stderr, "%s starting proxy on port %d (%s %s)...\n", time_now_string(), port, type, auth);
fprintf(stderr, "%s\n", buf);
pid_type r = async_run(buf);
if (r == 0) exit(1);
proxy_t t = { r, proxy_type };
running_proxies.insert(std::make_pair(port, t));
fprintf(stderr, "%s launched\n", aux::time_now_string());
fprintf(stderr, "%s launched\n", time_now_string());
test_sleep(500);
return port;
}
@ -823,7 +825,7 @@ setup_transfer(lt::session* ses1, lt::session* ses2, lt::session* ses3
port = ses2->listen_port();
fprintf(stderr, "%s: ses1: connecting peer port: %d\n"
, aux::time_now_string(), port);
, time_now_string(), port);
tor1.connect_peer(tcp::endpoint(address::from_string("127.0.0.1", ec)
, port));
@ -869,13 +871,13 @@ int start_web_server(bool ssl, bool chunked_encoding, bool keepalive)
snprintf(buf, sizeof(buf), "python ../web_server.py %d %d %d %d"
, port, chunked_encoding , ssl, keepalive);
fprintf(stderr, "%s starting web_server on port %d...\n", aux::time_now_string(), port);
fprintf(stderr, "%s starting web_server on port %d...\n", time_now_string(), port);
fprintf(stderr, "%s\n", buf);
pid_type r = async_run(buf);
if (r == 0) exit(1);
web_server_pid = r;
fprintf(stderr, "%s launched\n", aux::time_now_string());
fprintf(stderr, "%s launched\n", time_now_string());
test_sleep(500);
return port;
}

View File

@ -32,6 +32,8 @@ POSSIBILITY OF SUCH DAMAGE.
#include "test.hpp"
#include "setup_transfer.hpp"
#include "test_utils.hpp"
#include "libtorrent/socket.hpp"
#include "libtorrent/io.hpp"
#include "libtorrent/alloca.hpp"
@ -63,7 +65,7 @@ void log(char const* fmt, ...)
va_end(v);
fprintf(stderr, "\x1b[1m\x1b[36m%s: %s\x1b[0m\n"
, aux::time_now_string(), buf);
, time_now_string(), buf);
}
void print_session_log(lt::session& ses)

View File

@ -31,11 +31,13 @@ POSSIBILITY OF SUCH DAMAGE.
*/
#include "test.hpp"
#include "setup_transfer.hpp"
#include "test_utils.hpp"
#include "libtorrent/socket.hpp"
#include "libtorrent/socket_io.hpp" // print_endpoint
#include "libtorrent/http_connection.hpp"
#include "libtorrent/resolver.hpp"
#include "setup_transfer.hpp"
#include <fstream>
#include <iostream>
@ -55,12 +57,12 @@ char data_buffer[4000];
void print_http_header(http_parser const& p)
{
std::cerr << aux::time_now_string() << " < " << p.status_code() << " " << p.message() << std::endl;
std::cerr << time_now_string() << " < " << p.status_code() << " " << p.message() << std::endl;
for (std::multimap<std::string, std::string>::const_iterator i
= p.headers().begin(), end(p.headers().end()); i != end; ++i)
{
std::cerr << aux::time_now_string() << " < " << i->first << ": " << i->second << std::endl;
std::cerr << time_now_string() << " < " << i->first << ": " << i->second << std::endl;
}
}
@ -69,7 +71,7 @@ void http_connect_handler(http_connection& c)
++connect_handler_called;
TEST_CHECK(c.socket().is_open());
error_code ec;
std::cerr << aux::time_now_string() << " connected to: " << print_endpoint(c.socket().remote_endpoint(ec))
std::cerr << time_now_string() << " connected to: " << print_endpoint(c.socket().remote_endpoint(ec))
<< std::endl;
// 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));
@ -111,7 +113,7 @@ void run_test(std::string const& url, int size, int status, int connected
std::cerr << " ===== TESTING: " << url << " =====" << std::endl;
std::cerr << aux::time_now_string()
std::cerr << time_now_string()
<< " expecting: size: " << size
<< " status: " << status
<< " connected: " << connected
@ -124,14 +126,14 @@ void run_test(std::string const& url, int size, int status, int connected
ios.reset();
error_code e;
ios.run(e);
if (e) std::cerr << aux::time_now_string() << " run failed: " << e.message() << std::endl;
if (e) std::cerr << time_now_string() << " run failed: " << e.message() << std::endl;
std::cerr << aux::time_now_string() << " connect_handler_called: " << connect_handler_called << std::endl;
std::cerr << aux::time_now_string() << " handler_called: " << handler_called << std::endl;
std::cerr << aux::time_now_string() << " status: " << http_status << std::endl;
std::cerr << aux::time_now_string() << " size: " << data_size << std::endl;
std::cerr << aux::time_now_string() << " expected-size: " << size << std::endl;
std::cerr << aux::time_now_string() << " error_code: " << g_error_code.message() << std::endl;
std::cerr << time_now_string() << " connect_handler_called: " << connect_handler_called << std::endl;
std::cerr << time_now_string() << " handler_called: " << handler_called << std::endl;
std::cerr << time_now_string() << " status: " << http_status << std::endl;
std::cerr << time_now_string() << " size: " << data_size << std::endl;
std::cerr << time_now_string() << " expected-size: " << size << std::endl;
std::cerr << time_now_string() << " error_code: " << g_error_code.message() << std::endl;
TEST_CHECK(connect_handler_called == connected);
TEST_CHECK(handler_called == 1);
TEST_CHECK(data_size == size || size == -1);

View File

@ -30,16 +30,18 @@ POSSIBILITY OF SUCH DAMAGE.
*/
#include "test.hpp"
#include "setup_transfer.hpp"
#include "test_utils.hpp"
#include "test_utils.hpp"
#include "libtorrent/session.hpp"
#include "libtorrent/hasher.hpp"
#include "libtorrent/thread.hpp"
#include <boost/tuple/tuple.hpp>
#include "test.hpp"
#include "setup_transfer.hpp"
#include "libtorrent/extensions/metadata_transfer.hpp"
#include "libtorrent/extensions/ut_metadata.hpp"
#include <iostream>
#include <boost/tuple/tuple.hpp>
using boost::tuples::ignore;
@ -137,7 +139,7 @@ void test_transfer(int flags
error_code ec;
int port = seed->listen_port();
fprintf(stderr, "%s: downloader: connecting peer port: %d\n"
, aux::time_now_string(), port);
, time_now_string(), port);
tor2.connect_peer(tcp::endpoint(address::from_string("127.0.0.1", ec)
, port));
}
@ -146,7 +148,7 @@ void test_transfer(int flags
error_code ec;
int port = downloader->listen_port();
fprintf(stderr, "%s: seed: connecting peer port: %d\n"
, aux::time_now_string(), port);
, time_now_string(), port);
tor1.connect_peer(tcp::endpoint(address::from_string("127.0.0.1", ec)
, port));
}
@ -171,7 +173,7 @@ void test_transfer(int flags
if (flags & upload_only) goto done;
std::cerr << "waiting for transfer to complete\n";
fprintf(stderr, "waiting for transfer to complete\n");
for (int i = 0; i < timeout * 10; ++i)
{
@ -187,7 +189,7 @@ void test_transfer(int flags
}
TEST_CHECK(tor2.status().is_seeding);
if (tor2.status().is_seeding) std::cerr << "done\n";
if (tor2.status().is_seeding) fprintf(stderr, "done\n");
done:

View File

@ -35,6 +35,8 @@ POSSIBILITY OF SUCH DAMAGE.
#include "dht_server.hpp"
#include "peer_server.hpp"
#include "udp_tracker.hpp"
#include "test_utils.hpp"
#include "libtorrent/alert.hpp"
#include "libtorrent/random.hpp"
#include "libtorrent/alert_types.hpp"
@ -229,7 +231,7 @@ session_proxy test_proxy(settings_pack::proxy_type_t proxy_type, int flags)
if (flags & expect_http_reject)
TEST_CHECK(std::find(rejected_trackers.begin(), rejected_trackers.end(), http_tracker_url) != rejected_trackers.end());
fprintf(stderr, "%s: ~session\n", aux::time_now_string());
fprintf(stderr, "%s: ~session\n", time_now_string());
session_proxy pr = s->abort();
delete s;

View File

@ -30,6 +30,10 @@ POSSIBILITY OF SUCH DAMAGE.
*/
#include "test.hpp"
#include "setup_transfer.hpp"
#include "test_utils.hpp"
#include "libtorrent/storage.hpp"
#include "libtorrent/file_pool.hpp"
#include "libtorrent/hasher.hpp"
@ -42,8 +46,6 @@ POSSIBILITY OF SUCH DAMAGE.
#include <boost/make_shared.hpp>
#include <boost/utility.hpp>
#include "test.hpp"
#include "setup_transfer.hpp"
#include <iostream>
#include <fstream>
@ -62,33 +64,33 @@ char* piece3 = page_aligned_allocator::malloc(piece_size);
void signal_bool(bool* b, char const* string)
{
*b = true;
std::cerr << aux::time_now_string() << " " << string << std::endl;
std::cerr << time_now_string() << " " << string << std::endl;
}
void on_read_piece(int ret, disk_io_job const& j, char const* data, int size)
{
std::cerr << aux::time_now_string() << " on_read_piece piece: " << j.piece << std::endl;
std::cerr << time_now_string() << " on_read_piece piece: " << j.piece << std::endl;
TEST_EQUAL(ret, size);
if (ret > 0) TEST_CHECK(std::equal(j.buffer, j.buffer + ret, data));
}
void on_check_resume_data(disk_io_job const* j, bool* done)
{
std::cerr << aux::time_now_string() << " on_check_resume_data ret: " << j->ret;
std::cerr << time_now_string() << " on_check_resume_data ret: " << j->ret;
switch (j->ret)
{
case piece_manager::no_error:
std::cerr << aux::time_now_string() << " success" << std::endl;
std::cerr << time_now_string() << " success" << std::endl;
break;
case piece_manager::fatal_disk_error:
std::cerr << aux::time_now_string() << " disk error: " << j->error.ec.message()
std::cerr << time_now_string() << " disk error: " << j->error.ec.message()
<< " file: " << j->error.file << std::endl;
break;
case piece_manager::need_full_check:
std::cerr << aux::time_now_string() << " need full check" << std::endl;
std::cerr << time_now_string() << " need full check" << std::endl;
break;
case piece_manager::disk_check_aborted:
std::cerr << aux::time_now_string() << " aborted" << std::endl;
std::cerr << time_now_string() << " aborted" << std::endl;
break;
}
std::cerr << std::endl;
@ -98,7 +100,7 @@ void on_check_resume_data(disk_io_job const* j, bool* done)
void print_error(char const* call, int ret, storage_error const& ec)
{
fprintf(stderr, "%s: %s() returned: %d error: \"%s\" in file: %d operation: %d\n"
, aux::time_now_string(), call, ret, ec.ec.message().c_str(), ec.file, ec.operation);
, time_now_string(), call, ret, ec.ec.message().c_str(), ec.file, ec.operation);
}
void run_until(io_service& ios, bool const& done)
@ -113,7 +115,7 @@ void run_until(io_service& ios, bool const& done)
std::cerr << "run_one: " << ec.message().c_str() << std::endl;
return;
}
std::cerr << aux::time_now_string() << " done: " << done << std::endl;
std::cerr << time_now_string() << " done: " << done << std::endl;
}
}

View File

@ -38,11 +38,14 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/thread.hpp"
#include "libtorrent/time.hpp"
#include "libtorrent/file.hpp"
#include <boost/tuple/tuple.hpp>
#include <boost/bind.hpp>
#include "test.hpp"
#include "setup_transfer.hpp"
#include "test_utils.hpp"
#include <boost/tuple/tuple.hpp>
#include <boost/bind.hpp>
#include <fstream>
#include <iostream>
@ -306,7 +309,7 @@ void test_transfer(int proxy_type, settings_pack const& sett
// at this point we probably disconnected the seed
// so we need to reconnect as well
fprintf(stderr, "%s: reconnecting peer\n", aux::time_now_string());
fprintf(stderr, "%s: reconnecting peer\n", time_now_string());
error_code ec;
tor2.connect_peer(tcp::endpoint(address::from_string("127.0.0.1", ec)
, ses1.listen_port()));
@ -314,7 +317,7 @@ void test_transfer(int proxy_type, settings_pack const& sett
TEST_CHECK(tor2.status().is_finished == false);
fprintf(stderr, "disconnects: %d\n", peer_disconnects);
TEST_CHECK(peer_disconnects >= 2);
fprintf(stderr, "%s: discovered disk full mode. Raise limit and disable upload-mode\n", aux::time_now_string());
fprintf(stderr, "%s: discovered disk full mode. Raise limit and disable upload-mode\n", time_now_string());
peer_disconnects = 0;
continue;
}

55
test/test_utils.cpp Normal file
View File

@ -0,0 +1,55 @@
/*
Copyright (c) 2015, 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_utils.hpp"
#include "libtorrent/time.hpp"
namespace libtorrent
{
char const* time_now_string()
{
static const time_point start = clock_type::now();
static char ret[200];
int t = total_milliseconds(clock_type::now() - start);
int h = t / 1000 / 60 / 60;
t -= h * 60 * 60 * 1000;
int m = t / 1000 / 60;
t -= m * 60 * 1000;
int s = t / 1000;
t -= s * 1000;
int ms = t;
snprintf(ret, sizeof(ret), "%02d:%02d:%02d.%03d", h, m, s, ms);
return ret;
}
}

44
test/test_utils.hpp Normal file
View File

@ -0,0 +1,44 @@
/*
Copyright (c) 2015, 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.
*/
#ifndef TEST_UTILS_HPP
#define TEST_UTILS_HPP
#include "test.hpp"
namespace libtorrent
{
EXPORT char const* time_now_string();
}
#endif

View File

@ -41,6 +41,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/io.hpp"
#include "libtorrent/aux_/time.hpp"
#include "udp_tracker.hpp"
#include "test_utils.hpp"
#include <boost/detail/atomic_count.hpp>
#include <boost/shared_ptr.hpp>
@ -66,17 +67,17 @@ struct udp_tracker
{
if (ec)
{
fprintf(stderr, "%s: UDP tracker, read failed: %s\n", aux::time_now_string(), ec.message().c_str());
fprintf(stderr, "%s: UDP tracker, read failed: %s\n", time_now_string(), ec.message().c_str());
return;
}
if (bytes_transferred < 16)
{
fprintf(stderr, "%s: UDP message too short (from: %s)\n", aux::time_now_string(), print_endpoint(*from).c_str());
fprintf(stderr, "%s: UDP message too short (from: %s)\n", time_now_string(), print_endpoint(*from).c_str());
return;
}
fprintf(stderr, "%s: UDP message %d bytes\n", aux::time_now_string(), int(bytes_transferred));
fprintf(stderr, "%s: UDP message %d bytes\n", time_now_string(), int(bytes_transferred));
char* ptr = buffer;
detail::read_uint64(ptr);
@ -89,7 +90,7 @@ struct udp_tracker
{
case 0: // connect
fprintf(stderr, "%s: UDP connect from %s\n", aux::time_now_string()
fprintf(stderr, "%s: UDP connect from %s\n", time_now_string()
, print_endpoint(*from).c_str());
ptr = buffer;
detail::write_uint32(0, ptr); // action = connect
@ -97,15 +98,15 @@ struct udp_tracker
detail::write_uint64(10, ptr); // connection_id
m_socket.send_to(asio::buffer(buffer, 16), *from, 0, e);
if (e) fprintf(stderr, "%s: UDP send_to failed. ERROR: %s\n"
, aux::time_now_string(), e.message().c_str());
, time_now_string(), e.message().c_str());
else fprintf(stderr, "%s: UDP sent response to: %s\n"
, aux::time_now_string(), print_endpoint(*from).c_str());
, time_now_string(), print_endpoint(*from).c_str());
break;
case 1: // announce
++m_udp_announces;
fprintf(stderr, "%s: UDP announce [%d]\n", aux::time_now_string()
fprintf(stderr, "%s: UDP announce [%d]\n", time_now_string()
, int(m_udp_announces));
ptr = buffer;
detail::write_uint32(1, ptr); // action = announce
@ -116,16 +117,16 @@ struct udp_tracker
// 0 peers
m_socket.send_to(asio::buffer(buffer, 20), *from, 0, e);
if (e) fprintf(stderr, "%s: UDP send_to failed. ERROR: %s\n"
, aux::time_now_string(), e.message().c_str());
, time_now_string(), e.message().c_str());
else fprintf(stderr, "%s: UDP sent response to: %s\n"
, aux::time_now_string(), print_endpoint(*from).c_str());
, time_now_string(), print_endpoint(*from).c_str());
break;
case 2:
// ignore scrapes
fprintf(stderr, "%s: UDP scrape (ignored)\n", aux::time_now_string());
fprintf(stderr, "%s: UDP scrape (ignored)\n", time_now_string());
break;
default:
fprintf(stderr, "%s: UDP unknown message: %d\n", aux::time_now_string()
fprintf(stderr, "%s: UDP unknown message: %d\n", time_now_string()
, action);
break;
}
@ -161,7 +162,7 @@ struct udp_tracker
return;
}
fprintf(stderr, "%s: UDP tracker initialized on port %d\n", aux::time_now_string(), m_port);
fprintf(stderr, "%s: UDP tracker initialized on port %d\n", time_now_string(), m_port);
m_thread.reset(new thread(boost::bind(&udp_tracker::thread_fun, this)));
}
@ -223,8 +224,8 @@ int num_udp_announces()
void stop_udp_tracker()
{
fprintf(stderr, "%s: UDP stop_udp_tracker()\n", aux::time_now_string());
fprintf(stderr, "%s: UDP stop_udp_tracker()\n", time_now_string());
g_udp_tracker.reset();
fprintf(stderr, "%s: UDP stop_udp_tracker() done\n", aux::time_now_string());
fprintf(stderr, "%s: UDP stop_udp_tracker() done\n", time_now_string());
}