File indexing completed on 2024-05-12 05:50:11
0001 /* 0002 SPDX-FileCopyrightText: 2023 Kristen McWilliam <kmcwilliampublic@gmail.com> 0003 0004 SPDX-License-Identifier: BSD-2-Clause 0005 */ 0006 0007 /** 0008 This test verifies that user metadata set on an archive through Dolphin - 0009 such as tags, comments, and ratings - are preserved when the archive is 0010 modified. 0011 */ 0012 0013 #include "abstractaddtest.h" 0014 #include "jobs.h" 0015 #include "testhelper.h" 0016 0017 #include <KIO/CopyJob> 0018 0019 #include <QTest> 0020 0021 using namespace Kerfuffle; 0022 0023 const QString TEST_DATA_DIR = QStringLiteral("data"); 0024 const QString ARCHIVE_NAME = QStringLiteral("archive-with-metadata.zip"); 0025 const QStringList EXPECTED_TAGS = QStringList() << QStringLiteral("testTag"); 0026 const int EXPECTED_RATING = 10; 0027 const QString EXPECTED_COMMENT = QStringLiteral("This is a comment"); 0028 0029 class PreserveMetadataTest : public AbstractAddTest 0030 { 0031 Q_OBJECT 0032 0033 private Q_SLOTS: 0034 void init(); 0035 void cleanup(); 0036 0037 void addFiles(); 0038 void copyFiles(); 0039 void deleteFiles(); 0040 void encrypt(); 0041 void extractFiles(); 0042 void moveFiles(); 0043 0044 private: 0045 QString archivePath; 0046 Archive *archive; 0047 QTemporaryDir temporaryDir; 0048 0049 void setMetadata(); 0050 void verifyMetadata(); 0051 }; 0052 0053 QTEST_GUILESS_MAIN(PreserveMetadataTest) 0054 0055 void PreserveMetadataTest::init() 0056 { 0057 QVERIFY(temporaryDir.isValid()); 0058 0059 const QString sourceArchive = QFINDTESTDATA(TEST_DATA_DIR + QLatin1Char('/') + ARCHIVE_NAME); 0060 archivePath = temporaryDir.path() + QLatin1Char('/') + ARCHIVE_NAME; 0061 0062 // Copy the archive to a temporary directory 0063 QFile::copy(sourceArchive, archivePath); 0064 0065 // Set the metadata on the archive 0066 setMetadata(); 0067 0068 // Verify the archive has the expected metadata before Ark modifies it 0069 verifyMetadata(); 0070 0071 // Load the archive 0072 auto loadJob = Archive::load(archivePath); 0073 QVERIFY(loadJob); 0074 loadJob->setAutoDelete(false); 0075 TestHelper::startAndWaitForResult(loadJob); 0076 archive = loadJob->archive(); 0077 QVERIFY(archive); 0078 const uint archiveEntries = archive->numberOfEntries(); 0079 QCOMPARE(archiveEntries, 2); 0080 loadJob->deleteLater(); 0081 } 0082 0083 void PreserveMetadataTest::cleanup() 0084 { 0085 // Clear the archive from the temporary directory 0086 QFile::remove(archivePath); 0087 archive->deleteLater(); 0088 } 0089 0090 void PreserveMetadataTest::addFiles() 0091 { 0092 // Add a file to the archive 0093 const QString newFileName = QStringLiteral("testfile.txt"); 0094 const QString newFilePath = QFINDTESTDATA(TEST_DATA_DIR + QLatin1Char('/') + newFileName); 0095 0096 QVector<Archive::Entry *> entries; 0097 entries.append(new Archive::Entry(this, newFilePath)); 0098 0099 CompressionOptions options; 0100 options.setGlobalWorkDir(QFINDTESTDATA(TEST_DATA_DIR)); 0101 Archive::Entry *destination = new Archive::Entry(this); 0102 auto addJob = archive->addFiles(entries, destination, options); 0103 0104 TestHelper::startAndWaitForResult(addJob); 0105 0106 // Verify the archive now has the expected number of entries 0107 QCOMPARE(archive->numberOfEntries(), 3); 0108 0109 // Verify the archive still has the expected metadata after Ark modified it 0110 verifyMetadata(); 0111 } 0112 0113 void PreserveMetadataTest::copyFiles() 0114 { 0115 // The name of a file that already exists in the archive 0116 const QString existingFileName = QStringLiteral("test.txt"); 0117 0118 QVector<Archive::Entry *> targetEntries; 0119 targetEntries.append(new Archive::Entry(this, existingFileName)); 0120 Archive::Entry destination = Archive::Entry(this, QStringLiteral("testCopy")); 0121 0122 CompressionOptions options; 0123 options.setGlobalWorkDir(QFINDTESTDATA(TEST_DATA_DIR)); 0124 CopyJob *copyJob = archive->copyFiles(targetEntries, &destination, options); 0125 TestHelper::startAndWaitForResult(copyJob); 0126 0127 // Verify the archive now has the expected number of entries 0128 QCOMPARE(archive->numberOfEntries(), 3); 0129 0130 // Verify the archive still has the expected metadata after Ark modified it 0131 verifyMetadata(); 0132 } 0133 0134 void PreserveMetadataTest::deleteFiles() 0135 { 0136 // The name of the file that will be deleted from the archive 0137 const QString fileForDeletionName = QStringLiteral("a.txt"); 0138 0139 QVector<Archive::Entry *> targetEntries; 0140 targetEntries.append(new Archive::Entry(this, fileForDeletionName)); 0141 0142 DeleteJob *deleteJob = archive->deleteFiles(targetEntries); 0143 TestHelper::startAndWaitForResult(deleteJob); 0144 0145 // Verify the archive now has the expected number of entries 0146 QCOMPARE(archive->numberOfEntries(), 1); 0147 0148 // Verify the archive still has the expected metadata after Ark modified it 0149 verifyMetadata(); 0150 } 0151 0152 void PreserveMetadataTest::encrypt() 0153 { 0154 QCOMPARE(archive->encryptionType(), Archive::EncryptionType::Unencrypted); 0155 0156 // The password to use for encryption 0157 const QString password = QStringLiteral("password"); 0158 0159 // Encrypt the archive 0160 bool encryptHeader = false; 0161 archive->encrypt(password, encryptHeader); 0162 QCOMPARE(archive->encryptionType(), Archive::EncryptionType::Encrypted); 0163 0164 // Verify the archive still has the expected metadata after Ark modified it 0165 verifyMetadata(); 0166 0167 // Extract the archive, including the list of files 0168 encryptHeader = true; 0169 archive->encrypt(password, encryptHeader); 0170 QCOMPARE(archive->encryptionType(), Archive::EncryptionType::HeaderEncrypted); 0171 0172 // Verify the archive still has the expected metadata after Ark modified it 0173 verifyMetadata(); 0174 } 0175 0176 void PreserveMetadataTest::extractFiles() 0177 { 0178 // The name of the file that will be extracted from the archive 0179 const QString fileForExtractionName = QStringLiteral("test.txt"); 0180 0181 QVector<Archive::Entry *> targetEntries; 0182 targetEntries.append(new Archive::Entry(this, fileForExtractionName)); 0183 0184 // Extract the file from the archive 0185 const QString destinationDir = temporaryDir.path(); 0186 ExtractJob *extractJob = archive->extractFiles(targetEntries, destinationDir); 0187 TestHelper::startAndWaitForResult(extractJob); 0188 0189 // Verify the archive still has the expected metadata after Ark modified it 0190 verifyMetadata(); 0191 } 0192 0193 void PreserveMetadataTest::moveFiles() 0194 { 0195 // The name of the file that will be moved within the archive 0196 const QString fileForMoveName = QStringLiteral("test.txt"); 0197 0198 QVector<Archive::Entry *> targetEntries; 0199 targetEntries.append(new Archive::Entry(this, fileForMoveName)); 0200 0201 Archive::Entry destination = Archive::Entry(this, QStringLiteral("test-moved.txt")); 0202 0203 CompressionOptions options; 0204 options.setGlobalWorkDir(QFINDTESTDATA(TEST_DATA_DIR)); 0205 MoveJob *moveJob = archive->moveFiles(targetEntries, &destination, options); 0206 TestHelper::startAndWaitForResult(moveJob); 0207 0208 // Verify the archive still has the expected number of entries 0209 QCOMPARE(archive->numberOfEntries(), 2); 0210 0211 // Verify the archive still has the expected metadata after Ark modified it 0212 verifyMetadata(); 0213 } 0214 0215 /** 0216 * Set the metadata on the archive to the expected values. 0217 */ 0218 void PreserveMetadataTest::setMetadata() 0219 { 0220 KFileMetaData::UserMetaData metaData(archivePath); 0221 metaData.setTags(EXPECTED_TAGS); 0222 metaData.setRating(EXPECTED_RATING); 0223 metaData.setUserComment(EXPECTED_COMMENT); 0224 } 0225 0226 /** 0227 * Verify the metadata on the archive matches the expected values. 0228 */ 0229 void PreserveMetadataTest::verifyMetadata() 0230 { 0231 KFileMetaData::UserMetaData metaData(archivePath); 0232 QCOMPARE(metaData.tags(), EXPECTED_TAGS); 0233 QCOMPARE(metaData.rating(), EXPECTED_RATING); 0234 QCOMPARE(metaData.userComment(), EXPECTED_COMMENT); 0235 } 0236 0237 #include "preservemetadatatest.moc"