File indexing completed on 2025-01-05 04:37:24
0001 #ifndef QT_GUI_LIB 0002 #define QT_GUI_LIB 0003 #endif 0004 0005 #include <QLocale> 0006 #include <QObject> 0007 #include <QRandomGenerator64> 0008 #include <QtTest> 0009 0010 #include <ctime> 0011 #include <interfaces/queuemanagerinterface.h> 0012 #include <testlib/dummytorrentcreator.h> 0013 #include <torrent/torrentcontrol.h> 0014 #include <torrent/torrentfilestream.h> 0015 #include <unistd.h> 0016 #include <util/error.h> 0017 #include <util/fileops.h> 0018 #include <util/log.h> 0019 #include <util/sha1hashgen.h> 0020 0021 using namespace bt; 0022 0023 const bt::Uint32 TEST_FILE_SIZE = 5 * 1024 * 1024; 0024 0025 bt::Uint64 RandomSize(bt::Uint64 min_size, bt::Uint64 max_size) 0026 { 0027 bt::Uint64 r = max_size - min_size; 0028 return min_size + QRandomGenerator64::global()->generate() % r; 0029 } 0030 0031 class TorrentFileStreamMultiTest : public QEventLoop, public bt::QueueManagerInterface 0032 { 0033 Q_OBJECT 0034 public: 0035 TorrentFileStreamMultiTest(QObject *parent = nullptr) 0036 : QEventLoop(parent) 0037 { 0038 } 0039 0040 bool alreadyLoaded(const bt::SHA1Hash &ih) const override 0041 { 0042 Q_UNUSED(ih); 0043 return false; 0044 } 0045 0046 void mergeAnnounceList(const bt::SHA1Hash &ih, const bt::TrackerTier *trk) override 0047 { 0048 Q_UNUSED(ih); 0049 Q_UNUSED(trk); 0050 } 0051 0052 private Q_SLOTS: 0053 void initTestCase() 0054 { 0055 QLocale::setDefault(QLocale("main")); 0056 bt::InitLog("torrentfilestreammultitest.log", false, false); 0057 0058 files["aaa.avi"] = RandomSize(TEST_FILE_SIZE / 2, TEST_FILE_SIZE); 0059 files["bbb.avi"] = RandomSize(TEST_FILE_SIZE / 2, TEST_FILE_SIZE); 0060 files["ccc.avi"] = RandomSize(TEST_FILE_SIZE / 2, TEST_FILE_SIZE); 0061 0062 QVERIFY(creator.createMultiFileTorrent(files, "movies")); 0063 0064 Out(SYS_GEN | LOG_DEBUG) << "Created " << creator.torrentPath() << endl; 0065 try { 0066 tc.init(this, bt::LoadFile(creator.torrentPath()), creator.tempPath() + "tor0", creator.tempPath() + "data/"); 0067 tc.createFiles(); 0068 QVERIFY(tc.hasExistingFiles()); 0069 tc.startDataCheck(false, 0, tc.getStats().total_chunks); 0070 do { 0071 processEvents(AllEvents, 1000); 0072 } while (tc.getStats().status == bt::CHECKING_DATA); 0073 QVERIFY(tc.getStats().completed); 0074 } catch (bt::Error &err) { 0075 Out(SYS_GEN | LOG_DEBUG) << "Failed to load torrent: " << creator.torrentPath() << endl; 0076 QFAIL("Torrent load failure"); 0077 } 0078 } 0079 0080 void cleanupTestCase() 0081 { 0082 } 0083 0084 void testSimple() 0085 { 0086 Out(SYS_GEN | LOG_DEBUG) << "Begin: testSimple() " << endl; 0087 // Simple test read each file and check if they are the same on disk 0088 for (bt::Uint32 i = 0; i < tc.getNumFiles(); i++) { 0089 Out(SYS_GEN | LOG_DEBUG) << "Doing file " << i << endl; 0090 bt::TorrentFileStream::Ptr stream = tc.createTorrentFileStream(i, false, this); 0091 QVERIFY(stream); 0092 QVERIFY(!stream->open(QIODevice::ReadWrite)); 0093 QVERIFY(stream->open(QIODevice::ReadOnly)); 0094 QVERIFY(stream->size() == (qint64)tc.getTorrentFile(i).getSize()); 0095 0096 QByteArray tmp(stream->size(), 0); 0097 qint64 written = 0; 0098 while (written < stream->size()) { 0099 qint64 ret = stream->read(tmp.data(), stream->size()); 0100 Out(SYS_GEN | LOG_DEBUG) << "Returned " << ret << endl; 0101 QVERIFY(ret == stream->size()); 0102 written += ret; 0103 0104 // Load the file and check if the data is the same 0105 QFile fptr(stream->path()); 0106 QVERIFY(fptr.open(QIODevice::ReadOnly)); 0107 QByteArray tmp2(stream->size(), 0); 0108 QVERIFY(fptr.read(tmp2.data(), stream->size()) == stream->size()); 0109 QVERIFY(tmp == tmp2); 0110 } 0111 0112 stream->close(); 0113 } 0114 Out(SYS_GEN | LOG_DEBUG) << "End: testSimple() " << endl; 0115 } 0116 0117 void testSeek() 0118 { 0119 Out(SYS_GEN | LOG_DEBUG) << "Begin: testSeek() " << endl; 0120 0121 // In each file take a couple of random samples and compare them with the actual file 0122 for (bt::Uint32 i = 0; i < tc.getNumFiles(); i++) { 0123 bt::TorrentFileStream::Ptr stream = tc.createTorrentFileStream(i, false, this); 0124 QVERIFY(stream); 0125 QVERIFY(!stream->open(QIODevice::ReadWrite)); 0126 QVERIFY(stream->open(QIODevice::ReadOnly)); 0127 QVERIFY(stream->size() == (qint64)tc.getTorrentFile(i).getSize()); 0128 0129 QFile fptr(stream->path()); 0130 QVERIFY(fptr.open(QIODevice::ReadOnly)); 0131 0132 for (bt::Uint32 j = 0; j < 10; j++) { 0133 qint64 off = QRandomGenerator64::global()->generate() % (stream->size() - 256); 0134 QVERIFY(stream->seek(off)); 0135 QVERIFY(fptr.seek(off)); 0136 0137 QByteArray sdata(256, 0); 0138 QVERIFY(stream->read(sdata.data(), 256) == 256); 0139 0140 QByteArray fdata(256, 0); 0141 QVERIFY(fptr.read(fdata.data(), 256) == 256); 0142 QVERIFY(sdata == fdata); 0143 } 0144 stream->close(); 0145 } 0146 0147 Out(SYS_GEN | LOG_DEBUG) << "End: testSeek() " << endl; 0148 } 0149 0150 private: 0151 DummyTorrentCreator creator; 0152 bt::TorrentControl tc; 0153 QMap<QString, bt::Uint64> files; 0154 }; 0155 0156 QTEST_MAIN(TorrentFileStreamMultiTest) 0157 0158 #include "torrentfilestreammultitest.moc"