tor-browser

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

test_Chrome_history.js (5333B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 const { ChromeMigrationUtils } = ChromeUtils.importESModule(
      7  "resource:///modules/ChromeMigrationUtils.sys.mjs"
      8 );
      9 
     10 const SOURCE_PROFILE_DIR = "Library/Application Support/Google/Chrome/Default/";
     11 
     12 const PROFILE = {
     13  id: "Default",
     14  name: "Person 1",
     15 };
     16 
     17 /**
     18 * TEST_URLS reflects the data stored in '${SOURCE_PROFILE_DIR}HistoryMaster'.
     19 * The main object reflects the data in the 'urls' table. The visits property
     20 * reflects the associated data in the 'visits' table.
     21 */
     22 const TEST_URLS = [
     23  {
     24    id: 1,
     25    url: "http://example.com/",
     26    title: "test",
     27    visit_count: 1,
     28    typed_count: 0,
     29    last_visit_time: 13193151310368000,
     30    hidden: 0,
     31    visits: [
     32      {
     33        id: 1,
     34        url: 1,
     35        visit_time: 13193151310368000,
     36        from_visit: 0,
     37        transition: 805306370,
     38        segment_id: 0,
     39        visit_duration: 10745006,
     40        incremented_omnibox_typed_score: 0,
     41      },
     42    ],
     43  },
     44  {
     45    id: 2,
     46    url: "http://invalid.com/",
     47    title: "test2",
     48    visit_count: 1,
     49    typed_count: 0,
     50    last_visit_time: 13193154948901000,
     51    hidden: 0,
     52    visits: [
     53      {
     54        id: 2,
     55        url: 2,
     56        visit_time: 13193154948901000,
     57        from_visit: 0,
     58        transition: 805306376,
     59        segment_id: 0,
     60        visit_duration: 6568270,
     61        incremented_omnibox_typed_score: 0,
     62      },
     63    ],
     64  },
     65 ];
     66 
     67 async function setVisitTimes(time) {
     68  let loginDataFile = do_get_file(`${SOURCE_PROFILE_DIR}History`);
     69  let dbConn = await Sqlite.openConnection({ path: loginDataFile.path });
     70 
     71  await dbConn.execute(`UPDATE urls SET last_visit_time = :last_visit_time`, {
     72    last_visit_time: time,
     73  });
     74  await dbConn.execute(`UPDATE visits SET visit_time = :visit_time`, {
     75    visit_time: time,
     76  });
     77 
     78  await dbConn.close();
     79 }
     80 
     81 function setExpectedVisitTimes(time) {
     82  for (let urlInfo of TEST_URLS) {
     83    urlInfo.last_visit_time = time;
     84    urlInfo.visits[0].visit_time = time;
     85  }
     86 }
     87 
     88 function assertEntryMatches(entry, urlInfo, dateWasInFuture = false) {
     89  info(`Checking url: ${urlInfo.url}`);
     90  Assert.ok(entry, `Should have stored an entry`);
     91 
     92  Assert.equal(entry.url, urlInfo.url, "Should have the correct URL");
     93  Assert.equal(entry.title, urlInfo.title, "Should have the correct title");
     94  Assert.equal(
     95    entry.visits.length,
     96    urlInfo.visits.length,
     97    "Should have the correct number of visits"
     98  );
     99 
    100  for (let index in urlInfo.visits) {
    101    Assert.equal(
    102      entry.visits[index].transition,
    103      PlacesUtils.history.TRANSITIONS.LINK,
    104      "Should have Link type transition"
    105    );
    106 
    107    if (dateWasInFuture) {
    108      Assert.lessOrEqual(
    109        entry.visits[index].date.getTime(),
    110        new Date().getTime(),
    111        "Should have moved the date to no later than the current date."
    112      );
    113    } else {
    114      Assert.equal(
    115        entry.visits[index].date.getTime(),
    116        ChromeMigrationUtils.chromeTimeToDate(
    117          urlInfo.visits[index].visit_time,
    118          new Date()
    119        ).getTime(),
    120        "Should have the correct date"
    121      );
    122    }
    123  }
    124 }
    125 
    126 function setupHistoryFile() {
    127  removeHistoryFile();
    128  let file = do_get_file(`${SOURCE_PROFILE_DIR}HistoryMaster`);
    129  file.copyTo(file.parent, "History");
    130 }
    131 
    132 function removeHistoryFile() {
    133  let file = do_get_file(`${SOURCE_PROFILE_DIR}History`, true);
    134  try {
    135    file.remove(false);
    136  } catch (ex) {
    137    // It is ok if this doesn't exist.
    138    if (ex.result != Cr.NS_ERROR_FILE_NOT_FOUND) {
    139      throw ex;
    140    }
    141  }
    142 }
    143 
    144 add_task(async function setup() {
    145  registerFakePath("ULibDir", do_get_file("Library/"));
    146 
    147  registerCleanupFunction(async () => {
    148    await PlacesUtils.history.clear();
    149    removeHistoryFile();
    150  });
    151 });
    152 
    153 add_task(async function test_import() {
    154  setupHistoryFile();
    155  await PlacesUtils.history.clear();
    156  // Update to ~10 days ago since the date can't be too old or Places may expire it.
    157  const pastDate = new Date(new Date().getTime() - 1000 * 60 * 60 * 24 * 10);
    158  const pastChromeTime = ChromeMigrationUtils.dateToChromeTime(pastDate);
    159  await setVisitTimes(pastChromeTime);
    160  setExpectedVisitTimes(pastChromeTime);
    161 
    162  let migrator = await MigrationUtils.getMigrator("chrome");
    163  Assert.ok(
    164    await migrator.isSourceAvailable(),
    165    "Sanity check the source exists"
    166  );
    167 
    168  await promiseMigration(
    169    migrator,
    170    MigrationUtils.resourceTypes.HISTORY,
    171    PROFILE
    172  );
    173 
    174  for (let urlInfo of TEST_URLS) {
    175    let entry = await PlacesUtils.history.fetch(urlInfo.url, {
    176      includeVisits: true,
    177    });
    178    assertEntryMatches(entry, urlInfo);
    179  }
    180 });
    181 
    182 add_task(async function test_import_future_date() {
    183  setupHistoryFile();
    184  await PlacesUtils.history.clear();
    185  const futureDate = new Date().getTime() + 6000 * 60 * 24;
    186  await setVisitTimes(ChromeMigrationUtils.dateToChromeTime(futureDate));
    187 
    188  let migrator = await MigrationUtils.getMigrator("chrome");
    189  Assert.ok(
    190    await migrator.isSourceAvailable(),
    191    "Sanity check the source exists"
    192  );
    193 
    194  await promiseMigration(
    195    migrator,
    196    MigrationUtils.resourceTypes.HISTORY,
    197    PROFILE
    198  );
    199 
    200  for (let urlInfo of TEST_URLS) {
    201    let entry = await PlacesUtils.history.fetch(urlInfo.url, {
    202      includeVisits: true,
    203    });
    204    assertEntryMatches(entry, urlInfo, true);
    205  }
    206 });