fix temp file leakage during pop3 download with quarantining turned on, a=standard8, bug 741027
authorDavid Bienvenu <bienvenu@nventure.com>
Tue, 03 Apr 2012 12:33:20 -0700
changeset 10694 dd4c013f2477484ae2615f805ac9c4866767cf8f
parent 10690 142616026683ff7ae8b22bec6793f9646e76825b
child 10695 c1bb6edc45e29958db812c4db37c5387a4635abe
push idunknown
push userunknown
push dateunknown
reviewersstandard8, bug
bugs741027
fix temp file leakage during pop3 download with quarantining turned on, a=standard8, bug 741027
mailnews/local/src/nsPop3Sink.cpp
mailnews/local/test/unit/test_pop3DownloadTempFileHandling.js
mailnews/local/test/unit/xpcshell.ini
--- a/mailnews/local/src/nsPop3Sink.cpp
+++ b/mailnews/local/src/nsPop3Sink.cpp
@@ -526,21 +526,24 @@ nsPop3Sink::IncorporateBegin(const char*
                                          "newmsg",
                                          getter_AddRefs(tmpDownloadFile));
 
     NS_ASSERTION(NS_SUCCEEDED(rv),
                  "writing tmp pop3 download file: failed to append filename");
     if (NS_FAILED(rv))
       return rv;
 
-    //need a unique tmp file to prevent dataloss in multiuser environment
-    rv = tmpDownloadFile->CreateUnique(nsIFile::NORMAL_FILE_TYPE, 00600);
-    NS_ENSURE_SUCCESS(rv, rv);
+    if (!m_tmpDownloadFile)
+    {
+      //need a unique tmp file to prevent dataloss in multiuser environment
+      rv = tmpDownloadFile->CreateUnique(nsIFile::NORMAL_FILE_TYPE, 00600);
+      NS_ENSURE_SUCCESS(rv, rv);
 
-    m_tmpDownloadFile = do_QueryInterface(tmpDownloadFile, &rv);
+      m_tmpDownloadFile = do_QueryInterface(tmpDownloadFile, &rv);
+    }
     if (NS_SUCCEEDED(rv))
     {
       rv = MsgGetFileStream(m_tmpDownloadFile, getter_AddRefs(m_outFileStream));
       NS_ENSURE_SUCCESS(rv, rv);
     }
   }
   else
   {
new file mode 100644
--- /dev/null
+++ b/mailnews/local/test/unit/test_pop3DownloadTempFileHandling.js
@@ -0,0 +1,66 @@
+/**
+ * The intent of this file is to test temp file handling when
+ * downloading multiple pop3 messages with quarantining turned on.
+ *
+ * Original author: David Bienvenu <dbienvenu@mozilla.com>
+ */
+load("../../../resources/POP3pump.js");
+Components.utils.import("resource://gre/modules/Services.jsm");
+
+var testSubjects = ["[Bug 397009] A filter will let me tag, but not untag",
+                    "Hello, did you receive my bugmail?"];
+var gExpectedFiles;
+
+function run_test()
+{
+  Services.prefs.setBoolPref("mailnews.downloadToTempFile", true);
+  gExpectedFiles = createExpectedTemporaryFiles(2);
+  // add 2 messages
+  gPOP3Pump.files = ["../../../data/bugmail1",
+                     "../../../data/draft1"];
+  gPOP3Pump.onDone = continueTest;
+  do_test_pending();
+  gPOP3Pump.run();
+}
+
+function continueTest()
+{
+  dump("temp file path = " + gExpectedFiles[0].path + "\n");
+  dump("temp file path = " + gExpectedFiles[1].path + "\n");
+  for each (let expectedFile in gExpectedFiles)
+    do_check_false(expectedFile.exists());
+
+  // get message headers for the inbox folder
+  let enumerator = gLocalInboxFolder.msgDatabase.EnumerateMessages();
+  var msgCount = 0;
+  while (enumerator.hasMoreElements())
+  {
+    let hdr = enumerator.getNext().QueryInterface(Ci.nsIMsgDBHdr);
+    do_check_eq(hdr.subject, testSubjects[msgCount++]);
+  }
+  do_check_eq(msgCount, 2);
+  gPOP3Pump = null;
+  do_test_finished();
+}
+
+function createExpectedTemporaryFiles(numFiles) {
+  function createTemporaryFile() {
+    let file = Cc["@mozilla.org/file/directory_service;1"]
+                 .getService(Ci.nsIProperties)
+                 .get("TmpD", Ci.nsIFile);
+    file.append("newmsg");
+    file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0600);
+    return file;
+  }
+
+  let expectedFiles = [];
+  for (i = 0; i < numFiles; i++)
+    expectedFiles.push(createTemporaryFile());
+
+  for each (let expectedFile in expectedFiles)
+    expectedFile.remove(false);
+
+  return expectedFiles;
+}
+
+
--- a/mailnews/local/test/unit/xpcshell.ini
+++ b/mailnews/local/test/unit/xpcshell.ini
@@ -8,16 +8,17 @@ tail = tail_local.js
 [test_mailboxContentLength.js]
 [test_mailboxProtocol.js]
 [test_msgCopy.js]
 [test_msgIDParsing.js]
 [test_over2GBMailboxes.js]
 [test_over4GBMailboxes.js]
 [test_pop3AuthMethods.js]
 [test_pop3Download.js]
+[test_pop3DownloadTempFileHandling.js]
 [test_pop3Duplicates.js]
 [test_pop3GSSAPIFail.js]
 [test_pop3GetNewMail.js]
 [test_pop3MoveFilter.js]
 [test_pop3MoveFilter2.js]
 [test_pop3Password.js]
 [test_pop3Password2.js]
 [test_pop3Password3.js]