File indexing completed on 2024-11-10 04:40:17
0001 /* 0002 * SPDX-FileCopyrightText: 2015 Daniel Vrátil <dvratil@redhat.com> 0003 * 0004 * SPDX-License-Identifier: LGPL-2.1-or-later 0005 * 0006 */ 0007 0008 #include "private/externalpartstorage_p.h" 0009 #include "private/standarddirs_p.h" 0010 #include "shared/aktest.h" 0011 0012 #include <QDir> 0013 #include <QFile> 0014 #include <QObject> 0015 0016 using namespace Akonadi; 0017 0018 class ExternalPartStorageTest : public QObject 0019 { 0020 Q_OBJECT 0021 0022 private Q_SLOTS: 0023 void testResolveAbsolutePath_data(); 0024 void testResolveAbsolutePath(); 0025 0026 void testUpdateFileNameRevision_data(); 0027 void testUpdateFileNameRevision(); 0028 0029 void testNameForPartId_data(); 0030 void testNameForPartId(); 0031 0032 void testPartCreate(); 0033 void testPartUpdate(); 0034 void testPartDelete(); 0035 void testPartCreateTrxRollback(); 0036 void testPartUpdateTrxRollback(); 0037 void testPartDeleteTrxRollback(); 0038 void testPartCreateTrxCommit(); 0039 void testPartUpdateTrxCommit(); 0040 void testPartDeleteTrxCommit(); 0041 }; 0042 0043 void ExternalPartStorageTest::testResolveAbsolutePath_data() 0044 { 0045 QTest::addColumn<QString>("filename"); 0046 QTest::addColumn<QString>("expectedLevelCache"); 0047 QTest::addColumn<bool>("isAbsolute"); 0048 0049 QTest::newRow("1_r0") << QStringLiteral("1_r0") << QStringLiteral("01") << false; 0050 QTest::newRow("23_r0") << QStringLiteral("23_r0") << QStringLiteral("23") << false; 0051 QTest::newRow("567_r0") << QStringLiteral("567_r0") << QStringLiteral("67") << false; 0052 QTest::newRow("123456_r0") << QStringLiteral("123456_r0") << QStringLiteral("56") << false; 0053 QTest::newRow("absolute path") << QStringLiteral("/tmp/akonadi/file_db_data/99_r3") << QString() << true; 0054 } 0055 0056 void ExternalPartStorageTest::testResolveAbsolutePath() 0057 { 0058 QFETCH(QString, filename); 0059 QFETCH(QString, expectedLevelCache); 0060 QFETCH(bool, isAbsolute); 0061 0062 // Calculate the new levelled-cache hierarchy path 0063 const QString expectedBasePath = StandardDirs::saveDir("data", QStringLiteral("file_db_data")) + QDir::separator(); 0064 const QString expectedPath = expectedBasePath + expectedLevelCache + QDir::separator(); 0065 const QString expectedFilePath = expectedPath + filename; 0066 0067 // File does not exist, will return path for the new levelled hierarchy 0068 // (unless the path is absolute, then it returns the absolute path) 0069 bool exists = false; 0070 QString path = ExternalPartStorage::resolveAbsolutePath(filename, &exists); 0071 QVERIFY(!exists); 0072 if (isAbsolute) { 0073 // Absolute path is no further resolved 0074 QCOMPARE(path, filename); 0075 return; 0076 } 0077 QCOMPARE(path, expectedFilePath); 0078 0079 // Create the file in the old flat hierarchy 0080 QFile(expectedBasePath + filename).open(QIODevice::WriteOnly); 0081 QCOMPARE(ExternalPartStorage::resolveAbsolutePath(filename, &exists), QString(expectedBasePath + filename)); 0082 QVERIFY(exists); 0083 QVERIFY(QFile::remove(expectedBasePath + filename)); 0084 0085 // Create the file in the new hierarchy - will return the same as the first 0086 QDir().mkpath(expectedPath); 0087 QFile(expectedFilePath).open(QIODevice::WriteOnly); 0088 exists = false; 0089 // Check that this time we got the new path and exists flag is correctly set 0090 path = ExternalPartStorage::resolveAbsolutePath(filename, &exists); 0091 QCOMPARE(path, expectedFilePath); 0092 QVERIFY(exists); 0093 0094 // Clean up 0095 QVERIFY(QFile::remove(path)); 0096 } 0097 0098 void ExternalPartStorageTest::testUpdateFileNameRevision_data() 0099 { 0100 QTest::addColumn<QByteArray>("name"); 0101 QTest::addColumn<QByteArray>("expectedName"); 0102 0103 QTest::newRow("no revision") << QByteArray("1234") << QByteArray("1234_r0"); 0104 QTest::newRow("r0") << QByteArray("1234_r0") << QByteArray("1234_r1"); 0105 QTest::newRow("r12") << QByteArray("1234_r12") << QByteArray("1234_r13"); 0106 QTest::newRow("r123456") << QByteArray("1234_r123456") << QByteArray("1234_r123457"); 0107 } 0108 0109 void ExternalPartStorageTest::testUpdateFileNameRevision() 0110 { 0111 QFETCH(QByteArray, name); 0112 QFETCH(QByteArray, expectedName); 0113 0114 const QByteArray newName = ExternalPartStorage::updateFileNameRevision(name); 0115 QCOMPARE(newName, expectedName); 0116 } 0117 0118 void ExternalPartStorageTest::testNameForPartId_data() 0119 { 0120 QTest::addColumn<qint64>("id"); 0121 QTest::addColumn<QByteArray>("expectedName"); 0122 0123 QTest::newRow("0") << 0LL << QByteArray("0_r0"); 0124 QTest::newRow("12") << 12LL << QByteArray("12_r0"); 0125 QTest::newRow("9876543") << 9876543LL << QByteArray("9876543_r0"); 0126 } 0127 0128 void ExternalPartStorageTest::testNameForPartId() 0129 { 0130 QFETCH(qint64, id); 0131 QFETCH(QByteArray, expectedName); 0132 0133 const QByteArray name = ExternalPartStorage::nameForPartId(id); 0134 QCOMPARE(name, expectedName); 0135 } 0136 0137 void ExternalPartStorageTest::testPartCreate() 0138 { 0139 QByteArray filename; 0140 QVERIFY(ExternalPartStorage::self()->createPartFile("blabla", 1, filename)); 0141 const QString filePath = ExternalPartStorage::resolveAbsolutePath(filename); 0142 QVERIFY(QFile::exists(filePath)); 0143 QFile f(filePath); 0144 QVERIFY(f.open(QIODevice::ReadOnly)); 0145 QCOMPARE(f.readAll(), QByteArray("blabla")); 0146 f.close(); 0147 QVERIFY(f.remove()); 0148 } 0149 0150 void ExternalPartStorageTest::testPartUpdate() 0151 { 0152 QByteArray filename; 0153 QVERIFY(ExternalPartStorage::self()->createPartFile("blabla", 10, filename)); 0154 const QString filePath = ExternalPartStorage::resolveAbsolutePath(filename); 0155 QVERIFY(QFile::exists(filePath)); 0156 0157 QByteArray newfilename; 0158 QVERIFY(ExternalPartStorage::self()->updatePartFile("newdata", filename, newfilename)); 0159 QCOMPARE(ExternalPartStorage::updateFileNameRevision(filename), newfilename); 0160 const QString newFilePath = ExternalPartStorage::resolveAbsolutePath(newfilename); 0161 QVERIFY(!QFile::exists(filePath)); 0162 QVERIFY(QFile::exists(newFilePath)); 0163 0164 QFile f(newFilePath); 0165 QVERIFY(f.open(QIODevice::ReadOnly)); 0166 QCOMPARE(f.readAll(), QByteArray("newdata")); 0167 f.close(); 0168 QVERIFY(f.remove()); 0169 } 0170 0171 void ExternalPartStorageTest::testPartDelete() 0172 { 0173 QByteArray filename; 0174 QVERIFY(ExternalPartStorage::self()->createPartFile("blabla", 2, filename)); 0175 const QString filePath = ExternalPartStorage::resolveAbsolutePath(filename); 0176 QVERIFY(QFile::exists(filePath)); 0177 QVERIFY(ExternalPartStorage::self()->removePartFile(filePath)); 0178 QVERIFY(!QFile::exists(filePath)); 0179 } 0180 0181 void ExternalPartStorageTest::testPartCreateTrxRollback() 0182 { 0183 ExternalPartStorageTransaction trx; 0184 QByteArray filename; 0185 QVERIFY(ExternalPartStorage::self()->createPartFile("blabla", 3, filename)); 0186 const QString filePath = ExternalPartStorage::resolveAbsolutePath(filename); 0187 QVERIFY(QFile::exists(filePath)); 0188 QFile f(filePath); 0189 QVERIFY(f.open(QIODevice::ReadOnly)); 0190 QCOMPARE(f.readAll(), QByteArray("blabla")); 0191 f.close(); 0192 QVERIFY(trx.rollback()); 0193 QVERIFY(!QFile::exists(filePath)); 0194 } 0195 0196 void ExternalPartStorageTest::testPartUpdateTrxRollback() 0197 { 0198 QByteArray filename; 0199 QVERIFY(ExternalPartStorage::self()->createPartFile("blabla", 10, filename)); 0200 const QString filePath = ExternalPartStorage::resolveAbsolutePath(filename); 0201 QVERIFY(QFile::exists(filePath)); 0202 0203 ExternalPartStorageTransaction trx; 0204 0205 QByteArray newfilename; 0206 QVERIFY(ExternalPartStorage::self()->updatePartFile("newdata", filename, newfilename)); 0207 QCOMPARE(ExternalPartStorage::updateFileNameRevision(filename), newfilename); 0208 const QString newFilePath = ExternalPartStorage::resolveAbsolutePath(newfilename); 0209 QVERIFY(QFile::exists(filePath)); 0210 QVERIFY(QFile::exists(newFilePath)); 0211 0212 QFile f(newFilePath); 0213 QVERIFY(f.open(QIODevice::ReadOnly)); 0214 QCOMPARE(f.readAll(), QByteArray("newdata")); 0215 f.close(); 0216 0217 trx.rollback(); 0218 QVERIFY(QFile::exists(filePath)); 0219 QVERIFY(!QFile::exists(newFilePath)); 0220 0221 QFile f2(filePath); 0222 QVERIFY(f2.open(QIODevice::ReadOnly)); 0223 QCOMPARE(f2.readAll(), QByteArray("blabla")); 0224 f2.close(); 0225 QVERIFY(f2.remove()); 0226 } 0227 0228 void ExternalPartStorageTest::testPartDeleteTrxRollback() 0229 { 0230 QByteArray filename; 0231 QVERIFY(ExternalPartStorage::self()->createPartFile("blabla", 4, filename)); 0232 const QString filePath = ExternalPartStorage::resolveAbsolutePath(filename); 0233 QVERIFY(QFile::exists(filePath)); 0234 0235 ExternalPartStorageTransaction trx; 0236 QVERIFY(ExternalPartStorage::self()->removePartFile(filePath)); 0237 QVERIFY(QFile::exists(filePath)); 0238 QVERIFY(trx.rollback()); 0239 QVERIFY(QFile::exists(filePath)); 0240 0241 QFile::remove(filePath); 0242 } 0243 0244 void ExternalPartStorageTest::testPartCreateTrxCommit() 0245 { 0246 ExternalPartStorageTransaction trx; 0247 QByteArray filename; 0248 QVERIFY(ExternalPartStorage::self()->createPartFile("blabla", 6, filename)); 0249 const QString filePath = ExternalPartStorage::resolveAbsolutePath(filename); 0250 QVERIFY(QFile::exists(filePath)); 0251 QVERIFY(trx.commit()); 0252 QVERIFY(QFile::exists(filePath)); 0253 0254 QFile f(filePath); 0255 QVERIFY(f.open(QIODevice::ReadOnly)); 0256 QCOMPARE(f.readAll(), QByteArray("blabla")); 0257 f.close(); 0258 QVERIFY(f.remove()); 0259 } 0260 0261 void ExternalPartStorageTest::testPartUpdateTrxCommit() 0262 { 0263 QByteArray filename; 0264 QVERIFY(ExternalPartStorage::self()->createPartFile("blabla", 10, filename)); 0265 const QString filePath = ExternalPartStorage::resolveAbsolutePath(filename); 0266 QVERIFY(QFile::exists(filePath)); 0267 0268 ExternalPartStorageTransaction trx; 0269 0270 QByteArray newfilename; 0271 QVERIFY(ExternalPartStorage::self()->updatePartFile("newdata", filename, newfilename)); 0272 QCOMPARE(ExternalPartStorage::updateFileNameRevision(filename), newfilename); 0273 const QString newFilePath = ExternalPartStorage::resolveAbsolutePath(newfilename); 0274 QVERIFY(QFile::exists(filePath)); 0275 QVERIFY(QFile::exists(newFilePath)); 0276 0277 QFile f(newFilePath); 0278 QVERIFY(f.open(QIODevice::ReadOnly)); 0279 QCOMPARE(f.readAll(), QByteArray("newdata")); 0280 f.close(); 0281 0282 trx.commit(); 0283 QVERIFY(!QFile::exists(filePath)); 0284 QVERIFY(QFile::exists(newFilePath)); 0285 QVERIFY(QFile::remove(newFilePath)); 0286 } 0287 0288 void ExternalPartStorageTest::testPartDeleteTrxCommit() 0289 { 0290 QByteArray filename; 0291 QVERIFY(ExternalPartStorage::self()->createPartFile("blabla", 7, filename)); 0292 const QString filePath = ExternalPartStorage::resolveAbsolutePath(filename); 0293 QVERIFY(QFile::exists(filePath)); 0294 0295 ExternalPartStorageTransaction trx; 0296 QVERIFY(ExternalPartStorage::self()->removePartFile(filePath)); 0297 QVERIFY(QFile::exists(filePath)); 0298 QVERIFY(trx.commit()); 0299 QVERIFY(!QFile::exists(filePath)); 0300 } 0301 0302 AKTEST_MAIN(ExternalPartStorageTest) 0303 0304 #include "externalpartstoragetest.moc"