2009-10-22 15:35:33 +00:00
|
|
|
#include "util.h"
|
2008-11-27 15:29:15 +00:00
|
|
|
|
2009-10-22 14:43:42 +00:00
|
|
|
#include <cstring>
|
2008-11-27 15:29:15 +00:00
|
|
|
#include <string>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
|
2007-11-04 12:26:12 +00:00
|
|
|
#include "FixedNumberRandomizer.h"
|
2007-11-10 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Don't connect server before checking file integrity at startup,
if
filesize and output file path are known.
* src/AbstractCommand.cc
* src/StreamFileAllocationEntry.cc
* src/Metalink2RequestGroup.cc
* src/RequestGroup.{h, cc}
* src/HttpResponseCommand.cc
* src/FtpNegotiationCommand.cc
Added DownloadFailureException. If it is thrown, RequestGroup
should
halt.
* src/AbstractCommand.cc
* src/DownloadFailureException.h
* src/RequestGroup.cc
Catch RecoverableException, instead of DlAbortEx.
* src/RequestGroupMan.cc
* src/FillRequestGroupCommand.cc
* src/MetaFileUtil.cc
* src/IteratableChunkChecksumValidator.cc
Now first parameter of MSG_DOWNLOAD_ABORTED is
gid(RequestGroup::
getGID())
* src/CheckIntegrityCommand.cc
* src/message.h
Print gid instead of idx.
* src/RequestGroupMan.cc
Removed exception throwers declaration.
* src/DirectDiskAdaptor.{h, cc}
* src/SocketCore.{h, cc}
* src/MultiDiskAdaptor.{h, cc}
* src/HttpConnection.{h, cc}
* src/HttpResponse.{h, cc}
* src/DiskAdaptor.{h, cc}
* src/CopyDiskAdaptor.{h, cc}
* src/MultiDiskAdaptor.{h, cc}
* src/HttpHeaderProcessor.{h, cc}
* src/AbstractSingleDiskAdaptor.{h, cc}
* src/Util.{h, cc}
* test/UtilTest.cc
* src/DefaultDiskWriter.{h, cc}
* src/FtpConnection.{h, cc}
* src/AbstractDiskWriter.{h, cc}
Removed duplicate code.
* src/StreamCheckIntegrityEntry.cc
Removed unnecessary include.
* src/DiskWriter.h
Included Exception.h
* src/option_processing.cc
Included 2 files and added doc
* src/TrackerWatcherCommand.cc
* src/SocketCore.cc (writeData): Fixed send error with GnuTLS.
2007-11-09 18:01:12 +00:00
|
|
|
#include "DlAbortEx.h"
|
2007-11-13 10:10:11 +00:00
|
|
|
#include "BitfieldMan.h"
|
2007-11-27 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Rewritten to add content-type support.
* src/DownloadHandler.{h, cc}
* src/BtPostDownloadHandler.{h, cc}
* test/BtPostDownloadHandlerTest.cc
* src/MetalinkPostDownloadHandler.{h, cc}
* test/MetalinkPostDownloadHandlerTest.cc
* src/PostDownloadHandler.{h, cc}
* src/DownloadHandlerConstants.{h, cc}
* src/RequestGroup.cc
* src/HttpResponseCommand.cc
* src/FtpNegotiationCommand.cc
* src/SingleFileDownloadContext.{h, cc}
* src/RequestGroup.h
* src/RequestGroupCriteria.h
* src/ContentTypeRequestGroupCriteria.h
Added 'mem' option value for --follow-metalink,
--follow-torrent.
If it is give, metalink/torrent file is not written to the disk,
but
just is kept in memory. Parsing is occurred on memory.
* src/MetalinkHelper.{h, cc}
* src/MetalinkProcessor.h
* src/Xml2MetalinkProcessor.{h, cc}
* test/Xml2MetalinkProcessorTest.cc
* src/DownloadHandlerFactory.{h, cc}
* test/DownloadHandlerFactoryTest.cc
* src/PreDownloadHandler.{h, cc}
* src/OptionHandlerFactory.cc
* src/DefaultBtContext.{h, cc}
* test/DefaultBtContextTest.cc
* src/version_usage.cc
* src/Metalink2RequestGroup.{h, cc}
* src/RequestGroup.{h, cc}
* src/a2functional.h
* test/a2functionalTest.cc
* src/MemoryBufferPreDownloadHandler.{h, cc}
* src/OptionHandlerImpl.h
* src/prefs.h
* src/Util.{h, cc}
* test/UtilTest.cc
Keep DownloadResult rather than RequestGroup after downloads to
reduce
memory usage.
* src/RequestGroupMan.{h, cc}
* src/DownloadEngine.cc
* src/BtDependency.{h, cc}: Changed the type of dependee from
WeakHandle to SharedHandle because WeakHandle could be null.
* src/RequestGroup.{h, cc}
* src/DownloadEngineFactory.cc
* src/DownloadResult.h
Set totalLength after download finished
* src/UnknownLengthPieceStorage.{h, cc}
Keep torrent file specified in metalink in memory.
* src/Metalink2RequestGroup.cc
* src/BtDependency.cc
* src/TrueRequestGroupCriteria.h
Fixed the bug: seekg is used where seekp should be used.
* src/ByteArrayDiskWriter.cc
* test/ByteArraydiskWriterTest.cc
2007-11-27 12:27:10 +00:00
|
|
|
#include "ByteArrayDiskWriter.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "FileEntry.h"
|
|
|
|
#include "File.h"
|
2009-03-04 15:53:56 +00:00
|
|
|
#include "array_fun.h"
|
2006-02-17 13:35:04 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
namespace aria2 {
|
2006-02-17 13:35:04 +00:00
|
|
|
|
|
|
|
class UtilTest:public CppUnit::TestFixture {
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE(UtilTest);
|
|
|
|
CPPUNIT_TEST(testTrim);
|
|
|
|
CPPUNIT_TEST(testSplit);
|
2009-10-18 12:31:07 +00:00
|
|
|
CPPUNIT_TEST(testSplit_many);
|
2006-02-21 12:27:17 +00:00
|
|
|
CPPUNIT_TEST(testEndsWith);
|
2006-02-21 15:01:05 +00:00
|
|
|
CPPUNIT_TEST(testReplace);
|
2006-02-28 02:25:45 +00:00
|
|
|
CPPUNIT_TEST(testStartsWith);
|
2006-05-09 15:54:14 +00:00
|
|
|
// may be moved to other helper class in the future.
|
|
|
|
CPPUNIT_TEST(testGetContentDispositionFilename);
|
2006-06-22 15:26:18 +00:00
|
|
|
CPPUNIT_TEST(testRandomAlpha);
|
2006-07-03 14:19:23 +00:00
|
|
|
CPPUNIT_TEST(testToUpper);
|
|
|
|
CPPUNIT_TEST(testToLower);
|
2006-08-28 12:40:41 +00:00
|
|
|
CPPUNIT_TEST(testUrldecode);
|
2007-03-26 12:16:57 +00:00
|
|
|
CPPUNIT_TEST(testGetRealSize);
|
2007-06-05 11:37:25 +00:00
|
|
|
CPPUNIT_TEST(testAbbrevSize);
|
2007-07-01 10:40:30 +00:00
|
|
|
CPPUNIT_TEST(testToStream);
|
2007-08-28 11:51:20 +00:00
|
|
|
CPPUNIT_TEST(testIsNumber);
|
|
|
|
CPPUNIT_TEST(testIsLowercase);
|
|
|
|
CPPUNIT_TEST(testIsUppercase);
|
|
|
|
CPPUNIT_TEST(testAlphaToNum);
|
2007-11-05 15:13:55 +00:00
|
|
|
CPPUNIT_TEST(testMkdirs);
|
2007-11-13 10:10:11 +00:00
|
|
|
CPPUNIT_TEST(testConvertBitfield);
|
2007-11-21 16:14:40 +00:00
|
|
|
CPPUNIT_TEST(testParseIntRange);
|
|
|
|
CPPUNIT_TEST(testParseIntRange_invalidRange);
|
|
|
|
CPPUNIT_TEST(testParseInt);
|
2008-03-09 15:03:47 +00:00
|
|
|
CPPUNIT_TEST(testParseUInt);
|
2007-11-21 16:14:40 +00:00
|
|
|
CPPUNIT_TEST(testParseLLInt);
|
2008-03-15 04:19:46 +00:00
|
|
|
CPPUNIT_TEST(testParseULLInt);
|
2007-11-27 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Rewritten to add content-type support.
* src/DownloadHandler.{h, cc}
* src/BtPostDownloadHandler.{h, cc}
* test/BtPostDownloadHandlerTest.cc
* src/MetalinkPostDownloadHandler.{h, cc}
* test/MetalinkPostDownloadHandlerTest.cc
* src/PostDownloadHandler.{h, cc}
* src/DownloadHandlerConstants.{h, cc}
* src/RequestGroup.cc
* src/HttpResponseCommand.cc
* src/FtpNegotiationCommand.cc
* src/SingleFileDownloadContext.{h, cc}
* src/RequestGroup.h
* src/RequestGroupCriteria.h
* src/ContentTypeRequestGroupCriteria.h
Added 'mem' option value for --follow-metalink,
--follow-torrent.
If it is give, metalink/torrent file is not written to the disk,
but
just is kept in memory. Parsing is occurred on memory.
* src/MetalinkHelper.{h, cc}
* src/MetalinkProcessor.h
* src/Xml2MetalinkProcessor.{h, cc}
* test/Xml2MetalinkProcessorTest.cc
* src/DownloadHandlerFactory.{h, cc}
* test/DownloadHandlerFactoryTest.cc
* src/PreDownloadHandler.{h, cc}
* src/OptionHandlerFactory.cc
* src/DefaultBtContext.{h, cc}
* test/DefaultBtContextTest.cc
* src/version_usage.cc
* src/Metalink2RequestGroup.{h, cc}
* src/RequestGroup.{h, cc}
* src/a2functional.h
* test/a2functionalTest.cc
* src/MemoryBufferPreDownloadHandler.{h, cc}
* src/OptionHandlerImpl.h
* src/prefs.h
* src/Util.{h, cc}
* test/UtilTest.cc
Keep DownloadResult rather than RequestGroup after downloads to
reduce
memory usage.
* src/RequestGroupMan.{h, cc}
* src/DownloadEngine.cc
* src/BtDependency.{h, cc}: Changed the type of dependee from
WeakHandle to SharedHandle because WeakHandle could be null.
* src/RequestGroup.{h, cc}
* src/DownloadEngineFactory.cc
* src/DownloadResult.h
Set totalLength after download finished
* src/UnknownLengthPieceStorage.{h, cc}
Keep torrent file specified in metalink in memory.
* src/Metalink2RequestGroup.cc
* src/BtDependency.cc
* src/TrueRequestGroupCriteria.h
Fixed the bug: seekg is used where seekp should be used.
* src/ByteArrayDiskWriter.cc
* test/ByteArraydiskWriterTest.cc
2007-11-27 12:27:10 +00:00
|
|
|
CPPUNIT_TEST(testToString_binaryStream);
|
2008-02-24 09:43:31 +00:00
|
|
|
CPPUNIT_TEST(testItos);
|
|
|
|
CPPUNIT_TEST(testUitos);
|
2008-08-24 07:55:34 +00:00
|
|
|
CPPUNIT_TEST(testNtoh64);
|
2008-11-27 15:29:15 +00:00
|
|
|
CPPUNIT_TEST(testUrlencode);
|
2009-01-25 09:58:40 +00:00
|
|
|
CPPUNIT_TEST(testHtmlEscape);
|
2009-03-04 15:53:56 +00:00
|
|
|
CPPUNIT_TEST(testJoinPath);
|
2009-03-07 03:10:53 +00:00
|
|
|
CPPUNIT_TEST(testParseIndexPath);
|
|
|
|
CPPUNIT_TEST(testCreateIndexPathMap);
|
2009-10-22 14:43:42 +00:00
|
|
|
CPPUNIT_TEST(testGenerateRandomData);
|
2009-11-22 14:30:51 +00:00
|
|
|
CPPUNIT_TEST(testFromHex);
|
2009-11-29 06:43:38 +00:00
|
|
|
CPPUNIT_TEST(testParsePrioritizePieceRange);
|
2010-01-11 14:01:20 +00:00
|
|
|
CPPUNIT_TEST(testApplyDir);
|
2010-01-11 14:32:20 +00:00
|
|
|
CPPUNIT_TEST(testFixTaintedBasename);
|
2010-01-28 14:25:16 +00:00
|
|
|
CPPUNIT_TEST(testIsNumericHost);
|
2006-02-17 13:35:04 +00:00
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
private:
|
|
|
|
|
|
|
|
public:
|
|
|
|
void setUp() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void testTrim();
|
|
|
|
void testSplit();
|
2009-10-18 12:31:07 +00:00
|
|
|
void testSplit_many();
|
2006-02-21 12:27:17 +00:00
|
|
|
void testEndsWith();
|
2006-02-21 15:01:05 +00:00
|
|
|
void testReplace();
|
2006-02-28 02:25:45 +00:00
|
|
|
void testStartsWith();
|
2006-05-09 15:54:14 +00:00
|
|
|
// may be moved to other helper class in the future.
|
|
|
|
void testGetContentDispositionFilename();
|
2006-06-22 15:26:18 +00:00
|
|
|
void testRandomAlpha();
|
2006-07-03 14:19:23 +00:00
|
|
|
void testToUpper();
|
|
|
|
void testToLower();
|
2006-08-28 12:40:41 +00:00
|
|
|
void testUrldecode();
|
2007-03-26 12:16:57 +00:00
|
|
|
void testGetRealSize();
|
2007-06-05 11:37:25 +00:00
|
|
|
void testAbbrevSize();
|
2007-07-01 10:40:30 +00:00
|
|
|
void testToStream();
|
2007-08-28 11:51:20 +00:00
|
|
|
void testIsNumber();
|
|
|
|
void testIsLowercase();
|
|
|
|
void testIsUppercase();
|
|
|
|
void testAlphaToNum();
|
2007-11-05 15:13:55 +00:00
|
|
|
void testMkdirs();
|
2007-11-13 10:10:11 +00:00
|
|
|
void testConvertBitfield();
|
2007-11-21 16:14:40 +00:00
|
|
|
void testParseIntRange();
|
|
|
|
void testParseIntRange_invalidRange();
|
|
|
|
void testParseInt();
|
2008-03-09 15:03:47 +00:00
|
|
|
void testParseUInt();
|
2007-11-21 16:14:40 +00:00
|
|
|
void testParseLLInt();
|
2008-03-15 04:19:46 +00:00
|
|
|
void testParseULLInt();
|
2007-11-27 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Rewritten to add content-type support.
* src/DownloadHandler.{h, cc}
* src/BtPostDownloadHandler.{h, cc}
* test/BtPostDownloadHandlerTest.cc
* src/MetalinkPostDownloadHandler.{h, cc}
* test/MetalinkPostDownloadHandlerTest.cc
* src/PostDownloadHandler.{h, cc}
* src/DownloadHandlerConstants.{h, cc}
* src/RequestGroup.cc
* src/HttpResponseCommand.cc
* src/FtpNegotiationCommand.cc
* src/SingleFileDownloadContext.{h, cc}
* src/RequestGroup.h
* src/RequestGroupCriteria.h
* src/ContentTypeRequestGroupCriteria.h
Added 'mem' option value for --follow-metalink,
--follow-torrent.
If it is give, metalink/torrent file is not written to the disk,
but
just is kept in memory. Parsing is occurred on memory.
* src/MetalinkHelper.{h, cc}
* src/MetalinkProcessor.h
* src/Xml2MetalinkProcessor.{h, cc}
* test/Xml2MetalinkProcessorTest.cc
* src/DownloadHandlerFactory.{h, cc}
* test/DownloadHandlerFactoryTest.cc
* src/PreDownloadHandler.{h, cc}
* src/OptionHandlerFactory.cc
* src/DefaultBtContext.{h, cc}
* test/DefaultBtContextTest.cc
* src/version_usage.cc
* src/Metalink2RequestGroup.{h, cc}
* src/RequestGroup.{h, cc}
* src/a2functional.h
* test/a2functionalTest.cc
* src/MemoryBufferPreDownloadHandler.{h, cc}
* src/OptionHandlerImpl.h
* src/prefs.h
* src/Util.{h, cc}
* test/UtilTest.cc
Keep DownloadResult rather than RequestGroup after downloads to
reduce
memory usage.
* src/RequestGroupMan.{h, cc}
* src/DownloadEngine.cc
* src/BtDependency.{h, cc}: Changed the type of dependee from
WeakHandle to SharedHandle because WeakHandle could be null.
* src/RequestGroup.{h, cc}
* src/DownloadEngineFactory.cc
* src/DownloadResult.h
Set totalLength after download finished
* src/UnknownLengthPieceStorage.{h, cc}
Keep torrent file specified in metalink in memory.
* src/Metalink2RequestGroup.cc
* src/BtDependency.cc
* src/TrueRequestGroupCriteria.h
Fixed the bug: seekg is used where seekp should be used.
* src/ByteArrayDiskWriter.cc
* test/ByteArraydiskWriterTest.cc
2007-11-27 12:27:10 +00:00
|
|
|
void testToString_binaryStream();
|
2008-02-24 09:43:31 +00:00
|
|
|
void testItos();
|
|
|
|
void testUitos();
|
2008-08-24 07:55:34 +00:00
|
|
|
void testNtoh64();
|
2008-11-27 15:29:15 +00:00
|
|
|
void testUrlencode();
|
2009-01-25 09:58:40 +00:00
|
|
|
void testHtmlEscape();
|
2009-03-04 15:53:56 +00:00
|
|
|
void testJoinPath();
|
2009-03-07 03:10:53 +00:00
|
|
|
void testParseIndexPath();
|
|
|
|
void testCreateIndexPathMap();
|
2009-10-22 14:43:42 +00:00
|
|
|
void testGenerateRandomData();
|
2009-11-22 14:30:51 +00:00
|
|
|
void testFromHex();
|
2009-11-29 06:43:38 +00:00
|
|
|
void testParsePrioritizePieceRange();
|
2010-01-11 14:01:20 +00:00
|
|
|
void testApplyDir();
|
2010-01-11 14:32:20 +00:00
|
|
|
void testFixTaintedBasename();
|
2010-01-28 14:25:16 +00:00
|
|
|
void testIsNumericHost();
|
2006-02-17 13:35:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION( UtilTest );
|
|
|
|
|
|
|
|
void UtilTest::testTrim() {
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string str1 = "aria2";
|
2009-10-22 15:09:00 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(str1, util::trim("aria2"));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(str1, util::trim(" aria2"));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(str1, util::trim(" aria2 "));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(str1, util::trim(" aria2 "));
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string str2 = "aria2 debut";
|
2009-10-22 15:09:00 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(str2, util::trim("aria2 debut"));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(str2, util::trim(" aria2 debut "));
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string str3 = "";
|
2009-10-22 15:09:00 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(str3, util::trim(""));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(str3, util::trim(" "));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(str3, util::trim(" "));
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string str4 = "A";
|
2009-10-22 15:09:00 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(str4, util::trim("A"));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(str4, util::trim(" A "));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(str4, util::trim(" A "));
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void UtilTest::testSplit() {
|
2008-02-08 15:53:45 +00:00
|
|
|
std::pair<std::string, std::string> p1;
|
2009-10-22 15:09:00 +00:00
|
|
|
util::split(p1, "name=value", '=');
|
2008-02-08 15:53:45 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("name"), p1.first);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("value"), p1.second);
|
2009-10-22 15:09:00 +00:00
|
|
|
util::split(p1, " name = value ", '=');
|
2008-02-08 15:53:45 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("name"), p1.first);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("value"), p1.second);
|
2009-10-22 15:09:00 +00:00
|
|
|
util::split(p1, "=value", '=');
|
2008-02-08 15:53:45 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(""), p1.first);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("value"), p1.second);
|
2009-10-22 15:09:00 +00:00
|
|
|
util::split(p1, "name=", '=');
|
2008-02-08 15:53:45 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("name"), p1.first);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(""), p1.second);
|
2009-10-22 15:09:00 +00:00
|
|
|
util::split(p1, "name", '=');
|
2008-02-08 15:53:45 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("name"), p1.first);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(""), p1.second);
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
|
|
|
|
2009-10-18 12:31:07 +00:00
|
|
|
void UtilTest::testSplit_many() {
|
2010-02-17 16:09:19 +00:00
|
|
|
std::vector<std::string> v1;
|
2009-10-22 15:09:00 +00:00
|
|
|
util::split("name1=value1; name2=value2; name3=value3",std::back_inserter(v1),
|
2010-01-05 16:01:46 +00:00
|
|
|
";", true);
|
2010-02-17 16:09:19 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)3, v1.size());
|
|
|
|
std::vector<std::string>::iterator itr = v1.begin();
|
2008-02-08 15:53:45 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("name1=value1"), *itr++);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("name2=value2"), *itr++);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("name3=value3"), *itr++);
|
2006-11-05 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
To divide TorrentMan into 6 classes: BtContext, BtRuntime,
PeerStorage, PieceStorage, BtAnnounce and BtProgressInfoFile
* src/TrackerWatcherComand.h: Made subclass of
BtContextAwareCommand.
* src/SeedCheckCommand.cc: Use pieceStorage, btRuntime
* src/PeerAbstractCommand.h: Made subclass of
BtContextAwareCommand.
* src/PeerAbstractCommand.cc: Use btRuntime.
* src/BtContextAwareCommand.h: New class.
* src/FileEntry.h: Added accessor methods for following
variables.
(path): Made private.
(length): Made private.
(offset): Made private.
(extracted): Made private.
(requested): Made private.
(FileEntries): New definition.
(FileEntryHandle): New definition.
* src/FileEntry.cc: New file.
* src/HaveEraseCommand.h: Made subclass of
BtContextAwareCommand.
* src/HaveEraseCommand.cc: Use btRuntime, pieceStorage.
* src/PeerChokeCommand.h: Made subclass of
BtContextAwareCommand.
* src/PeerChokeCommand.cc: Use btRuntime, peerStorage,
pieceStorage.
* src/PieceStorage.h: New file.
* src/PeerInteractionCommand.h: Use btContext.
* src/PeerInteractionCommand.cc: Use pieceStorage, peerStorage,
btRuntime.
* src/DefaultBtProgressInfoFile.h: New file.
* src/DefaultBtProgressInfoFile.cc: New file.
* src/File.cc
(Util.h): New include.
(mkdirs): New function.
* src/MultiDiskAdaptor.h
(mkdir): New function.
* src/PeerListProcessor.h
(Peers): Removed.
* src/PeerInteraction.h
(torrentMan): Removed.
(btContext): New variable.
(peerStorage): New variable.
(pieceStorage): New variable.
(btAnnounce): New variable.
(getTorrentMan): Removed.
(getBtContext): New function.
* src/PeerInteraction.cc: Use btContext, peerStorage,
pieceStorage,
btAnnounce.
* src/HandshakeMessage.h
(TorrentMan.h): Removed.
* src/HandshakeMessage.cc: Use btContext.
* src/DefaultBtAnnounce.cc: New file.
* src/MultiDiskWriter.cc: Use the accessor methods of FileEntry.
* src/DefaultPieceStorage.cc: New file.
* src/DefaultBtContext.h: New file.
* src/TorrentRequestInfo.cc: Use btContext, pieceStorage.
Use the accessor methods of FileEntry.
* src/CookieBox.cc: Updated to use Util::slice().
* src/PieceMessage.cc: Use btContext, pieceStorage.
* src/common.h (SharedHandle.h): New include.
* src/PeerMessage.cc (PeerMessage): Added btContext,
peerStorage,
pieceStorage.
* src/TorrentAutoSaveCommand.h: Made subclass of
BtContextAwareCommand.
* src/DiskAdaptor.h
(topDir): Removed.
(getFileEntryFromPath): Changed the return type to
FileEntryHandle.
(setTopDir): Removed.
(getTopDir): Removed.
* src/BtContext.h: New file.
* src/DefaultPeerStorage.h: New file.
* src/PieceMessage.h (TorrentMan.h): Removed.
* src/RequestMessage.h (TorrentMan.h): Removed.
* src/TorrentDownloadEngine.h
(uploadLength): New variable.
(btContext): New variable.
(btRuntime): New variable.
(pieceStorage): New variable.
(peerStorage): New variable.
(btAnnounce): New variable.
(btProgressInfoFile): New variable.
(torrentMan): Removed.
(setBtContext): New function.
* src/TorrentDownloadEngine.cc: Use BtContext, BtRuntime,
pieceStorage,
peerStorage, btAnnounce, btProgressInfoFile.
* src/Piece.h
(toString): New function.
(Pieces): New type definition.
* src/Peer.h
(active): New variable.
(Peer): Added active.
(activate): Set active to true.
(deactivate): Set active to false.
(isActive): New function.
(Peers): New type definition.
* src/DirectDiskAdaptor.cc
(getFilePath): Use the accessor methods of FileEntry.
* src/TorrentConsoleDownloadEngine.h
(afterEachIteration): New function.
* src/TorrentConsoleDownloadEngine.cc
(haltRequested): New variable.
(sendStatistics): Use pieceStorage, btRuntime.
(afterEachIteration): New function.
* src/AnnounceList: AnnounceTier->AnnounceTierHandle.
* src/Directry.h
(Directory): New function.
(DirectoryHandle): New type definition.
* src/BtProgressInfoFile.h: New file.
* src/RequestMessage.cc: Use pieceStorage.
* src/BtRuntime.h: New file.
* src/DefaultBtContext.cc: New file.
* src/BitfieldMan.h
(getCompletedLength): New function(private).
(getCompletedLength): New function.
(getFilteredCompletedLength): New function.
* src/BitfieldMan.h
(getCompletedLength): New function(private).
(getCompletedLength): New function.
(getFilteredCompletedLength): New function.
* src/MultiDiskAdaptor.cc
(mkdir): New function.
(openFile): Call mkdir().
(initAndOpenFile): Call mkdir().
* src/CancelMessage.h
(TorrentMan.h): Removed.
* src/RejectMessage.h
(TorrentMan.h): Removed.
* src/DownloadEngineFactory.cc
(DefaultPieceStorage.h): New include.
(DefaultPeerStorage.h): New include.
(DefaultBtAnnounce.h): New include.
(DefaultBtProgressInfoFile.h): New include.
(newTorrentConsoleEngine): Rewritten.
* src/ShareRatioSeedCriteria.h
(torrentMan): Removed.
(btContext): New variable.
(peerStorage): New variable.
(btRuntime): New variable.
(evaluate): Use btContext, btRuntime, peerStorage.
* src/AnnounceTier.h: New file.
* src/BtAnnounce.h: New file.
* src/BtRegistry.h: New file.
* src/PeerInitiateConnectionCommand.h: Added btContext.
* src/PeerConnection.h (TorrentMan.h): Removed.
* src/PeerMessageFactory.cc: Use btContext, pieceStorage.
* src/Util.h
(slice): Added an argument.
* src/Util.cc
(slice): Added an argument to control whether trim is made or
not.
* src/PeerStorage.h: New file.
* src/BtRegistry.cc: New file.
* src/TrackerUpdateCommand.h: Made subclass of
BtContextAwareCommand.
* src/CopyDiskAdaptor.cc: Use the accessor methods of FileEntry.
* src/MultiDiskWriter.h: FileEntry -> FileEntryHandle
* src/PeerListenCommand.cc: Use btRuntime, peerStorage,
btContext.
* src/TorrentRequestInfo.h
(e): Removed.
(showFileEntry): Added an argument.
(getDownloadEngine): Return 0.
* src/DefaultBtAnnounce.h: New file.
* src/TorrentAutoSaveCommand.cc: Use btRuntime,
btProgressInfoFile.
* src/TrackerWatcherComand.cc: Use btRuntime, btAnnounce,
* src/PeerMessageFactory.h
(btContext): New variable.
(pieceStorage): New variable.
* src/TrackerUpdateCommand.cc: Use btRuntime, peerStorage,
btContext,
btAnnounce.
* src/DiskAdaptor.cc
(DiskAdaptor): Removed topDir.
(~DiskAdaptor): Removed topDir.
* src/PeerListenCommand.h: Made subclass of
BtContextAwareCommand.
* src/SeedCheckCommand.h: Made subclass of
BtContextAwareCommand.
* src/File.h (mkdirs): New function.
* src/DefaultPeerStorage): New file.
* src/DownloadEngineFactory.h
(newTorrentConsoleEngine): Use btContext.
* src/BtContextAwareCommand.cc: New file.
* src/PeerInitiateConnectionCommand.cc: Use btRuntime,
peerStorage.
* src/PeerMessage.h
(btContext): New variable.
(peerStorage): New variable.
(pieceStorage): New variable.
(setBtContext): New function.
* src/Directry.cc
(Directory): New function.
(createDir): Do nothing if name.size() == 0.
* src/AnnounceList.h
(AnnounceTier): Removed.
(AnnounceTiers): Removed.
* src/DefaultPieceStorage.h: New file.
* src/Piece.cc (toString): New function.
To fix typo:
* src/main.cc (showVersion): Fixed typo.
To fix compile warning:
* src/DelegatingPeerListProcessor.cc
(canHandle): Added "return false".
2006-11-05 15:04:17 +00:00
|
|
|
|
|
|
|
v1.clear();
|
|
|
|
|
2009-10-22 15:09:00 +00:00
|
|
|
util::split("name1=value1; name2=value2; name3=value3",std::back_inserter(v1),
|
2010-01-05 16:01:46 +00:00
|
|
|
";", false);
|
2010-02-17 16:09:19 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)3, v1.size());
|
2006-11-05 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
To divide TorrentMan into 6 classes: BtContext, BtRuntime,
PeerStorage, PieceStorage, BtAnnounce and BtProgressInfoFile
* src/TrackerWatcherComand.h: Made subclass of
BtContextAwareCommand.
* src/SeedCheckCommand.cc: Use pieceStorage, btRuntime
* src/PeerAbstractCommand.h: Made subclass of
BtContextAwareCommand.
* src/PeerAbstractCommand.cc: Use btRuntime.
* src/BtContextAwareCommand.h: New class.
* src/FileEntry.h: Added accessor methods for following
variables.
(path): Made private.
(length): Made private.
(offset): Made private.
(extracted): Made private.
(requested): Made private.
(FileEntries): New definition.
(FileEntryHandle): New definition.
* src/FileEntry.cc: New file.
* src/HaveEraseCommand.h: Made subclass of
BtContextAwareCommand.
* src/HaveEraseCommand.cc: Use btRuntime, pieceStorage.
* src/PeerChokeCommand.h: Made subclass of
BtContextAwareCommand.
* src/PeerChokeCommand.cc: Use btRuntime, peerStorage,
pieceStorage.
* src/PieceStorage.h: New file.
* src/PeerInteractionCommand.h: Use btContext.
* src/PeerInteractionCommand.cc: Use pieceStorage, peerStorage,
btRuntime.
* src/DefaultBtProgressInfoFile.h: New file.
* src/DefaultBtProgressInfoFile.cc: New file.
* src/File.cc
(Util.h): New include.
(mkdirs): New function.
* src/MultiDiskAdaptor.h
(mkdir): New function.
* src/PeerListProcessor.h
(Peers): Removed.
* src/PeerInteraction.h
(torrentMan): Removed.
(btContext): New variable.
(peerStorage): New variable.
(pieceStorage): New variable.
(btAnnounce): New variable.
(getTorrentMan): Removed.
(getBtContext): New function.
* src/PeerInteraction.cc: Use btContext, peerStorage,
pieceStorage,
btAnnounce.
* src/HandshakeMessage.h
(TorrentMan.h): Removed.
* src/HandshakeMessage.cc: Use btContext.
* src/DefaultBtAnnounce.cc: New file.
* src/MultiDiskWriter.cc: Use the accessor methods of FileEntry.
* src/DefaultPieceStorage.cc: New file.
* src/DefaultBtContext.h: New file.
* src/TorrentRequestInfo.cc: Use btContext, pieceStorage.
Use the accessor methods of FileEntry.
* src/CookieBox.cc: Updated to use Util::slice().
* src/PieceMessage.cc: Use btContext, pieceStorage.
* src/common.h (SharedHandle.h): New include.
* src/PeerMessage.cc (PeerMessage): Added btContext,
peerStorage,
pieceStorage.
* src/TorrentAutoSaveCommand.h: Made subclass of
BtContextAwareCommand.
* src/DiskAdaptor.h
(topDir): Removed.
(getFileEntryFromPath): Changed the return type to
FileEntryHandle.
(setTopDir): Removed.
(getTopDir): Removed.
* src/BtContext.h: New file.
* src/DefaultPeerStorage.h: New file.
* src/PieceMessage.h (TorrentMan.h): Removed.
* src/RequestMessage.h (TorrentMan.h): Removed.
* src/TorrentDownloadEngine.h
(uploadLength): New variable.
(btContext): New variable.
(btRuntime): New variable.
(pieceStorage): New variable.
(peerStorage): New variable.
(btAnnounce): New variable.
(btProgressInfoFile): New variable.
(torrentMan): Removed.
(setBtContext): New function.
* src/TorrentDownloadEngine.cc: Use BtContext, BtRuntime,
pieceStorage,
peerStorage, btAnnounce, btProgressInfoFile.
* src/Piece.h
(toString): New function.
(Pieces): New type definition.
* src/Peer.h
(active): New variable.
(Peer): Added active.
(activate): Set active to true.
(deactivate): Set active to false.
(isActive): New function.
(Peers): New type definition.
* src/DirectDiskAdaptor.cc
(getFilePath): Use the accessor methods of FileEntry.
* src/TorrentConsoleDownloadEngine.h
(afterEachIteration): New function.
* src/TorrentConsoleDownloadEngine.cc
(haltRequested): New variable.
(sendStatistics): Use pieceStorage, btRuntime.
(afterEachIteration): New function.
* src/AnnounceList: AnnounceTier->AnnounceTierHandle.
* src/Directry.h
(Directory): New function.
(DirectoryHandle): New type definition.
* src/BtProgressInfoFile.h: New file.
* src/RequestMessage.cc: Use pieceStorage.
* src/BtRuntime.h: New file.
* src/DefaultBtContext.cc: New file.
* src/BitfieldMan.h
(getCompletedLength): New function(private).
(getCompletedLength): New function.
(getFilteredCompletedLength): New function.
* src/BitfieldMan.h
(getCompletedLength): New function(private).
(getCompletedLength): New function.
(getFilteredCompletedLength): New function.
* src/MultiDiskAdaptor.cc
(mkdir): New function.
(openFile): Call mkdir().
(initAndOpenFile): Call mkdir().
* src/CancelMessage.h
(TorrentMan.h): Removed.
* src/RejectMessage.h
(TorrentMan.h): Removed.
* src/DownloadEngineFactory.cc
(DefaultPieceStorage.h): New include.
(DefaultPeerStorage.h): New include.
(DefaultBtAnnounce.h): New include.
(DefaultBtProgressInfoFile.h): New include.
(newTorrentConsoleEngine): Rewritten.
* src/ShareRatioSeedCriteria.h
(torrentMan): Removed.
(btContext): New variable.
(peerStorage): New variable.
(btRuntime): New variable.
(evaluate): Use btContext, btRuntime, peerStorage.
* src/AnnounceTier.h: New file.
* src/BtAnnounce.h: New file.
* src/BtRegistry.h: New file.
* src/PeerInitiateConnectionCommand.h: Added btContext.
* src/PeerConnection.h (TorrentMan.h): Removed.
* src/PeerMessageFactory.cc: Use btContext, pieceStorage.
* src/Util.h
(slice): Added an argument.
* src/Util.cc
(slice): Added an argument to control whether trim is made or
not.
* src/PeerStorage.h: New file.
* src/BtRegistry.cc: New file.
* src/TrackerUpdateCommand.h: Made subclass of
BtContextAwareCommand.
* src/CopyDiskAdaptor.cc: Use the accessor methods of FileEntry.
* src/MultiDiskWriter.h: FileEntry -> FileEntryHandle
* src/PeerListenCommand.cc: Use btRuntime, peerStorage,
btContext.
* src/TorrentRequestInfo.h
(e): Removed.
(showFileEntry): Added an argument.
(getDownloadEngine): Return 0.
* src/DefaultBtAnnounce.h: New file.
* src/TorrentAutoSaveCommand.cc: Use btRuntime,
btProgressInfoFile.
* src/TrackerWatcherComand.cc: Use btRuntime, btAnnounce,
* src/PeerMessageFactory.h
(btContext): New variable.
(pieceStorage): New variable.
* src/TrackerUpdateCommand.cc: Use btRuntime, peerStorage,
btContext,
btAnnounce.
* src/DiskAdaptor.cc
(DiskAdaptor): Removed topDir.
(~DiskAdaptor): Removed topDir.
* src/PeerListenCommand.h: Made subclass of
BtContextAwareCommand.
* src/SeedCheckCommand.h: Made subclass of
BtContextAwareCommand.
* src/File.h (mkdirs): New function.
* src/DefaultPeerStorage): New file.
* src/DownloadEngineFactory.h
(newTorrentConsoleEngine): Use btContext.
* src/BtContextAwareCommand.cc: New file.
* src/PeerInitiateConnectionCommand.cc: Use btRuntime,
peerStorage.
* src/PeerMessage.h
(btContext): New variable.
(peerStorage): New variable.
(pieceStorage): New variable.
(setBtContext): New function.
* src/Directry.cc
(Directory): New function.
(createDir): Do nothing if name.size() == 0.
* src/AnnounceList.h
(AnnounceTier): Removed.
(AnnounceTiers): Removed.
* src/DefaultPieceStorage.h: New file.
* src/Piece.cc (toString): New function.
To fix typo:
* src/main.cc (showVersion): Fixed typo.
To fix compile warning:
* src/DelegatingPeerListProcessor.cc
(canHandle): Added "return false".
2006-11-05 15:04:17 +00:00
|
|
|
itr = v1.begin();
|
2008-02-08 15:53:45 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("name1=value1"), *itr++);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(" name2=value2"), *itr++);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(" name3=value3"), *itr++);
|
2010-02-17 16:09:19 +00:00
|
|
|
|
|
|
|
v1.clear();
|
|
|
|
|
|
|
|
util::split("k=v", std::back_inserter(v1), ";", false, true);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1, v1.size());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("k=v"), v1[0]);
|
|
|
|
|
|
|
|
v1.clear();
|
|
|
|
|
|
|
|
util::split(" ", std::back_inserter(v1), ";", true, true);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1, v1.size());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(""), v1[0]);
|
|
|
|
|
|
|
|
v1.clear();
|
|
|
|
|
|
|
|
util::split(" ", std::back_inserter(v1), ";", true);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)0, v1.size());
|
2006-02-17 13:35:04 +00:00
|
|
|
}
|
2006-02-21 12:27:17 +00:00
|
|
|
|
|
|
|
void UtilTest::testEndsWith() {
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string target = "abcdefg";
|
|
|
|
std::string part = "fg";
|
2009-10-22 15:09:00 +00:00
|
|
|
CPPUNIT_ASSERT(util::endsWith(target, part));
|
2006-02-21 12:27:17 +00:00
|
|
|
|
|
|
|
target = "abdefg";
|
|
|
|
part = "g";
|
2009-10-22 15:09:00 +00:00
|
|
|
CPPUNIT_ASSERT(util::endsWith(target, part));
|
2006-02-21 12:27:17 +00:00
|
|
|
|
|
|
|
target = "abdefg";
|
|
|
|
part = "eg";
|
2009-10-22 15:09:00 +00:00
|
|
|
CPPUNIT_ASSERT(!util::endsWith(target, part));
|
2006-02-21 12:27:17 +00:00
|
|
|
|
|
|
|
target = "g";
|
|
|
|
part = "eg";
|
2009-10-22 15:09:00 +00:00
|
|
|
CPPUNIT_ASSERT(!util::endsWith(target, part));
|
2006-02-21 12:27:17 +00:00
|
|
|
|
|
|
|
target = "g";
|
|
|
|
part = "g";
|
2009-10-22 15:09:00 +00:00
|
|
|
CPPUNIT_ASSERT(util::endsWith(target, part));
|
2006-02-21 12:27:17 +00:00
|
|
|
|
|
|
|
target = "g";
|
|
|
|
part = "";
|
2009-10-22 15:09:00 +00:00
|
|
|
CPPUNIT_ASSERT(util::endsWith(target, part));
|
2006-02-21 12:27:17 +00:00
|
|
|
|
|
|
|
target = "";
|
|
|
|
part = "";
|
2009-10-22 15:09:00 +00:00
|
|
|
CPPUNIT_ASSERT(util::endsWith(target, part));
|
2006-02-21 12:27:17 +00:00
|
|
|
|
|
|
|
target = "";
|
|
|
|
part = "g";
|
2009-10-22 15:09:00 +00:00
|
|
|
CPPUNIT_ASSERT(!util::endsWith(target, part));
|
2006-02-21 12:27:17 +00:00
|
|
|
}
|
2006-02-21 15:01:05 +00:00
|
|
|
|
|
|
|
void UtilTest::testReplace() {
|
2009-10-22 15:09:00 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("abc\n"), util::replace("abc\r\n", "\r", ""));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("abc"), util::replace("abc\r\n", "\r\n", ""));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(""), util::replace("", "\r\n", ""));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("abc"), util::replace("abc", "", "a"));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("xbc"), util::replace("abc", "a", "x"));
|
2006-02-21 15:01:05 +00:00
|
|
|
}
|
2006-02-28 02:25:45 +00:00
|
|
|
|
|
|
|
void UtilTest::testStartsWith() {
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string target;
|
|
|
|
std::string part;
|
2006-02-28 02:25:45 +00:00
|
|
|
|
|
|
|
target = "abcdefg";
|
|
|
|
part = "abc";
|
2009-10-22 15:09:00 +00:00
|
|
|
CPPUNIT_ASSERT(util::startsWith(target, part));
|
2006-02-28 02:25:45 +00:00
|
|
|
|
|
|
|
target = "abcdefg";
|
|
|
|
part = "abx";
|
2009-10-22 15:09:00 +00:00
|
|
|
CPPUNIT_ASSERT(!util::startsWith(target, part));
|
2006-02-28 02:25:45 +00:00
|
|
|
|
|
|
|
target = "abcdefg";
|
|
|
|
part = "bcd";
|
2009-10-22 15:09:00 +00:00
|
|
|
CPPUNIT_ASSERT(!util::startsWith(target, part));
|
2006-02-28 02:25:45 +00:00
|
|
|
|
|
|
|
target = "";
|
|
|
|
part = "a";
|
2009-10-22 15:09:00 +00:00
|
|
|
CPPUNIT_ASSERT(!util::startsWith(target, part));
|
2006-02-28 02:25:45 +00:00
|
|
|
|
|
|
|
target = "";
|
|
|
|
part = "";
|
2009-10-22 15:09:00 +00:00
|
|
|
CPPUNIT_ASSERT(util::startsWith(target, part));
|
2006-02-28 02:25:45 +00:00
|
|
|
|
|
|
|
target = "a";
|
|
|
|
part = "";
|
2009-10-22 15:09:00 +00:00
|
|
|
CPPUNIT_ASSERT(util::startsWith(target, part));
|
2006-02-28 02:25:45 +00:00
|
|
|
|
|
|
|
target = "a";
|
|
|
|
part = "a";
|
2009-10-22 15:09:00 +00:00
|
|
|
CPPUNIT_ASSERT(util::startsWith(target, part));
|
2006-02-28 02:25:45 +00:00
|
|
|
|
|
|
|
}
|
2006-05-09 15:54:14 +00:00
|
|
|
|
|
|
|
void UtilTest::testGetContentDispositionFilename() {
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string h1 = "attachment; filename=\"aria2.tar.bz2\"";
|
2009-10-22 15:09:00 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"), util::getContentDispositionFilename(h1));
|
2006-05-09 15:54:14 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string h2 = "attachment; filename=\"\"";
|
2009-10-22 15:09:00 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(""), util::getContentDispositionFilename(h2));
|
2006-05-09 15:54:14 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string h3 = "attachment; filename=\"";
|
2009-10-22 15:09:00 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(""), util::getContentDispositionFilename(h3));
|
2006-05-09 15:54:14 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string h4 = "attachment;";
|
2009-10-22 15:09:00 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(""), util::getContentDispositionFilename(h4));
|
2007-07-18 11:36:41 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string h5 = "attachment; filename=aria2.tar.bz2";
|
2009-10-22 15:09:00 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"), util::getContentDispositionFilename(h5));
|
2007-07-18 11:36:41 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string h6 = "attachment; filename='aria2.tar.bz2'";
|
2009-10-22 15:09:00 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"), util::getContentDispositionFilename(h6));
|
2007-07-18 11:36:41 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string h7 = "attachment; filename='aria2.tar.bz2";
|
2009-10-22 15:09:00 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"), util::getContentDispositionFilename(h7));
|
2007-07-18 11:36:41 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string h8 = "attachment; filename=aria2.tar.bz2; creation-date=20 Jun 2007 00:00:00 GMT";
|
2009-10-22 15:09:00 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"), util::getContentDispositionFilename(h8));
|
2007-07-18 11:36:41 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string h9 = "attachment; filename=\"aria2.tar.bz2; creation-date=20 Jun 2007 00:00:00 GMT\"";
|
2010-02-17 16:09:19 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"),
|
|
|
|
util::getContentDispositionFilename(h9));
|
2007-07-18 11:36:41 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string h10 = "attachment; filename=";
|
2009-10-22 15:09:00 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(""), util::getContentDispositionFilename(h10));
|
2007-07-18 11:36:41 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string h11 = "attachment; filename=;";
|
2009-10-22 15:09:00 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(""), util::getContentDispositionFilename(h11));
|
2007-07-18 11:36:41 +00:00
|
|
|
|
2009-03-04 14:49:10 +00:00
|
|
|
std::string filenameWithDir = "attachment; filename=dir/file";
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("file"),
|
2010-01-05 16:01:46 +00:00
|
|
|
util::getContentDispositionFilename(filenameWithDir));
|
2009-03-04 14:49:10 +00:00
|
|
|
|
|
|
|
std::string parentDir = "attachment; filename=..";
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(),
|
2010-01-05 16:01:46 +00:00
|
|
|
util::getContentDispositionFilename(parentDir));
|
2009-03-04 14:49:10 +00:00
|
|
|
|
|
|
|
std::string currentDir = "attachment; filename=.";
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(),
|
2010-01-05 16:01:46 +00:00
|
|
|
util::getContentDispositionFilename(currentDir));
|
2010-02-17 16:09:19 +00:00
|
|
|
// RFC2231 Section4
|
|
|
|
std::string extparam2 = "filename*=''aria2";
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("aria2"),
|
|
|
|
util::getContentDispositionFilename(extparam2));
|
|
|
|
std::string extparam3 = "filename*='''";
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(""),
|
|
|
|
util::getContentDispositionFilename(extparam3));
|
|
|
|
std::string extparam4 = "filename*='aria2";
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(""),
|
|
|
|
util::getContentDispositionFilename(extparam4));
|
|
|
|
std::string extparam5 = "filename*='''aria2";
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(""),
|
|
|
|
util::getContentDispositionFilename(extparam5));
|
|
|
|
std::string extparam6 = "filename*";
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(""),
|
|
|
|
util::getContentDispositionFilename(extparam6));
|
|
|
|
std::string extparam7 = "filename*=UTF-8''aria2;filename=hello%20world";
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("aria2"),
|
|
|
|
util::getContentDispositionFilename(extparam7));
|
|
|
|
std::string extparam8 = "filename=aria2;filename*=UTF-8''hello%20world";
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("hello world"),
|
|
|
|
util::getContentDispositionFilename(extparam8));
|
2010-02-19 10:54:40 +00:00
|
|
|
std::string extparam9 = "filename*=ISO-8859-1''%A3";
|
2010-02-17 16:09:19 +00:00
|
|
|
std::string extparam9ans;
|
|
|
|
extparam9ans += 0xc2;
|
|
|
|
extparam9ans += 0xa3;
|
|
|
|
CPPUNIT_ASSERT_EQUAL(extparam9ans,
|
|
|
|
util::getContentDispositionFilename(extparam9));
|
2010-02-19 10:54:40 +00:00
|
|
|
|
|
|
|
// Tests from http://greenbytes.de/tech/tc2231/
|
|
|
|
// attwithasciifnescapedchar
|
|
|
|
CPPUNIT_ASSERT_EQUAL
|
|
|
|
(std::string("foo.html"),
|
|
|
|
util::getContentDispositionFilename("filename=\"f\\oo.html\""));
|
|
|
|
// attwithasciifilenameucase
|
|
|
|
CPPUNIT_ASSERT_EQUAL
|
|
|
|
(std::string("foo.html"),
|
|
|
|
util::getContentDispositionFilename("FILENAME=\"foo.html\""));
|
|
|
|
// attwithisofn2231iso
|
|
|
|
CPPUNIT_ASSERT_EQUAL
|
|
|
|
(std::string("foo-ä.html"),
|
|
|
|
util::getContentDispositionFilename("filename*=iso-8859-1''foo-%E4.html"));
|
|
|
|
// attwithfn2231utf8
|
|
|
|
CPPUNIT_ASSERT_EQUAL
|
|
|
|
(std::string("foo-ä-€.html"),
|
|
|
|
util::getContentDispositionFilename
|
|
|
|
("filename*=UTF-8''foo-%c3%a4-%e2%82%ac.html"));
|
|
|
|
// attwithfn2231utf8-bad
|
|
|
|
CPPUNIT_ASSERT_EQUAL
|
|
|
|
(std::string(""),
|
|
|
|
util::getContentDispositionFilename
|
|
|
|
("filename*=iso-8859-1''foo-%c3%a4-%e2%82%ac.html"));
|
|
|
|
// attwithfn2231ws1
|
|
|
|
CPPUNIT_ASSERT_EQUAL
|
|
|
|
(std::string(""),
|
|
|
|
util::getContentDispositionFilename("filename *=UTF-8''foo-%c3%a4.html"));
|
|
|
|
// attwithfn2231ws2
|
|
|
|
CPPUNIT_ASSERT_EQUAL
|
|
|
|
(std::string("foo-ä.html"),
|
|
|
|
util::getContentDispositionFilename("filename*= UTF-8''foo-%c3%a4.html"));
|
|
|
|
// attwithfn2231ws3
|
|
|
|
CPPUNIT_ASSERT_EQUAL
|
|
|
|
(std::string("foo-ä.html"),
|
|
|
|
util::getContentDispositionFilename("filename* =UTF-8''foo-%c3%a4.html"));
|
|
|
|
// attwithfn2231quot
|
|
|
|
CPPUNIT_ASSERT_EQUAL
|
|
|
|
(std::string(""),
|
|
|
|
util::getContentDispositionFilename
|
|
|
|
("filename*=\"UTF-8''foo-%c3%a4.html\""));
|
2006-05-09 15:54:14 +00:00
|
|
|
}
|
2006-05-18 17:08:29 +00:00
|
|
|
|
|
|
|
class Printer {
|
|
|
|
public:
|
|
|
|
template<class T>
|
|
|
|
void operator()(T t) {
|
2008-02-08 15:53:45 +00:00
|
|
|
std::cerr << t << ", ";
|
2006-05-18 17:08:29 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2006-06-22 15:26:18 +00:00
|
|
|
void UtilTest::testRandomAlpha() {
|
2008-04-20 00:50:22 +00:00
|
|
|
SharedHandle<Randomizer> rand(new FixedNumberRandomizer());
|
2009-10-22 15:09:00 +00:00
|
|
|
std::string s = util::randomAlpha(8, rand);
|
2008-02-08 15:53:45 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("AAAAAAAA"), s);
|
2006-06-22 15:26:18 +00:00
|
|
|
}
|
2006-07-03 14:19:23 +00:00
|
|
|
|
|
|
|
void UtilTest::testToUpper() {
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string src = "608cabc0f2fa18c260cafd974516865c772363d5";
|
|
|
|
std::string upp = "608CABC0F2FA18C260CAFD974516865C772363D5";
|
2006-07-03 14:19:23 +00:00
|
|
|
|
2009-10-22 15:09:00 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(upp, util::toUpper(src));
|
2006-07-03 14:19:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void UtilTest::testToLower() {
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string src = "608CABC0F2FA18C260CAFD974516865C772363D5";
|
|
|
|
std::string upp = "608cabc0f2fa18c260cafd974516865c772363d5";
|
2006-07-03 14:19:23 +00:00
|
|
|
|
2009-10-22 15:09:00 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(upp, util::toLower(src));
|
2006-07-03 14:19:23 +00:00
|
|
|
}
|
2006-08-28 12:40:41 +00:00
|
|
|
|
|
|
|
void UtilTest::testUrldecode() {
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string src = "http://aria2.sourceforge.net/aria2%200.7.0%20docs.html";
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("http://aria2.sourceforge.net/aria2 0.7.0 docs.html"),
|
2010-01-05 16:01:46 +00:00
|
|
|
util::urldecode(src));
|
2006-08-28 12:40:41 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string src2 = "aria2+aria2";
|
2009-10-22 15:09:00 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("aria2+aria2"), util::urldecode(src2));
|
2006-08-28 12:40:41 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string src3 = "%5t%20";
|
2009-10-22 15:09:00 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("%5t "), util::urldecode(src3));
|
2006-08-28 12:40:41 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string src4 = "%";
|
2009-10-22 15:09:00 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("%"), util::urldecode(src4));
|
2006-08-28 12:40:41 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string src5 = "%3";
|
2009-10-22 15:09:00 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("%3"), util::urldecode(src5));
|
2006-08-28 12:40:41 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string src6 = "%2f";
|
2009-10-22 15:09:00 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("/"), util::urldecode(src6));
|
2006-08-28 12:40:41 +00:00
|
|
|
}
|
2007-01-28 14:18:35 +00:00
|
|
|
|
2007-03-26 12:16:57 +00:00
|
|
|
void UtilTest::testGetRealSize()
|
|
|
|
{
|
2009-10-22 15:09:00 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((int64_t)4294967296LL, util::getRealSize("4096M"));
|
|
|
|
CPPUNIT_ASSERT_EQUAL((int64_t)1024, util::getRealSize("1K"));
|
2007-11-22 11:17:35 +00:00
|
|
|
try {
|
2009-10-22 15:09:00 +00:00
|
|
|
util::getRealSize("");
|
2007-11-22 11:17:35 +00:00
|
|
|
CPPUNIT_FAIL("exception must be thrown.");
|
2008-04-27 02:22:14 +00:00
|
|
|
} catch(Exception& e) {
|
|
|
|
std::cerr << e.stackTrace();
|
2007-11-22 11:17:35 +00:00
|
|
|
}
|
|
|
|
try {
|
2009-10-22 15:09:00 +00:00
|
|
|
util::getRealSize("foo");
|
2007-11-22 11:17:35 +00:00
|
|
|
CPPUNIT_FAIL("exception must be thrown.");
|
2008-04-27 02:22:14 +00:00
|
|
|
} catch(Exception& e) {
|
|
|
|
std::cerr << e.stackTrace();
|
2007-11-22 11:17:35 +00:00
|
|
|
}
|
|
|
|
try {
|
2009-10-22 15:09:00 +00:00
|
|
|
util::getRealSize("-1");
|
2007-11-22 11:17:35 +00:00
|
|
|
CPPUNIT_FAIL("exception must be thrown.");
|
2008-04-27 02:22:14 +00:00
|
|
|
} catch(Exception& e) {
|
|
|
|
std::cerr << e.stackTrace();
|
2007-11-22 11:17:35 +00:00
|
|
|
}
|
|
|
|
try {
|
2009-10-22 15:09:00 +00:00
|
|
|
util::getRealSize("9223372036854775807K");
|
2007-11-22 11:17:35 +00:00
|
|
|
CPPUNIT_FAIL("exception must be thrown.");
|
2008-04-27 02:22:14 +00:00
|
|
|
} catch(Exception& e) {
|
|
|
|
std::cerr << e.stackTrace();
|
2007-11-22 11:17:35 +00:00
|
|
|
}
|
|
|
|
try {
|
2009-10-22 15:09:00 +00:00
|
|
|
util::getRealSize("9223372036854775807M");
|
2007-11-22 11:17:35 +00:00
|
|
|
CPPUNIT_FAIL("exception must be thrown.");
|
2008-04-27 02:22:14 +00:00
|
|
|
} catch(Exception& e) {
|
|
|
|
std::cerr << e.stackTrace();
|
2007-11-22 11:17:35 +00:00
|
|
|
}
|
2007-03-26 12:16:57 +00:00
|
|
|
}
|
2007-06-05 11:37:25 +00:00
|
|
|
|
|
|
|
void UtilTest::testAbbrevSize()
|
|
|
|
{
|
2009-10-22 15:09:00 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("4,096.0Mi"), util::abbrevSize(4294967296LL));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("1.0Ki"), util::abbrevSize(1024));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("1,023"), util::abbrevSize(1023));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("0"), util::abbrevSize(0));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("1.1Ki"), util::abbrevSize(1127));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("1.5Mi"), util::abbrevSize(1572864));
|
2007-06-10 12:01:32 +00:00
|
|
|
|
2007-06-05 11:37:25 +00:00
|
|
|
}
|
2007-07-01 10:40:30 +00:00
|
|
|
|
|
|
|
void UtilTest::testToStream()
|
|
|
|
{
|
2008-02-08 15:53:45 +00:00
|
|
|
std::ostringstream os;
|
2008-04-20 00:50:22 +00:00
|
|
|
SharedHandle<FileEntry> f1(new FileEntry("aria2.tar.bz2", 12300, 0));
|
|
|
|
SharedHandle<FileEntry> f2(new FileEntry("aria2.txt", 556, 0));
|
2008-02-08 15:53:45 +00:00
|
|
|
std::deque<SharedHandle<FileEntry> > entries;
|
2007-07-01 10:40:30 +00:00
|
|
|
entries.push_back(f1);
|
|
|
|
entries.push_back(f2);
|
2009-10-22 15:09:00 +00:00
|
|
|
util::toStream(entries.begin(), entries.end(), os);
|
2007-07-01 10:40:30 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(
|
2010-01-05 16:01:46 +00:00
|
|
|
std::string("Files:\n"
|
|
|
|
"idx|path/length\n"
|
|
|
|
"===+===========================================================================\n"
|
|
|
|
" 1|aria2.tar.bz2\n"
|
|
|
|
" |12.0KiB (12,300)\n"
|
|
|
|
"---+---------------------------------------------------------------------------\n"
|
|
|
|
" 2|aria2.txt\n"
|
|
|
|
" |556B (556)\n"
|
|
|
|
"---+---------------------------------------------------------------------------\n"),
|
|
|
|
os.str());
|
2007-07-01 10:40:30 +00:00
|
|
|
}
|
2007-08-28 11:51:20 +00:00
|
|
|
|
|
|
|
void UtilTest::testIsNumber()
|
|
|
|
{
|
2009-10-22 15:09:00 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(true, util::isNumber("000"));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(false, util::isNumber("a"));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(false, util::isNumber("0a"));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(false, util::isNumber(""));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(false, util::isNumber(" "));
|
2007-08-28 11:51:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void UtilTest::testIsLowercase()
|
|
|
|
{
|
2009-10-22 15:09:00 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(true, util::isLowercase("alpha"));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(false, util::isLowercase("Alpha"));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(false, util::isLowercase("1alpha"));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(false, util::isLowercase(""));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(false, util::isLowercase(" "));
|
2007-08-28 11:51:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void UtilTest::testIsUppercase()
|
|
|
|
{
|
2009-10-22 15:09:00 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(true, util::isUppercase("ALPHA"));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(false, util::isUppercase("Alpha"));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(false, util::isUppercase("1ALPHA"));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(false, util::isUppercase(""));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(false, util::isUppercase(" "));
|
2007-08-28 11:51:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void UtilTest::testAlphaToNum()
|
|
|
|
{
|
2009-10-22 15:09:00 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(0U, util::alphaToNum("a"));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(0U, util::alphaToNum("aa"));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(1U, util::alphaToNum("b"));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(675U, util::alphaToNum("zz")); // 25*26+25
|
|
|
|
CPPUNIT_ASSERT_EQUAL(675U, util::alphaToNum("ZZ")); // 25*26+25
|
|
|
|
CPPUNIT_ASSERT_EQUAL(0U, util::alphaToNum(""));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(4294967295U, util::alphaToNum("NXMRLXV"));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(0U, util::alphaToNum("NXMRLXW")); // uint32_t overflow
|
2007-08-28 11:51:20 +00:00
|
|
|
}
|
2007-11-05 15:13:55 +00:00
|
|
|
|
|
|
|
void UtilTest::testMkdirs()
|
|
|
|
{
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string dir = "/tmp/aria2-UtilTest-testMkdirs";
|
2007-11-05 15:13:55 +00:00
|
|
|
File d(dir);
|
|
|
|
if(d.exists()) {
|
|
|
|
CPPUNIT_ASSERT(d.remove());
|
|
|
|
}
|
|
|
|
CPPUNIT_ASSERT(!d.exists());
|
2009-10-22 15:09:00 +00:00
|
|
|
util::mkdirs(dir);
|
2007-11-05 15:13:55 +00:00
|
|
|
CPPUNIT_ASSERT(d.isDir());
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string file = "./UtilTest.cc";
|
2007-11-05 15:13:55 +00:00
|
|
|
File f(file);
|
|
|
|
CPPUNIT_ASSERT(f.isFile());
|
|
|
|
try {
|
2009-10-22 15:09:00 +00:00
|
|
|
util::mkdirs(file);
|
2007-11-05 15:13:55 +00:00
|
|
|
CPPUNIT_FAIL("exception must be thrown.");
|
2008-04-27 02:22:14 +00:00
|
|
|
} catch(DlAbortEx& ex) {
|
|
|
|
std::cerr << ex.stackTrace() << std::endl;
|
2007-11-05 15:13:55 +00:00
|
|
|
}
|
|
|
|
}
|
2007-11-13 10:10:11 +00:00
|
|
|
|
|
|
|
void UtilTest::testConvertBitfield()
|
|
|
|
{
|
|
|
|
BitfieldMan srcBitfield(384*1024, 256*1024*256+1);
|
|
|
|
BitfieldMan destBitfield(512*1024, srcBitfield.getTotalLength());
|
|
|
|
srcBitfield.setAllBit();
|
|
|
|
srcBitfield.unsetBit(2);// <- range [768, 1152)
|
|
|
|
// which corresponds to the index [1,2] in destBitfield
|
2009-10-22 15:09:00 +00:00
|
|
|
util::convertBitfield(&destBitfield, &srcBitfield);
|
2007-11-13 10:10:11 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("9fffffffffffffffffffffffffffffff80"),
|
2010-01-05 16:01:46 +00:00
|
|
|
util::toHex(destBitfield.getBitfield(),
|
|
|
|
destBitfield.getBitfieldLength()));
|
2007-11-13 10:10:11 +00:00
|
|
|
}
|
2007-11-21 16:14:40 +00:00
|
|
|
|
|
|
|
void UtilTest::testParseIntRange()
|
|
|
|
{
|
2009-10-22 15:09:00 +00:00
|
|
|
IntSequence seq = util::parseIntRange("1,3-8,10");
|
2007-11-21 16:14:40 +00:00
|
|
|
|
|
|
|
CPPUNIT_ASSERT(seq.hasNext());
|
|
|
|
CPPUNIT_ASSERT_EQUAL((int32_t)1, seq.next());
|
|
|
|
CPPUNIT_ASSERT(seq.hasNext());
|
|
|
|
CPPUNIT_ASSERT_EQUAL((int32_t)3, seq.next());
|
|
|
|
CPPUNIT_ASSERT(seq.hasNext());
|
|
|
|
CPPUNIT_ASSERT_EQUAL((int32_t)4, seq.next());
|
|
|
|
CPPUNIT_ASSERT(seq.hasNext());
|
|
|
|
CPPUNIT_ASSERT_EQUAL((int32_t)5, seq.next());
|
|
|
|
CPPUNIT_ASSERT(seq.hasNext());
|
|
|
|
CPPUNIT_ASSERT_EQUAL((int32_t)6, seq.next());
|
|
|
|
CPPUNIT_ASSERT(seq.hasNext());
|
|
|
|
CPPUNIT_ASSERT_EQUAL((int32_t)7, seq.next());
|
|
|
|
CPPUNIT_ASSERT(seq.hasNext());
|
|
|
|
CPPUNIT_ASSERT_EQUAL((int32_t)8, seq.next());
|
|
|
|
CPPUNIT_ASSERT(seq.hasNext());
|
|
|
|
CPPUNIT_ASSERT_EQUAL((int32_t)10, seq.next());
|
|
|
|
CPPUNIT_ASSERT(!seq.hasNext());
|
|
|
|
CPPUNIT_ASSERT_EQUAL((int32_t)0, seq.next());
|
|
|
|
}
|
|
|
|
|
|
|
|
void UtilTest::testParseIntRange_invalidRange()
|
|
|
|
{
|
|
|
|
try {
|
2009-10-22 15:09:00 +00:00
|
|
|
IntSequence seq = util::parseIntRange("-1");
|
2007-11-21 16:14:40 +00:00
|
|
|
CPPUNIT_FAIL("exception must be thrown.");
|
2008-04-27 02:22:14 +00:00
|
|
|
} catch(Exception& e) {
|
|
|
|
std::cerr << e.stackTrace();
|
2007-11-21 16:14:40 +00:00
|
|
|
}
|
|
|
|
try {
|
2009-10-22 15:09:00 +00:00
|
|
|
IntSequence seq = util::parseIntRange("2147483648");
|
2007-11-21 16:14:40 +00:00
|
|
|
CPPUNIT_FAIL("exception must be thrown.");
|
2008-04-27 02:22:14 +00:00
|
|
|
} catch(Exception& e) {
|
|
|
|
std::cerr << e.stackTrace();
|
2007-11-21 16:14:40 +00:00
|
|
|
}
|
|
|
|
try {
|
2009-10-22 15:09:00 +00:00
|
|
|
IntSequence seq = util::parseIntRange("2147483647-2147483648");
|
2007-11-21 16:14:40 +00:00
|
|
|
CPPUNIT_FAIL("exception must be thrown.");
|
2008-04-27 02:22:14 +00:00
|
|
|
} catch(Exception& e) {
|
|
|
|
std::cerr << e.stackTrace();
|
2007-11-21 16:14:40 +00:00
|
|
|
}
|
|
|
|
try {
|
2009-10-22 15:09:00 +00:00
|
|
|
IntSequence seq = util::parseIntRange("1-2x");
|
2007-11-21 16:14:40 +00:00
|
|
|
CPPUNIT_FAIL("exception must be thrown.");
|
2008-04-27 02:22:14 +00:00
|
|
|
} catch(Exception& e) {
|
|
|
|
std::cerr << e.stackTrace();
|
2007-11-21 16:14:40 +00:00
|
|
|
}
|
|
|
|
try {
|
2009-10-22 15:09:00 +00:00
|
|
|
IntSequence seq = util::parseIntRange("3x-4");
|
2007-11-21 16:14:40 +00:00
|
|
|
CPPUNIT_FAIL("exception must be thrown.");
|
2008-04-27 02:22:14 +00:00
|
|
|
} catch(Exception& e) {
|
|
|
|
std::cerr << e.stackTrace();
|
2007-11-21 16:14:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UtilTest::testParseInt()
|
|
|
|
{
|
2009-10-22 15:09:00 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(-1, util::parseInt(" -1 "));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(2147483647, util::parseInt("2147483647"));
|
2007-11-21 16:14:40 +00:00
|
|
|
try {
|
2009-10-22 15:09:00 +00:00
|
|
|
util::parseInt("2147483648");
|
2007-11-21 16:14:40 +00:00
|
|
|
CPPUNIT_FAIL("exception must be thrown.");
|
2008-04-27 02:22:14 +00:00
|
|
|
} catch(Exception& e) {
|
|
|
|
std::cerr << e.stackTrace();
|
2007-11-21 16:14:40 +00:00
|
|
|
}
|
|
|
|
try {
|
2009-10-22 15:09:00 +00:00
|
|
|
util::parseInt("-2147483649");
|
2007-11-21 16:14:40 +00:00
|
|
|
CPPUNIT_FAIL("exception must be thrown.");
|
2008-04-27 02:22:14 +00:00
|
|
|
} catch(Exception& e) {
|
|
|
|
std::cerr << e.stackTrace();
|
2007-11-21 16:14:40 +00:00
|
|
|
}
|
|
|
|
try {
|
2009-10-22 15:09:00 +00:00
|
|
|
util::parseInt("12x");
|
2007-11-21 16:14:40 +00:00
|
|
|
CPPUNIT_FAIL("exception must be thrown.");
|
2008-04-27 02:22:14 +00:00
|
|
|
} catch(Exception& e) {
|
|
|
|
std::cerr << e.stackTrace();
|
2007-11-21 16:14:40 +00:00
|
|
|
}
|
2007-11-22 11:17:35 +00:00
|
|
|
try {
|
2009-10-22 15:09:00 +00:00
|
|
|
util::parseInt("");
|
2007-11-22 11:17:35 +00:00
|
|
|
CPPUNIT_FAIL("exception must be thrown.");
|
2008-04-27 02:22:14 +00:00
|
|
|
} catch(Exception& e) {
|
|
|
|
std::cerr << e.stackTrace();
|
2007-11-22 11:17:35 +00:00
|
|
|
}
|
2007-11-21 16:14:40 +00:00
|
|
|
}
|
|
|
|
|
2008-03-09 15:03:47 +00:00
|
|
|
void UtilTest::testParseUInt()
|
|
|
|
{
|
2009-10-22 15:09:00 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(4294967295U, util::parseUInt(" 4294967295 "));
|
2008-03-09 15:03:47 +00:00
|
|
|
try {
|
2009-10-22 15:09:00 +00:00
|
|
|
util::parseUInt("-1");
|
2008-03-09 15:03:47 +00:00
|
|
|
CPPUNIT_FAIL("exception must be thrown.");
|
2008-04-27 02:22:14 +00:00
|
|
|
} catch(Exception& e) {
|
|
|
|
std::cerr << e.stackTrace();
|
2008-03-09 15:03:47 +00:00
|
|
|
}
|
|
|
|
try {
|
2009-10-22 15:09:00 +00:00
|
|
|
util::parseUInt("4294967296");
|
2008-03-09 15:03:47 +00:00
|
|
|
CPPUNIT_FAIL("exception must be thrown.");
|
2008-04-27 02:22:14 +00:00
|
|
|
} catch(Exception& e) {
|
|
|
|
std::cerr << e.stackTrace();
|
2008-03-09 15:03:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-21 16:14:40 +00:00
|
|
|
void UtilTest::testParseLLInt()
|
|
|
|
{
|
2009-10-22 15:09:00 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((int64_t)-1LL, util::parseLLInt(" -1 "));
|
2008-07-31 12:28:12 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((int64_t)9223372036854775807LL,
|
2010-01-05 16:01:46 +00:00
|
|
|
util::parseLLInt("9223372036854775807"));
|
2007-11-21 16:14:40 +00:00
|
|
|
try {
|
2009-10-22 15:09:00 +00:00
|
|
|
util::parseLLInt("9223372036854775808");
|
2007-11-21 16:14:40 +00:00
|
|
|
CPPUNIT_FAIL("exception must be thrown.");
|
2008-04-27 02:22:14 +00:00
|
|
|
} catch(Exception& e) {
|
|
|
|
std::cerr << e.stackTrace();
|
2007-11-21 16:14:40 +00:00
|
|
|
}
|
|
|
|
try {
|
2009-10-22 15:09:00 +00:00
|
|
|
util::parseLLInt("-9223372036854775809");
|
2007-11-21 16:14:40 +00:00
|
|
|
CPPUNIT_FAIL("exception must be thrown.");
|
2008-04-27 02:22:14 +00:00
|
|
|
} catch(Exception& e) {
|
|
|
|
std::cerr << e.stackTrace();
|
2007-11-21 16:14:40 +00:00
|
|
|
}
|
|
|
|
try {
|
2009-10-22 15:09:00 +00:00
|
|
|
util::parseLLInt("12x");
|
2007-11-21 16:14:40 +00:00
|
|
|
CPPUNIT_FAIL("exception must be thrown.");
|
2008-04-27 02:22:14 +00:00
|
|
|
} catch(Exception& e) {
|
|
|
|
std::cerr << e.stackTrace();
|
2007-11-21 16:14:40 +00:00
|
|
|
}
|
2007-11-22 11:17:35 +00:00
|
|
|
try {
|
2009-10-22 15:09:00 +00:00
|
|
|
util::parseLLInt("");
|
2007-11-22 11:17:35 +00:00
|
|
|
CPPUNIT_FAIL("exception must be thrown.");
|
2008-04-27 02:22:14 +00:00
|
|
|
} catch(Exception& e) {
|
|
|
|
std::cerr << e.stackTrace();
|
2007-11-22 11:17:35 +00:00
|
|
|
}
|
2007-11-21 16:14:40 +00:00
|
|
|
}
|
2007-11-27 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Rewritten to add content-type support.
* src/DownloadHandler.{h, cc}
* src/BtPostDownloadHandler.{h, cc}
* test/BtPostDownloadHandlerTest.cc
* src/MetalinkPostDownloadHandler.{h, cc}
* test/MetalinkPostDownloadHandlerTest.cc
* src/PostDownloadHandler.{h, cc}
* src/DownloadHandlerConstants.{h, cc}
* src/RequestGroup.cc
* src/HttpResponseCommand.cc
* src/FtpNegotiationCommand.cc
* src/SingleFileDownloadContext.{h, cc}
* src/RequestGroup.h
* src/RequestGroupCriteria.h
* src/ContentTypeRequestGroupCriteria.h
Added 'mem' option value for --follow-metalink,
--follow-torrent.
If it is give, metalink/torrent file is not written to the disk,
but
just is kept in memory. Parsing is occurred on memory.
* src/MetalinkHelper.{h, cc}
* src/MetalinkProcessor.h
* src/Xml2MetalinkProcessor.{h, cc}
* test/Xml2MetalinkProcessorTest.cc
* src/DownloadHandlerFactory.{h, cc}
* test/DownloadHandlerFactoryTest.cc
* src/PreDownloadHandler.{h, cc}
* src/OptionHandlerFactory.cc
* src/DefaultBtContext.{h, cc}
* test/DefaultBtContextTest.cc
* src/version_usage.cc
* src/Metalink2RequestGroup.{h, cc}
* src/RequestGroup.{h, cc}
* src/a2functional.h
* test/a2functionalTest.cc
* src/MemoryBufferPreDownloadHandler.{h, cc}
* src/OptionHandlerImpl.h
* src/prefs.h
* src/Util.{h, cc}
* test/UtilTest.cc
Keep DownloadResult rather than RequestGroup after downloads to
reduce
memory usage.
* src/RequestGroupMan.{h, cc}
* src/DownloadEngine.cc
* src/BtDependency.{h, cc}: Changed the type of dependee from
WeakHandle to SharedHandle because WeakHandle could be null.
* src/RequestGroup.{h, cc}
* src/DownloadEngineFactory.cc
* src/DownloadResult.h
Set totalLength after download finished
* src/UnknownLengthPieceStorage.{h, cc}
Keep torrent file specified in metalink in memory.
* src/Metalink2RequestGroup.cc
* src/BtDependency.cc
* src/TrueRequestGroupCriteria.h
Fixed the bug: seekg is used where seekp should be used.
* src/ByteArrayDiskWriter.cc
* test/ByteArraydiskWriterTest.cc
2007-11-27 12:27:10 +00:00
|
|
|
|
2008-03-15 04:19:46 +00:00
|
|
|
void UtilTest::testParseULLInt()
|
|
|
|
{
|
2008-07-31 12:28:12 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((uint64_t)18446744073709551615ULL,
|
2010-01-05 16:01:46 +00:00
|
|
|
util::parseULLInt("18446744073709551615"));
|
2008-03-15 04:19:46 +00:00
|
|
|
try {
|
2009-10-22 15:09:00 +00:00
|
|
|
util::parseUInt("-1");
|
2008-03-15 04:19:46 +00:00
|
|
|
CPPUNIT_FAIL("exception must be thrown.");
|
2008-04-27 02:22:14 +00:00
|
|
|
} catch(Exception& e) {
|
|
|
|
std::cerr << e.stackTrace();
|
2008-03-15 04:19:46 +00:00
|
|
|
}
|
|
|
|
try {
|
2009-10-22 15:09:00 +00:00
|
|
|
util::parseLLInt("18446744073709551616");
|
2008-03-15 04:19:46 +00:00
|
|
|
CPPUNIT_FAIL("exception must be thrown.");
|
2008-04-27 02:22:14 +00:00
|
|
|
} catch(Exception& e) {
|
|
|
|
std::cerr << e.stackTrace();
|
2008-03-15 04:19:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-27 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Rewritten to add content-type support.
* src/DownloadHandler.{h, cc}
* src/BtPostDownloadHandler.{h, cc}
* test/BtPostDownloadHandlerTest.cc
* src/MetalinkPostDownloadHandler.{h, cc}
* test/MetalinkPostDownloadHandlerTest.cc
* src/PostDownloadHandler.{h, cc}
* src/DownloadHandlerConstants.{h, cc}
* src/RequestGroup.cc
* src/HttpResponseCommand.cc
* src/FtpNegotiationCommand.cc
* src/SingleFileDownloadContext.{h, cc}
* src/RequestGroup.h
* src/RequestGroupCriteria.h
* src/ContentTypeRequestGroupCriteria.h
Added 'mem' option value for --follow-metalink,
--follow-torrent.
If it is give, metalink/torrent file is not written to the disk,
but
just is kept in memory. Parsing is occurred on memory.
* src/MetalinkHelper.{h, cc}
* src/MetalinkProcessor.h
* src/Xml2MetalinkProcessor.{h, cc}
* test/Xml2MetalinkProcessorTest.cc
* src/DownloadHandlerFactory.{h, cc}
* test/DownloadHandlerFactoryTest.cc
* src/PreDownloadHandler.{h, cc}
* src/OptionHandlerFactory.cc
* src/DefaultBtContext.{h, cc}
* test/DefaultBtContextTest.cc
* src/version_usage.cc
* src/Metalink2RequestGroup.{h, cc}
* src/RequestGroup.{h, cc}
* src/a2functional.h
* test/a2functionalTest.cc
* src/MemoryBufferPreDownloadHandler.{h, cc}
* src/OptionHandlerImpl.h
* src/prefs.h
* src/Util.{h, cc}
* test/UtilTest.cc
Keep DownloadResult rather than RequestGroup after downloads to
reduce
memory usage.
* src/RequestGroupMan.{h, cc}
* src/DownloadEngine.cc
* src/BtDependency.{h, cc}: Changed the type of dependee from
WeakHandle to SharedHandle because WeakHandle could be null.
* src/RequestGroup.{h, cc}
* src/DownloadEngineFactory.cc
* src/DownloadResult.h
Set totalLength after download finished
* src/UnknownLengthPieceStorage.{h, cc}
Keep torrent file specified in metalink in memory.
* src/Metalink2RequestGroup.cc
* src/BtDependency.cc
* src/TrueRequestGroupCriteria.h
Fixed the bug: seekg is used where seekp should be used.
* src/ByteArrayDiskWriter.cc
* test/ByteArraydiskWriterTest.cc
2007-11-27 12:27:10 +00:00
|
|
|
void UtilTest::testToString_binaryStream()
|
|
|
|
{
|
2008-04-20 00:50:22 +00:00
|
|
|
SharedHandle<DiskWriter> dw(new ByteArrayDiskWriter());
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string data(16*1024+256, 'a');
|
2009-05-04 07:50:38 +00:00
|
|
|
dw->initAndOpenFile();
|
2007-11-27 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Rewritten to add content-type support.
* src/DownloadHandler.{h, cc}
* src/BtPostDownloadHandler.{h, cc}
* test/BtPostDownloadHandlerTest.cc
* src/MetalinkPostDownloadHandler.{h, cc}
* test/MetalinkPostDownloadHandlerTest.cc
* src/PostDownloadHandler.{h, cc}
* src/DownloadHandlerConstants.{h, cc}
* src/RequestGroup.cc
* src/HttpResponseCommand.cc
* src/FtpNegotiationCommand.cc
* src/SingleFileDownloadContext.{h, cc}
* src/RequestGroup.h
* src/RequestGroupCriteria.h
* src/ContentTypeRequestGroupCriteria.h
Added 'mem' option value for --follow-metalink,
--follow-torrent.
If it is give, metalink/torrent file is not written to the disk,
but
just is kept in memory. Parsing is occurred on memory.
* src/MetalinkHelper.{h, cc}
* src/MetalinkProcessor.h
* src/Xml2MetalinkProcessor.{h, cc}
* test/Xml2MetalinkProcessorTest.cc
* src/DownloadHandlerFactory.{h, cc}
* test/DownloadHandlerFactoryTest.cc
* src/PreDownloadHandler.{h, cc}
* src/OptionHandlerFactory.cc
* src/DefaultBtContext.{h, cc}
* test/DefaultBtContextTest.cc
* src/version_usage.cc
* src/Metalink2RequestGroup.{h, cc}
* src/RequestGroup.{h, cc}
* src/a2functional.h
* test/a2functionalTest.cc
* src/MemoryBufferPreDownloadHandler.{h, cc}
* src/OptionHandlerImpl.h
* src/prefs.h
* src/Util.{h, cc}
* test/UtilTest.cc
Keep DownloadResult rather than RequestGroup after downloads to
reduce
memory usage.
* src/RequestGroupMan.{h, cc}
* src/DownloadEngine.cc
* src/BtDependency.{h, cc}: Changed the type of dependee from
WeakHandle to SharedHandle because WeakHandle could be null.
* src/RequestGroup.{h, cc}
* src/DownloadEngineFactory.cc
* src/DownloadResult.h
Set totalLength after download finished
* src/UnknownLengthPieceStorage.{h, cc}
Keep torrent file specified in metalink in memory.
* src/Metalink2RequestGroup.cc
* src/BtDependency.cc
* src/TrueRequestGroupCriteria.h
Fixed the bug: seekg is used where seekp should be used.
* src/ByteArrayDiskWriter.cc
* test/ByteArraydiskWriterTest.cc
2007-11-27 12:27:10 +00:00
|
|
|
dw->writeData((const unsigned char*)data.c_str(), data.size(), 0);
|
|
|
|
|
2009-10-22 15:09:00 +00:00
|
|
|
std::string readData = util::toString(dw);
|
2007-11-27 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Rewritten to add content-type support.
* src/DownloadHandler.{h, cc}
* src/BtPostDownloadHandler.{h, cc}
* test/BtPostDownloadHandlerTest.cc
* src/MetalinkPostDownloadHandler.{h, cc}
* test/MetalinkPostDownloadHandlerTest.cc
* src/PostDownloadHandler.{h, cc}
* src/DownloadHandlerConstants.{h, cc}
* src/RequestGroup.cc
* src/HttpResponseCommand.cc
* src/FtpNegotiationCommand.cc
* src/SingleFileDownloadContext.{h, cc}
* src/RequestGroup.h
* src/RequestGroupCriteria.h
* src/ContentTypeRequestGroupCriteria.h
Added 'mem' option value for --follow-metalink,
--follow-torrent.
If it is give, metalink/torrent file is not written to the disk,
but
just is kept in memory. Parsing is occurred on memory.
* src/MetalinkHelper.{h, cc}
* src/MetalinkProcessor.h
* src/Xml2MetalinkProcessor.{h, cc}
* test/Xml2MetalinkProcessorTest.cc
* src/DownloadHandlerFactory.{h, cc}
* test/DownloadHandlerFactoryTest.cc
* src/PreDownloadHandler.{h, cc}
* src/OptionHandlerFactory.cc
* src/DefaultBtContext.{h, cc}
* test/DefaultBtContextTest.cc
* src/version_usage.cc
* src/Metalink2RequestGroup.{h, cc}
* src/RequestGroup.{h, cc}
* src/a2functional.h
* test/a2functionalTest.cc
* src/MemoryBufferPreDownloadHandler.{h, cc}
* src/OptionHandlerImpl.h
* src/prefs.h
* src/Util.{h, cc}
* test/UtilTest.cc
Keep DownloadResult rather than RequestGroup after downloads to
reduce
memory usage.
* src/RequestGroupMan.{h, cc}
* src/DownloadEngine.cc
* src/BtDependency.{h, cc}: Changed the type of dependee from
WeakHandle to SharedHandle because WeakHandle could be null.
* src/RequestGroup.{h, cc}
* src/DownloadEngineFactory.cc
* src/DownloadResult.h
Set totalLength after download finished
* src/UnknownLengthPieceStorage.{h, cc}
Keep torrent file specified in metalink in memory.
* src/Metalink2RequestGroup.cc
* src/BtDependency.cc
* src/TrueRequestGroupCriteria.h
Fixed the bug: seekg is used where seekp should be used.
* src/ByteArrayDiskWriter.cc
* test/ByteArraydiskWriterTest.cc
2007-11-27 12:27:10 +00:00
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL(data, readData);
|
|
|
|
}
|
2008-02-08 15:53:45 +00:00
|
|
|
|
2008-02-24 09:43:31 +00:00
|
|
|
void UtilTest::testItos()
|
|
|
|
{
|
|
|
|
{
|
2008-03-09 12:24:01 +00:00
|
|
|
int i = 0;
|
2009-10-22 15:09:00 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("0"), util::itos(i));
|
2008-02-24 09:43:31 +00:00
|
|
|
}
|
|
|
|
{
|
2008-03-09 12:24:01 +00:00
|
|
|
int i = 100;
|
2009-10-22 15:09:00 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("100"), util::itos(i, true));
|
2008-02-24 09:43:31 +00:00
|
|
|
}
|
|
|
|
{
|
2008-03-09 12:24:01 +00:00
|
|
|
int i = 100;
|
2009-10-22 15:09:00 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("100"), util::itos(i));
|
2008-02-24 09:43:31 +00:00
|
|
|
}
|
|
|
|
{
|
2008-03-09 12:24:01 +00:00
|
|
|
int i = 12345;
|
2009-10-22 15:09:00 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("12,345"), util::itos(i, true));
|
2008-02-24 09:43:31 +00:00
|
|
|
}
|
|
|
|
{
|
2008-03-09 12:24:01 +00:00
|
|
|
int i = 12345;
|
2009-10-22 15:09:00 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("12345"), util::itos(i));
|
2008-02-24 09:43:31 +00:00
|
|
|
}
|
|
|
|
{
|
2008-03-09 12:24:01 +00:00
|
|
|
int i = -12345;
|
2009-10-22 15:09:00 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("-12,345"), util::itos(i, true));
|
2008-02-24 09:43:31 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
int64_t i = INT64_MAX;
|
2009-10-22 15:09:00 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("9,223,372,036,854,775,807"), util::itos(i, true));
|
2008-02-24 09:43:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UtilTest::testUitos()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
uint16_t i = 12345;
|
2009-10-22 15:09:00 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("12345"), util::uitos(i));
|
2008-02-24 09:43:31 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
int16_t i = -12345;
|
2009-10-22 15:09:00 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("/.-,+"), util::uitos(i));
|
2008-02-24 09:43:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-24 07:55:34 +00:00
|
|
|
void UtilTest::testNtoh64()
|
|
|
|
{
|
|
|
|
uint64_t x = 0xff00ff00ee00ee00LL;
|
|
|
|
#ifdef WORDS_BIGENDIAN
|
|
|
|
CPPUNIT_ASSERT_EQUAL(x, ntoh64(x));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(x, hton64(x));
|
|
|
|
#else // !WORDS_BIGENDIAN
|
|
|
|
uint64_t y = 0x00ee00ee00ff00ffLL;
|
|
|
|
CPPUNIT_ASSERT_EQUAL(y, ntoh64(x));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(x, hton64(y));
|
|
|
|
#endif // !WORDS_BIGENDIAN
|
|
|
|
}
|
|
|
|
|
2008-11-27 15:29:15 +00:00
|
|
|
void UtilTest::testUrlencode()
|
|
|
|
{
|
|
|
|
CPPUNIT_ASSERT_EQUAL
|
|
|
|
(std::string("%3A%2F%3F%23%5B%5D%40%21%25%26%27%28%29%2A%2B%2C%3B%3D"),
|
2009-10-22 15:09:00 +00:00
|
|
|
util::urlencode(":/?#[]@!%&'()*+,;="));
|
2008-11-27 15:29:15 +00:00
|
|
|
|
|
|
|
std::string unreserved =
|
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
"abcdefghijklmnopqrstuvwxyz"
|
|
|
|
"0123456789"
|
|
|
|
"-._~";
|
2009-10-22 15:09:00 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(unreserved, util::urlencode(unreserved));
|
2008-11-27 15:29:15 +00:00
|
|
|
|
2009-10-22 15:09:00 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("1%5EA%20"), util::urlencode("1^A "));
|
2008-11-27 15:29:15 +00:00
|
|
|
}
|
|
|
|
|
2009-01-25 09:58:40 +00:00
|
|
|
void UtilTest::testHtmlEscape()
|
|
|
|
{
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("aria2<>"'util"),
|
2010-01-05 16:01:46 +00:00
|
|
|
util::htmlEscape("aria2<>\"'util"));
|
2009-01-25 09:58:40 +00:00
|
|
|
}
|
|
|
|
|
2009-03-04 15:53:56 +00:00
|
|
|
void UtilTest::testJoinPath()
|
|
|
|
{
|
|
|
|
const std::string dir1dir2file[] = { "dir1", "dir2", "file" };
|
|
|
|
CPPUNIT_ASSERT_EQUAL
|
|
|
|
(std::string("dir1/dir2/file"),
|
2009-10-22 15:09:00 +00:00
|
|
|
util::joinPath(&dir1dir2file[0],
|
2010-01-05 16:01:46 +00:00
|
|
|
&dir1dir2file[arrayLength(dir1dir2file)]));
|
2009-03-04 15:53:56 +00:00
|
|
|
|
|
|
|
const std::string dirparentfile[] = { "dir", "..", "file" };
|
|
|
|
CPPUNIT_ASSERT_EQUAL
|
|
|
|
(std::string("file"),
|
2009-10-22 15:09:00 +00:00
|
|
|
util::joinPath(&dirparentfile[0],
|
2010-01-05 16:01:46 +00:00
|
|
|
&dirparentfile[arrayLength(dirparentfile)]));
|
2009-03-04 15:53:56 +00:00
|
|
|
|
|
|
|
const std::string dirparentparentfile[] = { "dir", "..", "..", "file" };
|
|
|
|
CPPUNIT_ASSERT_EQUAL
|
|
|
|
(std::string("file"),
|
2009-10-22 15:09:00 +00:00
|
|
|
util::joinPath(&dirparentparentfile[0],
|
2010-01-05 16:01:46 +00:00
|
|
|
&dirparentparentfile[arrayLength(dirparentparentfile)]));
|
2009-03-04 15:53:56 +00:00
|
|
|
|
|
|
|
const std::string dirdotfile[] = { "dir", ".", "file" };
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("dir/file"),
|
2010-01-05 16:01:46 +00:00
|
|
|
util::joinPath(&dirdotfile[0],
|
|
|
|
&dirdotfile[arrayLength(dirdotfile)]));
|
2009-03-04 15:53:56 +00:00
|
|
|
|
|
|
|
const std::string empty[] = {};
|
2009-10-22 15:09:00 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(""), util::joinPath(&empty[0], &empty[0]));
|
2009-03-04 15:53:56 +00:00
|
|
|
|
|
|
|
const std::string parentdot[] = { "..", "." };
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string(""),
|
2010-01-05 16:01:46 +00:00
|
|
|
util::joinPath(&parentdot[0],
|
|
|
|
&parentdot[arrayLength(parentdot)]));
|
2009-03-04 15:53:56 +00:00
|
|
|
}
|
|
|
|
|
2009-03-07 03:10:53 +00:00
|
|
|
void UtilTest::testParseIndexPath()
|
|
|
|
{
|
2009-10-22 15:09:00 +00:00
|
|
|
std::map<size_t, std::string>::value_type p = util::parseIndexPath("1=foo");
|
2009-03-07 03:10:53 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1, p.first);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("foo"), p.second);
|
|
|
|
try {
|
2009-10-22 15:09:00 +00:00
|
|
|
util::parseIndexPath("1X=foo");
|
2009-03-07 03:10:53 +00:00
|
|
|
CPPUNIT_FAIL("exception must be thrown.");
|
|
|
|
} catch(Exception& e) {
|
|
|
|
// success
|
|
|
|
}
|
|
|
|
try {
|
2009-10-22 15:09:00 +00:00
|
|
|
util::parseIndexPath("1=");
|
2009-03-07 03:10:53 +00:00
|
|
|
CPPUNIT_FAIL("exception must be thrown.");
|
|
|
|
} catch(Exception& e) {
|
|
|
|
// success
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UtilTest::testCreateIndexPathMap()
|
|
|
|
{
|
|
|
|
std::stringstream in
|
|
|
|
("1=/tmp/myfile\n"
|
|
|
|
"100=/myhome/mypicture.png\n");
|
2009-10-22 15:09:00 +00:00
|
|
|
std::map<size_t, std::string> m = util::createIndexPathMap(in);
|
2009-03-07 03:10:53 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)2, m.size());
|
|
|
|
CPPUNIT_ASSERT(m.find(1) != m.end());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("/tmp/myfile"), m[1]);
|
|
|
|
CPPUNIT_ASSERT(m.find(100) != m.end());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("/myhome/mypicture.png"), m[100]);
|
|
|
|
}
|
|
|
|
|
2009-10-22 14:43:42 +00:00
|
|
|
void UtilTest::testGenerateRandomData()
|
|
|
|
{
|
|
|
|
unsigned char data1[20];
|
|
|
|
util::generateRandomData(data1, sizeof(data1));
|
|
|
|
unsigned char data2[20];
|
|
|
|
util::generateRandomData(data2, sizeof(data2));
|
|
|
|
CPPUNIT_ASSERT(memcmp(data1, data2, sizeof(data1)) != 0);
|
|
|
|
}
|
|
|
|
|
2009-11-22 14:30:51 +00:00
|
|
|
void UtilTest::testFromHex()
|
|
|
|
{
|
|
|
|
std::string src;
|
|
|
|
std::string dest;
|
|
|
|
|
|
|
|
src = "0011fF";
|
|
|
|
dest = util::fromHex(src);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)3, dest.size());
|
|
|
|
CPPUNIT_ASSERT_EQUAL((char)0x00, dest[0]);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((char)0x11, dest[1]);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((char)0xff, dest[2]);
|
|
|
|
|
|
|
|
src = "0011f";
|
|
|
|
CPPUNIT_ASSERT(util::fromHex(src).empty());
|
|
|
|
|
|
|
|
src = "001g";
|
|
|
|
CPPUNIT_ASSERT(util::fromHex(src).empty());
|
|
|
|
}
|
|
|
|
|
2009-11-29 06:43:38 +00:00
|
|
|
void UtilTest::testParsePrioritizePieceRange()
|
|
|
|
{
|
|
|
|
// piece index
|
|
|
|
// 0 1 2 3 4 5 6 7
|
|
|
|
// | | | |
|
|
|
|
// file1 | | |
|
|
|
|
// | | |
|
|
|
|
// file2 | |
|
|
|
|
// file3 |
|
|
|
|
// | |
|
|
|
|
// file4 |
|
|
|
|
size_t pieceLength = 1024;
|
|
|
|
std::vector<SharedHandle<FileEntry> > entries(4, SharedHandle<FileEntry>());
|
|
|
|
entries[0].reset(new FileEntry("file1", 1024, 0));
|
|
|
|
entries[1].reset(new FileEntry("file2",2560,entries[0]->getLastOffset()));
|
|
|
|
entries[2].reset(new FileEntry("file3",0,entries[1]->getLastOffset()));
|
|
|
|
entries[3].reset(new FileEntry("file4",3584,entries[2]->getLastOffset()));
|
|
|
|
|
|
|
|
std::vector<size_t> result;
|
2009-12-06 11:35:45 +00:00
|
|
|
util::parsePrioritizePieceRange(result, "head=1", entries, pieceLength);
|
2009-11-29 06:43:38 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)3, result.size());
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1, result[1]);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
|
|
|
|
result.clear();
|
2009-12-06 11:35:45 +00:00
|
|
|
util::parsePrioritizePieceRange(result, "tail=1", entries, pieceLength);
|
2009-11-29 06:43:38 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)3, result.size());
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)3, result[1]);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)6, result[2]);
|
|
|
|
result.clear();
|
|
|
|
util::parsePrioritizePieceRange(result, "head=1K", entries, pieceLength);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)4, result.size());
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1, result[1]);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)4, result[3]);
|
|
|
|
result.clear();
|
2009-12-07 12:49:19 +00:00
|
|
|
util::parsePrioritizePieceRange(result, "head", entries, pieceLength, 1024);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)4, result.size());
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1, result[1]);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)4, result[3]);
|
|
|
|
result.clear();
|
2009-11-29 06:43:38 +00:00
|
|
|
util::parsePrioritizePieceRange(result, "tail=1K", entries, pieceLength);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)4, result.size());
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)2, result[1]);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)6, result[3]);
|
|
|
|
result.clear();
|
2009-12-07 12:49:19 +00:00
|
|
|
util::parsePrioritizePieceRange(result, "tail", entries, pieceLength, 1024);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)4, result.size());
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)2, result[1]);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)6, result[3]);
|
|
|
|
result.clear();
|
2009-12-06 11:35:45 +00:00
|
|
|
util::parsePrioritizePieceRange(result, "head=1,tail=1", entries, pieceLength);
|
2009-11-29 06:43:38 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)4, result.size());
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)0, result[0]);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)1, result[1]);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)3, result[2]);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)6, result[3]);
|
|
|
|
result.clear();
|
2009-12-07 12:49:19 +00:00
|
|
|
util::parsePrioritizePieceRange(result, "head=300M,tail=300M",
|
2010-01-05 16:01:46 +00:00
|
|
|
entries, pieceLength);
|
2009-11-29 06:43:38 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)7, result.size());
|
|
|
|
for(size_t i = 0; i < 7; ++i) {
|
|
|
|
CPPUNIT_ASSERT_EQUAL(i, result[i]);
|
|
|
|
}
|
|
|
|
result.clear();
|
|
|
|
util::parsePrioritizePieceRange(result, "", entries, pieceLength);
|
|
|
|
CPPUNIT_ASSERT(result.empty());
|
|
|
|
}
|
|
|
|
|
2010-01-11 14:01:20 +00:00
|
|
|
void UtilTest::testApplyDir()
|
|
|
|
{
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("./pred"), util::applyDir("", "pred"));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("/pred"), util::applyDir("/", "pred"));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("./pred"), util::applyDir(".", "pred"));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("/dl/pred"), util::applyDir("/dl", "pred"));
|
|
|
|
}
|
|
|
|
|
2010-01-11 14:32:20 +00:00
|
|
|
void UtilTest::testFixTaintedBasename()
|
|
|
|
{
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("a_b"), util::fixTaintedBasename("a/b"));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("a_b"), util::fixTaintedBasename("a\\b"));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("a__b"), util::fixTaintedBasename("a\\/b"));
|
|
|
|
}
|
|
|
|
|
2010-01-28 14:25:16 +00:00
|
|
|
void UtilTest::testIsNumericHost()
|
|
|
|
{
|
|
|
|
CPPUNIT_ASSERT(util::isNumericHost("192.168.0.1"));
|
|
|
|
CPPUNIT_ASSERT(!util::isNumericHost("aria2.sf.net"));
|
|
|
|
CPPUNIT_ASSERT(util::isNumericHost("::1"));
|
|
|
|
}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
} // namespace aria2
|