From cb5c76944c0c3a8ee6360564369c1a3cc409255e Mon Sep 17 00:00:00 2001 From: Damien Caliste Date: Fri, 14 Sep 2018 13:35:10 +0200 Subject: Add tests for attachment finder Add two tests to check that attachment finder is working for level 1 and level 2 attachment locations. It's a follow up of previous commit "Add recursion when looking for attachments". It also check that calendar invitation is not returned as an attachment. Change-Id: If84bc67687d50720ab1bc0458e7b57ed21650348 Reviewed-by: Pekka Vuorela Reviewed-by: Matthew Vogt --- src/libraries/qmfclient/qmailmessage.cpp | 3 +- tests/tst_qmailmessage/tst_qmailmessage.cpp | 106 ++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+), 2 deletions(-) diff --git a/src/libraries/qmfclient/qmailmessage.cpp b/src/libraries/qmfclient/qmailmessage.cpp index dc782b9e..a04abc67 100644 --- a/src/libraries/qmfclient/qmailmessage.cpp +++ b/src/libraries/qmfclient/qmailmessage.cpp @@ -1163,8 +1163,7 @@ namespace findAttachments bool isText = (contentType.matches("text", "plain") || contentType.matches("text", "html")); - bool isCalendar = (contentType.type().toLower() == "text") && - (contentType.subType().toLower() == "calendar"); + bool isCalendar = contentType.matches("text", "calendar"); bool isInLine = (!part.contentDisposition().isNull()) && (part.contentDisposition().type() == QMailMessageContentDisposition::Inline); diff --git a/tests/tst_qmailmessage/tst_qmailmessage.cpp b/tests/tst_qmailmessage/tst_qmailmessage.cpp index 7aa25386..943ee8c1 100644 --- a/tests/tst_qmailmessage/tst_qmailmessage.cpp +++ b/tests/tst_qmailmessage/tst_qmailmessage.cpp @@ -126,6 +126,9 @@ private slots: void multiMultipart(); + void attachments(); + void recursiveAttachments(); + void copyAndAssign(); void unterminatedDoubleQuote(); @@ -1434,6 +1437,109 @@ void tst_QMailMessage::multiMultipart() } } +void tst_QMailMessage::attachments() +{ + QByteArray data; + QByteArray type; + + QMailMessagePart p1; + type = "text/plain; charset=UTF-8"; + data = "P1: This is a plain text part."; + p1.setBody(QMailMessageBody::fromData(data, QMailMessageContentType(type), + QMailMessageBody::EightBit, + QMailMessageBody::RequiresEncoding)); + + QMailMessagePart p2; + type = "text/calendar; charset=UTF-8"; + data = "BEGIN:VCALENDAR\nEND:VCALENDAR"; + p2.setBody(QMailMessageBody::fromData(data, QMailMessageContentType(type), + QMailMessageBody::EightBit, + QMailMessageBody::RequiresEncoding)); + + QMailMessagePart p3; + type = "application/octet-stream; name=\"attach.pdf\""; + data = "abcdef"; + p3.setBody(QMailMessageBody::fromData(data, QMailMessageContentType(type), + QMailMessageBody::Base64, + QMailMessageBody::AlreadyEncoded)); + p3.setContentDisposition(QMailMessageContentDisposition::Attachment); + + QMailMessage m; + m.setTo(QMailAddress("someone@example.net")); + m.setFrom(QMailAddress("someone@example.net")); + m.setSubject("multipart/mixed with attachment"); + + m.setMultipartType(QMailMessagePartContainer::MultipartMixed); + m.appendPart(p1); + m.appendPart(p2); + m.appendPart(p3); + QCOMPARE(m.contentType().toString().toLower(), + QByteArray("Content-Type: multipart/mixed").toLower()); + QCOMPARE(m.transferEncoding(), QMailMessageBody::NoEncoding); + QCOMPARE(m.partCount(), uint(3)); + for (uint i = 0; i < m.partCount(); ++i) + QCOMPARE( m.partAt(i).partNumber(), int(i) ); + + QList indices = m.findAttachmentLocations(); + QCOMPARE(indices.size(), 1); + QCOMPARE(indices.at(0).toString(false), QStringLiteral("3")); +} + +void tst_QMailMessage::recursiveAttachments() +{ + QByteArray data; + QByteArray type; + + QMailMessagePart p1; + type = "text/plain; charset=UTF-8"; + data = "P1: This is a plain text part."; + p1.setBody(QMailMessageBody::fromData(data, QMailMessageContentType(type), + QMailMessageBody::EightBit, + QMailMessageBody::RequiresEncoding)); + + QMailMessagePart p3; + type = "text/html; charset=UTF-8"; + data = ""; + p3.setBody(QMailMessageBody::fromData(data, QMailMessageContentType(type), + QMailMessageBody::EightBit, + QMailMessageBody::RequiresEncoding)); + + QMailMessagePart p4; + type = "application/octet-stream; name=\"attach.pdf\""; + data = "abcdef"; + p4.setBody(QMailMessageBody::fromData(data, QMailMessageContentType(type), + QMailMessageBody::Base64, + QMailMessageBody::AlreadyEncoded)); + p4.setContentDisposition(QMailMessageContentDisposition::Attachment); + + QMailMessagePart p2; + p2.setMultipartType(QMailMessagePartContainer::MultipartMixed); + p2.appendPart(p3); + p2.appendPart(p4); + + QMailMessage m; + m.setTo(QMailAddress("someone@example.net")); + m.setFrom(QMailAddress("someone@example.net")); + m.setSubject("multipart/alternative with attachment in mixed"); + + m.setMultipartType(QMailMessagePartContainer::MultipartAlternative); + m.appendPart(p1); + m.appendPart(p2); + QCOMPARE(m.contentType().toString().toLower(), + QByteArray("Content-Type: multipart/alternative").toLower()); + QCOMPARE(m.transferEncoding(), QMailMessageBody::NoEncoding); + QCOMPARE(m.partCount(), uint(2)); + for (uint i = 0; i < m.partCount(); ++i) + QCOMPARE( m.partAt(i).partNumber(), int(i) ); + QCOMPARE(m.partAt(1).partCount(), uint(2)); + for (uint i = 0; i < m.partAt(1).partCount(); ++i) + QCOMPARE( m.partAt(1).partAt(i).partNumber(), int(i) ); + + QList indices = m.findAttachmentLocations(); + QCOMPARE(indices.size(), 1); + QCOMPARE(indices.at(0).toString(false), QStringLiteral("2.2")); +} + void tst_QMailMessage::copyAndAssign() { QMailMessage m1; -- cgit v1.2.3