Branch 'libkolab-0.4' - 2 commits - kolabformat/v2helpers.cpp kolabformat/v2helpers.h kolabformat/xmlobject.cpp tests/CMakeLists.txt tests/xmlobjecttest.cpp tests/xmlobjecttest.h

Christian Mollekopf mollekopf at kolabsys.com
Tue Feb 19 16:19:55 CET 2013


 kolabformat/v2helpers.cpp |   24 ++++++++++++++++
 kolabformat/v2helpers.h   |    4 +-
 kolabformat/xmlobject.cpp |   28 +++++++++++++++++++
 tests/CMakeLists.txt      |    1 
 tests/xmlobjecttest.cpp   |   65 ++++++++++++++++++++++++++++++++++++++++++++++
 tests/xmlobjecttest.h     |   31 +++++++++++++++++++++
 6 files changed, 151 insertions(+), 2 deletions(-)

New commits:
commit 221cae6c555732334f2709ffdcc6e09d83e4362e
Author: Christian Mollekopf <mollekopf at kolabsys.com>
Date:   Tue Feb 19 16:19:23 2013 +0100

    Basic functionality check for the xmlobject interface.

diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index b3aa7ac..6f71a0c 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -21,3 +21,4 @@ addTest(icalendartest)
 addTest(freebusytest)
 addTest(kolabobjecttest)
 addTest(timezonetest)
+addTest(xmlobjecttest)
diff --git a/tests/xmlobjecttest.cpp b/tests/xmlobjecttest.cpp
new file mode 100644
index 0000000..a004cd9
--- /dev/null
+++ b/tests/xmlobjecttest.cpp
@@ -0,0 +1,65 @@
+/*
+ * Copyright (C) 2012  Christian Mollekopf <mollekopf at kolabsys.com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "xmlobjecttest.h"
+
+#include <QTest>
+#include <iostream>
+
+#include "kolabformat/xmlobject.h"
+
+void XMLObjectTest::testEvent()
+{
+    Kolab::Event event;
+    event.setStart(Kolab::cDateTime(2012,01,01));
+
+    Kolab::XMLObject xmlobject;
+    const std::string output = xmlobject.writeEvent(event, Kolab::KolabV2, "productid");
+    QVERIFY(!output.empty());
+    std::cout << output;
+
+    const Kolab::Event resultEvent = xmlobject.readEvent(output, Kolab::KolabV2);
+    QVERIFY(resultEvent.isValid());
+    
+}
+
+void XMLObjectTest::testDontCrash()
+{
+    Kolab::XMLObject ob;
+    ob.writeEvent(Kolab::Event(), Kolab::KolabV2, "");
+    ob.writeTodo(Kolab::Todo(), Kolab::KolabV2, "");
+    ob.writeJournal(Kolab::Journal(), Kolab::KolabV2, "");
+    ob.writeFreebusy(Kolab::Freebusy(), Kolab::KolabV2, "");
+    ob.writeContact(Kolab::Contact(), Kolab::KolabV2, "");
+    ob.writeDistlist(Kolab::DistList(), Kolab::KolabV2, "");
+    ob.writeNote(Kolab::Note(), Kolab::KolabV2, "");
+    ob.writeConfiguration(Kolab::Configuration(), Kolab::KolabV2, "");
+
+    ob.readEvent("", Kolab::KolabV2);
+    ob.readTodo("", Kolab::KolabV2);
+    ob.readJournal("", Kolab::KolabV2);
+    ob.readFreebusy("", Kolab::KolabV2);
+    ob.readContact("", Kolab::KolabV2);
+    ob.readDistlist("", Kolab::KolabV2);
+    ob.readNote("", Kolab::KolabV2);
+    ob.readConfiguration("", Kolab::KolabV2);
+
+}
+
+QTEST_MAIN( XMLObjectTest )
+
+#include "xmlobjecttest.moc"
diff --git a/tests/xmlobjecttest.h b/tests/xmlobjecttest.h
new file mode 100644
index 0000000..0471e5d
--- /dev/null
+++ b/tests/xmlobjecttest.h
@@ -0,0 +1,31 @@
+/*
+ * Copyright (C) 2012  Christian Mollekopf <mollekopf at kolabsys.com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef XMLOBJECTTEST_H
+#define XMLOBJECTTEST_H
+
+#include <QObject>
+
+class XMLObjectTest: public QObject
+{
+    Q_OBJECT
+private slots:
+    void testEvent();
+    void testDontCrash();
+};
+
+#endif // XMLOBJECTTEST_H


commit 5b814e701be22e9840453a832c2dacbf5672ccbf
Author: Christian Mollekopf <mollekopf at kolabsys.com>
Date:   Tue Feb 19 16:17:25 2013 +0100

    Avoid crashing with invalid data when using the xmlobject interface.

diff --git a/kolabformat/v2helpers.cpp b/kolabformat/v2helpers.cpp
index e679351..0f891cc 100644
--- a/kolabformat/v2helpers.cpp
+++ b/kolabformat/v2helpers.cpp
@@ -40,6 +40,10 @@ namespace Kolab {
 
 static QImage getPicture(const QString &pictureAttachmentName, const KMime::Message::Ptr &data, QByteArray &type)
 {
+    if (!data) {
+        Critical() << "empty message";
+        return QImage();
+    }
     KMime::Content *imgContent = Mime::findContentByName(data, pictureAttachmentName/*"kolab-picture.png"*/, type);
     if (!imgContent) {
         Warning() << "could not find picture: " << pictureAttachmentName;
@@ -79,6 +83,10 @@ static QImage getPicture(const QString &pictureAttachmentName, const KMime::Mess
 
 KABC::Addressee addresseeFromKolab( const QByteArray &xmlData, const KMime::Message::Ptr &data)
 {
+    if (!data) {
+        Critical() << "empty message";
+        return KABC::Addressee();
+    }
     KABC::Addressee addressee;
 //     Debug() << "xmlData " << xmlData;
     KolabV2::Contact contact(QString::fromUtf8(xmlData));
@@ -140,6 +148,10 @@ static QByteArray createPicture(const QImage &img, const QString &/*format*/, QS
 KMime::Message::Ptr contactToKolabFormat(const KolabV2::Contact& contact, const QString &productId)
 {
     KMime::Message::Ptr message = Mime::createMessage( KOLAB_TYPE_CONTACT, false, productId );
+    if (!message) {
+        Critical() << "empty message";
+        return KMime::Message::Ptr();
+    }
     message->subject()->fromUnicodeString( contact.uid(), "utf-8" );
     message->from()->fromUnicodeString( contact.fullEmail(), "utf-8" );
     
@@ -181,6 +193,10 @@ KABC::ContactGroup contactGroupFromKolab(const QByteArray &xmlData)
 KMime::Message::Ptr distListToKolabFormat(const KolabV2::DistributionList& distList, const QString &productId)
 {    
     KMime::Message::Ptr message = Mime::createMessage( KOLAB_TYPE_DISTLIST, false, productId );
+    if (!message) {
+        Critical() << "empty message";
+        return KMime::Message::Ptr();
+    }
     message->subject()->fromUnicodeString( distList.uid(), "utf-8" );
     message->from()->fromUnicodeString( distList.uid(), "utf-8" );
     
@@ -209,12 +225,20 @@ KMime::Message::Ptr noteFromKolab(const QByteArray &xmlData, const KDateTime &cr
 
 KMime::Message::Ptr noteToKolab(const KMime::Message::Ptr& msg, const QString &productId)
 {
+    if (!msg) {
+        Critical() << "empty message";
+        return KMime::Message::Ptr();
+    }
     Akonadi::NoteUtils::NoteMessageWrapper note(msg);
     return Mime::createMessage(note.title(), KOLAB_TYPE_NOTE, KOLAB_TYPE_NOTE, noteToKolabXML(msg), false, productId);
 }
 
 QByteArray noteToKolabXML(const KMime::Message::Ptr& msg)
 {
+    if (!msg) {
+        Critical() << "empty message";
+        return QByteArray();
+    }
     Akonadi::NoteUtils::NoteMessageWrapper note(msg);
     KolabV2::Note j;
     j.setSummary( note.title() );
diff --git a/kolabformat/v2helpers.h b/kolabformat/v2helpers.h
index f883efb..e047d35 100644
--- a/kolabformat/v2helpers.h
+++ b/kolabformat/v2helpers.h
@@ -47,7 +47,7 @@ static KCalPtr fromXML(const QByteArray &xmlData, QStringList &attachments)
 {
     const QDomDocument xmlDoc = KolabV2::KolabBase::loadDocument( QString::fromUtf8(xmlData) ); //TODO extract function from V2 format
     if ( xmlDoc.isNull() ) {
-        Error() << "Failed to read the xml document";
+        Critical() << "Failed to read the xml document";
         return KCalPtr();
     }
     const KCalPtr i = Container::fromXml( xmlDoc, QString() ); //For parsing we don't need the timezone, so we don't set one
@@ -64,7 +64,7 @@ static inline IncidencePtr incidenceFromKolabImpl( const KMime::Message::Ptr &da
 {
     KMime::Content *xmlContent = Mime::findContentByType( data, mimetype );
     if ( !xmlContent ) {
-        Warning() << "couldn't find part";
+        Critical() << "couldn't find part";
         return IncidencePtr();
     }
     const QByteArray &xmlData = xmlContent->decodedContent();
diff --git a/kolabformat/xmlobject.cpp b/kolabformat/xmlobject.cpp
index ebeca5f..9a8c78f 100644
--- a/kolabformat/xmlobject.cpp
+++ b/kolabformat/xmlobject.cpp
@@ -52,6 +52,10 @@ std::string XMLObject::writeEvent(const Event &event, Version version, const std
     mWrittenUID.clear();
     if (version == KolabV2) {
         const KCalCore::Event::Ptr i = Conversion::toKCalCore(event);
+        if (!i) {
+            Critical() << "invalid incidence";
+            return std::string();
+        }
         if (i->uid().isEmpty()) {
             i->setUid(createUuid());
         }
@@ -70,6 +74,10 @@ Event XMLObject::readEvent(const std::string& s, Version version)
     if (version == KolabV2) {
         QStringList attachments;
         const KCalCore::Event::Ptr event = Kolab::fromXML<KCalCore::Event::Ptr, KolabV2::Event>(QString::fromUtf8(s.c_str()).toUtf8(), attachments);
+        if (!event || Kolab::ErrorHandler::errorOccured()) {
+            Critical() << "failed to read xml";
+            return Event();
+        }
         mAttachments.clear();
         foreach (const QString &attachment, attachments) {
             mAttachments.push_back(Conversion::toStdString(attachment));
@@ -84,6 +92,10 @@ std::string XMLObject::writeTodo(const Todo &event, Version version, const std::
     mWrittenUID.clear();
     if (version == KolabV2) {
         const KCalCore::Todo::Ptr i = Conversion::toKCalCore(event);
+        if (!i) {
+            Critical() << "invalid incidence";
+            return std::string();
+        }
         if (i->uid().isEmpty()) {
             i->setUid(createUuid());
         }
@@ -102,6 +114,10 @@ Todo XMLObject::readTodo(const std::string& s, Version version)
     if (version == KolabV2) {
         QStringList attachments;
         const KCalCore::Todo::Ptr event = Kolab::fromXML<KCalCore::Todo::Ptr, KolabV2::Task>(QString::fromUtf8(s.c_str()).toUtf8(), attachments);
+        if (!event || Kolab::ErrorHandler::errorOccured()) {
+            Error() << "failed to read xml";
+            return Todo();
+        }
         mAttachments.clear();
         foreach (const QString &attachment, attachments) {
             mAttachments.push_back(Conversion::toStdString(attachment));
@@ -116,6 +132,10 @@ std::string XMLObject::writeJournal(const Journal &event, Version version, const
     mWrittenUID.clear();
     if (version == KolabV2) {
         const KCalCore::Journal::Ptr i = Conversion::toKCalCore(event);
+        if (!i) {
+            Critical() << "invalid journal";
+            return std::string();
+        }
         if (i->uid().isEmpty()) {
             i->setUid(createUuid());
         }
@@ -134,6 +154,10 @@ Journal XMLObject::readJournal(const std::string& s, Version version)
     if (version == KolabV2) {
         QStringList attachments;
         const KCalCore::Journal::Ptr event = Kolab::fromXML<KCalCore::Journal::Ptr, KolabV2::Journal>(QString::fromUtf8(s.c_str()).toUtf8(), attachments);
+        if (!event || Kolab::ErrorHandler::errorOccured()) {
+            Critical() << "failed to read xml";
+            return Journal();
+        }
         mAttachments.clear();
         foreach (const QString &attachment, attachments) {
             mAttachments.push_back(Conversion::toStdString(attachment));
@@ -244,6 +268,10 @@ Note XMLObject::readNote(const std::string& s, Version version)
 {
     if (version == KolabV2) {
         const KMime::Message::Ptr msg = noteFromKolab(QByteArray(s.c_str(), s.length()), KDateTime());
+        if (!msg || Kolab::ErrorHandler::errorOccured()) {
+            Critical() << "failed to read xml";
+            return Note();
+        }
         return Conversion::fromNote(msg);
     }
     return Kolab::readNote(s, false);





More information about the commits mailing list