summaryrefslogtreecommitdiffstats
path: root/toolkit/modules/tests/xpcshell/test_BrowserUtils_urlFormatting.js
blob: c7945cdb9c1fb002dbb7059dbc9c75d6d92f6270 (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

do_get_profile();

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

let tempFile = new FileUtils.File(PathUtils.tempDir);
const TEST_LOCAL_FILE_NAME = "hello.txt";
tempFile.append(TEST_LOCAL_FILE_NAME);

const gL10n = new Localization(["toolkit/global/browser-utils.ftl"], true);
const DATA_URL_EXPECTED_STRING = gL10n.formatValueSync(
  "browser-utils-url-data"
);
const EXTENSION_NAME = "My Test Extension";
const EXTENSION_URL_EXPECTED_STRING = gL10n.formatValueSync(
  "browser-utils-url-extension",
  { extension: EXTENSION_NAME }
);

const { AddonTestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/AddonTestUtils.sys.mjs"
);

const { ExtensionTestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/ExtensionXPCShellUtils.sys.mjs"
);

AddonTestUtils.init(this);
ExtensionTestUtils.init(this);

// Allow for unsigned addons.
AddonTestUtils.overrideCertDB();

AddonTestUtils.createAppInfo(
  "xpcshell@tests.mozilla.org",
  "XPCShell",
  "42",
  "42"
);

// Filled in in setup, shared state with the various test tasks so declared here.
let addonBaseURI;

// http/https tests first. These are split out from TESTS for the benefit of reuse by the
// blob: tests further down.
const HTTP_TESTS = [
  // Simple http/https examples with/without `www.`.
  {
    input: "https://example.com",
    output: "example.com",
  },
  {
    input: "https://example.com/path/?query#hash",
    output: "example.com",
  },
  {
    input: "https://www.example.com",
    output: "example.com",
  },
  {
    input: "https://www.example.com/path/?query#hash",
    output: "example.com",
  },
  {
    input: "http://example.com",
    output: "example.com",
  },
  {
    input: "http://www.example.com",
    output: "example.com",
  },

  // We shouldn't drop `www.` if that's the domain:
  {
    input: "https://www.com",
    output: "www.com",
  },

  // Multilevel TLDs should work:
  {
    input: "https://www.example.co.uk",
    output: "example.co.uk",
  },
  {
    input: "https://www.co.uk",
    output: "www.co.uk",
  },

  // Other sudomains should be kept:
  {
    input: "https://webmail.example.co.uk",
    output: "webmail.example.co.uk",
  },
  {
    input: "https://webmail.example.com",
    output: "webmail.example.com",
  },

  // IP addresses should work:
  {
    input: "http://[::1]/foo/bar?baz=bax#quux",
    output: "[::1]",
  },
  {
    input: "http://127.0.0.1/foo/bar?baz=bax#quux",
    output: "127.0.0.1",
  },
];

const TESTS = [
  ...HTTP_TESTS,

  // about URIs:
  {
    input: "about:config",
    output: "about:config",
  },
  {
    input: "about:config?foo#bar",
    output: "about:config",
  },

  // file URI directory:
  {
    input: Services.io.newFileURI(new FileUtils.File(PathUtils.tempDir)).spec,
    output: PathUtils.filename(PathUtils.tempDir),
  },
  // file URI directory that ends in slash:
  {
    input:
      Services.io.newFileURI(new FileUtils.File(PathUtils.tempDir)).spec + "/",
    output: PathUtils.filename(PathUtils.tempDir),
  },
  // file URI individual file:
  {
    input: Services.io.newFileURI(tempFile).spec,
    output: tempFile.leafName,
  },

  // As above but for chrome URIs:
  {
    input: "chrome://global/content/blah",
    output: "blah",
  },
  {
    input: "chrome://global/content/blah//",
    output: "blah",
  },
  {
    input: "chrome://global/content/foo.txt",
    output: "foo.txt",
  },

  // Also check data URIs:
  {
    input: "data:text/html,42",
    output: DATA_URL_EXPECTED_STRING,
  },
];

add_setup(async () => {
  const testExtensionData = {
    useAddonManager: "temporary",
    manifest: {
      browser_specific_settings: { gecko: { id: "myextension@example.com" } },
      name: EXTENSION_NAME,
    },
  };

  const testNoNameExtensionData = {
    useAddonManager: "temporary",
    manifest: {
      browser_specific_settings: { gecko: { id: "noname@example.com" } },
      name: "",
    },
  };

  await AddonTestUtils.promiseStartupManager();
  const extension = ExtensionTestUtils.loadExtension(testExtensionData);
  const noNameExtension = ExtensionTestUtils.loadExtension(
    testNoNameExtensionData
  );
  await extension.startup();
  await noNameExtension.startup();

  addonBaseURI = extension.extension.baseURI;
  let noNameAddonBaseURI = noNameExtension.extension.baseURI;

  TESTS.push(
    {
      input: addonBaseURI.spec,
      output: EXTENSION_URL_EXPECTED_STRING,
    },
    {
      input: "moz-extension://blah/",
      output: gL10n.formatValueSync("browser-utils-url-extension", {
        extension: "moz-extension://blah/",
      }),
    },
    {
      input: noNameAddonBaseURI.spec,
      output: gL10n.formatValueSync("browser-utils-url-extension", {
        extension: noNameAddonBaseURI.spec,
      }),
    }
  );

  registerCleanupFunction(async () => {
    await extension.unload();
    await noNameExtension.unload();
  });
});

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

add_task(async function test_checkStringFormatting() {
  for (let { input, output } of TESTS) {
    Assert.equal(
      BrowserUtils.formatURIStringForDisplay(input),
      output,
      `String ${input} formatted for output should match`
    );
  }
});

add_task(async function test_checkURIFormatting() {
  for (let { input, output } of TESTS) {
    let uri = Services.io.newURI(input);
    Assert.equal(
      BrowserUtils.formatURIForDisplay(uri),
      output,
      `URI ${input} formatted for output should match`
    );
  }
});

add_task(async function test_checkViewSourceFormatting() {
  for (let { input, output } of HTTP_TESTS) {
    Assert.equal(
      BrowserUtils.formatURIStringForDisplay("view-source:" + input),
      output,
      `String view-source:${input} formatted for output should match`
    );
    let uri = Services.io.newURI("view-source:" + input);
    Assert.equal(
      BrowserUtils.formatURIForDisplay(uri),
      output,
      `URI view-source:${input} formatted for output should match`
    );
  }
});

function createBlobURLWithSandbox(origin) {
  let sb = new Cu.Sandbox(origin, { wantGlobalProperties: ["Blob", "URL"] });
  // Need to pass 'false' for the validate filename param or this throws
  // exceptions. I'm not sure why...
  return Cu.evalInSandbox(
    'URL.createObjectURL(new Blob(["text"], { type: "text/plain" }))',
    sb,
    "",
    null,
    0,
    false
  );
}

add_task(async function test_checkBlobURIs() {
  // These don't just live in the TESTS array because creating a valid
  // blob URI is a bit more involved...
  let blob = new Blob(["test"], { type: "text/plain" });
  let url = URL.createObjectURL(blob);
  Assert.equal(
    BrowserUtils.formatURIStringForDisplay(url),
    DATA_URL_EXPECTED_STRING,
    `Blob url string without origin should be represented as (data)`
  );

  // Now with a null principal:
  url = createBlobURLWithSandbox(null);
  Assert.equal(
    BrowserUtils.formatURIStringForDisplay(url),
    DATA_URL_EXPECTED_STRING,
    `Blob url string with null principal origin should be represented as (data)`
  );

  // And some valid url principals:
  let BLOB_TESTS = [
    {
      input: addonBaseURI.spec,
      output: EXTENSION_URL_EXPECTED_STRING,
    },
  ].concat(HTTP_TESTS);
  for (let { input, output } of BLOB_TESTS) {
    url = createBlobURLWithSandbox(input);
    Assert.equal(
      BrowserUtils.formatURIStringForDisplay(url),
      output,
      `Blob url string with principal from ${input} should show principal URI`
    );
  }
});