summaryrefslogtreecommitdiffstats
path: root/toolkit/components/places/tests/unit/test_origins_parsing.js
blob: 35ba8bdd0d29805d87b7aac644cbe90346e59e38 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

// This test is a companion to test_origins.js.  It adds many URLs to the
// database and makes sure that their prefixes and hosts are correctly parsed.
// This test can take a while to run, which is why it's split out from
// test_origins.js.

"use strict";

add_task(async function parsing() {
  let prefixes = ["http://", "https://", "ftp://", "foo://", "bar:"];

  let userinfos = ["", "user:pass@", "user:pass:word@", "user:@"];

  let ports = ["", ":8888"];

  let paths = [
    "",

    "/",
    "/1",
    "/1/2",

    "?",
    "?1",
    "#",
    "#1",

    "/?",
    "/1?",
    "/?1",
    "/1?2",

    "/#",
    "/1#",
    "/#1",
    "/1#2",

    "/?#",
    "/1?#",
    "/?1#",
    "/?#1",
    "/1?2#",
    "/1?#2",
    "/?1#2",
  ];

  for (let userinfo of userinfos) {
    for (let port of ports) {
      for (let path of paths) {
        info(`Testing userinfo='${userinfo}' port='${port}' path='${path}'`);
        let expectedOrigins = prefixes.map(prefix => [
          prefix,
          "example.com" + port,
        ]);
        let uris = expectedOrigins.map(
          ([prefix, hostPort]) => prefix + userinfo + hostPort + path
        );

        await PlacesTestUtils.addVisits(uris.map(uri => ({ uri })));
        await checkDB(expectedOrigins);

        // Remove each URI, one at a time, and make sure the remaining origins
        // in the database are correct.
        for (let i = 0; i < uris.length; i++) {
          await PlacesUtils.history.remove(uris[i]);
          await checkDB(expectedOrigins.slice(i + 1, expectedOrigins.length));
        }
        await cleanUp();
      }
    }
  }
  await checkDB([]);
});

/**
 * Asserts that the moz_origins table is correct.
 *
 * @param expectedOrigins
 *        An array of expected origins.  Each origin in the array is itself an
 *        array that looks like this: [prefix, host]
 */
async function checkDB(expectedOrigins) {
  let db = await PlacesUtils.promiseDBConnection();
  let rows = await db.execute(`
    SELECT prefix, host
    FROM moz_origins
    ORDER BY id ASC
  `);
  let actualOrigins = rows.map(row => {
    let o = [];
    for (let c = 0; c < 2; c++) {
      o.push(row.getResultByIndex(c));
    }
    return o;
  });
  Assert.deepEqual(actualOrigins, expectedOrigins);
}

async function cleanUp() {
  await PlacesUtils.bookmarks.eraseEverything();
  await PlacesUtils.history.clear();
}