From 43502637288f9f60019be2759e5049d0fe9377fe Mon Sep 17 00:00:00 2001 From: hackademix Date: Tue, 2 Oct 2018 15:50:23 +0200 Subject: Jasmine-based automated tests suite. --- test/SpecRunner.html | 43 ++++++++ test/resources/app-trilicensed.js | 1 + test/resources/index.html | 38 +++++++ test/resources/jquery.js | 1 + test/resources/jslicense.html | 25 +++++ test/resources/proprietary.js | 1 + test/resources/tracker.js | 1 + test/spec/LibreJSSpec.js | 212 ++++++++++++++++++++++++++++++++++++++ 8 files changed, 322 insertions(+) create mode 100644 test/SpecRunner.html create mode 100644 test/resources/app-trilicensed.js create mode 100644 test/resources/index.html create mode 100644 test/resources/jquery.js create mode 100644 test/resources/jslicense.html create mode 100644 test/resources/proprietary.js create mode 100644 test/resources/tracker.js create mode 100644 test/spec/LibreJSSpec.js (limited to 'test') diff --git a/test/SpecRunner.html b/test/SpecRunner.html new file mode 100644 index 0000000..42a372f --- /dev/null +++ b/test/SpecRunner.html @@ -0,0 +1,43 @@ + + + + + + LibreJS Tests + + + + + + + + + + + + + +

LibreJS Autotest

+ + diff --git a/test/resources/app-trilicensed.js b/test/resources/app-trilicensed.js new file mode 100644 index 0000000..7b2c2ea --- /dev/null +++ b/test/resources/app-trilicensed.js @@ -0,0 +1 @@ +document.write(`

Executing ${document.currentScript.src}

`); diff --git a/test/resources/index.html b/test/resources/index.html new file mode 100644 index 0000000..c5f364c --- /dev/null +++ b/test/resources/index.html @@ -0,0 +1,38 @@ + + + + + + LibreJS test document + + + +

LibreJS test document

+JavaScript license information + + + + + + diff --git a/test/resources/jquery.js b/test/resources/jquery.js new file mode 100644 index 0000000..7b2c2ea --- /dev/null +++ b/test/resources/jquery.js @@ -0,0 +1 @@ +document.write(`

Executing ${document.currentScript.src}

`); diff --git a/test/resources/jslicense.html b/test/resources/jslicense.html new file mode 100644 index 0000000..0cd0449 --- /dev/null +++ b/test/resources/jslicense.html @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + +
Fake jQueryExpat +jquery-1.7.tar.gz
App specific script, tri-licensed (2 free and 1 proprietary license) +GNU V3 +
+Expat +
+Proprietary, optional
app-trilicensed.js
App specific script, proprietary only +Proprietaryproprietary.js
diff --git a/test/resources/proprietary.js b/test/resources/proprietary.js new file mode 100644 index 0000000..7b2c2ea --- /dev/null +++ b/test/resources/proprietary.js @@ -0,0 +1 @@ +document.write(`

Executing ${document.currentScript.src}

`); diff --git a/test/resources/tracker.js b/test/resources/tracker.js new file mode 100644 index 0000000..7b2c2ea --- /dev/null +++ b/test/resources/tracker.js @@ -0,0 +1 @@ +document.write(`

Executing ${document.currentScript.src}

`); diff --git a/test/spec/LibreJSSpec.js b/test/spec/LibreJSSpec.js new file mode 100644 index 0000000..8638d67 --- /dev/null +++ b/test/spec/LibreJSSpec.js @@ -0,0 +1,212 @@ +/* +* GNU LibreJS - A browser add-on to block nonfree nontrivial JavaScript. +* +* Copyright (C) 2018 Giorgio Maone +* +* This file is part of GNU LibreJS. +* +* GNU LibreJS is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* GNU LibreJS is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with GNU LibreJS. If not, see . +*/ +"use strict"; + +describe("LibreJS' components", () => { + const LENIENT_TAG_MSG = + `actual requirement? at this moment (20181001) + formal validity only gets checked, therefore this test would fail.`; + + let LibreJS = browser.extension.getBackgroundPage().LibreJS; + let license = { + id: 'GPL-3.0', + url: 'http://www.gnu.org/licenses/gpl-3.0.html', + magnet: 'magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt', + }; + let unknownLicense = { + id: 'Acme-proprietary-1.5', + url: 'http://www.acme.com/license-1.5.html', + magnet: 'magnet:?xt=urn:btih:2f739d935676111cfff4b4693e3816e664797050&dn=acme-1.5.txt' + }; + + let trivial = "1+1"; + let nontrivial = `function nt() { document.documentElement.innerHTML=""; nt(); }`; + let licensed = `// @license ${license.id} ${license.magnet}\n${nontrivial}\n// @license-end`; + let unknownLicensed = `// @license ${unknownLicense.id} ${unknownLicense.magnet}\n${nontrivial}\n// @license-end`; + let malformedLicensed = `// @license\n${nontrivial}`; + + let tab; + + beforeAll(async () => { + let url = browser.extension.getURL("/test/resources/index.html"); + tab = (await browser.tabs.query({url}))[0] || (await browser.tabs.create({url})); + }); + + 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({ + text: source, + request: {url, tabId: tab.id, documentUrl: tab.url, frameId: 0}, + }, whitelisted); + + it("should accept whitelisted scripts", async () => { + expect(await processScript(nontrivial, true) || nontrivial).toContain(nontrivial); + }); + + 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 () => { + let processed = await processScript(nontrivial); + expect(processed || nontrivial).not.toContain(nontrivial); + }); + + it("should accept scripts with known free license tags", async () => { + let processed = await processScript(licensed); + expect(processed || licensed).toContain(nontrivial); + }); + + xit("should block scripts with unknown license tags", async () => { + let processed = await processScript(unknownLicensed); + expect(processed).not.toContain(nontrivial); + }).pend(LENIENT_TAG_MSG); + + it("should block scripts with malformed license tags", async () => { + let processed = await processScript(malformedLicensed); + expect(processed).not.toContain(nontrivial); + }); + }); + + describe("The HTML processor", () => { + let processHtml = + async (html, whitelisted = false) => + LibreJS.editHtml(html, tab.url, tab.id, 0, whitelisted); + + let addScript = (html, script, before = "") => + html.replace(before, `${before}`); + + function extractScripts(html, def = "") { + let matches = html && html.match(/