test_source-utils.js (6548B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 /** 7 * Tests utility functions contained in `source-utils.js` 8 */ 9 10 const sourceUtils = require("resource://devtools/client/shared/source-utils.js"); 11 12 const CHROME_URLS = [ 13 "chrome://foo", 14 "resource://baz", 15 "jar:file:///Users/root", 16 ]; 17 18 const CONTENT_URLS = [ 19 "http://mozilla.org", 20 "https://mozilla.org", 21 "file:///Users/root", 22 "app://fxosapp", 23 "blob:http://mozilla.org", 24 "blob:https://mozilla.org", 25 ]; 26 27 // Test `sourceUtils.parseURL` 28 add_task(async function () { 29 let parsed = sourceUtils.parseURL("https://foo.com:8888/boo/bar.js?q=query"); 30 equal(parsed.fileName, "bar.js", "parseURL parsed valid fileName"); 31 equal(parsed.host, "foo.com:8888", "parseURL parsed valid host"); 32 equal(parsed.hostname, "foo.com", "parseURL parsed valid hostname"); 33 equal(parsed.port, "8888", "parseURL parsed valid port"); 34 equal( 35 parsed.href, 36 "https://foo.com:8888/boo/bar.js?q=query", 37 "parseURL parsed valid href" 38 ); 39 40 parsed = sourceUtils.parseURL("https://foo.com"); 41 equal( 42 parsed.host, 43 "foo.com", 44 "parseURL parsed valid host when no port given" 45 ); 46 equal( 47 parsed.hostname, 48 "foo.com", 49 "parseURL parsed valid hostname when no port given" 50 ); 51 52 equal( 53 sourceUtils.parseURL("self-hosted"), 54 null, 55 "parseURL returns `null` for invalid URLs" 56 ); 57 }); 58 59 // Test `sourceUtils.isContentScheme`. 60 add_task(async function () { 61 for (const url of CHROME_URLS) { 62 ok( 63 !sourceUtils.isContentScheme(url), 64 `${url} correctly identified as not content scheme` 65 ); 66 } 67 for (const url of CONTENT_URLS) { 68 ok( 69 sourceUtils.isContentScheme(url), 70 `${url} correctly identified as content scheme` 71 ); 72 } 73 }); 74 75 // Test `sourceUtils.isChromeScheme`. 76 add_task(async function () { 77 for (const url of CHROME_URLS) { 78 ok( 79 sourceUtils.isChromeScheme(url), 80 `${url} correctly identified as chrome scheme` 81 ); 82 } 83 for (const url of CONTENT_URLS) { 84 ok( 85 !sourceUtils.isChromeScheme(url), 86 `${url} correctly identified as not chrome scheme` 87 ); 88 } 89 }); 90 91 // Test `sourceUtils.isWASM`. 92 add_task(async function () { 93 ok( 94 sourceUtils.isWASM("wasm-function[66240] (?:13870536)"), 95 "wasm function correctly identified" 96 ); 97 ok( 98 !sourceUtils.isWASM(CHROME_URLS[0]), 99 `A chrome url does not identify as wasm.` 100 ); 101 }); 102 103 // Test `sourceUtils.isDataScheme`. 104 add_task(async function () { 105 const dataURI = "data:text/html;charset=utf-8,<!DOCTYPE html></html>"; 106 ok( 107 sourceUtils.isDataScheme(dataURI), 108 `${dataURI} correctly identified as data scheme` 109 ); 110 111 for (const url of CHROME_URLS) { 112 ok( 113 !sourceUtils.isDataScheme(url), 114 `${url} correctly identified as not data scheme` 115 ); 116 } 117 for (const url of CONTENT_URLS) { 118 ok( 119 !sourceUtils.isDataScheme(url), 120 `${url} correctly identified as not data scheme` 121 ); 122 } 123 }); 124 125 // Test `sourceUtils.getSourceNames`. 126 add_task(async function () { 127 testAbbreviation( 128 "http://example.com/foo/bar/baz/boo.js", 129 "boo.js", 130 "http://example.com/foo/bar/baz/boo.js", 131 "example.com" 132 ); 133 }); 134 135 // Test `sourceUtils.getSourceNames`. 136 add_task(async function () { 137 // Check length 138 const longMalformedURL = `example.com${new Array(100) 139 .fill("/a") 140 .join("")}/file.js`; 141 Assert.lessOrEqual( 142 sourceUtils.getSourceNames(longMalformedURL).short.length, 143 100, 144 "`short` names are capped at 100 characters" 145 ); 146 147 testAbbreviation("self-hosted", "self-hosted", "self-hosted"); 148 testAbbreviation("", "(unknown)", "(unknown)"); 149 150 // Test shortening data URIs, stripping mime/charset 151 testAbbreviation( 152 "data:text/html;charset=utf-8,<!DOCTYPE html></html>", 153 "data:<!DOCTYPE html></html>", 154 "data:text/html;charset=utf-8,<!DOCTYPE html></html>" 155 ); 156 157 const longDataURI = `data:image/png;base64,${new Array(100) 158 .fill("a") 159 .join("")}`; 160 const longDataURIShort = sourceUtils.getSourceNames(longDataURI).short; 161 162 // Test shortening data URIs and that the `short` result is capped 163 Assert.lessOrEqual( 164 longDataURIShort.length, 165 100, 166 "`short` names are capped at 100 characters for data URIs" 167 ); 168 equal( 169 longDataURIShort.substr(0, 10), 170 "data:aaaaa", 171 "truncated data URI short names still have `data:...`" 172 ); 173 174 // Test simple URL and cache retrieval by calling the same input multiple times. 175 const testUrl = "http://example.com/foo/bar/baz/boo.js"; 176 testAbbreviation(testUrl, "boo.js", testUrl, "example.com"); 177 testAbbreviation(testUrl, "boo.js", testUrl, "example.com"); 178 179 // Check query and hash and port 180 testAbbreviation( 181 "http://example.com:8888/foo/bar/baz.js?q=query#go", 182 "baz.js", 183 "http://example.com:8888/foo/bar/baz.js", 184 "example.com:8888" 185 ); 186 187 // Trailing "/" with nothing beyond host 188 testAbbreviation( 189 "http://example.com/", 190 "/", 191 "http://example.com/", 192 "example.com" 193 ); 194 195 // Trailing "/" 196 testAbbreviation( 197 "http://example.com/foo/bar/", 198 "bar", 199 "http://example.com/foo/bar/", 200 "example.com" 201 ); 202 203 // Non-extension ending 204 testAbbreviation( 205 "http://example.com/bar", 206 "bar", 207 "http://example.com/bar", 208 "example.com" 209 ); 210 211 // Check query 212 testAbbreviation( 213 "http://example.com/foo.js?bar=1&baz=2", 214 "foo.js", 215 "http://example.com/foo.js", 216 "example.com" 217 ); 218 219 // Check query with trailing slash 220 testAbbreviation( 221 "http://example.com/foo/?bar=1&baz=2", 222 "foo", 223 "http://example.com/foo/", 224 "example.com" 225 ); 226 }); 227 228 // Test for source mapped file name 229 add_task(async function () { 230 const { getSourceMappedFile } = sourceUtils; 231 const source = "baz.js"; 232 const output = getSourceMappedFile(source); 233 equal(output, "baz.js", "correctly formats file name"); 234 // Test for OSX file path 235 const source1 = "/foo/bar/baz.js"; 236 const output1 = getSourceMappedFile(source1); 237 equal(output1, "baz.js", "correctly formats Linux file path"); 238 // Test for Windows file path 239 const source2 = "Z:\\foo\\bar\\baz.js"; 240 const output2 = getSourceMappedFile(source2); 241 equal(output2, "baz.js", "correctly formats Windows file path"); 242 }); 243 244 function testAbbreviation(source, short, long, host) { 245 const results = sourceUtils.getSourceNames(source); 246 equal(results.short, short, `${source} has correct "short" name`); 247 equal(results.long, long, `${source} has correct "long" name`); 248 equal(results.host, host, `${source} has correct "host" name`); 249 }