File indexing completed on 2024-05-26 05:28:48

0001 /* Copyright (C) 2006 - 2016 Jan Kundrát <jkt@kde.org>
0002 
0003    This file is part of the Trojita Qt IMAP e-mail client,
0004    http://trojita.flaska.net/
0005 
0006    This program is free software; you can redistribute it and/or
0007    modify it under the terms of the GNU General Public License as
0008    published by the Free Software Foundation; either version 2 of
0009    the License or (at your option) version 3 or any later version
0010    accepted by the membership of KDE e.V. (or its successor approved
0011    by the membership of KDE e.V.), which shall act as a proxy
0012    defined in Section 14 of version 3 of the license.
0013 
0014    This program is distributed in the hope that it will be useful,
0015    but WITHOUT ANY WARRANTY; without even the implied warranty of
0016    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0017    GNU General Public License for more details.
0018 
0019    You should have received a copy of the GNU General Public License
0020    along with this program.  If not, see <http://www.gnu.org/licenses/>.
0021 */
0022 
0023 #include "test_Imap_Parser_write.h"
0024 #include "Utils/FakeCapabilitiesInjector.h"
0025 #include "Streams/FakeSocket.h"
0026 
0027 #define APPEND_PREFIX "APPEND a \"\\d{2}-[a-zA-Z]{3}-\\d{4} \\d{2}:\\d{2}:\\d{2} [+-]?\\d{4}\" "
0028 
0029 QByteArray plaintext10 = QByteArray(10, 'x');
0030 QByteArray plaintext4096 = QByteArray(4096, 'y');
0031 QByteArray plaintext4097 = QByteArray(4097, 'z');
0032 
0033 using namespace Imap::Mailbox;
0034 
0035 void ImapParserWriteTest::testNoLiteralPlus()
0036 {
0037     model->appendIntoMailbox(QStringLiteral("a"), plaintext10, QStringList(), QDateTime::currentDateTime());
0038     cClientRegExp(t.mk(APPEND_PREFIX) + "\\{" + QByteArray::number(plaintext10.size()) + "\\}");
0039     cServer("+ OK send your literal\r\n");
0040     cClient(plaintext10 + "\r\n");
0041     cServer(t.last("OK stored\r\n"));
0042     cEmpty();
0043 }
0044 
0045 void ImapParserWriteTest::testLiteralPlus()
0046 {
0047     FakeCapabilitiesInjector caps(model);
0048     caps.injectCapability(QStringLiteral("LITERAL+"));
0049     model->appendIntoMailbox(QStringLiteral("a"), plaintext4097, QStringList(), QDateTime::currentDateTime());
0050     // cClientRegExp doesn't support multiline data
0051     for (int i = 0; i < 10; ++i) {
0052         QCoreApplication::processEvents();
0053     }
0054     auto buf = SOCK->writtenStuff();
0055     QVERIFY(buf.startsWith(t.mk("APPEND a")));
0056     QVERIFY(buf.endsWith(QByteArrayLiteral(" {4097+}\r\n") + plaintext4097 + QByteArrayLiteral("\r\n")));
0057     cServer(t.last("OK stored\r\n"));
0058     cEmpty();
0059 }
0060 
0061 void ImapParserWriteTest::testLiteralMinus()
0062 {
0063     FakeCapabilitiesInjector caps(model);
0064     caps.injectCapability(QStringLiteral("LITERAL-"));
0065 
0066     model->appendIntoMailbox(QStringLiteral("a"), plaintext4096, QStringList(), QDateTime::currentDateTime());
0067     for (int i = 0; i < 10; ++i) {
0068         QCoreApplication::processEvents();
0069     }
0070     auto buf = SOCK->writtenStuff();
0071     QVERIFY(buf.startsWith(t.mk("APPEND a")));
0072     QVERIFY(buf.endsWith(QByteArrayLiteral(" {4096+}\r\n") + plaintext4096 + QByteArrayLiteral("\r\n")));
0073     cServer(t.last("OK stored\r\n"));
0074     cEmpty();
0075 
0076     model->appendIntoMailbox(QStringLiteral("a"), plaintext4097, QStringList(), QDateTime::currentDateTime());
0077     cClientRegExp(t.mk(APPEND_PREFIX) + "\\{" + QByteArray::number(plaintext4097.size()) + "\\}");
0078     cServer("+ OK send your literal\r\n");
0079     cClient(plaintext4097 + "\r\n");
0080     cServer(t.last("OK stored\r\n"));
0081     cEmpty();
0082 }
0083 
0084 QTEST_GUILESS_MAIN(ImapParserWriteTest)