Bug 1369299 - Part 2: Add a test case for testing GeoIP and region default search lookup are not requested when 'privacy.resistFingerprinting' is true. r?Gijs,arthuredelstein
This test case will first create a local HttpServer and set the lookup urls to
point to this local server. And then, we trigger the lookups and see whether the
request has been fired.
MozReview-Commit-ID: BCj2CvnPvBl
--- a/browser/components/resistfingerprinting/test/browser/browser.ini
+++ b/browser/components/resistfingerprinting/test/browser/browser.ini
@@ -1,14 +1,15 @@
[DEFAULT]
tags = resistfingerprinting
support-files =
file_dummy.html
head.js
+[browser_geoIPLookup.js]
[browser_roundedWindow_dialogWindow.js]
[browser_roundedWindow_newWindow.js]
[browser_roundedWindow_open_max.js]
[browser_roundedWindow_open_mid.js]
[browser_roundedWindow_open_min.js]
[browser_roundedWindow_windowSetting_max.js]
[browser_roundedWindow_windowSetting_mid.js]
[browser_roundedWindow_windowSetting_min.js]
new file mode 100644
--- /dev/null
+++ b/browser/components/resistfingerprinting/test/browser/browser_geoIPLookup.js
@@ -0,0 +1,191 @@
+/**
+ * Bug 1369299 - A test case to make sure the GeoIP and region default search
+ * lookup doesn't be requested when 'privacy.resistFingerprinting' is true.
+ */
+
+var { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components;
+
+Cu.import("resource://gre/modules/Services.jsm");
+Cu.import("resource://gre/modules/Promise.jsm");
+
+let {HttpServer} = Cu.import("resource://testing-common/httpd.js", {});
+
+var gReqs = 0;
+var originalRegionDefaultURL;
+var originalCountryCode;
+var originalRegion;
+
+const BROWSER_SEARCH_PREF = "browser.search.";
+
+/**
+ * Clean the profile of any cache file left from a previous run.
+ * Returns a boolean indicating if the cache file existed.
+ */
+function removeCacheFile() {
+ const CACHE_FILENAME = "search.json.mozlz4";
+
+ let file = Services.dirsvc.get("ProfD", Ci.nsIFile);
+ file.append(CACHE_FILENAME);
+ if (file.exists()) {
+ file.remove(false);
+ return true;
+ }
+ return false;
+}
+
+/**
+ * Returns a promise that is resolved when an observer notification from the
+ * search service fires with the specified data.
+ *
+ * @param aExpectedData
+ * The value the observer notification sends that causes us to resolve
+ * the promise.
+ */
+function waitForSearchNotification(aExpectedData, aCallback) {
+ const SEARCH_SERVICE_TOPIC = "browser-search-service";
+ Services.obs.addObserver(function observer(aSubject, aTopic, aData) {
+ if (aData != aExpectedData)
+ return;
+
+ Services.obs.removeObserver(observer, SEARCH_SERVICE_TOPIC);
+ aCallback();
+ }, SEARCH_SERVICE_TOPIC);
+}
+
+function asyncInit() {
+ return new Promise(resolve => {
+ Services.search.init(function() {
+ ok(Services.search.isInitialized, "search service should be initialized");
+ resolve();
+ });
+ });
+}
+
+function asyncReInit() {
+ const kLocalePref = "general.useragent.locale";
+
+ let promise = new Promise(resolve => {
+ waitForSearchNotification("reinit-complete", resolve);
+ });
+
+ Services.search.QueryInterface(Ci.nsIObserver)
+ .observe(null, "nsPref:changed", kLocalePref);
+
+ return promise;
+}
+
+function startHttpServer() {
+ let srv = new HttpServer();
+
+ function lookupCountry(metadata, response) {
+ gReqs++;
+
+ response.setStatusLine(metadata.httpVersion, 200, "OK");
+ response.setHeader("Content-Type", "application/json", false);
+ let body = '{"country_code" : "US"}';
+ response.bodyOutputStream.write(body, body.length);
+ }
+
+ function lookupRegionDefault(metadata, response) {
+ gReqs++;
+
+ response.setStatusLine(metadata.httpVersion, 200, "OK");
+ response.setHeader("Content-Type", "application/json", false);
+ let body = '{}';
+ response.bodyOutputStream.write(body, body.length);
+ }
+
+ srv.registerPathHandler("/lookup_country", lookupCountry);
+ srv.registerPathHandler("/lookup_regionDefault", lookupRegionDefault);
+ srv.start(-1);
+ return srv;
+}
+
+var gHttpServer;
+var gEngineCount;
+
+add_task(async function Setup() {
+ // ContentSearch is interferring with our async re-initializations of the
+ // search service: once _initServicePromise has resolved, it will access
+ // the search service, thus causing unpredictable behavior due to
+ // synchronous initializations of the service.
+ let originalContentSearchPromise = ContentSearch._initServicePromise;
+ ContentSearch._initServicePromise = new Promise(resolve => {
+ registerCleanupFunction(() => {
+ ContentSearch._initServicePromise = originalContentSearchPromise;
+ resolve();
+ });
+ });
+
+ await asyncInit();
+ gEngineCount = Services.search.getVisibleEngines().length;
+
+ // Backup the original country code and region.
+ originalCountryCode = Services.prefs.getCharPref(BROWSER_SEARCH_PREF + "countryCode");
+ originalRegion = Services.prefs.getCharPref(BROWSER_SEARCH_PREF + "region");
+
+ await SpecialPowers.pushPrefEnv({"set":
+ [["privacy.resistFingerprinting", true]]
+ });
+
+ gHttpServer = startHttpServer();
+
+ // Set the request URL for geoIP and region default lookup.
+ let geoUrl = "http://localhost:" + gHttpServer.identity.primaryPort + "/lookup_country";
+ Services.prefs.setCharPref(BROWSER_SEARCH_PREF + "geoip.url", geoUrl);
+ originalRegionDefaultURL = Services.prefs.getCharPref(BROWSER_SEARCH_PREF + "geoSpecificDefaults.url");
+ let regionDefaultUrl = "http://localhost:" + gHttpServer.identity.primaryPort + "/lookup_regionDefault";
+ Services.prefs.getDefaultBranch(BROWSER_SEARCH_PREF)
+ .setCharPref("geoSpecificDefaults.url", regionDefaultUrl);
+});
+
+add_task(async function runTest() {
+ waitForSearchNotification("uninit-complete", () => {
+ // Verify search service is not initialized
+ is(Services.search.isInitialized, false, "Search service should NOT be initialized");
+
+ removeCacheFile();
+
+ // Enable region default lookup.
+ Services.prefs.setBoolPref(BROWSER_SEARCH_PREF + "geoSpecificDefaults", true);
+
+ // Clear country code and region.
+ Services.prefs.clearUserPref(BROWSER_SEARCH_PREF + "countryCode");
+ Services.prefs.clearUserPref(BROWSER_SEARCH_PREF + "region");
+ });
+
+ await asyncReInit();
+
+ // Check that whether no requests has been made.
+ is(gReqs, 0, "No GeoIP and region default lookups been made.")
+});
+
+add_task(async function Cleanup() {
+ await SpecialPowers.pushPrefEnv({"set":
+ [["privacy.resistFingerprinting", false]]
+ });
+
+ waitForSearchNotification("uninit-complete", () => {
+ // Verify search service is not initialized
+ is(Services.search.isInitialized, false,
+ "Search service should NOT be initialized");
+ removeCacheFile();
+
+ // We can't clear the pref because it's set to false by testing/profiles/prefs_general.js
+ Services.prefs.setBoolPref(BROWSER_SEARCH_PREF + "geoSpecificDefaults", false);
+
+ Services.prefs.clearUserPref(BROWSER_SEARCH_PREF + "geoip.url");
+
+ // Restore country code and region.
+ Services.prefs.setCharPref(BROWSER_SEARCH_PREF + "countryCode", originalCountryCode)
+ Services.prefs.setCharPref(BROWSER_SEARCH_PREF + "region", originalRegion)
+
+ Services.prefs.getDefaultBranch(BROWSER_SEARCH_PREF)
+ .setCharPref("geoSpecificDefaults.url", originalRegionDefaultURL);
+ });
+
+ await asyncReInit();
+ is(gEngineCount, Services.search.getVisibleEngines().length,
+ "correct engine count after cleanup");
+ gHttpServer.stop(() => {});
+});