mirror of https://github.com/aria2/aria2
Made util::fromHex and base32::decode function template
parent
6ea1b68db1
commit
0c162dfbfb
|
@ -94,7 +94,7 @@ SharedHandle<LpdMessage> LpdMessageReceiver::receiveMessage()
|
||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
SharedHandle<HttpHeader> header = proc.getHttpRequestHeader();
|
SharedHandle<HttpHeader> header = proc.getHttpRequestHeader();
|
||||||
std::string infoHashString = header->getFirst("Infohash");
|
const std::string& infoHashString = header->getFirst("Infohash");
|
||||||
uint16_t port = header->getFirstAsUInt("Port");
|
uint16_t port = header->getFirstAsUInt("Port");
|
||||||
A2_LOG_INFO(fmt("LPD message received infohash=%s, port=%u from %s",
|
A2_LOG_INFO(fmt("LPD message received infohash=%s, port=%u from %s",
|
||||||
infoHashString.c_str(),
|
infoHashString.c_str(),
|
||||||
|
@ -102,7 +102,8 @@ SharedHandle<LpdMessage> LpdMessageReceiver::receiveMessage()
|
||||||
peerAddr.first.c_str()));
|
peerAddr.first.c_str()));
|
||||||
std::string infoHash;
|
std::string infoHash;
|
||||||
if(infoHashString.size() != 40 ||
|
if(infoHashString.size() != 40 ||
|
||||||
(infoHash = util::fromHex(infoHashString)).empty() ||
|
(infoHash = util::fromHex(infoHashString.begin(),
|
||||||
|
infoHashString.end())).empty() ||
|
||||||
port == 0) {
|
port == 0) {
|
||||||
A2_LOG_INFO(fmt("LPD bad request. infohash=%s", infoHashString.c_str()));
|
A2_LOG_INFO(fmt("LPD bad request. infohash=%s", infoHashString.c_str()));
|
||||||
msg.reset(new LpdMessage());
|
msg.reset(new LpdMessage());
|
||||||
|
|
|
@ -284,7 +284,7 @@ void MetalinkParserController::setHashOfChecksum(const std::string& md)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(MessageDigest::isValidHash(tChecksum_->getHashType(), md)) {
|
if(MessageDigest::isValidHash(tChecksum_->getHashType(), md)) {
|
||||||
tChecksum_->setDigest(util::fromHex(md));
|
tChecksum_->setDigest(util::fromHex(md.begin(), md.end()));
|
||||||
} else {
|
} else {
|
||||||
cancelChecksumTransaction();
|
cancelChecksumTransaction();
|
||||||
}
|
}
|
||||||
|
@ -360,7 +360,7 @@ void MetalinkParserController::addHashOfChunkChecksumV4(const std::string& md)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(MessageDigest::isValidHash(tChunkChecksumV4_->getHashType(), md)) {
|
if(MessageDigest::isValidHash(tChunkChecksumV4_->getHashType(), md)) {
|
||||||
tempChunkChecksumsV4_.push_back(util::fromHex(md));
|
tempChunkChecksumsV4_.push_back(util::fromHex(md.begin(), md.end()));
|
||||||
} else {
|
} else {
|
||||||
cancelChunkChecksumTransactionV4();
|
cancelChunkChecksumTransactionV4();
|
||||||
}
|
}
|
||||||
|
@ -463,7 +463,7 @@ void MetalinkParserController::setMessageDigestOfChunkChecksum(const std::string
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(MessageDigest::isValidHash(tChunkChecksum_->getHashType(), md)) {
|
if(MessageDigest::isValidHash(tChunkChecksum_->getHashType(), md)) {
|
||||||
tempHashPair_.second = util::fromHex(md);
|
tempHashPair_.second = util::fromHex(md.begin(), md.end());
|
||||||
} else {
|
} else {
|
||||||
cancelChunkChecksumTransaction();
|
cancelChunkChecksumTransaction();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1130,10 +1130,8 @@ void changeOption
|
||||||
std::pair<Scip, Scip> p;
|
std::pair<Scip, Scip> p;
|
||||||
util::divide(p, checksum.begin(), checksum.end(), '=');
|
util::divide(p, checksum.begin(), checksum.end(), '=');
|
||||||
std::string hashType(p.first.first, p.first.second);
|
std::string hashType(p.first.first, p.first.second);
|
||||||
std::string hexDigest(p.second.first, p.second.second);
|
|
||||||
util::lowercase(hashType);
|
util::lowercase(hashType);
|
||||||
util::lowercase(hexDigest);
|
dctx->setDigest(hashType, util::fromHex(p.second.first, p.second.second));
|
||||||
dctx->setDigest(hashType, util::fromHex(hexDigest));
|
|
||||||
}
|
}
|
||||||
if(option.defined(PREF_SELECT_FILE)) {
|
if(option.defined(PREF_SELECT_FILE)) {
|
||||||
SegList<int> sgl;
|
SegList<int> sgl;
|
||||||
|
|
|
@ -94,43 +94,6 @@ std::string encode(const std::string& src)
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string decode(const std::string& src)
|
|
||||||
{
|
|
||||||
std::string ret;
|
|
||||||
if(src.size()%8) {
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
bool done = false;
|
|
||||||
for(size_t i = 0; i < src.size() && !done; i += 8) {
|
|
||||||
uint64_t buf = 0;
|
|
||||||
size_t bits = 0;
|
|
||||||
for(size_t j = 0; j < 8; ++j) {
|
|
||||||
char ch = src[i+j];
|
|
||||||
unsigned char value;
|
|
||||||
if('A' <= ch && ch <= 'Z') {
|
|
||||||
value = ch-'A';
|
|
||||||
} else if('2' <= ch && ch <= '7') {
|
|
||||||
value = ch-'2'+26;
|
|
||||||
} else if(ch == '=') {
|
|
||||||
done = true;
|
|
||||||
break;
|
|
||||||
} else {
|
|
||||||
ret.clear();
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
buf <<= 5;
|
|
||||||
buf += value;
|
|
||||||
bits += 5;
|
|
||||||
}
|
|
||||||
buf >>= (bits%8);
|
|
||||||
bits = bits/8*8;
|
|
||||||
buf = hton64(buf);
|
|
||||||
char* p = reinterpret_cast<char*>(&buf);
|
|
||||||
ret.append(&p[(64-bits)/8], &p[8]);
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace base32
|
} // namespace base32
|
||||||
|
|
||||||
} // namespace aria2
|
} // namespace aria2
|
||||||
|
|
41
src/base32.h
41
src/base32.h
|
@ -39,13 +39,52 @@
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
namespace aria2 {
|
namespace aria2 {
|
||||||
|
|
||||||
namespace base32 {
|
namespace base32 {
|
||||||
|
|
||||||
std::string encode(const std::string& src);
|
std::string encode(const std::string& src);
|
||||||
|
|
||||||
std::string decode(const std::string& src);
|
template<typename InputIterator>
|
||||||
|
std::string decode(InputIterator first, InputIterator last)
|
||||||
|
{
|
||||||
|
std::string ret;
|
||||||
|
size_t len = last-first;
|
||||||
|
if(len%8) {
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
bool done = false;
|
||||||
|
for(; first != last && !done; first += 8) {
|
||||||
|
uint64_t buf = 0;
|
||||||
|
size_t bits = 0;
|
||||||
|
for(size_t i = 0; i < 8; ++i) {
|
||||||
|
char ch = *(first+i);
|
||||||
|
unsigned char value;
|
||||||
|
if('A' <= ch && ch <= 'Z') {
|
||||||
|
value = ch-'A';
|
||||||
|
} else if('2' <= ch && ch <= '7') {
|
||||||
|
value = ch-'2'+26;
|
||||||
|
} else if(ch == '=') {
|
||||||
|
done = true;
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
ret.clear();
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
buf <<= 5;
|
||||||
|
buf += value;
|
||||||
|
bits += 5;
|
||||||
|
}
|
||||||
|
buf >>= (bits%8);
|
||||||
|
bits = bits/8*8;
|
||||||
|
buf = hton64(buf);
|
||||||
|
char* p = reinterpret_cast<char*>(&buf);
|
||||||
|
ret.append(&p[(64-bits)/8], &p[8]);
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace base32
|
} // namespace base32
|
||||||
|
|
||||||
|
|
|
@ -881,15 +881,14 @@ SharedHandle<TorrentAttribute> parseMagnet(const std::string& magnet)
|
||||||
const String* xt = downcast<String>(*xtiter);
|
const String* xt = downcast<String>(*xtiter);
|
||||||
if(util::startsWith(xt->s().begin(), xt->s().end(),
|
if(util::startsWith(xt->s().begin(), xt->s().end(),
|
||||||
A2_URN_BTIH, vend(A2_URN_BTIH)-1)) {
|
A2_URN_BTIH, vend(A2_URN_BTIH)-1)) {
|
||||||
std::string xtarg(xt->s().begin()+9, xt->s().end());
|
size_t size = xt->s().end()-xt->s().begin()-9;
|
||||||
size_t size = xtarg.size();
|
|
||||||
if(size == 32) {
|
if(size == 32) {
|
||||||
std::string rawhash = base32::decode(xtarg);
|
std::string rawhash = base32::decode(xt->s().begin()+9, xt->s().end());
|
||||||
if(rawhash.size() == 20) {
|
if(rawhash.size() == 20) {
|
||||||
infoHash.swap(rawhash);
|
infoHash.swap(rawhash);
|
||||||
}
|
}
|
||||||
} else if(size == 40) {
|
} else if(size == 40) {
|
||||||
std::string rawhash = util::fromHex(xtarg);
|
std::string rawhash = util::fromHex(xt->s().begin()+9, xt->s().end());
|
||||||
if(!rawhash.empty()) {
|
if(!rawhash.empty()) {
|
||||||
infoHash.swap(rawhash);
|
infoHash.swap(rawhash);
|
||||||
}
|
}
|
||||||
|
|
21
src/util.cc
21
src/util.cc
|
@ -486,10 +486,8 @@ std::string toHex(const std::string& src)
|
||||||
return toHex(reinterpret_cast<const unsigned char*>(src.c_str()), src.size());
|
return toHex(reinterpret_cast<const unsigned char*>(src.c_str()), src.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace {
|
|
||||||
unsigned int hexCharToUInt(unsigned char ch)
|
unsigned int hexCharToUInt(unsigned char ch)
|
||||||
{
|
{
|
||||||
|
|
||||||
if('a' <= ch && ch <= 'f') {
|
if('a' <= ch && ch <= 'f') {
|
||||||
ch -= 'a';
|
ch -= 'a';
|
||||||
ch += 10;
|
ch += 10;
|
||||||
|
@ -503,25 +501,6 @@ unsigned int hexCharToUInt(unsigned char ch)
|
||||||
}
|
}
|
||||||
return ch;
|
return ch;
|
||||||
}
|
}
|
||||||
} // namespace
|
|
||||||
|
|
||||||
std::string fromHex(const std::string& src)
|
|
||||||
{
|
|
||||||
std::string dest;
|
|
||||||
if(src.size()%2) {
|
|
||||||
return dest;
|
|
||||||
}
|
|
||||||
for(size_t i = 0, eoi = src.size(); i < eoi; i += 2) {
|
|
||||||
unsigned char high = hexCharToUInt(src[i]);
|
|
||||||
unsigned char low = hexCharToUInt(src[i+1]);
|
|
||||||
if(high == 255 || low == 255) {
|
|
||||||
dest.clear();
|
|
||||||
return dest;
|
|
||||||
}
|
|
||||||
dest += (high*16+low);
|
|
||||||
}
|
|
||||||
return dest;
|
|
||||||
}
|
|
||||||
|
|
||||||
FILE* openFile(const std::string& filename, const std::string& mode) {
|
FILE* openFile(const std::string& filename, const std::string& mode) {
|
||||||
FILE* file = fopen(filename.c_str(), mode.c_str());
|
FILE* file = fopen(filename.c_str(), mode.c_str());
|
||||||
|
|
28
src/util.h
28
src/util.h
|
@ -233,10 +233,30 @@ std::string toHex(const char* src, size_t len);
|
||||||
|
|
||||||
std::string toHex(const std::string& src);
|
std::string toHex(const std::string& src);
|
||||||
|
|
||||||
// Converts hexadecimal ascii string 'src' into packed binary form and
|
unsigned int hexCharToUInt(unsigned char ch);
|
||||||
// return the result. If src is not well formed, then empty string is
|
|
||||||
// returned.
|
// Converts hexadecimal ascii characters in [first, last) into packed
|
||||||
std::string fromHex(const std::string& src);
|
// binary form and return the result. If characters in given range is
|
||||||
|
// not well formed, then empty string is returned.
|
||||||
|
template<typename InputIterator>
|
||||||
|
std::string fromHex(InputIterator first, InputIterator last)
|
||||||
|
{
|
||||||
|
std::string dest;
|
||||||
|
size_t len = last-first;
|
||||||
|
if(len%2) {
|
||||||
|
return dest;
|
||||||
|
}
|
||||||
|
for(; first != last; first += 2) {
|
||||||
|
unsigned char high = hexCharToUInt(*first);
|
||||||
|
unsigned char low = hexCharToUInt(*(first+1));
|
||||||
|
if(high == 255 || low == 255) {
|
||||||
|
dest.clear();
|
||||||
|
return dest;
|
||||||
|
}
|
||||||
|
dest += (high*16+low);
|
||||||
|
}
|
||||||
|
return dest;
|
||||||
|
}
|
||||||
|
|
||||||
FILE* openFile(const std::string& filename, const std::string& mode);
|
FILE* openFile(const std::string& filename, const std::string& mode);
|
||||||
|
|
||||||
|
|
|
@ -28,18 +28,26 @@ void Base32Test::testEncode()
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("GEZDG==="), base32::encode("123"));
|
CPPUNIT_ASSERT_EQUAL(std::string("GEZDG==="), base32::encode("123"));
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("GEZDGNA="), base32::encode("1234"));
|
CPPUNIT_ASSERT_EQUAL(std::string("GEZDGNA="), base32::encode("1234"));
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("GEZDGNBV"), base32::encode("12345"));
|
CPPUNIT_ASSERT_EQUAL(std::string("GEZDGNBV"), base32::encode("12345"));
|
||||||
|
std::string s = "248d0a1cd08284";
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("ESGQUHGQQKCA===="),
|
CPPUNIT_ASSERT_EQUAL(std::string("ESGQUHGQQKCA===="),
|
||||||
base32::encode(util::fromHex("248d0a1cd08284")));
|
base32::encode(util::fromHex(s.begin(), s.end())));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Base32Test::testDecode()
|
void Base32Test::testDecode()
|
||||||
{
|
{
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string(), base32::decode(""));
|
std::string s = "";
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("1"), base32::decode("GE======"));
|
CPPUNIT_ASSERT_EQUAL(std::string(), base32::decode(s.begin(), s.end()));
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("12"), base32::decode("GEZA===="));
|
s = "GE======";
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("123"), base32::decode("GEZDG==="));
|
CPPUNIT_ASSERT_EQUAL(std::string("1"), base32::decode(s.begin(), s.end()));
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("1234"), base32::decode("GEZDGNA="));
|
s = "GEZA====";
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("12345"), base32::decode("GEZDGNBV"));
|
CPPUNIT_ASSERT_EQUAL(std::string("12"), base32::decode(s.begin(), s.end()));
|
||||||
|
s = "GEZDG===";
|
||||||
|
CPPUNIT_ASSERT_EQUAL(std::string("123"), base32::decode(s.begin(), s.end()));
|
||||||
|
s = "GEZDGNA=";
|
||||||
|
CPPUNIT_ASSERT_EQUAL(std::string("1234"), base32::decode(s.begin(), s.end()));
|
||||||
|
s = "GEZDGNBV";
|
||||||
|
CPPUNIT_ASSERT_EQUAL(std::string("12345"),
|
||||||
|
base32::decode(s.begin(), s.end()));
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace aria2
|
} // namespace aria2
|
||||||
|
|
|
@ -417,7 +417,7 @@ void BittorrentHelperTest::testLoadFromMemory_multiFileNonUtf8Path()
|
||||||
{
|
{
|
||||||
SharedHandle<List> path = List::g();
|
SharedHandle<List> path = List::g();
|
||||||
path->append("path");
|
path->append("path");
|
||||||
path->append(util::fromHex("90a28a")+"E");
|
path->append(fromHex("90a28a")+"E");
|
||||||
SharedHandle<Dict> file = Dict::g();
|
SharedHandle<Dict> file = Dict::g();
|
||||||
file->put("length", Integer::g(1024));
|
file->put("length", Integer::g(1024));
|
||||||
file->put("path", path);
|
file->put("path", path);
|
||||||
|
@ -427,7 +427,7 @@ void BittorrentHelperTest::testLoadFromMemory_multiFileNonUtf8Path()
|
||||||
info->put("files", files);
|
info->put("files", files);
|
||||||
info->put("piece length", Integer::g(1024));
|
info->put("piece length", Integer::g(1024));
|
||||||
info->put("pieces", "01234567890123456789");
|
info->put("pieces", "01234567890123456789");
|
||||||
info->put("name", util::fromHex("1b")+"$B%O%m!<"+util::fromHex("1b")+"(B");
|
info->put("name", fromHex("1b")+"$B%O%m!<"+fromHex("1b")+"(B");
|
||||||
Dict dict;
|
Dict dict;
|
||||||
dict.put("info", info);
|
dict.put("info", info);
|
||||||
SharedHandle<DownloadContext> dctx(new DownloadContext());
|
SharedHandle<DownloadContext> dctx(new DownloadContext());
|
||||||
|
@ -446,7 +446,7 @@ void BittorrentHelperTest::testLoadFromMemory_singleFileNonUtf8Path()
|
||||||
SharedHandle<Dict> info = Dict::g();
|
SharedHandle<Dict> info = Dict::g();
|
||||||
info->put("piece length", Integer::g(1024));
|
info->put("piece length", Integer::g(1024));
|
||||||
info->put("pieces", "01234567890123456789");
|
info->put("pieces", "01234567890123456789");
|
||||||
info->put("name", util::fromHex("90a28a")+"E");
|
info->put("name", fromHex("90a28a")+"E");
|
||||||
info->put("length", Integer::g(1024));
|
info->put("length", Integer::g(1024));
|
||||||
Dict dict;
|
Dict dict;
|
||||||
dict.put("info", info);
|
dict.put("info", info);
|
||||||
|
@ -787,7 +787,7 @@ void BittorrentHelperTest::testParseMagnet()
|
||||||
void BittorrentHelperTest::testParseMagnet_base32()
|
void BittorrentHelperTest::testParseMagnet_base32()
|
||||||
{
|
{
|
||||||
std::string infoHash = "248d0a1cd08284299de78d5c1ed359bb46717d8c";
|
std::string infoHash = "248d0a1cd08284299de78d5c1ed359bb46717d8c";
|
||||||
std::string base32InfoHash = base32::encode(util::fromHex(infoHash));
|
std::string base32InfoHash = base32::encode(fromHex(infoHash));
|
||||||
std::string magnet = "magnet:?xt=urn:btih:"+base32InfoHash+"&dn=aria2";
|
std::string magnet = "magnet:?xt=urn:btih:"+base32InfoHash+"&dn=aria2";
|
||||||
SharedHandle<TorrentAttribute> attrs = bittorrent::parseMagnet(magnet);
|
SharedHandle<TorrentAttribute> attrs = bittorrent::parseMagnet(magnet);
|
||||||
CPPUNIT_ASSERT_EQUAL
|
CPPUNIT_ASSERT_EQUAL
|
||||||
|
@ -830,7 +830,7 @@ 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:"+fromHex(hextext);
|
||||||
SharedHandle<ValueBase> str =
|
SharedHandle<ValueBase> str =
|
||||||
bencode2::decode(peersstr.begin(), peersstr.end());
|
bencode2::decode(peersstr.begin(), peersstr.end());
|
||||||
std::deque<SharedHandle<Peer> > peers;
|
std::deque<SharedHandle<Peer> > peers;
|
||||||
|
@ -845,7 +845,7 @@ void BittorrentHelperTest::testExtractPeerFromString()
|
||||||
|
|
||||||
hextext = "c0a800011ae1";
|
hextext = "c0a800011ae1";
|
||||||
hextext += "c0a800021ae2";
|
hextext += "c0a800021ae2";
|
||||||
peersstr = "12:"+util::fromHex(hextext);
|
peersstr = "12:"+fromHex(hextext);
|
||||||
str = bencode2::decode(peersstr.begin(), peersstr.end());
|
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));
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
|
|
||||||
#include <cppunit/extensions/HelperMacros.h>
|
#include <cppunit/extensions/HelperMacros.h>
|
||||||
|
|
||||||
|
#include "TestUtil.h"
|
||||||
#include "Option.h"
|
#include "Option.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
#include "Exception.h"
|
#include "Exception.h"
|
||||||
|
@ -391,9 +392,9 @@ void DefaultBtAnnounceTest::testProcessAnnounceResponse()
|
||||||
"8:completei100e"
|
"8:completei100e"
|
||||||
"10:incompletei200e";
|
"10:incompletei200e";
|
||||||
res += "5:peers6:";
|
res += "5:peers6:";
|
||||||
res += util::fromHex("c0a800011ae1");
|
res += fromHex("c0a800011ae1");
|
||||||
res += "6:peers618:";
|
res += "6:peers618:";
|
||||||
res += util::fromHex("100210354527354678541237324732171ae1");
|
res += fromHex("100210354527354678541237324732171ae1");
|
||||||
res += "e";
|
res += "e";
|
||||||
|
|
||||||
DefaultBtAnnounce an(dctx_, option_);
|
DefaultBtAnnounce an(dctx_, option_);
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
#include <cppunit/extensions/HelperMacros.h>
|
#include <cppunit/extensions/HelperMacros.h>
|
||||||
|
|
||||||
|
#include "TestUtil.h"
|
||||||
#include "DownloadContext.h"
|
#include "DownloadContext.h"
|
||||||
#include "DefaultPieceStorage.h"
|
#include "DefaultPieceStorage.h"
|
||||||
#include "Option.h"
|
#include "Option.h"
|
||||||
|
@ -35,7 +36,7 @@ void IteratableChecksumValidatorTest::testValidate() {
|
||||||
SharedHandle<DownloadContext> dctx
|
SharedHandle<DownloadContext> dctx
|
||||||
(new DownloadContext(100, 250, A2_TEST_DIR"/chunkChecksumTestFile250.txt"));
|
(new DownloadContext(100, 250, A2_TEST_DIR"/chunkChecksumTestFile250.txt"));
|
||||||
dctx->setDigest("sha-1",
|
dctx->setDigest("sha-1",
|
||||||
util::fromHex("898a81b8e0181280ae2ee1b81e269196d91e869a"));
|
fromHex("898a81b8e0181280ae2ee1b81e269196d91e869a"));
|
||||||
SharedHandle<DefaultPieceStorage> ps(new DefaultPieceStorage(dctx, &option));
|
SharedHandle<DefaultPieceStorage> ps(new DefaultPieceStorage(dctx, &option));
|
||||||
ps->initStorage();
|
ps->initStorage();
|
||||||
ps->getDiskAdaptor()->enableReadOnly();
|
ps->getDiskAdaptor()->enableReadOnly();
|
||||||
|
@ -55,7 +56,7 @@ void IteratableChecksumValidatorTest::testValidate_fail() {
|
||||||
SharedHandle<DownloadContext> dctx
|
SharedHandle<DownloadContext> dctx
|
||||||
(new DownloadContext(100, 250, A2_TEST_DIR"/chunkChecksumTestFile250.txt"));
|
(new DownloadContext(100, 250, A2_TEST_DIR"/chunkChecksumTestFile250.txt"));
|
||||||
dctx->setDigest("sha-1",
|
dctx->setDigest("sha-1",
|
||||||
util::fromHex(std::string(40, '0'))); // set wrong checksum
|
fromHex(std::string(40, '0'))); // set wrong checksum
|
||||||
SharedHandle<DefaultPieceStorage> ps(new DefaultPieceStorage(dctx, &option));
|
SharedHandle<DefaultPieceStorage> ps(new DefaultPieceStorage(dctx, &option));
|
||||||
ps->initStorage();
|
ps->initStorage();
|
||||||
ps->getDiskAdaptor()->enableReadOnly();
|
ps->getDiskAdaptor()->enableReadOnly();
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
#include <cppunit/extensions/HelperMacros.h>
|
#include <cppunit/extensions/HelperMacros.h>
|
||||||
|
|
||||||
|
#include "TestUtil.h"
|
||||||
#include "DownloadContext.h"
|
#include "DownloadContext.h"
|
||||||
#include "DefaultPieceStorage.h"
|
#include "DefaultPieceStorage.h"
|
||||||
#include "Option.h"
|
#include "Option.h"
|
||||||
|
@ -32,9 +33,9 @@ public:
|
||||||
CPPUNIT_TEST_SUITE_REGISTRATION( IteratableChunkChecksumValidatorTest );
|
CPPUNIT_TEST_SUITE_REGISTRATION( IteratableChunkChecksumValidatorTest );
|
||||||
|
|
||||||
const std::string IteratableChunkChecksumValidatorTest::csArray[] =
|
const std::string IteratableChunkChecksumValidatorTest::csArray[] =
|
||||||
{ util::fromHex("29b0e7878271645fffb7eec7db4a7473a1c00bc1"),
|
{ fromHex("29b0e7878271645fffb7eec7db4a7473a1c00bc1"),
|
||||||
util::fromHex("4df75a661cb7eb2733d9cdaa7f772eae3a4e2976"),
|
fromHex("4df75a661cb7eb2733d9cdaa7f772eae3a4e2976"),
|
||||||
util::fromHex("0a4ea2f7dd7c52ddf2099a444ab2184b4d341bdb") };
|
fromHex("0a4ea2f7dd7c52ddf2099a444ab2184b4d341bdb") };
|
||||||
|
|
||||||
void IteratableChunkChecksumValidatorTest::testValidate() {
|
void IteratableChunkChecksumValidatorTest::testValidate() {
|
||||||
Option option;
|
Option option;
|
||||||
|
@ -60,7 +61,7 @@ void IteratableChunkChecksumValidatorTest::testValidate() {
|
||||||
|
|
||||||
// make the test fail
|
// make the test fail
|
||||||
std::deque<std::string> badHashes(&csArray[0], &csArray[3]);
|
std::deque<std::string> badHashes(&csArray[0], &csArray[3]);
|
||||||
badHashes[1] = util::fromHex("ffffffffffffffffffffffffffffffffffffffff");
|
badHashes[1] = fromHex("ffffffffffffffffffffffffffffffffffffffff");
|
||||||
dctx->setPieceHashes("sha-1", badHashes.begin(), badHashes.end());
|
dctx->setPieceHashes("sha-1", badHashes.begin(), badHashes.end());
|
||||||
|
|
||||||
validator.init();
|
validator.init();
|
||||||
|
@ -78,8 +79,8 @@ void IteratableChunkChecksumValidatorTest::testValidate_readError() {
|
||||||
SharedHandle<DownloadContext> dctx
|
SharedHandle<DownloadContext> dctx
|
||||||
(new DownloadContext(100, 500, A2_TEST_DIR"/chunkChecksumTestFile250.txt"));
|
(new DownloadContext(100, 500, A2_TEST_DIR"/chunkChecksumTestFile250.txt"));
|
||||||
std::deque<std::string> hashes(&csArray[0], &csArray[3]);
|
std::deque<std::string> hashes(&csArray[0], &csArray[3]);
|
||||||
hashes.push_back(util::fromHex("ffffffffffffffffffffffffffffffffffffffff"));
|
hashes.push_back(fromHex("ffffffffffffffffffffffffffffffffffffffff"));
|
||||||
hashes.push_back(util::fromHex("ffffffffffffffffffffffffffffffffffffffff"));
|
hashes.push_back(fromHex("ffffffffffffffffffffffffffffffffffffffff"));
|
||||||
dctx->setPieceHashes("sha-1", hashes.begin(), hashes.end());
|
dctx->setPieceHashes("sha-1", hashes.begin(), hashes.end());
|
||||||
SharedHandle<DefaultPieceStorage> ps(new DefaultPieceStorage(dctx, &option));
|
SharedHandle<DefaultPieceStorage> ps(new DefaultPieceStorage(dctx, &option));
|
||||||
ps->initStorage();
|
ps->initStorage();
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
|
|
||||||
#include <cppunit/extensions/HelperMacros.h>
|
#include <cppunit/extensions/HelperMacros.h>
|
||||||
|
|
||||||
|
#include "TestUtil.h"
|
||||||
#include "Exception.h"
|
#include "Exception.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
#include "LpdMessageDispatcher.h"
|
#include "LpdMessageDispatcher.h"
|
||||||
|
@ -37,7 +38,7 @@ void LpdMessageDispatcherTest::testCreateLpdRequest()
|
||||||
"Infohash: cd41c7fdddfd034a15a04d7ff881216e01c4ceaf\r\n"
|
"Infohash: cd41c7fdddfd034a15a04d7ff881216e01c4ceaf\r\n"
|
||||||
"\r\n\r\n"),
|
"\r\n\r\n"),
|
||||||
bittorrent::createLpdRequest("239.192.152.143", 6771,
|
bittorrent::createLpdRequest("239.192.152.143", 6771,
|
||||||
util::fromHex(infoHashString), 6000));
|
fromHex(infoHashString), 6000));
|
||||||
}
|
}
|
||||||
|
|
||||||
void LpdMessageDispatcherTest::testSendMessage()
|
void LpdMessageDispatcherTest::testSendMessage()
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
|
|
||||||
#include <cppunit/extensions/HelperMacros.h>
|
#include <cppunit/extensions/HelperMacros.h>
|
||||||
|
|
||||||
|
#include "TestUtil.h"
|
||||||
#include "Exception.h"
|
#include "Exception.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
#include "LpdMessageReceiver.h"
|
#include "LpdMessageReceiver.h"
|
||||||
|
@ -39,7 +40,7 @@ void LpdMessageReceiverTest::testReceiveMessage()
|
||||||
sendsock->setMulticastTtl(0);
|
sendsock->setMulticastTtl(0);
|
||||||
|
|
||||||
std::string infoHashString = "cd41c7fdddfd034a15a04d7ff881216e01c4ceaf";
|
std::string infoHashString = "cd41c7fdddfd034a15a04d7ff881216e01c4ceaf";
|
||||||
std::string infoHash = util::fromHex(infoHashString);
|
std::string infoHash = fromHex(infoHashString);
|
||||||
std::string request =
|
std::string request =
|
||||||
bittorrent::createLpdRequest(LPD_MULTICAST_ADDR, LPD_MULTICAST_PORT,
|
bittorrent::createLpdRequest(LPD_MULTICAST_ADDR, LPD_MULTICAST_PORT,
|
||||||
infoHash,
|
infoHash,
|
||||||
|
@ -59,7 +60,7 @@ void LpdMessageReceiverTest::testReceiveMessage()
|
||||||
std::string badInfoHashString = "cd41c7fdddfd034a15a04d7ff881216e01c4ce";
|
std::string badInfoHashString = "cd41c7fdddfd034a15a04d7ff881216e01c4ce";
|
||||||
request =
|
request =
|
||||||
bittorrent::createLpdRequest(LPD_MULTICAST_ADDR, LPD_MULTICAST_PORT,
|
bittorrent::createLpdRequest(LPD_MULTICAST_ADDR, LPD_MULTICAST_PORT,
|
||||||
util::fromHex(badInfoHashString),
|
fromHex(badInfoHashString),
|
||||||
6000);
|
6000);
|
||||||
sendsock->writeData(request.c_str(), request.size(),
|
sendsock->writeData(request.c_str(), request.size(),
|
||||||
LPD_MULTICAST_ADDR, LPD_MULTICAST_PORT);
|
LPD_MULTICAST_ADDR, LPD_MULTICAST_PORT);
|
||||||
|
|
|
@ -76,6 +76,11 @@ Cookie createCookie
|
||||||
(name, value, expiryTime, true, domain, hostOnly, path, secure, false, 0);
|
(name, value, expiryTime, true, domain, hostOnly, path, secure, false, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string fromHex(const std::string& s)
|
||||||
|
{
|
||||||
|
return util::fromHex(s.begin(), s.end());
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef ENABLE_MESSAGE_DIGEST
|
#ifdef ENABLE_MESSAGE_DIGEST
|
||||||
std::string fileHexDigest
|
std::string fileHexDigest
|
||||||
(const SharedHandle<MessageDigest>& ctx, const std::string& filename)
|
(const SharedHandle<MessageDigest>& ctx, const std::string& filename)
|
||||||
|
|
|
@ -42,6 +42,8 @@ Cookie createCookie
|
||||||
const std::string& path,
|
const std::string& path,
|
||||||
bool secure);
|
bool secure);
|
||||||
|
|
||||||
|
std::string fromHex(const std::string& s);
|
||||||
|
|
||||||
#ifdef ENABLE_MESSAGE_DIGEST
|
#ifdef ENABLE_MESSAGE_DIGEST
|
||||||
// Returns hex digest of contents of file denoted by filename.
|
// Returns hex digest of contents of file denoted by filename.
|
||||||
std::string fileHexDigest
|
std::string fileHexDigest
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
|
|
||||||
#include <cppunit/extensions/HelperMacros.h>
|
#include <cppunit/extensions/HelperMacros.h>
|
||||||
|
|
||||||
|
#include "TestUtil.h"
|
||||||
#include "Peer.h"
|
#include "Peer.h"
|
||||||
#include "a2netcompat.h"
|
#include "a2netcompat.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
@ -103,9 +104,9 @@ void UTPexExtensionMessageTest::testGetBencodedData()
|
||||||
|
|
||||||
std::string expected = "d5:added12:"+
|
std::string expected = "d5:added12:"+
|
||||||
std::string(&c1[0], &c1[6])+std::string(&c2[0], &c2[6])+
|
std::string(&c1[0], &c1[6])+std::string(&c2[0], &c2[6])+
|
||||||
"7:added.f2:"+util::fromHex("0200")+
|
"7:added.f2:"+fromHex("0200")+
|
||||||
"6:added618:"+std::string(&c5[0], &c5[COMPACT_LEN_IPV6])+
|
"6:added618:"+std::string(&c5[0], &c5[COMPACT_LEN_IPV6])+
|
||||||
"8:added6.f1:"+util::fromHex("00")+
|
"8:added6.f1:"+fromHex("00")+
|
||||||
"7:dropped12:"+std::string(&c3[0], &c3[6])+std::string(&c4[0], &c4[6])+
|
"7:dropped12:"+std::string(&c3[0], &c3[6])+std::string(&c4[0], &c4[6])+
|
||||||
"8:dropped618:"+std::string(&c6[0], &c6[COMPACT_LEN_IPV6])+
|
"8:dropped618:"+std::string(&c6[0], &c6[COMPACT_LEN_IPV6])+
|
||||||
"e";
|
"e";
|
||||||
|
@ -194,9 +195,9 @@ void UTPexExtensionMessageTest::testCreate()
|
||||||
|
|
||||||
std::string data = std::string(&id[0], &id[1])+"d5:added12:"+
|
std::string data = std::string(&id[0], &id[1])+"d5:added12:"+
|
||||||
std::string(&c1[0], &c1[6])+std::string(&c2[0], &c2[6])+
|
std::string(&c1[0], &c1[6])+std::string(&c2[0], &c2[6])+
|
||||||
"7:added.f2:"+util::fromHex("0200")+
|
"7:added.f2:"+fromHex("0200")+
|
||||||
"6:added618:"+std::string(&c5[0], &c5[COMPACT_LEN_IPV6])+
|
"6:added618:"+std::string(&c5[0], &c5[COMPACT_LEN_IPV6])+
|
||||||
"8:added6.f1:"+util::fromHex("00")+
|
"8:added6.f1:"+fromHex("00")+
|
||||||
"7:dropped12:"+std::string(&c3[0], &c3[6])+std::string(&c4[0], &c4[6])+
|
"7:dropped12:"+std::string(&c3[0], &c3[6])+std::string(&c4[0], &c4[6])+
|
||||||
"8:dropped618:"+std::string(&c6[0], &c6[COMPACT_LEN_IPV6])+
|
"8:dropped618:"+std::string(&c6[0], &c6[COMPACT_LEN_IPV6])+
|
||||||
"e";
|
"e";
|
||||||
|
|
|
@ -1553,17 +1553,17 @@ void UtilTest::testFromHex()
|
||||||
std::string dest;
|
std::string dest;
|
||||||
|
|
||||||
src = "0011fF";
|
src = "0011fF";
|
||||||
dest = util::fromHex(src);
|
dest = util::fromHex(src.begin(), src.end());
|
||||||
CPPUNIT_ASSERT_EQUAL((size_t)3, dest.size());
|
CPPUNIT_ASSERT_EQUAL((size_t)3, dest.size());
|
||||||
CPPUNIT_ASSERT_EQUAL((char)0x00, dest[0]);
|
CPPUNIT_ASSERT_EQUAL((char)0x00, dest[0]);
|
||||||
CPPUNIT_ASSERT_EQUAL((char)0x11, dest[1]);
|
CPPUNIT_ASSERT_EQUAL((char)0x11, dest[1]);
|
||||||
CPPUNIT_ASSERT_EQUAL((char)0xff, dest[2]);
|
CPPUNIT_ASSERT_EQUAL((char)0xff, dest[2]);
|
||||||
|
|
||||||
src = "0011f";
|
src = "0011f";
|
||||||
CPPUNIT_ASSERT(util::fromHex(src).empty());
|
CPPUNIT_ASSERT(util::fromHex(src.begin(), src.end()).empty());
|
||||||
|
|
||||||
src = "001g";
|
src = "001g";
|
||||||
CPPUNIT_ASSERT(util::fromHex(src).empty());
|
CPPUNIT_ASSERT(util::fromHex(src.begin(), src.end()).empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
void UtilTest::testParsePrioritizePieceRange()
|
void UtilTest::testParsePrioritizePieceRange()
|
||||||
|
@ -1720,31 +1720,31 @@ void UtilTest::testIsUtf8String()
|
||||||
CPPUNIT_ASSERT(util::isUtf8("ascii"));
|
CPPUNIT_ASSERT(util::isUtf8("ascii"));
|
||||||
// "Hello World" in Japanese UTF-8
|
// "Hello World" in Japanese UTF-8
|
||||||
CPPUNIT_ASSERT(util::isUtf8
|
CPPUNIT_ASSERT(util::isUtf8
|
||||||
(util::fromHex("e38193e38293e381abe381a1e381afe4b896e7958c")));
|
(fromHex("e38193e38293e381abe381a1e381afe4b896e7958c")));
|
||||||
// "World" in Shift_JIS
|
// "World" in Shift_JIS
|
||||||
CPPUNIT_ASSERT(!util::isUtf8(util::fromHex("90a28a")+"E"));
|
CPPUNIT_ASSERT(!util::isUtf8(fromHex("90a28a")+"E"));
|
||||||
// UTF8-2
|
// UTF8-2
|
||||||
CPPUNIT_ASSERT(util::isUtf8(util::fromHex("c280")));
|
CPPUNIT_ASSERT(util::isUtf8(fromHex("c280")));
|
||||||
CPPUNIT_ASSERT(util::isUtf8(util::fromHex("dfbf")));
|
CPPUNIT_ASSERT(util::isUtf8(fromHex("dfbf")));
|
||||||
// UTF8-3
|
// UTF8-3
|
||||||
CPPUNIT_ASSERT(util::isUtf8(util::fromHex("e0a080")));
|
CPPUNIT_ASSERT(util::isUtf8(fromHex("e0a080")));
|
||||||
CPPUNIT_ASSERT(util::isUtf8(util::fromHex("e0bf80")));
|
CPPUNIT_ASSERT(util::isUtf8(fromHex("e0bf80")));
|
||||||
CPPUNIT_ASSERT(util::isUtf8(util::fromHex("e18080")));
|
CPPUNIT_ASSERT(util::isUtf8(fromHex("e18080")));
|
||||||
CPPUNIT_ASSERT(util::isUtf8(util::fromHex("ec8080")));
|
CPPUNIT_ASSERT(util::isUtf8(fromHex("ec8080")));
|
||||||
CPPUNIT_ASSERT(util::isUtf8(util::fromHex("ed8080")));
|
CPPUNIT_ASSERT(util::isUtf8(fromHex("ed8080")));
|
||||||
CPPUNIT_ASSERT(util::isUtf8(util::fromHex("ed9f80")));
|
CPPUNIT_ASSERT(util::isUtf8(fromHex("ed9f80")));
|
||||||
CPPUNIT_ASSERT(util::isUtf8(util::fromHex("ee8080")));
|
CPPUNIT_ASSERT(util::isUtf8(fromHex("ee8080")));
|
||||||
CPPUNIT_ASSERT(util::isUtf8(util::fromHex("ef8080")));
|
CPPUNIT_ASSERT(util::isUtf8(fromHex("ef8080")));
|
||||||
// UTF8-4
|
// UTF8-4
|
||||||
CPPUNIT_ASSERT(util::isUtf8(util::fromHex("f0908080")));
|
CPPUNIT_ASSERT(util::isUtf8(fromHex("f0908080")));
|
||||||
CPPUNIT_ASSERT(util::isUtf8(util::fromHex("f0bf8080")));
|
CPPUNIT_ASSERT(util::isUtf8(fromHex("f0bf8080")));
|
||||||
CPPUNIT_ASSERT(util::isUtf8(util::fromHex("f1808080")));
|
CPPUNIT_ASSERT(util::isUtf8(fromHex("f1808080")));
|
||||||
CPPUNIT_ASSERT(util::isUtf8(util::fromHex("f3808080")));
|
CPPUNIT_ASSERT(util::isUtf8(fromHex("f3808080")));
|
||||||
CPPUNIT_ASSERT(util::isUtf8(util::fromHex("f4808080")));
|
CPPUNIT_ASSERT(util::isUtf8(fromHex("f4808080")));
|
||||||
CPPUNIT_ASSERT(util::isUtf8(util::fromHex("f48f8080")));
|
CPPUNIT_ASSERT(util::isUtf8(fromHex("f48f8080")));
|
||||||
|
|
||||||
CPPUNIT_ASSERT(util::isUtf8(""));
|
CPPUNIT_ASSERT(util::isUtf8(""));
|
||||||
CPPUNIT_ASSERT(!util::isUtf8(util::fromHex("00")));
|
CPPUNIT_ASSERT(!util::isUtf8(fromHex("00")));
|
||||||
}
|
}
|
||||||
|
|
||||||
void UtilTest::testNextParam()
|
void UtilTest::testNextParam()
|
||||||
|
|
Loading…
Reference in New Issue