/* -*- 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 = "" + '' + "example.com" + "example.net" + "Example" + "Example Mail" + // 1. - protocol not supported '' + "badprotocol.example.com" + "993" + "SSL" + "%EMAILLOCALPART%" + "ssl-client-cert" + "" + // 2. - socket type not supported '' + "badsocket.example.com" + "993" + "key-from-DNSSEC" + "%EMAILLOCALPART%" + "password-cleartext" + "" + // 3. - first supported incoming server '' + "imapmail.example.com" + "993" + "SSL" + "%EMAILLOCALPART%" + "password-cleartext" + "" + // 4. - auth method not supported '' + "badauth.example.com" + "993" + "SSL" + "%EMAILLOCALPART%" + "ssl-client-cert" + // Throw in some elements we don"t support yet "" + '' + '' + "" + "" + // 5. - second supported incoming server '' + "popmail.example.com" + // alternative hostname, not yet supported, should be ignored "popbackup.example.com" + "110" + "7878" + // unsupported socket type "GSSAPI2" + // but fall back "plain" + "%EMAILLOCALPART%" + "%EMAILADDRESS%" + // unsupported auth method "GSSAPI2" + // but fall back "password-encrypted" + "" + "true" + "999" + "" + "" + // outgoing server with invalid auth method '' + "badauth.example.com" + "587" + "STARTTLS" + "%EMAILADDRESS%" + "smtp-after-imap" + "" + // outgoing server - supported '' + "smtpout.example.com" + "smtpfallback.example.com" + "587" + "7878" + "GSSAPI2" + "STARTTLS" + "%EMAILADDRESS%" + "%EMAILLOCALPART%" + "GSSAPI2" + "client-IP-address" + "" + "" + // Throw in some more elements we don"t support yet '' + '' + "" + ""; 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 = "" + '' + "example.com" + "example.com" + "example.com" + '' + "pop.%EMAILDOMAIN%" + "995" + "SSL" + "%EMAILLOCALPART%" + "plain" + "" + "true" + "999" + "" + "" + '' + "smtp.example.com" + "587" + "STARTTLS" + "%EMAILADDRESS%" + "plain" + "true" + "false" + "" + "" + ""; 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(); }