summaryrefslogtreecommitdiffstats
path: root/browser/components/migration/tests/unit/test_Safari_history_strange_entries.js
blob: a22e6e1655f1038a6ea109af86140679b14778ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const { PlacesQuery } = ChromeUtils.importESModule(
  "resource://gre/modules/PlacesQuery.sys.mjs"
);

const HISTORY_FILE_PATH = "Library/Safari/History.db";
const HISTORY_STRANGE_ENTRIES_FILE_PATH =
  "Library/Safari/HistoryStrangeEntries.db";

// By default, our migrators will cut off migrating any history older than
// 180 days. In order to make sure this test continues to run correctly
// in the future, we copy the reference database to History.db, and then
// use Sqlite.sys.mjs to connect to it and manually update all of the visit
// times to be "now", so that they all fall within the 180 day window. The
// Nov 10th date below is right around when the reference database visit
// entries were created.
//
// This update occurs in `updateVisitTimes`.
const MS_SINCE_SNAPSHOT_TIME =
  new Date() - new Date("Nov 10, 2022 00:00:00 UTC");

async function setupHistoryFile() {
  removeHistoryFile();
  let file = do_get_file(HISTORY_STRANGE_ENTRIES_FILE_PATH);
  file.copyTo(file.parent, "History.db");
  await updateVisitTimes();
}

function removeHistoryFile() {
  let file = do_get_file(HISTORY_FILE_PATH, true);
  try {
    file.remove(false);
  } catch (ex) {
    // It is ok if this doesn't exist.
    if (ex.result != Cr.NS_ERROR_FILE_NOT_FOUND) {
      throw ex;
    }
  }
}

add_setup(async function setup() {
  registerFakePath("ULibDir", do_get_file("Library/"));
  await setupHistoryFile();
  registerCleanupFunction(async () => {
    await PlacesUtils.history.clear();
    removeHistoryFile();
  });
});

async function updateVisitTimes() {
  let cocoaSnapshotDelta = MS_SINCE_SNAPSHOT_TIME / 1000;
  let historyFile = do_get_file(HISTORY_FILE_PATH);
  let dbConn = await Sqlite.openConnection({ path: historyFile.path });

  await dbConn.execute(
    "UPDATE history_visits SET visit_time = visit_time + :cocoaSnapshotDelta;",
    {
      cocoaSnapshotDelta,
    }
  );

  await dbConn.close();
}

/**
 * Tests that we can import successfully from Safari when Safari's history
 * database contains malformed URLs.
 */
add_task(async function testHistoryImportStrangeEntries() {
  await PlacesUtils.history.clear();

  let placesQuery = new PlacesQuery();
  let emptyHistory = await placesQuery.getHistory({ daysOld: Infinity });
  Assert.equal(emptyHistory.size, 0, "Empty history should indeed be empty.");

  const EXPECTED_MIGRATED_SITES = 10;
  const EXPECTED_MIGRATED_VISTS = 23;

  let historyFile = do_get_file(HISTORY_FILE_PATH);
  let dbConn = await Sqlite.openConnection({ path: historyFile.path });
  let [rowCountResult] = await dbConn.execute(
    "SELECT COUNT(*) FROM history_visits"
  );
  Assert.greater(
    rowCountResult.getResultByName("COUNT(*)"),
    EXPECTED_MIGRATED_VISTS,
    "There are more total rows than valid rows"
  );
  await dbConn.close();

  let migrator = await MigrationUtils.getMigrator("safari");
  await promiseMigration(migrator, MigrationUtils.resourceTypes.HISTORY);
  let migratedHistory = await placesQuery.getHistory({
    daysOld: Infinity,
    sortBy: "site",
  });
  let siteCount = migratedHistory.size;
  let visitCount = 0;
  for (let [, visits] of migratedHistory) {
    visitCount += visits.length;
  }
  Assert.equal(
    siteCount,
    EXPECTED_MIGRATED_SITES,
    "Should have migrated all valid history sites"
  );
  Assert.equal(
    visitCount,
    EXPECTED_MIGRATED_VISTS,
    "Should have migrated all valid history visits"
  );

  placesQuery.close();
});