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

// Tests the origin-related SQL functions, which are:
// * get_host_and_port
// * get_prefix
// * strip_prefix_and_userinfo

// Tests actual URL strings.
add_task(async function urls() {
  let sets = [
    ["http:"],
    ["", "//"],
    ["", "user@", "user:@", "user:pass@", "user:pass:word@"],
    ["example.com"],
    ["", ":8888"],
    ["", "/", "/foo"],
    ["", "?", "?bar"],
    ["", "#", "#baz"],
  ];
  let db = await PlacesUtils.promiseDBConnection();
  for (let parts of permute(sets)) {
    let spec = parts.join("");
    let funcs = {
      get_prefix: parts.slice(0, 2).join(""),
      get_host_and_port: parts.slice(3, 5).join(""),
      strip_prefix_and_userinfo: parts.slice(3).join(""),
    };
    for (let [func, expectedValue] of Object.entries(funcs)) {
      let rows = await db.execute(`
        SELECT ${func}("${spec}");
      `);
      let value = rows[0].getString(0);
      Assert.equal(value, expectedValue, `function=${func} spec="${spec}"`);
    }
  }
});

// Tests strings that aren't URLs.
add_task(async function nonURLs() {
  let db = await PlacesUtils.promiseDBConnection();

  let value = (
    await db.execute(`
    SELECT get_prefix("hello");
  `)
  )[0].getString(0);
  Assert.equal(value, "");

  value = (
    await db.execute(`
    SELECT get_host_and_port("hello");
  `)
  )[0].getString(0);
  Assert.equal(value, "hello");

  value = (
    await db.execute(`
    SELECT strip_prefix_and_userinfo("hello");
  `)
  )[0].getString(0);
  Assert.equal(value, "hello");
});

function permute(sets = []) {
  if (!sets.length) {
    return [[]];
  }
  let firstSet = sets[0];
  let otherSets = sets.slice(1);
  let permutedSequences = [];
  let otherPermutedSequences = permute(otherSets);
  for (let other of otherPermutedSequences) {
    for (let value of firstSet) {
      permutedSequences.push([value].concat(other));
    }
  }
  return permutedSequences;
}