File indexing completed on 2025-01-05 04:58:37

0001 /*
0002  *   Copyright (C) 2016 Christian Mollekopf <chrigi_1@fastmail.fm>
0003  *
0004  *   This program is free software; you can redistribute it and/or modify
0005  *   it under the terms of the GNU General Public License as published by
0006  *   the Free Software Foundation; either version 2 of the License, or
0007  *   (at your option) any later version.
0008  *
0009  *   This program is distributed in the hope that it will be useful,
0010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0012  *   GNU General Public License for more details.
0013  *
0014  *   You should have received a copy of the GNU General Public License
0015  *   along with this program; if not, write to the
0016  *   Free Software Foundation, Inc.,
0017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
0018  */
0019 #include <QTest>
0020 #include <QTcpSocket>
0021 #include <KMime/Message>
0022 
0023 #include <tests/mailtest.h>
0024 
0025 #include "common/test.h"
0026 #include "common/domain/applicationdomaintype.h"
0027 #include "common/secretstore.h"
0028 #include "common/resourcecontrol.h"
0029 #include "common/store.h"
0030 
0031 using namespace Sink;
0032 using namespace Sink::ApplicationDomain;
0033 
0034 /**
0035  * Test of complete system using the imap resource.
0036  *
0037  * This test requires the imap resource installed.
0038  */
0039 class ImapMailTest : public Sink::MailTest
0040 {
0041     Q_OBJECT
0042 
0043 protected:
0044     bool isBackendAvailable() Q_DECL_OVERRIDE
0045     {
0046         QTcpSocket socket;
0047         socket.connectToHost("localhost", 143);
0048         return socket.waitForConnected(200);
0049     }
0050 
0051     void resetTestEnvironment() Q_DECL_OVERRIDE
0052     {
0053         system("resetmailbox.sh");
0054     }
0055 
0056     Sink::ApplicationDomain::SinkResource createResource() Q_DECL_OVERRIDE
0057     {
0058         auto resource = ApplicationDomain::ImapResource::create("account1");
0059         resource.setProperty("server", "localhost");
0060         resource.setProperty("port", 143);
0061         resource.setProperty("username", "doe");
0062         resource.setProperty("daysToSync", 0);
0063         Sink::SecretStore::instance().insert(resource.identifier(), "doe");
0064         return resource;
0065     }
0066 
0067 private slots:
0068 
0069     void testBogusMessageAppend()
0070     {
0071         using namespace Sink;
0072         using namespace Sink::ApplicationDomain;
0073 
0074         auto folder = Folder::create(mResourceInstanceIdentifier);
0075         folder.setName("bogusfolder");
0076         VERIFYEXEC(Store::create(folder));
0077 
0078         Mail bogusMail;
0079         {
0080             auto mail = Mail::create(mResourceInstanceIdentifier);
0081             mail.setMimeMessage("Bogus message: \0 this doesn't make any sense and contains NUL.");
0082             mail.setFolder(folder);
0083 
0084             VERIFYEXEC(Store::create(mail));
0085 
0086             VERIFYEXEC(ResourceControl::flushMessageQueue(mResourceInstanceIdentifier));
0087             auto mails = Store::read<Mail>(Query().request<Mail::Folder>().request<Mail::Subject>().request<Mail::MimeMessage>());
0088             QCOMPARE(mails.size(), 1);
0089             bogusMail = mails.at(0);
0090 
0091             VERIFYEXEC(ResourceControl::flushReplayQueue(mResourceInstanceIdentifier));
0092             VERIFYEXEC(ResourceControl::inspect<ApplicationDomain::Mail>(ResourceControl::Inspection::ExistenceInspection(mail, false)));
0093             //The cache will be off by one (because we failed to replay)
0094             // VERIFYEXEC(ResourceControl::inspect<ApplicationDomain::Folder>(ResourceControl::Inspection::CacheIntegrityInspection(folder)));
0095         }
0096 
0097 
0098         //Ensure we can still append further messages:
0099         {
0100             auto mail = Mail::create(mResourceInstanceIdentifier);
0101             {
0102                 auto message = KMime::Message::Ptr::create();
0103                 message->subject(true)->fromUnicodeString("Subject", "utf8");
0104                 message->assemble();
0105                 mail.setMimeMessage(message->encodedContent(true));
0106             }
0107             mail.setFolder(folder);
0108             VERIFYEXEC(Store::create(mail));
0109 
0110             VERIFYEXEC(ResourceControl::flushMessageQueue(mResourceInstanceIdentifier));
0111             auto mails = Store::read<Mail>(Query().request<Mail::Folder>().request<Mail::Subject>().request<Mail::MimeMessage>());
0112             QCOMPARE(mails.size(), 2);
0113 
0114             VERIFYEXEC(ResourceControl::flushReplayQueue(mResourceInstanceIdentifier));
0115             //The mail is still not available, because we'll end up trying to replay the bogus mail again.
0116             VERIFYEXEC(ResourceControl::inspect<ApplicationDomain::Mail>(ResourceControl::Inspection::ExistenceInspection(mail, false)));
0117 
0118             //Fix the situation by deleting the bogus mail and retrying to sync.
0119             VERIFYEXEC(Store::remove(bogusMail));
0120             VERIFYEXEC(ResourceControl::flushMessageQueue(mResourceInstanceIdentifier));
0121             VERIFYEXEC(ResourceControl::flushReplayQueue(mResourceInstanceIdentifier));
0122 
0123             //This will fail because we still try to resync the previous mail
0124             VERIFYEXEC(ResourceControl::inspect<ApplicationDomain::Mail>(ResourceControl::Inspection::ExistenceInspection(mail, true)));
0125             //The cache will be off by one (because we failed to replay)
0126             VERIFYEXEC(ResourceControl::inspect<ApplicationDomain::Folder>(ResourceControl::Inspection::CacheIntegrityInspection(folder)));
0127         }
0128 
0129     }
0130 };
0131 
0132 QTEST_MAIN(ImapMailTest)
0133 
0134 #include "imapmailtest.moc"