#include #include #include #include #include #include "backend/AppState.h" #include "backend/state/BookPositionStore.h" class BookPositionStoreTest : public QObject { Q_OBJECT private slots: void defaultPositionForUnknownHash() { QTemporaryFile f; f.setFileTemplate(QDir::tempPath() + QStringLiteral("/bps-def-XXXXXX.ini")); (void)f.open(); f.close(); QSettings s(f.fileName(), QSettings::IniFormat); AppState appState(s); BookPositionStore store(appState); const BookPosition pos = store.load(QByteArray("nonexistent")); QCOMPARE(pos.chunkIndex, 0); QCOMPARE(pos.intraOffset, 0); } void saveAndLoad() { QTemporaryFile f; f.setFileTemplate(QDir::tempPath() + QStringLiteral("/bps-sr-XXXXXX.ini")); (void)f.open(); f.close(); QSettings s(f.fileName(), QSettings::IniFormat); AppState appState(s); BookPositionStore store(appState); const QByteArray hash = QByteArray("abc123deadbeef"); store.save(hash, 7, 42); const BookPosition pos = store.load(hash); QCOMPARE(pos.chunkIndex, 7); QCOMPARE(pos.intraOffset, 42); } void separateHashesStoredIndependently() { QTemporaryFile f; f.setFileTemplate(QDir::tempPath() + QStringLiteral("/bps-sep-XXXXXX.ini")); (void)f.open(); f.close(); QSettings s(f.fileName(), QSettings::IniFormat); AppState appState(s); BookPositionStore store(appState); store.save(QByteArray("hash1"), 1, 10); store.save(QByteArray("hash2"), 2, 20); QCOMPARE(store.load(QByteArray("hash1")).chunkIndex, 1); QCOMPARE(store.load(QByteArray("hash2")).chunkIndex, 2); QCOMPARE(store.load(QByteArray("hash1")).intraOffset, 10); QCOMPARE(store.load(QByteArray("hash2")).intraOffset, 20); } void overwriteExistingPosition() { QTemporaryFile f; f.setFileTemplate(QDir::tempPath() + QStringLiteral("/bps-ow-XXXXXX.ini")); (void)f.open(); f.close(); QSettings s(f.fileName(), QSettings::IniFormat); AppState appState(s); BookPositionStore store(appState); const QByteArray hash = QByteArray("overwrite-hash"); store.save(hash, 3, 30); store.save(hash, 5, 55); const BookPosition pos = store.load(hash); QCOMPARE(pos.chunkIndex, 5); QCOMPARE(pos.intraOffset, 55); } void emptyHashIgnored() { QTemporaryFile f; f.setFileTemplate(QDir::tempPath() + QStringLiteral("/bps-empty-XXXXXX.ini")); (void)f.open(); f.close(); QSettings s(f.fileName(), QSettings::IniFormat); AppState appState(s); BookPositionStore store(appState); // Neither save nor load should crash with an empty hash store.save(QByteArray(), 1, 1); const BookPosition pos = store.load(QByteArray()); QCOMPARE(pos.chunkIndex, 0); QCOMPARE(pos.intraOffset, 0); } }; QTEST_MAIN(BookPositionStoreTest) #include "BookPositionStoreTest.moc"