test/trustwords_test.cc
author Krista Bennett <krista@pep-project.org>
Wed, 14 Jun 2017 18:52:55 +0200
branchENGINE-215
changeset 1856 99348d336639
parent 1747 061d8bf91506
child 1864 9ce429048fd7
permissions -rw-r--r--
ENGINE-215: needs to be tested, test cases impacted by the change need to be adjusted
     1 // This file is under GNU General Public License 3.0
     2 // see LICENSE.txt
     3 
     4 #include <iostream>
     5 #include <string>
     6 #include <cassert>
     7 #include "pEpEngine.h"
     8 #include "message_api.h"
     9 
    10 using namespace std;
    11 
    12 
    13 int main() {
    14     cout << "\n*** get_trustwords test ***\n\n";
    15 
    16     PEP_SESSION session = nullptr;
    17     
    18     cout << "calling init()\n";
    19     PEP_STATUS status1 = init(&session);
    20     assert(status1 == PEP_STATUS_OK);
    21     assert(session);
    22     cout << "init() completed.\n";
    23     
    24     pEp_identity* identity1  = new_identity(
    25         "leon.schumacher@digitalekho.com",
    26         "8BD08954C74D830EEFFB5DEB2682A17F7C87F73D",
    27         "23",
    28         "Leon Schumacher");
    29     
    30     pEp_identity* identity2 = new_identity(
    31         "krista@kgrothoff.org",
    32         "62D4932086185C15917B72D30571AFBCA5493553",
    33         "blargh",
    34         "Krista Grothoff");
    35     
    36     pEp_identity* identity2_with_spaces = new_identity(
    37         "krista@kgrothoff.org",
    38         " 62D4932086185C159 17B72D30571A FBCA    5493553   ",
    39         "blargh",
    40         "Krista Grothoff");
    41     
    42     string fingerprint1 = identity1->fpr;
    43     string fingerprint2 = identity2->fpr;
    44     char* words1 = nullptr;
    45     char* words2 = nullptr;
    46     char* full_wordlist = nullptr;
    47     size_t wsize1 = 0;
    48     size_t wsize2 = 0;
    49     size_t wsize_full = 0;
    50     
    51     cout << "\nTest 1: fpr1 > fpr2, short" << endl;
    52     
    53     cout << "\nfinding German trustwords for " << fingerprint1 << "...\n";
    54     trustwords(session, fingerprint1.c_str(), "de", &words1, &wsize1, 5);
    55     assert(words1);
    56     cout << words1 << "\n";
    57 
    58     cout << "\nfinding German trustwords for " << fingerprint2 << "...\n";
    59     trustwords(session, fingerprint2.c_str(), "de", &words2, &wsize2, 5);
    60     assert(words2);
    61     cout << words2 << "\n";
    62 
    63     cout << "\nfinding German trustwords for " << identity1->address << " and " << identity2->address << "...\n";
    64     get_trustwords(session, identity1, identity2, "de", &full_wordlist, &wsize_full, false);
    65     assert(full_wordlist);
    66     cout << full_wordlist << "\n";
    67 
    68     cout << "\nfinding Englis trustwords for " << identity1->address << " and " << identity2->address << "... with spaces\n";
    69     get_trustwords(session, identity1, identity2_with_spaces, "en", &full_wordlist, &wsize_full, false);
    70     assert(full_wordlist);
    71     cout << full_wordlist << "\n";
    72     
    73     
    74     pEp_free(words1);
    75     words1 = nullptr;
    76     pEp_free(words2);
    77     words2 = nullptr;
    78     pEp_free(full_wordlist);
    79     full_wordlist = nullptr;
    80 
    81     cout << "\nTest 2: fpr1 == fpr1, short" << endl;
    82     
    83     cout << "\nfinding French trustwords for " << fingerprint2 << "...\n";
    84     trustwords(session, fingerprint1.c_str(), "fr", &words1, &wsize1, 5);
    85     assert(words1);
    86     cout << words1 << "\n";
    87         
    88     cout << "\nfinding French trustwords for " << identity2->address << " and " << identity2->address << "...\n";
    89     get_trustwords(session, identity2, identity2, "fr", &full_wordlist, &wsize_full, false);
    90     assert(full_wordlist);
    91     cout << full_wordlist << "\n";
    92 
    93     cout << "\nfinding English trustwords for " << identity2->address << " and " << identity2->address << "... with spaces\n";
    94     get_trustwords(session, identity2, identity2_with_spaces, "en", &full_wordlist, &wsize_full, false);
    95     assert(full_wordlist);
    96     cout << full_wordlist << "\n";
    97 
    98     pEp_free(words1);
    99     words1 = nullptr;
   100     pEp_free(full_wordlist);
   101     full_wordlist = nullptr;
   102 
   103     cout << "\nTest 3: fpr1 < fpr2, long" << endl;
   104     
   105     cout << "\nfinding English trustwords for " << fingerprint2 << "...\n";
   106     trustwords(session, fingerprint2.c_str(), "en", &words1, &wsize1, 0);
   107     assert(words1);
   108     cout << words1 << "\n";
   109     
   110     cout << "\nfinding English trustwords for " << fingerprint1 << "...\n";
   111     trustwords(session, fingerprint1.c_str(), "en", &words2, &wsize2, 0);
   112     assert(words2);
   113     cout << words2 << "\n";
   114     
   115     cout << "\nfinding English trustwords for " << identity2->address << " and " << identity2->address << "...\n";
   116     get_trustwords(session, identity2, identity1, "en", &full_wordlist, &wsize_full, true);
   117     assert(full_wordlist);
   118     cout << full_wordlist << "\n";
   119     
   120     cout << "\nfinding English trustwords for " << identity2->address << " and " << identity2->address << "... with spaces\n";
   121     get_trustwords(session, identity2_with_spaces, identity1, "en", &full_wordlist, &wsize_full, false);
   122     assert(full_wordlist);
   123     cout << full_wordlist << "\n";
   124     
   125     pEp_free(words1);
   126     words1 = nullptr;
   127     pEp_free(words2);
   128     words2 = nullptr;
   129     pEp_free(full_wordlist);
   130     full_wordlist = nullptr;
   131     
   132     cout << "\nTest 4: fpr1 < fpr2, leading zeros (fpr1 has more), long" << endl;
   133     
   134     pEp_identity* identity3 = new_identity(
   135         "nobody@kgrothoff.org",
   136         "000F932086185C15917B72D30571AFBCA5493553",
   137         "blargh",
   138         "Krista Grothoff");
   139     
   140     pEp_identity* identity4 = new_identity(
   141         "nobody2@kgrothoff.org",
   142         "001F932086185C15917B72D30571AFBCA5493553",
   143         "blargh",
   144         "Krista Grothoff");
   145     
   146     pEp_identity* identity5 = new_identity(
   147         "nobody3@kgrothoff.org",
   148         "001F732086185C15917B72D30571AFBCA5493553",
   149         "blargh",
   150         "Krista Grothoff");
   151 
   152     string fingerprint3 = identity3->fpr;
   153     string fingerprint4 = identity4->fpr;
   154     string fingerprint5 = identity5->fpr; 
   155         
   156     cout << "\nfinding Catalan trustwords for " << fingerprint3 << "...\n";
   157     trustwords(session, fingerprint3.c_str(), "ca", &words1, &wsize1, 0);
   158     assert(words1);
   159     cout << words1 << "\n";
   160     
   161     cout << "\nfinding Catalan trustwords for " << fingerprint4 << "...\n";
   162     trustwords(session, fingerprint4.c_str(), "ca", &words2, &wsize2, 0);
   163     assert(words2);
   164     cout << words2 << "\n";
   165     
   166     cout << "\nfinding Catalan trustwords for " << identity3->address << " and " << identity4->address << "...\n";
   167     get_trustwords(session, identity3, identity4, "ca", &full_wordlist, &wsize_full, true);
   168     assert(full_wordlist);
   169     cout << full_wordlist << "\n";
   170 
   171     pEp_free(words1);
   172     words1 = nullptr;
   173     pEp_free(words2);
   174     words2 = nullptr;
   175     pEp_free(full_wordlist);
   176     full_wordlist = nullptr;
   177 
   178     cout << "\nTest 5: fpr1 > fpr2, leading zeros (same number), interior digit difference, short" << endl;
   179     
   180     cout << "\nfinding Turkish trustwords for " << fingerprint4 << "...\n";
   181     trustwords(session, fingerprint4.c_str(), "tr", &words1, &wsize1, 5);
   182     assert(words1);
   183     cout << words1 << "\n";
   184     
   185     cout << "\nfinding Turkish trustwords for " << fingerprint5 << "...\n";
   186     trustwords(session, fingerprint5.c_str(), "tr", &words2, &wsize2, 5);
   187     assert(words2);
   188     cout << words2 << "\n";
   189     
   190     cout << "\nfinding Turkish trustwords for " << identity4->address << " and " << identity5->address << "...\n";
   191     get_trustwords(session, identity4, identity5, "tr", &full_wordlist, &wsize_full, false);
   192     assert(full_wordlist);
   193     cout << full_wordlist << "\n";
   194     
   195     pEp_free(words1);
   196     words1 = nullptr;
   197     pEp_free(words2);
   198     words2 = nullptr;
   199     pEp_free(full_wordlist);
   200     full_wordlist = nullptr;
   201 
   202     cout << "\nTest 6: fpr2 is too short" << endl;
   203     
   204     pEp_identity* identity6 = new_identity(
   205         "nobody4@kgrothoff.org",
   206         "01F932086185C15917B72D30571AFBCA5493553",
   207         "blargh",
   208         "Krista Grothoff");
   209     
   210     cout << "\nfinding Turkish trustwords for " << identity5->address << " and " << identity6->address << "...\n";
   211     PEP_STATUS status6 = get_trustwords(session, identity5, identity6, "tr", &full_wordlist, &wsize_full, false);
   212     assert(status6 == PEP_TRUSTWORDS_FPR_WRONG_LENGTH);
   213     cout << "Bad fpr length correctly recognised." << "\n";
   214     
   215     pEp_identity* identity7 = new_identity(
   216         "nobody5@kgrothoff.org",
   217         "F01X932086185C15917B72D30571AFBCA5493553",
   218         "blargh",
   219         "Krista Grothoff");
   220 
   221     cout << "\nTest 7: fpr2 has a non-hex character" << endl;
   222     
   223     cout << "\nfinding Turkish trustwords for " << identity1->address << " and " << identity7->address << "...\n";
   224     PEP_STATUS status7 = get_trustwords(session, identity1, identity7, "tr", &full_wordlist, &wsize_full, true);
   225     assert(status7 == PEP_ILLEGAL_VALUE);
   226     cout << "Illegal digit value correctly recognised." << "\n";
   227     
   228     
   229     free_identity(identity1);
   230     free_identity(identity2);
   231     free_identity(identity3);
   232     free_identity(identity4);
   233     free_identity(identity5);
   234     free_identity(identity6);
   235     free_identity(identity7);
   236     
   237     cout << "calling release()\n";
   238     release(session);
   239     return 0;
   240 }
   241