aboutsummaryrefslogtreecommitdiffstats
path: root/lang/js/src
diff options
context:
space:
mode:
authorMaximilian Krambach <[email protected]>2018-04-25 17:45:39 +0000
committerMaximilian Krambach <[email protected]>2018-04-25 17:45:39 +0000
commit3685913bf510a14b8cb324d980217d90489e6453 (patch)
treeeb1acbdee90bf747f2dfbd4c9a61ed83d41c2b8f /lang/js/src
parentjs: Configuration and Error handling (diff)
downloadgpgme-3685913bf510a14b8cb324d980217d90489e6453.tar.gz
gpgme-3685913bf510a14b8cb324d980217d90489e6453.zip
js: First testing and improvements
-- * Introduced Mocha/chai as testsuite. After development build 'npm test' should run the unit tests. Functionality exclusive to Browsers/WebExtensions cannot be run this way, so some other testing is still needed. - package.json: Added required development packages - .babelrc indirect configuration for mocha. ES6 transpiling needs some babel configuration, but mocha has no setting for it. - test/mocha.opts Vonfiguration for mocha runs * Fixed errors: - Helpers.js toKeyIdArray; isLongId is now exported - Key.js Key constructor failed - Message.js will not throw an Error during construction, a new message is now created with createMessage, which can return an Error or a GPGME_Message object * Tests: - test/Helpers: exports from Helpers.js, GPGME_Error handling - test/Message: first init test with bad parameters
Diffstat (limited to '')
-rw-r--r--lang/js/src/Connection.js2
-rw-r--r--lang/js/src/Errors.js11
-rw-r--r--lang/js/src/Helpers.js7
-rw-r--r--lang/js/src/Key.js15
-rw-r--r--lang/js/src/Keyring.js7
-rw-r--r--lang/js/src/Message.js47
-rw-r--r--lang/js/src/gpgmejs.js31
7 files changed, 66 insertions, 54 deletions
diff --git a/lang/js/src/Connection.js b/lang/js/src/Connection.js
index 5b092ab0..a10f9d9a 100644
--- a/lang/js/src/Connection.js
+++ b/lang/js/src/Connection.js
@@ -83,7 +83,7 @@ export class Connection{
*/
post(message){
if (!this.isConnected){
- return Promise.reject(gpgme_error('CONN_NO_CONNECT'));
+ return Promise.reject(gpgme_error('CONN_DISCONNECTED'));
}
if (!message || !message instanceof GPGME_Message){
return Promise.reject(gpgme_error('PARAM_WRONG'), message);
diff --git a/lang/js/src/Errors.js b/lang/js/src/Errors.js
index 2f53aa89..d26aab18 100644
--- a/lang/js/src/Errors.js
+++ b/lang/js/src/Errors.js
@@ -55,14 +55,9 @@ const err_list = {
msg: 'The Message is empty.',
type: 'error'
},
- 'MSG_OP_PENDING': {
- msg: 'There is no operation specified yet. The parameter cannot'
- + ' be set',
- type: 'warning'
- },
'MSG_WRONG_OP': {
msg: 'The operation requested could not be found',
- type: 'warning'
+ type: 'error'
},
'MSG_NO_KEYS' : {
msg: 'There were no valid keys provided.',
@@ -78,7 +73,7 @@ const err_list = {
},
// generic
'PARAM_WRONG':{
- msg: 'invalid parameter was found',
+ msg: 'Invalid parameter was found',
type: 'error'
},
'PARAM_IGNORED': {
@@ -111,7 +106,7 @@ export function gpgme_error(code = 'GENERIC_ERROR', info){
return new GPGME_Error(code);
}
if (err_list[code].type === 'warning'){
- console.log(new GPGME_Error(code));
+ console.warn(code + ': ' + err_list[code].msg);
}
return null;
} else if (code === 'GNUPG_ERROR'){
diff --git a/lang/js/src/Helpers.js b/lang/js/src/Helpers.js
index 841c0eda..9a69f851 100644
--- a/lang/js/src/Helpers.js
+++ b/lang/js/src/Helpers.js
@@ -18,6 +18,7 @@
* 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
@@ -26,7 +27,7 @@ import { gpgme_error } from "./Errors";
* @returns {Array<String>} Array of fingerprints.
*/
-export function toKeyIdArray(input, nocheck){
+export function toKeyIdArray(input){
if (!input){
gpgme_error('MSG_NO_KEYS');
return [];
@@ -46,7 +47,7 @@ export function toKeyIdArray(input, nocheck){
let fpr = '';
if (input[i] instanceof GPGME_Key){
fpr = input[i].fingerprint;
- } else if (input[i].hasOwnProperty(primaryKey) &&
+ } else if (input[i].hasOwnProperty('primaryKey') &&
input[i].primaryKey.hasOwnProperty(getFingerprint)){
fpr = input[i].primaryKey.getFingerprint();
}
@@ -92,7 +93,7 @@ export function isFingerprint(string){
/**
* check if the input is a valid Hex string with a length of 16
*/
-function isLongId(string){
+export function isLongId(string){
return hextest(string, 16);
};
diff --git a/lang/js/src/Key.js b/lang/js/src/Key.js
index f6fa7ae3..0b44b245 100644
--- a/lang/js/src/Key.js
+++ b/lang/js/src/Key.js
@@ -26,9 +26,9 @@
*
*/
-import {isFingerprint} from './Helpers'
-import {gpgme_error} from './Errors'
-import { GPGME_Message } from './Message';
+import { isFingerprint } from './Helpers'
+import { gpgme_error } from './Errors'
+import { createMessage } from './Message';
import { permittedOperations } from './permittedOperations';
export class GPGME_Key {
@@ -39,7 +39,7 @@ export class GPGME_Key {
set fingerprint(fpr){
if (isFingerprint(fpr) === true && !this._fingerprint){
- this._fingerprint = fingerprint;
+ this._fingerprint = fpr;
}
}
@@ -181,9 +181,12 @@ function checkKey(fingerprint, property){
}
return new Promise(function(resolve, reject){
if (!isFingerprint(fingerprint)){
- reject('KEY_INVALID');
+ reject(gpgme_error('KEY_INVALID'));
+ }
+ let msg = createMessage ('keyinfo');
+ if (msg instanceof Error){
+ reject(gpgme_error('PARAM_WRONG'));
}
- let msg = new GPGME_Message('keyinfo');
msg.setParameter('fingerprint', this.fingerprint);
return (this.connection.post(msg)).then(function(result){
if (result.hasOwnProperty(property)){
diff --git a/lang/js/src/Keyring.js b/lang/js/src/Keyring.js
index e1f0a50f..470eeeec 100644
--- a/lang/js/src/Keyring.js
+++ b/lang/js/src/Keyring.js
@@ -18,7 +18,7 @@
* SPDX-License-Identifier: LGPL-2.1+
*/
-import {GPGME_Message} from './Message'
+import {createMessage} from './Message'
import {GPGME_Key} from './Key'
import { isFingerprint, isLongId } from './Helpers';
import { gpgme_error } from './Errors';
@@ -50,7 +50,10 @@ export class GPGME_Keyring {
*
*/
getKeys(pattern, include_secret){
- let msg = new GPGME_Message('listkeys');
+ let msg = createMessage('listkeys');
+ if (msg instanceof Error){
+ return Promise.reject(msg);
+ }
if (pattern && typeof(pattern) === 'string'){
msg.setParameter('pattern', pattern);
}
diff --git a/lang/js/src/Message.js b/lang/js/src/Message.js
index 06ac8db2..4d242277 100644
--- a/lang/js/src/Message.js
+++ b/lang/js/src/Message.js
@@ -19,13 +19,34 @@
*/
import { permittedOperations } from './permittedOperations'
import { gpgme_error } from './Errors'
-export class GPGME_Message {
+
+export function createMessage(operation){
+ if (typeof(operation) !== 'string'){
+ return gpgme_error('PARAM_WRONG');
+ }
+ if (permittedOperations.hasOwnProperty(operation)){
+ return new GPGME_Message(operation);
+ } else {
+ return gpgme_error('MSG_WRONG_OP');
+ }
+}
+
+/**
+ * Prepares a communication request. It checks operations and parameters in
+ * ./permittedOperations.
+ * @param {String} operation
+ */
+class GPGME_Message {
//TODO getter
constructor(operation){
- setOperation(this, operation);
+ this.operation = operation;
}
+ set operation (op){
+
+
+ }
get operation(){
return this._msg.op;
}
@@ -41,9 +62,6 @@ export class GPGME_Message {
if (!param || typeof(param) !== 'string'){
return gpgme_error('PARAM_WRONG');
}
- if (!this._msg || !this._msg.op){
- return gpgme_error('MSG_OP_PENDING');
- }
let po = permittedOperations[this._msg.op];
if (!po){
return gpgme_error('MSG_WRONG_OP');
@@ -90,22 +108,3 @@ export class GPGME_Message {
}
}
-
-/**
- * Defines the operation this message will have
- * @param {String} operation Must be defined in permittedOperations
- * TODO: move to constructor?
- */
-function setOperation (scope, operation){
- if (!operation || typeof(operation) !== 'string'){
- return gpgme_error('PARAM_WRONG');
- }
- if (permittedOperations.hasOwnProperty(operation)){
- if (!scope._msg){
- scope._msg = {};
- }
- scope._msg.op = operation;
- } else {
- return gpgme_error('MSG_WRONG_OP');
- }
-} \ No newline at end of file
diff --git a/lang/js/src/gpgmejs.js b/lang/js/src/gpgmejs.js
index b504a457..2ddf2964 100644
--- a/lang/js/src/gpgmejs.js
+++ b/lang/js/src/gpgmejs.js
@@ -19,7 +19,7 @@
*/
import {Connection} from "./Connection"
-import {GPGME_Message} from './Message'
+import {GPGME_Message, createMessage} from './Message'
import {toKeyIdArray} from "./Helpers"
import { gpgme_error } from "./Errors"
import { GPGME_Keyring } from "./Keyring";
@@ -71,8 +71,10 @@ export class GpgME {
*/
encrypt(data, publicKeys, wildcard=false){
- let msg = new GPGME_Message('encrypt');
-
+ let msg = createMessage('encrypt');
+ if (msg instanceof Error){
+ return Promise.reject(msg)
+ }
// TODO temporary
msg.setParameter('armor', true);
msg.setParameter('always-trust', true);
@@ -101,7 +103,10 @@ export class GpgME {
if (data === undefined){
return Promise.reject(gpgme_error('MSG_EMPTY'));
}
- let msg = new GPGME_Message('decrypt');
+ let msg = createMessage('decrypt');
+ if (msg instanceof Error){
+ return Promise.reject(msg);
+ }
putData(msg, data);
return this.connection.post(msg);
@@ -109,21 +114,27 @@ export class GpgME {
deleteKey(key, delete_secret = false, no_confirm = false){
return Promise.reject(gpgme_error('NOT_YET_IMPLEMENTED'));
- let msg = new GPGME_Message('deletekey');
+ let msg = createMessage('deletekey');
+ if (msg instanceof Error){
+ return Promise.reject(msg);
+ }
let key_arr = toKeyIdArray(key);
if (key_arr.length !== 1){
- throw('TODO');
- //should always be ONE key
+ return Promise.reject(
+ gpgme_error('GENERIC_ERROR'));
+ // TBD should always be ONE key?
}
msg.setParameter('key', key_arr[0]);
if (delete_secret === true){
- msg.setParameter('allow_secret', true); //TBD
+ msg.setParameter('allow_secret', true);
+ // TBD
}
if (no_confirm === true){ //TODO: Do we want this hidden deep in the code?
- msg.setParameter('delete_force', true); //TBD
+ msg.setParameter('delete_force', true);
+ // TBD
}
this.connection.post(msg).then(function(success){
- //TODO: it seems that there is always errors coming back:
+ // TODO: it seems that there is always errors coming back:
}, function(error){
switch (error.msg){
case 'ERR_NO_ERROR':