1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/url-classifier/tests/unit/test_digest256.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,146 @@ 1.4 +Cu.import("resource://gre/modules/XPCOMUtils.jsm"); 1.5 + 1.6 +XPCOMUtils.defineLazyModuleGetter(this, "NetUtil", 1.7 + "resource://gre/modules/NetUtil.jsm"); 1.8 +XPCOMUtils.defineLazyModuleGetter(this, "Promise", 1.9 + "resource://gre/modules/Promise.jsm"); 1.10 +// Global test server for serving safebrowsing updates. 1.11 +let gHttpServ = null; 1.12 +// Global nsIUrlClassifierDBService 1.13 +let gDbService = Cc["@mozilla.org/url-classifier/dbservice;1"] 1.14 + .getService(Ci.nsIUrlClassifierDBService); 1.15 +// Security manager for creating nsIPrincipals from URIs 1.16 +let gSecMan = Cc["@mozilla.org/scriptsecuritymanager;1"] 1.17 + .getService(Ci.nsIScriptSecurityManager); 1.18 + 1.19 +// A map of tables to arrays of update redirect urls. 1.20 +let gTables = {}; 1.21 + 1.22 +// Construct an update from a file. 1.23 +function readFileToString(aFilename) { 1.24 + let f = do_get_file(aFilename); 1.25 + let stream = Cc["@mozilla.org/network/file-input-stream;1"] 1.26 + .createInstance(Ci.nsIFileInputStream); 1.27 + stream.init(f, -1, 0, 0); 1.28 + let buf = NetUtil.readInputStreamToString(stream, stream.available()); 1.29 + return buf; 1.30 +} 1.31 + 1.32 +// Registers a table for which to serve update chunks. Returns a promise that 1.33 +// resolves when that chunk has been downloaded. 1.34 +function registerTableUpdate(aTable, aFilename) { 1.35 + let deferred = Promise.defer(); 1.36 + // If we haven't been given an update for this table yet, add it to the map 1.37 + if (!(aTable in gTables)) { 1.38 + gTables[aTable] = []; 1.39 + } 1.40 + 1.41 + // The number of chunks associated with this table. 1.42 + let numChunks = gTables[aTable].length + 1; 1.43 + let redirectPath = "/" + aTable + "-" + numChunks; 1.44 + let redirectUrl = "localhost:4444" + redirectPath; 1.45 + 1.46 + // Store redirect url for that table so we can return it later when we 1.47 + // process an update request. 1.48 + gTables[aTable].push(redirectUrl); 1.49 + 1.50 + gHttpServ.registerPathHandler(redirectPath, function(request, response) { 1.51 + do_print("Mock safebrowsing server handling request for " + redirectPath); 1.52 + let contents = readFileToString(aFilename); 1.53 + response.setHeader("Content-Type", 1.54 + "application/vnd.google.safebrowsing-update", false); 1.55 + response.setStatusLine(request.httpVersion, 200, "OK"); 1.56 + response.bodyOutputStream.write(contents, contents.length); 1.57 + deferred.resolve(contents); 1.58 + }); 1.59 + return deferred.promise; 1.60 +} 1.61 + 1.62 +// Construct a response with redirect urls. 1.63 +function processUpdateRequest() { 1.64 + let response = "n:1000\n"; 1.65 + for (let table in gTables) { 1.66 + response += "i:" + table + "\n"; 1.67 + for (let i = 0; i < gTables[table].length; ++i) { 1.68 + response += "u:" + gTables[table][i] + "\n"; 1.69 + } 1.70 + } 1.71 + do_print("Returning update response: " + response); 1.72 + return response; 1.73 +} 1.74 + 1.75 +// Set up our test server to handle update requests. 1.76 +function run_test() { 1.77 + gHttpServ = new HttpServer(); 1.78 + gHttpServ.registerDirectory("/", do_get_cwd()); 1.79 + 1.80 + gHttpServ.registerPathHandler("/downloads", function(request, response) { 1.81 + let buf = NetUtil.readInputStreamToString(request.bodyInputStream, 1.82 + request.bodyInputStream.available()); 1.83 + let blob = processUpdateRequest(); 1.84 + response.setHeader("Content-Type", 1.85 + "application/vnd.google.safebrowsing-update", false); 1.86 + response.setStatusLine(request.httpVersion, 200, "OK"); 1.87 + response.bodyOutputStream.write(blob, blob.length); 1.88 + }); 1.89 + 1.90 + gHttpServ.start(4444); 1.91 + run_next_test(); 1.92 +} 1.93 + 1.94 +function createURI(s) { 1.95 + let service = Cc["@mozilla.org/network/io-service;1"] 1.96 + .getService(Ci.nsIIOService); 1.97 + return service.newURI(s, null, null); 1.98 +} 1.99 + 1.100 +// Just throw if we ever get an update or download error. 1.101 +function handleError(aEvent) { 1.102 + do_throw("We didn't download or update correctly: " + aEvent); 1.103 +} 1.104 + 1.105 +add_test(function test_update() { 1.106 + let streamUpdater = Cc["@mozilla.org/url-classifier/streamupdater;1"] 1.107 + .getService(Ci.nsIUrlClassifierStreamUpdater); 1.108 + streamUpdater.updateUrl = "http://localhost:4444/downloads"; 1.109 + 1.110 + // Load up some update chunks for the safebrowsing server to serve. 1.111 + registerTableUpdate("goog-downloadwhite-digest256", "data/digest1.chunk"); 1.112 + registerTableUpdate("goog-downloadwhite-digest256", "data/digest2.chunk"); 1.113 + 1.114 + // Download some updates, and don't continue until the downloads are done. 1.115 + function updateSuccess(aEvent) { 1.116 + // Timeout of n:1000 is constructed in processUpdateRequest above and 1.117 + // passed back in the callback in nsIUrlClassifierStreamUpdater on success. 1.118 + do_check_eq("1000", aEvent); 1.119 + do_print("All data processed"); 1.120 + run_next_test(); 1.121 + } 1.122 + streamUpdater.downloadUpdates( 1.123 + "goog-downloadwhite-digest256", 1.124 + "goog-downloadwhite-digest256;\n", 1.125 + updateSuccess, handleError, handleError); 1.126 +}); 1.127 + 1.128 +add_test(function test_url_not_whitelisted() { 1.129 + let uri = createURI("http://example.com"); 1.130 + let principal = gSecMan.getNoAppCodebasePrincipal(uri); 1.131 + gDbService.lookup(principal, "goog-downloadwhite-digest256", 1.132 + function handleEvent(aEvent) { 1.133 + // This URI is not on any lists. 1.134 + do_check_eq("", aEvent); 1.135 + run_next_test(); 1.136 + }); 1.137 +}); 1.138 + 1.139 +add_test(function test_url_whitelisted() { 1.140 + // Hash of "whitelisted.com/" (canonicalized URL) is: 1.141 + // 93CA5F48E15E9861CD37C2D95DB43D23CC6E6DE5C3F8FA6E8BE66F97CC518907 1.142 + let uri = createURI("http://whitelisted.com"); 1.143 + let principal = gSecMan.getNoAppCodebasePrincipal(uri); 1.144 + gDbService.lookup(principal, "goog-downloadwhite-digest256", 1.145 + function handleEvent(aEvent) { 1.146 + do_check_eq("goog-downloadwhite-digest256", aEvent); 1.147 + run_next_test(); 1.148 + }); 1.149 +});