fixes bug where priorities where lost when force-rechecking.
[libtorrent.git] / test / test_bencoding.cpp
blob4a542d4d877764c41f51740e28bb9eaeb0c6e565
1 /*
3 Copyright (c) 2008, Arvid Norberg
4 All rights reserved.
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
10 * Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in
14 the documentation and/or other materials provided with the distribution.
15 * Neither the name of the author nor the names of its
16 contributors may be used to endorse or promote products derived
17 from this software without specific prior written permission.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 POSSIBILITY OF SUCH DAMAGE.
33 #include "libtorrent/bencode.hpp"
34 #include "libtorrent/lazy_entry.hpp"
35 #include <boost/lexical_cast.hpp>
36 #include <iostream>
38 #include "test.hpp"
40 using namespace libtorrent;
42 // test vectors from bittorrent protocol description
43 // http://www.bittorrent.com/protocol.html
45 std::string encode(entry const& e)
47 std::string ret;
48 bencode(std::back_inserter(ret), e);
49 return ret;
52 entry decode(std::string const& str)
54 return bdecode(str.begin(), str.end());
57 int test_main()
59 using namespace libtorrent;
61 // ** strings **
63 entry e("spam");
64 TEST_CHECK(encode(e) == "4:spam");
65 TEST_CHECK(decode(encode(e)) == e);
68 // ** integers **
70 entry e(3);
71 TEST_CHECK(encode(e) == "i3e");
72 TEST_CHECK(decode(encode(e)) == e);
76 entry e(-3);
77 TEST_CHECK(encode(e) == "i-3e");
78 TEST_CHECK(decode(encode(e)) == e);
82 entry e(int(0));
83 TEST_CHECK(encode(e) == "i0e");
84 TEST_CHECK(decode(encode(e)) == e);
87 // ** lists **
89 entry::list_type l;
90 l.push_back(entry("spam"));
91 l.push_back(entry("eggs"));
92 entry e(l);
93 TEST_CHECK(encode(e) == "l4:spam4:eggse");
94 TEST_CHECK(decode(encode(e)) == e);
97 // ** dictionaries **
99 entry e(entry::dictionary_t);
100 e["spam"] = entry("eggs");
101 e["cow"] = entry("moo");
102 TEST_CHECK(encode(e) == "d3:cow3:moo4:spam4:eggse");
103 TEST_CHECK(decode(encode(e)) == e);
107 char b[] = "i12453e";
108 lazy_entry e;
109 int ret = lazy_bdecode(b, b + sizeof(b)-1, e);
110 TORRENT_ASSERT(ret == 0);
111 std::cout << e << std::endl;
112 std::pair<const char*, int> section = e.data_section();
113 TORRENT_ASSERT(memcmp(b, section.first, section.second) == 0);
114 TORRENT_ASSERT(section.second == sizeof(b) - 1);
115 TORRENT_ASSERT(e.type() == lazy_entry::int_t);
116 TORRENT_ASSERT(e.int_value() == 12453);
120 char b[] = "26:abcdefghijklmnopqrstuvwxyz";
121 lazy_entry e;
122 int ret = lazy_bdecode(b, b + sizeof(b)-1, e);
123 TORRENT_ASSERT(ret == 0);
124 std::cout << e << std::endl;
125 std::pair<const char*, int> section = e.data_section();
126 TORRENT_ASSERT(memcmp(b, section.first, section.second) == 0);
127 TORRENT_ASSERT(section.second == sizeof(b) - 1);
128 TORRENT_ASSERT(e.type() == lazy_entry::string_t);
129 TORRENT_ASSERT(e.string_value() == std::string("abcdefghijklmnopqrstuvwxyz"));
130 TORRENT_ASSERT(e.string_length() == 26);
134 char b[] = "li12453e3:aaae";
135 lazy_entry e;
136 int ret = lazy_bdecode(b, b + sizeof(b)-1, e);
137 TORRENT_ASSERT(ret == 0);
138 std::cout << e << std::endl;
139 std::pair<const char*, int> section = e.data_section();
140 TORRENT_ASSERT(memcmp(b, section.first, section.second) == 0);
141 TORRENT_ASSERT(section.second == sizeof(b) - 1);
142 TORRENT_ASSERT(e.type() == lazy_entry::list_t);
143 TORRENT_ASSERT(e.list_size() == 2);
144 TORRENT_ASSERT(e.list_at(0)->type() == lazy_entry::int_t);
145 TORRENT_ASSERT(e.list_at(1)->type() == lazy_entry::string_t);
146 TORRENT_ASSERT(e.list_at(0)->int_value() == 12453);
147 TORRENT_ASSERT(e.list_at(1)->string_value() == std::string("aaa"));
148 TORRENT_ASSERT(e.list_at(1)->string_length() == 3);
149 section = e.list_at(1)->data_section();
150 TORRENT_ASSERT(memcmp("3:aaa", section.first, section.second) == 0);
151 TORRENT_ASSERT(section.second == 5);
155 char b[] = "d1:ai12453e1:b3:aaa1:c3:bbb1:X10:0123456789e";
156 lazy_entry e;
157 int ret = lazy_bdecode(b, b + sizeof(b)-1, e);
158 TORRENT_ASSERT(ret == 0);
159 std::cout << e << std::endl;
160 std::pair<const char*, int> section = e.data_section();
161 TORRENT_ASSERT(memcmp(b, section.first, section.second) == 0);
162 TORRENT_ASSERT(section.second == sizeof(b) - 1);
163 TORRENT_ASSERT(e.type() == lazy_entry::dict_t);
164 TORRENT_ASSERT(e.dict_size() == 4);
165 TORRENT_ASSERT(e.dict_find("a")->type() == lazy_entry::int_t);
166 TORRENT_ASSERT(e.dict_find("a")->int_value() == 12453);
167 TORRENT_ASSERT(e.dict_find("b")->type() == lazy_entry::string_t);
168 TORRENT_ASSERT(e.dict_find("b")->string_value() == std::string("aaa"));
169 TORRENT_ASSERT(e.dict_find("b")->string_length() == 3);
170 TORRENT_ASSERT(e.dict_find("c")->type() == lazy_entry::string_t);
171 TORRENT_ASSERT(e.dict_find("c")->string_value() == std::string("bbb"));
172 TORRENT_ASSERT(e.dict_find("c")->string_length() == 3);
173 TORRENT_ASSERT(e.dict_find_string_value("X") == "0123456789");
175 return 0;