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

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 <KConfigGroup>
0008 #include <KSharedConfig>
0009 #include <QObject>
0010 #include <QtTest>
0011 #include <ctime>
0012 #include <torrent/statsfile.h>
0013 #include <util/log.h>
0014 
0015 using namespace bt;
0016 
0017 QString test_data =
0018     "ASSURED_DOWNLOAD_SPEED=0\n\
0019 ASSURED_UPLOAD_SPEED=0\n\
0020 AUTOSTART=0\n\
0021 CUSTOM_OUTPUT_NAME=0\n\
0022 DHT=1\n\
0023 DISPLAY_NAME=\n\
0024 DOWNLOAD_LIMIT=0\n\
0025 ENCODING=UTF-8\n\
0026 IMPORTED=0\n\
0027 MAX_RATIO=3.00\n\
0028 MAX_SEED_TIME=30\n\
0029 OUTPUTDIR=/home/joris/ktorrent/downloads/\n\
0030 PRIORITY=4\n\
0031 QM_CAN_START=0\n\
0032 RESTART_DISK_PREALLOCATION=0\n\
0033 RUNNING_TIME_DL=7042\n\
0034 RUNNING_TIME_UL=7042\n\
0035 TIME_ADDED=1265650102\n\
0036 UPLOADED=0\n\
0037 UPLOAD_LIMIT=0\n\
0038 URL=file:///home/joris/tmp/Killers.torrent\n\
0039 UT_PEX=1\n";
0040 
0041 class StatsFileTest : public QEventLoop
0042 {
0043     Q_OBJECT
0044 public:
0045 private Q_SLOTS:
0046     void initTestCase()
0047     {
0048         bt::InitLog("statsfiletest.log", false, false);
0049         QVERIFY(file.open());
0050         file.setAutoRemove(true);
0051         QTextStream out(&file);
0052         out << test_data;
0053 
0054         const QStringList lines = test_data.split("\n");
0055         for (const QString &line : lines) {
0056             QStringList sl = line.split("=");
0057             if (sl.count() == 2) {
0058                 keys.append(sl[0]);
0059                 values.append(sl[1]);
0060             }
0061         }
0062     }
0063 
0064     void cleanupTestCase()
0065     {
0066     }
0067 
0068     void testRead()
0069     {
0070         StatsFile st(file.fileName());
0071 
0072         int idx = 0;
0073         for (const QString &key : std::as_const(keys)) {
0074             QVERIFY(st.hasKey(key));
0075             QVERIFY(st.readString(key) == values[idx++]);
0076         }
0077 
0078         QVERIFY(st.readInt("RUNNING_TIME_DL") == 7042);
0079         QVERIFY(st.readInt("RUNNING_TIME_UL") == 7042);
0080         QVERIFY(st.readBoolean("DHT") == true);
0081     }
0082 
0083     void testWrite()
0084     {
0085         StatsFile sta(file.fileName());
0086         sta.write("DINGES", "1234");
0087         sta.sync();
0088 
0089         StatsFile stb(file.fileName());
0090         QVERIFY(stb.readInt("DINGES") == 1234);
0091     }
0092 
0093 private:
0094     QTemporaryFile file;
0095     QStringList keys;
0096     QStringList values;
0097 };
0098 
0099 QTEST_MAIN(StatsFileTest)
0100 
0101 #include "statsfiletest.moc"