diff --git a/lang/js/BrowserTestExtension/browsertest.html b/lang/js/BrowserTestExtension/browsertest.html index d2c6396f..ce037a11 100644 --- a/lang/js/BrowserTestExtension/browsertest.html +++ b/lang/js/BrowserTestExtension/browsertest.html @@ -1,23 +1,25 @@

Browsertest

+ + diff --git a/lang/js/BrowserTestExtension/runbrowsertest.js b/lang/js/BrowserTestExtension/runbrowsertest.js index 39bc3fb9..308c716d 100644 --- a/lang/js/BrowserTestExtension/runbrowsertest.js +++ b/lang/js/BrowserTestExtension/runbrowsertest.js @@ -1,21 +1,22 @@ /* gpgme.js - Javascript integration for gpgme * Copyright (C) 2018 Bundesamt für Sicherheit in der Informationstechnik * * This file is part of GPGME. * * GPGME is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * GPGME 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program; if not, see . * SPDX-License-Identifier: LGPL-2.1+ */ mocha.run(); +Gpgmejs_test.unittests(); diff --git a/lang/js/BrowserTestExtension/tests/encryptTest.js b/lang/js/BrowserTestExtension/tests/encryptTest.js index e6000003..2178efac 100644 --- a/lang/js/BrowserTestExtension/tests/encryptTest.js +++ b/lang/js/BrowserTestExtension/tests/encryptTest.js @@ -1,71 +1,63 @@ /* gpgme.js - Javascript integration for gpgme * Copyright (C) 2018 Bundesamt für Sicherheit in der Informationstechnik * * This file is part of GPGME. * * GPGME is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * GPGME 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program; if not, see . * SPDX-License-Identifier: LGPL-2.1+ */ describe('Encryption', function(){ - - it('Successfull encrypt', function(done){ + it('Successfull encrypt', function(){ 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(); - }, function(err){ - expect(err).to.be.undefined; - done(); }); }); }); it('Sending encryption without keys fails', function(){ let prm = Gpgmejs.init(); prm.then(function(context){ context.encrypt( inputvalues.encrypt.good.data, null).then(function(answer){ expect(answer).to.be.undefined; - done(); }, function(error){ expect(error).to.be.an('Error'); expect(error.code).to.equal('MSG_INCOMPLETE'); - done() + //TODO: MSG_INCOMPLETE desired, GNUPG_ERROR coming }); }); }); it('Sending encryption without data fails', function(){ 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(); + expect(error.code).to.equal('PARAM_WRONG'); }); }); }); - // TODO check different valid parameter }); diff --git a/lang/js/README_testing b/lang/js/README_testing new file mode 100644 index 00000000..b61ca1a6 --- /dev/null +++ b/lang/js/README_testing @@ -0,0 +1,14 @@ +Test extension: + +The test extension contains tests with mocha and chai. It will be packed as an +extra extension (see build_extension.sh). + +Tests from BrowserTestExtension/tests will be run against the gpgmejs.bundle.js +itself. They aim to test the outward facing functionality and API. + +Unittests as defined in ./unittests.js will be bundled in +gpgmejs_unittests.bundle.js, and test the separate components of gpgmejs, +which mostly are not exported. + +The BrowserExtension can be installed the same way as the DemoExtension +(see README). \ No newline at end of file diff --git a/lang/js/build_extensions.sh b/lang/js/build_extensions.sh index b99a362c..91d5479b 100755 --- a/lang/js/build_extensions.sh +++ b/lang/js/build_extensions.sh @@ -1,15 +1,17 @@ #/!bin/bash npx webpack --config webpack.conf.js +npx webpack --config webpack.conf_unittests.js mkdir -p BrowserTestExtension/libs cp node_modules/chai/chai.js \ node_modules/mocha/mocha.css \ node_modules/mocha/mocha.js \ - build/gpgmejs.bundle.js BrowserTestExtension/libs + build/gpgmejs.bundle.js \ + build/gpgmejs_unittests.bundle.js BrowserTestExtension/libs rm -rf build/extensions mkdir -p build/extensions zip -r build/extensions/browsertest.zip BrowserTestExtension mkdir -p DemoExtension/libs cp build/gpgmejs.bundle.js DemoExtension/libs zip -r build/extensions/demoextension.zip DemoExtension diff --git a/lang/js/src/Helpers.js b/lang/js/src/Helpers.js index 9a69f851..ea056fff 100644 --- a/lang/js/src/Helpers.js +++ b/lang/js/src/Helpers.js @@ -1,103 +1,103 @@ /* gpgme.js - Javascript integration for gpgme * Copyright (C) 2018 Bundesamt für Sicherheit in der Informationstechnik * * This file is part of GPGME. * * GPGME is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * GPGME 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program; if not, see . * SPDX-License-Identifier: LGPL-2.1+ */ import { gpgme_error } from "./Errors"; import { GPGME_Key } from "./Key"; /** * Tries to return an array of fingerprints, either from input fingerprints or * from Key objects * @param {Key |Array| GPGME_Key | Array|String|Array} input * @returns {Array} Array of fingerprints. */ export function toKeyIdArray(input){ if (!input){ gpgme_error('MSG_NO_KEYS'); return []; } if (!Array.isArray(input)){ input = [input]; } let result = []; for (let i=0; i < input.length; i++){ if (typeof(input[i]) === 'string'){ if (isFingerprint(input[i]) === true){ result.push(input[i]); } else { gpgme_error('MSG_NOT_A_FPR'); } } else if (typeof(input[i]) === 'object'){ let fpr = ''; if (input[i] instanceof GPGME_Key){ fpr = input[i].fingerprint; } else if (input[i].hasOwnProperty('primaryKey') && input[i].primaryKey.hasOwnProperty(getFingerprint)){ fpr = input[i].primaryKey.getFingerprint(); } if (isFingerprint(fpr) === true){ result.push(fpr); } else { gpgme_error('MSG_NOT_A_FPR'); } } else { return gpgme_error('PARAM_WRONG'); } } if (result.length === 0){ gpgme_error('MSG_NO_KEYS'); return []; } else { return result; } }; /** * check if values are valid hexadecimal values of a specified length * @param {*} key input value. * @param {int} len the expected length of the value */ function hextest(key, len){ if (!key || typeof(key) !== "string"){ return false; } if (key.length !== len){ return false; } let regexp= /^[0-9a-fA-F]*$/i; return regexp.test(key); }; /** * check if the input is a valid Hex string with a length of 40 */ export function isFingerprint(string){ return hextest(string, 40); }; /** - * check if the input is a valid Hex string with a length of 16 + * TODO no usage; check if the input is a valid Hex string with a length of 16 */ -export function isLongId(string){ +function isLongId(string){ return hextest(string, 16); }; // TODO still not needed anywhere function isShortId(string){ return hextest(string, 8); }; diff --git a/lang/js/src/Key.js b/lang/js/src/Key.js index 1e0d3195..6d3cf17d 100644 --- a/lang/js/src/Key.js +++ b/lang/js/src/Key.js @@ -1,233 +1,242 @@ /* gpgme.js - Javascript integration for gpgme * Copyright (C) 2018 Bundesamt für Sicherheit in der Informationstechnik * * This file is part of GPGME. * * GPGME is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * GPGME 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program; if not, see . * SPDX-License-Identifier: LGPL-2.1+ */ /** * The key class allows to query the information defined in gpgme Key Objects * (see https://www.gnupg.org/documentation/manuals/gpgme/Key-objects.html) * * This is a stub, as the gpgme-json side is not yet implemented * */ import { isFingerprint } from './Helpers' import { gpgme_error } from './Errors' import { createMessage } from './Message'; import { permittedOperations } from './permittedOperations'; import { Connection } from './Connection'; export function createKey(fingerprint, parent){ if (!isFingerprint(fingerprint)){ return gpgme_error('KEY_INVALID'); } if ( parent instanceof Connection){ return new GPGME_Key(fingerprint, parent); } else if ( parent.hasOwnProperty('connection') && parent.connection instanceof Connection){ return new GPGME_Key(fingerprint, parent.connection); } } export class GPGME_Key { constructor(fingerprint, connection){ this.fingerprint = fingerprint; this.connection = connection; } set connection(conn){ if (this._connection instanceof Connection) { gpgme_error('CONN_ALREADY_CONNECTED'); } else if (conn instanceof Connection ) { this._connection = conn; } } get connection(){ + if (!this._fingerprint){ + return gpgme_error('KEY_INVALID'); + } if (!this._connection instanceof Connection){ return gpgme_error('CONN_NO_CONNECT'); } else { return this._connection; } } set fingerprint(fpr){ if (isFingerprint(fpr) === true && !this._fingerprint){ this._fingerprint = fpr; } } get fingerprint(){ + if (!this._fingerprint){ + return gpgme_error('KEY_INVALID'); + } return this._fingerprint; } /** * hasSecret returns true if a secret subkey is included in this Key */ get hasSecret(){ return this.checkKey('secret'); } get isRevoked(){ return this.checkKey('revoked'); } get isExpired(){ return this.checkKey('expired'); } get isDisabled(){ return this.checkKey('disabled'); } get isInvalid(){ return this.checkKey('invalid'); } get canEncrypt(){ return this.checkKey('can_encrypt'); } get canSign(){ return this.checkKey('can_sign'); } get canCertify(){ return this.checkKey('can_certify'); } get canAuthenticate(){ return this.checkKey('can_authenticate'); } get isQualified(){ return this.checkKey('is_qualified'); } get armored(){ let msg = createMessage ('export_key'); msg.setParameter('armor', true); if (msg instanceof Error){ - return gpgme_error('INVALID_KEY'); + return gpgme_error('KEY_INVALID'); } this.connection.post(msg).then(function(result){ return result.data; }); // TODO return value not yet checked. Should result in an armored block // in correct encoding } /** * TODO returns true if this is the default key used to sign */ get isDefault(){ throw('NOT_YET_IMPLEMENTED'); } /** * get the Key's subkeys as GPGME_Key objects * @returns {Array} */ get subkeys(){ return this.checkKey('subkeys').then(function(result){ // TBD expecting a list of fingerprints if (!Array.isArray(result)){ result = [result]; } let resultset = []; for (let i=0; i < result.length; i++){ let subkey = new GPGME_Key(result[i], this.connection); if (subkey instanceof GPGME_Key){ resultset.push(subkey); } } return Promise.resolve(resultset); }, function(error){ //TODO this.checkKey fails }); } /** * creation time stamp of the key * @returns {Date|null} TBD */ get timestamp(){ return this.checkKey('timestamp'); //TODO GPGME: -1 if the timestamp is invalid, and 0 if it is not available. } /** * The expiration timestamp of this key TBD * @returns {Date|null} TBD */ get expires(){ return this.checkKey('expires'); // TODO convert to Date; check for 0 } /** * getter name TBD * @returns {String|Array} The user ids associated with this key */ get userIds(){ return this.checkKey('uids'); } /** * @returns {String} The public key algorithm supported by this subkey */ get pubkey_algo(){ return this.checkKey('pubkey_algo'); } /** * generic function to query gnupg information on a key. * @param {*} property The gpgme-json property to check. * TODO: check if Promise.then(return) */ checkKey(property){ + if (!this._fingerprint){ + return gpgme_error('KEY_INVALID'); + } return gpgme_error('NOT_YET_IMPLEMENTED'); // TODO: async is not what is to be ecpected from Key information :( if (!property || typeof(property) !== 'string' || !permittedOperations['keyinfo'].hasOwnProperty(property)){ return gpgme_error('PARAM_WRONG'); } let msg = createMessage ('keyinfo'); if (msg instanceof Error){ return gpgme_error('PARAM_WRONG'); } msg.setParameter('fingerprint', this.fingerprint); this.connection.post(msg).then(function(result, error){ if (error){ return gpgme_error('GNUPG_ERROR',error.msg); } else if (result.hasOwnProperty(property)){ return result[property]; } else if (property == 'secret'){ // TBD property undefined means "not true" in case of secret? return false; } else { return gpgme_error('CONN_UNEXPECTED_ANSWER'); } }, function(error){ return gpgme_error('GENERIC_ERROR'); }); } }; \ No newline at end of file diff --git a/lang/js/src/Keyring.js b/lang/js/src/Keyring.js index 2cf87c24..4596035a 100644 --- a/lang/js/src/Keyring.js +++ b/lang/js/src/Keyring.js @@ -1,162 +1,162 @@ /* gpgme.js - Javascript integration for gpgme * Copyright (C) 2018 Bundesamt für Sicherheit in der Informationstechnik * * This file is part of GPGME. * * GPGME is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * GPGME 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program; if not, see . * SPDX-License-Identifier: LGPL-2.1+ */ import {createMessage} from './Message' import {GPGME_Key} from './Key' -import { isFingerprint, isLongId } from './Helpers'; +import { isFingerprint } from './Helpers'; import { gpgme_error } from './Errors'; import { Connection } from './Connection'; export class GPGME_Keyring { constructor(connection){ this.connection = connection; } set connection(connection){ if (!this._connection && connection instanceof Connection){ this._connection = connection; } } get connection(){ if (this._connection instanceof Connection){ if (this._connection.isConnected){ return this._connection; } return gpgme_error('CONN_DISCONNECTED'); } return gpgme_error('CONN_NO_CONNECT'); } /** * @param {String} (optional) pattern A pattern to search for, in userIds or KeyIds * @param {Boolean} (optional) Include listing of secret keys * @returns {Promise.>} * */ getKeys(pattern, include_secret){ let msg = createMessage('listkeys'); if (msg instanceof Error){ return Promise.reject(msg); } if (pattern && typeof(pattern) === 'string'){ msg.setParameter('pattern', pattern); } if (include_secret){ msg.setParameter('with-secret', true); } let me = this; this.connection.post(msg).then(function(result){ let fpr_list = []; let resultset = []; if (!Array.isArray(result.keys)){ //TODO check assumption keys = Array fpr_list = [result.keys]; } else { fpr_list = result.keys; } for (let i=0; i < fpr_list.length; i++){ let newKey = new GPGME_Key(fpr_list[i], me._connection); if (newKey instanceof GPGME_Key){ resultset.push(newKey); } } return Promise.resolve(resultset); }, function(error){ //TODO error handling }); } /** * @param {Object} flags subset filter expecting at least one of the * filters described below. True will filter on the condition, False will * reverse the filter, if not present or undefined, the filter will not be * considered. Please note that some combination may not make sense * @param {Boolean} flags.secret Only Keys containing a secret part. * @param {Boolean} flags.revoked revoked Keys only * @param {Boolean} flags.expired Expired Keys only * @param {String} (optional) pattern A pattern to search for, in userIds or KeyIds * @returns {Promise Array} * */ getSubset(flags, pattern){ if (flags === undefined) { throw('ERR_WRONG_PARAM'); }; let secretflag = false; if (flags.hasOwnProperty(secret) && flags.secret){ secretflag = true; } this.getKeys(pattern, secretflag).then(function(queryset){ let resultset = []; for (let i=0; i < queryset.length; i++ ){ let conditions = []; let anticonditions = []; if (secretflag === true){ conditions.push('hasSecret'); } else if (secretflag === false){ anticonditions.push('hasSecret'); } /** if (flags.defaultKey === true){ conditions.push('isDefault'); } else if (flags.defaultKey === false){ anticonditions.push('isDefault'); } */ /** * if (flags.valid === true){ anticonditions.push('isInvalid'); } else if (flags.valid === false){ conditions.push('isInvalid'); } */ if (flags.revoked === true){ conditions.push('isRevoked'); } else if (flags.revoked === false){ anticonditions.push('isRevoked'); } if (flags.expired === true){ conditions.push('isExpired'); } else if (flags.expired === false){ anticonditions.push('isExpired'); } let decision = undefined; for (let con = 0; con < conditions.length; con ++){ if (queryset[i][conditions[con]] !== true){ decision = false; } } for (let acon = 0; acon < anticonditions.length; acon ++){ if (queryset[i][anticonditions[acon]] === true){ decision = false; } } if (decision !== false){ resultset.push(queryset[i]); } } return Promise.resolve(resultset); }, function(error){ //TODO error handling }); } }; diff --git a/lang/js/unittest_inputvalues.js b/lang/js/unittest_inputvalues.js new file mode 100644 index 00000000..3450afd2 --- /dev/null +++ b/lang/js/unittest_inputvalues.js @@ -0,0 +1,45 @@ +import {Connection} from "./src/Connection"; +import {createKey} from "./src/Key"; + +let conn = new Connection; + +export const helper_params = { + validLongId: '0A0A0A0A0A0A0A0A', + validKeys: ['A1E3BC45BDC8E87B74F4392D53B151A1368E50F3', + createKey('ADDBC303B6D31026F5EB4591A27EABDF283121BB', conn), + 'EE17AEE730F88F1DE7713C54BBE0A4FF7851650A'], + validFingerprint: '9A9A7A7A8A9A9A7A7A8A9A9A7A7A8A9A9A7A7A8A', + validFingerprints: ['9A9A7A7A8A9A9A7A7A8A9A9A7A7A8A9A9A7A7A8A', + '9AAE7A338A9A9A7A7A8A9A9A7A7A8A9A9A7A7DDA'], + invalidLongId: '9A9A7A7A8A9A9A7A7A8A', + invalidFingerprints: [{hello:'World'}, ['kekekeke'], new Uint32Array(40)], + invalidKeyArray: {curiosity:'uncat'}, + invalidKeyArray_OneBad: [ + createKey('12AE9F3E41B33BF77DF52B6BE8EE1992D7909B08', conn), + 'E1D18E6E994FA9FE9360Bx0E687B940FEFEB095A', + '3AEA7FE4F5F416ED18CEC63DD519450D9C0FAEE5'], + invalidErrorCode: 'Please type in all your passwords.', + validGPGME_Key: createKey('ADDBC303B6D31026F5EB4591A27EABDF283121BB', conn), + valid_openpgplike: { primaryKey: { + getFingerprint: function(){ + return '85DE2A8BA5A5AB3A8A7BE2000B8AED24D7534BC2';} + } + } +} + +export const message_params = { + invalid_op_action : 'dance', + invalid_op_type : [234, 34, '<>'], + valid_encrypt_data: "مرحبا بالعالم", + invalid_param_test: { + valid_op: 'encrypt', + invalid_param_names: [22,'dance', {}], + validparam_name_0: 'mime', + invalid_values_0: [2134, 'All your passwords', + createKey('12AE9F3E41B33BF77DF52B6BE8EE1992D7909B08', conn), null] + } +} + +export const whatever_params = { + four_invalid_params: ['<(((-<', '>°;==;~~', '^^', '{{{{o}}}}'] +} diff --git a/lang/js/unittests.js b/lang/js/unittests.js new file mode 100644 index 00000000..0a1b4b48 --- /dev/null +++ b/lang/js/unittests.js @@ -0,0 +1,321 @@ +/* gpgme.js - Javascript integration for gpgme + * Copyright (C) 2018 Bundesamt für Sicherheit in der Informationstechnik + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1+ + */ +import "./node_modules/mocha/mocha"; +import "./node_modules/chai/chai"; +import { helper_params as hp } from "./unittest_inputvalues"; +import { message_params as mp } from "./unittest_inputvalues"; +import { whatever_params as wp } from "./unittest_inputvalues"; +import { Connection } from "./src/Connection"; +import { gpgme_error } from "./src/Errors"; +import { toKeyIdArray , isFingerprint } from "./src/Helpers"; +import { GPGME_Key , createKey } from "./src/Key"; +import { GPGME_Keyring } from "./src/Keyring"; +import {GPGME_Message, createMessage} from "./src/Message"; +import { setTimeout } from "timers"; + +mocha.setup('bdd'); +var expect = chai.expect; +chai.config.includeStack = true; + +function unittests (){ + describe('Connection testing', function(){ + + it('Connecting', function(done) { + let conn0 = new Connection; + let delayed = function(){ + expect(conn0.isConnected).to.be.true; + expect(conn0.connect).to.be.a('function'); + expect(conn0.disconnect).to.be.a('function'); + expect(conn0.post).to.be.a('function'); + done(); + }; + setTimeout(delayed, 5); + + }); + + it('Disconnecting', function(done) { + let conn0 = new Connection; + let delayed = function(){ + conn0.disconnect(); // TODO fails! + expect(conn0.isConnected).to.be.false; + done(); + }; + setTimeout(delayed, 5); + }); + + // broken + // it('Connect info still only available after a delay', function(done){ + // // if false, all delayed connections can be refactored + // let conn0 = new Connection; + // expect(conn0.isConnected).to.be.undefined; + // // + // }) + }); + + describe('Error Object handling', function(){ + + it('check the Timeout error', function(){ + let test0 = gpgme_error('CONN_TIMEOUT'); + + expect(test0).to.be.an.instanceof(Error); + expect(test0.code).to.equal('CONN_TIMEOUT'); + }); + + it('Error Object returns generic code if code is not listed', function(){ + let test0 = gpgme_error(hp.invalidErrorCode); + + expect(test0).to.be.an.instanceof(Error); + expect(test0.code).to.equal('GENERIC_ERROR'); + }); + + it('Warnings like PARAM_IGNORED should not return errors', function(){ + let test0 = gpgme_error('PARAM_IGNORED'); + + expect(test0).to.be.null; + }); + }); + + describe('Fingerprint checking', function(){ + + it('isFingerprint(): valid Fingerprint', function(){ + let test0 = isFingerprint(hp.validFingerprint); + + expect(test0).to.be.true; + }); + + it('isFingerprint(): invalid Fingerprints', function(){ + for (let i=0; i < hp.invalidFingerprints.length; i++){ + let test0 = isFingerprint(hp.invalidFingerprints[i]); + + expect(test0).to.be.false; + } + }); + }); + + describe('toKeyIdArray() (converting input to fingerprint', function(){ + + it('Correct fingerprint string', function(){ + let test0 = toKeyIdArray(hp.validFingerprint); + + expect(test0).to.be.an('array'); + expect(test0).to.include(hp.validFingerprint); + }); + + it('correct GPGME_Key', function(){ + expect(hp.validGPGME_Key).to.be.an.instanceof(GPGME_Key); + let test0 = toKeyIdArray(hp.validGPGME_Key); + + expect(test0).to.be.an('array'); + expect(test0).to.include(hp.validGPGME_Key.fingerprint); + }); + + it('openpgpjs-like object', function(){ + let test0 = toKeyIdArray(hp.valid_openpgplike); + + expect(test0).to.be.an('array').with.lengthOf(1); + console.log(test0); + expect(test0).to.include( + hp.valid_openpgplike.primaryKey.getFingerprint()); + }); + + it('Array of valid inputs', function(){ + let test0 = toKeyIdArray(hp.validKeys); + expect(test0).to.be.an('array'); + expect(test0).to.have.lengthOf(hp.validKeys.length); + }); + + it('Incorrect inputs', function(){ + + it('valid Long ID', function(){ + let test0 = toKeyIdArray(hp.validLongId); + + expect(test0).to.be.empty; + }); + + it('invalidFingerprint', function(){ + let test0 = toKeyIdArray(hp.invalidFingerprint); + + expect(test0).to.be.empty; + }); + + it('invalidKeyArray', function(){ + let test0 = toKeyIdArray(hp.invalidKeyArray); + + expect(test0).to.be.empty; + }); + + it('Partially invalid array', function(){ + let test0 = toKeyIdArray(hp.invalidKeyArray_OneBad); + + expect(test0).to.be.an('array'); + expect(test0).to.have.lengthOf( + hp.invalidKeyArray_OneBad.length - 1); + }); + }); + }); + + describe('GPGME_Key', function(){ + + it('correct Key initialization', function(){ + let conn = new Connection; + let key = createKey(hp.validFingerprint, conn); + + expect(key).to.be.an.instanceof(GPGME_Key); + expect(key.connection).to.be.an.instanceof(Connection); + // TODO not implemented yet: Further Key functionality + }); + + it('Key can use the connection', function(){ + let conn = new Connection; + let key = createKey(hp.validFingerprint, conn); + + expect(key.connection.isConnected).to.be.true; + + key.connection.disconnect(); + expect(key.connection.isConnected).to.be.false; + }); + + it('createKey returns error if parameters are wrong', function(){ + let conn = new Connection; + for (let i=0; i< 4; i++){ + let key0 = createKey(wp.four_invalid_params[i], conn); + + expect(key0).to.be.an.instanceof(Error); + expect(key0.code).to.equal('PARAM_WRONG'); + } + for (let i=0; i< 4; i++){ + let key0 = createKey( + hp.validFingerprint, wp.four_invalid_params[i]); + + expect(key0).to.be.an.instanceof(Error); + expect(key0.code).to.equal('PARAM_WRONG'); + } + }); + it('bad GPGME_Key returns Error if used', function(){ + let conn = new Connection; + for (let i=0; i < 4; i++){ + let key = new GPGME_Key(wp.four_invalid_params[i], conn); + + expect(key.connection).to.be.an.instanceof(Error); + expect(key.connection.code).to.equal('KEY_INVALID'); + } + }); + }); + + describe('GPGME_Keyring', function(){ + + it('correct initialization', function(){ + let conn = new Connection; + let keyring = new GPGME_Keyring(conn); + + expect(keyring).to.be.an.instanceof(GPGME_Keyring); + expect(keyring.connection).to.be.an.instanceof(Connection); + expect(keyring.getKeys).to.be.a('function'); + expect(keyring.getSubset).to.be.a('function'); + }); + + it('Keyring should return errors if not connected', function(){ + let keyring = new GPGME_Keyring; + + expect(keyring).to.be.an.instanceof(GPGME_Keyring); + expect(keyring.connection).to.be.an.instanceof(Error); + expect(keyring.connection.code).to.equal('CONN_NO_CONNECT'); + expect(keyring.getKeys).to.be.an.instanceof(Error); + expect(keyring.getkeys.code).to.equal('CONN_NO_CONNECT'); + }); + //TODO not yet implemented: + // getKeys(pattern, include_secret) //note: pattern can be null + // getSubset(flags, pattern) + // available Boolean flags: secret revoked expired + }); + + describe('GPGME_Message', function(){ + + it('creating encrypt Message', function(){ + let test0 = createMessage('encrypt'); + + expect(test0).to.be.an.instanceof(GPGME_Message); + expect(test0.isComplete).to.be.false; + }); + + it('Message is complete after setting mandatoy data', function(){ + let test0 = createMessage('encrypt'); + test0.setParameter('data', mp.valid_encrypt_data); + test0.setParameter('keys', hp.validFingerprints); + + expect(test0.isComplete).to.be.true; + }); + + it('Complete Message contains the data that was set', function(){ + let test0 = createMessage('encrypt'); + test0.setParameter('data', mp.valid_encrypt_data); + test0.setParameter('keys', hp.validFingerprints); + + expect(test0.message).to.not.be.null; + expect(test0.message).to.have.keys('op', 'data', 'keys'); + expect(test0.message.op).to.equal('encrypt'); + expect(test0.message.data).to.equal( + mp.valid_encrypt_data); + }); + + it ('Not accepting non-allowed operation', function(){ + let test0 = createMessage(mp.invalid_op_action); + + expect(test0).to.be.an.instanceof(Error); + expect(test0.code).to.equal('MSG_WRONG_OP'); + }); + it('Not accepting wrong parameter type', function(){ + let test0 = createMessage(mp.invalid_op_type); + + expect(test0).to.be.an.instanceof(Error); + expect(test0.code).to.equal('PARAM_WRONG'); + }); + + it('Not accepting wrong parameter name', function(){ + let test0 = createMessage(mp.invalid_param_test.valid_op); + for (let i=0; + i < mp.invalid_param_test.invalid_param_names.length; i++){ + let ret = test0.setParameter( + mp.invalid_param_test.invalid_param_names[i], + 'Somevalue'); + + expect(ret).to.be.an.instanceof(Error); + expect(ret.code).to.equal('PARAM_WRONG'); + } + }); + + it('Not accepting wrong parameter value', function(){ + let test0 = createMessage(mp.invalid_param_test.valid_op); + for (let j=0; + j < mp.invalid_param_test.invalid_values_0.length; j++){ + let ret = test0.setParameter( + mp.invalid_param_test.validparam_name_0, + mp.invalid_param_test.invalid_values_0[j]); + + expect(ret).to.be.an.instanceof(Error); + expect(ret.code).to.equal('PARAM_WRONG'); + } + }); + }); + + mocha.run(); +} + +export default {unittests}; \ No newline at end of file diff --git a/lang/js/BrowserTestExtension/runbrowsertest.js b/lang/js/webpack.conf_unittests.js similarity index 69% copy from lang/js/BrowserTestExtension/runbrowsertest.js copy to lang/js/webpack.conf_unittests.js index 39bc3fb9..4b903be6 100644 --- a/lang/js/BrowserTestExtension/runbrowsertest.js +++ b/lang/js/webpack.conf_unittests.js @@ -1,21 +1,34 @@ /* gpgme.js - Javascript integration for gpgme * Copyright (C) 2018 Bundesamt für Sicherheit in der Informationstechnik * * This file is part of GPGME. * * GPGME is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * GPGME 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program; if not, see . * SPDX-License-Identifier: LGPL-2.1+ + * + * This is the configuration file for building the gpgmejs-Library with webpack */ +const path = require('path'); -mocha.run(); +module.exports = { + entry: './unittests.js', + mode: 'production', + output: { + path: path.resolve(__dirname, 'build'), + filename: 'gpgmejs_unittests.bundle.js', + libraryTarget: 'var', + libraryExport: 'default', + library: 'Gpgmejs_test' + } +};