File indexing completed on 2025-01-05 04:37:09

0001 #ifndef QT_GUI_LIB
0002 #define QT_GUI_LIB
0003 #endif
0004 
0005 #include <time.h>
0006 #include <unistd.h>
0007 
0008 #include <QEventLoop>
0009 #include <QLocale>
0010 #include <QRandomGenerator64>
0011 #include <QtTest>
0012 
0013 #include <datachecker/multidatachecker.h>
0014 #include <datachecker/singledatachecker.h>
0015 #include <testlib/dummytorrentcreator.h>
0016 #include <torrent/torrentcontrol.h>
0017 #include <util/error.h>
0018 #include <util/fileops.h>
0019 #include <util/functions.h>
0020 #include <util/log.h>
0021 
0022 #include <boost/concept_check.hpp>
0023 
0024 using namespace bt;
0025 
0026 const bt::Uint64 TEST_FILE_SIZE = 15 * 1024 * 1024;
0027 
0028 bt::Uint64 RandomSize(bt::Uint64 min_size, bt::Uint64 max_size)
0029 {
0030     bt::Uint64 r = max_size - min_size;
0031     return min_size + QRandomGenerator64::global()->generate() % r;
0032 }
0033 
0034 class DataCheckerTest : public QEventLoop
0035 {
0036     Q_OBJECT
0037 
0038 public:
0039 private Q_SLOTS:
0040     void initTestCase()
0041     {
0042         QLocale::setDefault(QLocale("main"));
0043         bt::InitLibKTorrent();
0044         bt::InitLog("datacheckertest.log", false, true);
0045     }
0046 
0047     void testSingleFile()
0048     {
0049         Out(SYS_GEN | LOG_DEBUG) << "testSingleFile" << endl;
0050         DummyTorrentCreator creator;
0051         bt::TorrentControl tc;
0052         QVERIFY(creator.createSingleFileTorrent(TEST_FILE_SIZE, "test.avi"));
0053 
0054         Out(SYS_GEN | LOG_DEBUG) << "Created " << creator.torrentPath() << endl;
0055         try {
0056             tc.init(nullptr, bt::LoadFile(creator.torrentPath()), creator.tempPath() + "tor0", creator.tempPath() + "data/");
0057             tc.createFiles();
0058         } catch (bt::Error &err) {
0059             Out(SYS_GEN | LOG_DEBUG) << "Failed to load torrent: " << creator.torrentPath() << endl;
0060             QFAIL("Torrent load failure");
0061         }
0062 
0063         SingleDataChecker dc(0, tc.getStats().total_chunks);
0064         try {
0065             QString dnd = tc.getTorDir() + "dnd" + bt::DirSeparator();
0066             dc.check(tc.getStats().output_path, tc.getTorrent(), dnd, tc.downloadedChunksBitSet());
0067             QVERIFY(dc.getResult().allOn());
0068         } catch (bt::Error &err) {
0069             Out(SYS_GEN | LOG_DEBUG) << "Datacheck failed: " << err.toString() << endl;
0070             QFAIL("Torrent load failure");
0071         }
0072     }
0073 
0074     void testMultiFile()
0075     {
0076         Out(SYS_GEN | LOG_DEBUG) << "testMultiFile" << endl;
0077         QMap<QString, bt::Uint64> files;
0078 
0079         files["aaa.avi"] = RandomSize(TEST_FILE_SIZE / 2, TEST_FILE_SIZE);
0080         files["bbb.avi"] = RandomSize(TEST_FILE_SIZE / 2, TEST_FILE_SIZE);
0081         files["ccc.avi"] = RandomSize(TEST_FILE_SIZE / 2, TEST_FILE_SIZE);
0082 
0083         DummyTorrentCreator creator;
0084         bt::TorrentControl tc;
0085         QVERIFY(creator.createMultiFileTorrent(files, "movies"));
0086 
0087         Out(SYS_GEN | LOG_DEBUG) << "Created " << creator.torrentPath() << endl;
0088         try {
0089             tc.init(nullptr, bt::LoadFile(creator.torrentPath()), creator.tempPath() + "tor0", creator.tempPath() + "data/");
0090             tc.createFiles();
0091         } catch (bt::Error &err) {
0092             Out(SYS_GEN | LOG_DEBUG) << "Failed to load torrent: " << creator.torrentPath() << endl;
0093             QFAIL("Torrent load failure");
0094         }
0095 
0096         MultiDataChecker dc(0, tc.getStats().total_chunks);
0097         try {
0098             QString dnd = tc.getTorDir() + "dnd" + bt::DirSeparator();
0099             dc.check(tc.getStats().output_path, tc.getTorrent(), dnd, tc.downloadedChunksBitSet());
0100             QVERIFY(dc.getResult().allOn());
0101         } catch (bt::Error &err) {
0102             Out(SYS_GEN | LOG_DEBUG) << "Datacheck failed: " << err.toString() << endl;
0103             QFAIL("Torrent check failure");
0104         }
0105     }
0106 
0107     void testPartial()
0108     {
0109         QMap<QString, bt::Uint64> files;
0110 
0111         files["aaa.avi"] = RandomSize(TEST_FILE_SIZE / 2, TEST_FILE_SIZE);
0112         files["bbb.avi"] = RandomSize(TEST_FILE_SIZE / 2, TEST_FILE_SIZE);
0113         files["ccc.avi"] = RandomSize(TEST_FILE_SIZE / 2, TEST_FILE_SIZE);
0114         files["ddd.avi"] = RandomSize(TEST_FILE_SIZE / 2, TEST_FILE_SIZE);
0115 
0116         DummyTorrentCreator creator;
0117         bt::TorrentControl tc;
0118         QVERIFY(creator.createMultiFileTorrent(files, "movies"));
0119 
0120         Out(SYS_GEN | LOG_DEBUG) << "Created " << creator.torrentPath() << endl;
0121         try {
0122             tc.init(nullptr, bt::LoadFile(creator.torrentPath()), creator.tempPath() + "tor0", creator.tempPath() + "data/");
0123             tc.createFiles();
0124         } catch (bt::Error &err) {
0125             Out(SYS_GEN | LOG_DEBUG) << "Failed to load torrent: " << creator.torrentPath() << endl;
0126             QFAIL("Torrent load failure");
0127         }
0128 
0129         for (Uint32 file = 0; file < tc.getNumFiles(); file++) {
0130             const bt::TorrentFileInterface &fi = tc.getTorrentFile(file);
0131             MultiDataChecker dc(fi.getFirstChunk(), fi.getLastChunk());
0132             try {
0133                 QString dnd = tc.getTorDir() + "dnd" + bt::DirSeparator();
0134                 dc.check(tc.getStats().output_path, tc.getTorrent(), dnd, tc.downloadedChunksBitSet());
0135                 for (Uint32 i = 0; i < tc.getStats().total_chunks; i++)
0136                     QVERIFY(dc.getResult().get(i) == (i >= fi.getFirstChunk() && i <= fi.getLastChunk()));
0137             } catch (bt::Error &err) {
0138                 Out(SYS_GEN | LOG_DEBUG) << "Datacheck failed: " << err.toString() << endl;
0139                 QFAIL("Torrent check failure");
0140             }
0141         }
0142     }
0143 
0144 private:
0145 };
0146 
0147 QTEST_MAIN(DataCheckerTest)
0148 
0149 #include "datacheckertest.moc"