summaryrefslogtreecommitdiffstats
path: root/comm/mail/components/accountcreation/test/xpcshell/test_autoconfigUtils.js
blob: 763084f7505ceeedfe1abf47cf10f89f9aa6cff2 (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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/* -*- 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 for GuessConfig.jsm
 *
 * Currently tested:
 * - getHostEntry function.
 * - getIncomingTryOrder function.
 * - getOutgoingTryOrder function.
 *
 * TODO:
 * - Test the returned CMDS.
 * - Figure out what else to test.
 */

// Globals

var { GuessConfig } = ChromeUtils.import(
  "resource:///modules/accountcreation/GuessConfig.jsm"
);

var {
  UNKNOWN,
  IMAP,
  POP,
  SMTP,
  NONE,
  STARTTLS,
  SSL,
  getHostEntry,
  getIncomingTryOrder,
  getOutgoingTryOrder,
} = GuessConfig;

/*
 * UTILITIES
 */

function assert_equal(aA, aB, aWhy) {
  if (aA != aB) {
    do_throw(aWhy);
  }
  Assert.equal(aA, aB);
}

/**
 * Test that two host entries are the same, ignoring the commands.
 */
function assert_equal_host_entries(hostEntry, expected) {
  assert_equal(hostEntry.protocol, expected[0], "Protocols are different");
  assert_equal(hostEntry.socketType, expected[1], "SSL values are different");
  assert_equal(hostEntry.port, expected[2], "Port values are different");
}

/**
 * Assert that the list of tryOrders are the same.
 */
function assert_equal_try_orders(aA, aB) {
  assert_equal(aA.length, aB.length, "tryOrders have different length");
  for (let [i, subA] of aA.entries()) {
    let subB = aB[i];
    assert_equal_host_entries(subA, subB);
  }
}

/**
 * Check that the POP calculations are correct for a given host and
 * protocol.
 */
function checkPop(host, protocol) {
  // The list of protocol+ssl+port configurations should match
  // getIncomingTryOrder() in guessConfig.js.

  // port == UNKNOWN
  // [POP, STARTTLS, 110], [POP, SSL, 995], [POP, NONE, 110]
  // port != UNKNOWN
  // ssl == UNKNOWN
  // [POP, STARTTLS, port], [POP, SSL, port], [POP, NONE, port]
  // ssl != UNKNOWN
  // [POP, ssl, port]
  let ssl = UNKNOWN;
  let port = UNKNOWN;
  let tryOrder = getIncomingTryOrder(host, protocol, ssl, port);
  assert_equal_try_orders(tryOrder, [
    [POP, STARTTLS, 110],
    [POP, SSL, 995],
    [POP, NONE, 110],
  ]);

  ssl = STARTTLS;
  tryOrder = getIncomingTryOrder(host, protocol, ssl, port);
  assert_equal_try_orders(tryOrder, [[POP, ssl, 110]]);

  ssl = SSL;
  tryOrder = getIncomingTryOrder(host, protocol, ssl, port);
  assert_equal_try_orders(tryOrder, [[POP, ssl, 995]]);

  ssl = NONE;
  tryOrder = getIncomingTryOrder(host, protocol, ssl, port);
  assert_equal_try_orders(tryOrder, [[POP, ssl, 110]]);

  ssl = UNKNOWN;
  port = 31337;
  tryOrder = getIncomingTryOrder(host, protocol, ssl, port);
  assert_equal_try_orders(tryOrder, [
    [POP, STARTTLS, port],
    [POP, SSL, port],
    [POP, NONE, port],
  ]);

  for (ssl in [STARTTLS, SSL, NONE]) {
    tryOrder = getIncomingTryOrder(host, protocol, ssl, port);
    assert_equal_try_orders(tryOrder, [[POP, ssl, port]]);
  }
}

/**
 * Check that the IMAP calculations are correct for a given host and
 * protocol.
 */
function checkImap(host, protocol) {
  // The list of protocol+ssl+port configurations should match
  // getIncomingTryOrder() in guessConfig.js.

  // port == UNKNOWN
  // [IMAP, STARTTLS, 143], [IMAP, SSL, 993], [IMAP, NONE, 143]
  // port != UNKNOWN
  // ssl == UNKNOWN
  // [IMAP, STARTTLS, port], [IMAP, SSL, port], [IMAP, NONE, port]
  // ssl != UNKNOWN
  // [IMAP, ssl, port];

  let ssl = UNKNOWN;
  let port = UNKNOWN;
  let tryOrder = getIncomingTryOrder(host, protocol, ssl, port);
  assert_equal_try_orders(tryOrder, [
    [IMAP, STARTTLS, 143],
    [IMAP, SSL, 993],
    [IMAP, NONE, 143],
  ]);

  ssl = STARTTLS;
  tryOrder = getIncomingTryOrder(host, protocol, ssl, port);
  assert_equal_try_orders(tryOrder, [[IMAP, ssl, 143]]);

  ssl = SSL;
  tryOrder = getIncomingTryOrder(host, protocol, ssl, port);
  assert_equal_try_orders(tryOrder, [[IMAP, ssl, 993]]);

  ssl = NONE;
  tryOrder = getIncomingTryOrder(host, protocol, ssl, port);
  assert_equal_try_orders(tryOrder, [[IMAP, ssl, 143]]);

  ssl = UNKNOWN;
  port = 31337;
  tryOrder = getIncomingTryOrder(host, protocol, ssl, port);
  assert_equal_try_orders(tryOrder, [
    [IMAP, STARTTLS, port],
    [IMAP, SSL, port],
    [IMAP, NONE, port],
  ]);

  for (ssl in [STARTTLS, SSL, NONE]) {
    tryOrder = getIncomingTryOrder(host, protocol, ssl, port);
    assert_equal_try_orders(tryOrder, [[IMAP, ssl, port]]);
  }
}

/*
 * TESTS
 */

/**
 * Test that getHostEntry returns the correct port numbers.
 *
 * TODO:
 * - Test the returned commands as well.
 */
function test_getHostEntry() {
  // IMAP port numbers.
  assert_equal_host_entries(getHostEntry(IMAP, STARTTLS, UNKNOWN), [
    IMAP,
    STARTTLS,
    143,
  ]);
  assert_equal_host_entries(getHostEntry(IMAP, SSL, UNKNOWN), [IMAP, SSL, 993]);
  assert_equal_host_entries(getHostEntry(IMAP, NONE, UNKNOWN), [
    IMAP,
    NONE,
    143,
  ]);

  // POP port numbers.
  assert_equal_host_entries(getHostEntry(POP, STARTTLS, UNKNOWN), [
    POP,
    STARTTLS,
    110,
  ]);
  assert_equal_host_entries(getHostEntry(POP, SSL, UNKNOWN), [POP, SSL, 995]);
  assert_equal_host_entries(getHostEntry(POP, NONE, UNKNOWN), [POP, NONE, 110]);

  // SMTP port numbers.
  assert_equal_host_entries(getHostEntry(SMTP, STARTTLS, UNKNOWN), [
    SMTP,
    STARTTLS,
    587,
  ]);
  assert_equal_host_entries(getHostEntry(SMTP, SSL, UNKNOWN), [SMTP, SSL, 465]);
  assert_equal_host_entries(getHostEntry(SMTP, NONE, UNKNOWN), [
    SMTP,
    NONE,
    587,
  ]);
}

/**
 * Test the getIncomingTryOrder method.
 */
function test_getIncomingTryOrder() {
  // The list of protocol+ssl+port configurations should match
  // getIncomingTryOrder() in guessConfig.js.

  // protocol == POP || host starts with pop. || host starts with pop3.
  checkPop("example.com", POP);
  checkPop("pop.example.com", UNKNOWN);
  checkPop("pop3.example.com", UNKNOWN);
  checkPop("imap.example.com", POP);

  // protocol == IMAP || host starts with imap.
  checkImap("example.com", IMAP);
  checkImap("imap.example.com", UNKNOWN);
  checkImap("pop.example.com", IMAP);

  let domain = "example.com";
  let protocol = UNKNOWN;
  let ssl = UNKNOWN;
  let port = UNKNOWN;
  let tryOrder = getIncomingTryOrder(domain, protocol, ssl, port);
  assert_equal_try_orders(tryOrder, [
    [IMAP, STARTTLS, 143],
    [IMAP, SSL, 993],
    [POP, STARTTLS, 110],
    [POP, SSL, 995],
    [IMAP, NONE, 143],
    [POP, NONE, 110],
  ]);

  ssl = SSL;
  tryOrder = getIncomingTryOrder(domain, protocol, ssl, port);
  assert_equal_try_orders(tryOrder, [
    [IMAP, SSL, 993],
    [POP, SSL, 995],
  ]);

  ssl = UNKNOWN;
  port = 31337;
  tryOrder = getIncomingTryOrder(domain, protocol, ssl, port);
  assert_equal_try_orders(tryOrder, [
    [IMAP, STARTTLS, port],
    [IMAP, SSL, port],
    [POP, STARTTLS, port],
    [POP, SSL, port],
    [IMAP, NONE, port],
    [POP, NONE, port],
  ]);

  ssl = SSL;
  tryOrder = getIncomingTryOrder(domain, protocol, ssl, port);
  assert_equal_try_orders(tryOrder, [
    [IMAP, SSL, port],
    [POP, SSL, port],
  ]);
}

/**
 * Test the getOutgoingTryOrder method.
 */
function test_getOutgoingTryOrder() {
  // The list of protocol+ssl+port configurations should match
  // getOutgoingTryOrder() in guessConfig.js.
  let domain = "example.com";
  let protocol = SMTP;
  let ssl = UNKNOWN;
  let port = UNKNOWN;
  let tryOrder = getOutgoingTryOrder(domain, protocol, ssl, port);
  assert_equal_try_orders(tryOrder, [
    [SMTP, STARTTLS, 587],
    [SMTP, STARTTLS, 25],
    [SMTP, SSL, 465],
    [SMTP, NONE, 587],
    [SMTP, NONE, 25],
  ]);

  ssl = SSL;
  tryOrder = getOutgoingTryOrder(domain, protocol, ssl, port);
  assert_equal_try_orders(tryOrder, [[SMTP, SSL, 465]]);

  ssl = UNKNOWN;
  port = 31337;
  tryOrder = getOutgoingTryOrder(domain, protocol, ssl, port);
  assert_equal_try_orders(tryOrder, [
    [SMTP, STARTTLS, port],
    [SMTP, SSL, port],
    [SMTP, NONE, port],
  ]);

  ssl = SSL;
  tryOrder = getOutgoingTryOrder(domain, protocol, ssl, port);
  assert_equal_try_orders(tryOrder, [[SMTP, SSL, port]]);
}

function run_test() {
  test_getHostEntry();
  test_getIncomingTryOrder();
  test_getOutgoingTryOrder();
}