File indexing completed on 2024-11-24 04:31:13

0001 /*
0002     SPDX-FileCopyrightText: 2010 Joris Guisson <joris.guisson@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include <QLocale>
0008 #include <QtTest>
0009 #include <diskio/chunkmanager.h>
0010 #include <diskio/piecedata.h>
0011 #include <testlib/dummytorrentcreator.h>
0012 #include <testlib/utils.h>
0013 #include <torrent/torrentcontrol.h>
0014 #include <util/fileops.h>
0015 #include <util/functions.h>
0016 #include <util/log.h>
0017 #include <util/sha1hashgen.h>
0018 #include <util/signalcatcher.h>
0019 
0020 using namespace bt;
0021 
0022 const bt::Uint64 TEST_FILE_SIZE = 15 * 1024 * 1024;
0023 
0024 class ChunkManagerTest : public QObject
0025 {
0026     Q_OBJECT
0027 
0028 private Q_SLOTS:
0029     void initTestCase()
0030     {
0031         QLocale::setDefault(QLocale("main"));
0032         bt::InitLibKTorrent();
0033         bt::InitLog("chunkmanagertest.log", false, true);
0034         QMap<QString, bt::Uint64> files;
0035 
0036         files["aaa.avi"] = RandomSize(TEST_FILE_SIZE / 2, TEST_FILE_SIZE);
0037         files["bbb.avi"] = RandomSize(TEST_FILE_SIZE / 2, TEST_FILE_SIZE);
0038         files["ccc.avi"] = RandomSize(TEST_FILE_SIZE / 2, TEST_FILE_SIZE);
0039 
0040         QVERIFY(creator.createMultiFileTorrent(files, "movies"));
0041 
0042         Out(SYS_GEN | LOG_DEBUG) << "Created " << creator.torrentPath() << endl;
0043         try {
0044             tor.load(bt::LoadFile(creator.torrentPath()), false);
0045         } catch (bt::Error &err) {
0046             Out(SYS_GEN | LOG_DEBUG) << "Failed to load torrent: " << creator.torrentPath() << endl;
0047             QFAIL("Torrent load failure");
0048         }
0049     }
0050 
0051     void cleanupTestCase()
0052     {
0053     }
0054 
0055     void testPieceDataLoading()
0056     {
0057         ChunkManager cman(tor, creator.tempPath(), creator.dataPath(), true, nullptr);
0058         Chunk *c = cman.getChunk(0);
0059         QVERIFY(c);
0060 
0061         try {
0062             PieceData::Ptr ptr = c->getPiece(0, MAX_PIECE_LEN, true);
0063             QVERIFY(ptr && ptr->data() && ptr->inUse());
0064 
0065             PieceData::Ptr f = c->getPiece(0, c->getSize(), true);
0066             QVERIFY(f);
0067 
0068             f = PieceData::Ptr();
0069             QVERIFY(!f);
0070 
0071             cman.checkMemoryUsage();
0072             Uint8 tmp[20];
0073             QVERIFY(memcpy(tmp, ptr->data(), 20));
0074 
0075             memset(tmp, 0xFF, 20);
0076             QVERIFY(!ptr->writeable());
0077             ptr->write(tmp, 20);
0078             QFAIL("No exception thrown");
0079         } catch (bt::Error &err) {
0080         }
0081     }
0082 
0083 #ifndef Q_CC_MSVC
0084     void testBusErrorHandling()
0085     {
0086         ChunkManager cman(tor, creator.tempPath(), creator.dataPath(), true, nullptr);
0087         Chunk *c = cman.getChunk(0);
0088         QVERIFY(c);
0089 
0090         PieceData::Ptr f = c->getPiece(0, c->getSize(), false);
0091         QVERIFY(f);
0092         QVERIFY(f->writeable());
0093 
0094         try {
0095             QString path = creator.dataPath() + tor.getFile(0).getPath();
0096             bt::TruncateFile(path, 0);
0097 
0098             Uint8 tmp[20];
0099             memset(tmp, 0xFF, 20);
0100 
0101             f->write(tmp, 20);
0102             QFAIL("No BusError thrown\n");
0103         } catch (bt::BusError &err) {
0104         } catch (bt::Error &err) {
0105             QFAIL("Truncate failed\n");
0106         }
0107     }
0108 
0109 #endif
0110 
0111 private:
0112     DummyTorrentCreator creator;
0113     bt::Torrent tor;
0114 };
0115 
0116 QTEST_MAIN(ChunkManagerTest)
0117 
0118 #include "chunkmanagertest.moc"