Procházet zdrojové kódy

fix(tests): remove a real name and e-mail address from the mime_words tests

The RFC 2047 cases carried a real person's name and a real work address
in nine places. They were ported verbatim from an existing internal
implementation, which was the right call for fidelity and is also how
the identity came with them. Personal data does not belong in source,
tests or docs.

Replaced with a made-up accented Hungarian name and an example.com
address. The substitute stays ACCENTED deliberately: the accented
display name is the entire reason those cases exist, so a plain-ASCII
placeholder would quietly stop testing the thing under test.

The pre-encoded =?UTF-8?Q?...?= literal was recomputed rather than
hand-edited, and the decode assertion is what verifies it: corrupting
one byte of the encoded word fails exactly that case, so it checks the
encoding and not my arithmetic. Worth noting my first attempt at the
encoding was wrong AND my own check passed it, because the encoder and
the independent decoder I wrote to verify it shared the same flaw -
treating bytes above 127 as Latin-1 letters. A verification that cannot
fail is worse than none.

Also pins a FETCH shape no fake server produced, measured elsewhere
against a real provider: a provider volunteers the new FLAGS in the same
untagged line as the body when the fetch set them, so the data items in
front of the {n} are not a fixed shape. The parser already walks
name/value pairs over a parsed s-expression and was correct here; three
tests now assert it rather than leaving it to luck, including flags
trailing the literal, which is the order a positional parser would get
wrong.

Reported by the owners of that file under the one-way ownership
agreement. 1197/1197 green.
fszontagh před 1 měsícem
rodič
revize
6aebe2b283
3 změnil soubory, kde provedl 61 přidání a 9 odebrání
  1. 2 0
      CLAUDE.md
  2. 50 0
      runner/tests/imap_fetch_test.cpp
  3. 9 9
      tests/test_mime_words.cpp

+ 2 - 0
CLAUDE.md

@@ -103,6 +103,8 @@ docker run -d --name svapi-dev -p 8080:8080 -e SMARTBOTIC_VECTORAPI_KEY=dev svap
 - **One handle per (run, account), destroyed with the session. No `CURLSH`, no cross-tenant reuse.** A handle carries connection state and credentials. A fresh handle per command would mean a TCP connect, a TLS handshake and a LOGIN for every command, which is both slow and what gets an account throttled; a test asserts one LOGIN serves three commands.
 - **VERIFIED AGAINST A LIVE SERVER on 2026-10-08** (Dovecot-ish on `mail.onmail.hu`, IMAP 993 implicit TLS and SMTP 587 STARTTLS, credentials in `/data/dev/.env`). Everything below was measured against that server, not a fixture, and every fake-server claim in this section survived it: `LIST` and modified-UTF-7 decoding, `EXAMINE` reporting `PERMANENTFLAGS ()` read-only, `UID SEARCH` including `UNSEEN`, **a peek fetch of a genuinely UNSEEN message leaving it unseen** (the weak version of this test uses an already-seen message and proves nothing), the direct parse of a good fetch returning `NeedMore` while the framed re-parse returns `Ok`, no tagged line in the buffer, and **a multi-uid `UID FETCH` delivering zero bytes**. A full send-and-read-back round trip confirmed an accented Hungarian subject surviving as two `=?UTF-8?B?` encoded words and decoding back exactly, quoted-printable chosen for the accented text, a lone `.` body line arriving intact and not double-stuffed, an RFC 2231 attachment filename, and `BODYSTRUCTURE` part numbering of `1.1` for the text inside a `multipart/alternative` inside a `multipart/mixed` with the attachment at `2`. Note `EXAMINE` alone prevents a flag change, so that test confirms the read path is safe but does NOT isolate `BODY.PEEK`'s own contribution.
 - **The fake IMAP/SMTP servers are committed** (`runner/tests/fake_mail_server.hpp`): in-process TCP listeners on **ephemeral** ports. Ephemeral because a fixed port collides - twice during development a test reached a stale server from an earlier run and "passed" against the wrong process. They **mask credentials** before recording a transcript, and the SMTP one answers a bare `AUTH PLAIN` with a **334 challenge, not 235**: answering 235 made an earlier harness report a working login for a client that had sent no credentials. It also **un-stuffs** the leading period during DATA, as a real receiver does, or a correct client looks like it double-stuffed.
+- **A FETCH response's data items in front of the `{n}` are NOT a fixed shape.** Measured against a real mail provider: when a fetch sets flags, the provider volunteers the new `FLAGS` in the SAME untagged line as the body, so the items before the literal vary in number and order. Anchoring on the first `{` after `FETCH` is safe; counting fields or assuming a prefix length is not. Our parser walks name/value pairs over a parsed s-expression, so order is already irrelevant and unrecognised items land in `unknownItems` rather than breaking the parse - three tests pin that instead of assuming it.
+- **No real name or e-mail address belongs in source, tests or docs.** The `mime_words` tests arrived with a real person's name and a real work address in nine places, because the cases were ported verbatim and the accented display name is the entire point of the RFC 2047 cases. The substitute must therefore stay ACCENTED - a plain-ASCII placeholder would quietly stop testing what the case exists for. When replacing one, recompute any pre-encoded `=?UTF-8?Q?...?=` literal rather than hand-editing it, and let the decode assertion verify the new encoding: a one-byte corruption of the encoded word must fail that case, which is how you know it checks the encoding and not your arithmetic.
 - `src/functions/mime_words.*` is **adapted from an existing internal implementation**, is owned elsewhere, and changes flow one way INTO this repo. If a decoding bug turns up here, report it rather than fixing only this copy.
 
 ## Data model & runtime config

+ 50 - 0
runner/tests/imap_fetch_test.cpp

@@ -871,3 +871,53 @@ TEST(ImapFetchWrongAnswerShapes, ABodyContainingATaggedReplyAndTheTerminatorSurv
     ASSERT_NE(it, r.messages[0].bodyParts.end());
     EXPECT_EQ(it->second, body);
 }
+
+// A shape no fake server produced, measured against a real mail provider by
+// another team: when a fetch sets flags, the provider volunteers the NEW flags
+// in the SAME untagged line as the body. So the data items in front of the
+// `{n}` are not a fixed shape, and anything counting fields or assuming a
+// prefix length breaks on it. Our parser walks name/value pairs over a parsed
+// s-expression, so order is already irrelevant - these pin that rather than
+// assume it.
+TEST(ImapFetch, FlagsVolunteeredBeforeTheBodyLiteralDoNotShiftTheParse) {
+    const std::string raw =
+        "* 1 FETCH (UID 101 FLAGS (\\Seen \\Recent) BODY[HEADER] {12}\r\n"
+        "Subject: hi\n)\r\n";
+    const ImapFetchResult r = parseFetchResponse(raw);
+    ASSERT_EQ(r.status, ImapParse::Ok);
+    ASSERT_EQ(r.messages.size(), 1u);
+    const ImapFetchRecord& m = r.messages.front();
+    EXPECT_EQ(m.uid, 101u);
+    ASSERT_EQ(m.bodyParts.count("HEADER"), 1u);
+    EXPECT_EQ(m.bodyParts.at("HEADER"), "Subject: hi\n");
+    EXPECT_EQ(m.flags.size(), 2u);
+}
+
+TEST(ImapFetch, FlagsVolunteeredAFTERTheBodyLiteralAreStillRead) {
+    // The order a positional parser would certainly get wrong: the body comes
+    // first and the flags trail it.
+    const std::string raw =
+        "* 1 FETCH (UID 101 BODY[HEADER] {12}\r\n"
+        "Subject: hi\n FLAGS (\\Seen))\r\n";
+    const ImapFetchResult r = parseFetchResponse(raw);
+    ASSERT_EQ(r.status, ImapParse::Ok);
+    ASSERT_EQ(r.messages.size(), 1u);
+    const ImapFetchRecord& m = r.messages.front();
+    EXPECT_EQ(m.uid, 101u);
+    EXPECT_EQ(m.bodyParts.at("HEADER"), "Subject: hi\n");
+    ASSERT_EQ(m.flags.size(), 1u);
+    EXPECT_EQ(m.flags.front(), "\\Seen");
+}
+
+TEST(ImapFetch, AnUnrecognisedDataItemBesideTheBodyIsKeptNotFatal) {
+    // A provider is free to volunteer items we never asked for. They must not
+    // break the parse, and they must not be silently swallowed either.
+    const std::string raw =
+        "* 1 FETCH (UID 101 X-PROVIDER-THING \"whatever\" BODY[HEADER] {12}\r\n"
+        "Subject: hi\n)\r\n";
+    const ImapFetchResult r = parseFetchResponse(raw);
+    ASSERT_EQ(r.status, ImapParse::Ok);
+    ASSERT_EQ(r.messages.size(), 1u);
+    EXPECT_EQ(r.messages.front().bodyParts.at("HEADER"), "Subject: hi\n");
+    EXPECT_FALSE(r.messages.front().unknownItems.empty());
+}

+ 9 - 9
tests/test_mime_words.cpp

@@ -21,8 +21,8 @@ using svapi::functions::MimeWords;
 TEST(MimeWords, DecodesANameInFrontOfAnAddressLeavingTheAddressAlone) {
     // The shape that started it: only the display name is encoded, and the
     // address must come through untouched.
-    EXPECT_EQ(MimeWords::decode("=?UTF-8?Q?Szont=C3=A1gh_Ferenc?= <ferenc.szontagh@smartbotics.ai>"),
-              "Szontágh Ferenc <ferenc.szontagh@smartbotics.ai>");
+    EXPECT_EQ(MimeWords::decode("=?UTF-8?Q?K=C5=91v=C3=A1g=C3=B3_=C3=96rs?= <ors.kovago@example.com>"),
+              "Kővágó Örs <ors.kovago@example.com>");
 }
 
 TEST(MimeWords, RejoinsTwoWordsWithoutTheFoldSpace) {
@@ -80,14 +80,14 @@ TEST(MimeWords, ExistingQuotesAreNotDoubled) {
 TEST(MimeWords, RoundTripsWhateverWeSend) {
     // The property that matters most: whatever we send, a conforming reader
     // gets back what we meant.
-    for (const std::string& original : {std::string("Szontágh Ferenc"),
+    for (const std::string& original : {std::string("Kővágó Örs"),
                                         std::string("Sárkányok párzási és étkezési szokásaik"),
                                         std::string("árvíztűrő tükörfúrógép"),
                                         std::string("plain ascii")})
         EXPECT_EQ(MimeWords::decode(MimeWords::encode(original)), original) << original;
 
-    EXPECT_EQ(MimeWords::decode(MimeWords::encodeAddress("Szontágh Ferenc <a@b.com>")),
-              "Szontágh Ferenc <a@b.com>");
+    EXPECT_EQ(MimeWords::decode(MimeWords::encodeAddress("Kővágó Örs <a@b.com>")),
+              "Kővágó Örs <a@b.com>");
 }
 
 // --- the header/envelope distinction, which is where a send breaks ----------
@@ -97,7 +97,7 @@ TEST(MimeWords, TheEnvelopeTakesAnAddressAndNothingElse) {
     // by dropping the connection - reported as "Failed sending data to the
     // peer", which names neither the address nor the reason. That is why this
     // function exists rather than being a caller's responsibility.
-    EXPECT_EQ(MimeWords::envelopeAddress("Szontágh Ferenc <a@b.com>"), "a@b.com");
+    EXPECT_EQ(MimeWords::envelopeAddress("Kővágó Örs <a@b.com>"), "a@b.com");
     EXPECT_EQ(MimeWords::envelopeAddress("a@b.com"), "a@b.com");
     EXPECT_EQ(MimeWords::envelopeAddress("  a@b.com  "), "a@b.com");
     EXPECT_EQ(MimeWords::envelopeAddress("\"Doe, John\" <a@b.com>"), "a@b.com");
@@ -108,12 +108,12 @@ TEST(MimeWords, OneAddressYieldsTwoDifferentThings) {
     // (RFC 5322 name-addr, which is what the recipient's client shows) and the
     // ENVELOPE drops it (RFC 5321 takes an address only). They are two
     // different "To"s, and feeding one to the other is what broke a send.
-    const std::string address = "Szontágh Ferenc <ferenc.szontagh@smartbotics.ai>";
+    const std::string address = "Kővágó Örs <ors.kovago@example.com>";
     const std::string header = MimeWords::encodeAddress(address);
     EXPECT_EQ(MimeWords::decode(header), address) << "the header round-trips to what was meant";
-    EXPECT_NE(header.find("ferenc.szontagh@smartbotics.ai"), std::string::npos)
+    EXPECT_NE(header.find("ors.kovago@example.com"), std::string::npos)
         << "and keeps the address untouched: " << header;
-    EXPECT_EQ(MimeWords::envelopeAddress(address), "ferenc.szontagh@smartbotics.ai");
+    EXPECT_EQ(MimeWords::envelopeAddress(address), "ors.kovago@example.com");
 }
 
 TEST(MimeWords, AFilenameUsesRfc2231WhenItNeedsTo) {