ENGINE-552: Ok, sender fpr basics in, and YES YOU WERE CORRECT, ROKER ;) ENGINE-552
authorKrista 'DarthMama' Bennett <krista@pep.foundation>
Wed, 15 May 2019 15:50:18 +0200
branchENGINE-552
changeset 370842c376229484
parent 3707 5e780ba44426
child 3709 c560e8fd8246
child 3714 c47bc77a81ce
ENGINE-552: Ok, sender fpr basics in, and YES YOU WERE CORRECT, ROKER ;)
src/message_api.c
test/include/SenderFPRTests.h
test/src/SuiteMaker.cc
test/src/engine_tests/SenderFPRTests.cc
     1.1 --- a/src/message_api.c	Wed May 15 14:35:45 2019 +0200
     1.2 +++ b/src/message_api.c	Wed May 15 15:50:18 2019 +0200
     1.3 @@ -3294,51 +3294,6 @@
     1.4  
     1.5  }
     1.6  
     1.7 -static void parse_pEp_protocol_version(const char* version_str, unsigned int &major, unsigned int &minor) {
     1.8 -    *major = 0;
     1.9 -    *minor = 0;
    1.10 -    int mj = 0;
    1.11 -    int mn = 0;
    1.12 -    int i;
    1.13 -    char* buf = NULL;
    1.14 -    
    1.15 -    if (!EMPTYSTR(version_str)) {
    1.16 -        const char* split = strstr(version_str, ".");
    1.17 -        if (!EMPTYSTR(split) && split != version_str) {
    1.18 -            // I think presuming we're not going to have a protocol larger than 99999.99999 is safe.
    1.19 -            // Famous last programming words, I know. FIXME: put this up with the version as a constant.
    1.20 -            const unsigned int _PEP_VERPARSE_BUFSIZE = 12;
    1.21 -            buf = calloc(_PEP_VERPARSE_BUFSIZE, 1);
    1.22 -            size_t major_digits = (size_t)(version_str - split);
    1.23 -            if (major_digits < _PEP_VERPARSE_BUFSIZE) {
    1.24 -                for (i = 0; i < major_digits; i++, version_str++) {
    1.25 -                    buf[i] = *version_str;
    1.26 -                }
    1.27 -                        
    1.28 -                mj = atoi(buf);
    1.29 -                if (mj > 0) {
    1.30 -                    size_t minor_digits = strlen(++version_str);
    1.31 -                    if (minor_digits < (_PEP_VERPARSE_BUFSIZE - major_digits)) {
    1.32 -                        mn = atoi(version_str);
    1.33 -                        if (mn >= 0) {
    1.34 -                            if (mn == 0) {
    1.35 -                                for (i = 0; i < minor_digits; i++) {
    1.36 -                                    if (*(++split) != '0')
    1.37 -                                        goto pEp_free;
    1.38 -                                }
    1.39 -                            }
    1.40 -                            *major = mj;
    1.41 -                            *minor = mn;
    1.42 -                        }
    1.43 -                    } 
    1.44 -                }                    
    1.45 -            }
    1.46 -        }
    1.47 -    }
    1.48 -pEp_free:
    1.49 -    free(buf);
    1.50 -}
    1.51 -
    1.52  static PEP_STATUS _decrypt_message(
    1.53          PEP_SESSION session,
    1.54          message *src,
    1.55 @@ -3617,10 +3572,15 @@
    1.56                                      pEp_protocol_version = stringpair_list_find(inner_message->opt_fields, "X-pEp-Version");
    1.57                                      unsigned int pEp_v_major = 0;
    1.58                                      unsigned int pEp_v_minor = 0;
    1.59 -                                    if (pEp_protocol_version)
    1.60 -                                        parse_pEp_protocol_version(pep_protocol_version->value, &pEp_v_major, &pEp_v_minor);
    1.61 +                                    if (pEp_protocol_version && !EMPTYSTR(pEp_protocol_version->value->value)) {
    1.62 +                                        // Roker is of course right. Meh :)
    1.63 +                                        if (sscanf(pEp_protocol_version->value->value, "%u.%u", &pEp_v_major, &pEp_v_minor) != 2) {
    1.64 +                                            pEp_v_major = 0;
    1.65 +                                            pEp_v_minor = 0;
    1.66 +                                        }
    1.67 +                                    }
    1.68                                      
    1.69 -                                    if (((pEp_v_major == 2) && (pEp_v_minor >= 1)) || (pEp_v_major > 2))                              
    1.70 +                                    if (((pEp_v_major == 2) && (pEp_v_minor > 0)) || (pEp_v_major > 2))                              
    1.71                                          sender_fpr = stringpair_list_find(inner_message->opt_fields, "X-pEp-Sender-FPR");
    1.72  
    1.73                                      // FIXME - Message 2.1                                    
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/test/include/SenderFPRTests.h	Wed May 15 15:50:18 2019 +0200
     2.3 @@ -0,0 +1,19 @@
     2.4 +// This file is under GNU General Public License 3.0
     2.5 +// see LICENSE.txt
     2.6 +
     2.7 +#ifndef SENDER_F_P_R_H
     2.8 +#define SENDER_F_P_R_H
     2.9 +
    2.10 +#include <string>
    2.11 +#include "EngineTestIndividualSuite.h"
    2.12 +
    2.13 +using namespace std;
    2.14 +
    2.15 +class SenderFPRTests : public EngineTestIndividualSuite {
    2.16 +    public:
    2.17 +        SenderFPRTests(string test_suite, string test_home_dir);
    2.18 +    private:
    2.19 +        void check_sender_f_p_r();
    2.20 +};
    2.21 +
    2.22 +#endif
     3.1 --- a/test/src/SuiteMaker.cc	Wed May 15 14:35:45 2019 +0200
     3.2 +++ b/test/src/SuiteMaker.cc	Wed May 15 15:50:18 2019 +0200
     3.3 @@ -69,6 +69,7 @@
     3.4  #include "OwnKeysRetrieveTests.h"
     3.5  #include "TrustManipulationTests.h"
     3.6  #include "SyncTests.h"
     3.7 +#include "SenderFPRTests.h"
     3.8  #include "RevocationTests.h"
     3.9  #include "AppleMailTests.h"
    3.10  
    3.11 @@ -131,12 +132,13 @@
    3.12      "OwnKeysRetrieveTests",
    3.13      "TrustManipulationTests",
    3.14      "SyncTests",
    3.15 +    "SenderFPRTests",
    3.16      "RevocationTests",
    3.17      "AppleMailTests",
    3.18  };
    3.19  
    3.20  // This file is generated, so magic constants are ok.
    3.21 -int SuiteMaker::num_suites = 59;
    3.22 +int SuiteMaker::num_suites = 60;
    3.23  
    3.24  void SuiteMaker::suitemaker_build(const char* test_class_name, const char* test_home, Test::Suite** test_suite) {
    3.25      if (strcmp(test_class_name, "URIAddressTests") == 0)
    3.26 @@ -253,6 +255,8 @@
    3.27          *test_suite = new TrustManipulationTests(test_class_name, test_home);
    3.28      else if (strcmp(test_class_name, "SyncTests") == 0)
    3.29          *test_suite = new SyncTests(test_class_name, test_home);
    3.30 +    else if (strcmp(test_class_name, "SenderFPRTests") == 0)
    3.31 +        *test_suite = new SenderFPRTests(test_class_name, test_home);
    3.32      else if (strcmp(test_class_name, "RevocationTests") == 0)
    3.33          *test_suite = new RevocationTests(test_class_name, test_home);
    3.34      else if (strcmp(test_class_name, "AppleMailTests") == 0)
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/test/src/engine_tests/SenderFPRTests.cc	Wed May 15 15:50:18 2019 +0200
     4.3 @@ -0,0 +1,79 @@
     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 +#include "mime.h"
    4.16 +
    4.17 +#include "EngineTestIndividualSuite.h"
    4.18 +#include "SenderFPRTests.h"
    4.19 +
    4.20 +using namespace std;
    4.21 +
    4.22 +SenderFPRTests::SenderFPRTests(string suitename, string test_home_dir) :
    4.23 +    EngineTestIndividualSuite::EngineTestIndividualSuite(suitename, test_home_dir) {
    4.24 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("SenderFPRTests::check_sender_f_p_r"),
    4.25 +                                                                      static_cast<Func>(&SenderFPRTests::check_sender_f_p_r)));
    4.26 +}
    4.27 +
    4.28 +void SenderFPRTests::check_sender_f_p_r() {
    4.29 +    const char* alice_fpr = "4ABE3AAF59AC32CFE4F86500A9411D176FF00E97";
    4.30 +    const char* bob_fpr = "BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39";
    4.31 +    PEP_STATUS status = read_file_and_import_key(session,
    4.32 +                "test_keys/pub/pep-test-alice-0x6FF00E97_pub.asc");
    4.33 +    TEST_ASSERT(status == PEP_KEY_IMPORTED);
    4.34 +    status = set_up_ident_from_scratch(session,
    4.35 +                "test_keys/priv/pep-test-alice-0x6FF00E97_priv.asc",
    4.36 +                "pep.test.alice@pep-project.org", alice_fpr,
    4.37 +                PEP_OWN_USERID, "Alice in Wonderland", NULL, true
    4.38 +            );
    4.39 +    TEST_ASSERT(status == PEP_STATUS_OK);
    4.40 +    TEST_ASSERT_MSG(slurp_and_import_key(session, "test_keys/pub/pep-test-bob-0xC9C2EE39_pub.asc"),
    4.41 +                    "Unable to import test_keys/pub/pep-test-bob-0xC9C2EE39_pub.asc");
    4.42 +                    
    4.43 +
    4.44 +    message* msg = new_message(PEP_dir_outgoing);
    4.45 +    pEp_identity* alice = new_identity("pep.test.alice@pep-project.org", NULL, PEP_OWN_USERID, NULL);                
    4.46 +    pEp_identity* bob = new_identity("pep.test.bob@pep-project.org", NULL, "Bob", NULL);
    4.47 +    status = myself(session, alice);
    4.48 +    TEST_ASSERT(status == PEP_STATUS_OK);
    4.49 +    status = update_identity(session, bob);
    4.50 +    TEST_ASSERT(status == PEP_STATUS_OK);
    4.51 +    
    4.52 +    status = set_as_pEp_user(session, bob);
    4.53 +    TEST_ASSERT(status == PEP_STATUS_OK);
    4.54 +
    4.55 +    msg->to = new_identity_list(bob);
    4.56 +    msg->from = alice;
    4.57 +    msg->shortmsg = strdup("Yo Bob!");
    4.58 +    msg->longmsg = strdup("Look at my hot new sender fpr field!");
    4.59 +
    4.60 +    message* enc_msg = NULL;
    4.61 +    
    4.62 +    status = encrypt_message(session, msg, NULL, &enc_msg, PEP_enc_PGP_MIME, 0);
    4.63 +    TEST_ASSERT(status == PEP_STATUS_OK);
    4.64 +    TEST_ASSERT(stringpair_list_find(enc_msg->opt_fields, "X-pEp-Sender-FPR") == NULL);
    4.65 +    
    4.66 +    message* dec_msg = NULL;
    4.67 +
    4.68 +    stringlist_t* keylist = NULL;
    4.69 +    PEP_rating rating;
    4.70 +    PEP_decrypt_flags_t flags = 0;
    4.71 +    status = decrypt_message(session, enc_msg, &dec_msg, &keylist, &rating, &flags);
    4.72 +    TEST_ASSERT(status == PEP_STATUS_OK);
    4.73 +
    4.74 +    char* text = NULL;
    4.75 +    mime_encode_message(dec_msg, false, &text);
    4.76 +    cout << text << endl;
    4.77 +    free(text);
    4.78 +    
    4.79 +    stringpair_list_t* fpr_node = stringpair_list_find(dec_msg->opt_fields, "X-pEp-Sender-FPR");
    4.80 +    TEST_ASSERT(fpr_node);
    4.81 +    TEST_ASSERT(strcmp(fpr_node->value->value, alice_fpr) == 0);
    4.82 +}