js: reduce request spam at getKeys()
authorMaximilian Krambach <maximilian.krambach@intevation.de>
Mon, 9 Jul 2018 09:24:46 +0000 (11:24 +0200)
committerMaximilian Krambach <maximilian.krambach@intevation.de>
Mon, 9 Jul 2018 09:24:46 +0000 (11:24 +0200)
--

* Don't make a secret-Key request for each Key retrieved, use one
  request for all of them instead, and assemble the info later. This
  should reduce the traffic with large Keyrings. The bulk retrieval
  for the public armored Keys for each of these Keys is still up to
  discussion

* unittests: disabled assertion for the armored key (as it currently
  doesn't work)

* encryptTest: clarified the mechanism/reason of rejection for
  Messages >64 MB. This is still a TODO, as this error comes from a
  different place (the browser itself) and behaves different from the
  other errors.

lang/js/BrowserTestExtension/tests/encryptTest.js
lang/js/src/Keyring.js
lang/js/unittests.js

index 2cb4e58..1114125 100644 (file)
@@ -133,7 +133,7 @@ describe('Encryption', function () {
         });
     }).timeout(5000);
 
-    it('Overly large message ( > 65MB) is rejected', function (done) {
+    it('Overly large message ( > 64MB) is rejected', function (done) {
         let prm = Gpgmejs.init();
         prm.then(function (context) {
             context.encrypt(
@@ -142,10 +142,11 @@ describe('Encryption', function () {
                 expect(answer).to.be.undefined;
             }, function(error){
                 expect(error).to.be.an.instanceof(Error);
-                // expect(error.code).to.equal('GNUPG_ERROR');
                 // TODO: there is a 64 MB hard limit at least in chrome at:
                 // chromium//extensions/renderer/messaging_util.cc:
                 // kMaxMessageLength
+                // The error will be a browser error, not from gnupg or from
+                // this library
                 done();
             });
         });
index 358757b..09c43f7 100644 (file)
@@ -56,28 +56,46 @@ export class GPGME_Keyring {
             }
             msg.post().then(function(result){
                 let resultset = [];
-                let promises = [];
                 if (result.keys.length === 0){
                     resolve([]);
                 } else {
-                    for (let i=0; i< result.keys.length; i++){
-                        let k = createKey(result.keys[i].fingerprint);
-                        k.setKeyData(result.keys[i]);
-                        if (prepare_sync === true){
-                            promises.push(k.getArmor());
-                            promises.push(k.getHasSecret());
-                        }
-                        resultset.push(k);
-                    }
-                    if (promises.length > 0) {
-                        Promise.all(promises).then(function() {
-                            resolve(resultset);
-                        }, function(error){
-                            reject(error);
-                        });
+                    let secondrequest;
+                    if (prepare_sync === true) {
+                        secondrequest = function() {
+                            msg.setParameter('secret', true);
+                            return msg.post();
+                        };
                     } else {
-                        resolve(resultset);
+                        secondrequest = function() {
+                            return Promise.resolve(true);
+                        };
                     }
+                    secondrequest().then(function(answer) {
+                        for (let i=0; i < result.keys.length; i++){
+                            if (prepare_sync === true){
+                                result.keys[i].hasSecret = false;
+                                if (answer && answer.keys) {
+                                    for (let j=0; j < answer.keys.length; j++ ){
+                                        if (result.keys[i].fingerprint ===
+                                            answer.keys[j].fingerprint
+                                        ) {
+                                            if (answer.keys[j].secret === true){
+                                                result.keys[i].hasSecret = true;
+                                            }
+                                            break;
+                                        }
+                                    }
+                                    // TODO getArmor() to be used in sync
+                                }
+                            }
+                            let k = createKey(result.keys[i].fingerprint);
+                            k.setKeyData(result.keys[i]);
+                            resultset.push(k);
+                        }
+                        resolve(resultset);
+                    }, function(error){
+                        reject(error);
+                    });
                 }
             });
         });
index 169e8eb..04e15ef 100644 (file)
@@ -272,9 +272,9 @@ function unittests (){
                 keyring.getKeys(null, true).then(function(result){
                     expect(result).to.be.an('array');
                     expect(result[0]).to.be.an.instanceof(GPGME_Key);
-                    expect(result[0].get('armored')).to.be.a('string');
-                    expect(result[0].get('armored')).to.include(
-                        '-----END PGP PUBLIC KEY BLOCK-----');
+                    expect(result[0].get('hasSecret')).to.be.a('boolean');
+                    // expect(result[0].get('armored')).to.include(
+                    //     '-----END PGP PUBLIC KEY BLOCK-----');
                     done();
                 });
             }
@@ -287,9 +287,11 @@ function unittests (){
                     function(result){
                         expect(result).to.be.an('array');
                         expect(result[0]).to.be.an.instanceof(GPGME_Key);
-                        expect(result[0].get('armored')).to.be.a('string');
-                        expect(result[0].get('armored')).to.include(
-                            '-----END PGP PUBLIC KEY BLOCK-----');
+                        expect(result[0].get('hasSecret')).to.be.a('boolean');
+                        // TODO: preparing sync for armored is still in discussion
+                        // expect(result[0].get('armored')).to.be.a('string');
+                        // expect(result[0].get('armored')).to.include(
+                        //     '-----END PGP PUBLIC KEY BLOCK-----');
                         done();
                     }
                 );