more iostream fixes

This commit is contained in:
Arvid Norberg 2009-04-04 21:44:04 +00:00
parent b5bb51c6af
commit 2b671bc820
9 changed files with 67 additions and 24 deletions

View File

@ -59,7 +59,6 @@ POSSIBILITY OF SUCH DAMAGE.
*/
#include <iosfwd>
#include <map>
#include <list>
#include <string>
@ -70,6 +69,10 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/assert.hpp"
#include "libtorrent/error_code.hpp"
#if TORRENT_USE_IOSTREAM
#include <iosfwd>
#endif
namespace libtorrent
{
@ -171,7 +174,9 @@ namespace libtorrent
entry* find_key(std::string const& key);
entry const* find_key(std::string const& key) const;
#if TORRENT_USE_IOSTREAM
void print(std::ostream& os, int indent = 0) const;
#endif
protected:
@ -219,11 +224,13 @@ namespace libtorrent
#endif
};
#if TORRENT_USE_IOSTREAM
inline std::ostream& operator<<(std::ostream& os, const entry& e)
{
e.print(os, 0);
return os;
}
#endif
#ifndef BOOST_NO_EXCEPTIONS
inline void throw_type_error()

View File

@ -175,7 +175,7 @@ namespace libtorrent
{
char hex[40];
is.read(hex, 40);
if (from_hex(hex, 40, (char*)&peer[0]) == -1)
if (!from_hex(hex, 40, (char*)&peer[0]))
is.setstate(std::ios_base::failbit);
return is;
}

View File

@ -1841,7 +1841,7 @@ namespace libtorrent
i.begin += msg.size();
TORRENT_ASSERT(i.begin == i.end);
#ifdef TORRENT_VERBOSE_LOGGING
#if defined TORRENT_VERBOSE_LOGGING && TORRENT_USE_IOSTREAM
std::stringstream ext;
handshake.print(ext);
(*m_logger) << "==> EXTENDED HANDSHAKE: \n" << ext.str();

View File

@ -425,6 +425,7 @@ namespace libtorrent
int t = hex_to_int(*in);
if (t == -1) return false;
*out = t << 4;
++in;
t = hex_to_int(*in);
if (t == -1) return false;
*out |= t & 15;

View File

@ -279,7 +279,9 @@ setup_transfer(session* ses1, session* ses2, session* ses3
remove_all("./tmp2" + suffix + "/temporary");
remove_all("./tmp3" + suffix + "/temporary");
}
std::cerr << "generated torrent: " << t->info_hash() << std::endl;
char ih_hex[41];
to_hex((char const*)&t->info_hash()[0], 20, ih_hex);
std::cerr << "generated torrent: " << ih_hex << std::endl;
}
else
{

View File

@ -109,7 +109,9 @@ int test_main()
lazy_entry e;
int ret = lazy_bdecode(b, b + sizeof(b)-1, e);
TORRENT_ASSERT(ret == 0);
#if TORRENT_USE_IOSTREAM
std::cout << e << std::endl;
#endif
std::pair<const char*, int> section = e.data_section();
TORRENT_ASSERT(std::memcmp(b, section.first, section.second) == 0);
TORRENT_ASSERT(section.second == sizeof(b) - 1);
@ -122,7 +124,9 @@ int test_main()
lazy_entry e;
int ret = lazy_bdecode(b, b + sizeof(b)-1, e);
TORRENT_ASSERT(ret == 0);
#if TORRENT_USE_IOSTREAM
std::cout << e << std::endl;
#endif
std::pair<const char*, int> section = e.data_section();
TORRENT_ASSERT(std::memcmp(b, section.first, section.second) == 0);
TORRENT_ASSERT(section.second == sizeof(b) - 1);
@ -136,7 +140,9 @@ int test_main()
lazy_entry e;
int ret = lazy_bdecode(b, b + sizeof(b)-1, e);
TORRENT_ASSERT(ret == 0);
#if TORRENT_USE_IOSTREAM
std::cout << e << std::endl;
#endif
std::pair<const char*, int> section = e.data_section();
TORRENT_ASSERT(std::memcmp(b, section.first, section.second) == 0);
TORRENT_ASSERT(section.second == sizeof(b) - 1);
@ -157,7 +163,9 @@ int test_main()
lazy_entry e;
int ret = lazy_bdecode(b, b + sizeof(b)-1, e);
TORRENT_ASSERT(ret == 0);
#if TORRENT_USE_IOSTREAM
std::cout << e << std::endl;
#endif
std::pair<const char*, int> section = e.data_section();
TORRENT_ASSERT(std::memcmp(b, section.first, section.second) == 0);
TORRENT_ASSERT(section.second == sizeof(b) - 1);

View File

@ -69,7 +69,8 @@ int test_main()
for (int i = 0; i < repeat_count[test]; ++i)
h.update(test_array[test], std::strlen(test_array[test]));
sha1_hash result = boost::lexical_cast<sha1_hash>(result_array[test]);
sha1_hash result;
from_hex(result_array[test], 40, (char*)&result[0]);
TEST_CHECK(result == h.final());
}

View File

@ -54,6 +54,13 @@ using namespace libtorrent;
using namespace boost::tuples;
using boost::bind;
sha1_hash to_hash(char const* s)
{
sha1_hash ret;
from_hex(s, 40, (char*)&ret[0]);
return ret;
}
tuple<int, int, bool> feed_bytes(http_parser& parser, char const* str)
{
tuple<int, int, bool> ret(0, 0, false);
@ -347,6 +354,15 @@ int test_main()
{
using namespace libtorrent;
// test to/from hex conversion
char const* str = "0123456789012345678901234567890123456789";
char bin[20];
TEST_CHECK(from_hex(str, 40, bin));
char hex[41];
to_hex(bin, 20, hex);
TEST_CHECK(strcmp(hex, str) == 0);
// test itoa
TEST_CHECK(to_string(345).elems == std::string("345"));
@ -500,7 +516,7 @@ int test_main()
TEST_CHECK(atoi(parser.header("port").c_str()) == 6881);
TEST_CHECK(parser.header("infohash") == "12345678901234567890");
TEST_CHECK(!parser.finished());
TEST_CHECK(parser.finished());
parser.reset();
TEST_CHECK(!parser.finished());
@ -653,12 +669,12 @@ int test_main()
// test kademlia routing table
dht_settings s;
node_id id = boost::lexical_cast<sha1_hash>("6123456789abcdef01232456789abcdef0123456");
node_id id = to_hash("6123456789abcdef01232456789abcdef0123456");
dht::routing_table table(id, 10, s);
table.node_seen(id, udp::endpoint(address_v4::any(), rand()));
node_id tmp;
node_id diff = boost::lexical_cast<sha1_hash>("00001f7459456a9453f8719b09547c11d5f34064");
node_id diff = to_hash("00001f7459456a9453f8719b09547c11d5f34064");
std::vector<node_entry> nodes;
for (int i = 0; i < 10000; ++i)
{
@ -736,8 +752,8 @@ int test_main()
TEST_CHECK(!(h1 < h2));
TEST_CHECK(h1.is_all_zeros());
h1 = boost::lexical_cast<sha1_hash>("0123456789012345678901234567890123456789");
h2 = boost::lexical_cast<sha1_hash>("0113456789012345678901234567890123456789");
h1 = to_hash("0123456789012345678901234567890123456789");
h2 = to_hash("0113456789012345678901234567890123456789");
TEST_CHECK(h2 < h1);
TEST_CHECK(h2 == h2);
@ -745,34 +761,36 @@ int test_main()
h2.clear();
TEST_CHECK(h2.is_all_zeros());
h2 = boost::lexical_cast<sha1_hash>("ffffffffff0000000000ffffffffff0000000000");
h1 = boost::lexical_cast<sha1_hash>("fffff00000fffff00000fffff00000fffff00000");
h2 = to_hash("ffffffffff0000000000ffffffffff0000000000");
h1 = to_hash("fffff00000fffff00000fffff00000fffff00000");
h1 &= h2;
TEST_CHECK(h1 == boost::lexical_cast<sha1_hash>("fffff000000000000000fffff000000000000000"));
TEST_CHECK(h1 == to_hash("fffff000000000000000fffff000000000000000"));
h2 = boost::lexical_cast<sha1_hash>("ffffffffff0000000000ffffffffff0000000000");
h1 = boost::lexical_cast<sha1_hash>("fffff00000fffff00000fffff00000fffff00000");
h2 = to_hash("ffffffffff0000000000ffffffffff0000000000");
h1 = to_hash("fffff00000fffff00000fffff00000fffff00000");
h1 |= h2;
TEST_CHECK(h1 == boost::lexical_cast<sha1_hash>("fffffffffffffff00000fffffffffffffff00000"));
TEST_CHECK(h1 == to_hash("fffffffffffffff00000fffffffffffffff00000"));
h2 = boost::lexical_cast<sha1_hash>("0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f");
h2 = to_hash("0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f");
h1 ^= h2;
#if TORRENT_USE_IOSTREAM
std::cerr << h1 << std::endl;
TEST_CHECK(h1 == boost::lexical_cast<sha1_hash>("f0f0f0f0f0f0f0ff0f0ff0f0f0f0f0f0f0ff0f0f"));
#endif
TEST_CHECK(h1 == to_hash("f0f0f0f0f0f0f0ff0f0ff0f0f0f0f0f0f0ff0f0f"));
TEST_CHECK(h1 != h2);
h2 = sha1_hash(" ");
TEST_CHECK(h2 == boost::lexical_cast<sha1_hash>("2020202020202020202020202020202020202020"));
TEST_CHECK(h2 == to_hash("2020202020202020202020202020202020202020"));
// CIDR distance test
h1 = boost::lexical_cast<sha1_hash>("0123456789abcdef01232456789abcdef0123456");
h2 = boost::lexical_cast<sha1_hash>("0123456789abcdef01232456789abcdef0123456");
h1 = to_hash("0123456789abcdef01232456789abcdef0123456");
h2 = to_hash("0123456789abcdef01232456789abcdef0123456");
TEST_CHECK(common_bits(&h1[0], &h2[0], 20) == 160);
h2 = boost::lexical_cast<sha1_hash>("0120456789abcdef01232456789abcdef0123456");
h2 = to_hash("0120456789abcdef01232456789abcdef0123456");
TEST_CHECK(common_bits(&h1[0], &h2[0], 20) == 14);
h2 = boost::lexical_cast<sha1_hash>("012f456789abcdef01232456789abcdef0123456");
h2 = to_hash("012f456789abcdef01232456789abcdef0123456");
TEST_CHECK(common_bits(&h1[0], &h2[0], 20) == 12);
h2 = boost::lexical_cast<sha1_hash>("0123456789abcdef11232456789abcdef0123456");
h2 = to_hash("0123456789abcdef11232456789abcdef0123456");
TEST_CHECK(common_bits(&h1[0], &h2[0], 20) == 16 * 4 + 3);

View File

@ -512,7 +512,9 @@ void test_fastresume(path const& test_path)
ses.remove_torrent(h, session::delete_files);
}
TEST_CHECK(!exists(test_path / "tmp1/temporary"));
#if TORRENT_USE_IOSTREAM
resume.print(std::cout);
#endif
// make sure the fast resume check fails! since we removed the file
{
@ -583,7 +585,9 @@ void test_rename_file_in_fastresume(path const& test_path)
TEST_CHECK(!exists(test_path / "tmp2/temporary"));
TEST_CHECK(exists(test_path / "tmp2/testing_renamed_files"));
TEST_CHECK(resume.dict().find("mapped_files") != resume.dict().end());
#if TORRENT_USE_IOSTREAM
resume.print(std::cout);
#endif
// make sure the fast resume check succeeds, even though we renamed the file
{
@ -604,7 +608,9 @@ void test_rename_file_in_fastresume(path const& test_path)
ses.remove_torrent(h);
}
TEST_CHECK(resume.dict().find("mapped_files") != resume.dict().end());
#if TORRENT_USE_IOSTREAM
resume.print(std::cout);
#endif
remove_all(test_path / "tmp2");
}