js: code cleanup (eslint)
[gpgme.git] / lang / js / BrowserTestExtension / tests / encryptDecryptTest.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  * Author(s):
21  *     Maximilian Krambach <mkrambach@intevation.de>
22  */
23
24 /* global describe, it, expect, Gpgmejs */
25 /* global inputvalues, encryptedData, bigString, bigBoringString */
26
27 describe('Encryption and Decryption', function () {
28     it('Successful encrypt and decrypt simple string', function (done) {
29         let prm = Gpgmejs.init();
30         prm.then(function (context) {
31             context.encrypt(
32                 inputvalues.encrypt.good.data,
33                 inputvalues.encrypt.good.fingerprint).then(
34                 function (answer) {
35                     expect(answer).to.not.be.empty;
36                     expect(answer.data).to.be.a('string');
37                     expect(answer.data).to.include('BEGIN PGP MESSAGE');
38                     expect(answer.data).to.include('END PGP MESSAGE');
39                     context.decrypt(answer.data).then(function (result) {
40                         expect(result).to.not.be.empty;
41                         expect(result.data).to.be.a('string');
42                         expect(result.data).to.equal(
43                             inputvalues.encrypt.good.data);
44                         done();
45                     });
46                 });
47         });
48     });
49
50     it('Decrypt simple non-ascii', function (done) {
51         let prm = Gpgmejs.init();
52         prm.then(function (context) {
53             let data = encryptedData;
54             context.decrypt(data).then(
55                 function (result) {
56                     expect(result).to.not.be.empty;
57                     expect(result.data).to.be.a('string');
58                     expect(result.data).to.equal(
59                         '¡Äußerste µ€ før ñoquis@hóme! Добрый день\n');
60                     done();
61                 });
62         });
63     }).timeout(3000);
64
65     it('Roundtrip does not destroy trailing whitespace',
66         function (done) {
67             let prm = Gpgmejs.init();
68             prm.then(function (context) {
69                 let data = 'Keks. \rKeks \n Keks \r\n';
70                 context.encrypt(data,
71                     inputvalues.encrypt.good.fingerprint).then(
72                     function (answer) {
73                         expect(answer).to.not.be.empty;
74                         expect(answer.data).to.be.a('string');
75                         expect(answer.data).to.include(
76                             'BEGIN PGP MESSAGE');
77                         expect(answer.data).to.include(
78                             'END PGP MESSAGE');
79                         context.decrypt(answer.data).then(
80                             function (result) {
81                                 expect(result).to.not.be.empty;
82                                 expect(result.data).to.be.a('string');
83                                 expect(result.data).to.equal(data);
84                                 done();
85
86                             });
87                     });
88             });
89         }).timeout(5000);
90
91     for (let j = 0; j < inputvalues.encrypt.good.data_nonascii_32.length; j++){
92         it('Roundtrip with >1MB non-ascii input meeting default chunksize (' +
93             (j + 1) + '/'
94             + inputvalues.encrypt.good.data_nonascii_32.length + ')',
95         function (done) {
96             let input = inputvalues.encrypt.good.data_nonascii_32[j];
97             expect(input).to.have.length(32);
98             let prm = Gpgmejs.init();
99             prm.then(function (context) {
100                 let data = '';
101                 for (let i=0; i < 34 * 1024; i++){
102                     data += input;
103                 }
104                 context.encrypt(data,
105                     inputvalues.encrypt.good.fingerprint).then(
106                     function (answer) {
107                         expect(answer).to.not.be.empty;
108                         expect(answer.data).to.be.a('string');
109                         expect(answer.data).to.include(
110                             'BEGIN PGP MESSAGE');
111                         expect(answer.data).to.include(
112                             'END PGP MESSAGE');
113                         context.decrypt(answer.data).then(
114                             function (result) {
115                                 expect(result).to.not.be.empty;
116                                 expect(result.data).to.be.a('string');
117                                 expect(result.data).to.equal(data);
118                                 done();
119                             });
120                     });
121             });
122         }).timeout(3000);
123     }
124
125     it('Random data, as string', function (done) {
126         let data = bigString(1000);
127         let prm = Gpgmejs.init();
128         prm.then(function (context) {
129             context.encrypt(data,
130                 inputvalues.encrypt.good.fingerprint).then(
131                 function (answer) {
132                     expect(answer).to.not.be.empty;
133                     expect(answer.data).to.be.a('string');
134                     expect(answer.data).to.include(
135                         'BEGIN PGP MESSAGE');
136                     expect(answer.data).to.include(
137                         'END PGP MESSAGE');
138                     context.decrypt(answer.data).then(
139                         function (result) {
140                             expect(result).to.not.be.empty;
141                             expect(result.data).to.be.a('string');
142                             expect(result.data).to.equal(data);
143                             done();
144                         });
145                 });
146         });
147     }).timeout(3000);
148
149     it('Data, input as base64', function (done) {
150         let data = inputvalues.encrypt.good.data;
151         let b64data = btoa(data);
152         let prm = Gpgmejs.init();
153         prm.then(function (context) {
154             context.encrypt(b64data,
155                 inputvalues.encrypt.good.fingerprint).then(
156                 function (answer) {
157                     expect(answer).to.not.be.empty;
158                     expect(answer.data).to.be.a('string');
159                     expect(answer.data).to.include(
160                         'BEGIN PGP MESSAGE');
161                     expect(answer.data).to.include(
162                         'END PGP MESSAGE');
163                     context.decrypt(answer.data).then(
164                         function (result) {
165                             expect(result).to.not.be.empty;
166                             expect(result.data).to.be.a('string');
167                             expect(data).to.equal(data);
168                             done();
169                         });
170                 });
171         });
172     }).timeout(3000);
173
174     it('Random data, input as base64', function (done) {
175         let data = bigBoringString(0.001);
176         let b64data = btoa(data);
177         let prm = Gpgmejs.init();
178         prm.then(function (context) {
179             context.encrypt(b64data,
180                 inputvalues.encrypt.good.fingerprint, true).then(
181                 function (answer) {
182                     expect(answer).to.not.be.empty;
183                     expect(answer.data).to.be.a('string');
184                     expect(answer.data).to.include(
185                         'BEGIN PGP MESSAGE');
186                     expect(answer.data).to.include(
187                         'END PGP MESSAGE');
188                     context.decrypt(answer.data, true).then(
189                         function (result) {
190                             expect(result).to.not.be.empty;
191                             expect(result.data).to.be.a('string');
192                             expect(result.data).to.equal(b64data);
193                             done();
194                         });
195                 });
196         });
197     }).timeout(3000);
198
199     it('Random data, input and output as base64', function (done) {
200         let data = bigBoringString(0.0001);
201         let b64data = btoa(data);
202         let prm = Gpgmejs.init();
203         prm.then(function (context) {
204             context.encrypt(b64data,
205                 inputvalues.encrypt.good.fingerprint).then(
206                 function (answer) {
207                     expect(answer).to.not.be.empty;
208                     expect(answer.data).to.be.a('string');
209
210                     expect(answer.data).to.include(
211                         'BEGIN PGP MESSAGE');
212                     expect(answer.data).to.include(
213                         'END PGP MESSAGE');
214                     context.decrypt(answer.data, true).then(
215                         function (result) {
216                             expect(result).to.not.be.empty;
217                             expect(result.data).to.be.a('string');
218                             expect(result.data).to.equal(b64data);
219                             done();
220                         });
221                 });
222         });
223     }).timeout(3000);
224
225
226 });