services/sync/tests/unit/test_service_passwordUTF8.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial