e5c2f749990775f5cbbc6ace180e5805dc602750
[gpgme.git] / lang / js / BrowserTestExtension / tests / encryptDecryptTest.js
1
2 /* gpgme.js - Javascript integration for gpgme
3  * Copyright (C) 2018 Bundesamt für Sicherheit in der Informationstechnik
4  *
5  * This file is part of GPGME.
6  *
7  * GPGME is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * GPGME is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this program; if not, see <http://www.gnu.org/licenses/>.
19  * SPDX-License-Identifier: LGPL-2.1+
20  */
21
22 describe('Encryption and Decryption', function () {
23     it('Successful encrypt and decrypt simple string', function (done) {
24         let prm = Gpgmejs.init();
25         prm.then(function (context) {
26             context.encrypt(
27                 inputvalues.encrypt.good.data,
28                 inputvalues.encrypt.good.fingerprint).then(function (answer) {
29                     expect(answer).to.not.be.empty;
30                     expect(answer.data).to.be.a("string");
31                     expect(answer.data).to.include('BEGIN PGP MESSAGE');
32                     expect(answer.data).to.include('END PGP MESSAGE');
33                     context.decrypt(answer.data).then(function (result) {
34                         expect(result).to.not.be.empty;
35                         expect(result.data).to.be.a('string');
36                         expect(result.data).to.equal(inputvalues.encrypt.good.data);
37                         done();
38                     });
39                 });
40         });
41     });
42
43     it('Decrypt simple non-ascii', function (done) {
44         let prm = Gpgmejs.init();
45         prm.then(function (context) {
46             let data = encryptedData;
47             context.decrypt(data).then(
48                 function (result) {
49                     expect(result).to.not.be.empty;
50                     expect(result.data).to.be.a('string');
51                     expect(result.data).to.equal(
52                         '¡Äußerste µ€ før ñoquis@hóme! Добрый день\n');
53                     done();
54             });
55         });
56     }).timeout(3000);
57
58     it('Roundtrip does not destroy trailing whitespace',
59         function (done) {
60             let prm = Gpgmejs.init();
61             prm.then(function (context) {
62                 let data = 'Keks. \rKeks \n Keks \r\n';
63                 context.encrypt(data,
64                     inputvalues.encrypt.good.fingerprint).then(
65                     function (answer) {
66                         expect(answer).to.not.be.empty;
67                         expect(answer.data).to.be.a("string");
68                         expect(answer.data).to.include(
69                             'BEGIN PGP MESSAGE');
70                         expect(answer.data).to.include(
71                             'END PGP MESSAGE');
72                         context.decrypt(answer.data).then(
73                             function (result) {
74                                 expect(result).to.not.be.empty;
75                                 expect(result.data).to.be.a('string');
76                                 expect(result.data).to.equal(data);
77                                 done();
78
79                             });
80                     });
81             });
82     }).timeout(5000);
83
84     for (let j = 0; j < inputvalues.encrypt.good.data_nonascii_32.length; j++){
85         it('Roundtrip with >1MB non-ascii input meeting default chunksize (' + (j + 1) + '/' + inputvalues.encrypt.good.data_nonascii_32.length + ')',
86             function (done) {
87                 let input = inputvalues.encrypt.good.data_nonascii_32[j];
88                 expect(input).to.have.length(32);
89                 let prm = Gpgmejs.init();
90                 prm.then(function (context) {
91                     let data = '';
92                     for (let i=0; i < 34 * 1024; i++){
93                         data += input;
94                     }
95                     context.encrypt(data,
96                         inputvalues.encrypt.good.fingerprint).then(
97                         function (answer) {
98                             expect(answer).to.not.be.empty;
99                             expect(answer.data).to.be.a("string");
100                             expect(answer.data).to.include(
101                                 'BEGIN PGP MESSAGE');
102                             expect(answer.data).to.include(
103                                 'END PGP MESSAGE');
104                             context.decrypt(answer.data).then(
105                                 function (result) {
106                                     expect(result).to.not.be.empty;
107                                     expect(result.data).to.be.a('string');
108                                     expect(result.data).to.equal(data);
109                                     done();
110                                 });
111                         });
112                 });
113         }).timeout(3000);
114     };
115
116     it('Random data, as string', function (done) {
117         let data = bigString(1000);
118         let prm = Gpgmejs.init();
119         prm.then(function (context) {
120             context.encrypt(data,
121                 inputvalues.encrypt.good.fingerprint).then(
122                 function (answer) {
123                     expect(answer).to.not.be.empty;
124                     expect(answer.data).to.be.a("string");
125                     expect(answer.data).to.include(
126                         'BEGIN PGP MESSAGE');
127                     expect(answer.data).to.include(
128                         'END PGP MESSAGE');
129                     context.decrypt(answer.data).then(
130                         function (result) {
131                             expect(result).to.not.be.empty;
132                             expect(result.data).to.be.a('string');
133                             expect(result.data).to.equal(data);
134                             done();
135                         });
136                 });
137         });
138     }).timeout(3000);
139
140     it('Data, input as base64', function (done) {
141         let data = inputvalues.encrypt.good.data;
142         let b64data = btoa(data);
143         let prm = Gpgmejs.init();
144         prm.then(function (context) {
145             context.encrypt(b64data,
146                 inputvalues.encrypt.good.fingerprint,).then(
147                 function (answer) {
148                     expect(answer).to.not.be.empty;
149                     expect(answer.data).to.be.a("string");
150                     expect(answer.data).to.include(
151                         'BEGIN PGP MESSAGE');
152                     expect(answer.data).to.include(
153                         'END PGP MESSAGE');
154                     context.decrypt(answer.data).then(
155                         function (result) {
156                             expect(result).to.not.be.empty;
157                             expect(result.data).to.be.a('string');
158                             expect(data).to.equal(data);
159                             done();
160                         });
161                 });
162         });
163     }).timeout(3000);
164
165     it('Random data, input as base64', function (done) {
166         let data = bigBoringString(0.001);
167         let b64data = btoa(data);
168         let prm = Gpgmejs.init();
169         prm.then(function (context) {
170             context.encrypt(b64data,
171                 inputvalues.encrypt.good.fingerprint, true).then(
172                 function (answer) {
173                     expect(answer).to.not.be.empty;
174                     expect(answer.data).to.be.a("string");
175                     expect(answer.data).to.include(
176                         'BEGIN PGP MESSAGE');
177                     expect(answer.data).to.include(
178                         'END PGP MESSAGE');
179                     context.decrypt(answer.data, true).then(
180                         function (result) {
181                             expect(result).to.not.be.empty;
182                             expect(result.data).to.be.a('string');
183                             expect(result.data).to.equal(b64data);
184                             done();
185                         });
186                 });
187         });
188     }).timeout(3000);
189
190     it('Random data, input and output as base64', function (done) {
191         let data = bigBoringString(0.0001);
192         let b64data = btoa(data);
193         let prm = Gpgmejs.init();
194         prm.then(function (context) {
195             context.encrypt(b64data,
196                 inputvalues.encrypt.good.fingerprint).then(
197                 function (answer) {
198                     expect(answer).to.not.be.empty;
199                     expect(answer.data).to.be.a("string");
200
201                     expect(answer.data).to.include(
202                         'BEGIN PGP MESSAGE');
203                     expect(answer.data).to.include(
204                         'END PGP MESSAGE');
205                     context.decrypt(answer.data, true).then(
206                         function (result) {
207                             expect(result).to.not.be.empty;
208                             expect(result.data).to.be.a('string');
209                             expect(result.data).to.equal(b64data);
210                             done();
211                         });
212                 });
213         });
214     }).timeout(3000);
215
216
217 });