From dbfe1fb86719a18364f24bff57781aeb3262cb90 Mon Sep 17 00:00:00 2001
From: Yuchen Pei <hi@ypei.me>
Date: Thu, 13 Oct 2022 15:48:44 +1100
Subject: fixing a few problems with @license / @license-end regexes

- trailing whitespace, including newlines are consumed by the opneing
  regex (added a test case)

- added a test case for /* @license ... */ ... /* @license-end */
---
 test/spec/LibreJSSpec.js | 12 ++++++++++++
 1 file changed, 12 insertions(+)

(limited to 'test/spec')

diff --git a/test/spec/LibreJSSpec.js b/test/spec/LibreJSSpec.js
index 7e21c24..bbd699c 100644
--- a/test/spec/LibreJSSpec.js
+++ b/test/spec/LibreJSSpec.js
@@ -41,12 +41,16 @@ describe('LibreJS\' components', () => {
   // code calling anything else is trivial
   const trivialCall = 'foo();';
   const licensed = `// @license ${license.magnet} ${license.id}\n${nontrivial}\n// @license-end`;
+  const licensedStarCommented = `/* @license ${license.magnet} ${license.id} */${nontrivial}/* @license-end */`;
   const unknownLicensed = `// @license ${unknownLicense.magnet} ${unknownLicense.id}\n${nontrivial}\n// @license-end`;
   const commentedOutUnknownLicensed =
     unknownLicensed.split('\n').map(y => '// ' + y).join('\n');
   const malformedLicensed = `// @license\n${nontrivial}`;
   const commentedOutMalformedLicensed =
     malformedLicensed.split('\n').map(y => '// ' + y).join('\n');
+  const emptyLicensed = `// @license ${license.magnet} ${license.id}\n// @license-end`;
+  const licensedNontrivialTrivial = `${emptyLicensed}\n${nontrivial}\n${trivial}`;
+
   let tab, documentUrl;
   const enableContactFinderTests = false;
 
@@ -121,6 +125,8 @@ describe('LibreJS\' components', () => {
     it('should accept scripts with known free license tags', async () => {
       const processed = await processScript(licensed);
       expect(processed || licensed).toContain(nontrivial);
+      const processed1 = await processScript(licensedStarCommented);
+      expect(processed1 || licensed).toContain(nontrivial);
     });
 
     it('should block scripts with unknown license tags', async () => {
@@ -210,6 +216,12 @@ describe('LibreJS\' components', () => {
       expect(extractScripts(processed, nontrivial)).not.toContain(nontrivial);
     });
 
+    it('should block the unlicensed nontrivial part sandwiched between licensed and trivial parts', async () => {
+      const modifiedHtml = addScript(html, licensedNontrivialTrivial);
+      const processed = await processHtml(modifiedHtml);
+      expect(extractScripts(processed, nontrivial)).not.toContain(nontrivial);
+    });
+
     it('should accept scripts on globally licensed pages', async () => {
       const globalLicense = `/* @licstart The following is the entire license notice
         for the JavaScript code in this page.
-- 
cgit v1.2.3