Fixed gpg export key problem. Now returns key not found when it's not there. sync
authorKrista 'DarthMama' Bennett <krista@pep.foundation>
Thu, 02 May 2019 08:17:28 +0200
branchsync
changeset 359637f68fb62406
parent 3589 0627024f0184
child 3597 572de760c0bf
Fixed gpg export key problem. Now returns key not found when it's not there.
src/pgp_gpg.c
test/include/ExportKeyTests.h
test/src/SuiteMaker.cc
test/src/engine_tests/ExportKeyTests.cc
     1.1 --- a/src/pgp_gpg.c	Tue Apr 30 18:04:39 2019 +0200
     1.2 +++ b/src/pgp_gpg.c	Thu May 02 08:17:28 2019 +0200
     1.3 @@ -1615,7 +1615,7 @@
     1.4      gpgme_error_t gpgme_error;
     1.5      gpgme_data_t dh;
     1.6      size_t _size;
     1.7 -    char *buffer;
     1.8 +    char *buffer = NULL;
     1.9      int reading;
    1.10  
    1.11      assert(session);
    1.12 @@ -1666,6 +1666,16 @@
    1.13      assert(_size != -1);
    1.14      gpg.gpgme_data_seek(dh, 0, SEEK_SET);
    1.15  
    1.16 +    // Unfortunately, gpgme doesn't give us an error
    1.17 +    // when no key is found, so we end up with an 
    1.18 +    // empty string. So we need to do this:
    1.19 +    if (_size == 0) {
    1.20 +        *key_data = NULL;
    1.21 +        *size = 0;
    1.22 +        gpg.gpgme_data_release(dh);
    1.23 +        return PEP_KEY_NOT_FOUND;
    1.24 +    }
    1.25 +        
    1.26      buffer = malloc(_size + 1);
    1.27      assert(buffer);
    1.28      if (buffer == NULL) {
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/test/include/ExportKeyTests.h	Thu May 02 08:17:28 2019 +0200
     2.3 @@ -0,0 +1,20 @@
     2.4 +// This file is under GNU General Public License 3.0
     2.5 +// see LICENSE.txt
     2.6 +
     2.7 +#ifndef EXPORT_KEY_H
     2.8 +#define EXPORT_KEY_H
     2.9 +
    2.10 +#include <string>
    2.11 +#include "EngineTestIndividualSuite.h"
    2.12 +
    2.13 +using namespace std;
    2.14 +
    2.15 +class ExportKeyTests : public EngineTestIndividualSuite {
    2.16 +    public:
    2.17 +        ExportKeyTests(string test_suite, string test_home_dir);
    2.18 +    private:
    2.19 +        void check_export_key_no_key();
    2.20 +        void check_export_key_no_secret_key();
    2.21 +};
    2.22 +
    2.23 +#endif
     3.1 --- a/test/src/SuiteMaker.cc	Tue Apr 30 18:04:39 2019 +0200
     3.2 +++ b/test/src/SuiteMaker.cc	Thu May 02 08:17:28 2019 +0200
     3.3 @@ -25,6 +25,7 @@
     3.4  #include "PgpBinaryTests.h"
     3.5  #include "SubkeyRatingEvalTests.h"
     3.6  #include "MessageNullFromTests.h"
     3.7 +#include "ExportKeyTests.h"
     3.8  #include "LeastCommonDenomColorTests.h"
     3.9  #include "StringlistTests.h"
    3.10  #include "PgpListKeysTests.h"
    3.11 @@ -81,6 +82,7 @@
    3.12      "PgpBinaryTests",
    3.13      "SubkeyRatingEvalTests",
    3.14      "MessageNullFromTests",
    3.15 +    "ExportKeyTests",
    3.16      "LeastCommonDenomColorTests",
    3.17      "StringlistTests",
    3.18      "PgpListKeysTests",
    3.19 @@ -124,7 +126,7 @@
    3.20  };
    3.21  
    3.22  // This file is generated, so magic constants are ok.
    3.23 -int SuiteMaker::num_suites = 53;
    3.24 +int SuiteMaker::num_suites = 54;
    3.25  
    3.26  void SuiteMaker::suitemaker_build(const char* test_class_name, const char* test_home, Test::Suite** test_suite) {
    3.27      if (strcmp(test_class_name, "MimeTests") == 0)
    3.28 @@ -153,6 +155,8 @@
    3.29          *test_suite = new SubkeyRatingEvalTests(test_class_name, test_home);
    3.30      else if (strcmp(test_class_name, "MessageNullFromTests") == 0)
    3.31          *test_suite = new MessageNullFromTests(test_class_name, test_home);
    3.32 +    else if (strcmp(test_class_name, "ExportKeyTests") == 0)
    3.33 +        *test_suite = new ExportKeyTests(test_class_name, test_home);
    3.34      else if (strcmp(test_class_name, "LeastCommonDenomColorTests") == 0)
    3.35          *test_suite = new LeastCommonDenomColorTests(test_class_name, test_home);
    3.36      else if (strcmp(test_class_name, "StringlistTests") == 0)
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/test/src/engine_tests/ExportKeyTests.cc	Thu May 02 08:17:28 2019 +0200
     4.3 @@ -0,0 +1,61 @@
     4.4 +// This file is under GNU General Public License 3.0
     4.5 +// see LICENSE.txt
     4.6 +
     4.7 +#include <stdlib.h>
     4.8 +#include <cstring>
     4.9 +#include <string>
    4.10 +
    4.11 +#include <cpptest.h>
    4.12 +#include "test_util.h"
    4.13 +
    4.14 +#include "pEpEngine.h"
    4.15 +
    4.16 +#include "EngineTestIndividualSuite.h"
    4.17 +#include "ExportKeyTests.h"
    4.18 +
    4.19 +using namespace std;
    4.20 +
    4.21 +ExportKeyTests::ExportKeyTests(string suitename, string test_home_dir) :
    4.22 +    EngineTestIndividualSuite::EngineTestIndividualSuite(suitename, test_home_dir) {
    4.23 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("ExportKeyTests::check_export_key_no_key"),
    4.24 +                                                                      static_cast<Func>(&ExportKeyTests::check_export_key_no_key)));
    4.25 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("ExportKeyTests::check_export_key_no_secret_key"),
    4.26 +                                                                      static_cast<Func>(&ExportKeyTests::check_export_key_no_secret_key)));
    4.27 +}
    4.28 +
    4.29 +void ExportKeyTests::check_export_key_no_key() {
    4.30 +    char* keydata = NULL;
    4.31 +    size_t keysize = 0;
    4.32 +    PEP_STATUS status = export_key(session, "BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39", 
    4.33 +                                   &keydata, &keysize);
    4.34 +    TEST_ASSERT_MSG(status == PEP_KEY_NOT_FOUND, tl_status_string(status));
    4.35 +    free(keydata);
    4.36 +    keydata = NULL;
    4.37 +    keysize = 0;
    4.38 +    status = export_secret_key(session, "BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39", 
    4.39 +                                   &keydata, &keysize);
    4.40 +    TEST_ASSERT_MSG(status == PEP_KEY_NOT_FOUND, tl_status_string(status));
    4.41 +    free(keydata);
    4.42 +
    4.43 +    TEST_ASSERT(true);
    4.44 +}
    4.45 +
    4.46 +void ExportKeyTests::check_export_key_no_secret_key() {
    4.47 +    // Own pub key
    4.48 +    TEST_ASSERT_MSG(slurp_and_import_key(session, "test_keys/pub/pep-test-bob-0xC9C2EE39_pub.asc"),
    4.49 +                    "Unable to import test_keys/pub/pep-test-bob-0xC9C2EE39_pub.asc");
    4.50 +
    4.51 +    char* keydata = NULL;
    4.52 +    size_t keysize = 0;
    4.53 +    PEP_STATUS status = export_key(session, "BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39", 
    4.54 +                                   &keydata, &keysize);
    4.55 +    TEST_ASSERT_MSG(status == PEP_STATUS_OK, tl_status_string(status));
    4.56 +    free(keydata);
    4.57 +    keydata = NULL;
    4.58 +    keysize = 0;
    4.59 +    status = export_secret_key(session, "BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39", 
    4.60 +                                   &keydata, &keysize);
    4.61 +    TEST_ASSERT_MSG(status == PEP_KEY_NOT_FOUND, tl_status_string(status));
    4.62 +    free(keydata);
    4.63 +    TEST_ASSERT(true);
    4.64 +}