From cf217ac8bc1874ed9fa553e092e950bc9ee52c4e Mon Sep 17 00:00:00 2001 From: "dcamp%mozilla.com" Date: Thu, 26 Jul 2007 01:50:27 +0000 Subject: [PATCH] tests for bug 387196 git-svn-id: svn://10.0.0.236/trunk@231004 18797224-902f-48f8-a5cc-f745e15eee43 --- .../tests/unit/head_urlclassifier.js | 52 ++++++ .../tests/unit/tail_urlclassifier.js | 1 + .../tests/unit/test_dbservice.js | 167 ++++++++++++++++++ .../url-classifier/tests/unit/test_urikeys.js | 35 ++++ 4 files changed, 255 insertions(+) create mode 100644 mozilla/toolkit/components/url-classifier/tests/unit/head_urlclassifier.js create mode 100644 mozilla/toolkit/components/url-classifier/tests/unit/tail_urlclassifier.js create mode 100644 mozilla/toolkit/components/url-classifier/tests/unit/test_dbservice.js create mode 100644 mozilla/toolkit/components/url-classifier/tests/unit/test_urikeys.js diff --git a/mozilla/toolkit/components/url-classifier/tests/unit/head_urlclassifier.js b/mozilla/toolkit/components/url-classifier/tests/unit/head_urlclassifier.js new file mode 100644 index 00000000000..fc0e7573616 --- /dev/null +++ b/mozilla/toolkit/components/url-classifier/tests/unit/head_urlclassifier.js @@ -0,0 +1,52 @@ +function dumpn(s) { + dump(s + "\n"); +} + +const NS_APP_USER_PROFILE_50_DIR = "ProfD"; +const NS_APP_USER_PROFILE_LOCAL_50_DIR = "ProfLD"; +const Ci = Components.interfaces; +const Cc = Components.classes; +const Cr = Components.results; + +// If there's no location registered for the profile direcotry, register one now. +var dirSvc = Cc["@mozilla.org/file/directory_service;1"].getService(Ci.nsIProperties); +var profileDir = null; +try { + profileDir = dirSvc.get(NS_APP_USER_PROFILE_50_DIR, Ci.nsIFile); +} catch (e) {} + +if (!profileDir) { + // Register our own provider for the profile directory. + // It will simply return the current directory. + var provider = { + getFile: function(prop, persistent) { + persistent.value = true; + if (prop == NS_APP_USER_PROFILE_50_DIR || + prop == NS_APP_USER_PROFILE_LOCAL_50_DIR) { + return dirSvc.get("CurProcD", Ci.nsIFile); + } + throw Cr.NS_ERROR_FAILURE; + }, + QueryInterface: function(iid) { + if (iid.equals(Ci.nsIDirectoryServiceProvider) || + iid.equals(Ci.nsISupports)) { + return this; + } + throw Cr.NS_ERROR_NO_INTERFACE; + } + }; + dirSvc.QueryInterface(Ci.nsIDirectoryService).registerProvider(provider); +} + +var iosvc = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService); + +function cleanUp() { + try { + // Delete a previously created sqlite file + var file = dirSvc.get('ProfLD', Ci.nsIFile); + file.append("urlclassifier3.sqlite"); + if (file.exists()) + file.remove(false); + } catch (e) {} +} +cleanUp(); diff --git a/mozilla/toolkit/components/url-classifier/tests/unit/tail_urlclassifier.js b/mozilla/toolkit/components/url-classifier/tests/unit/tail_urlclassifier.js new file mode 100644 index 00000000000..37f39d1a8db --- /dev/null +++ b/mozilla/toolkit/components/url-classifier/tests/unit/tail_urlclassifier.js @@ -0,0 +1 @@ +cleanUp(); diff --git a/mozilla/toolkit/components/url-classifier/tests/unit/test_dbservice.js b/mozilla/toolkit/components/url-classifier/tests/unit/test_dbservice.js new file mode 100644 index 00000000000..ad10157f025 --- /dev/null +++ b/mozilla/toolkit/components/url-classifier/tests/unit/test_dbservice.js @@ -0,0 +1,167 @@ +var dbservice = Cc["@mozilla.org/url-classifier/dbservice;1"].getService(Ci.nsIUrlClassifierDBService); + +var checkUrls = []; +var checkExpect; + +var chunk1Urls = [ + "test.com/aba", + "test.com/foo/bar", + "foo.bar.com/a/b/c" +]; +var chunk1 = chunk1Urls.join("\n"); + +var chunk2Urls = [ + "blah.com/a", + "baz.com/", + "255.255.0.1/" +]; +var chunk2 = chunk2Urls.join("\n"); + +var chunk3Urls = [ + "test.com/a", + "foo.bar.com/a", + "blah.com/a", + ]; +var chunk3 = chunk3Urls.join("\n"); + +// we are going to add chunks 1 and 2 to phish-simple, and chunk 2 to +// malware-simple. Then we'll remove the urls in chunk3 from phish-simple, +// then expire chunk 1 from phish-simple. +var phishExpected = {}; +var phishUnexpected = {}; +var malwareExpected = {}; +for (var i = 0; i < chunk2Urls.length; i++) { + phishExpected[chunk2Urls[i]] = true; + malwareExpected[chunk2Urls[i]] = true; +} +for (var i = 0; i < chunk3Urls.length; i++) { + delete phishExpected[chunk3Urls[i]]; + phishUnexpected[chunk3Urls[i]] = true; +} +for (var i = 0; i < chunk1Urls.length; i++) { + // chunk1 urls are expired + phishUnexpected[chunk1Urls[i]] = true; +} + +var numExpecting; + +function testFailure(arg) { + do_throw(arg); +} + +function tablesCallback(tables) +{ + var parts = tables.split("\n"); + parts.sort(); + // there's a leading \n here because splitting left an empty string + // after the trailing newline, which will sort first + do_check_eq(parts.join("\n"), + "\ntesting-malware-simple;a:1\ntesting-phish-simple;a:2s:3"); + do_test_finished(); +} + +function checkChunks() +{ + dbservice.getTables(tablesCallback); +} + +function checkDone() { + if (--numExpecting == 0) + checkChunks(); +} + +function phishExists(result) { + dumpn("phishExists: " + result); + try { + do_check_true(result.indexOf("testing-phish-simple") != -1); + } finally { + checkDone(); + } +} + +function phishDoesntExist(result) { + dumpn("phishDoesntExist: " + result); + try { + do_check_true(result.indexOf("testing-phish-simple") == -1); + } finally { + checkDone(); + } +} + +function malwareExists(result) { + dumpn("malwareExists: " + result); + + try { + do_check_true(result.indexOf("testing-malware-simple") != -1); + } finally { + checkDone(); + } +} + +function checkState() +{ + numExpecting = 0; + for (var key in phishExpected) { + dbservice.lookup("http://" + key, phishExists, true); + numExpecting++; + } + + for (var key in phishUnexpected) { + dbservice.lookup("http://" + key, phishDoesntExist, true); + numExpecting++; + } + + for (var key in malwareExpected) { + dbservice.lookup("http://" + key, malwareExists, true); + numExpecting++; + } +} + +function testSubSuccess(result) +{ + do_check_eq(result, "1000"); + checkState(); +} + +function do_subs() { + var data = + "n:1000\n" + + "i:testing-phish-simple\n" + + "s:3:" + chunk3.length + "\n" + + chunk3 + "\n" + + "ad:1\n"; + + dbservice.update(data); + dbservice.finish(testSubSuccess, testFailure); +} + +function testAddSuccess(arg) { + do_check_eq(arg, "1000"); + + do_subs(); +} + +function do_adds() { + // This test relies on the fact that only -regexp tables are ungzipped, + // and only -hash tables are assumed to be pre-md5'd. So we use + // a 'simple' table type to get simple hostname-per-line semantics. + + var data = + "n:1000\n" + + "i:testing-phish-simple\n" + + "a:1:" + chunk1.length + "\n" + + chunk1 + "\n" + + "a:2:" + chunk2.length + "\n" + + chunk2 + "\n" + + "i:testing-malware-simple\n" + + "a:1:" + chunk2.length + "\n" + + chunk2 + "\n"; + + dbservice.update(data); + dbservice.finish(testAddSuccess, testFailure); +} + +function run_test() { + do_adds(); + do_test_pending(); +} diff --git a/mozilla/toolkit/components/url-classifier/tests/unit/test_urikeys.js b/mozilla/toolkit/components/url-classifier/tests/unit/test_urikeys.js new file mode 100644 index 00000000000..dbacedcebbd --- /dev/null +++ b/mozilla/toolkit/components/url-classifier/tests/unit/test_urikeys.js @@ -0,0 +1,35 @@ + +var utils = Cc["@mozilla.org/url-classifier/utils;1"].createInstance(Ci.nsIUrlClassifierUtils); + +function testKey(spec, expect) +{ + var uri = iosvc.newURI(spec, null, null); + do_check_eq('"' + expect + '"', '"' + utils.getKeyForURI(uri) + '"'); +} + +function run_test() { +// testKey("http://poseidon.marinet.gr/~elani", "poseidon.marinet.gr/%7Eelani"), + testKey("http://www.google.com..", "www.google.com/"); + testKey("https://www.yaho%6F.com", "www.yahoo.com/"); + testKey("http://012.034.01.0xa", "10.28.1.10/"); + testKey("ftp://wierd..chars...%0f,%fa", "wierd.chars.%2c/"); + testKey("http://0x18ac89d5/http.www.paypal.com/", "24.172.137.213/http.www.paypal.com/"); + testKey("http://413960661/http.www.paypal.com/", "24.172.137.213/http.www.paypal.com/"); + testKey("http://03053104725/http.www.paypal.com/", "24.172.137.213/http.www.paypal.com/"); + testKey("http://www.barclays.co.uk.brccontrol.assruspede.org.bz/detailsconfirm", "www.barclays.co.uk.brccontrol.assruspede.org.bz/detailsconfirm"); + testKey("http://www.mozilla.org/foo", "www.mozilla.org/foo"); + testKey("http://,=.mozilla.org/foo", "%2c%3d.mozilla.org/foo"); + testKey("http://f00.b4r.mozi=lla.org/", "f00.b4r.mozi%3dlla.org/"); + testKey("http://a-_b.mozilla.org/", "a-%5fb.mozilla.org/"); + testKey("http://z%38bl%61h%%2F.com/", "z8blah%25%2f.com/"); + testKey("http://moZilla.Org/", "mozilla.org/"); + testKey("http://030.0254.0x89d5./", "24.172.137.213/"); + testKey("http://030.0254.0x89d5.../", "24.172.137.213/"); + testKey("http://...030.0254.0x89d5.../", "24.172.137.213/"); + testKey("http://127.0.0.1./", "127.0.0.1/"); + testKey("http://127.0.0.1/", "127.0.0.1/"); + testKey("http://a.b.c.d.e.f.g/path", "a.b.c.d.e.f.g/path"); + testKey("http://a.b.c.d.e.f.g...../path", "a.b.c.d.e.f.g/path"); + testKey("http://a.b.c.d.e.f.g./path", "a.b.c.d.e.f.g/path"); + testKey("http://a.b.c.d.e.f.g./path/to/../", "a.b.c.d.e.f.g/path/"); +}