|
@@ -7,6 +7,7 @@
|
|
|
#include <thread>
|
|
#include <thread>
|
|
|
|
|
|
|
|
#include "fake_mail_server.hpp"
|
|
#include "fake_mail_server.hpp"
|
|
|
|
|
+#include "net/imap_fetch.hpp"
|
|
|
#include "net/mail_session.hpp"
|
|
#include "net/mail_session.hpp"
|
|
|
|
|
|
|
|
using namespace svapi::runner::net;
|
|
using namespace svapi::runner::net;
|
|
@@ -129,6 +130,81 @@ TEST_F(ImapFixture, CurlConsumesTheClosingParenAndTheTaggedCompletionLine) {
|
|
|
mailGlobalCleanup();
|
|
mailGlobalCleanup();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+TEST_F(ImapFixture, AnOverstatedLiteralIsDELIVEREDasSuccessAndCaughtOnlyByItsCONTENT) {
|
|
|
|
|
+ // The premise the whole padded-literal defence rests on, pinned against
|
|
|
|
|
+ // REAL curl rather than a hand-written buffer.
|
|
|
|
|
+ //
|
|
|
|
|
+ // A server announcing `{n}` larger than the body it sends is not refused by
|
|
|
|
|
+ // libcurl: curl counts the response's own framing toward n and reports
|
|
|
|
|
+ // CURLE_OK, so the literal handed to the write callback is the body plus
|
|
|
|
|
+ // `)<CRLF>` plus the first bytes of the tagged completion. There is no
|
|
|
|
|
+ // structural signal - curl strips the real framing in the healthy case too,
|
|
|
|
|
+ // so this response and a correct one are byte-for-byte the same SHAPE. Only
|
|
|
|
|
+ // the CONTENT differs, which is why `literalEndsWithFraming` exists.
|
|
|
|
|
+ //
|
|
|
|
|
+ // This is a PIN-BUMP test. It was measured once on 8.22.0 and recorded in a
|
|
|
|
|
+ // comment, and a comment cannot notice when curl changes: were a later curl
|
|
|
|
|
+ // to start rejecting the miscount, the detector would become dead code and
|
|
|
|
|
+ // nothing in the suite would say so. If this test ever fails because the
|
|
|
|
|
+ // command itself failed, that is the news - read it as "curl now catches
|
|
|
|
|
+ // this", not as a broken test.
|
|
|
|
|
+ ASSERT_EQ(mailGlobalInit(), std::string());
|
|
|
|
|
+ server.messageBody = "0123456789"; // 10 bytes
|
|
|
|
|
+ server.literalOverstateBytes = 10; // ... announced as 20
|
|
|
|
|
+ ImapSession session;
|
|
|
|
|
+ ASSERT_TRUE(session.open(accountFor(server.port()), optionsFor()).ok());
|
|
|
|
|
+
|
|
|
|
|
+ std::string response;
|
|
|
|
|
+ const MailFailure f = session.command("INBOX", "UID FETCH 7 BODY.PEEK[]", response);
|
|
|
|
|
+
|
|
|
|
|
+ // Half one: curl did NOT notice. Stated as its own assertion because it is
|
|
|
|
|
+ // the surprising half and the reason the content check has to exist.
|
|
|
|
|
+ ASSERT_TRUE(f.ok()) << "curl refused the overstated literal itself (" << f.slug << ": "
|
|
|
|
|
+ << f.message << ") - if that is now curl's behaviour, "
|
|
|
|
|
+ << "literalEndsWithFraming has become dead code and this test should say "
|
|
|
|
|
+ << "so rather than be deleted";
|
|
|
|
|
+ // Exactly 20 bytes of "literal", the last ten of them protocol text. The
|
|
|
|
|
+ // header is asserted whole and the tail by SHAPE, because the tag is
|
|
|
|
|
+ // curl's own and its NUMBER depends on how many commands preceded this one
|
|
|
|
|
+ // on the session - pinning "A003" would make the test fail if a login or a
|
|
|
|
|
+ // SELECT were ever added ahead of it, which is not what it is testing.
|
|
|
|
|
+ const std::string header = "* 1 FETCH (UID 7 BODY[] {20}\r\n";
|
|
|
|
|
+ ASSERT_EQ(response.rfind(header, 0), 0u) << "unexpected response: " << response;
|
|
|
|
|
+ const std::string literal = response.substr(header.size());
|
|
|
|
|
+ EXPECT_EQ(literal.size(), 20u) << "curl delivered a different count than it was announced";
|
|
|
|
|
+ EXPECT_EQ(literal.rfind("0123456789)\r\nA", 0), 0u)
|
|
|
|
|
+ << "the padding was not the response's own framing: " << literal;
|
|
|
|
|
+ EXPECT_NE(literal.find(" OK"), std::string::npos) << literal;
|
|
|
|
|
+
|
|
|
|
|
+ // Half two: the detector fires on what curl ACTUALLY delivered. The two
|
|
|
|
|
+ // parses mirror `parseCurlFetch` in host_context.cpp - the direct parse is
|
|
|
|
|
+ // short of its own announced count, and the framed re-parse is what reaches
|
|
|
|
|
+ // the content check.
|
|
|
|
|
+ const ImapFetchResult direct = parseFetchResponse(response);
|
|
|
|
|
+ EXPECT_NE(direct.status, ImapParse::Ok) << "the direct parse should still be short";
|
|
|
|
|
+ const ImapFetchResult framed = parseFetchResponse(response + ")\r\n");
|
|
|
|
|
+ ASSERT_EQ(framed.status, ImapParse::Ok);
|
|
|
|
|
+ ASSERT_EQ(framed.messages.size(), 1u);
|
|
|
|
|
+ ASSERT_FALSE(framed.messages.front().bodyParts.empty());
|
|
|
|
|
+ EXPECT_TRUE(literalEndsWithFraming(framed.messages.front().bodyParts.begin()->second))
|
|
|
|
|
+ << "curl's padding was not recognised as framing, so a function would have received "
|
|
|
|
|
+ "protocol text welded to the end of a message body";
|
|
|
|
|
+
|
|
|
|
|
+ // The CONTROL, and it is what makes the assertion above mean anything: the
|
|
|
|
|
+ // same server without the overstatement delivers a literal the detector
|
|
|
|
|
+ // does NOT flag. Without this, the test would pass just as well against a
|
|
|
|
|
+ // detector that flagged everything.
|
|
|
|
|
+ server.literalOverstateBytes = 0;
|
|
|
|
|
+ std::string honest;
|
|
|
|
|
+ ASSERT_TRUE(session.command("INBOX", "UID FETCH 7 BODY.PEEK[]", honest).ok());
|
|
|
|
|
+ const ImapFetchResult good = parseFetchResponse(honest + ")\r\n");
|
|
|
|
|
+ ASSERT_EQ(good.status, ImapParse::Ok);
|
|
|
|
|
+ ASSERT_FALSE(good.messages.front().bodyParts.empty());
|
|
|
|
|
+ EXPECT_FALSE(literalEndsWithFraming(good.messages.front().bodyParts.begin()->second))
|
|
|
|
|
+ << "a correct response was flagged as padded, which would refuse every good fetch";
|
|
|
|
|
+ mailGlobalCleanup();
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
TEST_F(ImapFixture, TheSessionReusesOneConnectionForSeveralCommands) {
|
|
TEST_F(ImapFixture, TheSessionReusesOneConnectionForSeveralCommands) {
|
|
|
// One handle per (run, account): a fresh handle per command would mean a
|
|
// One handle per (run, account): a fresh handle per command would mean a
|
|
|
// TCP connect, a TLS handshake and a LOGIN every time, which is both slow
|
|
// TCP connect, a TLS handshake and a LOGIN every time, which is both slow
|