5ef68a32ad10b672862f54a7c8e68ad5990964b8
[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 = bigString(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 /**
55     it('Successful encrypt 20 MB', function (done) {
56         let prm = Gpgmejs.init();
57         let data = bigString(20);
58         prm.then(function (context) {
59             context.encrypt(
60                 data,
61                 inputvalues.encrypt.good.fingerprint).then(function (answer) {
62                     expect(answer).to.not.be.empty;
63                     expect(answer.data).to.be.a("string");
64                     expect(answer.data).to.include('BEGIN PGP MESSAGE');
65                     expect(answer.data).to.include('END PGP MESSAGE');
66                     context.connection.disconnect();
67                     done();
68                 });
69         });
70     }).timeout(20000);
71 */
72 /**
73     it('Successful encrypt 30 MB', function (done) {
74         // TODO: There seems to be a limit imposed at least by chrome at about 21 MB
75         let prm = Gpgmejs.init();
76         let data = bigString(30);
77         prm.then(function (context) {
78             context.encrypt(
79                 data,
80                 inputvalues.encrypt.good.fingerprint).then(function (answer) {
81                     expect(answer).to.not.be.empty;
82                     expect(answer.data).to.be.a("string");
83                     expect(answer.data).to.include('BEGIN PGP MESSAGE');
84                     expect(answer.data).to.include('END PGP MESSAGE');
85                     context.connection.disconnect();
86                     done();
87                 });
88         });
89     }).timeout(20000);
90 */
91
92     it('Sending encryption without keys fails', function (done) {
93         let prm = Gpgmejs.init();
94         prm.then(function (context) {
95             context.encrypt(
96                 inputvalues.encrypt.good.data,
97                 null).then(function (answer) {
98                     expect(answer).to.be.undefined;
99                 }, function(error){
100                     expect(error).to.be.an('Error');
101                     expect(error.code).to.equal('MSG_INCOMPLETE');
102                     context.connection.disconnect();
103                     done();
104                 });
105         });
106     });
107
108     it('Sending encryption without data fails', function (done) {
109         let prm = Gpgmejs.init();
110         prm.then(function (context) {
111             context.encrypt(
112                 null, inputvalues.encrypt.good.keyid).then(function (answer) {
113                     expect(answer).to.be.undefined;
114                 }, function (error) {
115                     expect(error).to.be.an.instanceof(Error);
116                     expect(error.code).to.equal('MSG_INCOMPLETE');
117                     context.connection.disconnect();
118                     done();
119                 });
120         });
121     });
122
123
124     it('Sending encryption with non existing keys fails', function (done) {
125         let prm = Gpgmejs.init();
126         prm.then(function (context) {
127             context.encrypt(
128                 inputvalues.encrypt.good.data,
129                 inputvalues.encrypt.bad.fingerprint).then(function (answer) {
130                     expect(answer).to.be.undefined;
131                 }, function(error){
132                     expect(error).to.be.an('Error');
133                     expect(error.code).to.not.be.undefined;
134                     expect(error.code).to.equal('GNUPG_ERROR');
135                     context.connection.disconnect();
136                     done();
137                 });
138         });
139     }).timeout(5000);;
140
141     it('Overly large message ( >= 48MB) is rejected', function (done) {
142         let prm = Gpgmejs.init();
143         prm.then(function (context) {
144             context.encrypt(
145                 bigString(48),
146                 inputvalues.encrypt.good.fingerprint).then(function (answer) {
147                     expect(answer).to.be.undefined;
148                 }, function(error){
149                     expect(error).to.be.an.instanceof(Error);
150                     // TODO who is throwing the error here?
151                     // It is not a GPGME_Error!
152                     context.connection.disconnect();
153                     done();
154                 });
155         });
156     }).timeout(8000);
157     // TODO check different valid parameter
158 });