DefaultBtProgressInfoFileTest.cc 2.8 KB

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