File indexing completed on 2024-11-24 04:44:09
0001 /* 0002 * SPDX-FileCopyrightText: 2012 Christian Mollekopf <mollekopf@kolabsys.com> 0003 * 0004 * SPDX-License-Identifier: LGPL-3.0-or-later 0005 */ 0006 0007 #include "formattest.h" 0008 0009 #include <QBuffer> 0010 #include <QDebug> 0011 #include <QFile> 0012 #include <QTest> 0013 #include <kolabcontainers.h> 0014 #include <kolabformat.h> 0015 0016 #include <Akonadi/NoteUtils> 0017 0018 #include <KCalendarCore/ICalFormat> 0019 #include <KContacts/ContactGroupTool> 0020 #include <KContacts/VCardConverter> 0021 0022 #include "kolabformat/errorhandler.h" 0023 #include "kolabformat/kolabdefinitions.h" 0024 #include "kolabformat/kolabobject.h" 0025 #include "pimkolab_debug.h" 0026 #include "testutils.h" 0027 static bool compareMimeMessage(const KMime::Message::Ptr &msg, const KMime::Message::Ptr &expectedMsg) 0028 { 0029 // headers 0030 KCOMPARE(msg->subject()->asUnicodeString(), expectedMsg->subject()->asUnicodeString()); 0031 if (msg->from()->isEmpty() || expectedMsg->from()->isEmpty()) { 0032 KCOMPARE(msg->from()->asUnicodeString(), expectedMsg->from()->asUnicodeString()); 0033 } else { 0034 KCOMPARE(msg->from()->mailboxes().first().address(), 0035 expectedMsg->from()->mailboxes().first().address()); // matching address is enough, we don't need a display name 0036 } 0037 KCOMPARE(msg->contentType()->mimeType(), expectedMsg->contentType()->mimeType()); 0038 KCOMPARE(msg->headerByType(X_KOLAB_TYPE_HEADER)->as7BitString(), expectedMsg->headerByType(X_KOLAB_TYPE_HEADER)->as7BitString()); 0039 // date contains conversion time... 0040 // KCOMPARE( msg->date()->asUnicodeString(), expectedMsg->date()->asUnicodeString() ); 0041 0042 // body parts 0043 KCOMPARE(msg->contents().size(), expectedMsg->contents().size()); 0044 for (int i = 0; i < msg->contents().size(); ++i) { 0045 KMime::Content *part = msg->contents().at(i); 0046 KMime::Content *expectedPart = expectedMsg->contents().at(i); 0047 0048 // part headers 0049 KCOMPARE(part->contentType()->mimeType(), expectedPart->contentType()->mimeType()); 0050 KCOMPARE(part->contentDisposition()->filename(), expectedPart->contentDisposition()->filename()); 0051 0052 KCOMPARE(part->decodedContent().isEmpty(), false); 0053 0054 QString content(QString::fromUtf8(part->decodedContent())); 0055 normalizeMimemessage(content); 0056 QString expected(QString::fromUtf8(expectedPart->decodedContent())); 0057 normalizeMimemessage(expected); 0058 // showDiff(expected, content); 0059 0060 // part content 0061 KCOMPARE(content.simplified(), expected.simplified()); 0062 } 0063 return true; 0064 } 0065 0066 void FormatTest::initTestCase() 0067 { 0068 } 0069 0070 void FormatTest::testIncidence_data() 0071 { 0072 QTest::addColumn<Kolab::Version>("version"); 0073 QTest::addColumn<Kolab::ObjectType>("type"); 0074 QTest::addColumn<QString>("icalFileName"); 0075 QTest::addColumn<QString>("mimeFileName"); 0076 0077 QTest::newRow("v2eventSimple") << Kolab::KolabV2 << Kolab::EventObject << getPath("v2/event/simple.ics") << getPath("v2/event/simple.ics.mime"); 0078 QTest::newRow("v2eventComplex") << Kolab::KolabV2 << Kolab::EventObject << getPath("v2/event/complex.ics") << getPath("v2/event/complex.ics.mime"); 0079 QTest::newRow("v2eventAttachment") << Kolab::KolabV2 << Kolab::EventObject << getPath("v2/event/attachment.ics") << getPath("v2/event/attachment.ics.mime"); 0080 QTest::newRow("v2eventAllday") << Kolab::KolabV2 << Kolab::EventObject << getPath("v2/event/allday.ics") << getPath("v2/event/allday.ics.mime"); 0081 QTest::newRow("v2eventUtf8Attachment") << Kolab::KolabV2 << Kolab::EventObject << getPath("v2/event/attachmentUtf8.ics") 0082 << getPath("v2/event/attachmentUtf8.ics.mime"); 0083 // The following test just fails because we have a nicer mime message output than horde 0084 // QTest::newRow( "v2eventHorde" ) << Kolab::KolabV2 << Kolab::EventObject << getPath("v2/event/horde.ics") << getPath("v2/event/horde.ics.mime"); 0085 QTest::newRow("v2todoSimple") << Kolab::KolabV2 << Kolab::TodoObject << getPath("v2/task/simple.ics") << getPath("v2/task/simple.ics.mime"); 0086 QTest::newRow("v2todoComplex") << Kolab::KolabV2 << Kolab::TodoObject << getPath("v2/task/complex.ics") << getPath("v2/task/complex.ics.mime"); 0087 QTest::newRow("v2todoPrio1") << Kolab::KolabV2 << Kolab::TodoObject << getPath("v2/task/prioritytest1.ics") << getPath("v2/task/prioritytest1.ics.mime"); 0088 QTest::newRow("v2todoPrio2") << Kolab::KolabV2 << Kolab::TodoObject << getPath("v2/task/prioritytest2.ics") << getPath("v2/task/prioritytest2.ics.mime"); 0089 QTest::newRow("v2journalSimple") << Kolab::KolabV2 << Kolab::JournalObject << getPath("v2/journal/simple.ics") << getPath("v2/journal/simple.ics.mime"); 0090 QTest::newRow("v2journalComplex") << Kolab::KolabV2 << Kolab::JournalObject << getPath("v2/journal/complex.ics") << getPath("v2/journal/complex.ics.mime"); 0091 0092 QTest::newRow("v3eventSimple") << Kolab::KolabV3 << Kolab::EventObject << getPath("v3/event/simple.ics") << getPath("v3/event/simple.ics.mime"); 0093 QTest::newRow("v3eventComplex") << Kolab::KolabV3 << Kolab::EventObject << getPath("v3/event/complex.ics") << getPath("v3/event/complex.ics.mime"); 0094 QTest::newRow("v3todoSimple") << Kolab::KolabV3 << Kolab::TodoObject << getPath("v3/task/simple.ics") << getPath("v3/task/simple.ics.mime"); 0095 QTest::newRow("v3todoComplex") << Kolab::KolabV3 << Kolab::TodoObject << getPath("v3/task/complex.ics") << getPath("v3/task/complex.ics.mime"); 0096 QTest::newRow("v3journalSimple") << Kolab::KolabV3 << Kolab::JournalObject << getPath("v3/journal/simple.ics") << getPath("v3/journal/simple.ics.mime"); 0097 QTest::newRow("v3journalComplex") << Kolab::KolabV3 << Kolab::JournalObject << getPath("v3/journal/complex.ics") << getPath("v3/journal/complex.ics.mime"); 0098 QTest::newRow("v3utf8quotedPrintable") << Kolab::KolabV3 << Kolab::EventObject << getPath("v3/event/utf8.ics") 0099 << getPath("v3/event/utf8quotedPrintable.ics.mime"); 0100 QTest::newRow("v3utf8base64") << Kolab::KolabV3 << Kolab::EventObject << getPath("v3/event/utf8.ics") << getPath("v3/event/utf8base64.ics.mime"); 0101 QTest::newRow("v3utf88bit") << Kolab::KolabV3 << Kolab::EventObject << getPath("v3/event/utf8.ics") << getPath("v3/event/utf88bit.ics.mime"); 0102 } 0103 0104 void FormatTest::testIncidence() 0105 { 0106 QFETCH(Kolab::Version, version); 0107 QFETCH(Kolab::ObjectType, type); 0108 QFETCH(QString, icalFileName); // To compare 0109 QFETCH(QString, mimeFileName); // For parsing 0110 0111 // Parse mime message 0112 bool ok = false; 0113 const KMime::Message::Ptr &msg = readMimeFile(mimeFileName, ok); 0114 QVERIFY(ok); 0115 Kolab::KolabObjectReader reader; 0116 Kolab::ObjectType t = reader.parseMimeMessage(msg); 0117 QCOMPARE(t, type); 0118 QCOMPARE(reader.getVersion(), version); 0119 QCOMPARE(Kolab::ErrorHandler::instance().error(), Kolab::ErrorHandler::Debug); 0120 0121 KCalendarCore::Incidence::Ptr convertedIncidence = reader.getIncidence(); 0122 0123 // Parse ICalFile for comparison 0124 QFile icalFile(icalFileName); 0125 QVERIFY(icalFile.open(QFile::ReadOnly)); 0126 KCalendarCore::ICalFormat format; 0127 KCalendarCore::Incidence::Ptr realIncidence(format.fromString(QString::fromUtf8(icalFile.readAll()))); 0128 0129 // fix up the converted incidence for comparison 0130 normalizeIncidence(convertedIncidence); 0131 normalizeIncidence(realIncidence); 0132 0133 // recurrence objects are created on demand, but KCalendarCore::Incidence::operator==() doesn't take that into account 0134 // so make sure both incidences have one 0135 realIncidence->recurrence(); 0136 convertedIncidence->recurrence(); 0137 0138 realIncidence->setLastModified(convertedIncidence->lastModified()); 0139 0140 // The following test is just for debugging and not really relevant 0141 if (*(realIncidence.data()) != *(convertedIncidence.data())) { 0142 showDiff(format.toString(realIncidence), format.toString(convertedIncidence)); 0143 } 0144 QVERIFY(*(realIncidence.data()) == *(convertedIncidence.data())); 0145 0146 // Write 0147 Kolab::overrideTimestamp(Kolab::cDateTime(2012, 5, 5, 5, 5, 5, true)); 0148 KMime::Message::Ptr convertedMime = Kolab::KolabObjectWriter::writeIncidence(realIncidence, version); 0149 0150 if (!compareMimeMessage(convertedMime, msg)) { 0151 showDiff(QString::fromUtf8(msg->encodedContent()), QString::fromUtf8(convertedMime->encodedContent())); 0152 QVERIFY(false); 0153 } 0154 QCOMPARE(Kolab::ErrorHandler::instance().error(), Kolab::ErrorHandler::Debug); 0155 } 0156 0157 enum TestMode { 0158 ReadOnly, 0159 ReadWrite, 0160 }; 0161 Q_DECLARE_METATYPE(TestMode) 0162 0163 void FormatTest::testContact_data() 0164 { 0165 QTest::addColumn<Kolab::Version>("version"); 0166 QTest::addColumn<Kolab::ObjectType>("type"); 0167 QTest::addColumn<QString>("vcardFileName"); 0168 QTest::addColumn<QString>("mimeFileName"); 0169 QTest::addColumn<TestMode>("mode"); 0170 0171 QTest::newRow("v2contactSimple") << Kolab::KolabV2 << Kolab::ContactObject << getPath("v2/contacts/simple.vcf") << getPath("v2/contacts/simple.vcf.mime") 0172 << ReadWrite; 0173 // FIXME Reference files needs to be adjusted due to fix in how pictures are stored 0174 // QTest::newRow( "v2contactComplex" ) << Kolab::KolabV2 << Kolab::ContactObject << getPath("v2/contacts/complex.vcf") << 0175 // getPath("v2/contacts/complex.vcf.mime") << ReadWrite; 0176 QTest::newRow("v2contactAddress") << Kolab::KolabV2 << Kolab::ContactObject << getPath("v2/contacts/address.vcf") << getPath("v2/contacts/address.vcf.mime") 0177 << ReadWrite; 0178 QTest::newRow("v2contactBug238996") << Kolab::KolabV2 << Kolab::ContactObject << getPath("v2/contacts/bug238996.vcf") 0179 << getPath("v2/contacts/bug238996.vcf.mime") << ReadWrite; 0180 QTest::newRow("v2contactDisplayname") << Kolab::KolabV2 << Kolab::ContactObject << getPath("v2/contacts/displayname.vcf") 0181 << getPath("v2/contacts/displayname.vcf.mime") << ReadWrite; 0182 QTest::newRow("v2contactEmails") << Kolab::KolabV2 << Kolab::ContactObject << getPath("v2/contacts/emails.vcf") << getPath("v2/contacts/emails.vcf.mime") 0183 << ReadWrite; 0184 QTest::newRow("v2contactPhonenumbers") << Kolab::KolabV2 << Kolab::ContactObject << getPath("v2/contacts/phonenumbers.vcf") 0185 << getPath("v2/contacts/phonenumbers.vcf.mime") << ReadWrite; 0186 // FIXME Reference files needs to be adjusted due to fix in how pictures are stored 0187 // QTest::newRow( "v2contactPicture" ) << Kolab::KolabV2 << Kolab::ContactObject << getPath("v2/contacts/picture.vcf") << 0188 // getPath("v2/contacts/picture.vcf.mime") << ReadWrite; 0189 // FIXME the following test fails because the vcard implementation always writes jpeg (which is lossy). The reference vcf file is therefore probably also 0190 // not really useful 0191 // QTest::newRow( "v2pictureJPGHorde" ) << Kolab::KolabV2 << Kolab::ContactObject << getPath("v2/contacts/pictureJPGHorde.vcf") << 0192 // getPath("v2/contacts/pictureJPGHorde.vcf.mime"); 0193 0194 QTest::newRow("v2contactRelated") << Kolab::KolabV2 << Kolab::ContactObject << getPath("v2/contacts/related.vcf") << getPath("v2/contacts/related.vcf.mime") 0195 << ReadWrite; 0196 0197 QTest::newRow("v3contactSimple") << Kolab::KolabV3 << Kolab::ContactObject << getPath("v3/contacts/simple.vcf") << getPath("v3/contacts/simple.vcf.mime") 0198 << ReadWrite; 0199 QTest::newRow("v3contactComplex") << Kolab::KolabV3 << Kolab::ContactObject << getPath("v3/contacts/complex.vcf") << getPath("v3/contacts/complex.vcf.mime") 0200 << ReadWrite; 0201 QTest::newRow("v3contactPng") << Kolab::KolabV3 << Kolab::ContactObject << getPath("v3/readonly/png.vcf") << getPath("v3/readonly/png.vcf.mime") 0202 << ReadOnly; 0203 } 0204 0205 bool comparePictureToReference(const QImage &picture) 0206 { 0207 QImage img(getPath("picture.jpg")); 0208 QByteArray pic; 0209 QBuffer buffer(&pic); 0210 buffer.open(QIODevice::WriteOnly); 0211 img.save(&buffer, "JPEG"); 0212 buffer.close(); 0213 0214 QByteArray pic2; 0215 QBuffer buffer2(&pic2); 0216 buffer2.open(QIODevice::WriteOnly); 0217 picture.save(&buffer2, "JPEG"); 0218 buffer2.close(); 0219 0220 if (pic.toBase64() != pic2.toBase64()) { 0221 qDebug() << pic.toBase64(); 0222 qDebug() << pic2.toBase64(); 0223 return false; 0224 } 0225 return true; 0226 } 0227 0228 void FormatTest::testContact() 0229 { 0230 QFETCH(Kolab::Version, version); 0231 QFETCH(Kolab::ObjectType, type); 0232 QFETCH(QString, vcardFileName); // To compare 0233 QFETCH(QString, mimeFileName); // For parsing 0234 QFETCH(TestMode, mode); // For parsing 0235 0236 // Parse mime message 0237 bool ok = false; 0238 const KMime::Message::Ptr &msg = readMimeFile(mimeFileName, ok); 0239 QVERIFY(ok); 0240 Kolab::KolabObjectReader reader; 0241 Kolab::ObjectType t = reader.parseMimeMessage(msg); 0242 QCOMPARE(t, type); 0243 QCOMPARE(reader.getVersion(), version); 0244 QCOMPARE(Kolab::ErrorHandler::instance().error(), Kolab::ErrorHandler::Debug); 0245 0246 KContacts::Addressee convertedAddressee = reader.getContact(); 0247 QVERIFY(!convertedAddressee.isEmpty()); 0248 0249 // Parse vcard 0250 QFile vcardFile(vcardFileName); 0251 QVERIFY(vcardFile.open(QFile::ReadOnly)); 0252 KContacts::VCardConverter converter; 0253 const QByteArray &c = vcardFile.readAll(); 0254 KContacts::Addressee realAddressee = converter.parseVCard(c); 0255 0256 // fix up the converted addressee for comparison 0257 convertedAddressee.setName(realAddressee.name()); // name() apparently is something strange 0258 if (version == Kolab::KolabV2) { // No creation date in xcal 0259 QVERIFY(!convertedAddressee.custom(QStringLiteral("KOLAB"), QStringLiteral("CreationDate")).isEmpty()); 0260 convertedAddressee.removeCustom(QStringLiteral("KOLAB"), QStringLiteral("CreationDate")); // that's conversion time !? 0261 } else { 0262 normalizeContact(convertedAddressee); 0263 normalizeContact(realAddressee); 0264 } 0265 QVERIFY(normalizePhoneNumbers(convertedAddressee, realAddressee)); // phone number ids are random 0266 QVERIFY(normalizeAddresses(convertedAddressee, realAddressee)); // same here 0267 QCOMPARE(realAddressee.photo().type(), convertedAddressee.photo().type()); 0268 if (realAddressee != convertedAddressee) { 0269 showDiff(normalizeVCardMessage(QString::fromUtf8(converter.createVCard(realAddressee))), 0270 normalizeVCardMessage(QString::fromUtf8(converter.createVCard(convertedAddressee)))); 0271 } 0272 QEXPECT_FAIL("v2contactBug238996", "Currently fails due to missing type=pref attribute of preferred email address. Requires fix in KContacts.", Continue); 0273 QEXPECT_FAIL("v2contactEmails", "Currently fails due to missing type=pref attribute of preferred email address. Requires fix in KContacts.", Continue); 0274 QEXPECT_FAIL("v3contactComplex", "Currently fails due to missing type=pref attribute of preferred email address. Requires fix in KContacts.", Continue); 0275 QCOMPARE(realAddressee, convertedAddressee); 0276 0277 // Write 0278 if (mode == ReadWrite) { 0279 Kolab::overrideTimestamp(Kolab::cDateTime(2012, 5, 5, 5, 5, 5, true)); 0280 const KMime::Message::Ptr &convertedMime = Kolab::KolabObjectWriter::writeContact(realAddressee, version); 0281 0282 if (!compareMimeMessage(convertedMime, msg)) { 0283 QString expected = QString::fromUtf8(msg->encodedContent()); 0284 normalizeMimemessage(expected); 0285 QString converted = QString::fromUtf8(convertedMime->encodedContent()); 0286 normalizeMimemessage(converted); 0287 showDiff(expected, converted); 0288 QEXPECT_FAIL("v2contactSimple", "The kolab v3 containers don't support postbox, and we therefore loose it in the transformations.", Continue); 0289 QEXPECT_FAIL("v2contactAddress", "The kolab v3 containers don't support postbox, and we therefore loose it in the transformations.", Continue); 0290 QEXPECT_FAIL("v2contactBug238996", "The kolab v3 containers don't support postbox, and we therefore loose it in the transformations.", Continue); 0291 QVERIFY(false); 0292 } 0293 } 0294 QCOMPARE(Kolab::ErrorHandler::instance().error(), Kolab::ErrorHandler::Debug); 0295 } 0296 0297 void FormatTest::testDistlist_data() 0298 { 0299 QTest::addColumn<Kolab::Version>("version"); 0300 QTest::addColumn<Kolab::ObjectType>("type"); 0301 QTest::addColumn<QString>("vcardFileName"); 0302 QTest::addColumn<QString>("mimeFileName"); 0303 0304 QTest::newRow("v3distlistSimple") << Kolab::KolabV3 << Kolab::DistlistObject << getPath("v3/contacts/distlist.vcf") 0305 << getPath("v3/contacts/distlist.vcf.mime"); 0306 } 0307 0308 void FormatTest::testDistlist() 0309 { 0310 QFETCH(Kolab::Version, version); 0311 QFETCH(Kolab::ObjectType, type); 0312 QFETCH(QString, vcardFileName); // To compare 0313 QFETCH(QString, mimeFileName); // For parsing 0314 0315 // Parse mime message 0316 bool ok = false; 0317 const KMime::Message::Ptr &msg = readMimeFile(mimeFileName, ok); 0318 QVERIFY(ok); 0319 Kolab::KolabObjectReader reader; 0320 Kolab::ObjectType t = reader.parseMimeMessage(msg); 0321 QCOMPARE(t, type); 0322 QCOMPARE(reader.getVersion(), version); 0323 QCOMPARE(Kolab::ErrorHandler::instance().error(), Kolab::ErrorHandler::Debug); 0324 0325 KContacts::ContactGroup convertedAddressee = reader.getDistlist(); 0326 0327 // Parse vcard 0328 QFile vcardFile(vcardFileName); 0329 QVERIFY(vcardFile.open(QFile::ReadOnly)); 0330 KContacts::VCardConverter converter; 0331 QByteArray c = vcardFile.readAll(); 0332 QBuffer data(&c); 0333 data.open(QIODevice::ReadOnly); 0334 0335 KContacts::ContactGroup realAddressee; 0336 KContacts::ContactGroupTool::convertFromXml(&data, realAddressee); 0337 0338 { 0339 QBuffer expected; 0340 expected.open(QIODevice::WriteOnly); 0341 KContacts::ContactGroupTool::convertToXml(realAddressee, &expected); 0342 0343 QBuffer converted; 0344 converted.open(QIODevice::WriteOnly); 0345 KContacts::ContactGroupTool::convertToXml(convertedAddressee, &converted); 0346 0347 showDiff(QString::fromUtf8(expected.buffer()), QString::fromUtf8(converted.buffer())); 0348 } 0349 QCOMPARE(realAddressee, convertedAddressee); 0350 0351 // Write 0352 const KMime::Message::Ptr &convertedMime = Kolab::KolabObjectWriter::writeDistlist(realAddressee, version); 0353 0354 if (!compareMimeMessage(convertedMime, msg)) { 0355 QString expected = QString::fromUtf8(msg->encodedContent()); 0356 normalizeMimemessage(expected); 0357 QString converted = QString::fromUtf8(convertedMime->encodedContent()); 0358 normalizeMimemessage(converted); 0359 showDiff(expected, converted); 0360 QVERIFY(false); 0361 } 0362 QCOMPARE(Kolab::ErrorHandler::instance().error(), Kolab::ErrorHandler::Debug); 0363 } 0364 0365 void FormatTest::testNote_data() 0366 { 0367 QTest::addColumn<Kolab::Version>("version"); 0368 QTest::addColumn<Kolab::ObjectType>("type"); 0369 QTest::addColumn<QString>("noteFileName"); 0370 QTest::addColumn<QString>("mimeFileName"); 0371 0372 QTest::newRow("v3noteSimple") << Kolab::KolabV3 << Kolab::NoteObject << getPath("v3/note/note.mime") << getPath("v3/note/note.mime.mime"); 0373 } 0374 0375 void FormatTest::testNote() 0376 { 0377 QFETCH(Kolab::Version, version); 0378 QFETCH(Kolab::ObjectType, type); 0379 QFETCH(QString, noteFileName); // To compare 0380 QFETCH(QString, mimeFileName); // For parsing 0381 0382 // Parse mime message 0383 bool ok = false; 0384 const KMime::Message::Ptr &msg = readMimeFile(mimeFileName, ok); 0385 QVERIFY(ok); 0386 Kolab::KolabObjectReader reader; 0387 Kolab::ObjectType t = reader.parseMimeMessage(msg); 0388 QCOMPARE(t, type); 0389 QCOMPARE(reader.getVersion(), version); 0390 QCOMPARE(Kolab::ErrorHandler::instance().error(), Kolab::ErrorHandler::Debug); 0391 0392 KMime::Message::Ptr convertedNote = reader.getNote(); 0393 QVERIFY(convertedNote.data()); 0394 0395 // Parse note 0396 const KMime::Message::Ptr &realNote = readMimeFile(noteFileName, ok); 0397 QVERIFY(ok); 0398 QVERIFY(realNote.data()); 0399 0400 QString expected = QString::fromUtf8(realNote->encodedContent()); 0401 normalizeMimemessage(expected); 0402 QString converted = QString::fromUtf8(convertedNote->encodedContent()); 0403 normalizeMimemessage(converted); 0404 QEXPECT_FAIL("", "Header sorting is off", Continue); 0405 QCOMPARE(expected, converted); 0406 // showDiff(expected, converted); 0407 0408 // Write 0409 const KMime::Message::Ptr &convertedMime = Kolab::KolabObjectWriter::writeNote(realNote, version); 0410 QVERIFY(convertedMime.data()); 0411 QVERIFY(msg.data()); 0412 0413 QString expected2 = QString::fromUtf8(msg->encodedContent()); 0414 normalizeMimemessage(expected2); 0415 QString converted2 = QString::fromUtf8(convertedMime->encodedContent()); 0416 normalizeMimemessage(converted2); 0417 QEXPECT_FAIL("", "Header sorting is off", Continue); 0418 QCOMPARE(expected2, converted2); 0419 // showDiff(expected2, converted2); 0420 0421 QCOMPARE(Kolab::ErrorHandler::instance().error(), Kolab::ErrorHandler::Debug); 0422 } 0423 0424 // This function exists only to generate the reference files, it's not a real test. 0425 void FormatTest::generateMimefile() 0426 { 0427 // QFile icalFile( getPath("v3/journal/complex.ics") ); 0428 // QVERIFY( icalFile.open( QFile::ReadOnly ) ); 0429 // KCalendarCore::ICalFormat format; 0430 // const KCalendarCore::Incidence::Ptr realIncidence( format.fromString( QString::fromUtf8( icalFile.readAll() ) ) ); 0431 // 0432 // QString result; 0433 // QTextStream s(&result); 0434 // Kolab::overrideTimestamp(Kolab::cDateTime(2012, 5, 5, 5,5,5, true)); 0435 // Kolab::KolabObjectWriter::writeIncidence(realIncidence, Kolab::KolabV3)->toStream(s); 0436 0437 // QFile vcardFile( getPath("v3/contacts/complex.vcf") ); 0438 // QVERIFY( vcardFile.open( QFile::ReadOnly ) ); 0439 // KContacts::VCardConverter converter; 0440 // const KContacts::Addressee realAddressee = converter.parseVCard( vcardFile.readAll() ); 0441 // 0442 // qDebug() << realAddressee.photo().data(); 0443 // 0444 // QString result; 0445 // QTextStream s(&result); 0446 // Kolab::overrideTimestamp(Kolab::cDateTime(2012, 5, 5, 5,5,5, true)); 0447 // Kolab::KolabObjectWriter::writeContact(realAddressee, Kolab::KolabV3)->toStream(s); 0448 0449 // qDebug() << result; 0450 } 0451 0452 void FormatTest::generateVCard() 0453 { 0454 // bool ok = false; 0455 // const KMime::Message::Ptr &msg = readMimeFile( QString::fromLatin1("../")+getPath("v2/contacts/pictureJPGHorde.vcf.mime"), ok ); 0456 // qDebug() << msg->encodedContent(); 0457 // Kolab::KolabObjectReader reader; 0458 // Kolab::ObjectType t = reader.parseMimeMessage(msg); 0459 // 0460 // KContacts::Addressee convertedAddressee = reader.getContact(); 0461 // KContacts::VCardConverter converter; 0462 // qDebug() << converter.createVCard(convertedAddressee); 0463 0464 // bool ok = false; 0465 // const KMime::Message::Ptr &msg = readMimeFile( getPath("v3/contacts/distlist.vcf.mime"), ok ); 0466 // qDebug() << msg->encodedContent(); 0467 // Kolab::KolabObjectReader reader; 0468 // Kolab::ObjectType t = reader.parseMimeMessage(msg); 0469 // 0470 // KContacts::ContactGroup convertedAddressee = reader.getDistlist(); 0471 // QBuffer buf; 0472 // buf.open(QIODevice::WriteOnly); 0473 // KContacts::ContactGroupTool::convertToXml(convertedAddressee, &buf); 0474 // qDebug() << buf.buffer(); 0475 } 0476 0477 // Pseudo test to show that JPG is always lossy, even with quality set to 100 0478 void FormatTest::proveJPGisLossy() 0479 { 0480 // QImage img(getPath("picture.jpg")); 0481 // QByteArray pic; 0482 // QBuffer buffer(&pic); 0483 // buffer.open(QIODevice::WriteOnly); 0484 // img.save(&buffer, "JPEG"); 0485 // buffer.close(); 0486 // qDebug() << pic.toBase64(); 0487 // 0488 // QImage img2; 0489 // QByteArray pic2; 0490 // QBuffer buffer2(&pic2); 0491 // img2.loadFromData(pic); 0492 // img2.save(&buffer2, "JPEG"); 0493 // buffer2.close(); 0494 // qDebug() << pic2.toBase64(); 0495 } 0496 0497 QTEST_MAIN(FormatTest) 0498 0499 #include "moc_formattest.cpp"