DefaultBtProgressInfoFileTest.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include "DefaultBtProgressInfoFile.h"
  2. #include "DefaultBtContext.h"
  3. #include "Option.h"
  4. #include "Util.h"
  5. #include "Exception.h"
  6. #include "MockBtContext.h"
  7. #include "MockPeerStorage.h"
  8. #include "MockPieceStorage.h"
  9. #include "prefs.h"
  10. #include <cppunit/extensions/HelperMacros.h>
  11. using namespace std;
  12. class DefaultBtProgressInfoFileTest:public CppUnit::TestFixture {
  13. CPPUNIT_TEST_SUITE(DefaultBtProgressInfoFileTest);
  14. CPPUNIT_TEST(testSave);
  15. CPPUNIT_TEST_SUITE_END();
  16. private:
  17. BtContextHandle btContext;
  18. Option* option;
  19. public:
  20. DefaultBtProgressInfoFileTest():btContext(0) {}
  21. void setUp() {
  22. btContext = BtContextHandle(new DefaultBtContext());
  23. btContext->load("test.torrent");
  24. option = new Option();
  25. }
  26. void testSave();
  27. };
  28. CPPUNIT_TEST_SUITE_REGISTRATION(DefaultBtProgressInfoFileTest);
  29. void DefaultBtProgressInfoFileTest::testSave() {
  30. unsigned char infoHash[] = {
  31. 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa,
  32. 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00, 0xff, 0xff, 0xff, 0xff,
  33. };
  34. Option option;
  35. option.put(PREF_DIR, ".");
  36. MockBtContextHandle btContext = new MockBtContext();
  37. btContext->setInfoHash(infoHash);
  38. btContext->setName("save-temp");
  39. BitfieldMan bitfield(1024, 80*1024);
  40. bitfield.setAllBit();
  41. bitfield.unsetBit(79);
  42. MockPieceStorageHandle pieceStorage = new MockPieceStorage();
  43. pieceStorage->setBitfield(&bitfield);
  44. pieceStorage->setCompletedLength(80896);
  45. MockPeerStorageHandle peerStorage = new MockPeerStorage();
  46. TransferStat stat;
  47. stat.sessionUploadLength = 1024;
  48. peerStorage->setStat(stat);
  49. BtRuntimeHandle btRuntime = new BtRuntime();
  50. DefaultBtProgressInfoFile infoFile(btContext, &option);
  51. infoFile.setPieceStorage(pieceStorage);
  52. infoFile.setPeerStorage(peerStorage);
  53. infoFile.setBtRuntime(btRuntime);
  54. infoFile.save();
  55. // read and validate
  56. ifstream in(string(option.get(PREF_DIR)+"/"+btContext->getName()+".aria2").c_str());
  57. unsigned char infoHashRead[20];
  58. in.read((char*)infoHashRead, sizeof(infoHashRead));
  59. CPPUNIT_ASSERT_EQUAL(string("112233445566778899aabbccddeeff00ffffffff"),
  60. Util::toHex(infoHashRead, sizeof(infoHashRead)));
  61. unsigned char bitfieldRead[10];
  62. in.read((char*)bitfieldRead, sizeof(bitfieldRead));
  63. CPPUNIT_ASSERT_EQUAL(string("fffffffffffffffffffe"),
  64. Util::toHex(bitfieldRead, sizeof(bitfieldRead)));
  65. int64_t allTimeDownloadLengthRead = 0;
  66. in.read((char*)&allTimeDownloadLengthRead, sizeof(allTimeDownloadLengthRead));
  67. CPPUNIT_ASSERT_EQUAL((int64_t)80896, allTimeDownloadLengthRead);
  68. int64_t allTimeUploadLengthRead = 0;
  69. in.read((char*)&allTimeUploadLengthRead, sizeof(allTimeUploadLengthRead));
  70. CPPUNIT_ASSERT_EQUAL((int64_t)1024, allTimeUploadLengthRead);
  71. string temp;
  72. getline(in, temp);
  73. CPPUNIT_ASSERT_EQUAL(string(""), temp);
  74. CPPUNIT_ASSERT(in.eof());
  75. }