summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/base/test/unit/test_testsuite_fakeserverAuth.js
blob: 02181ae20b3bc8c373822970d279a34366be14ba (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
/**
 * Tests functions in mailnews/test/fakeserver/Auth.jsm
 * which are responsible for the authentication in the
 * fakeserver.
 *
 * Do NOT essentially re-code the auth schemes here,
 * just check roundtrips, against static values etc..
 */

var { AuthPLAIN, AuthCRAM } = ChromeUtils.import(
  "resource://testing-common/mailnews/Auth.jsm"
);

var kUsername = "fred1";
var kPassword = "wilma2";

function run_test() {
  authPLAIN();
  authCRAMMD5();
  return true;
}

/**
 * Test AUTH PLAIN
 */
function authPLAIN() {
  // roundtrip works
  var line = AuthPLAIN.encodeLine(kUsername, kPassword);
  var req = AuthPLAIN.decodeLine(line);
  Assert.equal(req.username, kUsername);
  Assert.equal(req.password, kPassword);

  // correct encoding
  Assert.equal(line, "AGZyZWQxAHdpbG1hMg==");
}

/**
 * Test AUTH CRAM-MD5
 */
function authCRAMMD5() {
  // AuthCRAM.createChallenge() creates a different challenge each time
  var hardcodedChallenge = btoa("<123@fake.invalid>");
  var hardcodedResponse =
    "ZnJlZDEgOTA5YjgwMmM3NTI5NTJlYzI2NjgyMTNmYTdjNWU0ZjQ=";

  // correct encoding
  var req = AuthCRAM.decodeLine(hardcodedResponse);
  Assert.equal(req.username, kUsername);
  var expectedDigest = AuthCRAM.encodeCRAMMD5(hardcodedChallenge, kPassword);
  Assert.equal(req.digest, expectedDigest);

  var challenge = AuthCRAM.createChallenge("fake.invalid");
  challenge = atob(challenge); // decode. function currently returns it already encoded
  var challengeSplit = challenge.split("@");
  Assert.equal(challengeSplit.length, 2);
  Assert.equal(challengeSplit[1], "fake.invalid>");
  Assert.equal(challengeSplit[0][0], "<");
}