File indexing completed on 2024-11-24 04:44:09

0001 /*
0002  * SPDX-FileCopyrightText: 2012 Sofia Balicka <balicka@kolabsys.com>
0003  * SPDX-FileCopyrightText: 2014 Christian Mollekopf <mollekopf@kolabsys.com>
0004  *
0005  * SPDX-License-Identifier: LGPL-3.0-or-later
0006  */
0007 
0008 #include "mimeobjecttest.h"
0009 #include "conversion/commonconversion.h"
0010 #include "kolabformat/mimeobject.h"
0011 #include "testutils.h"
0012 #include <QString>
0013 #include <QTest>
0014 #include <fstream>
0015 #include <sstream>
0016 
0017 static std::string readFile(const QString &path)
0018 {
0019     std::ifstream t(path.toStdString().c_str());
0020     std::stringstream buffer;
0021     buffer << t.rdbuf();
0022     return buffer.str();
0023 }
0024 
0025 static QString normalizeMimemessage(const std::string &path)
0026 {
0027     QString qString = Kolab::Conversion::fromStdString(path);
0028     normalizeMimemessage(qString);
0029     return qString;
0030 }
0031 
0032 template<class T>
0033 void testFunction(const QString &filename,
0034                   Kolab::Version version,
0035                   Kolab::ObjectType type,
0036                   T (Kolab::MIMEObject::*readFunction)(const std::string &),
0037                   std::string (Kolab::MIMEObject::*writeFunction)(const T &, Kolab::Version, const std::string &))
0038 {
0039     const std::string input = readFile(filename);
0040     Kolab::MIMEObject mimeobject;
0041     const T object = (mimeobject.*readFunction)(input);
0042     QCOMPARE(mimeobject.getType(), type);
0043     const std::string message = (mimeobject.*writeFunction)(object, version, std::string());
0044     DIFFCOMPARE(normalizeMimemessage(message), normalizeMimemessage(input));
0045 }
0046 
0047 template<class T>
0048 void testFunction(T (Kolab::MIMEObject::*readFunction)(const std::string &),
0049                   std::string (Kolab::MIMEObject::*writeFunction)(const T &, Kolab::Version, const std::string &))
0050 {
0051     testFunction<T>(TESTVALUE(QString, mimeFileName), TESTVALUE(Kolab::Version, version), TESTVALUE(Kolab::ObjectType, type), readFunction, writeFunction);
0052 }
0053 
0054 void MIMEObjectTest::testEvent_data()
0055 {
0056     QTest::addColumn<Kolab::Version>("version");
0057     QTest::addColumn<Kolab::ObjectType>("type");
0058     QTest::addColumn<QString>("mimeFileName");
0059 
0060     QTest::newRow("v2eventSimple") << Kolab::KolabV2 << Kolab::EventObject << getPath("v2/event/simple.ics.mime");
0061     QTest::newRow("v2eventComplex") << Kolab::KolabV2 << Kolab::EventObject << getPath("v2/event/complex.ics.mime");
0062     QTest::newRow("v2eventAttachment") << Kolab::KolabV2 << Kolab::EventObject << getPath("v2/event/attachment.ics.mime");
0063     QTest::newRow("v2eventAllday") << Kolab::KolabV2 << Kolab::EventObject << getPath("v2/event/allday.ics.mime");
0064     QTest::newRow("v2eventUtf8Attachment") << Kolab::KolabV2 << Kolab::EventObject << getPath("v2/event/attachmentUtf8.ics.mime");
0065     QTest::newRow("v3eventSimple") << Kolab::KolabV3 << Kolab::EventObject << getPath("v3/event/simple.ics.mime");
0066     QTest::newRow("v3eventComplex") << Kolab::KolabV3 << Kolab::EventObject << getPath("v3/event/complex.ics.mime");
0067     QTest::newRow("v3utf8quotedPrintable") << Kolab::KolabV3 << Kolab::EventObject << getPath("v3/event/utf8quotedPrintable.ics.mime");
0068     // TODO move to read-only
0069     // QTest::newRow("v2eventHorde") << Kolab::KolabV2 << Kolab::EventObject << getPath("v2/event/horde.ics") << getPath("v2/event/horde.ics.mime");
0070     // TODO read-only test, we never write base64
0071     // QTest::newRow("v3utf8base64") << Kolab::KolabV3 << Kolab::EventObject << getPath("v3/event/utf8base64.ics.mime");
0072     // TODO read-only test, we never write 8-bit
0073     // QTest::newRow("v3utf88bit") << Kolab::KolabV3 << Kolab::EventObject << getPath("v3/event/utf88bit.ics.mime");
0074 }
0075 
0076 void MIMEObjectTest::testEvent()
0077 {
0078     testFunction<Kolab::Event>(&Kolab::MIMEObject::readEvent, &Kolab::MIMEObject::writeEvent);
0079 }
0080 
0081 void MIMEObjectTest::testTodo_data()
0082 {
0083     QTest::addColumn<Kolab::Version>("version");
0084     QTest::addColumn<Kolab::ObjectType>("type");
0085     QTest::addColumn<QString>("mimeFileName");
0086 
0087     QTest::newRow("v2todoSimple") << Kolab::KolabV2 << Kolab::TodoObject << getPath("v2/task/simple.ics.mime");
0088     QTest::newRow("v2todoComplex") << Kolab::KolabV2 << Kolab::TodoObject << getPath("v2/task/complex.ics.mime");
0089     QTest::newRow("v2todoPrio1") << Kolab::KolabV2 << Kolab::TodoObject << getPath("v2/task/prioritytest1.ics.mime");
0090     QTest::newRow("v2todoPrio2") << Kolab::KolabV2 << Kolab::TodoObject << getPath("v2/task/prioritytest2.ics.mime");
0091     QTest::newRow("v3todoSimple") << Kolab::KolabV3 << Kolab::TodoObject << getPath("v3/task/simple.ics.mime");
0092     QTest::newRow("v3todoComplex") << Kolab::KolabV3 << Kolab::TodoObject << getPath("v3/task/complex.ics.mime");
0093 }
0094 
0095 void MIMEObjectTest::testTodo()
0096 {
0097     testFunction<Kolab::Todo>(&Kolab::MIMEObject::readTodo, &Kolab::MIMEObject::writeTodo);
0098 }
0099 
0100 void MIMEObjectTest::testJournal_data()
0101 {
0102     QTest::addColumn<Kolab::Version>("version");
0103     QTest::addColumn<Kolab::ObjectType>("type");
0104     QTest::addColumn<QString>("mimeFileName");
0105 
0106     QTest::newRow("v2journalSimple") << Kolab::KolabV2 << Kolab::JournalObject << getPath("v2/journal/simple.ics.mime");
0107     QTest::newRow("v2journalComplex") << Kolab::KolabV2 << Kolab::JournalObject << getPath("v2/journal/complex.ics.mime");
0108     QTest::newRow("v3journalSimple") << Kolab::KolabV3 << Kolab::JournalObject << getPath("v3/journal/simple.ics.mime");
0109     QTest::newRow("v3journalComplex") << Kolab::KolabV3 << Kolab::JournalObject << getPath("v3/journal/complex.ics.mime");
0110 }
0111 
0112 void MIMEObjectTest::testJournal()
0113 {
0114     testFunction<Kolab::Journal>(&Kolab::MIMEObject::readJournal, &Kolab::MIMEObject::writeJournal);
0115 }
0116 
0117 void MIMEObjectTest::testNote_data()
0118 {
0119     QTest::addColumn<Kolab::Version>("version");
0120     QTest::addColumn<Kolab::ObjectType>("type");
0121     QTest::addColumn<QString>("mimeFileName");
0122 
0123     QTest::newRow("v3noteSimple") << Kolab::KolabV3 << Kolab::NoteObject << getPath("v3/note/note.mime.mime");
0124 }
0125 
0126 void MIMEObjectTest::testNote()
0127 {
0128     testFunction<Kolab::Note>(&Kolab::MIMEObject::readNote, &Kolab::MIMEObject::writeNote);
0129 }
0130 
0131 void MIMEObjectTest::testContact_data()
0132 {
0133     QTest::addColumn<Kolab::Version>("version");
0134     QTest::addColumn<Kolab::ObjectType>("type");
0135     QTest::addColumn<QString>("mimeFileName");
0136 
0137     QTest::newRow("v2contactSimple") << Kolab::KolabV2 << Kolab::ContactObject << getPath("v2/contacts/simple.vcf.mime");
0138     QTest::newRow("v2contactAddress") << Kolab::KolabV2 << Kolab::ContactObject << getPath("v2/contacts/address.vcf.mime");
0139     // FIXME Figure out why this is broken
0140     // QTest::newRow("v2contactBug238996") << Kolab::KolabV2 << Kolab::ContactObject << getPath("v2/contacts/bug238996.vcf.mime");
0141     QTest::newRow("v2contactDisplayname") << Kolab::KolabV2 << Kolab::ContactObject << getPath("v2/contacts/displayname.vcf.mime");
0142     QTest::newRow("v2contactEmails") << Kolab::KolabV2 << Kolab::ContactObject << getPath("v2/contacts/emails.vcf.mime");
0143     QTest::newRow("v2contactPhonenumbers") << Kolab::KolabV2 << Kolab::ContactObject << getPath("v2/contacts/phonenumbers.vcf.mime");
0144     QTest::newRow("v3contactSimple") << Kolab::KolabV3 << Kolab::ContactObject << getPath("v3/contacts/simple.vcf.mime");
0145     QTest::newRow("v3contactComplex") << Kolab::KolabV3 << Kolab::ContactObject << getPath("v3/contacts/complex.vcf.mime");
0146 
0147     // FIXME Tested in read-only mode in formattest, perhaps move to read-only test that compares with ical?
0148     // QTest::newRow("v3contactPng") << Kolab::KolabV3 << Kolab::ContactObject << getPath("v3/readonly/png.vcf.mime");
0149 
0150     // FIXME Reference files needs to be adjusted due to fix in how pictures are stored
0151     // QTest::newRow("v2contactComplex") << Kolab::KolabV2 << Kolab::ContactObject << getPath("v2/contacts/complex.vcf.mime");
0152     // FIXME Reference files needs to be adjusted due to fix in how pictures are stored
0153     // QTest::newRow("v2contactPicture") << Kolab::KolabV2 << Kolab::ContactObject << getPath("v2/contacts/picture.vcf.mime");
0154     // FIXME the following test fails because the vcard implementation always writes jpeg (which is lossy).
0155     // The reference vcf file is therefore probably also not really useful
0156     // QTest::newRow("v2pictureJPGHorde") << Kolab::KolabV2 << Kolab::ContactObject << getPath("v2/contacts/pictureJPGHorde.vcf.mime");
0157 }
0158 
0159 void MIMEObjectTest::testContact()
0160 {
0161     testFunction<Kolab::Contact>(&Kolab::MIMEObject::readContact, &Kolab::MIMEObject::writeContact);
0162 }
0163 
0164 void MIMEObjectTest::testDistlist_data()
0165 {
0166     QTest::addColumn<Kolab::Version>("version");
0167     QTest::addColumn<Kolab::ObjectType>("type");
0168     QTest::addColumn<QString>("mimeFileName");
0169 
0170     QTest::newRow("v3distlistSimple") << Kolab::KolabV3 << Kolab::DistlistObject << getPath("v3/contacts/distlist.vcf.mime");
0171 }
0172 
0173 void MIMEObjectTest::testDistlist()
0174 {
0175     testFunction<Kolab::DistList>(&Kolab::MIMEObject::readDistlist, &Kolab::MIMEObject::writeDistlist);
0176 }
0177 
0178 QTEST_MAIN(MIMEObjectTest)
0179 
0180 #include "moc_mimeobjecttest.cpp"