From 3201ded91f91fcdf501ad966380b6ff29d798b09 Mon Sep 17 00:00:00 2001 From: Maximilian Krambach Date: Thu, 30 Aug 2018 15:00:19 +0200 Subject: [PATCH] js: add tests -- * BrowserTestExtension/tests: - decryptTest.js: Check Decryption and return values of binary data - encryptTest.js: Return data type of armored/non-armored encryption - added a small encoded input png for testing * DemoExtension/maindemo.js: Fixed unexpected usage of the Demo encrypt (non-armored) --- .../BrowserTestExtension/tests/decryptTest.js | 24 ++++++- .../BrowserTestExtension/tests/encryptTest.js | 45 +++++++++++- .../BrowserTestExtension/tests/inputvalues.js | 70 +++++++++++++++++++ lang/js/DemoExtension/maindemo.js | 4 +- 4 files changed, 138 insertions(+), 5 deletions(-) diff --git a/lang/js/BrowserTestExtension/tests/decryptTest.js b/lang/js/BrowserTestExtension/tests/decryptTest.js index 8852cb6a..606d96ed 100644 --- a/lang/js/BrowserTestExtension/tests/decryptTest.js +++ b/lang/js/BrowserTestExtension/tests/decryptTest.js @@ -22,7 +22,7 @@ */ /* global describe, it, before, expect, Gpgmejs */ -/* global bigString, inputvalues, sabotageMsg*/ +/* global bigString, inputvalues, sabotageMsg, binaryData */ describe('Decryption', function () { let context = null; @@ -76,4 +76,26 @@ describe('Decryption', function () { done(); }); }); + + it('decrypt of a png, result as base64', function (done){ + const data = binaryData.encryptedArmored; + context.decrypt({ data: data, expect: 'base64' }) + .then(function (result){ + expect(result.data).to.be.a('String'); + expect(result.data).to.equal(binaryData.base64); + expect(result.format).to.equal('base64'); + done(); + }); + }); + + it('decrypt of a png, result as Uint8Array', function (done){ + const data = binaryData.encryptedArmored; + context.decrypt({ data: data, expect: 'uint8' }) + .then(function (result){ + expect(result.data).to.be.an('Uint8Array'); + expect(result.format).to.equal('uint8'); + done(); + }); + }); + }); \ No newline at end of file diff --git a/lang/js/BrowserTestExtension/tests/encryptTest.js b/lang/js/BrowserTestExtension/tests/encryptTest.js index ccdb4994..d97b458d 100644 --- a/lang/js/BrowserTestExtension/tests/encryptTest.js +++ b/lang/js/BrowserTestExtension/tests/encryptTest.js @@ -22,7 +22,7 @@ */ /* global describe, it, expect, before, Gpgmejs */ -/* global inputvalues, fixedLengthString */ +/* global inputvalues, fixedLengthString, bigString */ describe('Encryption', function () { let context = null; @@ -47,6 +47,47 @@ describe('Encryption', function () { }); }); + + it( 'encrypt with \'armor\': true should returned an armored block', + function (done){ + const data = bigString(1000); + context.encrypt({ data: data, publicKeys: good_fpr, armor: 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'); + expect(answer.format).to.equal('ascii'); + done(); + }); + }); + + it( 'encrypt with \'armor\': false and \'expected\': \'uint8\' returns ' + + 'an Uint8Array', function (done) { + const data = bigString(1000); + context.encrypt({ + data: data, publicKeys: good_fpr, armor: false, expect: 'uint8' + }).then(function (answer){ + expect(answer).to.not.be.empty; + expect(answer.data).to.be.a('Uint8Array'); + expect(answer.format).to.equal('uint8'); + done(); + }); + }); + + it( 'encrypt with \'armor\': false and \'expected\': \'base64\' returns ' + + 'a base64 string', function (done) { + const data = bigString(1000); + context.encrypt({ + data: data, publicKeys: good_fpr, armor: false, expect: 'base64' + }).then(function (answer){ + expect(answer).to.not.be.empty; + expect(answer.data).to.be.a('string'); + expect(answer.format).to.equal('base64'); + done(); + }); + }); + const sizes = [5,20,50]; for (let i=0; i < sizes.length; i++) { it('Successful encrypt a ' + sizes[i] + 'MB message', function (done) { @@ -114,5 +155,5 @@ describe('Encryption', function () { }); }).timeout(8000); - // TODO check different valid parameter + }); diff --git a/lang/js/BrowserTestExtension/tests/inputvalues.js b/lang/js/BrowserTestExtension/tests/inputvalues.js index 5c2abf36..6b66621c 100644 --- a/lang/js/BrowserTestExtension/tests/inputvalues.js +++ b/lang/js/BrowserTestExtension/tests/inputvalues.js @@ -336,3 +336,73 @@ function sabotageMsg (msg, rate = 0.01, p= [35,35]){ } return msg; } + +// a simple png +// eslint-disable-next-line no-unused-vars +const binaryData = { + base64: + 'iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAIAAABvFaqvAAAEwElEQVQ4y62R228b1xn' + + 'E51z27I271K64okTqRkuOLRuNHcNBCqTwS//qvvYCtAmQNrETO7akyJJsipRIUeSSez' + + 'vnfH1IkBboW5F5mpf5ATPDiAi/hTh+I8n/I/NrCcbY/4DI/GKY+E/CGLIWROCccQ7Or' + + 'bVVXWmttTHKUY6UjuP8jJMggilhVjAluILwIUMwRlpTUVBVQWsoxT1PS3E7v7u6Ho0m' + + '48boOIz63V6WdsIgZIxJmBXyMyx/QjWEbCPYReuQRNsWhbm5tqMRFQVPU6x3JlZ/e/r' + + 'm76+/OR6elbrKovT5/ae/f/L5YGcQeL5EOcHsa8z/hOY1eIbVF6b5o/YeNrnWFxf2+5' + + 'fs5kY9PDKHdlgu/vrPv/357F95UxLo7G44nk+EEO2orbKuRHOL1feo/wJ8hFEmn5dNv' + + 'JB8MRP1xYV79j68HrEss1ub15Ph8dXpoil+3lqTPZ+PXp7+8On9x51kXcLUsAtgARBR' + + 'ZfRpnn93voouxsJM5ptJsp1lfn+HwtDOpAX77/sM2bvVfFUW2hgJJ4bTQ9UH5YA1psz' + + 'zyYery69ezaihzx883BgM2GDgxFGqq92N3YvltDA1CGDwJU8j3/UYZ1rCTRE+QfMBpQ' + + 'BdgRzwAFwyKSAV1jvU38HGptvy+4J/8ej5sih+uHq31HUg+ePexvNHg27mcGch4aZY+' + + '4wsB23Z4j1XCNeOtr0dG6Eyst3tFV47ZwpQcZQcDR6YUnfD9UWxSBL/06Peo0+67WRC' + + 'cinBBKmMnE8s98gcQjlh1Nn2sqArxiusyP1Yu8WdyUzVYhVpkiQ2/PX7W4PdQXdvP1x' + + 'Lx0x9Z8BkXVVU1rQgGjZ2VPNOy9nrxPGmjdRkUs8XuqhEAcOIrCzG1zdv3/x4fn7+2b' + + 'OngTMIPZfL0mJIsHJyfc3LUlyN5Jt37MfX4uixs96TmcO5MpxqggWKhlaNiUBE1pC2M' + + 'BaWOOPM40gY7hEq+fLVKx/olOVa07hEgjEwzhnzlYg9GXs2r21L8dgXLddGaWtwtBv1' + + '/LgbWE9rzn2khK7FWPT7/Xy5bIVBO03U5qbc2+fdDZkkJBUDlGCh4h2fd0LpKlOw6VQ' + + 'MF+FQxwvlyziMAyXBJxbH8vjkhKw92N8peplyOcWRE7mMk6t4J1KRJ1arQsIwW1qqKs' + + 'ymwekV+wcTTKLeNt0OdRwowJEvXrxoJ+1wM8kTNncWQpVtIVLWapEbKIebmvQyv72pl' + + '3da2VJMSzOu1RWAnG7KpjC1VOhK/lR++YcvucPIa6biw6V+zSy7J5/6uh3IlrEMpilm' + + 'N9fvXs0uT4KtnuoHvfBBg4Y46ztHbRvLleSrFpQrDw4OGqpv9Xi0qqf1R1hs6oPaVNp' + + 'oznlT1+VycXd5cvv2a6JnycaTA/93mRqAsYytp0UoRzO7mLPQlVEUNaY2dZ3RVoVnAN' + + 'ZExhpRmrJmDYyRXtDePgDQ3r63nuwEaUaKM8CtyZlN6fhEv33DuxtScAHmhIh62I+cx' + + 'BjDaye/XU3zO85E6HvK9bODo7X+nhdGYdrx/VgoF9ZCL20zqW9v6f17ayEBCCYDGTpc' + + 'Rc5aXdez5d3Hy/PT41MAh4eH+7s7ydY2Z8xRSjiOVC5jDLDkuBRFYm8fAEvSfwPxgHl' + + 'kzr8e4QAAAABJRU5ErkJggg==', + encryptedArmored: '-----BEGIN PGP MESSAGE-----\n' + + '\n' + + 'hQEMA6B8jfIUScGEAQgA3m9gtJswzXITlX3yJslszQSBBtHb3jTquF6ZoB5NPxMC\n' + + '5sX1WkjemyjYOy/pscLb8JRedj+owfaHYAGed1h+SM8iVHrHSVImbq+okzKbkYTB\n' + + 'lYoBK63y2WHjMdMEjHHWl5CPfyzmAutwix0sWGcDLwNdO31eUdKHtjPi69k+IEw2\n' + + 'hJ0+hdulttJk5jvNtW0mmuWjzKPgekZX7AFZnO+cJEgdDXEZ9oOD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+ + '=AkaP\n' + + '-----END PGP MESSAGE-----\n' +}; \ No newline at end of file diff --git a/lang/js/DemoExtension/maindemo.js b/lang/js/DemoExtension/maindemo.js index b472bcc8..c992e7e1 100644 --- a/lang/js/DemoExtension/maindemo.js +++ b/lang/js/DemoExtension/maindemo.js @@ -29,8 +29,8 @@ document.addEventListener('DOMContentLoaded', function () { function (){ let data = document.getElementById('inputtext').value; let keyId = document.getElementById('pubkey').value; - gpgmejs.encrypt({ data: data, publicKeys: keyId, armor:false }).then( - function (answer){ + gpgmejs.encrypt({ data: data, publicKeys: keyId, armor: true }) + .then(function (answer){ if (answer.data){ document.getElementById( 'answer').value = answer.data;