js: Testing lare messages
[gpgme.git] / lang / js / BrowserTestExtension / tests / encryptTest.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 describe('Encryption', function () {
21     it('Successful encrypt', function (done) {
22         let prm = Gpgmejs.init();
23         prm.then(function (context) {
24             context.encrypt(
25                 inputvalues.encrypt.good.data,
26                 inputvalues.encrypt.good.fingerprint).then(function (answer) {
27                     expect(answer).to.not.be.empty;
28                     expect(answer.data).to.be.a("string");
29                     expect(answer.data).to.include('BEGIN PGP MESSAGE');
30                     expect(answer.data).to.include('END PGP MESSAGE');
31                     context.connection.disconnect();
32                     done();
33                 });
34         });
35     });
36
37     it('Successful encrypt 5 MB', function (done) {
38         let prm = Gpgmejs.init();
39         let data = fixedLengthString(5);
40         prm.then(function (context) {
41             context.encrypt(
42                 data,
43                 inputvalues.encrypt.good.fingerprint).then(function (answer) {
44                     expect(answer).to.not.be.empty;
45                     expect(answer.data).to.be.a("string");
46                     expect(answer.data).to.include('BEGIN PGP MESSAGE');
47                     expect(answer.data).to.include('END PGP MESSAGE');
48                     context.connection.disconnect();
49                     done();
50                 });
51         });
52     }).timeout(10000);
53
54     it('Successful encrypt 20 MB', function (done) {
55         let prm = Gpgmejs.init();
56         let data = fixedLengthString(20);
57         prm.then(function (context) {
58             context.encrypt(
59                 data,
60                 inputvalues.encrypt.good.fingerprint).then(function (answer) {
61                     expect(answer).to.not.be.empty;
62                     expect(answer.data).to.be.a("string");
63                     expect(answer.data).to.include('BEGIN PGP MESSAGE');
64                     expect(answer.data).to.include('END PGP MESSAGE');
65                     context.connection.disconnect();
66                     done();
67                 });
68         });
69     }).timeout(20000);
70
71     it('Successful encrypt 50 MB', function (done) {
72         let prm = Gpgmejs.init();
73         let data = fixedLengthString(50);
74         prm.then(function (context) {
75             context.encrypt(
76                 data,
77                 inputvalues.encrypt.good.fingerprint).then(function (answer) {
78                     expect(answer).to.not.be.empty;
79                     expect(answer.data).to.be.a("string");
80                     expect(answer.data).to.include('BEGIN PGP MESSAGE');
81                     expect(answer.data).to.include('END PGP MESSAGE');
82                     context.connection.disconnect();
83                     done();
84                 });
85         });
86     }).timeout(20000);
87
88     it('Sending encryption without keys fails', function (done) {
89         let prm = Gpgmejs.init();
90         prm.then(function (context) {
91             context.encrypt(
92                 inputvalues.encrypt.good.data,
93                 null).then(function (answer) {
94                     expect(answer).to.be.undefined;
95                 }, function(error){
96                     expect(error).to.be.an('Error');
97                     expect(error.code).to.equal('MSG_INCOMPLETE');
98                     context.connection.disconnect();
99                     done();
100                 });
101         });
102     });
103
104     it('Sending encryption without data fails', function (done) {
105         let prm = Gpgmejs.init();
106         prm.then(function (context) {
107             context.encrypt(
108                 null, inputvalues.encrypt.good.keyid).then(function (answer) {
109                     expect(answer).to.be.undefined;
110                 }, function (error) {
111                     expect(error).to.be.an.instanceof(Error);
112                     expect(error.code).to.equal('MSG_INCOMPLETE');
113                     context.connection.disconnect();
114                     done();
115                 });
116         });
117     });
118
119     it('Sending encryption with non existing keys fails', function (done) {
120         let prm = Gpgmejs.init();
121         prm.then(function (context) {
122             context.encrypt(
123                 inputvalues.encrypt.good.data,
124                 inputvalues.encrypt.bad.fingerprint).then(function (answer) {
125                     expect(answer).to.be.undefined;
126                 }, function(error){
127                     expect(error).to.be.an('Error');
128                     expect(error.code).to.not.be.undefined;
129                     expect(error.code).to.equal('GNUPG_ERROR');
130                     context.connection.disconnect();
131                     done();
132                 });
133         });
134     }).timeout(5000);;
135
136     it('Overly large message ( > 65MB) is rejected', function (done) {
137         let prm = Gpgmejs.init();
138         prm.then(function (context) {
139             context.encrypt(
140                 fixedLengthString(65),
141                 inputvalues.encrypt.good.fingerprint).then(function (answer) {
142                     expect(answer).to.be.undefined;
143                 }, function(error){
144                     expect(error).to.be.an.instanceof(Error);
145                     // expect(error.code).to.equal('GNUPG_ERROR');
146                     // TODO: there is a 64 MB hard limit at least in chrome at:
147                     // chromium//extensions/renderer/messaging_util.cc:
148                     // kMaxMessageLength
149                     context.connection.disconnect();
150                     done();
151                 });
152         });
153     }).timeout(8000);
154
155     // TODO check different valid parameter
156 });