From d1d49036651012433595357ca31e6f65dbec3a6b Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sat, 5 Nov 2011 12:01:57 +0900 Subject: [PATCH] Rewritten bencode2::decode functions --- src/DHTMessageReceiver.cc | 2 +- src/DefaultBtAnnounce.cc | 2 +- src/DefaultExtensionMessageFactory.cc | 3 +- src/FtpConnection.cc | 2 +- src/HandshakeExtensionMessage.cc | 2 +- src/HttpServer.cc | 7 +- src/OptionParser.cc | 2 +- src/ParameterizedStringParser.cc | 2 +- src/UTPexExtensionMessage.cc | 2 +- src/bencode2.cc | 197 +++++++++++++++----------- src/bencode2.h | 27 ++-- src/bittorrent_helper.cc | 8 +- src/util.h | 13 +- test/AnnounceListTest.cc | 24 ++-- test/Bencode2Test.cc | 50 ++++--- test/BittorrentHelperTest.cc | 14 +- test/UtilTest.cc | 2 +- 17 files changed, 212 insertions(+), 147 deletions(-) diff --git a/src/DHTMessageReceiver.cc b/src/DHTMessageReceiver.cc index e5558d9d..8b2b98b1 100644 --- a/src/DHTMessageReceiver.cc +++ b/src/DHTMessageReceiver.cc @@ -76,7 +76,7 @@ SharedHandle DHTMessageReceiver::receiveMessage() return SharedHandle(); } bool isReply = false; - SharedHandle decoded = bencode2::decode(data, length); + SharedHandle decoded = bencode2::decode(data, data+length); const Dict* dict = downcast(decoded); if(dict) { const String* y = downcast(dict->get(DHTMessage::Y)); diff --git a/src/DefaultBtAnnounce.cc b/src/DefaultBtAnnounce.cc index 62ba3646..43778ab6 100644 --- a/src/DefaultBtAnnounce.cc +++ b/src/DefaultBtAnnounce.cc @@ -220,7 +220,7 @@ DefaultBtAnnounce::processAnnounceResponse(const unsigned char* trackerResponse, { A2_LOG_DEBUG("Now processing tracker response."); SharedHandle decodedValue = - bencode2::decode(trackerResponse, trackerResponseLength); + bencode2::decode(trackerResponse, trackerResponse+trackerResponseLength); const Dict* dict = downcast(decodedValue); if(!dict) { throw DL_ABORT_EX(MSG_NULL_TRACKER_RESPONSE); diff --git a/src/DefaultExtensionMessageFactory.cc b/src/DefaultExtensionMessageFactory.cc index 3d3f9b36..47fa26b3 100644 --- a/src/DefaultExtensionMessageFactory.cc +++ b/src/DefaultExtensionMessageFactory.cc @@ -101,7 +101,8 @@ DefaultExtensionMessageFactory::createMessage(const unsigned char* data, size_t static_cast(length))); } size_t end; - SharedHandle decoded = bencode2::decode(data+1, length-1, end); + SharedHandle decoded = + bencode2::decode(data+1, data+length, end); const Dict* dict = downcast(decoded); if(!dict) { throw DL_ABORT_EX("Bad ut_metadata: dictionary not found"); diff --git a/src/FtpConnection.cc b/src/FtpConnection.cc index d6cd94f0..9fc3ac80 100644 --- a/src/FtpConnection.cc +++ b/src/FtpConnection.cc @@ -435,7 +435,7 @@ unsigned int FtpConnection::receiveSizeResponse(uint64_t& size) std::pair response; if(bulkReceiveResponse(response)) { if(response.first == 213) { - std::pair rp; + std::pair rp; util::divide(rp, response.second.begin(), response.second.end(), ' '); size = util::parseULLInt(rp.second.first, rp.second.second); } diff --git a/src/HandshakeExtensionMessage.cc b/src/HandshakeExtensionMessage.cc index cec3ea2d..3fae69d8 100644 --- a/src/HandshakeExtensionMessage.cc +++ b/src/HandshakeExtensionMessage.cc @@ -173,7 +173,7 @@ HandshakeExtensionMessage::create(const unsigned char* data, size_t length) HandshakeExtensionMessageHandle msg(new HandshakeExtensionMessage()); A2_LOG_DEBUG(fmt("Creating HandshakeExtensionMessage from %s", util::percentEncode(data, length).c_str())); - SharedHandle decoded = bencode2::decode(data+1, length-1); + SharedHandle decoded = bencode2::decode(data+1, data+length); const Dict* dict = downcast(decoded); if(!dict) { throw DL_ABORT_EX diff --git a/src/HttpServer.cc b/src/HttpServer.cc index cef02a80..318635c6 100644 --- a/src/HttpServer.cc +++ b/src/HttpServer.cc @@ -227,10 +227,11 @@ bool HttpServer::authenticate() } std::string userpass = Base64::decode(std::string(p.second.first, p.second.second)); - util::divide(p, userpass.begin(), userpass.end(), ':'); - return util::streq(p.first.first, p.first.second, + std::pair up; + util::divide(up, userpass.begin(), userpass.end(), ':'); + return util::streq(up.first.first, up.first.second, username_.begin(), username_.end()) && - util::streq(p.second.first, p.second.second, + util::streq(up.second.first, up.second.second, password_.begin(), password_.end()); } diff --git a/src/OptionParser.cc b/src/OptionParser.cc index 157d2adc..a1e4090d 100644 --- a/src/OptionParser.cc +++ b/src/OptionParser.cc @@ -184,7 +184,7 @@ void OptionParser::parse(Option& option, std::istream& is) const if(line.empty() || line[0] == '#') { continue; } - std::pair nv; + std::pair nv; util::divide(nv, line.begin(), line.end(), '='); if(nv.first.first == nv.first.second) { continue; diff --git a/src/ParameterizedStringParser.cc b/src/ParameterizedStringParser.cc index 2b80d41b..579881fa 100644 --- a/src/ParameterizedStringParser.cc +++ b/src/ParameterizedStringParser.cc @@ -124,7 +124,7 @@ ParameterizedStringParser::createLoop(const std::string& src, int& offset) } loopStr.erase(colonIndex); } - std::pair range; + std::pair range; util::divide(range, loopStr.begin(), loopStr.end(), '-'); if(range.first.first == range.first.second || range.second.first == range.second.second) { diff --git a/src/UTPexExtensionMessage.cc b/src/UTPexExtensionMessage.cc index a974d8f2..fe0e99c6 100644 --- a/src/UTPexExtensionMessage.cc +++ b/src/UTPexExtensionMessage.cc @@ -190,7 +190,7 @@ UTPexExtensionMessage::create(const unsigned char* data, size_t len) } UTPexExtensionMessageHandle msg(new UTPexExtensionMessage(*data)); - SharedHandle decoded = bencode2::decode(data+1, len-1); + SharedHandle decoded = bencode2::decode(data+1, data+len); const Dict* dict = downcast(decoded); if(dict) { const String* added = downcast(dict->get("added")); diff --git a/src/bencode2.cc b/src/bencode2.cc index 181309a8..1db0ddbe 100644 --- a/src/bencode2.cc +++ b/src/bencode2.cc @@ -40,90 +40,92 @@ #include "DlAbortEx.h" #include "error_code.h" #include "BufferedFile.h" +#include "util.h" namespace aria2 { namespace bencode2 { namespace { -SharedHandle decodeiter(std::istream& ss, size_t depth); +template +std::pair, InputIterator> +decodeiter +(InputIterator first, + InputIterator last, + size_t depth); } // namespace namespace { -void checkdelim(std::istream& ss, const char delim = ':') +template +std::pair +decoderawstring(InputIterator first, InputIterator last) { - char d; - if(!(ss.get(d) && d == delim)) { - throw DL_ABORT_EX2 - (fmt("Bencode decoding failed: Delimiter '%c' not found.", - delim), - error_code::BENCODE_PARSE_ERROR); - } -} -} // namespace - -namespace { -std::string decoderawstring(std::istream& ss) -{ - int length; - ss >> length; - if(!ss || length < 0) { + InputIterator i = first; + int32_t len; + for(; i != last && *i != ':'; ++i); + if(i == last || i == first || !util::parseIntNoThrow(len, first, i) || + len < 0) { throw DL_ABORT_EX2("Bencode decoding failed:" " A positive integer expected but none found.", error_code::BENCODE_PARSE_ERROR); } - // TODO check length, it must be less than or equal to INT_MAX - checkdelim(ss); - char* buf = new char[length]; - ss.read(buf, length); - std::string str(&buf[0], &buf[length]); - delete [] buf; - if(ss.gcount() != static_cast(length)) { + ++i; + if(last-i < len) { throw DL_ABORT_EX2 (fmt("Bencode decoding failed:" - " Expected %lu bytes of data, but only %ld read.", - static_cast(length), - static_cast(ss.gcount())), + " Expected %d bytes of data, but only %d read.", + len, static_cast(last-i)), error_code::BENCODE_PARSE_ERROR); } - return str; + return std::make_pair(std::string(i, i+len), i+len); } } // namespace namespace { -SharedHandle decodestring(std::istream& ss) +template +std::pair, InputIterator> +decodestring(InputIterator first, InputIterator last) { - return String::g(decoderawstring(ss)); + std::pair r = decoderawstring(first, last); + return std::make_pair(String::g(r.first), r.second); } } // namespace namespace { -SharedHandle decodeinteger(std::istream& ss) +template +std::pair, InputIterator> +decodeinteger(InputIterator first, InputIterator last) { + InputIterator i = first; + for(; i != last && *i != 'e'; ++i); Integer::ValueType iv; - ss >> iv; - if(!ss) { + if(i == last || !util::parseLLIntNoThrow(iv, first, i)) { throw DL_ABORT_EX2("Bencode decoding failed:" " Integer expected but none found", error_code::BENCODE_PARSE_ERROR); } - checkdelim(ss, 'e'); - return Integer::g(iv); + return std::make_pair(Integer::g(iv), ++i); } } // namespace namespace { -SharedHandle decodedict(std::istream& ss, size_t depth) +template +std::pair, InputIterator> +decodedict +(InputIterator first, + InputIterator last, + size_t depth) { SharedHandle dict = Dict::g(); - char c; - while(ss.get(c)) { - if(c == 'e') { - return dict; + while(first != last) { + if(*first == 'e') { + return std::make_pair(dict, ++first); } else { - ss.unget(); - std::string key = decoderawstring(ss); - dict->put(key, decodeiter(ss, depth)); + std::pair keyp = decoderawstring(first, last); + std::pair, InputIterator> r = + decodeiter(keyp.second, last, depth); + dict->put(keyp.first, r.first); + first = r.second; } } throw DL_ABORT_EX2("Bencode decoding failed:" @@ -133,16 +135,22 @@ SharedHandle decodedict(std::istream& ss, size_t depth) } // namespace namespace { -SharedHandle decodelist(std::istream& ss, size_t depth) +template +std::pair, InputIterator> +decodelist +(InputIterator first, + InputIterator last, + size_t depth) { SharedHandle list = List::g(); - char c; - while(ss.get(c)) { - if(c == 'e') { - return list; + while(first != last) { + if(*first == 'e') { + return std::make_pair(list, ++first); } else { - ss.unget(); - list->append(decodeiter(ss, depth)); + std::pair, InputIterator> r = + decodeiter(first, last, depth); + list->append(r.first); + first = r.second; } } throw DL_ABORT_EX2("Bencode decoding failed:" @@ -162,60 +170,79 @@ void checkDepth(size_t depth) } // namespace namespace { -SharedHandle decodeiter(std::istream& ss, size_t depth) +template +std::pair, InputIterator> +decodeiter +(InputIterator first, + InputIterator last, + size_t depth) { checkDepth(depth); - char c; - if(!ss.get(c)) { + if(first == last) { throw DL_ABORT_EX2("Bencode decoding failed:" " Unexpected EOF in term context." " 'd', 'l', 'i' or digit is expected.", error_code::BENCODE_PARSE_ERROR); } - if(c == 'd') { - return decodedict(ss, depth+1); - } else if(c == 'l') { - return decodelist(ss, depth+1); - } else if(c == 'i') { - return decodeinteger(ss); + if(*first == 'd') { + return decodedict(++first, last, depth+1); + } else if(*first == 'l') { + return decodelist(++first, last, depth+1); + } else if(*first == 'i') { + return decodeinteger(++first, last); } else { - ss.unget(); - return decodestring(ss); + return decodestring(first, last); } } } // namespace -SharedHandle decode(std::istream& in) +namespace { +template +SharedHandle decodegen +(InputIterator first, + InputIterator last, + size_t& end) { - return decodeiter(in, 0); -} - -SharedHandle decode(const std::string& s) -{ - size_t end; - return decode(s, end); -} - -SharedHandle decode(const std::string& s, size_t& end) -{ - if(s.empty()) { + if(first == last) { return SharedHandle(); } - std::istringstream ss(s); + std::pair, InputIterator> p = + decodeiter(first, last, 0); + end = p.second-first; + return p.first; +} +} // namespace - SharedHandle vlb = decodeiter(ss, 0); - end = ss.tellg(); - return vlb; +SharedHandle decode +(std::string::const_iterator first, + std::string::const_iterator last) +{ + size_t end; + return decodegen(first, last, end); } -SharedHandle decode(const unsigned char* data, size_t length) +SharedHandle 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 decode(const unsigned char* data, size_t length, size_t& end) +SharedHandle 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 decode +(const unsigned char* first, + const unsigned char* last, + size_t& end) +{ + return decodegen(first, last, end); } SharedHandle decodeFromFile(const std::string& filename) @@ -225,7 +252,9 @@ SharedHandle decodeFromFile(const std::string& filename) std::stringstream ss; fp.transfer(ss); fp.close(); - return decode(ss); + const std::string s = ss.str(); + size_t end; + return decodegen(s.begin(), s.end(), end); } else { throw DL_ABORT_EX2 (fmt("Bencode decoding failed: Cannot open file '%s'.", diff --git a/src/bencode2.h b/src/bencode2.h index 7f4e226f..bfc95de6 100644 --- a/src/bencode2.h +++ b/src/bencode2.h @@ -48,19 +48,26 @@ namespace bencode2 { const size_t MAX_STRUCTURE_DEPTH = 100; -SharedHandle decode(std::istream& in); +// Decode the data in [first, last). +SharedHandle decode +(std::string::const_iterator first, + std::string::const_iterator last); -// Decode the data in s. -SharedHandle decode(const std::string& s); - -// Decode the data in s. After decode is done successfully, return the -// bencoded string length in end. -SharedHandle decode(const std::string& s, size_t& end); - -SharedHandle decode(const unsigned char* data, size_t length); +// Decode the data in [first, last). After decode is done +// successfully, return the bencoded string length in end. +SharedHandle decode +(std::string::const_iterator first, + std::string::const_iterator last, + size_t& end); SharedHandle decode -(const unsigned char* data, size_t length, size_t& end); +(const unsigned char* first, + const unsigned char* last); + +SharedHandle decode +(const unsigned char* first, + const unsigned char* last, + size_t& end); SharedHandle decodeFromFile(const std::string& filename); diff --git a/src/bittorrent_helper.cc b/src/bittorrent_helper.cc index 0346d96b..27f82443 100644 --- a/src/bittorrent_helper.cc +++ b/src/bittorrent_helper.cc @@ -544,7 +544,7 @@ void loadFromMemory(const unsigned char* content, const std::string& overrideName) { processRootDictionary(ctx, - bencode2::decode(content, length), + bencode2::decode(content, content+length), option, defaultName, overrideName, @@ -560,7 +560,7 @@ void loadFromMemory(const unsigned char* content, const std::string& overrideName) { processRootDictionary(ctx, - bencode2::decode(content, length), + bencode2::decode(content, content+length), option, defaultName, overrideName, @@ -575,7 +575,7 @@ void loadFromMemory(const std::string& context, { processRootDictionary (ctx, - bencode2::decode(context), + bencode2::decode(context.begin(), context.end()), option, defaultName, overrideName, std::vector()); @@ -590,7 +590,7 @@ void loadFromMemory(const std::string& context, { processRootDictionary (ctx, - bencode2::decode(context), + bencode2::decode(context.begin(), context.end()), option, defaultName, overrideName, uris); diff --git a/src/util.h b/src/util.h index 36cc0504..1af2c86a 100644 --- a/src/util.h +++ b/src/util.h @@ -112,7 +112,8 @@ namespace util { template void divide -(std::pair& hp, +(std::pair, + std::pair >& hp, InputIterator first, InputIterator last, char delim) @@ -248,7 +249,7 @@ bool parseIntNoThrow (int32_t& result, InputIterator first, InputIterator last, int base = 10) { // Without strip, strtol(" -1 ",..) emits error. - Scip p = util::stripIter(first, last); + std::pair p = util::stripIter(first, last); if(p.first == p.second) { return false; } @@ -277,7 +278,7 @@ bool parseUIntNoThrow (uint32_t& result, InputIterator first, InputIterator last, int base = 10) { // Without strip, strtol(" -1 ",..) emits error. - Scip p = util::stripIter(first, last); + std::pair p = util::stripIter(first, last); if(p.first == p.second) { return false; } @@ -309,7 +310,7 @@ bool parseLLIntNoThrow (int64_t& result, InputIterator first, InputIterator last, int base = 10) { // Without strip, strtol(" -1 ",..) emits error. - Scip p = util::stripIter(first, last); + std::pair p = util::stripIter(first, last); if(p.first == p.second) { return false; } @@ -335,7 +336,7 @@ bool parseLLIntNoThrow template int64_t parseLLInt(InputIterator first, InputIterator last, int base = 10) { - Scip p = util::stripIter(first, last); + std::pair p = util::stripIter(first, last); if(p.first == p.second) { throw DL_ABORT_EX(fmt(MSG_STRING_INTEGER_CONVERSION_FAILURE, "empty string")); @@ -373,7 +374,7 @@ int32_t parseInt(InputIterator first, InputIterator last, int base = 10) template uint64_t parseULLInt(InputIterator first, InputIterator last, int base = 10) { - Scip p = util::stripIter(first, last); + std::pair p = util::stripIter(first, last); if(p.first == p.second) { throw DL_ABORT_EX(fmt(MSG_STRING_INTEGER_CONVERSION_FAILURE, "empty string")); diff --git a/test/AnnounceListTest.cc b/test/AnnounceListTest.cc index da010891..5bb528e5 100644 --- a/test/AnnounceListTest.cc +++ b/test/AnnounceListTest.cc @@ -64,7 +64,8 @@ std::vector > toVector void AnnounceListTest::testSingleElementList() { std::string peersString = "ll8:tracker1el8:tracker2el8:tracker3ee"; - SharedHandle announcesList = bencode2::decode(peersString); + SharedHandle announcesList = + bencode2::decode(peersString.begin(), peersString.end()); // ANNOUNCE_LIST // [ [ tracker1 ], [ tracker2 ], [ tracker3 ] ] @@ -110,7 +111,8 @@ void AnnounceListTest::testSingleElementList() { void AnnounceListTest::testMultiElementList() { std::string peersString = "ll8:tracker18:tracker28:tracker3ee"; - SharedHandle announcesList = bencode2::decode(peersString); + SharedHandle announcesList = + bencode2::decode(peersString.begin(), peersString.end()); // ANNOUNCE_LIST // [ [ tracker1, tracker2, tracker3 ] ] @@ -143,7 +145,8 @@ void AnnounceListTest::testMultiElementList() { void AnnounceListTest::testSingleAndMulti() { std::string peersString = "ll8:tracker18:tracker2el8:tracker3ee"; - SharedHandle announcesList = bencode2::decode(peersString); + SharedHandle announcesList = + bencode2::decode(peersString.begin(), peersString.end()); // ANNOUNCE_LIST // [ [ tracker1, tracker2 ], [ tracker3 ] ] @@ -169,14 +172,16 @@ void AnnounceListTest::testSingleAndMulti() { void AnnounceListTest::testNoGroup() { std::string peersString = "llee"; - SharedHandle announcesList = bencode2::decode(peersString); + SharedHandle announcesList = + bencode2::decode(peersString.begin(), peersString.end()); AnnounceList announceList(toVector(downcast(announcesList))); CPPUNIT_ASSERT(announceList.countTier() == 0); } void AnnounceListTest::testNextEventIfAfterStarted() { std::string peersString = "ll8:tracker1ee"; - SharedHandle announcesList = bencode2::decode(peersString); + SharedHandle announcesList = + bencode2::decode(peersString.begin(), peersString.end()); // ANNOUNCE_LIST // [ [ tracker1 ] ] @@ -196,7 +201,8 @@ void AnnounceListTest::testNextEventIfAfterStarted() { void AnnounceListTest::testEvent() { std::string peersString = "ll8:tracker1el8:tracker2el8:tracker3ee"; - SharedHandle announcesList = bencode2::decode(peersString); + SharedHandle announcesList = + bencode2::decode(peersString.begin(), peersString.end()); // ANNOUNCE_LIST // [ [ tracker1 ], [ tracker2 ], [ tracker3 ] ] @@ -220,7 +226,8 @@ void AnnounceListTest::testEvent() { void AnnounceListTest::testCountStoppedAllowedTier() { std::string peersString = "ll8:tracker1el8:tracker2el8:tracker3ee"; - SharedHandle announcesList = bencode2::decode(peersString); + SharedHandle announcesList = + bencode2::decode(peersString.begin(), peersString.end()); // ANNOUNCE_LIST // [ [ tracker1 ], [ tracker2 ], [ tracker3 ] ] @@ -247,7 +254,8 @@ void AnnounceListTest::testCountStoppedAllowedTier() { void AnnounceListTest::testCountCompletedAllowedTier() { std::string peersString = "ll8:tracker1el8:tracker2el8:tracker3ee"; - SharedHandle announcesList = bencode2::decode(peersString); + SharedHandle announcesList = + bencode2::decode(peersString.begin(), peersString.end()); // ANNOUNCE_LIST // [ [ tracker1 ], [ tracker2 ], [ tracker3 ] ] diff --git a/test/Bencode2Test.cc b/test/Bencode2Test.cc index fb526464..88b0e5eb 100644 --- a/test/Bencode2Test.cc +++ b/test/Bencode2Test.cc @@ -27,8 +27,8 @@ void Bencode2Test::testDecode() { { // string, integer and list in dict - SharedHandle r = - bencode2::decode("d4:name5:aria24:sizei12345678900e5:filesl3:bin3:docee"); + std::string src = "d4:name5:aria24:sizei12345678900e5:filesl3:bin3:docee"; + SharedHandle r = bencode2::decode(src.begin(), src.end()); const Dict* dict = downcast(r); CPPUNIT_ASSERT(dict); CPPUNIT_ASSERT_EQUAL(std::string("aria2"), @@ -45,7 +45,8 @@ void Bencode2Test::testDecode() } { // dict in list - SharedHandle r = bencode2::decode("ld1:ki123eee"); + std::string src = "ld1:ki123eee"; + SharedHandle r = bencode2::decode(src.begin(), src.end()); const List* list = downcast(r); CPPUNIT_ASSERT(list); CPPUNIT_ASSERT_EQUAL(static_cast(1), list->size()); @@ -56,38 +57,44 @@ void Bencode2Test::testDecode() } { // empty key is allowed - SharedHandle s = bencode2::decode("d0:1:ve"); + std::string src = "d0:1:ve"; + SharedHandle s = bencode2::decode(src.begin(), src.end()); } { // empty string - SharedHandle s = bencode2::decode("0:"); + std::string src = "0:"; + SharedHandle s = bencode2::decode(src.begin(), src.end()); CPPUNIT_ASSERT_EQUAL(std::string(""), downcast(s)->s()); } { // empty dict - SharedHandle d = bencode2::decode("de"); + std::string src = "de"; + SharedHandle d = bencode2::decode(src.begin(), src.end()); CPPUNIT_ASSERT(downcast(d)->empty()); } { // empty list - SharedHandle l = bencode2::decode("le"); + std::string src = "le"; + SharedHandle l = bencode2::decode(src.begin(), src.end()); CPPUNIT_ASSERT(downcast(l)->empty()); } { // integer, without ending 'e' + std::string src = "i3"; try { - bencode2::decode("i3"); + bencode2::decode(src.begin(), src.end()); CPPUNIT_FAIL("exception must be thrown."); } catch(RecoverableException& e) { CPPUNIT_ASSERT_EQUAL(std::string("Bencode decoding failed:" - " Delimiter 'e' not found."), + " Integer expected but none found"), std::string(e.what())); } } { // dict, without ending 'e' + std::string src = "d"; try { - bencode2::decode("d"); + bencode2::decode(src.begin(), src.end()); CPPUNIT_FAIL("exception must be thrown."); } catch(RecoverableException& e) { CPPUNIT_ASSERT_EQUAL(std::string("Bencode decoding failed:" @@ -99,7 +106,8 @@ void Bencode2Test::testDecode() { // list, without ending 'e' try { - bencode2::decode("l"); + std::string src = "l"; + bencode2::decode(src.begin(), src.end()); CPPUNIT_FAIL("exception must be thrown."); } catch(RecoverableException& e) { CPPUNIT_ASSERT_EQUAL(std::string("Bencode decoding failed:" @@ -111,7 +119,8 @@ void Bencode2Test::testDecode() { // string, less than the specified length. try { - bencode2::decode("3:ab"); + std::string src = "3:ab"; + bencode2::decode(src.begin(), src.end()); CPPUNIT_FAIL("exception must be thrown."); } catch(RecoverableException& e) { CPPUNIT_ASSERT_EQUAL(std::string("Bencode decoding failed:" @@ -123,7 +132,8 @@ void Bencode2Test::testDecode() { // string, but length is invalid try { - bencode2::decode("x:abc"); + std::string src = "x:abc"; + bencode2::decode(src.begin(), src.end()); CPPUNIT_FAIL("exception must be thrown."); } catch(RecoverableException& e) { CPPUNIT_ASSERT_EQUAL(std::string("Bencode decoding failed:" @@ -134,8 +144,9 @@ void Bencode2Test::testDecode() } { // string with minus length + std::string src = "-1:a"; try { - bencode2::decode("-1:a"); + bencode2::decode(src.begin(), src.end()); CPPUNIT_FAIL("exception must be thrown."); } catch(RecoverableException& e) { CPPUNIT_ASSERT_EQUAL(std::string("Bencode decoding failed:" @@ -146,17 +157,20 @@ void Bencode2Test::testDecode() } { // 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. - SharedHandle s = bencode2::decode("5:aria2trail"); + std::string src = "5:aria2trail"; + SharedHandle s = bencode2::decode(src.begin(), src.end()); CPPUNIT_ASSERT_EQUAL(std::string("aria2"), downcast(s)->s()); } { // Get trailing garbage position + std::string src = "5:aria2trail"; size_t end; - SharedHandle s = bencode2::decode("5:aria2trail", end); + SharedHandle s = bencode2::decode(src.begin(), src.end(), end); CPPUNIT_ASSERT_EQUAL(std::string("aria2"), downcast(s)->s()); CPPUNIT_ASSERT_EQUAL((size_t)7, end); } @@ -173,7 +187,7 @@ void Bencode2Test::testDecode_overflow() s += "e"; } try { - bencode2::decode(s); + bencode2::decode(s.begin(), s.end()); CPPUNIT_FAIL("exception must be thrown."); } catch(RecoverableException& e) { // success diff --git a/test/BittorrentHelperTest.cc b/test/BittorrentHelperTest.cc index 58b0dc05..0e4c941d 100644 --- a/test/BittorrentHelperTest.cc +++ b/test/BittorrentHelperTest.cc @@ -746,7 +746,8 @@ void BittorrentHelperTest::testMetadata() { SharedHandle dctx(new DownloadContext()); load(A2_TEST_DIR"/test.torrent", dctx, option_); std::string torrentData = readFile(A2_TEST_DIR"/test.torrent"); - SharedHandle tr = bencode2::decode(torrentData); + SharedHandle tr = + bencode2::decode(torrentData.begin(), torrentData.end()); SharedHandle infoDic = downcast(tr)->get("info"); std::string metadata = bencode2::encode(infoDic); SharedHandle attrs = getTorrentAttrs(dctx); @@ -830,7 +831,8 @@ void BittorrentHelperTest::testExtractPeerFromString() std::string hextext = "100210354527354678541237324732171ae1"; hextext += "20010db8bd0501d2288a1fc0000110ee1ae2"; std::string peersstr = "36:"+util::fromHex(hextext); - SharedHandle str = bencode2::decode(peersstr); + SharedHandle str = + bencode2::decode(peersstr.begin(), peersstr.end()); std::deque > peers; extractPeer(str, AF_INET6, std::back_inserter(peers)); CPPUNIT_ASSERT_EQUAL((size_t)2, peers.size()); @@ -844,7 +846,7 @@ void BittorrentHelperTest::testExtractPeerFromString() hextext = "c0a800011ae1"; hextext += "c0a800021ae2"; peersstr = "12:"+util::fromHex(hextext); - str = bencode2::decode(peersstr); + str = bencode2::decode(peersstr.begin(), peersstr.end()); peers.clear(); extractPeer(str, AF_INET, std::back_inserter(peers)); 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" "4:porti2006eeee"; - SharedHandle dict = bencode2::decode(peersString); + SharedHandle dict = + bencode2::decode(peersString.begin(), peersString.end()); std::deque > peers; extractPeer(downcast(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:porti2007eeee"; - SharedHandle dict = bencode2::decode(peersString); + SharedHandle dict = + bencode2::decode(peersString.begin(), peersString.end()); std::deque > peers; extractPeer(downcast(dict)->get("peers"), AF_INET, std::back_inserter(peers)); diff --git a/test/UtilTest.cc b/test/UtilTest.cc index f81182f2..e6e330c8 100644 --- a/test/UtilTest.cc +++ b/test/UtilTest.cc @@ -268,7 +268,7 @@ void UtilTest::testLstripIter_char() } void UtilTest::testDivide() { - std::pair p1; + std::pair p1; std::string s = "name=value"; util::divide(p1, s.begin(), s.end(), '='); CPPUNIT_ASSERT_EQUAL(std::string("name"),