e28dd66bb871325fc55f5704a2360ce01631d44f
[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
21 describe('Encryption and Decryption', function () {
22     it('Successful encrypt and decrypt', function (done) {
23         let prm = Gpgmejs.init();
24         prm.then(function (context) {
25             context.encrypt(
26                 inputvalues.encrypt.good.data,
27                 inputvalues.encrypt.good.fingerprint).then(function (answer) {
28                     expect(answer).to.not.be.empty;
29                     expect(answer.data).to.be.a("string");
30                     expect(answer.data).to.include('BEGIN PGP MESSAGE');
31                     expect(answer.data).to.include('END PGP MESSAGE');
32                     context.decrypt(answer.data).then(function (result) {
33                         expect(result).to.not.be.empty;
34                         expect(result.data).to.be.a('string');
35                         expect(result.data).to.equal(inputvalues.encrypt.good.data);
36                         context.connection.disconnect();
37                         done();
38                     });
39                 });
40         });
41     });
42
43     /**
44      * Fails with random data! Some bytes (up to 100) of the original are missing in
45      * the result
46 */
47 /**
48     for (let j = 0; j < 10; j++){
49     it('Successful encrypt and decrypt specific sets: ',
50             function (done) {
51                 let prm = Gpgmejs.init();
52                 let data = bigBoringString(5); //see ./inputvalues.js
53                 expect(Object.prototype.toString.call(data)).to.equal("[object String]");
54                 prm.then(function (context) {
55                     context.encrypt(data,
56                         inputvalues.encrypt.good.fingerprint).then(
57                         function (answer) {
58                             expect(answer).to.not.be.empty;
59                             expect(answer.data).to.be.a("string");
60                             expect(answer.data).to.include(
61                                 'BEGIN PGP MESSAGE');
62                             expect(answer.data).to.include(
63                                 'END PGP MESSAGE');
64                             context.decrypt(answer.data).then(
65                                 function (result) {
66                                     if (data.length !== result.data.length) {
67
68                                         for (let k = 0; k < data.length; k++) {
69                                             if (data[k] !== result.data[k]) {
70                                                 console.log(k);
71                                                 console.log(data[k - 2] + data[k - 1] + data[k] + data[k + 1]);
72                                                 console.log(result.data[k - 2] + result.data[k - 1] + result.data[k] + result.data[k + 1]);
73                                                 break;
74                                             }
75                                         }
76                                     }
77                                     expect(result).to.not.be.empty;
78                                     expect(result.data).to.be.a('string');
79                                     expect(result.data).to.equal(data);
80                                     context.connection.disconnect();
81                                     done();
82
83                                 });
84                         });
85                 });
86             }).timeout(5000);
87         }
88
89
90     it('Roundtrip does not destroy trailing whitespace',
91         function (done) {
92             let prm = Gpgmejs.init();
93             prm.then(function (context) {
94                 let data = 'Keks. \rKeks \n Keks \r\n';
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                                 context.connection.disconnect();
110                                 done();
111
112                             });
113                     });
114             });
115         }).timeout(3000);
116
117     it('Test with simple non-ascii input',
118         function (done) {
119             let prm = Gpgmejs.init();
120             prm.then(function (context) {
121                 let data = '';
122                 for (let i=0; i < 1024 * 1024 * 0.1; i++){
123                     data += inputvalues.encrypt.good.data_nonascii;
124                 }
125                 context.encrypt(data,
126                     inputvalues.encrypt.good.fingerprint).then(
127                     function (answer) {
128                         expect(answer).to.not.be.empty;
129                         expect(answer.data).to.be.a("string");
130                         expect(answer.data).to.include(
131                             'BEGIN PGP MESSAGE');
132                         expect(answer.data).to.include(
133                             'END PGP MESSAGE');
134                         console.log(answer);
135                         context.decrypt(answer.data).then(
136                             function (result) {
137                                 expect(result).to.not.be.empty;
138                                 expect(result.data).to.be.a('string');
139                                 if (data.length !== result.data.length) {
140
141                                     for (let k = 0; k < data.length; k++) {
142                                         if (data[k] !== result.data[k]) {
143                                             console.log(k);
144                                             console.log(data[k - 2] + data[k - 1] + data[k] + data[k + 1]);
145                                             console.log(result.data[k - 2] + result.data[k - 1] + result.data[k] + result.data[k + 1]);
146                                             break;
147                                         }
148                                     }
149                                 }
150                                 console.log(data.length - result.data.length);
151                                 expect(result.data).to.equal(data);
152                                 context.connection.disconnect();
153                                 done();
154
155                             });
156                     });
157             });
158         }).timeout(3000);
159 */
160 /**
161     for (let i=0; i< 100; i++) {
162         it('Successful encrypt random data '+ (i+1) + '/100', function (done) {
163             let prm = Gpgmejs.init();
164             let data = bigString(0.2); // << set source data here
165                 prm.then(function (context) {
166                     context.encrypt(data,
167                         inputvalues.encrypt.good.fingerprint).then(
168                             function (answer){
169                                 expect(answer).to.not.be.empty;
170                                 expect(answer.data).to.be.a("string");
171                                 expect(answer.data).to.include(
172                                     'BEGIN PGP MESSAGE');
173                                 expect(answer.data).to.include(
174                                     'END PGP MESSAGE');
175                                 context.decrypt(answer.data).then(
176                                     function(result){
177                                         expect(result).to.not.be.empty;
178                                         expect(result.data).to.be.a('string');
179                                         expect(result.data).to.equal(data);
180                                         context.connection.disconnect();
181                                         done();
182                                 });
183                         });
184                 });
185         }).timeout(5000);
186     };
187 */
188
189 /** still fails
190     it('Successful encrypt 0.8 MB Uint8Array', function (done) {
191         let prm = Gpgmejs.init();
192         let data = bigUint8(0.8);
193         prm.then(function (context) {
194                 context.encrypt(data,
195                     inputvalues.encrypt.good.fingerprint).then(
196                         function (answer){
197                             expect(answer).to.not.be.empty;
198                             expect(answer.data).to.be.a("string");
199                             expect(answer.data).to.include(
200                                 'BEGIN PGP MESSAGE');
201                             expect(answer.data).to.include(
202                                 'END PGP MESSAGE');
203                             context.decrypt(answer.data).then(
204                                 function(result){
205                                     expect(result).to.not.be.empty;
206                                     expect(result.data).to.be.a('string');
207                                     expect(result.data).to.equal(data);
208                                     done();
209                             });
210                     });
211             });
212     }).timeout(5000);
213 */
214
215     it('Decrypt simple non-ascii',
216         function (done) {
217             let prm = Gpgmejs.init();
218             prm.then(function (context) {
219                 data = encryptedData;
220                 context.decrypt(data).then(
221                     function (result) {
222                         expect(result).to.not.be.empty;
223                         expect(result.data).to.be.a('string');
224                         expect(result.data).to.equal(inputvalues.encrypt.good.data_nonascii);
225                         context.encrypt(inputvalues.encrypt.good.data_nonascii, inputvalues.encrypt.good.fingerprint).then(
226                             function(result){
227                                 context.decrypt(result.data).then(function(answer){
228                                     expect(answer.data).to.equal(inputvalues.encrypt.good.data_nonascii);
229                                     context.connection.disconnect();
230                                     done();
231                                 });
232                             });
233                         });
234
235             });
236     }).timeout(8000);
237
238 });