summaryrefslogtreecommitdiffstats
path: root/caps/tests/unit/test_oa_partitionKey_pattern.js
blob: 5d0625018f2a6a3cfd24ba6740179c12e53fcd81 (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

/**
 * Tests origin attributes partitionKey pattern matching.
 */

"use strict";

function testMatch(oa, pattern, shouldMatch = true) {
  let msg = `Origin attributes should ${
    shouldMatch ? "match" : "not match"
  } pattern.`;
  msg += ` oa: ${JSON.stringify(oa)} - pattern: ${JSON.stringify(pattern)}`;
  Assert.equal(
    ChromeUtils.originAttributesMatchPattern(oa, pattern),
    shouldMatch,
    msg
  );
}

function getPartitionKey(scheme, baseDomain, port) {
  if (!scheme || !baseDomain) {
    return "";
  }
  return `(${scheme},${baseDomain}${port ? `,${port}` : ``})`;
}

function getOAWithPartitionKey(scheme, baseDomain, port, oa = {}) {
  oa.partitionKey = getPartitionKey(scheme, baseDomain, port);
  return oa;
}

/**
 * Tests that an OriginAttributesPattern which is empty or only has an empty
 * partitionKeyPattern matches any partitionKey.
 */
add_task(async function test_empty_pattern_matches_any() {
  let list = [
    getOAWithPartitionKey("https", "example.com"),
    getOAWithPartitionKey("http", "example.net", 8080),
    getOAWithPartitionKey(),
  ];

  for (let oa of list) {
    testMatch(oa, {});
    testMatch(oa, { partitionKeyPattern: {} });
  }
});

/**
 * Tests that if a partitionKeyPattern is passed, but the partitionKey is
 * invalid, the pattern match will always fail.
 */
add_task(async function test_invalid_pk() {
  let list = [
    "()",
    "(,,)",
    "(https)",
    "(https,,)",
    "(example.com)",
    "(http,example.com,invalid)",
    "(http,example.com,8000,1000)",
  ].map(partitionKey => ({ partitionKey }));

  for (let oa of list) {
    testMatch(oa, {});
    testMatch(oa, { partitionKeyPattern: {} });
    testMatch(
      oa,
      { partitionKeyPattern: { baseDomain: "example.com" } },
      false
    );
    testMatch(oa, { partitionKeyPattern: { scheme: "https" } }, false);
  }
});

/**
 * Tests that if a pattern sets "partitionKey" it takes precedence over "partitionKeyPattern".
 */
add_task(async function test_string_overwrites_pattern() {
  let oa = getOAWithPartitionKey("https", "example.com", 8080, {
    userContextId: 2,
  });

  testMatch(oa, { partitionKey: oa.partitionKey });
  testMatch(oa, {
    partitionKey: oa.partitionKey,
    partitionKeyPattern: { baseDomain: "example.com" },
  });
  testMatch(oa, {
    partitionKey: oa.partitionKey,
    partitionKeyPattern: { baseDomain: "example.net" },
  });
  testMatch(
    oa,
    {
      partitionKey: getPartitionKey("https", "example.net"),
      partitionKeyPattern: { scheme: "https", baseDomain: "example.com" },
    },
    false
  );
});

/**
 * Tests that we can match parts of a partitionKey by setting
 * partitionKeyPattern.
 */
add_task(async function test_pattern() {
  let a = getOAWithPartitionKey("https", "example.com", 8080, {
    userContextId: 2,
  });
  let b = getOAWithPartitionKey("https", "example.com", undefined, {
    privateBrowsingId: 1,
  });

  for (let oa of [a, b]) {
    // Match
    testMatch(oa, { partitionKeyPattern: { scheme: "https" } });
    testMatch(oa, {
      partitionKeyPattern: { scheme: "https", baseDomain: "example.com" },
    });
    testMatch(
      oa,
      {
        partitionKeyPattern: {
          scheme: "https",
          baseDomain: "example.com",
          port: 8080,
        },
      },
      oa == a
    );
    testMatch(oa, {
      partitionKeyPattern: { baseDomain: "example.com" },
    });
    testMatch(
      oa,
      {
        partitionKeyPattern: { port: 8080 },
      },
      oa == a
    );

    // Mismatch
    testMatch(oa, { partitionKeyPattern: { scheme: "http" } }, false);
    testMatch(
      oa,
      { partitionKeyPattern: { baseDomain: "example.net" } },
      false
    );
    testMatch(oa, { partitionKeyPattern: { port: 8443 } }, false);
    testMatch(
      oa,
      { partitionKeyPattern: { scheme: "https", baseDomain: "example.net" } },
      false
    );
  }
});