This commit is contained in:
dcamp@mozilla.com 2007-07-25 23:39:22 -07:00
Родитель 532a82cb78
Коммит a5c43e3212
3 изменённых файлов: 220 добавлений и 0 удалений

Просмотреть файл

@ -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();

Просмотреть файл

@ -0,0 +1 @@
cleanUp();

Просмотреть файл

@ -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();
}