Explorar el Código

refactor(js): convert more tests to fast-check

Fixes https://github.com/PrivateBin/PrivateBin/issues/1812

Co-authored-by: Junie <junie@jetbrains.com>
rugk hace 2 meses
padre
commit
846476402f
Se han modificado 3 ficheros con 51 adiciones y 40 borrados
  1. 28 19
      js/test/CryptTool.js
  2. 7 6
      js/test/ServerInteraction.js
  3. 16 15
      js/test/UiHelper.js

+ 28 - 19
js/test/CryptTool.js

@@ -1,5 +1,6 @@
 'use strict';
 const common = require('../common');
+const fc = require('fast-check');
 const fs = require('fs');
 
 describe('CryptTool', function () {
@@ -10,18 +11,20 @@ describe('CryptTool', function () {
         });
 
         this.timeout(30000);
-        it('can en- and decrypt any message', function () {
-            jsc.assert(jsc.forall(
-                'string',
-                'string',
-                'string',
+        it('can en- and decrypt any message', async function () {
+            await fc.assert(fc.asyncProperty(
+                fc.string(),
+                fc.string(),
+                fc.string(),
                 async function (key, password, message) {
                     const clean = globalThis.cleanup();
                     // ensure zlib is getting loaded
                     PrivateBin.Controller.initZ();
                     Object.defineProperty(window, 'crypto', {
                         value: new WebCrypto(),
-                        writeable: false
+                        configurable: true,
+                        enumerable: true,
+                        writable: false
                     });
                     global.atob = common.atob;
                     global.btoa = common.btoa;
@@ -38,7 +41,7 @@ describe('CryptTool', function () {
                     return result;
                 }
             ),
-            {tests: 3});
+            {numRuns: 3});
         });
 
         it('does not truncate messages', async function () {
@@ -46,7 +49,9 @@ describe('CryptTool', function () {
                 clean = globalThis.cleanup();
             Object.defineProperty(window, 'crypto', {
                 value: new WebCrypto(),
-                writeable: false
+                configurable: true,
+                enumerable: true,
+                writable: false
             });
             // ensure zlib is getting loaded
             PrivateBin.Controller.initZ();
@@ -65,10 +70,10 @@ describe('CryptTool', function () {
             assert.strictEqual(message, plaintext);
         });
 
-        it('can en- and decrypt a particular message (#260)', function () {
-            jsc.assert(jsc.forall(
-                'string',
-                'string',
+        it('can en- and decrypt a particular message (#260)', async function () {
+            await fc.assert(fc.asyncProperty(
+                fc.string(),
+                fc.string(),
                 async function (key, password) {
                     const message = `
 1 subgoal
@@ -97,7 +102,9 @@ conseq_or_bottom inv (interp (nth_iterate sBody n) (MemElem mem))
                     PrivateBin.Controller.initZ();
                     Object.defineProperty(window, 'crypto', {
                         value: new WebCrypto(),
-                        writeable: false
+                        configurable: true,
+                        enumerable: true,
+                        writable: false
                     });
                     const cipherMessage = await PrivateBin.CryptTool.cipher(
                             key, password, message, []
@@ -111,7 +118,7 @@ conseq_or_bottom inv (interp (nth_iterate sBody n) (MemElem mem))
                     return result;
                 }
             ),
-            {tests: 3});
+            {numRuns: 3});
         });
     });
 
@@ -120,14 +127,16 @@ conseq_or_bottom inv (interp (nth_iterate sBody n) (MemElem mem))
         let keys = [];
 
         // the parameter is used to ensure the test is run more then one time
-        it('returns random, non-empty keys', function () {
-            jsc.assert(jsc.forall(
-                'integer',
+        it('returns random, non-empty keys', () => {
+            fc.assert(fc.property(
+                fc.integer(),
                 function(counter) {
                     const clean = globalThis.cleanup();
                     Object.defineProperty(window, 'crypto', {
                         value: new WebCrypto(),
-                        writeable: false
+                        configurable: true,
+                        enumerable: true,
+                        writable: false
                     });
                     const key = PrivateBin.CryptTool.getSymmetricKey(),
                         result = (key !== '' && keys.indexOf(key) === -1);
@@ -136,7 +145,7 @@ conseq_or_bottom inv (interp (nth_iterate sBody n) (MemElem mem))
                     return result;
                 }
             ),
-            {tests: 10});
+            {numRuns: 10});
         });
     });
 });

+ 7 - 6
js/test/ServerInteraction.js

@@ -1,5 +1,6 @@
 'use strict';
 require('../common');
+const fc = require('fast-check');
 
 describe('ServerInteraction', function () {
     describe('prepare', function () {
@@ -8,11 +9,11 @@ describe('ServerInteraction', function () {
             await new Promise(resolve => setTimeout(resolve, 1900));
         });
         this.timeout(30000);
-        it('can prepare an encrypted document', function () {
-            jsc.assert(jsc.forall(
-                'string',
-                'string',
-                'string',
+        it('can prepare an encrypted document', async function () {
+            await fc.assert(fc.asyncProperty(
+                fc.string(),
+                fc.string(),
+                fc.string(),
                 async function (key, password, message) {
                     // pause to let async functions conclude
                     await new Promise(resolve => setTimeout(resolve, 300));
@@ -34,7 +35,7 @@ describe('ServerInteraction', function () {
                     return true;
                 }
             ),
-            {tests: 3});
+            {numRuns: 3});
         });
     });
 });

+ 16 - 15
js/test/UiHelper.js

@@ -57,22 +57,23 @@ describe('UiHelper', function () {
             PrivateBin.Helper.reset();
         });
 
-        jsc.property(
-            'redirects to home',
-            common.jscUrl(),
-            function (url) {
-                const clean = globalThis.cleanup('', {url: common.urlToString(url)});
-                delete(url.query);
-                delete(url.fragment);
-                const expected = common.urlToString(url);
+        it('redirects to home', () => {
+            fc.assert(fc.property(
+                common.fcUrl(),
+                function (url) {
+                    const clean = globalThis.cleanup('', {url: common.urlToString(url)});
+                    delete(url.query);
+                    delete(url.fragment);
+                    const expected = common.urlToString(url);
 
-                PrivateBin.UiHelper.reloadHome();
-                PrivateBin.Helper.reset();
-                var result = window.location.href;
-                clean();
-                return expected === result;
-            }
-        );
+                    PrivateBin.UiHelper.reloadHome();
+                    PrivateBin.Helper.reset();
+                    var result = window.location.href;
+                    clean();
+                    return expected === result;
+                }
+            ));
+        });
         */
     });
 });