Converted AppleMailTest googletest
authorKrista 'DarthMama' Bennett <krista@pep.foundation>
Mon, 26 Aug 2019 11:00:07 +0200
branchgoogletest
changeset 40135f0d261aa755
parent 4012 beb70e301d31
child 4014 fa60b97b0b25
Converted AppleMailTest
gtest/src/AppleMailTest.cc
test/src/engine_tests/AppleMailTests.cc
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gtest/src/AppleMailTest.cc	Mon Aug 26 11:00:07 2019 +0200
     1.3 @@ -0,0 +1,211 @@
     1.4 +// This file is under GNU General Public License 3.0
     1.5 +// see LICENSE.txt
     1.6 +
     1.7 +#include <stdlib.h>
     1.8 +#include <string.h>
     1.9 +#include <iostream>
    1.10 +#include <fstream>
    1.11 +#include <string>
    1.12 +#include <cstring> // for strcmp()
    1.13 +#include "platform.h"
    1.14 +#include <iostream>
    1.15 +#include <fstream>
    1.16 +#include <sstream>
    1.17 +#include "keymanagement.h"
    1.18 +#include "message_api.h"
    1.19 +#include "mime.h"
    1.20 +#include "test_util.h" // for slurp()
    1.21 +#include "TestConstants.h"
    1.22 +
    1.23 +
    1.24 +
    1.25 +#include "Engine.h"
    1.26 +
    1.27 +#include <gtest/gtest.h>
    1.28 +
    1.29 +
    1.30 +namespace {
    1.31 +
    1.32 +	//The fixture for AppleMailTest
    1.33 +    class AppleMailTest : public ::testing::Test {
    1.34 +        public:
    1.35 +            Engine* engine;
    1.36 +            PEP_SESSION session;
    1.37 +
    1.38 +        protected:
    1.39 +            // You can remove any or all of the following functions if its body
    1.40 +            // is empty.
    1.41 +            AppleMailTest() {
    1.42 +                // You can do set-up work for each test here.
    1.43 +                test_suite_name = ::testing::UnitTest::GetInstance()->current_test_info()->test_suite_name();
    1.44 +                test_name = ::testing::UnitTest::GetInstance()->current_test_info()->name();
    1.45 +                test_path = get_main_test_home_dir() + "/" + test_suite_name + "/" + test_name;
    1.46 +            }
    1.47 +
    1.48 +            ~AppleMailTest() override {
    1.49 +                // You can do clean-up work that doesn't throw exceptions here.
    1.50 +            }
    1.51 +
    1.52 +            // If the constructor and destructor are not enough for setting up
    1.53 +            // and cleaning up each test, you can define the following methods:
    1.54 +
    1.55 +            void SetUp() override {
    1.56 +                // Code here will be called immediately after the constructor (right
    1.57 +                // before each test).
    1.58 +
    1.59 +                // Leave this empty if there are no files to copy to the home directory path
    1.60 +                std::vector<std::pair<std::string, std::string>> init_files = std::vector<std::pair<std::string, std::string>>();
    1.61 +
    1.62 +                // Get a new test Engine.
    1.63 +                engine = new Engine(test_path);
    1.64 +                ASSERT_NE(engine, nullptr);
    1.65 +
    1.66 +                // Ok, let's initialize test directories etc.
    1.67 +                engine->prep(NULL, NULL, init_files);
    1.68 +
    1.69 +                // Ok, try to start this bugger.
    1.70 +                engine->start();
    1.71 +                ASSERT_NE(engine->session, nullptr);
    1.72 +                session = engine->session;
    1.73 +
    1.74 +                // Engine is up. Keep on truckin'
    1.75 +            }
    1.76 +
    1.77 +            void TearDown() override {
    1.78 +                // Code here will be called immediately after each test (right
    1.79 +                // before the destructor).
    1.80 +                engine->shut_down();
    1.81 +                delete engine;
    1.82 +                engine = NULL;
    1.83 +                session = NULL;
    1.84 +            }
    1.85 +
    1.86 +        private:
    1.87 +            const char* test_suite_name;
    1.88 +            const char* test_name;
    1.89 +            string test_path;
    1.90 +            // Objects declared here can be used by all tests in the AppleMailTest suite.
    1.91 +
    1.92 +    };
    1.93 +
    1.94 +}  // namespace
    1.95 +
    1.96 +
    1.97 +TEST_F(AppleMailTest, check_apple_mail_text_signed_encrypted) {
    1.98 +
    1.99 +    const char* mailfile = "test_mails/apple_mail_TC_signed_encrypted.eml";
   1.100 +
   1.101 +    const string keytextkey1 = slurp("test_keys/pub/pep-test-apple-0x1CCBC7D7_pub.asc");
   1.102 +    const string keytextkey2 = slurp("test_keys/priv/pep-test-recip-0x08DB0AEE_priv.asc");
   1.103 +    const string keytextkey3 = slurp("test_keys/pub/pep-test-recip-0x08DB0AEE_pub.asc");
   1.104 +
   1.105 +    PEP_STATUS statuskey1 = import_key(session, keytextkey1.c_str(), keytextkey1.length(), NULL);
   1.106 +    PEP_STATUS statuskey2 = import_key(session, keytextkey2.c_str(), keytextkey2.length(), NULL);
   1.107 +    PEP_STATUS statuskey3 = import_key(session, keytextkey3.c_str(), keytextkey3.length(), NULL);
   1.108 +
   1.109 +    const string mailtext = slurp(mailfile);
   1.110 +    pEp_identity * me = new_identity("pep.test.recip@kgrothoff.org", "93D19F24AD6F4C4BA9134AAF84D9217908DB0AEE", PEP_OWN_USERID, "pEp Test Recipient");
   1.111 +    me->me = true;
   1.112 +    PEP_STATUS status = set_own_key(session, me, "93D19F24AD6F4C4BA9134AAF84D9217908DB0AEE");
   1.113 +
   1.114 +    pEp_identity * you = new_identity("pep.test.apple@pep-project.org", NULL, "pep.test.apple@pep-project.org", "pEp Apple Test");
   1.115 +    you->me = false;
   1.116 +    status = update_identity(session, you);
   1.117 +
   1.118 +    trust_personal_key(session, you);
   1.119 +
   1.120 +    status = update_identity(session, you);
   1.121 +
   1.122 +    message* msg_ptr = nullptr;
   1.123 +    message* dest_msg = nullptr;
   1.124 +    stringlist_t* keylist = nullptr;
   1.125 +    PEP_rating rating;
   1.126 +    PEP_decrypt_flags_t flags = 0;
   1.127 +
   1.128 +    message* final_ptr = nullptr;
   1.129 +    status = mime_decode_message(mailtext.c_str(), mailtext.length(), &msg_ptr);
   1.130 +    ASSERT_EQ(status , PEP_STATUS_OK);
   1.131 +    ASSERT_NE(msg_ptr, nullptr);
   1.132 +
   1.133 +    update_identity(session, msg_ptr->from);
   1.134 +    update_identity(session, msg_ptr->to->ident);
   1.135 +
   1.136 +    final_ptr = msg_ptr;
   1.137 +
   1.138 +    status = decrypt_message(session, msg_ptr, &dest_msg, &keylist, &rating, &flags);
   1.139 +    final_ptr = dest_msg ? dest_msg : msg_ptr;
   1.140 +
   1.141 +    cout << "shortmsg: " << final_ptr->shortmsg << endl << endl;
   1.142 +    cout << "longmsg: " << final_ptr->longmsg << endl << endl;
   1.143 +    cout << "longmsg_formatted: " << (final_ptr->longmsg_formatted ? final_ptr->longmsg_formatted : "(empty)") << endl << endl;
   1.144 +
   1.145 +    ASSERT_EQ(color_from_rating(rating) , PEP_color_green);
   1.146 +
   1.147 +    if (final_ptr == dest_msg)
   1.148 +    	free_message(dest_msg);
   1.149 +    free_message(msg_ptr);
   1.150 +    free_stringlist(keylist);
   1.151 +}
   1.152 +
   1.153 +TEST_F(AppleMailTest, check_apple_mail_html_signed_encrypted) {
   1.154 +
   1.155 +    // Start state copy
   1.156 +    // N.B. As part of breaking up formerly monolith tests into individual tests, I've copied state setup from the
   1.157 +    // original functions into many functions. It should, when there's time, either be refactored (if necessary for this
   1.158 +    // test) or removed (if not).
   1.159 +    const string keytextkey1 = slurp("test_keys/pub/pep-test-apple-0x1CCBC7D7_pub.asc");
   1.160 +    const string keytextkey2 = slurp("test_keys/priv/pep-test-recip-0x08DB0AEE_priv.asc");
   1.161 +    const string keytextkey3 = slurp("test_keys/pub/pep-test-recip-0x08DB0AEE_pub.asc");
   1.162 +
   1.163 +    PEP_STATUS statuskey1 = import_key(session, keytextkey1.c_str(), keytextkey1.length(), NULL);
   1.164 +    PEP_STATUS statuskey2 = import_key(session, keytextkey2.c_str(), keytextkey2.length(), NULL);
   1.165 +    PEP_STATUS statuskey3 = import_key(session, keytextkey3.c_str(), keytextkey3.length(), NULL);
   1.166 +
   1.167 +    pEp_identity * me = new_identity("pep.test.recip@kgrothoff.org", "93D19F24AD6F4C4BA9134AAF84D9217908DB0AEE", PEP_OWN_USERID, "pEp Test Recipient");
   1.168 +    me->me = true;
   1.169 +    PEP_STATUS status = set_own_key(session, me, "93D19F24AD6F4C4BA9134AAF84D9217908DB0AEE");
   1.170 +
   1.171 +    pEp_identity * you = new_identity("pep.test.apple@pep-project.org", NULL, "pep.test.apple@pep-project.org", "pEp Apple Test");
   1.172 +    you->me = false;
   1.173 +    status = update_identity(session, you);
   1.174 +
   1.175 +    trust_personal_key(session, you);
   1.176 +
   1.177 +    status = update_identity(session, you);
   1.178 +
   1.179 +    // End state copy
   1.180 +
   1.181 +    message* msg_ptr = nullptr;
   1.182 +    message* dest_msg = nullptr;
   1.183 +    message* final_ptr = nullptr;
   1.184 +    stringlist_t* keylist = nullptr;
   1.185 +    PEP_rating rating;
   1.186 +    PEP_decrypt_flags_t flags = 0;
   1.187 +
   1.188 +    const char* mailfile2 = "test_mails/apple_mail_TC_html_signed_encrypted.eml";
   1.189 +    const string mailtext2 = slurp(mailfile2);
   1.190 +
   1.191 +    status = mime_decode_message(mailtext2.c_str(), mailtext2.length(), &msg_ptr);
   1.192 +    ASSERT_EQ(status, PEP_STATUS_OK);
   1.193 +    ASSERT_NE(msg_ptr, nullptr);
   1.194 +    final_ptr = msg_ptr;
   1.195 +    status = decrypt_message(session, msg_ptr, &dest_msg, &keylist, &rating, &flags);
   1.196 +    final_ptr = dest_msg ? dest_msg : msg_ptr;
   1.197 +
   1.198 +    cout << "shortmsg: " << final_ptr->shortmsg << endl << endl;
   1.199 +    cout << "longmsg: " << final_ptr->longmsg << endl << endl;
   1.200 +    cout << "longmsg_formatted: " << (final_ptr->longmsg_formatted ? final_ptr->longmsg_formatted : "(empty)") << endl << endl;
   1.201 +
   1.202 +    ASSERT_EQ(color_from_rating(rating) , PEP_color_green);
   1.203 +
   1.204 +    if (final_ptr == dest_msg)
   1.205 +    	free_message(dest_msg);
   1.206 +    free_message(msg_ptr);
   1.207 +    free_stringlist(keylist);
   1.208 +
   1.209 +    msg_ptr = nullptr;
   1.210 +    dest_msg = nullptr;
   1.211 +    final_ptr = nullptr;
   1.212 +    keylist = nullptr;
   1.213 +    rating = PEP_rating_unreliable;
   1.214 +}
     2.1 --- a/test/src/engine_tests/AppleMailTests.cc	Mon Aug 26 10:49:51 2019 +0200
     2.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.3 @@ -1,155 +0,0 @@
     2.4 -// This file is under GNU General Public License 3.0
     2.5 -// see LICENSE.txt
     2.6 -
     2.7 -#include <cpptest.h>
     2.8 -#include <cpptest-suite.h>
     2.9 -#include <cpptest-textoutput.h>
    2.10 -#include <stdlib.h>
    2.11 -#include <string.h>
    2.12 -#include <iostream>
    2.13 -#include <fstream>
    2.14 -#include <string>
    2.15 -#include <cstring> // for strcmp()
    2.16 -#include "platform.h"
    2.17 -#include <iostream>
    2.18 -#include <fstream>
    2.19 -#include <sstream>
    2.20 -#include "keymanagement.h"
    2.21 -#include "message_api.h"
    2.22 -#include "mime.h"
    2.23 -#include "test_util.h" // for slurp()
    2.24 -#include "TestConstants.h"
    2.25 -
    2.26 -#include "EngineTestSuite.h"
    2.27 -#include "EngineTestIndividualSuite.h"
    2.28 -#include "AppleMailTests.h"
    2.29 -
    2.30 -using namespace std;
    2.31 -
    2.32 -AppleMailTests::AppleMailTests(string suitename, string test_home_dir) : 
    2.33 -    EngineTestIndividualSuite::EngineTestIndividualSuite(suitename, test_home_dir) {            
    2.34 -    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("AppleMailTests::check_apple_mail_text_signed_encrypted"),
    2.35 -                                                                      static_cast<Func>(&AppleMailTests::check_apple_mail_text_signed_encrypted)));
    2.36 -    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("AppleMailTests::check_apple_mail_html_signed_encrypted"),
    2.37 -                                                                      static_cast<Func>(&AppleMailTests::check_apple_mail_html_signed_encrypted)));
    2.38 -                                                                  
    2.39 -}
    2.40 -
    2.41 -void AppleMailTests::check_apple_mail_text_signed_encrypted() {
    2.42 -    
    2.43 -    const char* mailfile = "test_mails/apple_mail_TC_signed_encrypted.eml";
    2.44 -    
    2.45 -    const string keytextkey1 = slurp("test_keys/pub/pep-test-apple-0x1CCBC7D7_pub.asc");
    2.46 -    const string keytextkey2 = slurp("test_keys/priv/pep-test-recip-0x08DB0AEE_priv.asc");
    2.47 -    const string keytextkey3 = slurp("test_keys/pub/pep-test-recip-0x08DB0AEE_pub.asc");
    2.48 -
    2.49 -    PEP_STATUS statuskey1 = import_key(session, keytextkey1.c_str(), keytextkey1.length(), NULL);
    2.50 -    PEP_STATUS statuskey2 = import_key(session, keytextkey2.c_str(), keytextkey2.length(), NULL);
    2.51 -    PEP_STATUS statuskey3 = import_key(session, keytextkey3.c_str(), keytextkey3.length(), NULL);
    2.52 -        
    2.53 -    const string mailtext = slurp(mailfile);
    2.54 -    pEp_identity * me = new_identity("pep.test.recip@kgrothoff.org", "93D19F24AD6F4C4BA9134AAF84D9217908DB0AEE", PEP_OWN_USERID, "pEp Test Recipient");    
    2.55 -    me->me = true;    
    2.56 -    PEP_STATUS status = set_own_key(session, me, "93D19F24AD6F4C4BA9134AAF84D9217908DB0AEE");
    2.57 -    
    2.58 -    pEp_identity * you = new_identity("pep.test.apple@pep-project.org", NULL, "pep.test.apple@pep-project.org", "pEp Apple Test");    
    2.59 -    you->me = false;    
    2.60 -    status = update_identity(session, you);
    2.61 -
    2.62 -    trust_personal_key(session, you);
    2.63 -    
    2.64 -    status = update_identity(session, you);
    2.65 -    
    2.66 -    message* msg_ptr = nullptr;
    2.67 -    message* dest_msg = nullptr;
    2.68 -    stringlist_t* keylist = nullptr;
    2.69 -    PEP_rating rating;
    2.70 -    PEP_decrypt_flags_t flags = 0;
    2.71 -    
    2.72 -    message* final_ptr = nullptr;
    2.73 -    status = mime_decode_message(mailtext.c_str(), mailtext.length(), &msg_ptr);
    2.74 -    TEST_ASSERT_MSG((status == PEP_STATUS_OK), "status == PEP_STATUS_OK");
    2.75 -    TEST_ASSERT_MSG((msg_ptr), "msg_ptr");
    2.76 -    
    2.77 -    update_identity(session, msg_ptr->from);
    2.78 -    update_identity(session, msg_ptr->to->ident);
    2.79 -    
    2.80 -    final_ptr = msg_ptr;
    2.81 -    
    2.82 -    status = decrypt_message(session, msg_ptr, &dest_msg, &keylist, &rating, &flags);
    2.83 -    final_ptr = dest_msg ? dest_msg : msg_ptr;
    2.84 -  
    2.85 -    cout << "shortmsg: " << final_ptr->shortmsg << endl << endl;
    2.86 -    cout << "longmsg: " << final_ptr->longmsg << endl << endl;
    2.87 -    cout << "longmsg_formatted: " << (final_ptr->longmsg_formatted ? final_ptr->longmsg_formatted : "(empty)") << endl << endl;
    2.88 -
    2.89 -    TEST_ASSERT_MSG((color_from_rating(rating) == PEP_color_green), "color_from_rating(rating) == PEP_color_green");
    2.90 -
    2.91 -    if (final_ptr == dest_msg)
    2.92 -    	free_message(dest_msg);
    2.93 -    free_message(msg_ptr);
    2.94 -    free_stringlist(keylist);
    2.95 -}
    2.96 -
    2.97 -void AppleMailTests::check_apple_mail_html_signed_encrypted() {
    2.98 -
    2.99 -    // Start state copy
   2.100 -    // N.B. As part of breaking up formerly monolith tests into individual tests, I've copied state setup from the
   2.101 -    // original functions into many functions. It should, when there's time, either be refactored (if necessary for this 
   2.102 -    // test) or removed (if not).
   2.103 -    const string keytextkey1 = slurp("test_keys/pub/pep-test-apple-0x1CCBC7D7_pub.asc");
   2.104 -    const string keytextkey2 = slurp("test_keys/priv/pep-test-recip-0x08DB0AEE_priv.asc");
   2.105 -    const string keytextkey3 = slurp("test_keys/pub/pep-test-recip-0x08DB0AEE_pub.asc");
   2.106 -
   2.107 -    PEP_STATUS statuskey1 = import_key(session, keytextkey1.c_str(), keytextkey1.length(), NULL);
   2.108 -    PEP_STATUS statuskey2 = import_key(session, keytextkey2.c_str(), keytextkey2.length(), NULL);
   2.109 -    PEP_STATUS statuskey3 = import_key(session, keytextkey3.c_str(), keytextkey3.length(), NULL);
   2.110 -
   2.111 -    pEp_identity * me = new_identity("pep.test.recip@kgrothoff.org", "93D19F24AD6F4C4BA9134AAF84D9217908DB0AEE", PEP_OWN_USERID, "pEp Test Recipient");    
   2.112 -    me->me = true;    
   2.113 -    PEP_STATUS status = set_own_key(session, me, "93D19F24AD6F4C4BA9134AAF84D9217908DB0AEE");
   2.114 -    
   2.115 -    pEp_identity * you = new_identity("pep.test.apple@pep-project.org", NULL, "pep.test.apple@pep-project.org", "pEp Apple Test");    
   2.116 -    you->me = false;    
   2.117 -    status = update_identity(session, you);
   2.118 -
   2.119 -    trust_personal_key(session, you);
   2.120 -    
   2.121 -    status = update_identity(session, you);
   2.122 -
   2.123 -    // End state copy
   2.124 -    
   2.125 -    message* msg_ptr = nullptr;
   2.126 -    message* dest_msg = nullptr;
   2.127 -    message* final_ptr = nullptr;
   2.128 -    stringlist_t* keylist = nullptr;
   2.129 -    PEP_rating rating;
   2.130 -    PEP_decrypt_flags_t flags = 0;
   2.131 -     
   2.132 -    const char* mailfile2 = "test_mails/apple_mail_TC_html_signed_encrypted.eml";
   2.133 -    const string mailtext2 = slurp(mailfile2);
   2.134 -    
   2.135 -    status = mime_decode_message(mailtext2.c_str(), mailtext2.length(), &msg_ptr);
   2.136 -    TEST_ASSERT_MSG((status == PEP_STATUS_OK), "status == PEP_STATUS_OK");
   2.137 -    TEST_ASSERT_MSG((msg_ptr), "msg_ptr");
   2.138 -    final_ptr = msg_ptr;
   2.139 -    status = decrypt_message(session, msg_ptr, &dest_msg, &keylist, &rating, &flags);
   2.140 -    final_ptr = dest_msg ? dest_msg : msg_ptr;
   2.141 -  
   2.142 -    cout << "shortmsg: " << final_ptr->shortmsg << endl << endl;
   2.143 -    cout << "longmsg: " << final_ptr->longmsg << endl << endl;
   2.144 -    cout << "longmsg_formatted: " << (final_ptr->longmsg_formatted ? final_ptr->longmsg_formatted : "(empty)") << endl << endl;
   2.145 -
   2.146 -    TEST_ASSERT_MSG((color_from_rating(rating) == PEP_color_green), "color_from_rating(rating) == PEP_color_green");
   2.147 -
   2.148 -    if (final_ptr == dest_msg)
   2.149 -    	free_message(dest_msg);
   2.150 -    free_message(msg_ptr);
   2.151 -    free_stringlist(keylist);
   2.152 -
   2.153 -    msg_ptr = nullptr;
   2.154 -    dest_msg = nullptr;
   2.155 -    final_ptr = nullptr;
   2.156 -    keylist = nullptr;
   2.157 -    rating = PEP_rating_unreliable;        
   2.158 -}