443aa685fe7f228bab577dce0038726abd7f2c5e
[gpgme.git] / lang / js / unittests.js
1 /* gpgme.js - Javascript integration for gpgme
2  * Copyright (C) 2018 Bundesamt für Sicherheit in der Informationstechnik
3  *
4  * This file is part of GPGME.
5  *
6  * GPGME is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as
8  * published by the Free Software Foundation; either version 2.1 of
9  * the License, or (at your option) any later version.
10  *
11  * GPGME is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this program; if not, see <http://www.gnu.org/licenses/>.
18  * SPDX-License-Identifier: LGPL-2.1+
19  */
20 import "./node_modules/mocha/mocha";
21 import "./node_modules/chai/chai";
22 import { helper_params as hp } from "./unittest_inputvalues";
23 import { message_params as mp } from "./unittest_inputvalues";
24 import { whatever_params as wp } from "./unittest_inputvalues";
25 import { key_params as kp } from "./unittest_inputvalues";
26 import { Connection } from "./src/Connection";
27 import { gpgme_error } from "./src/Errors";
28 import { toKeyIdArray , isFingerprint } from "./src/Helpers";
29 import { GPGME_Key , createKey } from "./src/Key";
30 import { GPGME_Keyring } from "./src/Keyring";
31 import {GPGME_Message, createMessage} from "./src/Message";
32 import { setTimeout } from "timers";
33
34 mocha.setup('bdd');
35 var expect = chai.expect;
36 chai.config.includeStack = true;
37
38 function unittests (){
39     describe('Connection testing', function(){
40
41         it('Connecting', function(done) {
42             let conn0 = new Connection;
43             conn0.checkConnection().then(function(answer) {
44                 expect(answer).to.not.be.empty;
45                 expect(answer.gpgme).to.not.be.undefined;
46                 expect(answer.gpgme).to.be.a('string');
47                 expect(answer.info).to.be.an('Array');
48                 expect(conn0.disconnect).to.be.a('function');
49                 expect(conn0.post).to.be.a('function');
50                 done();
51             });
52
53         });
54
55         it('Disconnecting', function(done) {
56             let conn0 = new Connection;
57             conn0.checkConnection(false).then(function(answer) {
58                 expect(answer).to.be.true;
59                 conn0.disconnect();
60                 conn0.checkConnection(false).then(function(result) {
61                     expect(result).to.be.false;
62                     done();
63                 });
64             });
65         });
66     });
67
68     describe('Error Object handling', function(){
69         // TODO: new GPGME_Error codes
70         it('check the Timeout error', function(){
71             let test0 = gpgme_error('CONN_TIMEOUT');
72
73             expect(test0).to.be.an.instanceof(Error);
74             expect(test0.code).to.equal('CONN_TIMEOUT');
75         });
76
77         it('Error Object returns generic code if code is not listed', function(){
78             let test0 = gpgme_error(hp.invalidErrorCode);
79
80             expect(test0).to.be.an.instanceof(Error);
81             expect(test0.code).to.equal('GENERIC_ERROR');
82         });
83
84         it('Warnings like PARAM_IGNORED should not return errors', function(){
85             let test0 = gpgme_error('PARAM_IGNORED');
86
87             expect(test0).to.be.null;
88         });
89     });
90
91     describe('Fingerprint checking', function(){
92
93         it('isFingerprint(): valid Fingerprint', function(){
94             let test0  = isFingerprint(hp.validFingerprint);
95
96             expect(test0).to.be.true;
97         });
98
99         it('isFingerprint(): invalid Fingerprints', function(){
100             for (let i=0; i < hp.invalidFingerprints.length; i++){
101                 let test0 = isFingerprint(hp.invalidFingerprints[i]);
102
103                 expect(test0).to.be.false;
104             }
105         });
106     });
107
108     describe('toKeyIdArray() (converting input to fingerprint)', function(){
109
110         it('Correct fingerprint string', function(){
111             let test0 = toKeyIdArray(hp.validFingerprint);
112
113             expect(test0).to.be.an('array');
114             expect(test0).to.include(hp.validFingerprint);
115         });
116
117         it('correct GPGME_Key', function(){
118             expect(hp.validGPGME_Key).to.be.an.instanceof(GPGME_Key);
119             let test0 = toKeyIdArray(hp.validGPGME_Key);
120
121             expect(test0).to.be.an('array');
122             expect(test0).to.include(hp.validGPGME_Key.fingerprint);
123         });
124
125         it('openpgpjs-like object', function(){
126             let test0 = toKeyIdArray(hp.valid_openpgplike);
127
128             expect(test0).to.be.an('array').with.lengthOf(1);
129             expect(test0).to.include(
130                 hp.valid_openpgplike.primaryKey.getFingerprint());
131         });
132
133         it('Array of valid inputs', function(){
134             let test0 = toKeyIdArray(hp.validKeys);
135             expect(test0).to.be.an('array');
136             expect(test0).to.have.lengthOf(hp.validKeys.length);
137         });
138
139         it('Incorrect inputs', function(){
140
141             it('valid Long ID', function(){
142                 let test0 = toKeyIdArray(hp.validLongId);
143
144                 expect(test0).to.be.empty;
145             });
146
147             it('invalidFingerprint', function(){
148                 let test0 = toKeyIdArray(hp.invalidFingerprint);
149
150                 expect(test0).to.be.empty;
151             });
152
153             it('invalidKeyArray', function(){
154                 let test0 = toKeyIdArray(hp.invalidKeyArray);
155
156                 expect(test0).to.be.empty;
157             });
158
159             it('Partially invalid array', function(){
160                 let test0 = toKeyIdArray(hp.invalidKeyArray_OneBad);
161
162                 expect(test0).to.be.an('array');
163                 expect(test0).to.have.lengthOf(
164                     hp.invalidKeyArray_OneBad.length - 1);
165             });
166         });
167     });
168
169     describe('GPGME_Key', function(){
170
171         it('correct Key initialization', function(){
172             let key = createKey(kp.validKeyFingerprint);
173             expect(key).to.be.an.instanceof(GPGME_Key);
174         });
175         it('Key has data after a first refresh', function(done) {
176             let key = createKey(kp.validKeyFingerprint);
177             key.refreshKey().then(function(key2){
178                 expect(key2).to.be.an.instanceof(GPGME_Key);
179                 expect(key2.get).to.be.a('function');
180                 for (let i=0; i < kp.validKeyProperties.length; i++) {
181                     let prop = key2.get(kp.validKeyProperties[i]);
182                     expect(prop).to.not.be.undefined;
183                     expect(prop).to.be.a('boolean');
184                 }
185                 expect(isFingerprint(key2.get('fingerprint'))).to.be.true;
186                 expect(
187                     key2.get('fingerprint')).to.equal(kp.validKeyFingerprint);
188                 expect(
189                     key2.get('fingerprint')).to.equal(key.fingerprint);
190                 done();
191             });
192         });
193
194         it('Non-cached key async data retrieval', function (done){
195             let key = createKey(kp.validKeyFingerprint);
196             key.get('can_authenticate',false).then(function(result){
197                 expect(result).to.be.a('boolean');
198                 done();
199             });
200         });
201
202         it('Non-cached key async armored Key', function (done){
203             let key = createKey(kp.validKeyFingerprint);
204             key.get('armor', false).then(function(result){
205                 expect(result).to.be.a('string');
206                 expect(result).to.include('KEY BLOCK-----');
207                 done();
208             });
209         });
210
211         it('Non-cached key async hasSecret', function (done){
212             let key = createKey(kp.validKeyFingerprint);
213             key.get('hasSecret', false).then(function(result){
214                 expect(result).to.be.a('boolean');
215                 done();
216             });
217         });
218
219         it('Non-cached key async hasSecret (no secret in Key)', function (done){
220             let key = createKey(kp.validFingerprintNoSecret);
221             expect(key).to.be.an.instanceof(GPGME_Key);
222             key.get('hasSecret', false).then(function(result){
223                 expect(result).to.be.a('boolean');
224                 expect(result).to.equal(false);
225                 done();
226             });
227         });
228
229         it('Querying non-existing Key returns an error', function(done) {
230             let key = createKey(kp.invalidKeyFingerprint);
231             key.refreshKey().then(function(){},
232                 function(error){
233                     expect(error).to.be.an.instanceof(Error);
234                     expect(error.code).to.equal('KEY_NOKEY');
235                     done();
236             });
237         });
238
239         it('createKey returns error if parameters are wrong', function(){
240             for (let i=0; i< 4; i++){
241                 let key0 = createKey(wp.four_invalid_params[i]);
242                 expect(key0).to.be.an.instanceof(Error);
243                 expect(key0.code).to.equal('PARAM_WRONG');
244             }
245         });
246
247         it('malformed GPGME_Key cannot be used', function(){
248             for (let i=0; i < 4; i++){
249                 let key = new GPGME_Key(wp.four_invalid_params[i]);
250                 expect(key.fingerprint).to.be.an.instanceof(Error);
251                 expect(key.fingerprint.code).to.equal('KEY_INVALID');
252             }
253         });
254         // TODO: tests for subkeys
255         // TODO: tests for userids
256         // TODO: some invalid tests for key/keyring
257     });
258
259     describe('GPGME_Keyring', function(){
260
261         it('correct Keyring initialization', function(){
262             let keyring = new GPGME_Keyring;
263             expect(keyring).to.be.an.instanceof(GPGME_Keyring);
264             expect(keyring.getKeys).to.be.a('function');
265         });
266
267         it('Loading Keys from Keyring, to be used synchronously', function(done){
268             let keyring = new GPGME_Keyring;
269             keyring.getKeys(null, true).then(function(result){
270                 expect(result).to.be.an('array');
271                 expect(result[0]).to.be.an.instanceof(GPGME_Key);
272                 expect(result[0].get('armor')).to.be.a('string');
273                 expect(result[0].get('armor')).to.include(
274                     '-----END PGP PUBLIC KEY BLOCK-----');
275                 done();
276             });
277         });
278     });
279
280     describe('GPGME_Message', function(){
281
282         it('creating encrypt Message', function(){
283             let test0 = createMessage('encrypt');
284
285             expect(test0).to.be.an.instanceof(GPGME_Message);
286             expect(test0.isComplete).to.be.false;
287         });
288
289         it('Message is complete after setting mandatory data', function(){
290             let test0 = createMessage('encrypt');
291             test0.setParameter('data', mp.valid_encrypt_data);
292             test0.setParameter('keys', hp.validFingerprints);
293
294             expect(test0.isComplete).to.be.true;
295         });
296
297         it('Message is not complete after mandatory data is empty', function(){
298             let test0 = createMessage('encrypt');
299             test0.setParameter('data', '');
300             test0.setParameter('keys', hp.validFingerprints);
301             expect(test0.isComplete).to.be.false;
302         });
303
304         it('Complete Message contains the data that was set', function(){
305             let test0 = createMessage('encrypt');
306             test0.setParameter('data', mp.valid_encrypt_data);
307             test0.setParameter('keys', hp.validFingerprints);
308
309             expect(test0.message).to.not.be.null;
310             expect(test0.message).to.have.keys('op', 'data', 'keys');
311             expect(test0.message.op).to.equal('encrypt');
312             expect(test0.message.data).to.equal(
313                 mp.valid_encrypt_data);
314         });
315
316         it ('Not accepting non-allowed operation', function(){
317             let test0 = createMessage(mp.invalid_op_action);
318
319             expect(test0).to.be.an.instanceof(Error);
320             expect(test0.code).to.equal('MSG_WRONG_OP');
321         });
322         it('Not accepting wrong parameter type', function(){
323             let test0 = createMessage(mp.invalid_op_type);
324
325             expect(test0).to.be.an.instanceof(Error);
326             expect(test0.code).to.equal('PARAM_WRONG');
327         });
328
329         it('Not accepting wrong parameter name', function(){
330             let test0 = createMessage(mp.invalid_param_test.valid_op);
331             for (let i=0;
332                 i < mp.invalid_param_test.invalid_param_names.length; i++){
333                     let ret = test0.setParameter(
334                         mp.invalid_param_test.invalid_param_names[i],
335                         'Somevalue');
336
337                     expect(ret).to.be.an.instanceof(Error);
338                     expect(ret.code).to.equal('PARAM_WRONG');
339             }
340         });
341
342         it('Not accepting wrong parameter value', function(){
343             let test0 = createMessage(mp.invalid_param_test.valid_op);
344             for (let j=0;
345                 j < mp.invalid_param_test.invalid_values_0.length; j++){
346                     let ret = test0.setParameter(
347                         mp.invalid_param_test.validparam_name_0,
348                         mp.invalid_param_test.invalid_values_0[j]);
349
350                     expect(ret).to.be.an.instanceof(Error);
351                     expect(ret.code).to.equal('PARAM_WRONG');
352             }
353         });
354     });
355
356 }
357
358 export default {unittests};