diff options
Diffstat (limited to 'test/spec')
-rw-r--r-- | test/spec/LibreJSSpec.js | 140 |
1 files changed, 70 insertions, 70 deletions
diff --git a/test/spec/LibreJSSpec.js b/test/spec/LibreJSSpec.js index 1320a2a..9e96af5 100644 --- a/test/spec/LibreJSSpec.js +++ b/test/spec/LibreJSSpec.js @@ -18,9 +18,9 @@ * You should have received a copy of the GNU General Public License * along with GNU LibreJS. If not, see <http://www.gnu.org/licenses/>. */ -"use strict"; +'use strict'; -describe("LibreJS' components", () => { +describe('LibreJS\' components', () => { let LibreJS = browser.extension.getBackgroundPage().LibreJS; let license = { id: 'GPL-3.0', @@ -33,60 +33,60 @@ describe("LibreJS' components", () => { magnet: 'magnet:?xt=urn:btih:2f739d935676111cfff4b4693e3816e664797050&dn=acme-1.5.txt' }; - let trivial = "1+1"; - let nontrivial = `function nt() { document.documentElement.innerHTML=""; nt(); }`; + let trivial = '1+1'; + let nontrivial = 'function nt() { document.documentElement.innerHTML=""; nt(); }'; let licensed = `// @license ${license.magnet} ${license.id}\n${nontrivial}\n// @license-end`; let unknownLicensed = `// @license ${unknownLicense.magnet} ${unknownLicense.id}\n${nontrivial}\n// @license-end`; let malformedLicensed = `// @license\n${nontrivial}`; let tab, documentUrl; beforeAll(async () => { - let url = browser.extension.getURL("/test/resources/index.html"); + let url = browser.extension.getURL('/test/resources/index.html'); tab = (await browser.tabs.query({ url }))[0] || (await browser.tabs.create({ url })); documentUrl = url; }); - describe("The whitelist/blacklist manager", () => { + describe('The whitelist/blacklist manager', () => { let { ListManager, ListStore, Storage } = LibreJS; - let lm = new ListManager(new ListStore("_test.whitelist", Storage.CSV), new ListStore("_test.blacklist", Storage.CSV), new Set()); - let forgot = ["http://formerly.whitelist.ed/", "http://formerly.blacklist.ed/"]; + let lm = new ListManager(new ListStore('_test.whitelist', Storage.CSV), new ListStore('_test.blacklist', Storage.CSV), new Set()); + let forgot = ['http://formerly.whitelist.ed/', 'http://formerly.blacklist.ed/']; beforeAll(async () => { - await lm.whitelist("https://fsf.org/*", "https://*.gnu.org/*", forgot[0]); - await lm.blacklist("https://*.evil.gnu.org/*", "https://verybad.com/*", forgot[1]); + await lm.whitelist('https://fsf.org/*', 'https://*.gnu.org/*', forgot[0]); + await lm.blacklist('https://*.evil.gnu.org/*', 'https://verybad.com/*', forgot[1]); }); - it("Should handle basic CRUD operations", async () => { - expect(lm.getStatus(forgot[0])).toBe("whitelisted"); - expect(lm.getStatus(forgot[1])).toBe("blacklisted"); + it('Should handle basic CRUD operations', async () => { + expect(lm.getStatus(forgot[0])).toBe('whitelisted'); + expect(lm.getStatus(forgot[1])).toBe('blacklisted'); await lm.forget(...forgot); for (let url of forgot) { - expect(lm.getStatus(url)).toBe("unknown"); + expect(lm.getStatus(url)).toBe('unknown'); } }); - it("Should support full path wildcards", () => { - expect(lm.getStatus("https://unknown.org")).toBe("unknown"); - expect(lm.getStatus("https://fsf.org/some/path")).toBe("whitelisted"); - expect(lm.getStatus("https://fsf.org/")).toBe("whitelisted"); - expect(lm.getStatus("https://fsf.org")).toBe("whitelisted"); - expect(lm.getStatus("https://subdomain.fsf.org")).toBe("unknown"); - expect(lm.getStatus("https://verybad.com/some/other/path?with=querystring")).toBe("blacklisted"); + it('Should support full path wildcards', () => { + expect(lm.getStatus('https://unknown.org')).toBe('unknown'); + expect(lm.getStatus('https://fsf.org/some/path')).toBe('whitelisted'); + expect(lm.getStatus('https://fsf.org/')).toBe('whitelisted'); + expect(lm.getStatus('https://fsf.org')).toBe('whitelisted'); + expect(lm.getStatus('https://subdomain.fsf.org')).toBe('unknown'); + expect(lm.getStatus('https://verybad.com/some/other/path?with=querystring')).toBe('blacklisted'); }); - it("Should support subdomain wildcards", () => { - expect(lm.getStatus("https://gnu.org")).toBe("whitelisted"); - expect(lm.getStatus("https://www.gnu.org")).toBe("whitelisted"); - expect(lm.getStatus("https://evil.gnu.org")).toBe("blacklisted"); - expect(lm.getStatus("https://more.evil.gnu.org")).toBe("blacklisted"); - expect(lm.getStatus("https://more.evil.gnu.org/some/evil/path?too")).toBe("blacklisted"); + it('Should support subdomain wildcards', () => { + expect(lm.getStatus('https://gnu.org')).toBe('whitelisted'); + expect(lm.getStatus('https://www.gnu.org')).toBe('whitelisted'); + expect(lm.getStatus('https://evil.gnu.org')).toBe('blacklisted'); + expect(lm.getStatus('https://more.evil.gnu.org')).toBe('blacklisted'); + expect(lm.getStatus('https://more.evil.gnu.org/some/evil/path?too')).toBe('blacklisted'); }); }) - describe("The external script source processor", () => { - let url = "https://www.gnu.org/mock-script.js"; + describe('The external script source processor', () => { + let url = 'https://www.gnu.org/mock-script.js'; let processScript = async (source, whitelisted = false) => await LibreJS.handle_script({ @@ -94,125 +94,125 @@ describe("LibreJS' components", () => { request: { url, tabId: tab.id, documentUrl, frameId: 0 }, }, whitelisted); - it("should accept whitelisted scripts", async () => { + it('should accept whitelisted scripts', async () => { expect(await processScript(nontrivial, true) || nontrivial).toContain(nontrivial); }); - it("should block trivial scripts too", async () => { + it('should block trivial scripts too', async () => { let processed = await processScript(trivial); expect(processed || trivial).not.toContain(trivial); }); - it("should block non-trivial scripts", async () => { + it('should block non-trivial scripts', async () => { let processed = await processScript(nontrivial); expect(processed || nontrivial).not.toContain(nontrivial); }); - it("should accept scripts with known free license tags", async () => { + it('should accept scripts with known free license tags', async () => { let processed = await processScript(licensed); expect(processed || licensed).toContain(nontrivial); }); - it("should block scripts with unknown license tags", async () => { + it('should block scripts with unknown license tags', async () => { let processed = await processScript(unknownLicensed); expect(processed).not.toContain(nontrivial); }); - it("should block scripts with malformed license tags", async () => { + it('should block scripts with malformed license tags', async () => { let processed = await processScript(malformedLicensed); expect(processed).not.toContain(nontrivial); }); }); - describe("The HTML processor", () => { + describe('The HTML processor', () => { let processHtml = async (html, whitelisted = false) => LibreJS.editHtml(html, tab.url, tab.id, 0, whitelisted); - let addScript = (html, script, before = "</head>") => + let addScript = (html, script, before = '</head>') => html.replace(before, `<script>${script}</script>${before}`); - let addToBody = (html, fragment) => html.replace("</body>", `${fragment}</body>`); + let addToBody = (html, fragment) => html.replace('</body>', `${fragment}</body>`); let jsUrl = js => `javascript:${encodeURIComponent(js)}`; - function extractScripts(html, def = "") { + function extractScripts(html, def = '') { let matches = html && html.match(/<script>[^]*?<\/script>/g); - return matches && matches.join("") || def; + return matches && matches.join('') || def; } let html, nontrivialInHtml; beforeAll(async () => { html = (await browser.tabs.executeScript(tab.id, { - runAt: "document_start", - code: "document.documentElement.outerHTML" + runAt: 'document_start', + code: 'document.documentElement.outerHTML' }))[0]; nontrivialInHtml = addScript(html, nontrivial); }); - it("should not modify scriptless documents", async () => { + it('should not modify scriptless documents', async () => { expect(await processHtml(html)).toBeNull(); }); - it("should not modify whitelisted documents", async () => { + it('should not modify whitelisted documents', async () => { expect(await processHtml(nontrivialInHtml, true)).toBeNull(); }); - it("should accept trivial scripts", async () => { + it('should accept trivial scripts', async () => { let trivialInHtml = addScript(html, trivial); let processed = await processHtml(trivialInHtml); expect(extractScripts(processed, trivial)).toContain(trivial); }); - it("should block non-trivial scripts", async () => { + it('should block non-trivial scripts', async () => { let processed = await processHtml(nontrivialInHtml); expect(extractScripts(processed, nontrivial)).not.toContain(nontrivial); }); - it("should accept scripts with known free license tags", async () => { + it('should accept scripts with known free license tags', async () => { let licensedInHtml = addScript(html, licensed); let processed = await processHtml(licensedInHtml); expect(extractScripts(processed, licensed)).toContain(nontrivial); }); - it("should block scripts with unknown license tags", async () => { + it('should block scripts with unknown license tags', async () => { let unknownInHtml = addScript(html, unknownLicensed); let processed = await processHtml(unknownInHtml); expect(extractScripts(processed, nontrivial)).not.toContain(nontrivial); }); - it("should block scripts with malformed license tags", async () => { + it('should block scripts with malformed license tags', async () => { let malformedInHtml = addScript(html, malformedLicensed); let processed = await processHtml(malformedInHtml); expect(extractScripts(processed, nontrivial)).not.toContain(nontrivial); }); - it("should accept scripts on globally licensed pages", async () => { + it('should accept scripts on globally licensed pages', async () => { let globalLicense = `/* @licstart The following is the entire license notice for the JavaScript code in this page. -- Some free license -- @licend The above is the entire license notice for the JavaScript code in this page. */`; - let licensed = addScript(nontrivialInHtml, globalLicense, "<script>"); + let licensed = addScript(nontrivialInHtml, globalLicense, '<script>'); let processed = await processHtml(html); expect(extractScripts(processed, licensed)).toContain(nontrivial); }); - it("should discriminate trivial, non-trivial and licensed mixed on the same page", async () => { + it('should discriminate trivial, non-trivial and licensed mixed on the same page', async () => { let mixedPage = addScript(addScript(nontrivialInHtml, trivial), licensed); let processed = await processHtml(mixedPage); expect(processed).not.toBeNull(); let scripts = extractScripts(processed, nontrivial); expect(scripts).toContain(trivial); expect(scripts).toContain(licensed); - expect(scripts.replace(licensed, "")).not.toContain(nontrivial); + expect(scripts.replace(licensed, '')).not.toContain(nontrivial); }); - it("should correctly process (de)duplicated inline scripts", async () => { + it('should correctly process (de)duplicated inline scripts', async () => { let trivialAsUrl = jsUrl(trivial); let nontrivialAsUrl = jsUrl(nontrivial); let a = (url, label) => `<a href="${url}">${label}</a>`; - let mixedPage = `<body></body>`; + let mixedPage = '<body></body>'; for (let dup = 0; dup < 3; dup++) { mixedPage = addToBody(mixedPage, a(trivialAsUrl, `Trivial #${dup}`)); mixedPage = addToBody(mixedPage, a(nontrivialAsUrl, `Nontrivial #${dup}`)); @@ -223,8 +223,8 @@ describe("LibreJS' components", () => { expect(processed).not.toContain(nontrivialAsUrl); }); - it("should force displaying NOSCRIPT elements (except those with @data-librejs-nodisplay) where scripts have been blocked", async () => { - let noscriptContent = "I'm NOSCRIPT content"; + it('should force displaying NOSCRIPT elements (except those with @data-librejs-nodisplay) where scripts have been blocked', async () => { + let noscriptContent = 'I\'m NOSCRIPT content'; let asNoscript = `<noscript>${noscriptContent}</noscript>`; let asNodisplay = `<noscript data-librejs-nodisplay>${noscriptContent}</noscript>`; let asSpan = `<span>${noscriptContent}</span>`; @@ -235,11 +235,11 @@ describe("LibreJS' components", () => { expect(processed).not.toContain(asNodisplay); }); - it("should always force displaying @data-librejs-display elements", async () => { - let content = "I'm FORCED content"; + it('should always force displaying @data-librejs-display elements', async () => { + let content = 'I\'m FORCED content'; let asDisplay = `<span data-librejs-display>${content}</span>`; let asSpan = `<span>${content}</span>`; - for (let page of [nontrivialInHtml, "<body></body>"]) { + for (let page of [nontrivialInHtml, '<body></body>']) { page = addToBody(page, asDisplay); let processed = await processHtml(page); expect(processed).not.toContain(asDisplay); @@ -248,7 +248,7 @@ describe("LibreJS' components", () => { }); }); - describe("The external (Web Labels) license checker", () => { + describe('The external (Web Labels) license checker', () => { let { ExternalLicenses } = LibreJS; let check; @@ -257,27 +257,27 @@ describe("LibreJS' components", () => { let resolve = url => new URL(url, documentUrl).href; check = async url => await ExternalLicenses.check(Object.assign({ url: resolve(url) }, args)); await browser.tabs.executeScript(tab.id, { - file: "/content/externalLicenseChecker.js" + file: '/content/externalLicenseChecker.js' }); }); - it("should recognize free licenses", async () => { - let scriptInfo = await check("jquery.js"); + it('should recognize free licenses', async () => { + let scriptInfo = await check('jquery.js'); console.debug(scriptInfo); expect(scriptInfo.free).toBeTruthy(); }); - it("should accept scripts if any of multiple licenses is free", async () => { - let scriptInfo = await check("app-trilicensed.js"); + it('should accept scripts if any of multiple licenses is free', async () => { + let scriptInfo = await check('app-trilicensed.js'); console.debug(scriptInfo); expect(scriptInfo.free).toBeTruthy(); }); - it("should block scripts declaring only proprietary license(s)", async () => { - let scriptInfo = await check("proprietary.js"); + it('should block scripts declaring only proprietary license(s)', async () => { + let scriptInfo = await check('proprietary.js'); console.debug(scriptInfo); expect(scriptInfo.free).toBeFalsy(); }); - it("should block scripts not declaring any license", async () => { - let scriptInfo = await check("tracker.js"); + it('should block scripts not declaring any license', async () => { + let scriptInfo = await check('tracker.js'); console.debug(scriptInfo); expect(scriptInfo).toBeNull(); }); |