summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/imap/test/unit/test_starttlsFailure.js
blob: 59c7f2e70fb37408b18ba0cf693c89319385d3fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/* 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/. */

/**
 * This test checks that we handle the server dropping the connection
 * on starttls. Since fakeserver doesn't support STARTTLS, I've made
 * it drop the connection when it's attempted.
 */

/* import-globals-from ../../../test/resources/alertTestUtils.js */
load("../../../resources/alertTestUtils.js");

var { MailServices } = ChromeUtils.import(
  "resource:///modules/MailServices.jsm"
);
var { PromiseTestUtils } = ChromeUtils.import(
  "resource://testing-common/mailnews/PromiseTestUtils.jsm"
);

var gAlertResolve;
var gGotAlert = new Promise(resolve => {
  gAlertResolve = resolve;
});

/* exported alert to alertTestUtils.js */
function alertPS(parent, aDialogTitle, aText) {
  gAlertResolve(aText);
}

add_setup(async function () {
  // Set up IMAP fakeserver and incoming server.
  IMAPPump.daemon = new ImapDaemon();
  IMAPPump.server = makeServer(IMAPPump.daemon, "", { dropOnStartTLS: true });
  IMAPPump.incomingServer = createLocalIMAPServer(IMAPPump.server.port);
  IMAPPump.incomingServer.socketType = Ci.nsMsgSocketType.alwaysSTARTTLS;

  // We need a local account for the IMAP server to have its sent messages in.
  localAccountUtils.loadLocalMailAccount();

  // We need an identity so that updateFolder doesn't fail.
  let imapAccount = MailServices.accounts.createAccount();
  let identity = MailServices.accounts.createIdentity();
  imapAccount.addIdentity(identity);
  imapAccount.defaultIdentity = identity;
  imapAccount.incomingServer = IMAPPump.incomingServer;
  MailServices.accounts.defaultAccount = imapAccount;

  // The server doesn't support more than one connection.
  Services.prefs.setIntPref("mail.server.server1.max_cached_connections", 1);
  // We aren't interested in downloading messages automatically.
  Services.prefs.setBoolPref("mail.server.server1.download_on_biff", false);

  IMAPPump.inbox = IMAPPump.incomingServer.rootFolder
    .getChildNamed("Inbox")
    .QueryInterface(Ci.nsIMsgImapMailFolder);

  registerAlertTestUtils();

  let listener = new PromiseTestUtils.PromiseUrlListener();
  IMAPPump.inbox.updateFolderWithListener(gDummyMsgWindow, listener);
  await listener.promise
    .then(res => {
      throw new Error("updateFolderWithListener has to fail");
    })
    .catch(exitCode => {
      Assert.ok(!Components.isSuccessCode(exitCode));
    });
});

add_task(async function check_alert() {
  let alertText = await gGotAlert;
  Assert.ok(alertText.startsWith("Server localhost has disconnected"));
});

add_task(function teardown() {
  IMAPPump.incomingServer.closeCachedConnections();
  IMAPPump.server.stop();
});