|
@@ -824,3 +824,50 @@ TEST(ImapFetchJson, ProjectsThePartNumbersAPeekWouldNeed) {
|
|
|
EXPECT_EQ(j.at("parts")[2].at("number"), "3");
|
|
EXPECT_EQ(j.at("parts")[2].at("number"), "3");
|
|
|
EXPECT_EQ(j.at("parts")[2].at("parts")[0].at("parts")[1].at("number"), "3.2");
|
|
EXPECT_EQ(j.at("parts")[2].at("parts")[0].at("parts")[1].at("number"), "3.2");
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+// ---------------------------------------------------------------------------
|
|
|
|
|
+// The three shapes that produced wrong ANSWERS (not crashes) in a sibling
|
|
|
|
|
+// implementation of this protocol. Each is a case where a parser reports
|
|
|
|
|
+// success while handing back something that is not the email, which is far
|
|
|
|
|
+// worse than failing: the caller cannot tell. Pinned here because our reader
|
|
|
|
|
+// is octet-counted and therefore immune to all three, and a future
|
|
|
|
|
+// "optimisation" that searched for a terminator would reintroduce every one.
|
|
|
|
|
+// ---------------------------------------------------------------------------
|
|
|
|
|
+
|
|
|
|
|
+TEST(ImapFetchWrongAnswerShapes, AnAbsentUidIsZeroMessagesRatherThanTheProtocolLineAsAnEmail) {
|
|
|
|
|
+ // Fetching nothing is not an error in IMAP: the server answers the tagged
|
|
|
|
|
+ // completion and sends no FETCH line. A parser that reports success and
|
|
|
|
|
+ // then reads the buffer anyway returns "A003 OK Fetch completed" AS THE
|
|
|
|
|
+ // MESSAGE BODY, which then parses into a mail with no subject and no
|
|
|
|
|
+ // sender - a successful fetch of a message that does not exist. The caller
|
|
|
|
|
+ // above must turn this into not-found; what matters here is that no record
|
|
|
|
|
+ // is manufactured.
|
|
|
|
|
+ const ImapFetchResult r = parseFetchResponse("A003 OK Fetch completed\r\n");
|
|
|
|
|
+ EXPECT_EQ(r.status, ImapParse::Ok);
|
|
|
|
|
+ EXPECT_TRUE(r.messages.empty());
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(ImapFetchWrongAnswerShapes, ALiteralCutShortIsNeedMoreAndYieldsNoPartialBody) {
|
|
|
|
|
+ // The announced count is 50 and 10 bytes arrived. Reporting success with
|
|
|
|
|
+ // the 10 bytes gives a short but perfectly valid-looking email, so an
|
|
|
|
|
+ // interrupted transfer is indistinguishable from a brief message.
|
|
|
|
|
+ const ImapFetchResult r = parseFetchResponse("* 1 FETCH (UID 7 BODY[] {50}\r\n0123456789");
|
|
|
|
|
+ EXPECT_EQ(r.status, ImapParse::NeedMore);
|
|
|
|
|
+ EXPECT_TRUE(r.messages.empty());
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(ImapFetchWrongAnswerShapes, ABodyContainingATaggedReplyAndTheTerminatorSurvivesByteIntact) {
|
|
|
|
|
+ // This is why the byte count is trusted and no delimiter is searched for.
|
|
|
|
|
+ // The body deliberately contains both ")\r\n" and a line that looks like a
|
|
|
|
|
+ // tagged completion; real mail contains such text, and a
|
|
|
|
|
+ // terminator-searching parser truncates exactly these messages.
|
|
|
|
|
+ const std::string body = "A003 OK Fetch completed\r\n)\r\nreal body\r\n";
|
|
|
|
|
+ const std::string response = "* 1 FETCH (UID 7 BODY[] {" + std::to_string(body.size()) +
|
|
|
|
|
+ "}\r\n" + body + ")\r\nA003 OK Fetch completed\r\n";
|
|
|
|
|
+ const ImapFetchResult r = parseFetchResponse(response);
|
|
|
|
|
+ ASSERT_EQ(r.status, ImapParse::Ok);
|
|
|
|
|
+ ASSERT_EQ(r.messages.size(), 1u);
|
|
|
|
|
+ const auto it = r.messages[0].bodyParts.find("");
|
|
|
|
|
+ ASSERT_NE(it, r.messages[0].bodyParts.end());
|
|
|
|
|
+ EXPECT_EQ(it->second, body);
|
|
|
|
|
+}
|