test/trustwords_test.cc
author Volker Birk <vb@pep.foundation>
Tue, 27 Dec 2016 21:13:41 +0100
changeset 1513 e7f7e42385b5
parent 1496 eacf1934b6cc
child 1746 498b0671ff4e
permissions -rw-r--r--
adding license info to each file
     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     string fingerprint1 = identity1->fpr;
    37     string fingerprint2 = identity2->fpr;
    38     char* words1 = nullptr;
    39     char* words2 = nullptr;
    40     char* full_wordlist = nullptr;
    41     size_t wsize1 = 0;
    42     size_t wsize2 = 0;
    43     size_t wsize_full = 0;
    44     
    45     cout << "\nTest 1: fpr1 > fpr2, short" << endl;
    46     
    47     cout << "\nfinding German trustwords for " << fingerprint1 << "...\n";
    48     trustwords(session, fingerprint1.c_str(), "de", &words1, &wsize1, 5);
    49     assert(words1);
    50     cout << words1 << "\n";
    51 
    52     cout << "\nfinding German trustwords for " << fingerprint2 << "...\n";
    53     trustwords(session, fingerprint2.c_str(), "de", &words2, &wsize2, 5);
    54     assert(words2);
    55     cout << words2 << "\n";
    56 
    57     cout << "\nfinding German trustwords for " << identity1->address << " and " << identity2->address << "...\n";
    58     get_trustwords(session, identity1, identity2, "de", &full_wordlist, &wsize_full, false);
    59     assert(full_wordlist);
    60     cout << full_wordlist << "\n";
    61     
    62     
    63     pEp_free(words1);
    64     words1 = nullptr;
    65     pEp_free(words2);
    66     words2 = nullptr;
    67     pEp_free(full_wordlist);
    68     full_wordlist = nullptr;
    69 
    70     cout << "\nTest 2: fpr1 == fpr1, short" << endl;
    71     
    72     cout << "\nfinding French trustwords for " << fingerprint2 << "...\n";
    73     trustwords(session, fingerprint1.c_str(), "fr", &words1, &wsize1, 5);
    74     assert(words1);
    75     cout << words1 << "\n";
    76         
    77     cout << "\nfinding French trustwords for " << identity2->address << " and " << identity2->address << "...\n";
    78     get_trustwords(session, identity2, identity2, "fr", &full_wordlist, &wsize_full, false);
    79     assert(full_wordlist);
    80     cout << full_wordlist << "\n";
    81 
    82     pEp_free(words1);
    83     words1 = nullptr;
    84     pEp_free(full_wordlist);
    85     full_wordlist = nullptr;
    86 
    87     cout << "\nTest 3: fpr1 < fpr2, long" << endl;
    88     
    89     cout << "\nfinding English trustwords for " << fingerprint2 << "...\n";
    90     trustwords(session, fingerprint2.c_str(), "en", &words1, &wsize1, 0);
    91     assert(words1);
    92     cout << words1 << "\n";
    93     
    94     cout << "\nfinding English trustwords for " << fingerprint1 << "...\n";
    95     trustwords(session, fingerprint1.c_str(), "en", &words2, &wsize2, 0);
    96     assert(words2);
    97     cout << words2 << "\n";
    98     
    99     cout << "\nfinding English trustwords for " << identity2->address << " and " << identity2->address << "...\n";
   100     get_trustwords(session, identity2, identity1, "en", &full_wordlist, &wsize_full, true);
   101     assert(full_wordlist);
   102     cout << full_wordlist << "\n";
   103     
   104     pEp_free(words1);
   105     words1 = nullptr;
   106     pEp_free(words2);
   107     words2 = nullptr;
   108     pEp_free(full_wordlist);
   109     full_wordlist = nullptr;
   110     
   111     cout << "\nTest 4: fpr1 < fpr2, leading zeros (fpr1 has more), long" << endl;
   112     
   113     pEp_identity* identity3 = new_identity(
   114         "nobody@kgrothoff.org",
   115         "000F932086185C15917B72D30571AFBCA5493553",
   116         "blargh",
   117         "Krista Grothoff");
   118     
   119     pEp_identity* identity4 = new_identity(
   120         "nobody2@kgrothoff.org",
   121         "001F932086185C15917B72D30571AFBCA5493553",
   122         "blargh",
   123         "Krista Grothoff");
   124     
   125     pEp_identity* identity5 = new_identity(
   126         "nobody3@kgrothoff.org",
   127         "001F732086185C15917B72D30571AFBCA5493553",
   128         "blargh",
   129         "Krista Grothoff");
   130 
   131     string fingerprint3 = identity3->fpr;
   132     string fingerprint4 = identity4->fpr;
   133     string fingerprint5 = identity5->fpr; 
   134         
   135     cout << "\nfinding Catalan trustwords for " << fingerprint3 << "...\n";
   136     trustwords(session, fingerprint3.c_str(), "ca", &words1, &wsize1, 0);
   137     assert(words1);
   138     cout << words1 << "\n";
   139     
   140     cout << "\nfinding Catalan trustwords for " << fingerprint4 << "...\n";
   141     trustwords(session, fingerprint4.c_str(), "ca", &words2, &wsize2, 0);
   142     assert(words2);
   143     cout << words2 << "\n";
   144     
   145     cout << "\nfinding Catalan trustwords for " << identity3->address << " and " << identity4->address << "...\n";
   146     get_trustwords(session, identity3, identity4, "ca", &full_wordlist, &wsize_full, true);
   147     assert(full_wordlist);
   148     cout << full_wordlist << "\n";
   149 
   150     pEp_free(words1);
   151     words1 = nullptr;
   152     pEp_free(words2);
   153     words2 = nullptr;
   154     pEp_free(full_wordlist);
   155     full_wordlist = nullptr;
   156 
   157     cout << "\nTest 5: fpr1 > fpr2, leading zeros (same number), interior digit difference, short" << endl;
   158     
   159     cout << "\nfinding Turkish trustwords for " << fingerprint4 << "...\n";
   160     trustwords(session, fingerprint4.c_str(), "tr", &words1, &wsize1, 5);
   161     assert(words1);
   162     cout << words1 << "\n";
   163     
   164     cout << "\nfinding Turkish trustwords for " << fingerprint5 << "...\n";
   165     trustwords(session, fingerprint5.c_str(), "tr", &words2, &wsize2, 5);
   166     assert(words2);
   167     cout << words2 << "\n";
   168     
   169     cout << "\nfinding Turkish trustwords for " << identity4->address << " and " << identity5->address << "...\n";
   170     get_trustwords(session, identity4, identity5, "tr", &full_wordlist, &wsize_full, false);
   171     assert(full_wordlist);
   172     cout << full_wordlist << "\n";
   173     
   174     pEp_free(words1);
   175     words1 = nullptr;
   176     pEp_free(words2);
   177     words2 = nullptr;
   178     pEp_free(full_wordlist);
   179     full_wordlist = nullptr;
   180 
   181     cout << "\nTest 6: fpr2 is too short" << endl;
   182     
   183     pEp_identity* identity6 = new_identity(
   184         "nobody4@kgrothoff.org",
   185         "01F932086185C15917B72D30571AFBCA5493553",
   186         "blargh",
   187         "Krista Grothoff");
   188     
   189     cout << "\nfinding Turkish trustwords for " << identity5->address << " and " << identity6->address << "...\n";
   190     PEP_STATUS status6 = get_trustwords(session, identity5, identity6, "tr", &full_wordlist, &wsize_full, false);
   191     assert(status6 == PEP_TRUSTWORDS_FPR_WRONG_LENGTH);
   192     cout << "Bad fpr length correctly recognised." << "\n";
   193     
   194     pEp_identity* identity7 = new_identity(
   195         "nobody5@kgrothoff.org",
   196         "F01X932086185C15917B72D30571AFBCA5493553",
   197         "blargh",
   198         "Krista Grothoff");
   199 
   200     cout << "\nTest 7: fpr2 has a non-hex character" << endl;
   201     
   202     cout << "\nfinding Turkish trustwords for " << identity1->address << " and " << identity7->address << "...\n";
   203     PEP_STATUS status7 = get_trustwords(session, identity1, identity7, "tr", &full_wordlist, &wsize_full, true);
   204     assert(status7 == PEP_ILLEGAL_VALUE);
   205     cout << "Illegal digit value correctly recognised." << "\n";
   206     
   207     
   208     free_identity(identity1);
   209     free_identity(identity2);
   210     free_identity(identity3);
   211     free_identity(identity4);
   212     free_identity(identity5);
   213     free_identity(identity6);
   214     free_identity(identity7);
   215     
   216     cout << "calling release()\n";
   217     release(session);
   218     return 0;
   219 }
   220