added some precautionary checks in bdecoder
[libtorrent.git] / test / test_torrent.cpp
blobaaa66a520ccff5df93e6252ee826e323d4072d70
1 #include "libtorrent/session.hpp"
2 #include "libtorrent/session_settings.hpp"
3 #include "libtorrent/hasher.hpp"
4 #include "libtorrent/create_torrent.hpp"
5 #include <boost/thread.hpp>
6 #include <boost/tuple/tuple.hpp>
8 #include "test.hpp"
9 #include "setup_transfer.hpp"
11 int test_main()
13 using namespace libtorrent;
15 session ses(fingerprint("LT", 0, 1, 0, 0), std::make_pair(48130, 48140));
17 file_storage fs;
18 size_type file_size = 1 * 1024 * 1024 * 1024;
19 fs.add_file("test_torrent/tmp1", file_size);
20 fs.add_file("test_torrent/tmp2", file_size);
21 fs.add_file("test_torrent/tmp3", file_size);
22 libtorrent::create_torrent t(fs, 4 * 1024 * 1024);
23 t.add_tracker("http://non-existing.com/announce");
25 std::vector<char> piece(4 * 1024 * 1024);
26 for (int i = 0; i < int(piece.size()); ++i)
27 piece[i] = (i % 26) + 'A';
29 // calculate the hash for all pieces
30 sha1_hash ph = hasher(&piece[0], piece.size()).final();
31 int num = t.num_pieces();
32 for (int i = 0; i < num; ++i)
33 t.set_hash(i, ph);
35 std::vector<char> tmp;
36 std::back_insert_iterator<std::vector<char> > out(tmp);
37 bencode(out, t.generate());
38 boost::intrusive_ptr<torrent_info> info(new torrent_info(&tmp[0], tmp.size()));
40 add_torrent_params p;
41 p.ti = info;
42 p.save_path = ".";
43 torrent_handle h = ses.add_torrent(p);
45 test_sleep(500);
46 torrent_status st = h.status();
48 std::cout << "total_wanted: " << st.total_wanted << " : " << file_size * 3 << std::endl;
49 TEST_CHECK(st.total_wanted == file_size * 3);
50 std::cout << "total_wanted_done: " << st.total_wanted_done << " : 0" << std::endl;
51 TEST_CHECK(st.total_wanted_done == 0);
53 std::vector<int> prio(3, 1);
54 prio[0] = 0;
55 h.prioritize_files(prio);
57 test_sleep(500);
58 st = h.status();
60 std::cout << "total_wanted: " << st.total_wanted << " : " << file_size * 2 << std::endl;
61 TEST_CHECK(st.total_wanted == file_size * 2);
62 std::cout << "total_wanted_done: " << st.total_wanted_done << " : 0" << std::endl;
63 TEST_CHECK(st.total_wanted_done == 0);
65 prio[1] = 0;
66 h.prioritize_files(prio);
68 test_sleep(500);
69 st = h.status();
71 std::cout << "total_wanted: " << st.total_wanted << " : " << file_size << std::endl;
72 TEST_CHECK(st.total_wanted == file_size);
73 std::cout << "total_wanted_done: " << st.total_wanted_done << " : 0" << std::endl;
74 TEST_CHECK(st.total_wanted_done == 0);
75 return 0;