Bug 648018 - ISP files aren't read anymore; r=BenB
authorMark Banner <bugzilla@standard8.plus.com>
Mon, 02 May 2011 15:50:56 +0100
changeset 7665 23be2a8ad62761d89d36446c35c0e778dbd8c9b9
parent 7664 e30e000dd47c50f7f200b1eb8fe86c0c6ed8abe8
child 7666 42c914e4278333e090fadb8f8dca8e0506d7d121
push id5881
push userbugzilla@standard8.plus.com
push dateMon, 02 May 2011 14:51:56 +0000
treeherdercomm-central@23be2a8ad627 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersBenB
bugs648018
Bug 648018 - ISP files aren't read anymore; r=BenB
mailnews/base/prefs/content/accountcreation/fetchConfig.js
mailnews/base/test/unit/data/example.com.xml
mailnews/base/test/unit/test_autoconfigFetchDisk.js
--- a/mailnews/base/prefs/content/accountcreation/fetchConfig.js
+++ b/mailnews/base/prefs/content/accountcreation/fetchConfig.js
@@ -36,24 +36,30 @@
  * ***** END LICENSE BLOCK ***** */
 
 /**
  * Tries to find a configuration for this ISP on the local harddisk, in the
  * application install directory's "isp" subdirectory.
  * Params @see fetchConfigFromISP()
  */
 
+Components.utils.import("resource://gre/modules/Services.jsm");
+
 function fetchConfigFromDisk(domain, successCallback, errorCallback)
 {
   return new TimeoutAbortable(runAsync(function()
   {
     try {
       // <TB installdir>/isp/example.com.xml
-      var uri = "resource:///isp/" + sanitize.hostname(domain) + ".xml";
-      var contents = readURLasUTF8(makeNSIURI(uri));
+      var configLocation = Services.dirsvc.get("CurProcD", Ci.nsIFile);
+      configLocation.append("isp");
+      configLocation.append(sanitize.hostname(domain) + ".xml");
+
+      var contents =
+        readURLasUTF8(Services.io.newFileURI(configLocation));
        // Bug 336551 trips over <?xml ... >
       contents = contents.replace(/<\?xml[^>]*\?>/, "");
       successCallback(readFromXML(new XML(contents)));
     } catch (e) { errorCallback(e); }
   }));
 }
 
 /**
@@ -131,17 +137,17 @@ function fetchConfigFromISP(domain, emai
  * Params @see fetchConfigFromISP()
  */
 
 function fetchConfigFromDB(domain, successCallback, errorCallback)
 {
   let pref = Cc["@mozilla.org/preferences-service;1"]
              .getService(Ci.nsIPrefBranch);
   let url = pref.getCharPref("mailnews.auto_config_url");
-  let domain = sanitize.hostname(domain);
+  domain = sanitize.hostname(domain);
 
   // If we don't specify a place to put the domain, put it at the end.
   if (url.indexOf("{{domain}}") == -1)
     url = url + domain;
   else
     url = url.replace("{{domain}}", domain);
   var accountManager = Cc["@mozilla.org/messenger/account-manager;1"]
                        .getService(Ci.nsIMsgAccountManager);
@@ -177,17 +183,17 @@ function fetchConfigFromDB(domain, succe
  * - We make a look up for the base domain. E.g. if MX is
  *   mx1.incoming.servers.hoster.com, we look up hoster.com.
  *   Thanks to nsIEffectiveTLDService, we also get bbc.co.uk right.
  *
  * Params @see fetchConfigFromISP()
  */
 function fetchConfigForMX(domain, successCallback, errorCallback)
 {
-  var domain = sanitize.hostname(domain);
+  domain = sanitize.hostname(domain);
 
   var sucAbortable = new SuccessiveAbortable();
   var time = Date.now();
   sucAbortable.current = getMX(domain,
     function(mxHostname) // success
     {
       ddump("getmx took " + (Date.now() - time) + "ms");
       var tldServ = Cc["@mozilla.org/network/effective-tld-service;1"]
@@ -225,17 +231,17 @@ function fetchConfigForMX(domain, succes
  * @param successCallback {function(hostname {String})
  *   Called when we found an MX for the domain.
  *   For |hostname|, see description above.
  * @param errorCallback @see fetchConfigFromISP()
  * @returns @see fetchConfigFromISP()
  */
 function getMX(domain, successCallback, errorCallback)
 {
-  let domain = sanitize.hostname(domain);
+  domain = sanitize.hostname(domain);
 
   let pref = Cc["@mozilla.org/preferences-service;1"]
                .getService(Ci.nsIPrefBranch);
   let url = pref.getCharPref("mailnews.mx_service_url");
   if (!url)
     errorCallback("no URL for MX service configured");
   url += domain;
 
new file mode 100644
--- /dev/null
+++ b/mailnews/base/test/unit/data/example.com.xml
@@ -0,0 +1,21 @@
+<clientConfig version="1.1">
+  <emailProvider id="example.com">
+    <domain>example.com</domain>
+    <displayName>example.com</displayName>
+    <displayShortName>example.com</displayShortName>
+    <incomingServer type="pop3">
+      <hostname>pop.example.com</hostname>
+      <port>995</port>
+      <socketType>SSL</socketType>
+      <authentication>plain</authentication>
+      <username>%EMAILLOCALPART%</username>
+    </incomingServer>
+    <outgoingServer type="smtp">
+      <hostname>smtp.example.com</hostname>
+      <port>587</port>
+      <socketType>STARTTLS</socketType>
+      <username>%EMAILADDRESS%</username>
+      <authentication>plain</authentication>
+    </outgoingServer>
+  </emailProvider>
+</clientConfig>
new file mode 100644
--- /dev/null
+++ b/mailnews/base/test/unit/test_autoconfigFetchDisk.js
@@ -0,0 +1,130 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* ***** BEGIN LICENSE BLOCK *****
+ * Version: MPL 1.1/GPL 2.0/LGPL 2.1
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Original Code is autoconfig test code.
+ *
+ * The Initial Developer of the Original Code is
+ *   Mozilla Foundation
+ * Portions created by the Initial Developer are Copyright (c) 2011
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ *   Mark Banner <mbanner@mozilla.com>
+ *
+ * Alternatively, the contents of this file may be used under the terms of
+ * either the GNU General Public License Version 2 or later (the "GPL"), or
+ * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+ * in which case the provisions of the GPL or the LGPL are applicable instead
+ * of those above. If you wish to allow use of your version of this file only
+ * under the terms of either the GPL or the LGPL, and not to allow others to
+ * use your version of this file under the terms of the MPL, indicate your
+ * decision by deleting the provisions above and replace them with the notice
+ * and other provisions required by the GPL or the LGPL. If you do not delete
+ * the provisions above, a recipient may use your version of this file under
+ * the terms of any one of the MPL, the GPL or the LGPL.
+ *
+ * ***** END LICENSE BLOCK ***** */
+
+/**
+ * Tests getting a configuration file from the local isp directory and
+ * reading that file.
+ */
+
+// Globals
+const kXMLFile = "example.com.xml";
+var fetchConfigAbortable;
+var copyLocation;
+
+var xmlReader =
+{
+  setTimeout : function(func, interval) {
+    do_timeout(interval, func);
+  }
+};
+
+try {
+  let loader = Cc["@mozilla.org/moz/jssubscript-loader;1"]
+                 .getService(Ci.mozIJSSubScriptLoader);
+  loader.loadSubScript(
+    "chrome://messenger/content/accountcreation/util.js", xmlReader);
+  loader.loadSubScript(
+    "chrome://messenger/content/accountcreation/fetchConfig.js", xmlReader);
+  loader.loadSubScript(
+    "chrome://messenger/content/accountcreation/accountConfig.js", xmlReader);
+  loader.loadSubScript(
+    "chrome://messenger/content/accountcreation/sanitizeDatatypes.js",
+    xmlReader);
+  loader.loadSubScript(
+    "chrome://messenger/content/accountcreation/readFromXML.js", xmlReader);
+} catch (ex) {
+  dump(ex);
+  // The "accountcreation" files are not available in SeaMonkey (yet).
+  xmlReader = null;
+}
+
+function onTestSuccess(config)
+{
+  // Check that we got the expected config.
+  xmlReader.replaceVariables(config, 
+                             "Yamato Nadeshiko",
+                             "yamato.nadeshiko@example.com",
+                             "abc12345");
+
+  do_check_eq(config.incoming.username, "yamato.nadeshiko");
+  do_check_eq(config.outgoing.username, "yamato.nadeshiko@example.com");
+  do_check_eq(config.incoming.hostname, "pop.example.com");
+  do_check_eq(config.outgoing.hostname, "smtp.example.com");
+  do_check_eq(config.identity.realname, "Yamato Nadeshiko");
+  do_check_eq(config.identity.emailAddress, "yamato.nadeshiko@example.com");
+  do_test_finished();
+}
+
+function onTestFailure(e)
+{
+  do_throw(e);
+}
+
+function run_test()
+{
+  do_register_cleanup(finish_test);
+  if (!xmlReader) {
+    // if you see this and this is Thunderbird, then it's an error
+    dump("INFO | test_autoconfigFetchDisk.js not running, because this is SeaMonkey.");
+    return;
+  }
+
+  // Copy the xml file into place
+  let file = do_get_file("data/" + kXMLFile);
+
+  copyLocation = Services.dirsvc.get("CurProcD", Ci.nsIFile);
+  copyLocation.append("isp");
+  
+  file.copyTo(copyLocation, kXMLFile);
+
+  do_test_pending();
+
+  // Now run the actual test
+  // Note we keep a global copy of this so that the abortable doesn't get
+  // garbage collected before the async operation has finished.
+  fetchConfigAbortable = xmlReader.fetchConfigFromDisk("example.com",
+                                                       onTestSuccess,
+                                                       onTestFailure);
+}
+
+function finish_test()
+{
+  // Remove the test config file
+  copyLocation.append(kXMLFile);
+  copyLocation.remove(false);
+}