merging sync
authorVolker Birk <vb@pep-project.org>
Thu, 02 May 2019 11:01:53 +0200
branchsync
changeset 360265065c61bae9
parent 3601 0052c5587b9a
parent 3600 07d9a2af323f
child 3604 4bf5759ea073
merging
     1.1 --- a/src/pgp_gpg.c	Thu May 02 11:01:42 2019 +0200
     1.2 +++ b/src/pgp_gpg.c	Thu May 02 11:01:53 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 11:01:53 2019 +0200
     2.3 @@ -0,0 +1,22 @@
     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_pubkey();
    2.21 +        void check_export_key_secret_key();
    2.22 +        void check_export_key_no_secret_key();
    2.23 +};
    2.24 +
    2.25 +#endif
     3.1 --- a/test/src/SuiteMaker.cc	Thu May 02 11:01:42 2019 +0200
     3.2 +++ b/test/src/SuiteMaker.cc	Thu May 02 11:01:53 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 11:01:53 2019 +0200
     4.3 @@ -0,0 +1,128 @@
     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_pubkey"),
    4.26 +                                                                      static_cast<Func>(&ExportKeyTests::check_export_key_pubkey)));
    4.27 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("ExportKeyTests::check_export_key_no_secret_key"),
    4.28 +                                                                      static_cast<Func>(&ExportKeyTests::check_export_key_no_secret_key)));
    4.29 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("ExportKeyTests::check_export_key_no_secret_key"),
    4.30 +                                                                      static_cast<Func>(&ExportKeyTests::check_export_key_no_secret_key)));
    4.31 +}
    4.32 +
    4.33 +void ExportKeyTests::check_export_key_no_key() {
    4.34 +    char* keydata = NULL;
    4.35 +    size_t keysize = 0;
    4.36 +    PEP_STATUS status = export_key(session, "BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39", 
    4.37 +                                   &keydata, &keysize);
    4.38 +    TEST_ASSERT_MSG(status == PEP_KEY_NOT_FOUND, tl_status_string(status));
    4.39 +    free(keydata);
    4.40 +    keydata = NULL;
    4.41 +    keysize = 0;
    4.42 +    status = export_secret_key(session, "BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39", 
    4.43 +                                   &keydata, &keysize);
    4.44 +    TEST_ASSERT_MSG(status == PEP_KEY_NOT_FOUND, tl_status_string(status));
    4.45 +    free(keydata);
    4.46 +
    4.47 +    TEST_ASSERT(true);
    4.48 +}
    4.49 +
    4.50 +void ExportKeyTests::check_export_key_pubkey() {
    4.51 +    // Own pub key
    4.52 +    TEST_ASSERT_MSG(slurp_and_import_key(session, "test_keys/pub/pep-test-bob-0xC9C2EE39_pub.asc"),
    4.53 +                    "Unable to import test_keys/pub/pep-test-bob-0xC9C2EE39_pub.asc");
    4.54 +
    4.55 +    char* keydata = NULL;
    4.56 +    size_t keysize = 0;
    4.57 +    stringlist_t* keylist = NULL;
    4.58 +    PEP_STATUS status = find_keys(session, "BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39", &keylist);
    4.59 +    TEST_ASSERT(keylist && keylist->value);
    4.60 +    TEST_ASSERT(strcmp(keylist->value, "BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39") == 0);
    4.61 +    free_stringlist(keylist);
    4.62 +
    4.63 +    status = export_key(session, "BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39", 
    4.64 +                                   &keydata, &keysize);
    4.65 +    TEST_ASSERT_MSG(status == PEP_STATUS_OK, tl_status_string(status));
    4.66 +    TEST_ASSERT(keydata);
    4.67 +    TEST_ASSERT(keysize > 0);
    4.68 +    
    4.69 +    free(keydata);
    4.70 +}
    4.71 +
    4.72 +void ExportKeyTests::check_export_key_secret_key() {
    4.73 +    TEST_ASSERT_MSG(slurp_and_import_key(session, "test_keys/pub/pep-test-bob-0xC9C2EE39_pub.asc"),
    4.74 +                    "Unable to import test_keys/pub/pep-test-bob-0xC9C2EE39_pub.asc");
    4.75 +    TEST_ASSERT_MSG(slurp_and_import_key(session, "test_keys/priv/pep-test-bob-0xC9C2EE39_priv.asc"),
    4.76 +                    "Unable to import test_keys/priv/pep-test-bob-0xC9C2EE39_priv.asc");
    4.77 +    char* keydata = NULL;
    4.78 +    size_t keysize = 0;
    4.79 +    stringlist_t* keylist = NULL;
    4.80 +    PEP_STATUS status = find_keys(session, "BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39", &keylist);
    4.81 +    TEST_ASSERT(keylist && keylist->value);
    4.82 +    TEST_ASSERT(strcmp(keylist->value, "BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39") == 0);
    4.83 +    free_stringlist(keylist);
    4.84 +    keylist = NULL;
    4.85 +    
    4.86 +    bool has_private = false;
    4.87 +    contains_priv_key(session, "BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39", &has_private);
    4.88 +    TEST_ASSERT_MSG(has_private, "Secret key not found.")
    4.89 +    
    4.90 +    status = export_key(session, "BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39", 
    4.91 +                                   &keydata, &keysize);
    4.92 +    TEST_ASSERT_MSG(status == PEP_STATUS_OK, tl_status_string(status));
    4.93 +    TEST_ASSERT(keydata);
    4.94 +    TEST_ASSERT(keysize > 0);
    4.95 +
    4.96 +    free(keydata);
    4.97 +    keydata = NULL;
    4.98 +    keysize = 0;
    4.99 +    status = export_secret_key(session, "BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39", 
   4.100 +                                   &keydata, &keysize);
   4.101 +    TEST_ASSERT_MSG(status == PEP_STATUS_OK, tl_status_string(status));
   4.102 +
   4.103 +    free(keydata);
   4.104 +    TEST_ASSERT(true);
   4.105 +}
   4.106 +
   4.107 +
   4.108 +void ExportKeyTests::check_export_key_no_secret_key() {
   4.109 +    // Own pub key
   4.110 +    TEST_ASSERT_MSG(slurp_and_import_key(session, "test_keys/pub/pep-test-bob-0xC9C2EE39_pub.asc"),
   4.111 +                    "Unable to import test_keys/pub/pep-test-bob-0xC9C2EE39_pub.asc");
   4.112 +
   4.113 +    char* keydata = NULL;
   4.114 +    size_t keysize = 0;
   4.115 +    stringlist_t* keylist = NULL;
   4.116 +    PEP_STATUS status = find_keys(session, "BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39", &keylist);
   4.117 +    TEST_ASSERT(keylist && keylist->value);
   4.118 +    TEST_ASSERT(strcmp(keylist->value, "BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39") == 0);
   4.119 +
   4.120 +    status = export_key(session, "BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39", 
   4.121 +                                   &keydata, &keysize);
   4.122 +    TEST_ASSERT_MSG(status == PEP_STATUS_OK, tl_status_string(status));
   4.123 +    free(keydata);
   4.124 +    keydata = NULL;
   4.125 +    keysize = 0;
   4.126 +    status = export_secret_key(session, "BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39", 
   4.127 +                                   &keydata, &keysize);
   4.128 +    TEST_ASSERT_MSG(status == PEP_KEY_NOT_FOUND, tl_status_string(status));
   4.129 +    free(keydata);
   4.130 +    TEST_ASSERT(true);
   4.131 +}