summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/local/test/unit/test_pop3GSSAPIFail.js
blob: 2ff133a14e2513878cfdeddeba6cf3daef4c4a5e (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
216
217
218
219
220
221
222
/* -*- Mode: JavaScript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/**
 * A server offers GSSAPI (Kerberos), but auth fails, due to client or server.
 *
 * This mainly tests whether we use the correct login mode.
 *
 * Whether it fails due to
 * - client not set up
 * - client ticket expired / not logged in
 * - server not being set up properly
 * makes no difference to Thunderbird, as that's all hidden in the gssapi-Library
 * from the OS. So, the server here just returning err is a good approximation
 * of reality of the above cases.
 *
 * Actually, we (more precisely the OS GSSAPI lib) fail out of band
 * in the Kerberos protocol, before the AUTH GSSAPI command is even issued.
 *
 * @author Ben Bucksch
 */

var server;
var daemon;
var authSchemes;
var incomingServer;
var thisTest;

var tests = [
  {
    title: "GSSAPI auth, server with GSSAPI only",
    clientAuthMethod: Ci.nsMsgAuthMethod.GSSAPI,
    serverAuthMethods: ["GSSAPI"],
    expectSuccess: false,
    transaction: ["AUTH", "CAPA"],
  },
  {
    // First GSSAPI step happens and fails out of band, thus no "AUTH GSSAPI"
    title: "GSSAPI auth, server with GSSAPI and CRAM-MD5",
    clientAuthMethod: Ci.nsMsgAuthMethod.GSSAPI,
    serverAuthMethods: ["GSSAPI", "CRAM-MD5"],
    expectSuccess: false,
    transaction: ["AUTH", "CAPA"],
  },
  {
    title: "Any secure auth, server with GSSAPI only",
    clientAuthMethod: Ci.nsMsgAuthMethod.secure,
    serverAuthMethods: ["GSSAPI"],
    expectSuccess: false,
    transaction: ["AUTH", "CAPA"],
  },
  {
    title: "Any secure auth, server with GSSAPI and CRAM-MD5",
    clientAuthMethod: Ci.nsMsgAuthMethod.secure,
    serverAuthMethods: ["GSSAPI", "CRAM-MD5"],
    expectSuccess: true,
    transaction: ["AUTH", "CAPA", "AUTH CRAM-MD5", "STAT"],
  },
  {
    title: "Encrypted password, server with GSSAPI and CRAM-MD5",
    clientAuthMethod: Ci.nsMsgAuthMethod.passwordEncrypted,
    serverAuthMethods: ["GSSAPI", "CRAM-MD5"],
    expectSuccess: true,
    transaction: ["AUTH", "CAPA", "AUTH CRAM-MD5", "STAT"],
  },
];

var urlListener = {
  OnStartRunningUrl(url) {},
  OnStopRunningUrl(url, result) {
    try {
      if (thisTest.expectSuccess) {
        Assert.equal(result, 0);
      } else {
        Assert.notEqual(result, 0);
      }

      var transaction = server.playTransaction();
      do_check_transaction(transaction, thisTest.transaction);

      do_timeout(0, checkBusy);
    } catch (e) {
      server.stop();
      var thread = gThreadManager.currentThread;
      while (thread.hasPendingEvents()) {
        thread.processNextEvent(true);
      }

      do_throw(e);
    }
  },
};

function checkBusy() {
  if (tests.length == 0) {
    incomingServer.closeCachedConnections();

    // No more tests, let everything finish
    server.stop();

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

    do_test_finished();
    return;
  }

  // If the server hasn't quite finished, just delay a little longer.
  if (incomingServer.serverBusy) {
    do_timeout(20, checkBusy);
    return;
  }

  testNext();
}

function testNext() {
  thisTest = tests.shift();

  // Handle the server in a try/catch/finally loop so that we always will stop
  // the server if something fails.
  try {
    server.resetTest();

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

    authSchemes = thisTest.serverAuthMethods;

    // Mailnews caches server capabilities, so try to reset it
    deletePop3Server();
    incomingServer = createPop3Server();

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

    MailServices.pop3.GetNewMail(
      null,
      urlListener,
      localAccountUtils.inboxFolder,
      incomingServer
    );
    server.performTest();
  } catch (e) {
    server.stop();
    do_throw(e);
  }
}

// <copied from="head_maillocal.js::createPop3ServerAndLocalFolders()">
function createPop3Server() {
  let incoming = MailServices.accounts.createIncomingServer(
    "fred",
    "localhost",
    "pop3"
  );
  incoming.port = server.port;
  incoming.password = "wilma";
  return incoming;
}
// </copied>

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

class GSSAPIFail_handler extends POP3_RFC5034_handler {
  _needGSSAPI = false;
  // kAuthSchemes will be set by test

  AUTH(restLine) {
    var scheme = restLine.split(" ")[0];
    if (scheme == "GSSAPI") {
      this._multiline = true;
      this._needGSSAPI = true;
      return "+";
    }
    return super.AUTH(restLine); // call parent
  }
  onMultiline(line) {
    if (this._needGSSAPI) {
      this._multiline = false;
      this._needGSSAPI = false;
      return "-ERR hm.... shall I allow you? hm... NO.";
    }

    if (super.onMultiline) {
      // Call parent.
      return super.onMultiline(line);
    }
    return undefined;
  }
}

function run_test() {
  // Disable new mail notifications
  Services.prefs.setBoolPref("mail.biff.play_sound", false);
  Services.prefs.setBoolPref("mail.biff.show_alert", false);
  Services.prefs.setBoolPref("mail.biff.show_tray_icon", false);
  Services.prefs.setBoolPref("mail.biff.animate_dock_icon", false);

  daemon = new Pop3Daemon();
  function createHandler(d) {
    var handler = new GSSAPIFail_handler(d);
    handler.kAuthSchemes = authSchemes;
    return handler;
  }
  server = new nsMailServer(createHandler, daemon);
  server.start();

  // incomingServer = createPop3ServerAndLocalFolders();
  localAccountUtils.loadLocalMailAccount();

  do_test_pending();

  testNext();
}