|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 * http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
3 |
|
4 Cu.import("resource://services-sync/resource.js"); |
|
5 Cu.import("resource://services-sync/util.js"); |
|
6 Cu.import("resource://services-sync/service.js"); |
|
7 Cu.import("resource://testing-common/services/sync/utils.js"); |
|
8 |
|
9 const JAPANESE = "\u34ff\u35ff\u36ff\u37ff"; |
|
10 const APPLES = "\uf8ff\uf8ff\uf8ff\uf8ff"; |
|
11 const LOWBYTES = "\xff\xff\xff\xff"; |
|
12 |
|
13 // Poor man's /etc/passwd. Static since there's no btoa()/atob() in xpcshell. |
|
14 let basicauth = {}; |
|
15 basicauth[LOWBYTES] = "Basic am9obmRvZTr/////"; |
|
16 basicauth[Utils.encodeUTF8(JAPANESE)] = "Basic am9obmRvZTrjk7/jl7/jm7/jn78="; |
|
17 |
|
18 // Global var for the server password, read by info_collections(), |
|
19 // modified by change_password(). |
|
20 let server_password; |
|
21 |
|
22 function login_handling(handler) { |
|
23 return function (request, response) { |
|
24 let basic = basicauth[server_password]; |
|
25 |
|
26 if (basic && (request.getHeader("Authorization") == basic)) { |
|
27 handler(request, response); |
|
28 } else { |
|
29 let body = "Unauthorized"; |
|
30 response.setStatusLine(request.httpVersion, 401, "Unauthorized"); |
|
31 response.setHeader("WWW-Authenticate", 'Basic realm="secret"', false); |
|
32 response.bodyOutputStream.write(body, body.length); |
|
33 } |
|
34 }; |
|
35 } |
|
36 |
|
37 function change_password(request, response) { |
|
38 let body, statusCode, status; |
|
39 let basic = basicauth[server_password]; |
|
40 |
|
41 if (basic && (request.getHeader("Authorization") == basic)) { |
|
42 server_password = readBytesFromInputStream(request.bodyInputStream); |
|
43 body = ""; |
|
44 statusCode = 200; |
|
45 status = "OK"; |
|
46 } else { |
|
47 statusCode = 401; |
|
48 body = status = "Unauthorized"; |
|
49 } |
|
50 response.setStatusLine(request.httpVersion, statusCode, status); |
|
51 response.setHeader("WWW-Authenticate", 'Basic realm="secret"', false); |
|
52 response.bodyOutputStream.write(body, body.length); |
|
53 } |
|
54 |
|
55 function run_test() { |
|
56 initTestLogging("Trace"); |
|
57 let collectionsHelper = track_collections_helper(); |
|
58 let upd = collectionsHelper.with_updated_collection; |
|
59 let collections = collectionsHelper.collections; |
|
60 |
|
61 ensureLegacyIdentityManager(); |
|
62 |
|
63 do_test_pending(); |
|
64 let server = httpd_setup({ |
|
65 "/1.1/johndoe/info/collections": login_handling(collectionsHelper.handler), |
|
66 "/1.1/johndoe/storage/meta/global": upd("meta", new ServerWBO("global").handler()), |
|
67 "/1.1/johndoe/storage/crypto/keys": upd("crypto", new ServerWBO("keys").handler()), |
|
68 "/user/1.0/johndoe/password": change_password |
|
69 }); |
|
70 |
|
71 setBasicCredentials("johndoe", JAPANESE, "irrelevant"); |
|
72 Service.serverURL = server.baseURI; |
|
73 |
|
74 try { |
|
75 _("Try to log in with the password."); |
|
76 server_password = "foobar"; |
|
77 do_check_false(Service.verifyLogin()); |
|
78 do_check_eq(server_password, "foobar"); |
|
79 |
|
80 _("Make the server password the low byte version of our password."); |
|
81 server_password = LOWBYTES; |
|
82 do_check_false(Service.verifyLogin()); |
|
83 do_check_eq(server_password, LOWBYTES); |
|
84 |
|
85 _("Can't use a password that has the same low bytes as ours."); |
|
86 server_password = Utils.encodeUTF8(JAPANESE); |
|
87 Service.identity.basicPassword = APPLES; |
|
88 do_check_false(Service.verifyLogin()); |
|
89 do_check_eq(server_password, Utils.encodeUTF8(JAPANESE)); |
|
90 |
|
91 } finally { |
|
92 server.stop(do_test_finished); |
|
93 Svc.Prefs.resetBranch(""); |
|
94 } |
|
95 } |