Rewritten bencode2::decode functions

pull/2/head
Tatsuhiro Tsujikawa 2011-11-05 12:01:57 +09:00
parent d305432ec0
commit d1d4903665
17 changed files with 212 additions and 147 deletions

View File

@ -76,7 +76,7 @@ SharedHandle<DHTMessage> DHTMessageReceiver::receiveMessage()
return SharedHandle<DHTMessage>(); return SharedHandle<DHTMessage>();
} }
bool isReply = false; bool isReply = false;
SharedHandle<ValueBase> decoded = bencode2::decode(data, length); SharedHandle<ValueBase> decoded = bencode2::decode(data, data+length);
const Dict* dict = downcast<Dict>(decoded); const Dict* dict = downcast<Dict>(decoded);
if(dict) { if(dict) {
const String* y = downcast<String>(dict->get(DHTMessage::Y)); const String* y = downcast<String>(dict->get(DHTMessage::Y));

View File

@ -220,7 +220,7 @@ DefaultBtAnnounce::processAnnounceResponse(const unsigned char* trackerResponse,
{ {
A2_LOG_DEBUG("Now processing tracker response."); A2_LOG_DEBUG("Now processing tracker response.");
SharedHandle<ValueBase> decodedValue = SharedHandle<ValueBase> decodedValue =
bencode2::decode(trackerResponse, trackerResponseLength); bencode2::decode(trackerResponse, trackerResponse+trackerResponseLength);
const Dict* dict = downcast<Dict>(decodedValue); const Dict* dict = downcast<Dict>(decodedValue);
if(!dict) { if(!dict) {
throw DL_ABORT_EX(MSG_NULL_TRACKER_RESPONSE); throw DL_ABORT_EX(MSG_NULL_TRACKER_RESPONSE);

View File

@ -101,7 +101,8 @@ DefaultExtensionMessageFactory::createMessage(const unsigned char* data, size_t
static_cast<unsigned long>(length))); static_cast<unsigned long>(length)));
} }
size_t end; size_t end;
SharedHandle<ValueBase> decoded = bencode2::decode(data+1, length-1, end); SharedHandle<ValueBase> decoded =
bencode2::decode(data+1, data+length, end);
const Dict* dict = downcast<Dict>(decoded); const Dict* dict = downcast<Dict>(decoded);
if(!dict) { if(!dict) {
throw DL_ABORT_EX("Bad ut_metadata: dictionary not found"); throw DL_ABORT_EX("Bad ut_metadata: dictionary not found");

View File

@ -435,7 +435,7 @@ unsigned int FtpConnection::receiveSizeResponse(uint64_t& size)
std::pair<unsigned int, std::string> response; std::pair<unsigned int, std::string> response;
if(bulkReceiveResponse(response)) { if(bulkReceiveResponse(response)) {
if(response.first == 213) { if(response.first == 213) {
std::pair<Scip, Scip> rp; std::pair<Sip, Sip> rp;
util::divide(rp, response.second.begin(), response.second.end(), ' '); util::divide(rp, response.second.begin(), response.second.end(), ' ');
size = util::parseULLInt(rp.second.first, rp.second.second); size = util::parseULLInt(rp.second.first, rp.second.second);
} }

View File

@ -173,7 +173,7 @@ HandshakeExtensionMessage::create(const unsigned char* data, size_t length)
HandshakeExtensionMessageHandle msg(new HandshakeExtensionMessage()); HandshakeExtensionMessageHandle msg(new HandshakeExtensionMessage());
A2_LOG_DEBUG(fmt("Creating HandshakeExtensionMessage from %s", A2_LOG_DEBUG(fmt("Creating HandshakeExtensionMessage from %s",
util::percentEncode(data, length).c_str())); util::percentEncode(data, length).c_str()));
SharedHandle<ValueBase> decoded = bencode2::decode(data+1, length-1); SharedHandle<ValueBase> decoded = bencode2::decode(data+1, data+length);
const Dict* dict = downcast<Dict>(decoded); const Dict* dict = downcast<Dict>(decoded);
if(!dict) { if(!dict) {
throw DL_ABORT_EX throw DL_ABORT_EX

View File

@ -227,10 +227,11 @@ bool HttpServer::authenticate()
} }
std::string userpass = Base64::decode(std::string(p.second.first, std::string userpass = Base64::decode(std::string(p.second.first,
p.second.second)); p.second.second));
util::divide(p, userpass.begin(), userpass.end(), ':'); std::pair<Sip, Sip> up;
return util::streq(p.first.first, p.first.second, util::divide(up, userpass.begin(), userpass.end(), ':');
return util::streq(up.first.first, up.first.second,
username_.begin(), username_.end()) && username_.begin(), username_.end()) &&
util::streq(p.second.first, p.second.second, util::streq(up.second.first, up.second.second,
password_.begin(), password_.end()); password_.begin(), password_.end());
} }

View File

@ -184,7 +184,7 @@ void OptionParser::parse(Option& option, std::istream& is) const
if(line.empty() || line[0] == '#') { if(line.empty() || line[0] == '#') {
continue; continue;
} }
std::pair<Scip, Scip> nv; std::pair<Sip, Sip> nv;
util::divide(nv, line.begin(), line.end(), '='); util::divide(nv, line.begin(), line.end(), '=');
if(nv.first.first == nv.first.second) { if(nv.first.first == nv.first.second) {
continue; continue;

View File

@ -124,7 +124,7 @@ ParameterizedStringParser::createLoop(const std::string& src, int& offset)
} }
loopStr.erase(colonIndex); loopStr.erase(colonIndex);
} }
std::pair<Scip, Scip> range; std::pair<Sip, Sip> range;
util::divide(range, loopStr.begin(), loopStr.end(), '-'); util::divide(range, loopStr.begin(), loopStr.end(), '-');
if(range.first.first == range.first.second || if(range.first.first == range.first.second ||
range.second.first == range.second.second) { range.second.first == range.second.second) {

View File

@ -190,7 +190,7 @@ UTPexExtensionMessage::create(const unsigned char* data, size_t len)
} }
UTPexExtensionMessageHandle msg(new UTPexExtensionMessage(*data)); UTPexExtensionMessageHandle msg(new UTPexExtensionMessage(*data));
SharedHandle<ValueBase> decoded = bencode2::decode(data+1, len-1); SharedHandle<ValueBase> decoded = bencode2::decode(data+1, data+len);
const Dict* dict = downcast<Dict>(decoded); const Dict* dict = downcast<Dict>(decoded);
if(dict) { if(dict) {
const String* added = downcast<String>(dict->get("added")); const String* added = downcast<String>(dict->get("added"));

View File

@ -40,90 +40,92 @@
#include "DlAbortEx.h" #include "DlAbortEx.h"
#include "error_code.h" #include "error_code.h"
#include "BufferedFile.h" #include "BufferedFile.h"
#include "util.h"
namespace aria2 { namespace aria2 {
namespace bencode2 { namespace bencode2 {
namespace { namespace {
SharedHandle<ValueBase> decodeiter(std::istream& ss, size_t depth); template<typename InputIterator>
std::pair<SharedHandle<ValueBase>, InputIterator>
decodeiter
(InputIterator first,
InputIterator last,
size_t depth);
} // namespace } // namespace
namespace { namespace {
void checkdelim(std::istream& ss, const char delim = ':') template<typename InputIterator>
std::pair<std::string, InputIterator>
decoderawstring(InputIterator first, InputIterator last)
{ {
char d; InputIterator i = first;
if(!(ss.get(d) && d == delim)) { int32_t len;
throw DL_ABORT_EX2 for(; i != last && *i != ':'; ++i);
(fmt("Bencode decoding failed: Delimiter '%c' not found.", if(i == last || i == first || !util::parseIntNoThrow(len, first, i) ||
delim), len < 0) {
error_code::BENCODE_PARSE_ERROR);
}
}
} // namespace
namespace {
std::string decoderawstring(std::istream& ss)
{
int length;
ss >> length;
if(!ss || length < 0) {
throw DL_ABORT_EX2("Bencode decoding failed:" throw DL_ABORT_EX2("Bencode decoding failed:"
" A positive integer expected but none found.", " A positive integer expected but none found.",
error_code::BENCODE_PARSE_ERROR); error_code::BENCODE_PARSE_ERROR);
} }
// TODO check length, it must be less than or equal to INT_MAX ++i;
checkdelim(ss); if(last-i < len) {
char* buf = new char[length];
ss.read(buf, length);
std::string str(&buf[0], &buf[length]);
delete [] buf;
if(ss.gcount() != static_cast<int>(length)) {
throw DL_ABORT_EX2 throw DL_ABORT_EX2
(fmt("Bencode decoding failed:" (fmt("Bencode decoding failed:"
" Expected %lu bytes of data, but only %ld read.", " Expected %d bytes of data, but only %d read.",
static_cast<unsigned long>(length), len, static_cast<int>(last-i)),
static_cast<long int>(ss.gcount())),
error_code::BENCODE_PARSE_ERROR); error_code::BENCODE_PARSE_ERROR);
} }
return str; return std::make_pair(std::string(i, i+len), i+len);
} }
} // namespace } // namespace
namespace { namespace {
SharedHandle<ValueBase> decodestring(std::istream& ss) template<typename InputIterator>
std::pair<SharedHandle<ValueBase>, InputIterator>
decodestring(InputIterator first, InputIterator last)
{ {
return String::g(decoderawstring(ss)); std::pair<std::string, InputIterator> r = decoderawstring(first, last);
return std::make_pair(String::g(r.first), r.second);
} }
} // namespace } // namespace
namespace { namespace {
SharedHandle<ValueBase> decodeinteger(std::istream& ss) template<typename InputIterator>
std::pair<SharedHandle<ValueBase>, InputIterator>
decodeinteger(InputIterator first, InputIterator last)
{ {
InputIterator i = first;
for(; i != last && *i != 'e'; ++i);
Integer::ValueType iv; Integer::ValueType iv;
ss >> iv; if(i == last || !util::parseLLIntNoThrow(iv, first, i)) {
if(!ss) {
throw DL_ABORT_EX2("Bencode decoding failed:" throw DL_ABORT_EX2("Bencode decoding failed:"
" Integer expected but none found", " Integer expected but none found",
error_code::BENCODE_PARSE_ERROR); error_code::BENCODE_PARSE_ERROR);
} }
checkdelim(ss, 'e'); return std::make_pair(Integer::g(iv), ++i);
return Integer::g(iv);
} }
} // namespace } // namespace
namespace { namespace {
SharedHandle<ValueBase> decodedict(std::istream& ss, size_t depth) template<typename InputIterator>
std::pair<SharedHandle<ValueBase>, InputIterator>
decodedict
(InputIterator first,
InputIterator last,
size_t depth)
{ {
SharedHandle<Dict> dict = Dict::g(); SharedHandle<Dict> dict = Dict::g();
char c; while(first != last) {
while(ss.get(c)) { if(*first == 'e') {
if(c == 'e') { return std::make_pair(dict, ++first);
return dict;
} else { } else {
ss.unget(); std::pair<std::string, InputIterator> keyp = decoderawstring(first, last);
std::string key = decoderawstring(ss); std::pair<SharedHandle<ValueBase>, InputIterator> r =
dict->put(key, decodeiter(ss, depth)); decodeiter(keyp.second, last, depth);
dict->put(keyp.first, r.first);
first = r.second;
} }
} }
throw DL_ABORT_EX2("Bencode decoding failed:" throw DL_ABORT_EX2("Bencode decoding failed:"
@ -133,16 +135,22 @@ SharedHandle<ValueBase> decodedict(std::istream& ss, size_t depth)
} // namespace } // namespace
namespace { namespace {
SharedHandle<ValueBase> decodelist(std::istream& ss, size_t depth) template<typename InputIterator>
std::pair<SharedHandle<ValueBase>, InputIterator>
decodelist
(InputIterator first,
InputIterator last,
size_t depth)
{ {
SharedHandle<List> list = List::g(); SharedHandle<List> list = List::g();
char c; while(first != last) {
while(ss.get(c)) { if(*first == 'e') {
if(c == 'e') { return std::make_pair(list, ++first);
return list;
} else { } else {
ss.unget(); std::pair<SharedHandle<ValueBase>, InputIterator> r =
list->append(decodeiter(ss, depth)); decodeiter(first, last, depth);
list->append(r.first);
first = r.second;
} }
} }
throw DL_ABORT_EX2("Bencode decoding failed:" throw DL_ABORT_EX2("Bencode decoding failed:"
@ -162,60 +170,79 @@ void checkDepth(size_t depth)
} // namespace } // namespace
namespace { namespace {
SharedHandle<ValueBase> decodeiter(std::istream& ss, size_t depth) template<typename InputIterator>
std::pair<SharedHandle<ValueBase>, InputIterator>
decodeiter
(InputIterator first,
InputIterator last,
size_t depth)
{ {
checkDepth(depth); checkDepth(depth);
char c; if(first == last) {
if(!ss.get(c)) {
throw DL_ABORT_EX2("Bencode decoding failed:" throw DL_ABORT_EX2("Bencode decoding failed:"
" Unexpected EOF in term context." " Unexpected EOF in term context."
" 'd', 'l', 'i' or digit is expected.", " 'd', 'l', 'i' or digit is expected.",
error_code::BENCODE_PARSE_ERROR); error_code::BENCODE_PARSE_ERROR);
} }
if(c == 'd') { if(*first == 'd') {
return decodedict(ss, depth+1); return decodedict(++first, last, depth+1);
} else if(c == 'l') { } else if(*first == 'l') {
return decodelist(ss, depth+1); return decodelist(++first, last, depth+1);
} else if(c == 'i') { } else if(*first == 'i') {
return decodeinteger(ss); return decodeinteger(++first, last);
} else { } else {
ss.unget(); return decodestring(first, last);
return decodestring(ss);
} }
} }
} // namespace } // namespace
SharedHandle<ValueBase> decode(std::istream& in) namespace {
template<typename InputIterator>
SharedHandle<ValueBase> decodegen
(InputIterator first,
InputIterator last,
size_t& end)
{ {
return decodeiter(in, 0); if(first == last) {
}
SharedHandle<ValueBase> decode(const std::string& s)
{
size_t end;
return decode(s, end);
}
SharedHandle<ValueBase> decode(const std::string& s, size_t& end)
{
if(s.empty()) {
return SharedHandle<ValueBase>(); return SharedHandle<ValueBase>();
} }
std::istringstream ss(s); std::pair<SharedHandle<ValueBase>, InputIterator> p =
decodeiter(first, last, 0);
end = p.second-first;
return p.first;
}
} // namespace
SharedHandle<ValueBase> vlb = decodeiter(ss, 0); SharedHandle<ValueBase> decode
end = ss.tellg(); (std::string::const_iterator first,
return vlb; std::string::const_iterator last)
{
size_t end;
return decodegen(first, last, end);
} }
SharedHandle<ValueBase> decode(const unsigned char* data, size_t length) SharedHandle<ValueBase> decode
(std::string::const_iterator first,
std::string::const_iterator last,
size_t& end)
{ {
return decode(std::string(&data[0], &data[length])); return decodegen(first, last, end);
} }
SharedHandle<ValueBase> decode(const unsigned char* data, size_t length, size_t& end) SharedHandle<ValueBase> decode
(const unsigned char* first,
const unsigned char* last)
{ {
return decode(std::string(&data[0], &data[length]), end); size_t end;
return decodegen(first, last, end);
}
SharedHandle<ValueBase> decode
(const unsigned char* first,
const unsigned char* last,
size_t& end)
{
return decodegen(first, last, end);
} }
SharedHandle<ValueBase> decodeFromFile(const std::string& filename) SharedHandle<ValueBase> decodeFromFile(const std::string& filename)
@ -225,7 +252,9 @@ SharedHandle<ValueBase> decodeFromFile(const std::string& filename)
std::stringstream ss; std::stringstream ss;
fp.transfer(ss); fp.transfer(ss);
fp.close(); fp.close();
return decode(ss); const std::string s = ss.str();
size_t end;
return decodegen(s.begin(), s.end(), end);
} else { } else {
throw DL_ABORT_EX2 throw DL_ABORT_EX2
(fmt("Bencode decoding failed: Cannot open file '%s'.", (fmt("Bencode decoding failed: Cannot open file '%s'.",

View File

@ -48,19 +48,26 @@ namespace bencode2 {
const size_t MAX_STRUCTURE_DEPTH = 100; const size_t MAX_STRUCTURE_DEPTH = 100;
SharedHandle<ValueBase> decode(std::istream& in); // Decode the data in [first, last).
SharedHandle<ValueBase> decode
(std::string::const_iterator first,
std::string::const_iterator last);
// Decode the data in s. // Decode the data in [first, last). After decode is done
SharedHandle<ValueBase> decode(const std::string& s); // successfully, return the bencoded string length in end.
SharedHandle<ValueBase> decode
// Decode the data in s. After decode is done successfully, return the (std::string::const_iterator first,
// bencoded string length in end. std::string::const_iterator last,
SharedHandle<ValueBase> decode(const std::string& s, size_t& end); size_t& end);
SharedHandle<ValueBase> decode(const unsigned char* data, size_t length);
SharedHandle<ValueBase> decode SharedHandle<ValueBase> decode
(const unsigned char* data, size_t length, size_t& end); (const unsigned char* first,
const unsigned char* last);
SharedHandle<ValueBase> decode
(const unsigned char* first,
const unsigned char* last,
size_t& end);
SharedHandle<ValueBase> decodeFromFile(const std::string& filename); SharedHandle<ValueBase> decodeFromFile(const std::string& filename);

View File

@ -544,7 +544,7 @@ void loadFromMemory(const unsigned char* content,
const std::string& overrideName) const std::string& overrideName)
{ {
processRootDictionary(ctx, processRootDictionary(ctx,
bencode2::decode(content, length), bencode2::decode(content, content+length),
option, option,
defaultName, defaultName,
overrideName, overrideName,
@ -560,7 +560,7 @@ void loadFromMemory(const unsigned char* content,
const std::string& overrideName) const std::string& overrideName)
{ {
processRootDictionary(ctx, processRootDictionary(ctx,
bencode2::decode(content, length), bencode2::decode(content, content+length),
option, option,
defaultName, defaultName,
overrideName, overrideName,
@ -575,7 +575,7 @@ void loadFromMemory(const std::string& context,
{ {
processRootDictionary processRootDictionary
(ctx, (ctx,
bencode2::decode(context), bencode2::decode(context.begin(), context.end()),
option, option,
defaultName, overrideName, defaultName, overrideName,
std::vector<std::string>()); std::vector<std::string>());
@ -590,7 +590,7 @@ void loadFromMemory(const std::string& context,
{ {
processRootDictionary processRootDictionary
(ctx, (ctx,
bencode2::decode(context), bencode2::decode(context.begin(), context.end()),
option, option,
defaultName, overrideName, defaultName, overrideName,
uris); uris);

View File

@ -112,7 +112,8 @@ namespace util {
template<typename InputIterator> template<typename InputIterator>
void divide void divide
(std::pair<Scip, Scip>& hp, (std::pair<std::pair<InputIterator, InputIterator>,
std::pair<InputIterator, InputIterator> >& hp,
InputIterator first, InputIterator first,
InputIterator last, InputIterator last,
char delim) char delim)
@ -248,7 +249,7 @@ bool parseIntNoThrow
(int32_t& result, InputIterator first, InputIterator last, int base = 10) (int32_t& result, InputIterator first, InputIterator last, int base = 10)
{ {
// Without strip, strtol(" -1 ",..) emits error. // Without strip, strtol(" -1 ",..) emits error.
Scip p = util::stripIter(first, last); std::pair<InputIterator, InputIterator> p = util::stripIter(first, last);
if(p.first == p.second) { if(p.first == p.second) {
return false; return false;
} }
@ -277,7 +278,7 @@ bool parseUIntNoThrow
(uint32_t& result, InputIterator first, InputIterator last, int base = 10) (uint32_t& result, InputIterator first, InputIterator last, int base = 10)
{ {
// Without strip, strtol(" -1 ",..) emits error. // Without strip, strtol(" -1 ",..) emits error.
Scip p = util::stripIter(first, last); std::pair<InputIterator, InputIterator> p = util::stripIter(first, last);
if(p.first == p.second) { if(p.first == p.second) {
return false; return false;
} }
@ -309,7 +310,7 @@ bool parseLLIntNoThrow
(int64_t& result, InputIterator first, InputIterator last, int base = 10) (int64_t& result, InputIterator first, InputIterator last, int base = 10)
{ {
// Without strip, strtol(" -1 ",..) emits error. // Without strip, strtol(" -1 ",..) emits error.
Scip p = util::stripIter(first, last); std::pair<InputIterator, InputIterator> p = util::stripIter(first, last);
if(p.first == p.second) { if(p.first == p.second) {
return false; return false;
} }
@ -335,7 +336,7 @@ bool parseLLIntNoThrow
template<typename InputIterator> template<typename InputIterator>
int64_t parseLLInt(InputIterator first, InputIterator last, int base = 10) int64_t parseLLInt(InputIterator first, InputIterator last, int base = 10)
{ {
Scip p = util::stripIter(first, last); std::pair<InputIterator, InputIterator> p = util::stripIter(first, last);
if(p.first == p.second) { if(p.first == p.second) {
throw DL_ABORT_EX(fmt(MSG_STRING_INTEGER_CONVERSION_FAILURE, throw DL_ABORT_EX(fmt(MSG_STRING_INTEGER_CONVERSION_FAILURE,
"empty string")); "empty string"));
@ -373,7 +374,7 @@ int32_t parseInt(InputIterator first, InputIterator last, int base = 10)
template<typename InputIterator> template<typename InputIterator>
uint64_t parseULLInt(InputIterator first, InputIterator last, int base = 10) uint64_t parseULLInt(InputIterator first, InputIterator last, int base = 10)
{ {
Scip p = util::stripIter(first, last); std::pair<InputIterator, InputIterator> p = util::stripIter(first, last);
if(p.first == p.second) { if(p.first == p.second) {
throw DL_ABORT_EX(fmt(MSG_STRING_INTEGER_CONVERSION_FAILURE, throw DL_ABORT_EX(fmt(MSG_STRING_INTEGER_CONVERSION_FAILURE,
"empty string")); "empty string"));

View File

@ -64,7 +64,8 @@ std::vector<std::vector<std::string> > toVector
void AnnounceListTest::testSingleElementList() { void AnnounceListTest::testSingleElementList() {
std::string peersString = "ll8:tracker1el8:tracker2el8:tracker3ee"; std::string peersString = "ll8:tracker1el8:tracker2el8:tracker3ee";
SharedHandle<ValueBase> announcesList = bencode2::decode(peersString); SharedHandle<ValueBase> announcesList =
bencode2::decode(peersString.begin(), peersString.end());
// ANNOUNCE_LIST // ANNOUNCE_LIST
// [ [ tracker1 ], [ tracker2 ], [ tracker3 ] ] // [ [ tracker1 ], [ tracker2 ], [ tracker3 ] ]
@ -110,7 +111,8 @@ void AnnounceListTest::testSingleElementList() {
void AnnounceListTest::testMultiElementList() { void AnnounceListTest::testMultiElementList() {
std::string peersString = "ll8:tracker18:tracker28:tracker3ee"; std::string peersString = "ll8:tracker18:tracker28:tracker3ee";
SharedHandle<ValueBase> announcesList = bencode2::decode(peersString); SharedHandle<ValueBase> announcesList =
bencode2::decode(peersString.begin(), peersString.end());
// ANNOUNCE_LIST // ANNOUNCE_LIST
// [ [ tracker1, tracker2, tracker3 ] ] // [ [ tracker1, tracker2, tracker3 ] ]
@ -143,7 +145,8 @@ void AnnounceListTest::testMultiElementList() {
void AnnounceListTest::testSingleAndMulti() { void AnnounceListTest::testSingleAndMulti() {
std::string peersString = "ll8:tracker18:tracker2el8:tracker3ee"; std::string peersString = "ll8:tracker18:tracker2el8:tracker3ee";
SharedHandle<ValueBase> announcesList = bencode2::decode(peersString); SharedHandle<ValueBase> announcesList =
bencode2::decode(peersString.begin(), peersString.end());
// ANNOUNCE_LIST // ANNOUNCE_LIST
// [ [ tracker1, tracker2 ], [ tracker3 ] ] // [ [ tracker1, tracker2 ], [ tracker3 ] ]
@ -169,14 +172,16 @@ void AnnounceListTest::testSingleAndMulti() {
void AnnounceListTest::testNoGroup() { void AnnounceListTest::testNoGroup() {
std::string peersString = "llee"; std::string peersString = "llee";
SharedHandle<ValueBase> announcesList = bencode2::decode(peersString); SharedHandle<ValueBase> announcesList =
bencode2::decode(peersString.begin(), peersString.end());
AnnounceList announceList(toVector(downcast<List>(announcesList))); AnnounceList announceList(toVector(downcast<List>(announcesList)));
CPPUNIT_ASSERT(announceList.countTier() == 0); CPPUNIT_ASSERT(announceList.countTier() == 0);
} }
void AnnounceListTest::testNextEventIfAfterStarted() { void AnnounceListTest::testNextEventIfAfterStarted() {
std::string peersString = "ll8:tracker1ee"; std::string peersString = "ll8:tracker1ee";
SharedHandle<ValueBase> announcesList = bencode2::decode(peersString); SharedHandle<ValueBase> announcesList =
bencode2::decode(peersString.begin(), peersString.end());
// ANNOUNCE_LIST // ANNOUNCE_LIST
// [ [ tracker1 ] ] // [ [ tracker1 ] ]
@ -196,7 +201,8 @@ void AnnounceListTest::testNextEventIfAfterStarted() {
void AnnounceListTest::testEvent() { void AnnounceListTest::testEvent() {
std::string peersString = "ll8:tracker1el8:tracker2el8:tracker3ee"; std::string peersString = "ll8:tracker1el8:tracker2el8:tracker3ee";
SharedHandle<ValueBase> announcesList = bencode2::decode(peersString); SharedHandle<ValueBase> announcesList =
bencode2::decode(peersString.begin(), peersString.end());
// ANNOUNCE_LIST // ANNOUNCE_LIST
// [ [ tracker1 ], [ tracker2 ], [ tracker3 ] ] // [ [ tracker1 ], [ tracker2 ], [ tracker3 ] ]
@ -220,7 +226,8 @@ void AnnounceListTest::testEvent() {
void AnnounceListTest::testCountStoppedAllowedTier() { void AnnounceListTest::testCountStoppedAllowedTier() {
std::string peersString = "ll8:tracker1el8:tracker2el8:tracker3ee"; std::string peersString = "ll8:tracker1el8:tracker2el8:tracker3ee";
SharedHandle<ValueBase> announcesList = bencode2::decode(peersString); SharedHandle<ValueBase> announcesList =
bencode2::decode(peersString.begin(), peersString.end());
// ANNOUNCE_LIST // ANNOUNCE_LIST
// [ [ tracker1 ], [ tracker2 ], [ tracker3 ] ] // [ [ tracker1 ], [ tracker2 ], [ tracker3 ] ]
@ -247,7 +254,8 @@ void AnnounceListTest::testCountStoppedAllowedTier() {
void AnnounceListTest::testCountCompletedAllowedTier() { void AnnounceListTest::testCountCompletedAllowedTier() {
std::string peersString = "ll8:tracker1el8:tracker2el8:tracker3ee"; std::string peersString = "ll8:tracker1el8:tracker2el8:tracker3ee";
SharedHandle<ValueBase> announcesList = bencode2::decode(peersString); SharedHandle<ValueBase> announcesList =
bencode2::decode(peersString.begin(), peersString.end());
// ANNOUNCE_LIST // ANNOUNCE_LIST
// [ [ tracker1 ], [ tracker2 ], [ tracker3 ] ] // [ [ tracker1 ], [ tracker2 ], [ tracker3 ] ]

View File

@ -27,8 +27,8 @@ void Bencode2Test::testDecode()
{ {
{ {
// string, integer and list in dict // string, integer and list in dict
SharedHandle<ValueBase> r = std::string src = "d4:name5:aria24:sizei12345678900e5:filesl3:bin3:docee";
bencode2::decode("d4:name5:aria24:sizei12345678900e5:filesl3:bin3:docee"); SharedHandle<ValueBase> r = bencode2::decode(src.begin(), src.end());
const Dict* dict = downcast<Dict>(r); const Dict* dict = downcast<Dict>(r);
CPPUNIT_ASSERT(dict); CPPUNIT_ASSERT(dict);
CPPUNIT_ASSERT_EQUAL(std::string("aria2"), CPPUNIT_ASSERT_EQUAL(std::string("aria2"),
@ -45,7 +45,8 @@ void Bencode2Test::testDecode()
} }
{ {
// dict in list // dict in list
SharedHandle<ValueBase> r = bencode2::decode("ld1:ki123eee"); std::string src = "ld1:ki123eee";
SharedHandle<ValueBase> r = bencode2::decode(src.begin(), src.end());
const List* list = downcast<List>(r); const List* list = downcast<List>(r);
CPPUNIT_ASSERT(list); CPPUNIT_ASSERT(list);
CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), list->size()); CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), list->size());
@ -56,38 +57,44 @@ void Bencode2Test::testDecode()
} }
{ {
// empty key is allowed // empty key is allowed
SharedHandle<ValueBase> s = bencode2::decode("d0:1:ve"); std::string src = "d0:1:ve";
SharedHandle<ValueBase> s = bencode2::decode(src.begin(), src.end());
} }
{ {
// empty string // empty string
SharedHandle<ValueBase> s = bencode2::decode("0:"); std::string src = "0:";
SharedHandle<ValueBase> s = bencode2::decode(src.begin(), src.end());
CPPUNIT_ASSERT_EQUAL(std::string(""), downcast<String>(s)->s()); CPPUNIT_ASSERT_EQUAL(std::string(""), downcast<String>(s)->s());
} }
{ {
// empty dict // empty dict
SharedHandle<ValueBase> d = bencode2::decode("de"); std::string src = "de";
SharedHandle<ValueBase> d = bencode2::decode(src.begin(), src.end());
CPPUNIT_ASSERT(downcast<Dict>(d)->empty()); CPPUNIT_ASSERT(downcast<Dict>(d)->empty());
} }
{ {
// empty list // empty list
SharedHandle<ValueBase> l = bencode2::decode("le"); std::string src = "le";
SharedHandle<ValueBase> l = bencode2::decode(src.begin(), src.end());
CPPUNIT_ASSERT(downcast<List>(l)->empty()); CPPUNIT_ASSERT(downcast<List>(l)->empty());
} }
{ {
// integer, without ending 'e' // integer, without ending 'e'
std::string src = "i3";
try { try {
bencode2::decode("i3"); bencode2::decode(src.begin(), src.end());
CPPUNIT_FAIL("exception must be thrown."); CPPUNIT_FAIL("exception must be thrown.");
} catch(RecoverableException& e) { } catch(RecoverableException& e) {
CPPUNIT_ASSERT_EQUAL(std::string("Bencode decoding failed:" CPPUNIT_ASSERT_EQUAL(std::string("Bencode decoding failed:"
" Delimiter 'e' not found."), " Integer expected but none found"),
std::string(e.what())); std::string(e.what()));
} }
} }
{ {
// dict, without ending 'e' // dict, without ending 'e'
std::string src = "d";
try { try {
bencode2::decode("d"); bencode2::decode(src.begin(), src.end());
CPPUNIT_FAIL("exception must be thrown."); CPPUNIT_FAIL("exception must be thrown.");
} catch(RecoverableException& e) { } catch(RecoverableException& e) {
CPPUNIT_ASSERT_EQUAL(std::string("Bencode decoding failed:" CPPUNIT_ASSERT_EQUAL(std::string("Bencode decoding failed:"
@ -99,7 +106,8 @@ void Bencode2Test::testDecode()
{ {
// list, without ending 'e' // list, without ending 'e'
try { try {
bencode2::decode("l"); std::string src = "l";
bencode2::decode(src.begin(), src.end());
CPPUNIT_FAIL("exception must be thrown."); CPPUNIT_FAIL("exception must be thrown.");
} catch(RecoverableException& e) { } catch(RecoverableException& e) {
CPPUNIT_ASSERT_EQUAL(std::string("Bencode decoding failed:" CPPUNIT_ASSERT_EQUAL(std::string("Bencode decoding failed:"
@ -111,7 +119,8 @@ void Bencode2Test::testDecode()
{ {
// string, less than the specified length. // string, less than the specified length.
try { try {
bencode2::decode("3:ab"); std::string src = "3:ab";
bencode2::decode(src.begin(), src.end());
CPPUNIT_FAIL("exception must be thrown."); CPPUNIT_FAIL("exception must be thrown.");
} catch(RecoverableException& e) { } catch(RecoverableException& e) {
CPPUNIT_ASSERT_EQUAL(std::string("Bencode decoding failed:" CPPUNIT_ASSERT_EQUAL(std::string("Bencode decoding failed:"
@ -123,7 +132,8 @@ void Bencode2Test::testDecode()
{ {
// string, but length is invalid // string, but length is invalid
try { try {
bencode2::decode("x:abc"); std::string src = "x:abc";
bencode2::decode(src.begin(), src.end());
CPPUNIT_FAIL("exception must be thrown."); CPPUNIT_FAIL("exception must be thrown.");
} catch(RecoverableException& e) { } catch(RecoverableException& e) {
CPPUNIT_ASSERT_EQUAL(std::string("Bencode decoding failed:" CPPUNIT_ASSERT_EQUAL(std::string("Bencode decoding failed:"
@ -134,8 +144,9 @@ void Bencode2Test::testDecode()
} }
{ {
// string with minus length // string with minus length
std::string src = "-1:a";
try { try {
bencode2::decode("-1:a"); bencode2::decode(src.begin(), src.end());
CPPUNIT_FAIL("exception must be thrown."); CPPUNIT_FAIL("exception must be thrown.");
} catch(RecoverableException& e) { } catch(RecoverableException& e) {
CPPUNIT_ASSERT_EQUAL(std::string("Bencode decoding failed:" CPPUNIT_ASSERT_EQUAL(std::string("Bencode decoding failed:"
@ -146,17 +157,20 @@ void Bencode2Test::testDecode()
} }
{ {
// empty encoded data // empty encoded data
CPPUNIT_ASSERT(!bencode2::decode("")); std::string src = "";
CPPUNIT_ASSERT(!bencode2::decode(src.begin(), src.end()));
} }
{ {
// ignore trailing garbage at the end of the input. // ignore trailing garbage at the end of the input.
SharedHandle<ValueBase> s = bencode2::decode("5:aria2trail"); std::string src = "5:aria2trail";
SharedHandle<ValueBase> s = bencode2::decode(src.begin(), src.end());
CPPUNIT_ASSERT_EQUAL(std::string("aria2"), downcast<String>(s)->s()); CPPUNIT_ASSERT_EQUAL(std::string("aria2"), downcast<String>(s)->s());
} }
{ {
// Get trailing garbage position // Get trailing garbage position
std::string src = "5:aria2trail";
size_t end; size_t end;
SharedHandle<ValueBase> s = bencode2::decode("5:aria2trail", end); SharedHandle<ValueBase> s = bencode2::decode(src.begin(), src.end(), end);
CPPUNIT_ASSERT_EQUAL(std::string("aria2"), downcast<String>(s)->s()); CPPUNIT_ASSERT_EQUAL(std::string("aria2"), downcast<String>(s)->s());
CPPUNIT_ASSERT_EQUAL((size_t)7, end); CPPUNIT_ASSERT_EQUAL((size_t)7, end);
} }
@ -173,7 +187,7 @@ void Bencode2Test::testDecode_overflow()
s += "e"; s += "e";
} }
try { try {
bencode2::decode(s); bencode2::decode(s.begin(), s.end());
CPPUNIT_FAIL("exception must be thrown."); CPPUNIT_FAIL("exception must be thrown.");
} catch(RecoverableException& e) { } catch(RecoverableException& e) {
// success // success

View File

@ -746,7 +746,8 @@ void BittorrentHelperTest::testMetadata() {
SharedHandle<DownloadContext> dctx(new DownloadContext()); SharedHandle<DownloadContext> dctx(new DownloadContext());
load(A2_TEST_DIR"/test.torrent", dctx, option_); load(A2_TEST_DIR"/test.torrent", dctx, option_);
std::string torrentData = readFile(A2_TEST_DIR"/test.torrent"); std::string torrentData = readFile(A2_TEST_DIR"/test.torrent");
SharedHandle<ValueBase> tr = bencode2::decode(torrentData); SharedHandle<ValueBase> tr =
bencode2::decode(torrentData.begin(), torrentData.end());
SharedHandle<ValueBase> infoDic = downcast<Dict>(tr)->get("info"); SharedHandle<ValueBase> infoDic = downcast<Dict>(tr)->get("info");
std::string metadata = bencode2::encode(infoDic); std::string metadata = bencode2::encode(infoDic);
SharedHandle<TorrentAttribute> attrs = getTorrentAttrs(dctx); SharedHandle<TorrentAttribute> attrs = getTorrentAttrs(dctx);
@ -830,7 +831,8 @@ void BittorrentHelperTest::testExtractPeerFromString()
std::string hextext = "100210354527354678541237324732171ae1"; std::string hextext = "100210354527354678541237324732171ae1";
hextext += "20010db8bd0501d2288a1fc0000110ee1ae2"; hextext += "20010db8bd0501d2288a1fc0000110ee1ae2";
std::string peersstr = "36:"+util::fromHex(hextext); std::string peersstr = "36:"+util::fromHex(hextext);
SharedHandle<ValueBase> str = bencode2::decode(peersstr); SharedHandle<ValueBase> str =
bencode2::decode(peersstr.begin(), peersstr.end());
std::deque<SharedHandle<Peer> > peers; std::deque<SharedHandle<Peer> > peers;
extractPeer(str, AF_INET6, std::back_inserter(peers)); extractPeer(str, AF_INET6, std::back_inserter(peers));
CPPUNIT_ASSERT_EQUAL((size_t)2, peers.size()); CPPUNIT_ASSERT_EQUAL((size_t)2, peers.size());
@ -844,7 +846,7 @@ void BittorrentHelperTest::testExtractPeerFromString()
hextext = "c0a800011ae1"; hextext = "c0a800011ae1";
hextext += "c0a800021ae2"; hextext += "c0a800021ae2";
peersstr = "12:"+util::fromHex(hextext); peersstr = "12:"+util::fromHex(hextext);
str = bencode2::decode(peersstr); str = bencode2::decode(peersstr.begin(), peersstr.end());
peers.clear(); peers.clear();
extractPeer(str, AF_INET, std::back_inserter(peers)); extractPeer(str, AF_INET, std::back_inserter(peers));
CPPUNIT_ASSERT_EQUAL((size_t)2, peers.size()); CPPUNIT_ASSERT_EQUAL((size_t)2, peers.size());
@ -860,7 +862,8 @@ void BittorrentHelperTest::testExtractPeerFromList()
"d5:peersld2:ip11:192.168.0.17:peer id20:aria2-00000000000000" "d5:peersld2:ip11:192.168.0.17:peer id20:aria2-00000000000000"
"4:porti2006eeee"; "4:porti2006eeee";
SharedHandle<ValueBase> dict = bencode2::decode(peersString); SharedHandle<ValueBase> dict =
bencode2::decode(peersString.begin(), peersString.end());
std::deque<SharedHandle<Peer> > peers; std::deque<SharedHandle<Peer> > peers;
extractPeer(downcast<Dict>(dict)->get("peers"), AF_INET, std::back_inserter(peers)); extractPeer(downcast<Dict>(dict)->get("peers"), AF_INET, std::back_inserter(peers));
@ -877,7 +880,8 @@ void BittorrentHelperTest::testExtract2PeersFromList()
"4:porti65535eed2:ip11:192.168.0.27:peer id20:aria2-00000000000000" "4:porti65535eed2:ip11:192.168.0.27:peer id20:aria2-00000000000000"
"4:porti2007eeee"; "4:porti2007eeee";
SharedHandle<ValueBase> dict = bencode2::decode(peersString); SharedHandle<ValueBase> dict =
bencode2::decode(peersString.begin(), peersString.end());
std::deque<SharedHandle<Peer> > peers; std::deque<SharedHandle<Peer> > peers;
extractPeer(downcast<Dict>(dict)->get("peers"), AF_INET, std::back_inserter(peers)); extractPeer(downcast<Dict>(dict)->get("peers"), AF_INET, std::back_inserter(peers));

View File

@ -268,7 +268,7 @@ void UtilTest::testLstripIter_char()
} }
void UtilTest::testDivide() { void UtilTest::testDivide() {
std::pair<Scip, Scip> p1; std::pair<Sip, Sip> p1;
std::string s = "name=value"; std::string s = "name=value";
util::divide(p1, s.begin(), s.end(), '='); util::divide(p1, s.begin(), s.end(), '=');
CPPUNIT_ASSERT_EQUAL(std::string("name"), CPPUNIT_ASSERT_EQUAL(std::string("name"),