tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

browser_sanitize-timespans.js (33317B)


      1 requestLongerTimeout(2);
      2 
      3 const { PlacesTestUtils } = ChromeUtils.importESModule(
      4  "resource://testing-common/PlacesTestUtils.sys.mjs"
      5 );
      6 
      7 // Bug 453440 - Test the timespan-based logic of the sanitizer code
      8 var now_mSec = Date.now();
      9 var now_uSec = now_mSec * 1000;
     10 
     11 function promiseFormHistoryRemoved() {
     12  return new Promise(resolve => {
     13    Services.obs.addObserver(function onfh() {
     14      Services.obs.removeObserver(onfh, "satchel-storage-changed");
     15      resolve();
     16    }, "satchel-storage-changed");
     17  });
     18 }
     19 
     20 function promiseDownloadRemoved(list) {
     21  return new Promise(resolve => {
     22    let view = {
     23      onDownloadRemoved() {
     24        list.removeView(view);
     25        resolve();
     26      },
     27    };
     28 
     29    list.addView(view);
     30  });
     31 }
     32 
     33 add_task(async function test() {
     34  await setupDownloads();
     35  await setupFormHistory();
     36  await setupHistory();
     37  await onHistoryReady();
     38 });
     39 
     40 async function countEntries(name, message, check) {
     41  var obj = {};
     42  if (name !== null) {
     43    obj.fieldname = name;
     44  }
     45  let count = await FormHistory.count(obj);
     46  check(count, message);
     47 }
     48 
     49 async function onHistoryReady() {
     50  var hoursSinceMidnight = new Date().getHours();
     51  var minutesSinceMidnight = hoursSinceMidnight * 60 + new Date().getMinutes();
     52 
     53  // Should test cookies here, but nsICookieManager/nsICookieService
     54  // doesn't let us fake creation times.  bug 463127
     55 
     56  var itemPrefs = Services.prefs.getBranch("privacy.cpd.");
     57  itemPrefs.setBoolPref("history", true);
     58  itemPrefs.setBoolPref("downloads", true);
     59  itemPrefs.setBoolPref("cache", false);
     60  itemPrefs.setBoolPref("cookies", false);
     61  itemPrefs.setBoolPref("formdata", true);
     62  itemPrefs.setBoolPref("offlineApps", false);
     63  itemPrefs.setBoolPref("passwords", false);
     64  itemPrefs.setBoolPref("sessions", false);
     65  itemPrefs.setBoolPref("siteSettings", false);
     66 
     67  let publicList = await Downloads.getList(Downloads.PUBLIC);
     68  let downloadPromise = promiseDownloadRemoved(publicList);
     69  let formHistoryPromise = promiseFormHistoryRemoved();
     70 
     71  // Clear 10 minutes ago
     72  let range = [now_uSec - 10 * 60 * 1000000, now_uSec];
     73  await Sanitizer.sanitize(null, { range, ignoreTimespan: false });
     74 
     75  await formHistoryPromise;
     76  await downloadPromise;
     77 
     78  ok(
     79    !(await PlacesUtils.history.hasVisits("https://10minutes.com")),
     80    "Pretend visit to 10minutes.com should now be deleted"
     81  );
     82  ok(
     83    await PlacesUtils.history.hasVisits("https://1hour.com"),
     84    "Pretend visit to 1hour.com should should still exist"
     85  );
     86  ok(
     87    await PlacesUtils.history.hasVisits("https://1hour10minutes.com"),
     88    "Pretend visit to 1hour10minutes.com should should still exist"
     89  );
     90  ok(
     91    await PlacesUtils.history.hasVisits("https://2hour.com"),
     92    "Pretend visit to 2hour.com should should still exist"
     93  );
     94  ok(
     95    await PlacesUtils.history.hasVisits("https://2hour10minutes.com"),
     96    "Pretend visit to 2hour10minutes.com should should still exist"
     97  );
     98  ok(
     99    await PlacesUtils.history.hasVisits("https://4hour.com"),
    100    "Pretend visit to 4hour.com should should still exist"
    101  );
    102  ok(
    103    await PlacesUtils.history.hasVisits("https://4hour10minutes.com"),
    104    "Pretend visit to 4hour10minutes.com should should still exist"
    105  );
    106  if (minutesSinceMidnight > 10) {
    107    ok(
    108      await PlacesUtils.history.hasVisits("https://today.com"),
    109      "Pretend visit to today.com should still exist"
    110    );
    111  }
    112  ok(
    113    await PlacesUtils.history.hasVisits("https://before-today.com"),
    114    "Pretend visit to before-today.com should still exist"
    115  );
    116 
    117  let checkZero = function (num, message) {
    118    is(num, 0, message);
    119  };
    120  let checkOne = function (num, message) {
    121    is(num, 1, message);
    122  };
    123 
    124  await countEntries(
    125    "10minutes",
    126    "10minutes form entry should be deleted",
    127    checkZero
    128  );
    129  await countEntries("1hour", "1hour form entry should still exist", checkOne);
    130  await countEntries(
    131    "1hour10minutes",
    132    "1hour10minutes form entry should still exist",
    133    checkOne
    134  );
    135  await countEntries("2hour", "2hour form entry should still exist", checkOne);
    136  await countEntries(
    137    "2hour10minutes",
    138    "2hour10minutes form entry should still exist",
    139    checkOne
    140  );
    141  await countEntries("4hour", "4hour form entry should still exist", checkOne);
    142  await countEntries(
    143    "4hour10minutes",
    144    "4hour10minutes form entry should still exist",
    145    checkOne
    146  );
    147  if (minutesSinceMidnight > 10) {
    148    await countEntries(
    149      "today",
    150      "today form entry should still exist",
    151      checkOne
    152    );
    153  }
    154  await countEntries(
    155    "b4today",
    156    "b4today form entry should still exist",
    157    checkOne
    158  );
    159 
    160  ok(
    161    !(await downloadExists(publicList, "fakefile-10-minutes")),
    162    "10 minute download should now be deleted"
    163  );
    164  ok(
    165    await downloadExists(publicList, "fakefile-1-hour"),
    166    "<1 hour download should still be present"
    167  );
    168  ok(
    169    await downloadExists(publicList, "fakefile-1-hour-10-minutes"),
    170    "1 hour 10 minute download should still be present"
    171  );
    172  ok(
    173    await downloadExists(publicList, "fakefile-old"),
    174    "Year old download should still be present"
    175  );
    176  ok(
    177    await downloadExists(publicList, "fakefile-2-hour"),
    178    "<2 hour old download should still be present"
    179  );
    180  ok(
    181    await downloadExists(publicList, "fakefile-2-hour-10-minutes"),
    182    "2 hour 10 minute download should still be present"
    183  );
    184  ok(
    185    await downloadExists(publicList, "fakefile-4-hour"),
    186    "<4 hour old download should still be present"
    187  );
    188  ok(
    189    await downloadExists(publicList, "fakefile-4-hour-10-minutes"),
    190    "4 hour 10 minute download should still be present"
    191  );
    192 
    193  if (minutesSinceMidnight > 10) {
    194    ok(
    195      await downloadExists(publicList, "fakefile-today"),
    196      "'Today' download should still be present"
    197    );
    198  }
    199 
    200  downloadPromise = promiseDownloadRemoved(publicList);
    201  formHistoryPromise = promiseFormHistoryRemoved();
    202 
    203  // Clear 1 hour
    204  Services.prefs.setIntPref(Sanitizer.PREF_TIMESPAN, 1);
    205  await Sanitizer.sanitize(null, { ignoreTimespan: false });
    206 
    207  await formHistoryPromise;
    208  await downloadPromise;
    209 
    210  ok(
    211    !(await PlacesUtils.history.hasVisits("https://1hour.com")),
    212    "Pretend visit to 1hour.com should now be deleted"
    213  );
    214  ok(
    215    await PlacesUtils.history.hasVisits("https://1hour10minutes.com"),
    216    "Pretend visit to 1hour10minutes.com should should still exist"
    217  );
    218  ok(
    219    await PlacesUtils.history.hasVisits("https://2hour.com"),
    220    "Pretend visit to 2hour.com should should still exist"
    221  );
    222  ok(
    223    await PlacesUtils.history.hasVisits("https://2hour10minutes.com"),
    224    "Pretend visit to 2hour10minutes.com should should still exist"
    225  );
    226  ok(
    227    await PlacesUtils.history.hasVisits("https://4hour.com"),
    228    "Pretend visit to 4hour.com should should still exist"
    229  );
    230  ok(
    231    await PlacesUtils.history.hasVisits("https://4hour10minutes.com"),
    232    "Pretend visit to 4hour10minutes.com should should still exist"
    233  );
    234  if (hoursSinceMidnight > 1) {
    235    ok(
    236      await PlacesUtils.history.hasVisits("https://today.com"),
    237      "Pretend visit to today.com should still exist"
    238    );
    239  }
    240  ok(
    241    await PlacesUtils.history.hasVisits("https://before-today.com"),
    242    "Pretend visit to before-today.com should still exist"
    243  );
    244 
    245  await countEntries("1hour", "1hour form entry should be deleted", checkZero);
    246  await countEntries(
    247    "1hour10minutes",
    248    "1hour10minutes form entry should still exist",
    249    checkOne
    250  );
    251  await countEntries("2hour", "2hour form entry should still exist", checkOne);
    252  await countEntries(
    253    "2hour10minutes",
    254    "2hour10minutes form entry should still exist",
    255    checkOne
    256  );
    257  await countEntries("4hour", "4hour form entry should still exist", checkOne);
    258  await countEntries(
    259    "4hour10minutes",
    260    "4hour10minutes form entry should still exist",
    261    checkOne
    262  );
    263  if (hoursSinceMidnight > 1) {
    264    await countEntries(
    265      "today",
    266      "today form entry should still exist",
    267      checkOne
    268    );
    269  }
    270  await countEntries(
    271    "b4today",
    272    "b4today form entry should still exist",
    273    checkOne
    274  );
    275 
    276  ok(
    277    !(await downloadExists(publicList, "fakefile-1-hour")),
    278    "<1 hour download should now be deleted"
    279  );
    280  ok(
    281    await downloadExists(publicList, "fakefile-1-hour-10-minutes"),
    282    "1 hour 10 minute download should still be present"
    283  );
    284  ok(
    285    await downloadExists(publicList, "fakefile-old"),
    286    "Year old download should still be present"
    287  );
    288  ok(
    289    await downloadExists(publicList, "fakefile-2-hour"),
    290    "<2 hour old download should still be present"
    291  );
    292  ok(
    293    await downloadExists(publicList, "fakefile-2-hour-10-minutes"),
    294    "2 hour 10 minute download should still be present"
    295  );
    296  ok(
    297    await downloadExists(publicList, "fakefile-4-hour"),
    298    "<4 hour old download should still be present"
    299  );
    300  ok(
    301    await downloadExists(publicList, "fakefile-4-hour-10-minutes"),
    302    "4 hour 10 minute download should still be present"
    303  );
    304 
    305  if (hoursSinceMidnight > 1) {
    306    ok(
    307      await downloadExists(publicList, "fakefile-today"),
    308      "'Today' download should still be present"
    309    );
    310  }
    311 
    312  downloadPromise = promiseDownloadRemoved(publicList);
    313  formHistoryPromise = promiseFormHistoryRemoved();
    314 
    315  // Clear 1 hour 10 minutes
    316  range = [now_uSec - 70 * 60 * 1000000, now_uSec];
    317  await Sanitizer.sanitize(null, { range, ignoreTimespan: false });
    318 
    319  await formHistoryPromise;
    320  await downloadPromise;
    321 
    322  ok(
    323    !(await PlacesUtils.history.hasVisits("https://1hour10minutes.com")),
    324    "Pretend visit to 1hour10minutes.com should now be deleted"
    325  );
    326  ok(
    327    await PlacesUtils.history.hasVisits("https://2hour.com"),
    328    "Pretend visit to 2hour.com should should still exist"
    329  );
    330  ok(
    331    await PlacesUtils.history.hasVisits("https://2hour10minutes.com"),
    332    "Pretend visit to 2hour10minutes.com should should still exist"
    333  );
    334  ok(
    335    await PlacesUtils.history.hasVisits("https://4hour.com"),
    336    "Pretend visit to 4hour.com should should still exist"
    337  );
    338  ok(
    339    await PlacesUtils.history.hasVisits("https://4hour10minutes.com"),
    340    "Pretend visit to 4hour10minutes.com should should still exist"
    341  );
    342  if (minutesSinceMidnight > 70) {
    343    ok(
    344      await PlacesUtils.history.hasVisits("https://today.com"),
    345      "Pretend visit to today.com should still exist"
    346    );
    347  }
    348  ok(
    349    await PlacesUtils.history.hasVisits("https://before-today.com"),
    350    "Pretend visit to before-today.com should still exist"
    351  );
    352 
    353  await countEntries(
    354    "1hour10minutes",
    355    "1hour10minutes form entry should be deleted",
    356    checkZero
    357  );
    358  await countEntries("2hour", "2hour form entry should still exist", checkOne);
    359  await countEntries(
    360    "2hour10minutes",
    361    "2hour10minutes form entry should still exist",
    362    checkOne
    363  );
    364  await countEntries("4hour", "4hour form entry should still exist", checkOne);
    365  await countEntries(
    366    "4hour10minutes",
    367    "4hour10minutes form entry should still exist",
    368    checkOne
    369  );
    370  if (minutesSinceMidnight > 70) {
    371    await countEntries(
    372      "today",
    373      "today form entry should still exist",
    374      checkOne
    375    );
    376  }
    377  await countEntries(
    378    "b4today",
    379    "b4today form entry should still exist",
    380    checkOne
    381  );
    382 
    383  ok(
    384    !(await downloadExists(publicList, "fakefile-1-hour-10-minutes")),
    385    "1 hour 10 minute old download should now be deleted"
    386  );
    387  ok(
    388    await downloadExists(publicList, "fakefile-old"),
    389    "Year old download should still be present"
    390  );
    391  ok(
    392    await downloadExists(publicList, "fakefile-2-hour"),
    393    "<2 hour old download should still be present"
    394  );
    395  ok(
    396    await downloadExists(publicList, "fakefile-2-hour-10-minutes"),
    397    "2 hour 10 minute download should still be present"
    398  );
    399  ok(
    400    await downloadExists(publicList, "fakefile-4-hour"),
    401    "<4 hour old download should still be present"
    402  );
    403  ok(
    404    await downloadExists(publicList, "fakefile-4-hour-10-minutes"),
    405    "4 hour 10 minute download should still be present"
    406  );
    407  if (minutesSinceMidnight > 70) {
    408    ok(
    409      await downloadExists(publicList, "fakefile-today"),
    410      "'Today' download should still be present"
    411    );
    412  }
    413 
    414  downloadPromise = promiseDownloadRemoved(publicList);
    415  formHistoryPromise = promiseFormHistoryRemoved();
    416 
    417  // Clear 2 hours
    418  Services.prefs.setIntPref(Sanitizer.PREF_TIMESPAN, 2);
    419  await Sanitizer.sanitize(null, { ignoreTimespan: false });
    420 
    421  await formHistoryPromise;
    422  await downloadPromise;
    423 
    424  ok(
    425    !(await PlacesUtils.history.hasVisits("https://2hour.com")),
    426    "Pretend visit to 2hour.com should now be deleted"
    427  );
    428  ok(
    429    await PlacesUtils.history.hasVisits("https://2hour10minutes.com"),
    430    "Pretend visit to 2hour10minutes.com should should still exist"
    431  );
    432  ok(
    433    await PlacesUtils.history.hasVisits("https://4hour.com"),
    434    "Pretend visit to 4hour.com should should still exist"
    435  );
    436  ok(
    437    await PlacesUtils.history.hasVisits("https://4hour10minutes.com"),
    438    "Pretend visit to 4hour10minutes.com should should still exist"
    439  );
    440  if (hoursSinceMidnight > 2) {
    441    ok(
    442      await PlacesUtils.history.hasVisits("https://today.com"),
    443      "Pretend visit to today.com should still exist"
    444    );
    445  }
    446  ok(
    447    await PlacesUtils.history.hasVisits("https://before-today.com"),
    448    "Pretend visit to before-today.com should still exist"
    449  );
    450 
    451  await countEntries("2hour", "2hour form entry should be deleted", checkZero);
    452  await countEntries(
    453    "2hour10minutes",
    454    "2hour10minutes form entry should still exist",
    455    checkOne
    456  );
    457  await countEntries("4hour", "4hour form entry should still exist", checkOne);
    458  await countEntries(
    459    "4hour10minutes",
    460    "4hour10minutes form entry should still exist",
    461    checkOne
    462  );
    463  if (hoursSinceMidnight > 2) {
    464    await countEntries(
    465      "today",
    466      "today form entry should still exist",
    467      checkOne
    468    );
    469  }
    470  await countEntries(
    471    "b4today",
    472    "b4today form entry should still exist",
    473    checkOne
    474  );
    475 
    476  ok(
    477    !(await downloadExists(publicList, "fakefile-2-hour")),
    478    "<2 hour old download should now be deleted"
    479  );
    480  ok(
    481    await downloadExists(publicList, "fakefile-old"),
    482    "Year old download should still be present"
    483  );
    484  ok(
    485    await downloadExists(publicList, "fakefile-2-hour-10-minutes"),
    486    "2 hour 10 minute download should still be present"
    487  );
    488  ok(
    489    await downloadExists(publicList, "fakefile-4-hour"),
    490    "<4 hour old download should still be present"
    491  );
    492  ok(
    493    await downloadExists(publicList, "fakefile-4-hour-10-minutes"),
    494    "4 hour 10 minute download should still be present"
    495  );
    496  if (hoursSinceMidnight > 2) {
    497    ok(
    498      await downloadExists(publicList, "fakefile-today"),
    499      "'Today' download should still be present"
    500    );
    501  }
    502 
    503  downloadPromise = promiseDownloadRemoved(publicList);
    504  formHistoryPromise = promiseFormHistoryRemoved();
    505 
    506  // Clear 2 hours 10 minutes
    507  range = [now_uSec - 130 * 60 * 1000000, now_uSec];
    508  await Sanitizer.sanitize(null, { range, ignoreTimespan: false });
    509 
    510  await formHistoryPromise;
    511  await downloadPromise;
    512 
    513  ok(
    514    !(await PlacesUtils.history.hasVisits("https://2hour10minutes.com")),
    515    "Pretend visit to 2hour10minutes.com should now be deleted"
    516  );
    517  ok(
    518    await PlacesUtils.history.hasVisits("https://4hour.com"),
    519    "Pretend visit to 4hour.com should should still exist"
    520  );
    521  ok(
    522    await PlacesUtils.history.hasVisits("https://4hour10minutes.com"),
    523    "Pretend visit to 4hour10minutes.com should should still exist"
    524  );
    525  if (minutesSinceMidnight > 130) {
    526    ok(
    527      await PlacesUtils.history.hasVisits("https://today.com"),
    528      "Pretend visit to today.com should still exist"
    529    );
    530  }
    531  ok(
    532    await PlacesUtils.history.hasVisits("https://before-today.com"),
    533    "Pretend visit to before-today.com should still exist"
    534  );
    535 
    536  await countEntries(
    537    "2hour10minutes",
    538    "2hour10minutes form entry should be deleted",
    539    checkZero
    540  );
    541  await countEntries("4hour", "4hour form entry should still exist", checkOne);
    542  await countEntries(
    543    "4hour10minutes",
    544    "4hour10minutes form entry should still exist",
    545    checkOne
    546  );
    547  if (minutesSinceMidnight > 130) {
    548    await countEntries(
    549      "today",
    550      "today form entry should still exist",
    551      checkOne
    552    );
    553  }
    554  await countEntries(
    555    "b4today",
    556    "b4today form entry should still exist",
    557    checkOne
    558  );
    559 
    560  ok(
    561    !(await downloadExists(publicList, "fakefile-2-hour-10-minutes")),
    562    "2 hour 10 minute old download should now be deleted"
    563  );
    564  ok(
    565    await downloadExists(publicList, "fakefile-4-hour"),
    566    "<4 hour old download should still be present"
    567  );
    568  ok(
    569    await downloadExists(publicList, "fakefile-4-hour-10-minutes"),
    570    "4 hour 10 minute download should still be present"
    571  );
    572  ok(
    573    await downloadExists(publicList, "fakefile-old"),
    574    "Year old download should still be present"
    575  );
    576  if (minutesSinceMidnight > 130) {
    577    ok(
    578      await downloadExists(publicList, "fakefile-today"),
    579      "'Today' download should still be present"
    580    );
    581  }
    582 
    583  downloadPromise = promiseDownloadRemoved(publicList);
    584  formHistoryPromise = promiseFormHistoryRemoved();
    585 
    586  // Clear 4 hours
    587  Services.prefs.setIntPref(Sanitizer.PREF_TIMESPAN, 3);
    588  await Sanitizer.sanitize(null, { ignoreTimespan: false });
    589 
    590  await formHistoryPromise;
    591  await downloadPromise;
    592 
    593  ok(
    594    !(await PlacesUtils.history.hasVisits("https://4hour.com")),
    595    "Pretend visit to 4hour.com should now be deleted"
    596  );
    597  ok(
    598    await PlacesUtils.history.hasVisits("https://4hour10minutes.com"),
    599    "Pretend visit to 4hour10minutes.com should should still exist"
    600  );
    601  if (hoursSinceMidnight > 4) {
    602    ok(
    603      await PlacesUtils.history.hasVisits("https://today.com"),
    604      "Pretend visit to today.com should still exist"
    605    );
    606  }
    607  ok(
    608    await PlacesUtils.history.hasVisits("https://before-today.com"),
    609    "Pretend visit to before-today.com should still exist"
    610  );
    611 
    612  await countEntries("4hour", "4hour form entry should be deleted", checkZero);
    613  await countEntries(
    614    "4hour10minutes",
    615    "4hour10minutes form entry should still exist",
    616    checkOne
    617  );
    618  if (hoursSinceMidnight > 4) {
    619    await countEntries(
    620      "today",
    621      "today form entry should still exist",
    622      checkOne
    623    );
    624  }
    625  await countEntries(
    626    "b4today",
    627    "b4today form entry should still exist",
    628    checkOne
    629  );
    630 
    631  ok(
    632    !(await downloadExists(publicList, "fakefile-4-hour")),
    633    "<4 hour old download should now be deleted"
    634  );
    635  ok(
    636    await downloadExists(publicList, "fakefile-4-hour-10-minutes"),
    637    "4 hour 10 minute download should still be present"
    638  );
    639  ok(
    640    await downloadExists(publicList, "fakefile-old"),
    641    "Year old download should still be present"
    642  );
    643  if (hoursSinceMidnight > 4) {
    644    ok(
    645      await downloadExists(publicList, "fakefile-today"),
    646      "'Today' download should still be present"
    647    );
    648  }
    649 
    650  downloadPromise = promiseDownloadRemoved(publicList);
    651  formHistoryPromise = promiseFormHistoryRemoved();
    652 
    653  // Clear 4 hours 10 minutes
    654  range = [now_uSec - 250 * 60 * 1000000, now_uSec];
    655  await Sanitizer.sanitize(null, { range, ignoreTimespan: false });
    656 
    657  await formHistoryPromise;
    658  await downloadPromise;
    659 
    660  ok(
    661    !(await PlacesUtils.history.hasVisits("https://4hour10minutes.com")),
    662    "Pretend visit to 4hour10minutes.com should now be deleted"
    663  );
    664  if (minutesSinceMidnight > 250) {
    665    ok(
    666      await PlacesUtils.history.hasVisits("https://today.com"),
    667      "Pretend visit to today.com should still exist"
    668    );
    669  }
    670  ok(
    671    await PlacesUtils.history.hasVisits("https://before-today.com"),
    672    "Pretend visit to before-today.com should still exist"
    673  );
    674 
    675  await countEntries(
    676    "4hour10minutes",
    677    "4hour10minutes form entry should be deleted",
    678    checkZero
    679  );
    680  if (minutesSinceMidnight > 250) {
    681    await countEntries(
    682      "today",
    683      "today form entry should still exist",
    684      checkOne
    685    );
    686  }
    687  await countEntries(
    688    "b4today",
    689    "b4today form entry should still exist",
    690    checkOne
    691  );
    692 
    693  ok(
    694    !(await downloadExists(publicList, "fakefile-4-hour-10-minutes")),
    695    "4 hour 10 minute download should now be deleted"
    696  );
    697  ok(
    698    await downloadExists(publicList, "fakefile-old"),
    699    "Year old download should still be present"
    700  );
    701  if (minutesSinceMidnight > 250) {
    702    ok(
    703      await downloadExists(publicList, "fakefile-today"),
    704      "'Today' download should still be present"
    705    );
    706  }
    707 
    708  // The 'Today' download might have been already deleted, in which case we
    709  // should not wait for a download removal notification.
    710  if (minutesSinceMidnight > 250) {
    711    downloadPromise = promiseDownloadRemoved(publicList);
    712    formHistoryPromise = promiseFormHistoryRemoved();
    713  } else {
    714    downloadPromise = formHistoryPromise = Promise.resolve();
    715  }
    716 
    717  // Clear Today
    718  Services.prefs.setIntPref(Sanitizer.PREF_TIMESPAN, 4);
    719  let progress = await Sanitizer.sanitize(null, { ignoreTimespan: false });
    720  Assert.deepEqual(progress, {
    721    history: "cleared",
    722    formdata: "cleared",
    723    downloads: "cleared",
    724  });
    725 
    726  await formHistoryPromise;
    727  await downloadPromise;
    728 
    729  // Be careful.  If we add our objectss just before midnight, and sanitize
    730  // runs immediately after, they won't be expired.  This is expected, but
    731  // we should not test in that case.  We cannot just test for opposite
    732  // condition because we could cross midnight just one moment after we
    733  // cache our time, then we would have an even worse random failure.
    734  var today = isToday(new Date(now_mSec));
    735  if (today) {
    736    ok(
    737      !(await PlacesUtils.history.hasVisits("https://today.com")),
    738      "Pretend visit to today.com should now be deleted"
    739    );
    740 
    741    await countEntries(
    742      "today",
    743      "today form entry should be deleted",
    744      checkZero
    745    );
    746    ok(
    747      !(await downloadExists(publicList, "fakefile-today")),
    748      "'Today' download should now be deleted"
    749    );
    750  }
    751 
    752  ok(
    753    await PlacesUtils.history.hasVisits("https://before-today.com"),
    754    "Pretend visit to before-today.com should still exist"
    755  );
    756  await countEntries(
    757    "b4today",
    758    "b4today form entry should still exist",
    759    checkOne
    760  );
    761  ok(
    762    await downloadExists(publicList, "fakefile-old"),
    763    "Year old download should still be present"
    764  );
    765 
    766  downloadPromise = promiseDownloadRemoved(publicList);
    767  formHistoryPromise = promiseFormHistoryRemoved();
    768 
    769  // Choose everything
    770  Services.prefs.setIntPref(Sanitizer.PREF_TIMESPAN, 0);
    771  await Sanitizer.sanitize(null, { ignoreTimespan: false });
    772 
    773  await formHistoryPromise;
    774  await downloadPromise;
    775 
    776  ok(
    777    !(await PlacesUtils.history.hasVisits("https://before-today.com")),
    778    "Pretend visit to before-today.com should now be deleted"
    779  );
    780 
    781  await countEntries(
    782    "b4today",
    783    "b4today form entry should be deleted",
    784    checkZero
    785  );
    786 
    787  ok(
    788    !(await downloadExists(publicList, "fakefile-old")),
    789    "Year old download should now be deleted"
    790  );
    791 }
    792 
    793 async function setupHistory() {
    794  let places = [];
    795 
    796  function addPlace(aURI, aTitle, aVisitDate) {
    797    places.push({
    798      uri: aURI,
    799      title: aTitle,
    800      visitDate: aVisitDate,
    801      transition: Ci.nsINavHistoryService.TRANSITION_LINK,
    802    });
    803  }
    804 
    805  addPlace(
    806    "https://10minutes.com/",
    807    "10 minutes ago",
    808    now_uSec - 10 * kUsecPerMin
    809  );
    810  addPlace(
    811    "https://1hour.com/",
    812    "Less than 1 hour ago",
    813    now_uSec - 45 * kUsecPerMin
    814  );
    815  addPlace(
    816    "https://1hour10minutes.com/",
    817    "1 hour 10 minutes ago",
    818    now_uSec - 70 * kUsecPerMin
    819  );
    820  addPlace(
    821    "https://2hour.com/",
    822    "Less than 2 hours ago",
    823    now_uSec - 90 * kUsecPerMin
    824  );
    825  addPlace(
    826    "https://2hour10minutes.com/",
    827    "2 hours 10 minutes ago",
    828    now_uSec - 130 * kUsecPerMin
    829  );
    830  addPlace(
    831    "https://4hour.com/",
    832    "Less than 4 hours ago",
    833    now_uSec - 180 * kUsecPerMin
    834  );
    835  addPlace(
    836    "https://4hour10minutes.com/",
    837    "4 hours 10 minutesago",
    838    now_uSec - 250 * kUsecPerMin
    839  );
    840 
    841  let today = new Date();
    842  today.setHours(0);
    843  today.setMinutes(0);
    844  today.setSeconds(0);
    845  today.setMilliseconds(1);
    846  addPlace("https://today.com/", "Today", today.getTime() * 1000);
    847 
    848  let lastYear = new Date();
    849  lastYear.setFullYear(lastYear.getFullYear() - 1);
    850  addPlace(
    851    "https://before-today.com/",
    852    "Before Today",
    853    lastYear.getTime() * 1000
    854  );
    855  await PlacesTestUtils.addVisits(places);
    856 }
    857 
    858 async function setupFormHistory() {
    859  function searchEntries(terms, params) {
    860    return FormHistory.search(terms, params);
    861  }
    862 
    863  // Make sure we've got a clean DB to start with, then add the entries we'll be testing.
    864  await FormHistory.update([
    865    {
    866      op: "remove",
    867    },
    868    {
    869      op: "add",
    870      fieldname: "10minutes",
    871      value: "10m",
    872    },
    873    {
    874      op: "add",
    875      fieldname: "1hour",
    876      value: "1h",
    877    },
    878    {
    879      op: "add",
    880      fieldname: "1hour10minutes",
    881      value: "1h10m",
    882    },
    883    {
    884      op: "add",
    885      fieldname: "2hour",
    886      value: "2h",
    887    },
    888    {
    889      op: "add",
    890      fieldname: "2hour10minutes",
    891      value: "2h10m",
    892    },
    893    {
    894      op: "add",
    895      fieldname: "4hour",
    896      value: "4h",
    897    },
    898    {
    899      op: "add",
    900      fieldname: "4hour10minutes",
    901      value: "4h10m",
    902    },
    903    {
    904      op: "add",
    905      fieldname: "today",
    906      value: "1d",
    907    },
    908    {
    909      op: "add",
    910      fieldname: "b4today",
    911      value: "1y",
    912    },
    913  ]);
    914 
    915  // Artifically age the entries to the proper vintage.
    916  let timestamp = now_uSec - 10 * kUsecPerMin;
    917  let results = await searchEntries(["guid"], { fieldname: "10minutes" });
    918  await FormHistory.update({
    919    op: "update",
    920    firstUsed: timestamp,
    921    guid: results[0].guid,
    922  });
    923 
    924  timestamp = now_uSec - 45 * kUsecPerMin;
    925  results = await searchEntries(["guid"], { fieldname: "1hour" });
    926  await FormHistory.update({
    927    op: "update",
    928    firstUsed: timestamp,
    929    guid: results[0].guid,
    930  });
    931 
    932  timestamp = now_uSec - 70 * kUsecPerMin;
    933  results = await searchEntries(["guid"], { fieldname: "1hour10minutes" });
    934  await FormHistory.update({
    935    op: "update",
    936    firstUsed: timestamp,
    937    guid: results[0].guid,
    938  });
    939 
    940  timestamp = now_uSec - 90 * kUsecPerMin;
    941  results = await searchEntries(["guid"], { fieldname: "2hour" });
    942  await FormHistory.update({
    943    op: "update",
    944    firstUsed: timestamp,
    945    guid: results[0].guid,
    946  });
    947 
    948  timestamp = now_uSec - 130 * kUsecPerMin;
    949  results = await searchEntries(["guid"], { fieldname: "2hour10minutes" });
    950  await FormHistory.update({
    951    op: "update",
    952    firstUsed: timestamp,
    953    guid: results[0].guid,
    954  });
    955 
    956  timestamp = now_uSec - 180 * kUsecPerMin;
    957  results = await searchEntries(["guid"], { fieldname: "4hour" });
    958  await FormHistory.update({
    959    op: "update",
    960    firstUsed: timestamp,
    961    guid: results[0].guid,
    962  });
    963 
    964  timestamp = now_uSec - 250 * kUsecPerMin;
    965  results = await searchEntries(["guid"], { fieldname: "4hour10minutes" });
    966  await FormHistory.update({
    967    op: "update",
    968    firstUsed: timestamp,
    969    guid: results[0].guid,
    970  });
    971 
    972  let today = new Date();
    973  today.setHours(0);
    974  today.setMinutes(0);
    975  today.setSeconds(0);
    976  today.setMilliseconds(1);
    977  timestamp = today.getTime() * 1000;
    978  results = await searchEntries(["guid"], { fieldname: "today" });
    979  await FormHistory.update({
    980    op: "update",
    981    firstUsed: timestamp,
    982    guid: results[0].guid,
    983  });
    984 
    985  let lastYear = new Date();
    986  lastYear.setFullYear(lastYear.getFullYear() - 1);
    987  timestamp = lastYear.getTime() * 1000;
    988  results = await searchEntries(["guid"], { fieldname: "b4today" });
    989  await FormHistory.update({
    990    op: "update",
    991    firstUsed: timestamp,
    992    guid: results[0].guid,
    993  });
    994 
    995  var checks = 0;
    996  let checkOne = function (num, message) {
    997    is(num, 1, message);
    998    checks++;
    999  };
   1000 
   1001  // Sanity check.
   1002  await countEntries(
   1003    "10minutes",
   1004    "Checking for 10minutes form history entry creation",
   1005    checkOne
   1006  );
   1007  await countEntries(
   1008    "1hour",
   1009    "Checking for 1hour form history entry creation",
   1010    checkOne
   1011  );
   1012  await countEntries(
   1013    "1hour10minutes",
   1014    "Checking for 1hour10minutes form history entry creation",
   1015    checkOne
   1016  );
   1017  await countEntries(
   1018    "2hour",
   1019    "Checking for 2hour form history entry creation",
   1020    checkOne
   1021  );
   1022  await countEntries(
   1023    "2hour10minutes",
   1024    "Checking for 2hour10minutes form history entry creation",
   1025    checkOne
   1026  );
   1027  await countEntries(
   1028    "4hour",
   1029    "Checking for 4hour form history entry creation",
   1030    checkOne
   1031  );
   1032  await countEntries(
   1033    "4hour10minutes",
   1034    "Checking for 4hour10minutes form history entry creation",
   1035    checkOne
   1036  );
   1037  await countEntries(
   1038    "today",
   1039    "Checking for today form history entry creation",
   1040    checkOne
   1041  );
   1042  await countEntries(
   1043    "b4today",
   1044    "Checking for b4today form history entry creation",
   1045    checkOne
   1046  );
   1047  is(checks, 9, "9 checks made");
   1048 }
   1049 
   1050 async function setupDownloads() {
   1051  let publicList = await Downloads.getList(Downloads.PUBLIC);
   1052 
   1053  let download = await Downloads.createDownload({
   1054    source: "https://bugzilla.mozilla.org/show_bug.cgi?id=480169",
   1055    target: "fakefile-10-minutes",
   1056  });
   1057  download.startTime = new Date(now_mSec - 10 * kMsecPerMin); // 10 minutes ago
   1058  download.canceled = true;
   1059  await publicList.add(download);
   1060 
   1061  download = await Downloads.createDownload({
   1062    source: "https://bugzilla.mozilla.org/show_bug.cgi?id=453440",
   1063    target: "fakefile-1-hour",
   1064  });
   1065  download.startTime = new Date(now_mSec - 45 * kMsecPerMin); // 45 minutes ago
   1066  download.canceled = true;
   1067  await publicList.add(download);
   1068 
   1069  download = await Downloads.createDownload({
   1070    source: "https://bugzilla.mozilla.org/show_bug.cgi?id=480169",
   1071    target: "fakefile-1-hour-10-minutes",
   1072  });
   1073  download.startTime = new Date(now_mSec - 70 * kMsecPerMin); // 70 minutes ago
   1074  download.canceled = true;
   1075  await publicList.add(download);
   1076 
   1077  download = await Downloads.createDownload({
   1078    source: "https://bugzilla.mozilla.org/show_bug.cgi?id=453440",
   1079    target: "fakefile-2-hour",
   1080  });
   1081  download.startTime = new Date(now_mSec - 90 * kMsecPerMin); // 90 minutes ago
   1082  download.canceled = true;
   1083  await publicList.add(download);
   1084 
   1085  download = await Downloads.createDownload({
   1086    source: "https://bugzilla.mozilla.org/show_bug.cgi?id=480169",
   1087    target: "fakefile-2-hour-10-minutes",
   1088  });
   1089  download.startTime = new Date(now_mSec - 130 * kMsecPerMin); // 130 minutes ago
   1090  download.canceled = true;
   1091  await publicList.add(download);
   1092 
   1093  download = await Downloads.createDownload({
   1094    source: "https://bugzilla.mozilla.org/show_bug.cgi?id=453440",
   1095    target: "fakefile-4-hour",
   1096  });
   1097  download.startTime = new Date(now_mSec - 180 * kMsecPerMin); // 180 minutes ago
   1098  download.canceled = true;
   1099  await publicList.add(download);
   1100 
   1101  download = await Downloads.createDownload({
   1102    source: "https://bugzilla.mozilla.org/show_bug.cgi?id=480169",
   1103    target: "fakefile-4-hour-10-minutes",
   1104  });
   1105  download.startTime = new Date(now_mSec - 250 * kMsecPerMin); // 250 minutes ago
   1106  download.canceled = true;
   1107  await publicList.add(download);
   1108 
   1109  // Add "today" download
   1110  let today = new Date();
   1111  today.setHours(0);
   1112  today.setMinutes(0);
   1113  today.setSeconds(0);
   1114  today.setMilliseconds(1);
   1115 
   1116  download = await Downloads.createDownload({
   1117    source: "https://bugzilla.mozilla.org/show_bug.cgi?id=453440",
   1118    target: "fakefile-today",
   1119  });
   1120  download.startTime = today; // 12:00:01 AM this morning
   1121  download.canceled = true;
   1122  await publicList.add(download);
   1123 
   1124  // Add "before today" download
   1125  let lastYear = new Date();
   1126  lastYear.setFullYear(lastYear.getFullYear() - 1);
   1127 
   1128  download = await Downloads.createDownload({
   1129    source: "https://bugzilla.mozilla.org/show_bug.cgi?id=453440",
   1130    target: "fakefile-old",
   1131  });
   1132  download.startTime = lastYear;
   1133  download.canceled = true;
   1134  await publicList.add(download);
   1135 
   1136  // Confirm everything worked
   1137  let downloads = await publicList.getAll();
   1138  is(downloads.length, 9, "9 Pretend downloads added");
   1139 
   1140  ok(
   1141    await downloadExists(publicList, "fakefile-old"),
   1142    "Pretend download for everything case should exist"
   1143  );
   1144  ok(
   1145    await downloadExists(publicList, "fakefile-10-minutes"),
   1146    "Pretend download for 10-minutes case should exist"
   1147  );
   1148  ok(
   1149    await downloadExists(publicList, "fakefile-1-hour"),
   1150    "Pretend download for 1-hour case should exist"
   1151  );
   1152  ok(
   1153    await downloadExists(publicList, "fakefile-1-hour-10-minutes"),
   1154    "Pretend download for 1-hour-10-minutes case should exist"
   1155  );
   1156  ok(
   1157    await downloadExists(publicList, "fakefile-2-hour"),
   1158    "Pretend download for 2-hour case should exist"
   1159  );
   1160  ok(
   1161    await downloadExists(publicList, "fakefile-2-hour-10-minutes"),
   1162    "Pretend download for 2-hour-10-minutes case should exist"
   1163  );
   1164  ok(
   1165    await downloadExists(publicList, "fakefile-4-hour"),
   1166    "Pretend download for 4-hour case should exist"
   1167  );
   1168  ok(
   1169    await downloadExists(publicList, "fakefile-4-hour-10-minutes"),
   1170    "Pretend download for 4-hour-10-minutes case should exist"
   1171  );
   1172  ok(
   1173    await downloadExists(publicList, "fakefile-today"),
   1174    "Pretend download for Today case should exist"
   1175  );
   1176 }
   1177 
   1178 /**
   1179 * Checks to see if the downloads with the specified id exists.
   1180 *
   1181 * @param aID
   1182 *        The ids of the downloads to check.
   1183 */
   1184 let downloadExists = async function (list, path) {
   1185  let listArray = await list.getAll();
   1186  return listArray.some(i => i.target.path == path);
   1187 };
   1188 
   1189 function isToday(aDate) {
   1190  return aDate.getDate() == new Date().getDate();
   1191 }