Bug 1456001 - Bug 1454257 follow-up: fix inter-word space compression and add test for header folding in database. r=aceman a=jorgk
authorJorg K <jorgk@jorgk.com>
Mon, 23 Apr 2018 21:53:12 +0200
changeset 31415 2ed7c90c8ab0e9c3bc5fe621300092f1d15d0e28
parent 31414 ef08af42ccdff19d0c8317d73674a7dd43d9c7b3
child 31416 6f5d2abfedc93296ff6ea0a8dc9705822fb69d11
push id383
push userclokep@gmail.com
push dateMon, 07 May 2018 21:52:48 +0000
reviewersaceman, jorgk
bugs1456001, 1454257
Bug 1456001 - Bug 1454257 follow-up: fix inter-word space compression and add test for header folding in database. r=aceman a=jorgk
mailnews/base/test/unit/test_headerFoldingInDatabase.js
mailnews/base/test/unit/xpcshell.ini
mailnews/local/src/nsParseMailbox.cpp
mailnews/test/data/badly-folded-headers.eml
new file mode 100644
--- /dev/null
+++ b/mailnews/base/test/unit/test_headerFoldingInDatabase.js
@@ -0,0 +1,44 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+/*
+ * Testing header folding in nsParseMailMessageState::ParseHeaders(),
+ * see bug 1454257 and bug 1456001.
+ */
+
+ChromeUtils.import("resource:///modules/mailServices.js");
+
+var hdr;
+
+function run_test()
+{
+  localAccountUtils.loadLocalMailAccount();
+
+  var copyListener =
+  {
+    OnStartCopy: function() {},
+    OnProgress: function(aProgress, aProgressMax) {},
+    SetMessageKey: function(aKey) {
+      hdr = localAccountUtils.inboxFolder.GetMessageHeader(aKey);
+    },
+    SetMessageId: function(aMessageId) {},
+    OnStopCopy: function(aStatus) { continueTest();}
+  };
+
+  // Get a message into the local filestore.
+  var message = do_get_file("../../../data/badly-folded-headers.eml");
+  do_test_pending();
+  MailServices.copy.CopyFileMessage(message, localAccountUtils.inboxFolder, null, false, 0,
+                                    "", copyListener, null);
+}
+
+function continueTest()
+{
+  Assert.equal(hdr.author, "sender@example.com");
+  Assert.equal(hdr.recipients, "\"Recipient  with  spaces\" <recipient@example.com>");
+  Assert.equal(hdr.subject, "Badly folded headers, one line with   space   between   To and From");
+  hdr = null;
+  do_test_finished();
+}
+
--- a/mailnews/base/test/unit/xpcshell.ini
+++ b/mailnews/base/test/unit/xpcshell.ini
@@ -26,16 +26,17 @@ support-files = nodelist_test.xml data/*
 [test_copyThenMoveManual.js]
 [test_copyToInvalidDB.js]
 [test_detachToFile.js]
 [test_emptyTrash.js]
 [test_fix_deferred_accounts.js]
 [test_folderCompact.js]
 [test_folderLookupService.js]
 [test_getMsgTextFromStream.js]
+[test_headerFoldingInDatabase.js]
 [test_hostnameUtils.js]
 [test_imapPump.js]
 [test_inheritedFolderProperties.js]
 [test_iteratorUtils.js]
 [test_jsTreeSelection.js]
 [test_junkingWhenDisabled.js]
 [test_junkWhitelisting.js]
 [test_loadVirtualFolders.js]
--- a/mailnews/local/src/nsParseMailbox.cpp
+++ b/mailnews/local/src/nsParseMailbox.cpp
@@ -1070,37 +1070,33 @@ nsresult nsParseMailMessageState::ParseH
     // We will be shuffling downwards, so this is our insertion point.
     char *bufWrite = buf;
 
 SEARCH_NEWLINE:
     // move past any non terminating characters, rewriting them if folding white space
     // exists
     while (buf < buf_end && *buf != '\r' && *buf != '\n')
     {
-      // Compress white-space.
-      if ((*buf == ' ' || *buf == '\t') &&
-          (*(bufWrite - 1) == ' ' || *(bufWrite - 1) == '\t')) {
-         buf++;
-         continue;
-      }
       if (buf != bufWrite)
         *bufWrite = *buf;
       buf++;
       bufWrite++;
     }
 
     // Look for folding, so CRLF, CR or LF followed by space or tab.
     if ((buf + 2 < buf_end && (buf[0] == '\r' && buf[1] == '\n') &&
                               (buf[2] == ' ' || buf[2] == '\t')) ||
         (buf + 1 < buf_end && (buf[0] == '\r' || buf[0] == '\n') &&
                               (buf[1] == ' ' || buf[1] == '\t')))
     {
-      // Add a single folding space if necessary.
-      if (*(bufWrite - 1) != ' ' && *(bufWrite - 1) != '\t')
-        *(bufWrite++) = ' ';
+      // Remove trailing spaces at the "write position" and add a single
+      // folding space.
+      while (*(bufWrite - 1) == ' ' || *(bufWrite - 1) == '\t')
+        bufWrite--;
+      *(bufWrite++) = ' ';
 
       // Skip CRLF, CR+space or LF+space ...
       buf += 2;
 
       // ... and skip leading spaces in that line.
       while (buf < buf_end && (*buf == ' ' || *buf == '\t'))
         buf++;
 
new file mode 100644
--- /dev/null
+++ b/mailnews/test/data/badly-folded-headers.eml
@@ -0,0 +1,16 @@
+From - Sat Apr 14 14:01:30 2018
+Date: Sat, 14 Apr 2018 14:01:30 +0200
+Subject: Badly    
+ folded   
+    headers,    
+ one
+ 
+ 
+ line
+ with   space   between   To and From
+ 
+To: "Recipient  with  spaces" <recipient@example.com>
+ 
+From: sender@example.com
+
+Here's our body