diff options
Diffstat (limited to 'comm/mail/components/accountcreation/test/xpcshell')
5 files changed, 691 insertions, 0 deletions
diff --git a/comm/mail/components/accountcreation/test/xpcshell/data/example.com.xml b/comm/mail/components/accountcreation/test/xpcshell/data/example.com.xml new file mode 100644 index 0000000000..871ce732e2 --- /dev/null +++ b/comm/mail/components/accountcreation/test/xpcshell/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> diff --git a/comm/mail/components/accountcreation/test/xpcshell/test_autoconfigFetchDisk.js b/comm/mail/components/accountcreation/test/xpcshell/test_autoconfigFetchDisk.js new file mode 100644 index 0000000000..5b57a42ebb --- /dev/null +++ b/comm/mail/components/accountcreation/test/xpcshell/test_autoconfigFetchDisk.js @@ -0,0 +1,76 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* 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/. */ + +/** + * Tests getting a configuration file from the local isp directory and + * reading that file. + */ + +// Globals + +var { AccountConfig } = ChromeUtils.import( + "resource:///modules/accountcreation/AccountConfig.jsm" +); +var { FetchConfig } = ChromeUtils.import( + "resource:///modules/accountcreation/FetchConfig.jsm" +); + +var kXMLFile = "example.com.xml"; +var fetchConfigAbortable; +var copyLocation; + +function onTestSuccess(config) { + // Check that we got the expected config. + AccountConfig.replaceVariables( + config, + "Yamato Nadeshiko", + "yamato.nadeshiko@example.com", + "abc12345" + ); + + Assert.equal(config.incoming.username, "yamato.nadeshiko"); + Assert.equal(config.outgoing.username, "yamato.nadeshiko@example.com"); + Assert.equal(config.incoming.hostname, "pop.example.com"); + Assert.equal(config.outgoing.hostname, "smtp.example.com"); + Assert.equal(config.identity.realname, "Yamato Nadeshiko"); + Assert.equal(config.identity.emailAddress, "yamato.nadeshiko@example.com"); + + Assert.equal(config.subSource, "xml-from-disk"); + + do_test_finished(); +} + +function onTestFailure(e) { + do_throw(e); +} + +function run_test() { + registerCleanupFunction(finish_test); + + // 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 = FetchConfig.fromDisk( + "example.com", + onTestSuccess, + onTestFailure + ); +} + +function finish_test() { + // Remove the test config file + copyLocation.append(kXMLFile); + copyLocation.remove(false); +} diff --git a/comm/mail/components/accountcreation/test/xpcshell/test_autoconfigUtils.js b/comm/mail/components/accountcreation/test/xpcshell/test_autoconfigUtils.js new file mode 100644 index 0000000000..763084f750 --- /dev/null +++ b/comm/mail/components/accountcreation/test/xpcshell/test_autoconfigUtils.js @@ -0,0 +1,319 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* 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/. */ + +/* + * Tests for GuessConfig.jsm + * + * Currently tested: + * - getHostEntry function. + * - getIncomingTryOrder function. + * - getOutgoingTryOrder function. + * + * TODO: + * - Test the returned CMDS. + * - Figure out what else to test. + */ + +// Globals + +var { GuessConfig } = ChromeUtils.import( + "resource:///modules/accountcreation/GuessConfig.jsm" +); + +var { + UNKNOWN, + IMAP, + POP, + SMTP, + NONE, + STARTTLS, + SSL, + getHostEntry, + getIncomingTryOrder, + getOutgoingTryOrder, +} = GuessConfig; + +/* + * UTILITIES + */ + +function assert_equal(aA, aB, aWhy) { + if (aA != aB) { + do_throw(aWhy); + } + Assert.equal(aA, aB); +} + +/** + * Test that two host entries are the same, ignoring the commands. + */ +function assert_equal_host_entries(hostEntry, expected) { + assert_equal(hostEntry.protocol, expected[0], "Protocols are different"); + assert_equal(hostEntry.socketType, expected[1], "SSL values are different"); + assert_equal(hostEntry.port, expected[2], "Port values are different"); +} + +/** + * Assert that the list of tryOrders are the same. + */ +function assert_equal_try_orders(aA, aB) { + assert_equal(aA.length, aB.length, "tryOrders have different length"); + for (let [i, subA] of aA.entries()) { + let subB = aB[i]; + assert_equal_host_entries(subA, subB); + } +} + +/** + * Check that the POP calculations are correct for a given host and + * protocol. + */ +function checkPop(host, protocol) { + // The list of protocol+ssl+port configurations should match + // getIncomingTryOrder() in guessConfig.js. + + // port == UNKNOWN + // [POP, STARTTLS, 110], [POP, SSL, 995], [POP, NONE, 110] + // port != UNKNOWN + // ssl == UNKNOWN + // [POP, STARTTLS, port], [POP, SSL, port], [POP, NONE, port] + // ssl != UNKNOWN + // [POP, ssl, port] + let ssl = UNKNOWN; + let port = UNKNOWN; + let tryOrder = getIncomingTryOrder(host, protocol, ssl, port); + assert_equal_try_orders(tryOrder, [ + [POP, STARTTLS, 110], + [POP, SSL, 995], + [POP, NONE, 110], + ]); + + ssl = STARTTLS; + tryOrder = getIncomingTryOrder(host, protocol, ssl, port); + assert_equal_try_orders(tryOrder, [[POP, ssl, 110]]); + + ssl = SSL; + tryOrder = getIncomingTryOrder(host, protocol, ssl, port); + assert_equal_try_orders(tryOrder, [[POP, ssl, 995]]); + + ssl = NONE; + tryOrder = getIncomingTryOrder(host, protocol, ssl, port); + assert_equal_try_orders(tryOrder, [[POP, ssl, 110]]); + + ssl = UNKNOWN; + port = 31337; + tryOrder = getIncomingTryOrder(host, protocol, ssl, port); + assert_equal_try_orders(tryOrder, [ + [POP, STARTTLS, port], + [POP, SSL, port], + [POP, NONE, port], + ]); + + for (ssl in [STARTTLS, SSL, NONE]) { + tryOrder = getIncomingTryOrder(host, protocol, ssl, port); + assert_equal_try_orders(tryOrder, [[POP, ssl, port]]); + } +} + +/** + * Check that the IMAP calculations are correct for a given host and + * protocol. + */ +function checkImap(host, protocol) { + // The list of protocol+ssl+port configurations should match + // getIncomingTryOrder() in guessConfig.js. + + // port == UNKNOWN + // [IMAP, STARTTLS, 143], [IMAP, SSL, 993], [IMAP, NONE, 143] + // port != UNKNOWN + // ssl == UNKNOWN + // [IMAP, STARTTLS, port], [IMAP, SSL, port], [IMAP, NONE, port] + // ssl != UNKNOWN + // [IMAP, ssl, port]; + + let ssl = UNKNOWN; + let port = UNKNOWN; + let tryOrder = getIncomingTryOrder(host, protocol, ssl, port); + assert_equal_try_orders(tryOrder, [ + [IMAP, STARTTLS, 143], + [IMAP, SSL, 993], + [IMAP, NONE, 143], + ]); + + ssl = STARTTLS; + tryOrder = getIncomingTryOrder(host, protocol, ssl, port); + assert_equal_try_orders(tryOrder, [[IMAP, ssl, 143]]); + + ssl = SSL; + tryOrder = getIncomingTryOrder(host, protocol, ssl, port); + assert_equal_try_orders(tryOrder, [[IMAP, ssl, 993]]); + + ssl = NONE; + tryOrder = getIncomingTryOrder(host, protocol, ssl, port); + assert_equal_try_orders(tryOrder, [[IMAP, ssl, 143]]); + + ssl = UNKNOWN; + port = 31337; + tryOrder = getIncomingTryOrder(host, protocol, ssl, port); + assert_equal_try_orders(tryOrder, [ + [IMAP, STARTTLS, port], + [IMAP, SSL, port], + [IMAP, NONE, port], + ]); + + for (ssl in [STARTTLS, SSL, NONE]) { + tryOrder = getIncomingTryOrder(host, protocol, ssl, port); + assert_equal_try_orders(tryOrder, [[IMAP, ssl, port]]); + } +} + +/* + * TESTS + */ + +/** + * Test that getHostEntry returns the correct port numbers. + * + * TODO: + * - Test the returned commands as well. + */ +function test_getHostEntry() { + // IMAP port numbers. + assert_equal_host_entries(getHostEntry(IMAP, STARTTLS, UNKNOWN), [ + IMAP, + STARTTLS, + 143, + ]); + assert_equal_host_entries(getHostEntry(IMAP, SSL, UNKNOWN), [IMAP, SSL, 993]); + assert_equal_host_entries(getHostEntry(IMAP, NONE, UNKNOWN), [ + IMAP, + NONE, + 143, + ]); + + // POP port numbers. + assert_equal_host_entries(getHostEntry(POP, STARTTLS, UNKNOWN), [ + POP, + STARTTLS, + 110, + ]); + assert_equal_host_entries(getHostEntry(POP, SSL, UNKNOWN), [POP, SSL, 995]); + assert_equal_host_entries(getHostEntry(POP, NONE, UNKNOWN), [POP, NONE, 110]); + + // SMTP port numbers. + assert_equal_host_entries(getHostEntry(SMTP, STARTTLS, UNKNOWN), [ + SMTP, + STARTTLS, + 587, + ]); + assert_equal_host_entries(getHostEntry(SMTP, SSL, UNKNOWN), [SMTP, SSL, 465]); + assert_equal_host_entries(getHostEntry(SMTP, NONE, UNKNOWN), [ + SMTP, + NONE, + 587, + ]); +} + +/** + * Test the getIncomingTryOrder method. + */ +function test_getIncomingTryOrder() { + // The list of protocol+ssl+port configurations should match + // getIncomingTryOrder() in guessConfig.js. + + // protocol == POP || host starts with pop. || host starts with pop3. + checkPop("example.com", POP); + checkPop("pop.example.com", UNKNOWN); + checkPop("pop3.example.com", UNKNOWN); + checkPop("imap.example.com", POP); + + // protocol == IMAP || host starts with imap. + checkImap("example.com", IMAP); + checkImap("imap.example.com", UNKNOWN); + checkImap("pop.example.com", IMAP); + + let domain = "example.com"; + let protocol = UNKNOWN; + let ssl = UNKNOWN; + let port = UNKNOWN; + let tryOrder = getIncomingTryOrder(domain, protocol, ssl, port); + assert_equal_try_orders(tryOrder, [ + [IMAP, STARTTLS, 143], + [IMAP, SSL, 993], + [POP, STARTTLS, 110], + [POP, SSL, 995], + [IMAP, NONE, 143], + [POP, NONE, 110], + ]); + + ssl = SSL; + tryOrder = getIncomingTryOrder(domain, protocol, ssl, port); + assert_equal_try_orders(tryOrder, [ + [IMAP, SSL, 993], + [POP, SSL, 995], + ]); + + ssl = UNKNOWN; + port = 31337; + tryOrder = getIncomingTryOrder(domain, protocol, ssl, port); + assert_equal_try_orders(tryOrder, [ + [IMAP, STARTTLS, port], + [IMAP, SSL, port], + [POP, STARTTLS, port], + [POP, SSL, port], + [IMAP, NONE, port], + [POP, NONE, port], + ]); + + ssl = SSL; + tryOrder = getIncomingTryOrder(domain, protocol, ssl, port); + assert_equal_try_orders(tryOrder, [ + [IMAP, SSL, port], + [POP, SSL, port], + ]); +} + +/** + * Test the getOutgoingTryOrder method. + */ +function test_getOutgoingTryOrder() { + // The list of protocol+ssl+port configurations should match + // getOutgoingTryOrder() in guessConfig.js. + let domain = "example.com"; + let protocol = SMTP; + let ssl = UNKNOWN; + let port = UNKNOWN; + let tryOrder = getOutgoingTryOrder(domain, protocol, ssl, port); + assert_equal_try_orders(tryOrder, [ + [SMTP, STARTTLS, 587], + [SMTP, STARTTLS, 25], + [SMTP, SSL, 465], + [SMTP, NONE, 587], + [SMTP, NONE, 25], + ]); + + ssl = SSL; + tryOrder = getOutgoingTryOrder(domain, protocol, ssl, port); + assert_equal_try_orders(tryOrder, [[SMTP, SSL, 465]]); + + ssl = UNKNOWN; + port = 31337; + tryOrder = getOutgoingTryOrder(domain, protocol, ssl, port); + assert_equal_try_orders(tryOrder, [ + [SMTP, STARTTLS, port], + [SMTP, SSL, port], + [SMTP, NONE, port], + ]); + + ssl = SSL; + tryOrder = getOutgoingTryOrder(domain, protocol, ssl, port); + assert_equal_try_orders(tryOrder, [[SMTP, SSL, port]]); +} + +function run_test() { + test_getHostEntry(); + test_getIncomingTryOrder(); + test_getOutgoingTryOrder(); +} diff --git a/comm/mail/components/accountcreation/test/xpcshell/test_autoconfigXML.js b/comm/mail/components/accountcreation/test/xpcshell/test_autoconfigXML.js new file mode 100644 index 0000000000..919b0ffc2f --- /dev/null +++ b/comm/mail/components/accountcreation/test/xpcshell/test_autoconfigXML.js @@ -0,0 +1,266 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* 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/. */ + +/** + * Tests accountcreation/readFromXML.js , reading the XML files + * containing a mail configuration. + * + * To allow forwards-compatibility (add new stuff in the future without + * breaking old clients on the new files), we are now fairly tolerant when + * reading and allow fallback mechanisms. This test checks whether that works, + * and of course also whether we can read a normal config and get the proper + * values. + */ + +// Globals + +var { AccountConfig } = ChromeUtils.import( + "resource:///modules/accountcreation/AccountConfig.jsm" +); +var { readFromXML } = ChromeUtils.import( + "resource:///modules/accountcreation/readFromXML.jsm" +); + +var { JXON } = ChromeUtils.import("resource:///modules/JXON.jsm"); + +/* + * UTILITIES + */ + +function assert_equal(aA, aB, aWhy) { + if (aA != aB) { + do_throw(aWhy); + } + Assert.equal(aA, aB); +} + +/** + * Test that two config entries are the same. + */ +function assert_equal_config(aA, aB, field) { + assert_equal(aA, aB, "Configured " + field + " is incorrect."); +} + +/* + * TESTS + */ + +/** + * Test that the xml reader returns a proper config and + * is also forwards-compatible to new additions to the data format. + */ +function test_readFromXML_config1() { + var clientConfigXML = + "<clientConfig>" + + '<emailProvider id="example.com">' + + "<domain>example.com</domain>" + + "<domain>example.net</domain>" + + "<displayName>Example</displayName>" + + "<displayShortName>Example Mail</displayShortName>" + + // 1. - protocol not supported + '<incomingServer type="imap5">' + + "<hostname>badprotocol.example.com</hostname>" + + "<port>993</port>" + + "<socketType>SSL</socketType>" + + "<username>%EMAILLOCALPART%</username>" + + "<authentication>ssl-client-cert</authentication>" + + "</incomingServer>" + + // 2. - socket type not supported + '<incomingServer type="imap">' + + "<hostname>badsocket.example.com</hostname>" + + "<port>993</port>" + + "<socketType>key-from-DNSSEC</socketType>" + + "<username>%EMAILLOCALPART%</username>" + + "<authentication>password-cleartext</authentication>" + + "</incomingServer>" + + // 3. - first supported incoming server + '<incomingServer type="imap">' + + "<hostname>imapmail.example.com</hostname>" + + "<port>993</port>" + + "<socketType>SSL</socketType>" + + "<username>%EMAILLOCALPART%</username>" + + "<authentication>password-cleartext</authentication>" + + "</incomingServer>" + + // 4. - auth method not supported + '<incomingServer type="imap">' + + "<hostname>badauth.example.com</hostname>" + + "<port>993</port>" + + "<socketType>SSL</socketType>" + + "<username>%EMAILLOCALPART%</username>" + + "<authentication>ssl-client-cert</authentication>" + + // Throw in some elements we don"t support yet + "<imap>" + + '<rootFolder path="INBOX."/>' + + '<specialFolder id="sent" path="INBOX.Sent Mail"/>' + + "</imap>" + + "</incomingServer>" + + // 5. - second supported incoming server + '<incomingServer type="pop3">' + + "<hostname>popmail.example.com</hostname>" + + // alternative hostname, not yet supported, should be ignored + "<hostname>popbackup.example.com</hostname>" + + "<port>110</port>" + + "<port>7878</port>" + + // unsupported socket type + "<socketType>GSSAPI2</socketType>" + + // but fall back + "<socketType>plain</socketType>" + + "<username>%EMAILLOCALPART%</username>" + + "<username>%EMAILADDRESS%</username>" + + // unsupported auth method + "<authentication>GSSAPI2</authentication>" + + // but fall back + "<authentication>password-encrypted</authentication>" + + "<pop3>" + + "<leaveMessagesOnServer>true</leaveMessagesOnServer>" + + "<daysToLeaveMessagesOnServer>999</daysToLeaveMessagesOnServer>" + + "</pop3>" + + "</incomingServer>" + + // outgoing server with invalid auth method + '<outgoingServer type="smtp">' + + "<hostname>badauth.example.com</hostname>" + + "<port>587</port>" + + "<socketType>STARTTLS</socketType>" + + "<username>%EMAILADDRESS%</username>" + + "<authentication>smtp-after-imap</authentication>" + + "</outgoingServer>" + + // outgoing server - supported + '<outgoingServer type="smtp">' + + "<hostname>smtpout.example.com</hostname>" + + "<hostname>smtpfallback.example.com</hostname>" + + "<port>587</port>" + + "<port>7878</port>" + + "<socketType>GSSAPI2</socketType>" + + "<socketType>STARTTLS</socketType>" + + "<username>%EMAILADDRESS%</username>" + + "<username>%EMAILLOCALPART%</username>" + + "<authentication>GSSAPI2</authentication>" + + "<authentication>client-IP-address</authentication>" + + "<smtp/>" + + "</outgoingServer>" + + // Throw in some more elements we don"t support yet + '<enableURL url="http://foobar"/>' + + '<instructionsURL url="http://foobar"/>' + + "</emailProvider>" + + "</clientConfig>"; + + var domParser = new DOMParser(); + var config = readFromXML( + JXON.build(domParser.parseFromString(clientConfigXML, "text/xml")) + ); + + Assert.equal(config instanceof AccountConfig, true); + Assert.equal("example.com", config.id); + Assert.equal("Example", config.displayName); + Assert.notEqual(-1, config.domains.indexOf("example.com")); + // 1. incoming server skipped because of an unsupported protocol + // 2. incoming server skipped because of an so-far unknown auth method + // 3. incoming server is fine for us: IMAP, SSL, cleartext password + let server = config.incoming; + Assert.equal("imapmail.example.com", server.hostname); + Assert.equal("imap", server.type); + Assert.equal(Ci.nsMsgSocketType.SSL, server.socketType); + Assert.equal(3, server.auth); // cleartext password + // only one more supported incoming server + Assert.equal(1, config.incomingAlternatives.length); + // 4. incoming server skipped because of an so-far unknown socketType + // 5. server: POP + server = config.incomingAlternatives[0]; + Assert.equal("popmail.example.com", server.hostname); + Assert.equal("pop3", server.type); + Assert.equal(Ci.nsMsgSocketType.plain, server.socketType); + Assert.equal(4, server.auth); // encrypted password + + // SMTP server, most preferred + server = config.outgoing; + Assert.equal("smtpout.example.com", server.hostname); + Assert.equal("smtp", server.type); + Assert.equal(Ci.nsMsgSocketType.alwaysSTARTTLS, server.socketType); + Assert.equal(1, server.auth); // no auth + // no other SMTP servers + Assert.equal(0, config.outgoingAlternatives.length); +} + +/** + * Test the replaceVariables method. + */ +function test_replaceVariables() { + var clientConfigXML = + "<clientConfig>" + + '<emailProvider id="example.com">' + + "<domain>example.com</domain>" + + "<displayName>example.com</displayName>" + + "<displayShortName>example.com</displayShortName>" + + '<incomingServer type="pop3">' + + "<hostname>pop.%EMAILDOMAIN%</hostname>" + + "<port>995</port>" + + "<socketType>SSL</socketType>" + + "<username>%EMAILLOCALPART%</username>" + + "<authentication>plain</authentication>" + + "<pop3>" + + "<leaveMessagesOnServer>true</leaveMessagesOnServer>" + + "<daysToLeaveMessagesOnServer>999</daysToLeaveMessagesOnServer>" + + "</pop3>" + + "</incomingServer>" + + '<outgoingServer type="smtp">' + + "<hostname>smtp.example.com</hostname>" + + "<port>587</port>" + + "<socketType>STARTTLS</socketType>" + + "<username>%EMAILADDRESS%</username>" + + "<authentication>plain</authentication>" + + "<addThisServer>true</addThisServer>" + + "<useGlobalPreferredServer>false</useGlobalPreferredServer>" + + "</outgoingServer>" + + "</emailProvider>" + + "</clientConfig>"; + + var domParser = new DOMParser(); + var config = readFromXML( + JXON.build(domParser.parseFromString(clientConfigXML, "text/xml")) + ); + + AccountConfig.replaceVariables( + config, + "Yamato Nadeshiko", + "yamato.nadeshiko@example.com", + "abc12345" + ); + + assert_equal_config( + config.incoming.username, + "yamato.nadeshiko", + "incoming server username" + ); + assert_equal_config( + config.outgoing.username, + "yamato.nadeshiko@example.com", + "outgoing server username" + ); + assert_equal_config( + config.incoming.hostname, + "pop.example.com", + "incoming server hostname" + ); + assert_equal_config( + config.outgoing.hostname, + "smtp.example.com", + "outgoing server hostname" + ); + assert_equal_config( + config.identity.realname, + "Yamato Nadeshiko", + "user real name" + ); + assert_equal_config( + config.identity.emailAddress, + "yamato.nadeshiko@example.com", + "user email address" + ); +} + +function run_test() { + test_readFromXML_config1(); + test_replaceVariables(); +} diff --git a/comm/mail/components/accountcreation/test/xpcshell/xpcshell.ini b/comm/mail/components/accountcreation/test/xpcshell/xpcshell.ini new file mode 100644 index 0000000000..8d42ab2145 --- /dev/null +++ b/comm/mail/components/accountcreation/test/xpcshell/xpcshell.ini @@ -0,0 +1,9 @@ +[DEFAULT] +head = +tail = +support-files = data/* + +[test_autoconfigFetchDisk.js] +skip-if = os == 'win' && msix # MSIX cannot write to application directory +[test_autoconfigUtils.js] +[test_autoconfigXML.js] |