summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/imap/test/unit/test_imapAuthMethods.js
blob: 18e5d543962b1a50a46027ed4b8e76e4be6acbf9 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
 * Login tests for IMAP
 *
 * Test code <copied from="test_mailboxes.js">
 * and <copied from="test_pop3AuthMethods.js">
 *
 * BUGS:
 * - cleanup after each test doesn't seem to work correctly. Effects:
 *    - one more "lsub" per test, e.g. "capability", "auth...", "lsub", "lsub", "lsub", "list" in the 3. test.,
 *    - root folder check succeeds although login failed
 * - removeIncomingServer(..., true); (cleanup files) fails.
 */

var { MailServices } = ChromeUtils.import(
  "resource:///modules/MailServices.jsm"
);
/* import-globals-from ../../../test/resources/alertTestUtils.js */
load("../../../resources/alertTestUtils.js");

// const kUsername = "fred";
// const kPassword = "wilma";

var thisTest;

var tests = [
  {
    title: "Cleartext password, with server only supporting old-style login",
    clientAuthMethod: Ci.nsMsgAuthMethod.passwordCleartext,
    serverAuthMethods: [],
    expectSuccess: true,
    transaction: ["CAPABILITY", "LOGIN", "CAPABILITY", "LIST", "LSUB"],
  },
  {
    // Just to make sure we clean up properly - in the test and in TB, e.g. don't cache stuff
    title:
      "Second time Cleartext password, with server only supporting old-style login",
    clientAuthMethod: Ci.nsMsgAuthMethod.passwordCleartext,
    serverAuthMethods: [],
    expectSuccess: true,
    transaction: ["CAPABILITY", "LOGIN", "CAPABILITY", "LIST", "LSUB"],
  },
  {
    title:
      "Cleartext password, with server supporting AUTH PLAIN, LOGIN and CRAM",
    clientAuthMethod: Ci.nsMsgAuthMethod.passwordCleartext,
    serverAuthMethods: ["PLAIN", "LOGIN", "CRAM-MD5"],
    expectSuccess: true,
    transaction: [
      "CAPABILITY",
      "AUTHENTICATE PLAIN",
      "CAPABILITY",
      "LIST",
      "LSUB",
    ],
  },
  {
    title: "Cleartext password, with server supporting only AUTH LOGIN",
    clientAuthMethod: Ci.nsMsgAuthMethod.passwordCleartext,
    serverAuthMethods: ["LOGIN"],
    expectSuccess: true,
    transaction: [
      "CAPABILITY",
      "AUTHENTICATE LOGIN",
      "CAPABILITY",
      "LIST",
      "LSUB",
    ],
  },
  {
    title: "Encrypted password, with server supporting PLAIN and CRAM",
    clientAuthMethod: Ci.nsMsgAuthMethod.passwordEncrypted,
    serverAuthMethods: ["PLAIN", "LOGIN", "CRAM-MD5"],
    expectSuccess: true,
    transaction: [
      "CAPABILITY",
      "AUTHENTICATE CRAM-MD5",
      "CAPABILITY",
      "LIST",
      "LSUB",
    ],
  },
  {
    title:
      "Encrypted password, with server only supporting AUTH PLAIN and LOGIN (must fail)",
    clientAuthMethod: Ci.nsMsgAuthMethod.passwordEncrypted,
    serverAuthMethods: ["PLAIN", "LOGIN"],
    expectSuccess: false,
    transaction: ["CAPABILITY"],
  },
];

function nextTest() {
  try {
    thisTest = tests.shift();
    if (!thisTest) {
      endTest();
      return;
    }

    dump("NEXT test: " + thisTest.title + "\n");

    // (re)create fake server
    var daemon = new ImapDaemon();
    var server = makeServer(daemon, "", {
      kAuthSchemes: thisTest.serverAuthMethods,
    });
    server.setDebugLevel(fsDebugAll);

    // If Mailnews ever caches server capabilities, delete and re-create the incomingServer here
    var incomingServer = createLocalIMAPServer(server.port);

    let msgServer = incomingServer;
    msgServer.QueryInterface(Ci.nsIMsgIncomingServer);
    msgServer.authMethod = thisTest.clientAuthMethod;

    // connect
    incomingServer.performExpand(null);
    server.performTest("LSUB");

    dump("should " + (thisTest.expectSuccess ? "" : "not ") + "be logged in\n");
    Assert.equal(true, incomingServer instanceof Ci.nsIImapServerSink);
    do_check_transaction(server.playTransaction(), thisTest.transaction, false);

    do {
      incomingServer.closeCachedConnections();
    } while (incomingServer.serverBusy);
    incomingServer.shutdown();
    deleteIMAPServer(incomingServer);
    incomingServer = null;
    MailServices.accounts.closeCachedConnections();
    MailServices.accounts.shutdownServers();
    MailServices.accounts.unloadAccounts();
    server.stop();
  } catch (e) {
    // server.stop();
    // endTest();
    do_throw(e);
  }

  nextTest();
}

function deleteIMAPServer(incomingServer) {
  if (!incomingServer) {
    return;
  }
  MailServices.accounts.removeIncomingServer(incomingServer, true);
}

function run_test() {
  do_test_pending();

  registerAlertTestUtils();

  nextTest();
}

function endTest() {
  var thread = gThreadManager.currentThread;
  while (thread.hasPendingEvents()) {
    thread.processNextEvent(true);
  }

  do_test_finished();
}