summaryrefslogtreecommitdiffstats
path: root/devtools/client/shared/test/xpcshell/test_source-utils.js
blob: dec32eb5317fb20aabf81bfc8c73c054d45f58f5 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

/**
 * Tests utility functions contained in `source-utils.js`
 */

const sourceUtils = require("resource://devtools/client/shared/source-utils.js");

const CHROME_URLS = [
  "chrome://foo",
  "resource://baz",
  "jar:file:///Users/root",
];

const CONTENT_URLS = [
  "http://mozilla.org",
  "https://mozilla.org",
  "file:///Users/root",
  "app://fxosapp",
  "blob:http://mozilla.org",
  "blob:https://mozilla.org",
];

// Test `sourceUtils.parseURL`
add_task(async function () {
  let parsed = sourceUtils.parseURL("https://foo.com:8888/boo/bar.js?q=query");
  equal(parsed.fileName, "bar.js", "parseURL parsed valid fileName");
  equal(parsed.host, "foo.com:8888", "parseURL parsed valid host");
  equal(parsed.hostname, "foo.com", "parseURL parsed valid hostname");
  equal(parsed.port, "8888", "parseURL parsed valid port");
  equal(
    parsed.href,
    "https://foo.com:8888/boo/bar.js?q=query",
    "parseURL parsed valid href"
  );

  parsed = sourceUtils.parseURL("https://foo.com");
  equal(
    parsed.host,
    "foo.com",
    "parseURL parsed valid host when no port given"
  );
  equal(
    parsed.hostname,
    "foo.com",
    "parseURL parsed valid hostname when no port given"
  );

  equal(
    sourceUtils.parseURL("self-hosted"),
    null,
    "parseURL returns `null` for invalid URLs"
  );
});

// Test `sourceUtils.isContentScheme`.
add_task(async function () {
  for (const url of CHROME_URLS) {
    ok(
      !sourceUtils.isContentScheme(url),
      `${url} correctly identified as not content scheme`
    );
  }
  for (const url of CONTENT_URLS) {
    ok(
      sourceUtils.isContentScheme(url),
      `${url} correctly identified as content scheme`
    );
  }
});

// Test `sourceUtils.isChromeScheme`.
add_task(async function () {
  for (const url of CHROME_URLS) {
    ok(
      sourceUtils.isChromeScheme(url),
      `${url} correctly identified as chrome scheme`
    );
  }
  for (const url of CONTENT_URLS) {
    ok(
      !sourceUtils.isChromeScheme(url),
      `${url} correctly identified as not chrome scheme`
    );
  }
});

// Test `sourceUtils.isWASM`.
add_task(async function () {
  ok(
    sourceUtils.isWASM("wasm-function[66240] (?:13870536)"),
    "wasm function correctly identified"
  );
  ok(
    !sourceUtils.isWASM(CHROME_URLS[0]),
    `A chrome url does not identify as wasm.`
  );
});

// Test `sourceUtils.isDataScheme`.
add_task(async function () {
  const dataURI = "data:text/html;charset=utf-8,<!DOCTYPE html></html>";
  ok(
    sourceUtils.isDataScheme(dataURI),
    `${dataURI} correctly identified as data scheme`
  );

  for (const url of CHROME_URLS) {
    ok(
      !sourceUtils.isDataScheme(url),
      `${url} correctly identified as not data scheme`
    );
  }
  for (const url of CONTENT_URLS) {
    ok(
      !sourceUtils.isDataScheme(url),
      `${url} correctly identified as not data scheme`
    );
  }
});

// Test `sourceUtils.getSourceNames`.
add_task(async function () {
  testAbbreviation(
    "http://example.com/foo/bar/baz/boo.js",
    "boo.js",
    "http://example.com/foo/bar/baz/boo.js",
    "example.com"
  );
});

// Test `sourceUtils.getSourceNames`.
add_task(async function () {
  // Check length
  const longMalformedURL = `example.com${new Array(100)
    .fill("/a")
    .join("")}/file.js`;
  Assert.lessOrEqual(
    sourceUtils.getSourceNames(longMalformedURL).short.length,
    100,
    "`short` names are capped at 100 characters"
  );

  testAbbreviation("self-hosted", "self-hosted", "self-hosted");
  testAbbreviation("", "(unknown)", "(unknown)");

  // Test shortening data URIs, stripping mime/charset
  testAbbreviation(
    "data:text/html;charset=utf-8,<!DOCTYPE html></html>",
    "data:<!DOCTYPE html></html>",
    "data:text/html;charset=utf-8,<!DOCTYPE html></html>"
  );

  const longDataURI = `data:image/png;base64,${new Array(100)
    .fill("a")
    .join("")}`;
  const longDataURIShort = sourceUtils.getSourceNames(longDataURI).short;

  // Test shortening data URIs and that the `short` result is capped
  Assert.lessOrEqual(
    longDataURIShort.length,
    100,
    "`short` names are capped at 100 characters for data URIs"
  );
  equal(
    longDataURIShort.substr(0, 10),
    "data:aaaaa",
    "truncated data URI short names still have `data:...`"
  );

  // Test simple URL and cache retrieval by calling the same input multiple times.
  const testUrl = "http://example.com/foo/bar/baz/boo.js";
  testAbbreviation(testUrl, "boo.js", testUrl, "example.com");
  testAbbreviation(testUrl, "boo.js", testUrl, "example.com");

  // Check query and hash and port
  testAbbreviation(
    "http://example.com:8888/foo/bar/baz.js?q=query#go",
    "baz.js",
    "http://example.com:8888/foo/bar/baz.js",
    "example.com:8888"
  );

  // Trailing "/" with nothing beyond host
  testAbbreviation(
    "http://example.com/",
    "/",
    "http://example.com/",
    "example.com"
  );

  // Trailing "/"
  testAbbreviation(
    "http://example.com/foo/bar/",
    "bar",
    "http://example.com/foo/bar/",
    "example.com"
  );

  // Non-extension ending
  testAbbreviation(
    "http://example.com/bar",
    "bar",
    "http://example.com/bar",
    "example.com"
  );

  // Check query
  testAbbreviation(
    "http://example.com/foo.js?bar=1&baz=2",
    "foo.js",
    "http://example.com/foo.js",
    "example.com"
  );

  // Check query with trailing slash
  testAbbreviation(
    "http://example.com/foo/?bar=1&baz=2",
    "foo",
    "http://example.com/foo/",
    "example.com"
  );
});

// Test for source mapped file name
add_task(async function () {
  const { getSourceMappedFile } = sourceUtils;
  const source = "baz.js";
  const output = getSourceMappedFile(source);
  equal(output, "baz.js", "correctly formats file name");
  // Test for OSX file path
  const source1 = "/foo/bar/baz.js";
  const output1 = getSourceMappedFile(source1);
  equal(output1, "baz.js", "correctly formats Linux file path");
  // Test for Windows file path
  const source2 = "Z:\\foo\\bar\\baz.js";
  const output2 = getSourceMappedFile(source2);
  equal(output2, "baz.js", "correctly formats Windows file path");
});

function testAbbreviation(source, short, long, host) {
  const results = sourceUtils.getSourceNames(source);
  equal(results.short, short, `${source} has correct "short" name`);
  equal(results.long, long, `${source} has correct "long" name`);
  equal(results.host, host, `${source} has correct "host" name`);
}