Bug 1584479 - Part 4: Update TrackingDBService test. r=ewright
authorNihanth Subramanya <nhnt11@gmail.com>
Fri, 01 Nov 2019 23:24:27 +0000
changeset 500282 16aac90f6572f363d2b5c8205e13115ac818869d
parent 500281 8da9387aac08174fd17e968351423d24e28ace91
child 500283 eb454f238f45f46da4f762f41bd511fd2d77e499
push id99500
push usernhnt11@gmail.com
push dateSat, 02 Nov 2019 09:56:01 +0000
treeherderautoland@16aac90f6572 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersewright
bugs1584479
milestone72.0a1
first release with
nightly linux32
16aac90f6572 / 72.0a1 / 20191102215129 / files
nightly linux64
16aac90f6572 / 72.0a1 / 20191102215129 / files
nightly win32
16aac90f6572 / 72.0a1 / 20191102215129 / files
nightly win64
16aac90f6572 / 72.0a1 / 20191102215129 / files
nightly mac
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
releases
nightly linux32
nightly linux64
nightly win32
nightly win64
Bug 1584479 - Part 4: Update TrackingDBService test. r=ewright Differential Revision: https://phabricator.services.mozilla.com/D51409
toolkit/components/antitracking/test/xpcshell/test_tracking_db_service.js
--- a/toolkit/components/antitracking/test/xpcshell/test_tracking_db_service.js
+++ b/toolkit/components/antitracking/test/xpcshell/test_tracking_db_service.js
@@ -55,60 +55,75 @@ const LOG = {
   "https://6.example.com": [
     [
       Ci.nsIWebProgressListener.STATE_COOKIES_BLOCKED_ALL |
         Ci.nsIWebProgressListener.STATE_LOADED_TRACKING_CONTENT,
       true,
       4,
     ],
   ],
+  "https://7.example.com": [
+    [Ci.nsIWebProgressListener.STATE_COOKIES_BLOCKED_SOCIALTRACKER, true, 1],
+  ],
+  "https://8.example.com": [
+    [Ci.nsIWebProgressListener.STATE_BLOCKED_SOCIALTRACKING_CONTENT, true, 1],
+  ],
 
   // The contents below should not add to the database.
   // Cookie loaded but not blocked.
-  "https://7.example.com": [
+  "https://10.example.com": [
     [Ci.nsIWebProgressListener.STATE_COOKIES_LOADED, true, 1],
   ],
   // Tracker cookie loaded but not blocked.
-  "https://8.example.com": [
+  "https://11.unblocked.example.com": [
     [Ci.nsIWebProgressListener.STATE_COOKIES_LOADED_TRACKER, true, 1],
   ],
   // Social tracker cookie loaded but not blocked.
-  "https://9.example.com": [
+  "https://12.example.com": [
     [Ci.nsIWebProgressListener.STATE_COOKIES_LOADED_SOCIALTRACKER, true, 1],
   ],
   // Cookie blocked for other reason (not a tracker)
-  "https://10.example.com": [
+  "https://13.example.com": [
     [Ci.nsIWebProgressListener.STATE_COOKIES_BLOCKED_BY_PERMISSION, true, 2],
   ],
   // Fingerprinters set to block, but this one has an exception
-  "https://11.example.com": [
+  "https://14.example.com": [
     [Ci.nsIWebProgressListener.STATE_BLOCKED_FINGERPRINTING_CONTENT, false, 1],
   ],
 };
 
 do_get_profile();
 
+Services.prefs.setBoolPref("browser.contentblocking.database.enabled", true);
+Services.prefs.setBoolPref(
+  "privacy.socialtracking.block_cookies.enabled",
+  true
+);
+registerCleanupFunction(() => {
+  Services.prefs.clearUserPref("browser.contentblocking.database.enabled");
+  Services.prefs.clearUserPref("privacy.socialtracking.block_cookies.enabled");
+});
+
 // This tests that data is added successfully, different types of events should get
 // their own entries, when the type is the same they should be aggregated. Events
 // that are not blocking events should not be recorded. Cookie blocking events
 // should only be recorded if we can identify the cookie as a tracking cookie.
 add_task(async function test_save_and_delete() {
-  Services.prefs.setBoolPref("browser.contentblocking.database.enabled", true);
   await TrackingDBService.saveEvents(JSON.stringify(LOG));
 
   // Peek in the DB to make sure we have the right data.
   let db = await Sqlite.openConnection({ path: DB_PATH });
   // Make sure the items table was created.
   ok(await db.tableExists("events"), "events table exists");
 
   // make sure we have the correct contents in the database
   let rows = await db.execute(SQL.selectAll);
   equal(
     rows.length,
-    4,
+    5,
     "Events that should not be saved have not been, length is 4"
   );
   rows = await db.execute(SQL.selectAllEntriesOfType, {
     type: TrackingDBService.TRACKERS_ID,
   });
   equal(rows.length, 1, "Only one day has had tracker entries, length is 1");
   let count = rows[0].getResultByName("count");
   equal(count, 1, "there is only one tracker entry");
@@ -137,29 +152,34 @@ add_task(async function test_save_and_de
   equal(
     rows.length,
     1,
     "Only one day has had fingerprinters entries, length is 1"
   );
   count = rows[0].getResultByName("count");
   equal(count, 1, "there is only one fingerprinter entry");
 
+  rows = await db.execute(SQL.selectAllEntriesOfType, {
+    type: TrackingDBService.SOCIAL_ID,
+  });
+  equal(rows.length, 1, "Only one day has had social entries, length is 1");
+  count = rows[0].getResultByName("count");
+  equal(count, 2, "there are two social entries");
+
   // Use the TrackingDBService API to delete the data.
   await TrackingDBService.clearAll();
   // Make sure the data was deleted.
   rows = await db.execute(SQL.selectAll);
   equal(rows.length, 0, "length is 0");
   await db.close();
-  Services.prefs.clearUserPref("browser.contentblocking.database.enabled");
 });
 
 // This tests that content blocking events encountered on the same day get aggregated,
 // and those on different days get seperate entries
 add_task(async function test_timestamp_aggragation() {
-  Services.prefs.setBoolPref("browser.contentblocking.database.enabled", true);
   // This creates the schema.
   await TrackingDBService.saveEvents(JSON.stringify({}));
   let db = await Sqlite.openConnection({ path: DB_PATH });
 
   let yesterday = new Date(Date.now() - 24 * 60 * 60 * 1000).toISOString();
   let today = new Date().toISOString();
   await db.execute(SQL.insertCustomTimeEvent, {
     type: TrackingDBService.TRACKERS_ID,
@@ -273,17 +293,16 @@ add_task(async function test_timestamp_a
   }
 
   // Use the TrackingDBService API to delete the data.
   await TrackingDBService.clearAll();
   // Make sure the data was deleted.
   rows = await db.execute(SQL.selectAll);
   equal(rows.length, 0, "length is 0");
   await db.close();
-  Services.prefs.clearUserPref("browser.contentblocking.database.enabled");
 });
 
 let addEventsToDB = async db => {
   let d = new Date(1521009000000);
   let date = d.toISOString();
   await db.execute(SQL.insertCustomTimeEvent, {
     type: TrackingDBService.CRYPTOMINERS_ID,
     count: 3,
@@ -317,17 +336,16 @@ let addEventsToDB = async db => {
     count: 2,
     timestamp: date,
   });
 };
 
 // This tests that TrackingDBService.getEventsByDateRange can accept two timestamps in unix epoch time
 // and return entries that occur within the timestamps, rounded to the nearest day and inclusive.
 add_task(async function test_getEventsByDateRange() {
-  Services.prefs.setBoolPref("browser.contentblocking.database.enabled", true);
   // This creates the schema.
   await TrackingDBService.saveEvents(JSON.stringify({}));
   let db = await Sqlite.openConnection({ path: DB_PATH });
   await addEventsToDB(db);
 
   let d = new Date(1521009000000);
   let daysBefore1 = new Date(d - 24 * 60 * 60 * 1000);
   let daysBefore4 = new Date(d - 4 * 24 * 60 * 60 * 1000);
@@ -354,45 +372,41 @@ add_task(async function test_getEventsBy
   equal(
     events.length,
     2,
     "There is 2 event entries between nine and four days before, inclusive"
   );
 
   await TrackingDBService.clearAll();
   await db.close();
-  Services.prefs.clearUserPref("browser.contentblocking.database.enabled");
 });
 
 // This tests that TrackingDBService.sumAllEvents returns the number of
 // tracking events in the database, and can handle 0 entries.
 add_task(async function test_sumAllEvents() {
-  Services.prefs.setBoolPref("browser.contentblocking.database.enabled", true);
   // This creates the schema.
   await TrackingDBService.saveEvents(JSON.stringify({}));
   let db = await Sqlite.openConnection({ path: DB_PATH });
 
   let sum = await TrackingDBService.sumAllEvents();
   equal(sum, 0, "There have been 0 events recorded");
 
   // populate the database
   await addEventsToDB(db);
 
   sum = await TrackingDBService.sumAllEvents();
   equal(sum, 11, "There have been 11 events recorded");
 
   await TrackingDBService.clearAll();
   await db.close();
-  Services.prefs.clearUserPref("browser.contentblocking.database.enabled");
 });
 
 // This tests that TrackingDBService.getEarliestRecordedDate returns the
 // earliest date recorded and can handle 0 entries.
 add_task(async function test_getEarliestRecordedDate() {
-  Services.prefs.setBoolPref("browser.contentblocking.database.enabled", true);
   // This creates the schema.
   await TrackingDBService.saveEvents(JSON.stringify({}));
   let db = await Sqlite.openConnection({ path: DB_PATH });
 
   let timestamp = await TrackingDBService.getEarliestRecordedDate();
   equal(timestamp, null, "There is no earliest recorded date");
 
   // populate the database
@@ -403,10 +417,9 @@ add_task(async function test_getEarliest
     .split("T")[0];
 
   timestamp = await TrackingDBService.getEarliestRecordedDate();
   let date = new Date(timestamp).toISOString().split("T")[0];
   equal(date, daysBefore9, "The earliest recorded event is nine days before.");
 
   await TrackingDBService.clearAll();
   await db.close();
-  Services.prefs.clearUserPref("browser.contentblocking.database.enabled");
 });