File indexing completed on 2024-12-22 05:05:17

0001 // SPDX-FileCopyrightText: 2023 g10 Code GmbH
0002 // SPDX-FileContributor: Carl Schwan <carl@carlschwan.eu>
0003 // SPDX-License-Identifier: LGPL-2.0-or-later
0004 
0005 #include <MimeTreeParserCore/FileOpener>
0006 
0007 #include <QTemporaryFile>
0008 #include <QTest>
0009 
0010 using namespace MimeTreeParser::Core;
0011 
0012 class FileOpenerTest : public QObject
0013 {
0014     Q_OBJECT
0015 
0016 private Q_SLOTS:
0017     void openSingleMboxTest()
0018     {
0019         const auto messages = FileOpener::openFile(QLatin1StringView(MAIL_DATA_DIR) + QLatin1Char('/') + QLatin1StringView("smime-opaque-enc+sign.mbox"));
0020         QCOMPARE(messages.count(), 1);
0021     }
0022 
0023     void openSingleCombinedTest()
0024     {
0025         const auto messages = FileOpener::openFile(QLatin1StringView(MAIL_DATA_DIR) + QLatin1Char('/') + QLatin1StringView("combined.mbox"));
0026         QCOMPARE(messages.count(), 3);
0027     }
0028 
0029     void openAscTest()
0030     {
0031         const auto messages = FileOpener::openFile(QLatin1StringView(MAIL_DATA_DIR) + QLatin1Char('/') + QLatin1StringView("msg.asc"));
0032         QCOMPARE(messages.count(), 1);
0033         auto message = messages[0];
0034 
0035         QCOMPARE(message->contentType()->mimeType(), "multipart/encrypted");
0036         QCOMPARE(message->contents().count(), 2);
0037 
0038         auto pgpPart = message->contents()[0];
0039         QCOMPARE(pgpPart->contentType()->mimeType(), "application/pgp-encrypted");
0040 
0041         auto octetStreamPart = message->contents()[1];
0042         QCOMPARE(octetStreamPart->contentType()->mimeType(), "application/octet-stream");
0043     }
0044 
0045     void openSmimeTest()
0046     {
0047         const auto messages = FileOpener::openFile(QLatin1StringView(MAIL_DATA_DIR) + QLatin1Char('/') + QLatin1StringView("smime.p7m"));
0048         QCOMPARE(messages.count(), 1);
0049         auto message = messages[0];
0050 
0051         QCOMPARE(message->contentType()->mimeType(), "application/pkcs7-mime");
0052         QCOMPARE(message->contentType()->parameter(QStringLiteral("smime-type")), QStringLiteral("enveloped-data"));
0053         QCOMPARE(message->contentDisposition()->filename(), QStringLiteral("smime.p7m"));
0054     }
0055 
0056     void openInexistingFileTest()
0057     {
0058         const auto messages = FileOpener::openFile(QLatin1StringView(MAIL_DATA_DIR) + QLatin1Char('/') + QLatin1StringView("not-here.p7m"));
0059         QCOMPARE(messages.count(), 0);
0060     }
0061 
0062     void openEmptyFile()
0063     {
0064         QTemporaryFile file;
0065         QVERIFY(file.open());
0066         const auto messages = FileOpener::openFile(file.fileName());
0067         QCOMPARE(messages.count(), 0);
0068     }
0069 };
0070 
0071 QTEST_GUILESS_MAIN(FileOpenerTest)
0072 #include "fileopenertest.moc"