js: clean up test extension
authorMaximilian Krambach <maximilian.krambach@intevation.de>
Fri, 27 Jul 2018 09:20:33 +0000 (11:20 +0200)
committerMaximilian Krambach <maximilian.krambach@intevation.de>
Fri, 27 Jul 2018 09:20:33 +0000 (11:20 +0200)
--

Tests will now run with one instance of gpgmejs each block,
which reduces overhead. Readability is (hopefully) improved),
some negative tests are added.

There is still a performance problem in base64 encoding/decoding,
which causes some tests to fail due to time out.

lang/js/BrowserTestExtension/tests/KeyImportExport.js
lang/js/BrowserTestExtension/tests/encryptDecryptTest.js
lang/js/BrowserTestExtension/tests/encryptTest.js
lang/js/BrowserTestExtension/tests/inputvalues.js
lang/js/BrowserTestExtension/tests/longRunningTests.js
lang/js/BrowserTestExtension/tests/signTest.js
lang/js/BrowserTestExtension/tests/verifyTest.js

index ed307b3..d2fa2d3 100644 (file)
  *     Raimund Renkert <rrenkert@intevation.de>
  */
 
-/* global describe, it, expect, Gpgmejs, ImportablePublicKey, inputvalues */
+/* global describe, it, expect, before, afterEach, Gpgmejs*/
+/* global ImportablePublicKey, inputvalues */
 
 describe('Key importing', function () {
-    it('Prepare test Key (deleting it from gnupg, if present)', function(done){
-        let prm = Gpgmejs.init();
-        prm.then(function (context) {
-            expect(context.Keyring.getKeys).to.be.a('function');
-            context.Keyring.getKeys(ImportablePublicKey.fingerprint).then(
+    const fpr = ImportablePublicKey.fingerprint;
+    const pubKey = ImportablePublicKey.key;
+    const changedKey = ImportablePublicKey.keyChangedUserId;
+
+    let context = null;
+    before(function(done){
+        const prm = Gpgmejs.init();
+        prm.then(function(gpgmejs){
+            context = gpgmejs;
+            context.Keyring.getKeys(fpr).then(
                 function(result){
                     if (result.length === 1) {
-                        result[0].delete().then(function(result){
-                            expect(result).to.be.true;
+                        result[0].delete().then(function(){
+                            done();
+                        },function(){
                             done();
                         });
                     } else {
@@ -43,76 +50,79 @@ describe('Key importing', function () {
         });
     });
 
-    it('importing, updating, then deleting public Key', function (done) {
-        //This test runs in one large step, to ensure the proper state of the
-        // key in all stages.
-        let prm = Gpgmejs.init();
-        prm.then(function (context) {
-            context.Keyring.getKeys(ImportablePublicKey.fingerprint).then(
-                function(result){
-                    expect(result).to.be.an('array');
-                    expect(result.length).to.equal(0);
-                    context.Keyring.importKey(ImportablePublicKey.key, true)
-                        .then(function(result){
-                            expect(result.Keys[0]).to.not.be.undefined;
-                            expect(result.Keys[0].key).to.be.an('object');
-                            expect(result.Keys[0].key.fingerprint).to.equal(
-                                ImportablePublicKey.fingerprint);
-                            expect(result.Keys[0].status).to.equal('newkey');
-                            context.Keyring.importKey(
-                                ImportablePublicKey.keyChangedUserId,true)
-                                .then(function(res){
-                                    expect(res.Keys[0]).to.not.be.undefined;
-                                    expect(res.Keys[0].key).to.be.an('object');
-                                    expect(res.Keys[0].key.fingerprint).to.equal(
-                                        ImportablePublicKey.fingerprint);
-                                    expect(res.Keys[0].status).to.equal(
-                                        'change');
-                                    expect(
-                                        res.Keys[0].changes.userId).to.be.true;
-                                    expect(
-                                        res.Keys[0].changes.subkey).to.be.false;
-                                    expect(
-                                        res.Keys[0].changes.signature).to.be.true;
-                                    res.Keys[0].key.delete().then(function(result){
-                                        expect(result).to.be.true;
-                                        done();
-                                    });
-                                });
-                        });
+    afterEach(function(done){
+        // delete the test key if still present
+        context.Keyring.getKeys(fpr).then(
+            function(result){
+                if (result.length === 1) {
+                    result[0].delete().then(function(){
+                        done();
+                    },function(){
+                        done();
+                    });
+                } else {
+                    done();
+                }
+            });
+    });
+
+    it('Importing Key', function (done) {
+        context.Keyring.getKeys(fpr).then(function(result){
+            expect(result).to.be.an('array');
+            expect(result.length).to.equal(0);
+            context.Keyring.importKey(pubKey).then(function(result){
+                expect(result[0]).to.not.be.undefined;
+                expect(result[0].key).to.be.an('object');
+                expect(result[0].key.fingerprint).to.equal(fpr);
+                expect(result[0].status).to.equal('newkey');
+                done();
+            });
+        });
+    });
+
+    it('Updating Key', function(done){
+        context.Keyring.importKey(pubKey)
+            .then(function(result){
+                expect(result[0].key).to.not.be.undefined;
+                expect(result[0].status).to.equal('newkey');
+                context.Keyring.importKey(changedKey).then(function(res){
+                    expect(res[0].key).to.be.an('object');
+                    expect(res[0].key.fingerprint).to.equal(fpr);
+                    expect(res[0].status).to.equal('change');
+                    expect(res[0].changes.userId).to.be.true;
+                    expect(res[0].changes.subkey).to.be.false;
+                    expect(res[0].changes.signature).to.be.true;
+                    done();
                 });
+            });
+    });
+
+    it('Deleting Key', function(done) {
+        context.Keyring.importKey(pubKey).then(function(result){
+            expect(result[0].key).to.be.an('object');
+            expect(result[0].key.fingerprint).to.equal(fpr);
+            result[0].key.delete().then(function(result){
+                expect(result).to.be.true;
+                done();
+            });
         });
     });
 
     it('Import result feedback', function(done){
-        let prm = Gpgmejs.init();
-        prm.then(function (context) {
-            context.Keyring.getKeys(ImportablePublicKey.fingerprint).then(
-                function(result){
-                    expect(result).to.be.an('array');
-                    expect(result.length).to.equal(0);
-                    context.Keyring.importKey(ImportablePublicKey.key, true)
-                        .then(function(result){
-                            expect(result).to.be.an('object');
-                            expect(result.Keys[0]).to.be.an('object');
-                            expect(result.Keys[0].key.fingerprint).to.equal(
-                                ImportablePublicKey.fingerprint);
-                            expect(result.Keys[0].status).to.equal('newkey');
-                            result.Keys[0].key.getArmor().then(function(armor){
-                                expect(armor).to.be.a('string');
-                                result.Keys[0].key.delete().then(function(){
-                                    done();
-                                });
-                            });
-                        });
-                });
+        context.Keyring.importKey(pubKey, true).then(function(result){
+            expect(result).to.be.an('object');
+            expect(result.Keys[0]).to.be.an('object');
+            expect(result.Keys[0].key.fingerprint).to.equal(fpr);
+            expect(result.Keys[0].status).to.equal('newkey');
+            result.Keys[0].key.getArmor().then(function(armor){
+                expect(armor).to.be.a('string');
+                done();
+            });
         });
     });
 
     it('exporting armored Key with getKeysArmored', function (done) {
-        let prm = Gpgmejs.init();
-        const fpr = inputvalues.encrypt.good.fingerprint;
-        prm.then(function (context) {
+        context.Keyring.importKey(pubKey).then(function(){
             context.Keyring.getKeysArmored(fpr).then(function(result){
                 expect(result).to.be.an('object');
                 expect(result.armored).to.be.a('string');
@@ -121,18 +131,15 @@ describe('Key importing', function () {
             });
         });
     });
-    it('exporting armored Key (including secret fingerprints) with '
-        + 'getKeysArmored', function (done) {
-        let prm = Gpgmejs.init();
-        const fpr = inputvalues.encrypt.good.fingerprint;
-        prm.then(function (context) {
-            context.Keyring.getKeysArmored(fpr, true).then(function(result){
-                expect(result).to.be.an('object');
-                expect(result.armored).to.be.a('string');
-                expect(result.secret_fprs).to.be.an('array');
-                expect(result.secret_fprs[0]).to.equal(fpr);
-                done();
-            });
+
+    it('Exporting Key (including secret fingerprints)', function (done) {
+        const key_secret = inputvalues.encrypt.good.fingerprint;
+        context.Keyring.getKeysArmored(key_secret, true).then(function(result){
+            expect(result).to.be.an('object');
+            expect(result.armored).to.be.a('string');
+            expect(result.secret_fprs).to.be.an('array');
+            expect(result.secret_fprs[0]).to.equal(key_secret);
+            done();
         });
     });
 });
\ No newline at end of file
index bd72c1d..80b293d 100644 (file)
  *     Maximilian Krambach <mkrambach@intevation.de>
  */
 
-/* global describe, it, expect, Gpgmejs */
+/* global describe, it, expect, before, Gpgmejs */
 /* global inputvalues, encryptedData, bigString, bigBoringString */
 
 describe('Encryption and Decryption', function () {
+    let context = null;
+    let good_fpr = inputvalues.encrypt.good.fingerprint;
+
+    before(function(done){
+        const prm = Gpgmejs.init();
+        prm.then(function(gpgmejs){
+            context = gpgmejs;
+            done();
+        });
+    });
+
     it('Successful encrypt and decrypt simple string', function (done) {
-        let prm = Gpgmejs.init();
-        prm.then(function (context) {
-            context.encrypt(
-                inputvalues.encrypt.good.data,
-                inputvalues.encrypt.good.fingerprint).then(
-                function (answer) {
-                    expect(answer).to.not.be.empty;
-                    expect(answer.data).to.be.a('string');
-                    expect(answer.data).to.include('BEGIN PGP MESSAGE');
-                    expect(answer.data).to.include('END PGP MESSAGE');
-                    context.decrypt(answer.data).then(function (result) {
-                        expect(result).to.not.be.empty;
-                        expect(result.data).to.be.a('string');
-                        expect(result.data).to.equal(
-                            inputvalues.encrypt.good.data);
-                        done();
-                    });
-                });
+        let data = inputvalues.encrypt.good.data;
+        context.encrypt(data, good_fpr).then(function (answer) {
+            expect(answer).to.not.be.empty;
+            expect(answer.data).to.be.a('string');
+            expect(answer.data).to.include('BEGIN PGP MESSAGE');
+            expect(answer.data).to.include('END PGP MESSAGE');
+
+            context.decrypt(answer.data).then(function (result) {
+                expect(result).to.not.be.empty;
+                expect(result.data).to.be.a('string');
+                expect(result.data).to.equal(
+                    inputvalues.encrypt.good.data);
+                done();
+            });
         });
     });
 
     it('Decrypt simple non-ascii', function (done) {
-        let prm = Gpgmejs.init();
-        prm.then(function (context) {
-            let data = encryptedData;
-            context.decrypt(data).then(
-                function (result) {
-                    expect(result).to.not.be.empty;
-                    expect(result.data).to.be.a('string');
-                    expect(result.data).to.equal(
-                        '¡Äußerste µ€ før ñoquis@hóme! Добрый день\n');
-                    done();
-                });
+        let data = encryptedData;
+        context.decrypt(data).then(function (result) {
+            expect(result).to.not.be.empty;
+            expect(result.data).to.be.a('string');
+            expect(result.data).to.equal(
+                '¡Äußerste µ€ før ñoquis@hóme! Добрый день\n');
+            done();
         });
     }).timeout(3000);
 
-    it('Roundtrip does not destroy trailing whitespace',
-        function (done) {
-            let prm = Gpgmejs.init();
-            prm.then(function (context) {
-                let data = 'Keks. \rKeks \n Keks \r\n';
-                context.encrypt(data,
-                    inputvalues.encrypt.good.fingerprint).then(
-                    function (answer) {
-                        expect(answer).to.not.be.empty;
-                        expect(answer.data).to.be.a('string');
-                        expect(answer.data).to.include(
-                            'BEGIN PGP MESSAGE');
-                        expect(answer.data).to.include(
-                            'END PGP MESSAGE');
-                        context.decrypt(answer.data).then(
-                            function (result) {
-                                expect(result).to.not.be.empty;
-                                expect(result.data).to.be.a('string');
-                                expect(result.data).to.equal(data);
-                                done();
-
-                            });
-                    });
-            });
-        }).timeout(5000);
+    it('Trailing whitespace and different line endings', function (done) {
+        const data = 'Keks. \rKeks \n Keks \r\n';
+        context.encrypt(data, good_fpr).then(function (answer) {
+            expect(answer).to.not.be.empty;
+            expect(answer.data).to.be.a('string');
+            expect(answer.data).to.include('BEGIN PGP MESSAGE');
+            expect(answer.data).to.include('END PGP MESSAGE');
 
-    for (let j = 0; j < inputvalues.encrypt.good.data_nonascii_32.length; j++){
-        it('Roundtrip with >1MB non-ascii input meeting default chunksize (' +
-            (j + 1) + '/'
-            + inputvalues.encrypt.good.data_nonascii_32.length + ')',
-        function (done) {
-            let input = inputvalues.encrypt.good.data_nonascii_32[j];
-            expect(input).to.have.length(32);
-            let prm = Gpgmejs.init();
-            prm.then(function (context) {
-                let data = '';
-                for (let i=0; i < 34 * 1024; i++){
-                    data += input;
-                }
-                context.encrypt(data,
-                    inputvalues.encrypt.good.fingerprint).then(
-                    function (answer) {
-                        expect(answer).to.not.be.empty;
-                        expect(answer.data).to.be.a('string');
-                        expect(answer.data).to.include(
-                            'BEGIN PGP MESSAGE');
-                        expect(answer.data).to.include(
-                            'END PGP MESSAGE');
-                        context.decrypt(answer.data).then(
-                            function (result) {
-                                expect(result).to.not.be.empty;
-                                expect(result.data).to.be.a('string');
-                                expect(result.data).to.equal(data);
-                                done();
-                            });
-                    });
+            context.decrypt(answer.data).then(function (result) {
+                expect(result).to.not.be.empty;
+                expect(result.data).to.be.a('string');
+                expect(result.data).to.equal(data);
+                done();
             });
-        }).timeout(3000);
-    }
+        });
+    }).timeout(5000);
 
     it('Random data, as string', function (done) {
         let data = bigString(1000);
-        let prm = Gpgmejs.init();
-        prm.then(function (context) {
-            context.encrypt(data,
-                inputvalues.encrypt.good.fingerprint).then(
-                function (answer) {
-                    expect(answer).to.not.be.empty;
-                    expect(answer.data).to.be.a('string');
-                    expect(answer.data).to.include(
-                        'BEGIN PGP MESSAGE');
-                    expect(answer.data).to.include(
-                        'END PGP MESSAGE');
-                    context.decrypt(answer.data).then(
-                        function (result) {
-                            expect(result).to.not.be.empty;
-                            expect(result.data).to.be.a('string');
-                            expect(result.data).to.equal(data);
-                            done();
-                        });
-                });
+        context.encrypt(data, good_fpr).then(function (answer) {
+            expect(answer).to.not.be.empty;
+            expect(answer.data).to.be.a('string');
+            expect(answer.data).to.include(
+                'BEGIN PGP MESSAGE');
+            expect(answer.data).to.include(
+                'END PGP MESSAGE');
+            context.decrypt(answer.data).then(function (result) {
+                expect(result).to.not.be.empty;
+                expect(result.data).to.be.a('string');
+                expect(result.data).to.equal(data);
+                done();
+            });
         });
     }).timeout(3000);
 
     it('Data, input as base64', function (done) {
         let data = inputvalues.encrypt.good.data;
         let b64data = btoa(data);
-        let prm = Gpgmejs.init();
-        prm.then(function (context) {
-            context.encrypt(b64data,
-                inputvalues.encrypt.good.fingerprint, true).then(
-                function (answer) {
-                    expect(answer).to.not.be.empty;
-                    expect(answer.data).to.be.a('string');
-                    expect(answer.data).to.include(
-                        'BEGIN PGP MESSAGE');
-                    expect(answer.data).to.include(
-                        'END PGP MESSAGE');
-                    context.decrypt(answer.data).then(
-                        function (result) {
-                            expect(result).to.not.be.empty;
-                            expect(result.data).to.be.a('string');
-                            expect(data).to.equal(data);
-                            done();
-                        });
+        context.encrypt(b64data, good_fpr, true).then(function (answer) {
+            expect(answer).to.not.be.empty;
+            expect(answer.data).to.be.a('string');
+            expect(answer.data).to.include(
+                'BEGIN PGP MESSAGE');
+            expect(answer.data).to.include(
+                'END PGP MESSAGE');
+            context.decrypt(answer.data).then(
+                function (result) {
+                    expect(result).to.not.be.empty;
+                    expect(result.data).to.be.a('string');
+                    expect(data).to.equal(data);
+                    done();
                 });
         });
     }).timeout(3000);
@@ -174,26 +123,48 @@ describe('Encryption and Decryption', function () {
     it('Random data, input as base64', function (done) {
         let data = bigBoringString(0.001);
         let b64data = btoa(data);
-        let prm = Gpgmejs.init();
-        prm.then(function (context) {
-            context.encrypt(b64data,
-                inputvalues.encrypt.good.fingerprint, true).then(
-                function (answer) {
-                    expect(answer).to.not.be.empty;
-                    expect(answer.data).to.be.a('string');
-                    expect(answer.data).to.include(
-                        'BEGIN PGP MESSAGE');
-                    expect(answer.data).to.include(
-                        'END PGP MESSAGE');
-                    context.decrypt(answer.data).then(
-                        function (result) {
-                            expect(result).to.not.be.empty;
-                            expect(result.data).to.be.a('string');
-                            expect(result.data).to.equal(b64data);
-                            done();
-                        });
+        context.encrypt(b64data, good_fpr, true).then(function (answer) {
+            expect(answer).to.not.be.empty;
+            expect(answer.data).to.be.a('string');
+            expect(answer.data).to.include(
+                'BEGIN PGP MESSAGE');
+            expect(answer.data).to.include(
+                'END PGP MESSAGE');
+            context.decrypt(answer.data).then(
+                function (result) {
+                    expect(result).to.not.be.empty;
+                    expect(result.data).to.be.a('string');
+                    expect(result.data).to.equal(b64data);
+                    done();
                 });
         });
     }).timeout(3000);
 
+    for (let j = 0; j < inputvalues.encrypt.good.data_nonascii_32.length; j++){
+        it('Roundtrip with >1MB non-ascii input meeting default chunksize (' +
+            (j + 1) + '/'
+            + inputvalues.encrypt.good.data_nonascii_32.length + ')',
+        function (done) {
+            let input = inputvalues.encrypt.good.data_nonascii_32[j];
+            expect(input).to.have.length(32);
+            let data = '';
+            for (let i=0; i < 34 * 1024; i++){
+                data += input;
+            }
+            context.encrypt(data,good_fpr).then(function (answer) {
+                expect(answer).to.not.be.empty;
+                expect(answer.data).to.be.a('string');
+                expect(answer.data).to.include(
+                    'BEGIN PGP MESSAGE');
+                expect(answer.data).to.include(
+                    'END PGP MESSAGE');
+                context.decrypt(answer.data).then(function (result) {
+                    expect(result).to.not.be.empty;
+                    expect(result.data).to.be.a('string');
+                    expect(result.data).to.equal(data);
+                    done();
+                });
+            });
+        }).timeout(5000);
+    }
 });
index 1114125..3ead815 100644 (file)
  *     Maximilian Krambach <mkrambach@intevation.de>
  */
 
-/* global describe, it, expect, Gpgmejs */
+/* global describe, it, expect, before, Gpgmejs */
 /* global inputvalues, fixedLengthString */
 
 describe('Encryption', function () {
-    it('Successful encrypt', function (done) {
-        let prm = Gpgmejs.init();
-        prm.then(function (context) {
-            context.encrypt(
-                inputvalues.encrypt.good.data,
-                inputvalues.encrypt.good.fingerprint).then(function (answer) {
-                expect(answer).to.not.be.empty;
-                expect(answer.data).to.be.a('string');
-                expect(answer.data).to.include('BEGIN PGP MESSAGE');
-                expect(answer.data).to.include('END PGP MESSAGE');
-                done();
-            });
+    let context = null;
+    const good_fpr = inputvalues.encrypt.good.fingerprint;
+    before(function(done){
+        const prm = Gpgmejs.init();
+        prm.then(function(gpgmejs){
+            context = gpgmejs;
+            done();
         });
     });
 
-    it('Successful encrypt 5 MB', function (done) {
-        let prm = Gpgmejs.init();
-        let data = fixedLengthString(5);
-        prm.then(function (context) {
-            context.encrypt(
-                data,
-                inputvalues.encrypt.good.fingerprint).then(function (answer) {
-                expect(answer).to.not.be.empty;
-                expect(answer.data).to.be.a('string');
-                expect(answer.data).to.include('BEGIN PGP MESSAGE');
-                expect(answer.data).to.include('END PGP MESSAGE');
-                done();
-            });
-        });
-    }).timeout(10000);
-
-    it('Successful encrypt 20 MB', function (done) {
-        let prm = Gpgmejs.init();
-        let data = fixedLengthString(20);
-        prm.then(function (context) {
-            context.encrypt(
-                data,
-                inputvalues.encrypt.good.fingerprint).then(function (answer) {
-                expect(answer).to.not.be.empty;
-                expect(answer.data).to.be.a('string');
-                expect(answer.data).to.include('BEGIN PGP MESSAGE');
-                expect(answer.data).to.include('END PGP MESSAGE');
-                done();
-            });
+    it('Successful encrypt', function (done) {
+        const data = inputvalues.encrypt.good.data;
+        context.encrypt(data, good_fpr).then(function (answer) {
+            expect(answer).to.not.be.empty;
+            expect(answer.data).to.be.a('string');
+            expect(answer.data).to.include('BEGIN PGP MESSAGE');
+            expect(answer.data).to.include('END PGP MESSAGE');
+            done();
         });
-    }).timeout(20000);
+    });
 
-    it('Successful encrypt 50 MB', function (done) {
-        let prm = Gpgmejs.init();
-        let data = fixedLengthString(50);
-        prm.then(function (context) {
-            context.encrypt(
-                data,
-                inputvalues.encrypt.good.fingerprint).then(function (answer) {
+    const sizes = [5,20,50];
+    for (let i=0; i < sizes.length; i++) {
+        it('Successful encrypt a ' + sizes[i] + 'MB message', function (done) {
+            const data = fixedLengthString(sizes[i]);
+            context.encrypt(data, good_fpr).then(function (answer) {
                 expect(answer).to.not.be.empty;
                 expect(answer.data).to.be.a('string');
                 expect(answer.data).to.include('BEGIN PGP MESSAGE');
                 expect(answer.data).to.include('END PGP MESSAGE');
                 done();
             });
-        });
-    }).timeout(20000);
+        }).timeout(20000);
+    }
 
     it('Sending encryption without keys fails', function (done) {
-        let prm = Gpgmejs.init();
-        prm.then(function (context) {
-            context.encrypt(
-                inputvalues.encrypt.good.data,
-                null).then(function (answer) {
-                expect(answer).to.be.undefined;
-            }, function(error){
-                expect(error).to.be.an('Error');
-                expect(error.code).to.equal('MSG_INCOMPLETE');
-                done();
-            });
+        const data = inputvalues.encrypt.good.data;
+        context.encrypt(data,null).then(function (answer) {
+            expect(answer).to.be.undefined;
+        }, function(error){
+            expect(error).to.be.an('Error');
+            expect(error.code).to.equal('MSG_INCOMPLETE');
+            done();
         });
     });
 
     it('Sending encryption without data fails', function (done) {
-        let prm = Gpgmejs.init();
-        prm.then(function (context) {
-            context.encrypt(
-                null, inputvalues.encrypt.good.keyid).then(function (answer) {
-                expect(answer).to.be.undefined;
-            }, function (error) {
-                expect(error).to.be.an.instanceof(Error);
-                expect(error.code).to.equal('MSG_INCOMPLETE');
-                done();
-            });
+        context.encrypt(null, good_fpr).then(function (answer) {
+            expect(answer).to.be.undefined;
+        }, function (error) {
+            expect(error).to.be.an.instanceof(Error);
+            expect(error.code).to.equal('MSG_INCOMPLETE');
+            done();
         });
     });
 
     it('Sending encryption with non existing keys fails', function (done) {
-        let prm = Gpgmejs.init();
-        prm.then(function (context) {
-            context.encrypt(
-                inputvalues.encrypt.good.data,
-                inputvalues.encrypt.bad.fingerprint).then(function (answer) {
-                expect(answer).to.be.undefined;
-            }, function(error){
-                expect(error).to.be.an('Error');
-                expect(error.code).to.not.be.undefined;
-                expect(error.code).to.equal('GNUPG_ERROR');
-                done();
-            });
+        const data = inputvalues.encrypt.good.data;
+        const bad_fpr = inputvalues.encrypt.bad.fingerprint;
+        context.encrypt(data, bad_fpr).then(function (answer) {
+            expect(answer).to.be.undefined;
+        }, function(error){
+            expect(error).to.be.an('Error');
+            expect(error.code).to.not.be.undefined;
+            expect(error.code).to.equal('GNUPG_ERROR');
+            done();
         });
     }).timeout(5000);
 
     it('Overly large message ( > 64MB) is rejected', function (done) {
-        let prm = Gpgmejs.init();
-        prm.then(function (context) {
-            context.encrypt(
-                fixedLengthString(65),
-                inputvalues.encrypt.good.fingerprint).then(function (answer) {
-                expect(answer).to.be.undefined;
-            }, function(error){
-                expect(error).to.be.an.instanceof(Error);
-                // TODO: there is a 64 MB hard limit at least in chrome at:
-                // chromium//extensions/renderer/messaging_util.cc:
-                // kMaxMessageLength
-                // The error will be a browser error, not from gnupg or from
-                // this library
-                done();
-            });
+        const data = fixedLengthString(65);
+        context.encrypt(data, good_fpr).then(function (answer) {
+            expect(answer).to.be.undefined;
+        }, function(error){
+            expect(error).to.be.an.instanceof(Error);
+            // TODO: there is a 64 MB hard limit at least in chrome at:
+            // chromium//extensions/renderer/messaging_util.cc:
+            // kMaxMessageLength
+            // The error will be a browser error, not from gnupg or from
+            // this library
+            done();
         });
     }).timeout(8000);
 
index 1c70190..5289eab 100644 (file)
@@ -53,6 +53,39 @@ const inputvalues = {// eslint-disable-line no-unused-vars
             // bogus fingerprint)
             fingerprint: 'CDC3A2B2860625CCBFC5AAAAAC6D1B604967FC4A'
         }
+    },
+
+    signedMessage: {
+        good: '-----BEGIN PGP SIGNED MESSAGE-----\n' +
+        'Hash: SHA256\n' +
+        '\n' +
+        'Matschige Münsteraner Marshmallows\n' +
+        '-----BEGIN PGP SIGNATURE-----\n' +
+        '\n' +
+        'iQEzBAEBCAAdFiEE1Bc1uRI2/biCBIxaIwFjXu/wywUFAltRoiMACgkQIwFjXu/w\n' +
+        'ywUvagf6ApQbZbTPOROqfTfxAPdtzJsSDKHla6D0G5wom2gJbAVb0B2YS1c3Gjpq\n' +
+        'I4kTKT1W1RRkne0mK9cexf4sjb5DQcV8PLhfmmAJEpljDFei6i/E309BvW4CZ4rG\n' +
+        'jiurf8CkaNkrwn2fXJDaT4taVCX3V5FQAlgLxgOrm1zjiGA4mz98gi5zL4hvZXF9\n' +
+        'dHY0jLwtQMVUO99q+5XC1TJfPsnteWL9m4e/YYPfYJMZZso+/0ib/yX5vHCk7RXH\n' +
+        'CfhY40nMXSYdfl8mDOhvnKcCvy8qxetFv9uCX06OqepAamu/bvxslrzocRyJ/eq0\n' +
+        'T2JfzEN+E7Y3PB8UwLgp/ZRmG8zRrQ==\n' +
+        '=ioB6\n' +
+        '-----END PGP SIGNATURE-----\n',
+        bad: '-----BEGIN PGP SIGNED MESSAGE-----\n' +
+        'Hash: SHA256\n' +
+        '\n' +
+        'Matschige Münchener Marshmallows\n' +
+        '-----BEGIN PGP SIGNATURE-----\n' +
+        '\n' +
+        'iQEzBAEBCAAdFiEE1Bc1uRI2/biCBIxaIwFjXu/wywUFAltRoiMACgkQIwFjXu/w\n' +
+        'ywUvagf6ApQbZbTPOROqfTfxAPdtzJsSDKHla6D0G5wom2gJbAVb0B2YS1c3Gjpq\n' +
+        'I4kTKT1W1RRkne0mK9cexf4sjb5DQcV8PLhfmmAJEpljDFei6i/E309BvW4CZ4rG\n' +
+        'jiurf8CkaNkrwn2fXJDaT4taVCX3V5FQAlgLxgOrm1zjiGA4mz98gi5zL4hvZXF9\n' +
+        'dHY0jLwtQMVUO99q+5XC1TJfPsnteWL9m4e/YYPfYJMZZso+/0ib/yX5vHCk7RXH\n' +
+        'CfhY40nMXSYdfl8mDOhvnKcCvy8qxetFv9uCX06OqepAamu/bvxslrzocRyJ/eq0\n' +
+        'T2JfzEN+E7Y3PB8UwLgp/ZRmG8zRrQ==\n' +
+        '=ioB6\n' +
+        '-----END PGP SIGNATURE-----\n',
     }
 };
 
@@ -83,7 +116,7 @@ function bigUint8(megabytes){// eslint-disable-line no-unused-vars
     let maxlength = 1024 * 1024 * megabytes;
     let uint = new Uint8Array(maxlength);
     for (let i= 0; i < maxlength; i++){
-        uint[i] = Math.random() * Math.floor(256);
+        uint[i] = Math.floor(Math.random() * 256);
     }
     return uint;
 }
@@ -125,6 +158,27 @@ function slightlyLessBoringString(megabytes, set){
     return string.join('');
 }
 
+// Take a gpg looking string and destroy it a bit by changing random values
+// eslint-disable-next-line no-unused-vars
+function destroylegitimateGpg(string, mutations=5){
+    const allowed = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/\n';
+    for (let i=0; i < mutations.length; i++){
+        // leave the first and last 35 chars (header/footer) intact
+        let position = Math.floor(Math.random() *(string.length - 70)) + 35;
+        let str0 = string.substring(0,position - 1);
+        let str1 = string.substring(position, position + 1);
+        let str2 = string.substring(position +1);
+        let success = false;
+        while (!success){
+            let newchar = Math.floor(Math.random() * allowed.length);
+            if (newchar !== str1){
+                string = str0 + newchar + str2;
+                success = true;
+            }
+        }
+    }
+}
+
 // Data encrypted with testKey
 const encryptedData =// eslint-disable-line no-unused-vars
     '-----BEGIN PGP MESSAGE-----\n' +
index e148d1c..03f0390 100644 (file)
  * Author(s):
  *     Maximilian Krambach <mkrambach@intevation.de>
  */
-/* global describe, it, expect, Gpgmejs */
+/* global describe, it, before, expect, Gpgmejs */
 /* global bigString, inputvalues */
 
 describe('Long running Encryption/Decryption', function () {
+    let context = null;
+    const good_fpr = inputvalues.encrypt.good.fingerprint;
+    before(function(done){
+        const prm = Gpgmejs.init();
+        prm.then(function(gpgmejs){
+            context = gpgmejs;
+            done();
+        });
+    });
+
     for (let i=0; i < 101; i++) {
-        it('Successful encrypt/decrypt completely random data ' +
-            (i+1) + '/100', function (done) {
-            let prm = Gpgmejs.init();
-            let data = bigString(2*1024*1024);
-            prm.then(function (context) {
-                context.encrypt(data,
-                    inputvalues.encrypt.good.fingerprint).then(
-                    function (answer){
-                        expect(answer).to.not.be.empty;
-                        expect(answer.data).to.be.a('string');
-                        expect(answer.data).to.include(
-                            'BEGIN PGP MESSAGE');
-                        expect(answer.data).to.include(
-                            'END PGP MESSAGE');
-                        context.decrypt(answer.data).then(
-                            function(result){
-                                expect(result).to.not.be.empty;
-                                expect(result.data).to.be.a('string');
-                                /*
-                                if (result.data.length !== data.length) {
-                                    console.log('diff: ' +
-                                    (result.data.length - data.length));
-                                    for (let i=0; i < result.data.length; i++){
-                                        if (result.data[i] !== data[i]){
-                                            console.log('position: ' + i);
-                                            console.log('result : ' +
-                                            result.data.charCodeAt(i) +
-                                            result.data[i-2] +
-                                            result.data[i-1] +
-                                            result.data[i] +
-                                            result.data[i+1] +
-                                            result.data[i+2]);
-                                            console.log('original: ' +
-                                            data.charCodeAt(i) +
-                                            data[i-2] +
-                                            data[i-1] +
-                                            data[i] +
-                                            data[i+1] +
-                                            data[i+2]);
-                                            break;
-                                        }
-                                    }
-                                }
-                                */
-                                expect(result.data).to.equal(data);
-                                done();
-                            });
-                    });
+        it('Successful encrypt/decrypt completely random data '
+            + (i+1) + '/100', function (done) {
+            const data = bigString(2*1024*1024);
+            context.encrypt(data,good_fpr).then(function (answer){
+                expect(answer).to.not.be.empty;
+                expect(answer.data).to.be.a('string');
+                expect(answer.data).to.include('BEGIN PGP MESSAGE');
+                expect(answer.data).to.include('END PGP MESSAGE');
+                context.decrypt(answer.data).then(function(result){
+                    expect(result).to.not.be.empty;
+                    expect(result.data).to.be.a('string');
+                    expect(result.data).to.equal(data);
+                    done();
+                });
             });
-        }).timeout(8000);
+        }).timeout(15000);
     }
 
 });
index ffd2d5d..2763dad 100644 (file)
  *     Maximilian Krambach <mkrambach@intevation.de>
  */
 
-/* global describe, it, expect, Gpgmejs */
+/* global describe, it, expect, before, Gpgmejs */
 /* global bigString, inputvalues */
 
 describe('Signing', function () {
+    let context = null;
+    const good_fpr = inputvalues.encrypt.good.fingerprint;
+
+    before(function(done){
+        const prm = Gpgmejs.init();
+        prm.then(function(gpgmejs){
+            context = gpgmejs;
+            done();
+        });
+    });
+
     it('Sign a message', function (done) {
-        let prm = Gpgmejs.init();
-        prm.then(function (context) {
-            let data = bigString(100);
-            context.sign(
-                data,
-                inputvalues.encrypt.good.fingerprint).then(function (answer) {
-                expect(answer).to.not.be.empty;
-                expect(answer.data).to.be.a('string');
-                expect(answer.data).to.include('BEGIN PGP SIGNATURE');
-                expect(answer.data).to.include('END PGP SIGNATURE');
-                expect(answer.data).to.include(data);
-                done();
-            });
+        const data = bigString(100);
+        context.sign(data, good_fpr).then(function (answer) {
+            expect(answer).to.not.be.empty;
+            expect(answer.data).to.be.a('string');
+            expect(answer.data).to.include('BEGIN PGP SIGNATURE');
+            expect(answer.data).to.include('END PGP SIGNATURE');
+            expect(answer.data).to.include(data);
+            done();
         });
     });
+
     it('Detached sign a message', function (done) {
-        let prm = Gpgmejs.init();
-        prm.then(function (context) {
-            let data = bigString(100);
-            context.sign(
-                data,
-                inputvalues.encrypt.good.fingerprint,
-                'detached'
-            ).then(function (answer) {
-                expect(answer).to.not.be.empty;
-                expect(answer.data).to.be.a('string');
-                expect(answer.data).to.include(data);
-                expect(answer.signature).to.be.a('string');
-                expect(answer.signature).to.be.a('string');
-                done();
-            });
+        const data = bigString(100);
+        context.sign(data,good_fpr, 'detached').then(function (answer) {
+            expect(answer).to.not.be.empty;
+            expect(answer.data).to.be.a('string');
+            expect(answer.data).to.include(data);
+            expect(answer.signature).to.be.a('string');
+            expect(answer.signature).to.be.a('string');
+            done();
         });
     });
 
index e8e2c76..1617e2d 100644 (file)
  *     Maximilian Krambach <mkrambach@intevation.de>
  */
 
-/* global describe, it, expect, bigString, inputvalues, Gpgmejs */
+/* global describe, it, expect, before, bigString, inputvalues, Gpgmejs */
 
-let verifyData = {
-    signedMessage: '-----BEGIN PGP SIGNED MESSAGE-----\n' +
-    'Hash: SHA256\n' +
-    '\n' +
-    'Matschige Münsteraner Marshmallows\n' +
-    '-----BEGIN PGP SIGNATURE-----\n' +
-    '\n' +
-    'iQEzBAEBCAAdFiEE1Bc1uRI2/biCBIxaIwFjXu/wywUFAltRoiMACgkQIwFjXu/w\n' +
-    'ywUvagf6ApQbZbTPOROqfTfxAPdtzJsSDKHla6D0G5wom2gJbAVb0B2YS1c3Gjpq\n' +
-    'I4kTKT1W1RRkne0mK9cexf4sjb5DQcV8PLhfmmAJEpljDFei6i/E309BvW4CZ4rG\n' +
-    'jiurf8CkaNkrwn2fXJDaT4taVCX3V5FQAlgLxgOrm1zjiGA4mz98gi5zL4hvZXF9\n' +
-    'dHY0jLwtQMVUO99q+5XC1TJfPsnteWL9m4e/YYPfYJMZZso+/0ib/yX5vHCk7RXH\n' +
-    'CfhY40nMXSYdfl8mDOhvnKcCvy8qxetFv9uCX06OqepAamu/bvxslrzocRyJ/eq0\n' +
-    'T2JfzEN+E7Y3PB8UwLgp/ZRmG8zRrQ==\n' +
-    '=ioB6\n' +
-    '-----END PGP SIGNATURE-----\n'
-};
 
-describe('Verify data', function () {
+
+describe('Verifying data', function () {
+    let context = null;
+    before(function(done){
+        const prm = Gpgmejs.init();
+        prm.then(function(gpgmejs){
+            context = gpgmejs;
+            done();
+        });
+    });
     it('Successful verify message', function (done) {
-        let message = verifyData.signedMessage;
-        let prm = Gpgmejs.init();
-        prm.then(function (context) {
-            context.verify(message).then(function(result){
-                expect(result.data).to.be.a('string');
-                expect(result.all_valid).to.be.true;
-                expect(result.count).to.equal(1);
-                expect(result.signatures.good).to.be.an('array');
-                expect(result.signatures.good.length).to.equal(1);
-                expect(result.signatures.good[0].fingerprint)
-                    .to.be.a('string');
-                expect(result.signatures.good[0].valid).to.be.true;
-                done();
-            });
+        const message = inputvalues.signedMessage.good;
+        context.verify(message).then(function(result){
+            expect(result.data).to.be.a('string');
+            expect(result.all_valid).to.be.true;
+            expect(result.count).to.equal(1);
+            expect(result.signatures.good).to.be.an('array');
+            expect(result.signatures.good.length).to.equal(1);
+            expect(result.signatures.good[0].fingerprint).to.be.a('string');
+            expect(result.signatures.good[0].valid).to.be.true;
+            done();
+        });
+    });
+
+    it('Successfully recognize changed cleartext', function (done) {
+        const message = inputvalues.signedMessage.bad;
+        context.verify(message).then(function(result){
+            expect(result.data).to.be.a('string');
+            expect(result.all_valid).to.be.false;
+            expect(result.count).to.equal(1);
+            expect(result.signatures.bad).to.be.an('array');
+            expect(result.signatures.bad.length).to.equal(1);
+            expect(result.signatures.bad[0].fingerprint).to.be.a('string');
+            expect(result.signatures.bad[0].valid).to.be.false;
+            done();
         });
     });
 
     it('Encrypt-Sign-Verify random message', function (done) {
-        let message = bigString(2000);
+        const message = bigString(2000);
         let fpr = inputvalues.encrypt.good.fingerprint;
-        let prm = Gpgmejs.init();
-        prm.then(function (context) {
-            context.encrypt(message, fpr).then(function(message_enc){
-                context.sign(message_enc.data, fpr).then(function(message_encsign){
-                    context.verify(message_encsign.data).then(function(result){
-                        expect(result.data).to.equal(message_enc.data);
-                        expect(result.data).to.be.a('string');
-                        expect(result.all_valid).to.be.true;
-                        expect(result.count).to.equal(1);
-                        expect(result.signatures.good).to.be.an('array');
-                        expect(result.signatures.good.length).to.equal(1);
-                        expect(result.signatures.good[0].fingerprint)
-                            .to.equal(fpr);
-                        expect(result.signatures.good[0].valid).to.be.true;
-                        done();
-                    });
+        context.encrypt(message, fpr).then(function(message_enc){
+            context.sign(message_enc.data, fpr).then(function(message_encsign){
+                context.verify(message_encsign.data).then(function(result){
+                    expect(result.data).to.equal(message_enc.data);
+                    expect(result.data).to.be.a('string');
+                    expect(result.all_valid).to.be.true;
+                    expect(result.count).to.equal(1);
+                    expect(result.signatures.good).to.be.an('array');
+                    expect(result.signatures.good.length).to.equal(1);
+                    expect(
+                        result.signatures.good[0].fingerprint).to.equal(fpr);
+                    expect(result.signatures.good[0].valid).to.be.true;
+                    done();
                 });
             });
         });