One step closer. Converted first test (not really converted, just shoved into framework) local_cpptest
authorKrista Bennett <krista@pep-project.org>
Thu, 26 Apr 2018 17:30:42 +0200
branchlocal_cpptest
changeset 2634b754efa45304
parent 2633 d7f52765febf
child 2635 04e6df2957e8
One step closer. Converted first test (not really converted, just shoved into framework)
test/DecorateTests.cc
test/DecorateTests.h
test/EngineTestIndividualSuite.h
test/EngineTestSuite.cc
test/SuiteMaker.cc
test/SuiteMaker.h
test/TestDriver.cc
test/TestDriver.h
test/decorate_test.cc
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/DecorateTests.cc	Thu Apr 26 17:30:42 2018 +0200
     1.3 @@ -0,0 +1,78 @@
     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 "platform.h"
    1.10 +#include <iostream>
    1.11 +#include <fstream>
    1.12 +#include <TEST_ASSERT.h>
    1.13 +#include <sstream>
    1.14 +#include "mime.h"
    1.15 +#include "message_api.h"
    1.16 +#include "test_util.h"
    1.17 +
    1.18 +using namespace std;
    1.19 +
    1.20 +DecorateTests::DecorateTests(string suitename, string test_home_dir) : 
    1.21 +    EngineTestSessionSuite::EngineTestSessionSuite(suitename, test_home_dir) {            
    1.22 +    TEST_ADD(DecorateTests::check_decorate);
    1.23 +}
    1.24 +
    1.25 +void DecorateTests::check_decorate() {
    1.26 +
    1.27 +    const string alice_pub_key = slurp("test_keys/pub/pep-test-alice-0x6FF00E97_pub.asc");
    1.28 +    const string alice_priv_key = slurp("test_keys/priv/pep-test-alice-0x6FF00E97_priv.asc");
    1.29 +    const string bob_pub_key = slurp("test_keys/pub/pep-test-bob-0xC9C2EE39_pub.asc");
    1.30 +    PEP_STATUS statuspub = import_key(session, alice_pub_key.c_str(), alice_pub_key.length(), NULL);
    1.31 +    PEP_STATUS statuspriv = import_key(session, alice_priv_key.c_str(), alice_priv_key.length(), NULL);
    1.32 +    PEP_STATUS statusbob = import_key(session, bob_pub_key.c_str(), bob_pub_key.length(), NULL);
    1.33 +    TEST_ASSERT(statuspub == PEP_STATUS_OK);
    1.34 +    TEST_ASSERT(statuspriv == PEP_STATUS_OK);
    1.35 +    TEST_ASSERT(statusbob == PEP_STATUS_OK);
    1.36 +
    1.37 +    cout << "creating message…\n";
    1.38 +    pEp_identity* alice = new_identity("pep.test.alice@pep-project.org", NULL, PEP_OWN_USERID, "Alice Test");
    1.39 +    pEp_identity* bob = new_identity("pep.test.bob@pep-project.org", NULL, "42", "Bob Test");
    1.40 +    alice->me = true;
    1.41 +    identity_list* to_list = new_identity_list(bob); // to bob
    1.42 +    message* outgoing_message = new_message(PEP_dir_outgoing);
    1.43 +    TEST_ASSERT(outgoing_message);
    1.44 +    outgoing_message->from = alice;
    1.45 +    outgoing_message->to = to_list;
    1.46 +    outgoing_message->shortmsg = strdup("Greetings, humans!");
    1.47 +    outgoing_message->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
    1.48 +    outgoing_message->longmsg = strdup("This is a dumb message.\nBut it's done.\n");
    1.49 +    TEST_ASSERT(outgoing_message->longmsg);
    1.50 +    cout << "message created.\n";
    1.51 +
    1.52 +    char* encoded_text = nullptr;
    1.53 +
    1.54 +    message* encrypted_msg = nullptr;
    1.55 +    cout << "calling encrypt_message\n";
    1.56 +    PEP_STATUS status = encrypt_message (session, outgoing_message, NULL, &encrypted_msg, PEP_enc_PGP_MIME, 0);
    1.57 +    cout << "encrypt_message() returns " << tl_status_string(status) << '.' << endl;
    1.58 +    TEST_ASSERT(status == PEP_STATUS_OK);
    1.59 +    TEST_ASSERT(encrypted_msg);
    1.60 +    cout << "message encrypted.\n";
    1.61 +    
    1.62 +    status = mime_encode_message(encrypted_msg, false, &encoded_text);
    1.63 +    TEST_ASSERT(status == PEP_STATUS_OK);
    1.64 +    TEST_ASSERT(encoded_text);
    1.65 +    
    1.66 +    bool contains_version = false;
    1.67 +    
    1.68 +    const char* version_str = "X-pEp-Version: ";
    1.69 +    size_t version_prefix_len = strlen(version_str);
    1.70 +    
    1.71 +    istringstream f(encoded_text);
    1.72 +    string enc_string;
    1.73 +    while (getline(f, enc_string)) {
    1.74 +        if (strncmp(enc_string.c_str(), version_str, version_prefix_len) == 0)
    1.75 +            contains_version = true;
    1.76 +    }
    1.77 +    TEST_ASSERT(contains_version);
    1.78 +    
    1.79 +    if (contains_version)
    1.80 +        cout << "Version string in encrypted message, as it should be." << endl;    
    1.81 +}
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/test/DecorateTests.h	Thu Apr 26 17:30:42 2018 +0200
     2.3 @@ -0,0 +1,21 @@
     2.4 +// This file is under GNU General Public License 3.0
     2.5 +// see LICENSE.txt
     2.6 +
     2.7 +#include <stdlib.h>
     2.8 +#include <string.h>
     2.9 +#include "platform.h"
    2.10 +#include <iostream>
    2.11 +#include <fstream>
    2.12 +#include <sstream>
    2.13 +#include "mime.h"
    2.14 +#include "message_api.h"
    2.15 +#include "test_util.h"
    2.16 +
    2.17 +using namespace std;
    2.18 +
    2.19 +class DecorateTests : public EngineTestSessionSuite {
    2.20 +    public:
    2.21 +        DecorateTests(string suitename, string test_home_dir);
    2.22 +    private:
    2.23 +        void check_decorate();
    2.24 +};
     3.1 --- a/test/EngineTestIndividualSuite.h	Thu Apr 26 12:01:33 2018 +0200
     3.2 +++ b/test/EngineTestIndividualSuite.h	Thu Apr 26 17:30:42 2018 +0200
     3.3 @@ -11,5 +11,8 @@
     3.4      public:
     3.5          EngineIndividualTestSuite(string suitename, string test_home_dir);
     3.6          ~EngineIndividualTestSuite();
     3.7 +    protected:
     3.8 +        virtual void setup();
     3.9 +        virtual void tear_down();
    3.10  };
    3.11  #endif
     4.1 --- a/test/EngineTestSuite.cc	Thu Apr 26 12:01:33 2018 +0200
     4.2 +++ b/test/EngineTestSuite.cc	Thu Apr 26 17:30:42 2018 +0200
     4.3 @@ -89,3 +89,6 @@
     4.4      
     4.5      return retval;
     4.6  }
     4.7 +
     4.8 +void EngineTestSuite::setup() {}
     4.9 +void EngineTestSuite::tear_down() {}
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/test/SuiteMaker.cc	Thu Apr 26 17:30:42 2018 +0200
     5.3 @@ -0,0 +1,16 @@
     5.4 +#include <cpptest-suite.h>
     5.5 +#include <string>
     5.6 +#include <sys/stat.h>
     5.7 +#include <errno.h>
     5.8 +#include "EngineTestSuite.h"
     5.9 +#include "EngineTestIndividualSuite.h"
    5.10 +#include "EngineTestSessionSuite.h"
    5.11 +
    5.12 +// Begin where we should generate stuff
    5.13 +#include "DecorateTests.h"
    5.14 +
    5.15 +static EngineTestSuite* build(const char* test_class_name, const char* test_home) {
    5.16 +    if (strcmp(test_class_name, "DecorateTests") == 0)
    5.17 +        return new DecorateTests(test_class_name, test_home);
    5.18 +    return NULL;
    5.19 +}
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/test/SuiteMaker.h	Thu Apr 26 17:30:42 2018 +0200
     6.3 @@ -0,0 +1,16 @@
     6.4 +#include <cpptest-suite.h>
     6.5 +#include <string>
     6.6 +#include <sys/stat.h>
     6.7 +#include <errno.h>
     6.8 +#include "EngineTestSuite.h"
     6.9 +#include "EngineTestIndividualSuite.h"
    6.10 +#include "EngineTestSessionSuite.h"
    6.11 +
    6.12 +// Begin where we should generate stuff
    6.13 +#include "DecorateTests.h"
    6.14 +
    6.15 +class SuiteMaker {
    6.16 +    public:
    6.17 +        static EngineTestSuite* build(const char* test_class_name, 
    6.18 +                                      const char* test_home);
    6.19 +};
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/test/TestDriver.cc	Thu Apr 26 17:30:42 2018 +0200
     7.3 @@ -0,0 +1,71 @@
     7.4 +#include <cpptest-suite.h>
     7.5 +#include <string>
     7.6 +#include <sys/stat.h>
     7.7 +#include <errno.h>
     7.8 +#include "EngineTestSuite.h"
     7.9 +#include "EngineTestIndividualSuite.h"
    7.10 +#include "EngineTestSessionSuite.h"
    7.11 +
    7.12 +using namespace std;
    7.13 +
    7.14 +string common_test_home = "~/pEp_tests";
    7.15 +
    7.16 +void usage() {
    7.17 +    throw "Bad usage. Fix me, you loser developer.";
    7.18 +}
    7.19 +
    7.20 +EngineTestSuite* 
    7.21 +
    7.22 +
    7.23 +
    7.24 +int main(int argc, char** argv) {
    7.25 +    const int MIN_ARGC = 1;
    7.26 +    if (argc < MIN_ARGC)
    7.27 +        usage();
    7.28 +
    7.29 +    int start_index = 1;
    7.30 +    
    7.31 +
    7.32 +    if (argc > 1) {
    7.33 +        string tmpstr = argv[1];
    7.34 +        if (tmpstr.compare(0,10,"--testdir=")) {
    7.35 +            try {
    7.36 +                tmpstr = tmpstr.substr(10);
    7.37 +            } 
    7.38 +            catch (std::out_of_range o) {
    7.39 +                usage();
    7.40 +            }
    7.41 +            common_test_home = tmpstr;
    7.42 +            start_index++;
    7.43 +        }
    7.44 +    }
    7.45 +
    7.46 +    struct stat dirchk;
    7.47 +    if (stat(common_test_home.c_str(), &dirchk) == 0) {
    7.48 +        if (!S_ISDIR(dirchk.st_mode))
    7.49 +            throw ("The test directory, " + common_test_home + "exists, but is not a directory.").c_str(); 
    7.50 +    }
    7.51 +    else if (common_test_home.compare("~/pEp_tests")) {
    7.52 +        int errchk = mkdir(common_test_home);
    7.53 +        if (errchk != 0)
    7.54 +            throw "Error creating a test directory.";
    7.55 +    }
    7.56 +    else
    7.57 +        throw "Test directory does not exist. Test directories from the command line must be created first. Because we're lazy.";
    7.58 +        
    7.59 +            
    7.60 +    EngineTestSuite* test_runner = new EngineTestSuite("MainTestDriver", test_home);
    7.61 +        
    7.62 +    for (int i = start_index; i < argc; i++) {
    7.63 +        char* curr_arg = argv[i];
    7.64 +        EngineTestSuite* test_suite = SuiteMaker.build(argv[i], common_test_home);
    7.65 +        if (!test_suite)
    7.66 +            throw "Could not create a test suite instance."; // FIXME, better error, cleanup, obviously
    7.67 +        test_runner.add(test_suite);
    7.68 +        test_suite = NULL;
    7.69 +    }
    7.70 +
    7.71 +    Test::TextOutput output(Test::TextOutput::Terse);
    7.72 +    return test_runner.run(output) ? 1 : 0;
    7.73 +    
    7.74 +}
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/test/TestDriver.h	Thu Apr 26 17:30:42 2018 +0200
     8.3 @@ -0,0 +1,11 @@
     8.4 +#include <cpptest-suite.h>
     8.5 +#include <string>
     8.6 +#include <map>
     8.7 +#include "EngineTestSuite.h"
     8.8 +#include "EngineTestIndividualSuite.h"
     8.9 +#include "EngineTestSessionSuite.h"
    8.10 +
    8.11 +using namespace std;
    8.12 +
    8.13 +typedef map<string, EngineTestSuite> SuiteMap;
    8.14 +typedef set<string> NameSet;
     9.1 --- a/test/decorate_test.cc	Thu Apr 26 12:01:33 2018 +0200
     9.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.3 @@ -1,91 +0,0 @@
     9.4 -// This file is under GNU General Public License 3.0
     9.5 -// see LICENSE.txt
     9.6 -
     9.7 -#include <stdlib.h>
     9.8 -#include <string.h>
     9.9 -#include "platform.h"
    9.10 -#include <iostream>
    9.11 -#include <fstream>
    9.12 -#include <assert.h>
    9.13 -#include <sstream>
    9.14 -#include "mime.h"
    9.15 -#include "message_api.h"
    9.16 -#include "test_util.h"
    9.17 -
    9.18 -using namespace std;
    9.19 -
    9.20 -int main() {
    9.21 -    cout << "\n*** check that X-pEp-Version is added to decorated text ***\n\n";
    9.22 -
    9.23 -    PEP_SESSION session;
    9.24 -    
    9.25 -    cout << "calling init()\n";
    9.26 -    PEP_STATUS status1 = init(&session);
    9.27 -    assert(status1 == PEP_STATUS_OK);
    9.28 -    assert(session);
    9.29 -    cout << "init() completed.\n";
    9.30 -
    9.31 -    const string alice_pub_key = slurp("test_keys/pub/pep-test-alice-0x6FF00E97_pub.asc");
    9.32 -    const string alice_priv_key = slurp("test_keys/priv/pep-test-alice-0x6FF00E97_priv.asc");
    9.33 -    const string bob_pub_key = slurp("test_keys/pub/pep-test-bob-0xC9C2EE39_pub.asc");
    9.34 -    PEP_STATUS statuspub = import_key(session, alice_pub_key.c_str(), alice_pub_key.length(), NULL);
    9.35 -    PEP_STATUS statuspriv = import_key(session, alice_priv_key.c_str(), alice_priv_key.length(), NULL);
    9.36 -    PEP_STATUS statusbob = import_key(session, bob_pub_key.c_str(), bob_pub_key.length(), NULL);
    9.37 -    assert(statuspub == PEP_STATUS_OK);
    9.38 -    assert(statuspriv == PEP_STATUS_OK);
    9.39 -    assert(statusbob == PEP_STATUS_OK);
    9.40 -
    9.41 -    cout << "creating message…\n";
    9.42 -    pEp_identity* alice = new_identity("pep.test.alice@pep-project.org", NULL, PEP_OWN_USERID, "Alice Test");
    9.43 -    pEp_identity* bob = new_identity("pep.test.bob@pep-project.org", NULL, "42", "Bob Test");
    9.44 -    alice->me = true;
    9.45 -    identity_list* to_list = new_identity_list(bob); // to bob
    9.46 -    message* outgoing_message = new_message(PEP_dir_outgoing);
    9.47 -    assert(outgoing_message);
    9.48 -    outgoing_message->from = alice;
    9.49 -    outgoing_message->to = to_list;
    9.50 -    outgoing_message->shortmsg = strdup("Greetings, humans!");
    9.51 -    outgoing_message->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
    9.52 -    outgoing_message->longmsg = strdup("This is a dumb message.\nBut it's done.\n");
    9.53 -    assert(outgoing_message->longmsg);
    9.54 -    cout << "message created.\n";
    9.55 -
    9.56 -    char* encoded_text = nullptr;
    9.57 -
    9.58 -    message* encrypted_msg = nullptr;
    9.59 -    cout << "calling encrypt_message\n";
    9.60 -    PEP_STATUS status = encrypt_message (session, outgoing_message, NULL, &encrypted_msg, PEP_enc_PGP_MIME, 0);
    9.61 -    cout << "encrypt_message() returns " << tl_status_string(status) << '.' << endl;
    9.62 -    assert(status == PEP_STATUS_OK);
    9.63 -    assert(encrypted_msg);
    9.64 -    cout << "message encrypted.\n";
    9.65 -    
    9.66 -    status = mime_encode_message(encrypted_msg, false, &encoded_text);
    9.67 -    assert(status == PEP_STATUS_OK);
    9.68 -    assert(encoded_text);
    9.69 -    
    9.70 -    bool contains_version = false;
    9.71 -    
    9.72 -    const char* version_str = "X-pEp-Version: ";
    9.73 -    size_t version_prefix_len = strlen(version_str);
    9.74 -    
    9.75 -    istringstream f(encoded_text);
    9.76 -    string enc_string;
    9.77 -    while (getline(f, enc_string)) {
    9.78 -        if (strncmp(enc_string.c_str(), version_str, version_prefix_len) == 0)
    9.79 -            contains_version = true;
    9.80 -    }
    9.81 -    assert(contains_version);
    9.82 -    
    9.83 -    if (contains_version)
    9.84 -        cout << "Version string in encrypted message, as it should be." << endl;
    9.85 -    
    9.86 -    cout << "freeing messages…\n";
    9.87 -    free_message(encrypted_msg);
    9.88 -    free_message(outgoing_message);
    9.89 -    cout << "done.\n";
    9.90 -
    9.91 -    cout << "calling release()\n";
    9.92 -    release(session);
    9.93 -    return 0;
    9.94 -}