summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/compose/test/unit/test_smtpAuthMethods.js
blob: 24d5c6d554a20fb3fe39573666fe489f84deeb43 (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
/**
 * Authentication tests for SMTP.
 *
 * Test code <copied from="test_pop3AuthMethods.js">
 */

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

var server;
var kAuthSchemes;
var smtpServer;
var testFile;
var identity;

var kUsername = "fred";
var kPassword = "wilma";
var kIdentityMail = "identity@foo.invalid";
var kSender = "from@foo.invalid";
var kTo = "to@foo.invalid";
var MAILFROM = "MAIL FROM:<" + kSender + "> BODY=8BITMIME SIZE=159";
var RCPTTO = "RCPT TO:<" + kTo + ">";
var AUTHPLAIN = "AUTH PLAIN " + AuthPLAIN.encodeLine(kUsername, kPassword);

var tests = [
  {
    title:
      "Cleartext password, with server supporting AUTH PLAIN, LOGIN, and CRAM",
    clientAuthMethod: Ci.nsMsgAuthMethod.passwordCleartext,
    serverAuthMethods: ["PLAIN", "LOGIN", "CRAM-MD5"],
    expectSuccess: true,
    transaction: ["EHLO test", AUTHPLAIN, MAILFROM, RCPTTO, "DATA"],
  },
  {
    title: "Cleartext password, with server only supporting AUTH LOGIN",
    clientAuthMethod: Ci.nsMsgAuthMethod.passwordCleartext,
    serverAuthMethods: ["LOGIN"],
    expectSuccess: true,
    transaction: ["EHLO test", "AUTH LOGIN", MAILFROM, RCPTTO, "DATA"],
  },
  {
    title:
      "Encrypted password, with server supporting AUTH PLAIN, LOGIN and CRAM",
    clientAuthMethod: Ci.nsMsgAuthMethod.passwordEncrypted,
    serverAuthMethods: ["PLAIN", "LOGIN", "CRAM-MD5"],
    expectSuccess: true,
    transaction: ["EHLO test", "AUTH CRAM-MD5", MAILFROM, RCPTTO, "DATA"],
  },
  {
    title:
      "Encrypted password, with server only supporting AUTH PLAIN (must fail)",
    clientAuthMethod: Ci.nsMsgAuthMethod.passwordEncrypted,
    serverAuthMethods: ["PLAIN"],
    expectSuccess: false,
    transaction: ["EHLO test"],
  },
  {
    title:
      "Any secure method, with server supporting AUTH PLAIN, LOGIN and CRAM",
    clientAuthMethod: Ci.nsMsgAuthMethod.secure,
    serverAuthMethods: ["PLAIN", "LOGIN", "CRAM-MD5"],
    expectSuccess: true,
    transaction: ["EHLO test", "AUTH CRAM-MD5", MAILFROM, RCPTTO, "DATA"],
  },
  {
    title:
      "Any secure method, with server only supporting AUTH PLAIN (must fail)",
    clientAuthMethod: Ci.nsMsgAuthMethod.secure,
    serverAuthMethods: ["PLAIN"],
    expectSuccess: false,
    transaction: ["EHLO test"],
  },
];

function nextTest() {
  if (tests.length == 0) {
    // this is sync, so we run into endTest() at the end of run_test() now
    return;
  }
  server.resetTest();

  var curTest = tests.shift();
  test = curTest.title;
  dump("NEXT test: " + curTest.title + "\n");

  // Adapt to curTest
  kAuthSchemes = curTest.serverAuthMethods;
  smtpServer.authMethod = curTest.clientAuthMethod;

  // Run test
  let urlListener = new PromiseTestUtils.PromiseUrlListener();
  MailServices.smtp.sendMailMessage(
    testFile,
    kTo,
    identity,
    kSender,
    null,
    urlListener,
    null,
    null,
    false,
    "",
    {},
    {}
  );
  let resolved = false;
  urlListener.promise.catch(e => {}).finally(() => (resolved = true));
  Services.tm.spinEventLoopUntil("wait for sending", () => resolved);

  do_check_transaction(server.playTransaction(), curTest.transaction);

  smtpServer.closeCachedConnections();
  nextTest();
}

function run_test() {
  // Handle the server in a try/catch/finally loop so that we always will stop
  // the server if something fails.
  try {
    function createHandler(d) {
      var handler = new SMTP_RFC2821_handler(d);
      handler.kUsername = kUsername;
      handler.kPassword = kPassword;
      handler.kAuthRequired = true;
      handler.kAuthSchemes = kAuthSchemes;
      return handler;
    }
    server = setupServerDaemon(createHandler);
    dump("AUTH PLAIN = " + AUTHPLAIN + "\n");
    server.start();

    localAccountUtils.loadLocalMailAccount();
    smtpServer = getBasicSmtpServer(server.port);
    smtpServer.socketType = Ci.nsMsgSocketType.plain;
    smtpServer.username = kUsername;
    smtpServer.password = kPassword;
    identity = getSmtpIdentity(kIdentityMail, smtpServer);

    testFile = do_get_file("data/message1.eml");

    nextTest();
  } catch (e) {
    do_throw(e);
  } finally {
    endTest();
  }
}

function endTest() {
  dump("endTest()\n");
  server.stop();

  dump("emptying event loop\n");
  var thread = gThreadManager.currentThread;
  while (thread.hasPendingEvents()) {
    dump("next event\n");
    thread.processNextEvent(true);
  }
}