toolkit/components/url-classifier/tests/unit/test_digest256.js

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

michael@0 1 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
michael@0 2
michael@0 3 XPCOMUtils.defineLazyModuleGetter(this, "NetUtil",
michael@0 4 "resource://gre/modules/NetUtil.jsm");
michael@0 5 XPCOMUtils.defineLazyModuleGetter(this, "Promise",
michael@0 6 "resource://gre/modules/Promise.jsm");
michael@0 7 // Global test server for serving safebrowsing updates.
michael@0 8 let gHttpServ = null;
michael@0 9 // Global nsIUrlClassifierDBService
michael@0 10 let gDbService = Cc["@mozilla.org/url-classifier/dbservice;1"]
michael@0 11 .getService(Ci.nsIUrlClassifierDBService);
michael@0 12 // Security manager for creating nsIPrincipals from URIs
michael@0 13 let gSecMan = Cc["@mozilla.org/scriptsecuritymanager;1"]
michael@0 14 .getService(Ci.nsIScriptSecurityManager);
michael@0 15
michael@0 16 // A map of tables to arrays of update redirect urls.
michael@0 17 let gTables = {};
michael@0 18
michael@0 19 // Construct an update from a file.
michael@0 20 function readFileToString(aFilename) {
michael@0 21 let f = do_get_file(aFilename);
michael@0 22 let stream = Cc["@mozilla.org/network/file-input-stream;1"]
michael@0 23 .createInstance(Ci.nsIFileInputStream);
michael@0 24 stream.init(f, -1, 0, 0);
michael@0 25 let buf = NetUtil.readInputStreamToString(stream, stream.available());
michael@0 26 return buf;
michael@0 27 }
michael@0 28
michael@0 29 // Registers a table for which to serve update chunks. Returns a promise that
michael@0 30 // resolves when that chunk has been downloaded.
michael@0 31 function registerTableUpdate(aTable, aFilename) {
michael@0 32 let deferred = Promise.defer();
michael@0 33 // If we haven't been given an update for this table yet, add it to the map
michael@0 34 if (!(aTable in gTables)) {
michael@0 35 gTables[aTable] = [];
michael@0 36 }
michael@0 37
michael@0 38 // The number of chunks associated with this table.
michael@0 39 let numChunks = gTables[aTable].length + 1;
michael@0 40 let redirectPath = "/" + aTable + "-" + numChunks;
michael@0 41 let redirectUrl = "localhost:4444" + redirectPath;
michael@0 42
michael@0 43 // Store redirect url for that table so we can return it later when we
michael@0 44 // process an update request.
michael@0 45 gTables[aTable].push(redirectUrl);
michael@0 46
michael@0 47 gHttpServ.registerPathHandler(redirectPath, function(request, response) {
michael@0 48 do_print("Mock safebrowsing server handling request for " + redirectPath);
michael@0 49 let contents = readFileToString(aFilename);
michael@0 50 response.setHeader("Content-Type",
michael@0 51 "application/vnd.google.safebrowsing-update", false);
michael@0 52 response.setStatusLine(request.httpVersion, 200, "OK");
michael@0 53 response.bodyOutputStream.write(contents, contents.length);
michael@0 54 deferred.resolve(contents);
michael@0 55 });
michael@0 56 return deferred.promise;
michael@0 57 }
michael@0 58
michael@0 59 // Construct a response with redirect urls.
michael@0 60 function processUpdateRequest() {
michael@0 61 let response = "n:1000\n";
michael@0 62 for (let table in gTables) {
michael@0 63 response += "i:" + table + "\n";
michael@0 64 for (let i = 0; i < gTables[table].length; ++i) {
michael@0 65 response += "u:" + gTables[table][i] + "\n";
michael@0 66 }
michael@0 67 }
michael@0 68 do_print("Returning update response: " + response);
michael@0 69 return response;
michael@0 70 }
michael@0 71
michael@0 72 // Set up our test server to handle update requests.
michael@0 73 function run_test() {
michael@0 74 gHttpServ = new HttpServer();
michael@0 75 gHttpServ.registerDirectory("/", do_get_cwd());
michael@0 76
michael@0 77 gHttpServ.registerPathHandler("/downloads", function(request, response) {
michael@0 78 let buf = NetUtil.readInputStreamToString(request.bodyInputStream,
michael@0 79 request.bodyInputStream.available());
michael@0 80 let blob = processUpdateRequest();
michael@0 81 response.setHeader("Content-Type",
michael@0 82 "application/vnd.google.safebrowsing-update", false);
michael@0 83 response.setStatusLine(request.httpVersion, 200, "OK");
michael@0 84 response.bodyOutputStream.write(blob, blob.length);
michael@0 85 });
michael@0 86
michael@0 87 gHttpServ.start(4444);
michael@0 88 run_next_test();
michael@0 89 }
michael@0 90
michael@0 91 function createURI(s) {
michael@0 92 let service = Cc["@mozilla.org/network/io-service;1"]
michael@0 93 .getService(Ci.nsIIOService);
michael@0 94 return service.newURI(s, null, null);
michael@0 95 }
michael@0 96
michael@0 97 // Just throw if we ever get an update or download error.
michael@0 98 function handleError(aEvent) {
michael@0 99 do_throw("We didn't download or update correctly: " + aEvent);
michael@0 100 }
michael@0 101
michael@0 102 add_test(function test_update() {
michael@0 103 let streamUpdater = Cc["@mozilla.org/url-classifier/streamupdater;1"]
michael@0 104 .getService(Ci.nsIUrlClassifierStreamUpdater);
michael@0 105 streamUpdater.updateUrl = "http://localhost:4444/downloads";
michael@0 106
michael@0 107 // Load up some update chunks for the safebrowsing server to serve.
michael@0 108 registerTableUpdate("goog-downloadwhite-digest256", "data/digest1.chunk");
michael@0 109 registerTableUpdate("goog-downloadwhite-digest256", "data/digest2.chunk");
michael@0 110
michael@0 111 // Download some updates, and don't continue until the downloads are done.
michael@0 112 function updateSuccess(aEvent) {
michael@0 113 // Timeout of n:1000 is constructed in processUpdateRequest above and
michael@0 114 // passed back in the callback in nsIUrlClassifierStreamUpdater on success.
michael@0 115 do_check_eq("1000", aEvent);
michael@0 116 do_print("All data processed");
michael@0 117 run_next_test();
michael@0 118 }
michael@0 119 streamUpdater.downloadUpdates(
michael@0 120 "goog-downloadwhite-digest256",
michael@0 121 "goog-downloadwhite-digest256;\n",
michael@0 122 updateSuccess, handleError, handleError);
michael@0 123 });
michael@0 124
michael@0 125 add_test(function test_url_not_whitelisted() {
michael@0 126 let uri = createURI("http://example.com");
michael@0 127 let principal = gSecMan.getNoAppCodebasePrincipal(uri);
michael@0 128 gDbService.lookup(principal, "goog-downloadwhite-digest256",
michael@0 129 function handleEvent(aEvent) {
michael@0 130 // This URI is not on any lists.
michael@0 131 do_check_eq("", aEvent);
michael@0 132 run_next_test();
michael@0 133 });
michael@0 134 });
michael@0 135
michael@0 136 add_test(function test_url_whitelisted() {
michael@0 137 // Hash of "whitelisted.com/" (canonicalized URL) is:
michael@0 138 // 93CA5F48E15E9861CD37C2D95DB43D23CC6E6DE5C3F8FA6E8BE66F97CC518907
michael@0 139 let uri = createURI("http://whitelisted.com");
michael@0 140 let principal = gSecMan.getNoAppCodebasePrincipal(uri);
michael@0 141 gDbService.lookup(principal, "goog-downloadwhite-digest256",
michael@0 142 function handleEvent(aEvent) {
michael@0 143 do_check_eq("goog-downloadwhite-digest256", aEvent);
michael@0 144 run_next_test();
michael@0 145 });
michael@0 146 });

mercurial