File indexing completed on 2025-01-05 04:49:43
0001 /* 0002 SPDX-FileCopyrightText: 2018 Sandro Knauß <sknauss@kde.org> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "utils.h" 0008 #include <MessageViewer/HeaderStyle> 0009 #include <QFile> 0010 #include <QProcess> 0011 #include <QRegularExpression> 0012 #include <QStandardPaths> 0013 #include <QTest> 0014 0015 void testHeaderFile(const QString &data, const QString &name, const QString &dir) 0016 { 0017 QString header = QStringLiteral( 0018 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n" 0019 "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n" 0020 "<body>\n"); 0021 header += data; 0022 header += QStringLiteral("\n</body>\n</html>\n"); 0023 0024 QString imagePath(QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("libmessageviewer/pics"), QStandardPaths::LocateDirectory)); 0025 header.replace(QStringLiteral("file://") + imagePath, QStringLiteral("file://PATHTOIMAGES")); 0026 header.replace(QRegularExpression(QStringLiteral("[\t ]+")), QStringLiteral(" ")); 0027 header.replace(QRegularExpression(QStringLiteral("[\t ]*\n+[\t ]*")), QStringLiteral("\n")); 0028 header.replace(QRegularExpression(QStringLiteral("([\n\t ])\\1+")), QStringLiteral("\\1")); 0029 header.replace(QRegularExpression(QStringLiteral(">\n+[\t ]*")), QStringLiteral(">")); 0030 header.replace(QRegularExpression(QStringLiteral("[\t ]*\n+[\t ]*<")), QStringLiteral("<")); 0031 header.replace(QLatin1StringView(" "), QLatin1StringView("NBSP_ENTITY_PLACEHOLDER")); // xmlling chokes on 0032 0033 QString outName = name + QStringLiteral(".out.html"); 0034 QString fName = name + QStringLiteral(".html"); 0035 0036 QString referenceFile = QStringLiteral(HEADER_DATA_DIR "/"); 0037 if (!dir.isEmpty()) { 0038 referenceFile += dir + QStringLiteral("/"); 0039 } 0040 referenceFile += fName; 0041 0042 QVERIFY(QFile(referenceFile).exists()); 0043 0044 { 0045 QFile f(outName); 0046 f.open(QIODevice::WriteOnly); 0047 f.write(header.toUtf8()); 0048 f.close(); 0049 } 0050 // TODO add proper cmake check for xmllint and diff 0051 { 0052 const QStringList args = QStringList() << QStringLiteral("--format") << QStringLiteral("--encode") << QStringLiteral("UTF8") 0053 << QStringLiteral("--output") << fName << outName; 0054 QCOMPARE(QProcess::execute(QStringLiteral("xmllint"), args), 0); 0055 } 0056 0057 { 0058 // compare to reference file 0059 const QStringList args = QStringList() << QStringLiteral("-u") << fName << referenceFile; 0060 QProcess proc; 0061 proc.setProcessChannelMode(QProcess::ForwardedChannels); 0062 proc.start(QStringLiteral("diff"), args); 0063 QVERIFY(proc.waitForFinished()); 0064 0065 QCOMPARE(proc.exitCode(), 0); 0066 } 0067 } 0068 0069 KMime::Message::Ptr readAndParseMail(const QString &mailFile) 0070 { 0071 QFile file(QStringLiteral(HEADER_DATA_DIR) + QLatin1Char('/') + mailFile); 0072 bool openFile = file.open(QIODevice::ReadOnly); 0073 Q_ASSERT(openFile); 0074 const QByteArray data = KMime::CRLFtoLF(file.readAll()); 0075 Q_ASSERT(!data.isEmpty()); 0076 KMime::Message::Ptr msg(new KMime::Message); 0077 msg->setContent(data); 0078 msg->parse(); 0079 return msg; 0080 }