--- a/toolkit/components/places/tests/unit/test_hosts_triggers.js
+++ b/toolkit/components/places/tests/unit/test_hosts_triggers.js
@@ -26,30 +26,44 @@ function isHostInMozPlaces(aURI) {
result = true;
break;
}
}
stmt.finalize();
return result;
}
-function isHostInMozHosts(aURI, aTyped, aPrefix) {
+function checkHostInMozHosts(aURI, aTyped, aPrefix, aShouldBePresent = true) {
+ if (typeof aURI == "string") {
+ aURI = new URL(aURI);
+ }
let stmt = DBConn().createStatement(
`SELECT host, typed, prefix
FROM moz_hosts
WHERE host = fixup_url(:host)
AND frecency NOTNULL`
);
- let result = false;
+ let result;
stmt.params.host = aURI.host;
if (stmt.executeStep()) {
- result = aTyped == stmt.row.typed && aPrefix == stmt.row.prefix;
+ result = {typed: stmt.row.typed, prefix: stmt.row.prefix};
}
stmt.finalize();
- return result;
+
+ if (aShouldBePresent) {
+ Assert.ok(result, "Result should be defined.");
+ Assert.equal(result.typed, aTyped, "The typed field should match.");
+ Assert.equal(result.prefix, aPrefix, "The prefix field should match.");
+ } else {
+ Assert.strictEqual(result, undefined);
+ }
+}
+
+function checkHostNotInMozHosts(aURI, aTyped, aPrefix) {
+ checkHostInMozHosts(aURI, aTyped, aPrefix, false);
}
var urls = [{uri: NetUtil.newURI("http://visit1.mozilla.org"),
expected: "visit1.mozilla.org",
typed: 0,
prefix: null
},
{uri: NetUtil.newURI("http://visit2.mozilla.org"),
@@ -72,26 +86,26 @@ add_task(function* test_moz_hosts_update
let place = { uri: url.uri,
title: "test for " + url.url,
transition: url.typed ? TRANSITION_TYPED : undefined };
places.push(place);
});
yield PlacesTestUtils.addVisits(places);
- do_check_true(isHostInMozHosts(urls[0].uri, urls[0].typed, urls[0].prefix));
- do_check_true(isHostInMozHosts(urls[1].uri, urls[1].typed, urls[1].prefix));
- do_check_true(isHostInMozHosts(urls[2].uri, urls[2].typed, urls[2].prefix));
+ checkHostInMozHosts(urls[0].uri, urls[0].typed, urls[0].prefix);
+ checkHostInMozHosts(urls[1].uri, urls[1].typed, urls[1].prefix);
+ checkHostInMozHosts(urls[2].uri, urls[2].typed, urls[2].prefix);
});
add_task(function* test_remove_places() {
yield PlacesUtils.history.remove(urls.map(x => x.uri));
for (let idx in urls) {
- do_check_false(isHostInMozHosts(urls[idx].uri, urls[idx].typed, urls[idx].prefix));
+ checkHostNotInMozHosts(urls[idx].uri, urls[idx].typed, urls[idx].prefix);
}
});
add_task(function* test_bookmark_changes() {
let testUri = NetUtil.newURI("http://test.mozilla.org");
let itemId = PlacesUtils.bookmarks.insertBookmark(PlacesUtils.unfiledBookmarksFolderId,
testUri,
@@ -102,43 +116,43 @@ add_task(function* test_bookmark_changes
// Change the hostname
PlacesUtils.bookmarks.changeBookmarkURI(itemId, NetUtil.newURI(NEW_URL));
yield PlacesTestUtils.clearHistory();
let newUri = NetUtil.newURI(NEW_URL);
do_check_true(isHostInMozPlaces(newUri));
- do_check_true(isHostInMozHosts(newUri, false, null));
- do_check_false(isHostInMozHosts(NetUtil.newURI("http://test.mozilla.org"), false, null));
+ checkHostInMozHosts(newUri, false, null);
+ checkHostNotInMozHosts(NetUtil.newURI("http://test.mozilla.org"), false, null);
});
add_task(function* test_bookmark_removal() {
let itemId = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.unfiledBookmarksFolderId,
PlacesUtils.bookmarks.DEFAULT_INDEX);
let newUri = NetUtil.newURI(NEW_URL);
PlacesUtils.bookmarks.removeItem(itemId);
yield PlacesTestUtils.clearHistory();
- do_check_false(isHostInMozHosts(newUri, false, null));
+ checkHostNotInMozHosts(newUri, false, null);
});
add_task(function* test_moz_hosts_typed_update() {
const TEST_URI = NetUtil.newURI("http://typed.mozilla.com");
let places = [{ uri: TEST_URI
, title: "test for " + TEST_URI.spec
},
{ uri: TEST_URI
, title: "test for " + TEST_URI.spec
, transition: TRANSITION_TYPED
}];
yield PlacesTestUtils.addVisits(places);
- do_check_true(isHostInMozHosts(TEST_URI, true, null));
+ checkHostInMozHosts(TEST_URI, true, null);
yield PlacesTestUtils.clearHistory();
});
add_task(function* test_moz_hosts_www_remove() {
function* test_removal(aURIToRemove, aURIToKeep, aCallback) {
let places = [{ uri: aURIToRemove
, title: "test for " + aURIToRemove.spec
, transition: TRANSITION_TYPED
@@ -151,17 +165,17 @@ add_task(function* test_moz_hosts_www_re
yield PlacesTestUtils.addVisits(places);
print("removing " + aURIToRemove.spec + " keeping " + aURIToKeep);
dump_table("moz_hosts");
dump_table("moz_places");
yield PlacesUtils.history.remove(aURIToRemove);
let prefix = /www/.test(aURIToKeep.spec) ? "www." : null;
dump_table("moz_hosts");
dump_table("moz_places");
- do_check_true(isHostInMozHosts(aURIToKeep, true, prefix));
+ checkHostInMozHosts(aURIToKeep, true, prefix);
}
const TEST_URI = NetUtil.newURI("http://rem.mozilla.com");
const TEST_WWW_URI = NetUtil.newURI("http://www.rem.mozilla.com");
yield test_removal(TEST_URI, TEST_WWW_URI);
yield test_removal(TEST_WWW_URI, TEST_URI);
yield PlacesTestUtils.clearHistory();
});
@@ -170,41 +184,101 @@ add_task(function* test_moz_hosts_ftp_ma
const TEST_URI_1 = NetUtil.newURI("ftp://www.mozilla.com/");
const TEST_URI_2 = NetUtil.newURI("ftp://mozilla.com/");
yield PlacesTestUtils.addVisits([
{ uri: TEST_URI_1, transition: TRANSITION_TYPED },
{ uri: TEST_URI_2, transition: TRANSITION_TYPED }
]);
- do_check_true(isHostInMozHosts(TEST_URI_1, true, "ftp://"));
+ checkHostInMozHosts(TEST_URI_1, true, "ftp://");
});
add_task(function* test_moz_hosts_ftp_not_matchall() {
const TEST_URI_1 = NetUtil.newURI("http://mozilla.com/");
const TEST_URI_2 = NetUtil.newURI("ftp://mozilla.com/");
yield PlacesTestUtils.addVisits([
{ uri: TEST_URI_1, transition: TRANSITION_TYPED },
{ uri: TEST_URI_2, transition: TRANSITION_TYPED }
]);
- do_check_true(isHostInMozHosts(TEST_URI_1, true, null));
+ checkHostInMozHosts(TEST_URI_1, true, null);
});
add_task(function* test_moz_hosts_update_2() {
// Check that updating trigger takes into account prefixes for different
// rev_hosts.
const TEST_URI_1 = NetUtil.newURI("https://www.google.it/");
const TEST_URI_2 = NetUtil.newURI("https://google.it/");
let places = [{ uri: TEST_URI_1
, transition: TRANSITION_TYPED
},
{ uri: TEST_URI_2
}];
yield PlacesTestUtils.addVisits(places);
- do_check_true(isHostInMozHosts(TEST_URI_1, true, "https://www."));
+ checkHostInMozHosts(TEST_URI_1, true, "https://www.");
});
-function run_test() {
- run_next_test();
+function getTestSection(baseURL1, baseURL2, extra) {
+ let extraStr = "";
+ let expectedSimplePrefix = null;
+ let expectedSecurePrefix = "https://";
+ if (extra) {
+ extraStr = ` (${extra})`;
+ expectedSimplePrefix = `${extra}.`;
+ expectedSecurePrefix = `https://${extra}.`;
+ }
+ return [{
+ title: `Test simple insecure${extraStr}`,
+ visits: [{ uri: baseURL1, transition: TRANSITION_TYPED }],
+ expect: [baseURL1, true, expectedSimplePrefix ]
+ }, {
+ title: `Test upgrade secure${extraStr}`,
+ visits: [{ uri: baseURL2, transition: TRANSITION_TYPED }],
+ expect: [baseURL2, true, null]
+ }, {
+ title: `Test remove insecure completely${extraStr}`,
+ remove: baseURL1,
+ expect: [baseURL2, true, expectedSecurePrefix]
+ }, {
+ title: `Test add more visits${extraStr}`,
+ visits: [
+ { uri: baseURL2, transition: TRANSITION_TYPED },
+ { uri: baseURL1, transition: TRANSITION_TYPED },
+ ],
+ expect: [baseURL2, true, null]
+ }, {
+ title: `Test switch to insecure${extraStr}`,
+ visits: [{ uri: baseURL1, transition: TRANSITION_TYPED }],
+ expect: [baseURL2, true, null]
+ }];
}
+
+const updateTestURL1 = "http://example.com/";
+const updateTestURL2 = "https://example.com/";
+
+const hostsUpdateTests = [{
+ title: "Upgrade Secure/Downgrade Insecure",
+ tests: getTestSection("http://example.com", "https://example.com")
+}, {
+ title: "Upgrade Secure/Downgrade Insecure (www)",
+ tests: getTestSection("http://www.example1.com", "https://www.example1.com", "www")
+}];
+
+add_task(function* test_moz_hosts_update() {
+ for (const section of hostsUpdateTests) {
+ do_print(section.title);
+
+ for (const test of section.tests) {
+ do_print(test.title);
+
+ if ("visits" in test) {
+ yield PlacesTestUtils.addVisits(test.visits);
+ }
+ if ("remove" in test) {
+ yield PlacesUtils.history.remove(test.remove);
+ }
+ checkHostInMozHosts(test.expect[0], test.expect[1], test.expect[2]);
+ }
+ }
+});