summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/local/test/unit/test_pop3PasswordFailure_rfc2449.js
blob: 1701f999c909f5034588597a1cd47732bc5d8209 (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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/* 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 password failure with RFC2449 auth.
 *
 * This test checks to see if the pop3 password failure is handled correctly.
 * The steps are:
 *   - Have an invalid password in the password database.
 *   - Check we get a prompt asking what to do.
 *   - Check retry does what it should do.
 *   - Check cancel does what it should do.
 *   - Re-initiate connection, this time select enter new password, check that
 *     we get a new password prompt and can enter the password.
 */

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

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

var server;
var daemon;
var incomingServer;
var attempt = 0;

var kUserName = "testpop3";
var kInvalidPassword = "pop3test";
var kValidPassword = "testpop3";

add_setup(async function () {
  // Enable debug for the sign on.
  Services.prefs.setBoolPref("signon.debug", true);

  // Prepare files for passwords (generated by a script in bug 1018624).
  await setupForPassword("signons-mailnews1.8.json");

  registerAlertTestUtils();

  // Set up the Server
  daemon = new Pop3Daemon();
  function createHandler(d) {
    var handler = new POP3_RFC2449_handler(d);
    handler.dropOnAuthFailure = true;

    // Login information needs to match the one stored in the signons json file.
    handler.kUsername = kUserName;
    handler.kPassword = kValidPassword;
    return handler;
  }
  server = new nsMailServer(createHandler, daemon);
  server.start();

  // Set up the basic accounts and folders.
  // We would use createPop3ServerAndLocalFolders() however we want to have
  // a different username and NO password for this test (as we expect to load
  // it from the signons json file in which the login information is stored).
  localAccountUtils.loadLocalMailAccount();

  incomingServer = MailServices.accounts.createIncomingServer(
    kUserName,
    "localhost",
    "pop3"
  );

  incomingServer.port = server.port;

  // Check that we haven't got any messages in the folder, if we have its a test
  // setup issue.
  Assert.equal(localAccountUtils.inboxFolder.getTotalMessages(false), 0);

  daemon.setMessages(["message1.eml"]);
});

add_task(async function getMail1() {
  // Now get the mail.
  let urlListener = new PromiseTestUtils.PromiseUrlListener();
  MailServices.pop3.GetNewMail(
    gDummyMsgWindow,
    urlListener,
    localAccountUtils.inboxFolder,
    incomingServer
  );
  server.performTest();
  await Assert.rejects(
    urlListener.promise,
    reason => {
      return reason === Cr.NS_ERROR_FAILURE;
    },
    "Check that wrong password is entered and thrown"
  );
  // We shouldn't have emails as the auth failed.
  Assert.equal(localAccountUtils.inboxFolder.getTotalMessages(false), 0);
  // Sanity check that we are at attempt 2.
  Assert.equal(attempt, 2);

  // Check that we haven't forgotten the login even though we've retried and cancelled.
  let logins = Services.logins.findLogins(
    "mailbox://localhost",
    null,
    "mailbox://localhost"
  );

  Assert.equal(logins.length, 1);
  Assert.equal(logins[0].username, kUserName);
  Assert.equal(logins[0].password, kInvalidPassword);

  server.resetTest();
});

add_task(async function getMail2() {
  // Now get the mail.
  let urlListener = new PromiseTestUtils.PromiseUrlListener();
  MailServices.pop3.GetNewMail(
    gDummyMsgWindow,
    urlListener,
    localAccountUtils.inboxFolder,
    incomingServer
  );
  server.performTest();
  await urlListener.promise;
  Assert.equal(attempt, 4);
  // On the last attempt (4th), we should have successfully got one mail.
  Assert.equal(localAccountUtils.inboxFolder.getTotalMessages(false), 1);

  // Now check the new one has been saved.
  let logins = Services.logins.findLogins(
    "mailbox://localhost",
    null,
    "mailbox://localhost"
  );

  Assert.equal(logins.length, 1);
  Assert.equal(logins[0].username, kUserName);
  Assert.equal(logins[0].password, kValidPassword);
});

add_task(function endTest() {
  // Cleanup for potential Sockets/Ports leakage.
  server.stop();
  server = null;
  daemon = null;
  incomingServer = null;
  var thread = gThreadManager.currentThread;
  while (thread.hasPendingEvents()) {
    thread.processNextEvent(true);
  }
});

function alertPS(parent, aDialogText, aText) {
  // The first few attempts may prompt about the password problem, the last
  // attempt shouldn't.
  Assert.ok(attempt < 4);

  // Log the fact we've got an alert, but we don't need to test anything here.
  info("Alert Title: " + aDialogText + "\nAlert Text: " + aText);
}

function confirmExPS(
  parent,
  aDialogTitle,
  aText,
  aButtonFlags,
  aButton0Title,
  aButton1Title,
  aButton2Title,
  aCheckMsg,
  aCheckState
) {
  switch (++attempt) {
    // First attempt, retry.
    case 1:
      info("Attempting retry");
      return 0;
    // Second attempt, cancel.
    case 2:
      info("Cancelling login attempt");
      return 1;
    // Third attempt, retry.
    case 3:
      info("Attempting Retry");
      return 0;
    // Fourth attempt, enter a new password.
    case 4:
      info("Enter new password");
      return 2;
    default:
      throw new Error("unexpected attempt number " + attempt);
  }
}

/**
 * Extension for alertTestUtils.
 * Make sure that at the 4th attempt the correct password is used.
 */
function promptPasswordPS(
  aParent,
  aDialogTitle,
  aText,
  aPassword,
  aCheckMsg,
  aCheckState
) {
  if (attempt == 4) {
    aPassword.value = kValidPassword;
    aCheckState.value = true;
    return true;
  }
  return false;
}