Added test for identity_lists
authorKrista Grothoff <krista@pep-project.org>
Sun, 17 Jul 2016 10:00:48 +0200
changeset 89006daf5bf9a1d
parent 889 31234c7c92c1
child 891 e4c80fda31af
Added test for identity_lists
test/identity_list_test.cc
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/identity_list_test.cc	Sun Jul 17 10:00:48 2016 +0200
     1.3 @@ -0,0 +1,172 @@
     1.4 +#include <stdlib.h>
     1.5 +#include <string.h>
     1.6 +#include "platform.h"
     1.7 +#include <iostream>
     1.8 +#include <fstream>
     1.9 +#include <assert.h>
    1.10 +
    1.11 +#include "identity_list.h"
    1.12 +
    1.13 +using namespace std;
    1.14 +
    1.15 +/*
    1.16 + *     char *address;              // C string with address UTF-8 encoded
    1.17 +    char *fpr;                  // C string with fingerprint UTF-8 encoded
    1.18 +    char *user_id;              // C string with user ID UTF-8 encoded
    1.19 +    char *username;             // C string with user name UTF-8 encoded
    1.20 +    PEP_comm_type comm_type;    // type of communication with this ID
    1.21 +    char lang[3];               // language of conversation
    1.22 +                                // ISO 639-1 ALPHA-2, last byte is 0
    1.23 +    bool me;                    // if this is the local user herself/himself
    1.24 +    */
    1.25 +
    1.26 +int test_identity_equals(pEp_identity* val1, pEp_identity* val2) {
    1.27 +    assert(val1);
    1.28 +    assert(val2);
    1.29 +    assert(val1->address);
    1.30 +    assert(val2->address);
    1.31 +    assert(val1->fpr);
    1.32 +    assert(val2->fpr);
    1.33 +    assert(val1->username);
    1.34 +    assert(val2->username);
    1.35 +    return((strcmp(val1->address, val2->address) == 0) && (strcmp(val1->fpr, val2->fpr) == 0)
    1.36 +        && (strcmp(val1->username, val2->username) == 0) && (val1->comm_type == val2->comm_type)
    1.37 +        && (val1->lang[0] == val2->lang[0]) && (val1->lang[1] == val2->lang[1])
    1.38 +        && (val1->lang[2] == val2->lang[2]) && (val1->me == val2->me));
    1.39 +}
    1.40 +
    1.41 +int main() {
    1.42 +    cout << "\n*** data structures: identity_list_test ***\n\n";
    1.43 +
    1.44 +    pEp_identity* id1 = new_identity(
    1.45 +        "leon.schumacher@digitalekho.com",
    1.46 +        "8BD08954C74D830EEFFB5DEB2682A17F7C87F73D",
    1.47 +        "23",
    1.48 +        "Leon Schumacher"
    1.49 +    );
    1.50 +    id1->comm_type = PEP_ct_pEp;
    1.51 +    
    1.52 +    pEp_identity* id2 = new_identity(
    1.53 +        "krista@kgrothoff.org",
    1.54 +        "62D4932086185C15917B72D30571AFBCA5493553",
    1.55 +        "42",
    1.56 +        "Krista Bennett Grothoff"
    1.57 +    );
    1.58 +    
    1.59 +    id2->comm_type = PEP_ct_OpenPGP;
    1.60 +
    1.61 +    pEp_identity* id3 = new_identity(
    1.62 +        "krista@pep-project.org",
    1.63 +        "51BF42D25BB5B154D71BF6CD3CF25B776D149247",
    1.64 +        "10",
    1.65 +        "Krista Grothoff"
    1.66 +    );
    1.67 +    
    1.68 +    id3->comm_type = PEP_ct_OTR;
    1.69 +
    1.70 +    pEp_identity* id4 = new_identity(
    1.71 +        "papa@smurf.lu",
    1.72 +        "00001111222233334444555566667777DEADBEEF",
    1.73 +        "667",
    1.74 +        "Papa Smurf"
    1.75 +    );
    1.76 +    
    1.77 +    id4->comm_type = PEP_ct_key_b0rken;
    1.78 +    
    1.79 +    pEp_identity* id_arr[4] = {id1, id2, id3, id4};
    1.80 +        
    1.81 +    int i;
    1.82 +        
    1.83 +    cout << "creating one-element identity_list...\n";
    1.84 +    
    1.85 +    pEp_identity* new_id = identity_dup(id1);
    1.86 +    assert(new_id);
    1.87 +    identity_list* idlist = new_identity_list(new_id);
    1.88 +    assert(idlist->ident);
    1.89 +    assert(test_identity_equals(id1, idlist->ident));
    1.90 +    assert(idlist->next == NULL);
    1.91 +    cout << "one-element identity_list created, next element is NULL\n\n";
    1.92 +    
    1.93 +    cout << "duplicating one-element list...\n";
    1.94 +    identity_list* duplist = identity_list_dup(idlist);
    1.95 +    pEp_identity* srcid = idlist->ident;
    1.96 +    pEp_identity* dstid = duplist->ident;
    1.97 +    assert(dstid);
    1.98 +    assert(test_identity_equals(srcid, dstid));
    1.99 +    assert(srcid->address != dstid->address);   // test deep copies 
   1.100 +    assert(srcid->fpr != dstid->fpr);
   1.101 +    assert(srcid->username != dstid->username);
   1.102 +    assert(duplist->next == NULL);
   1.103 +    cout << "one-element identity_list duplicated.\n\n";
   1.104 +    
   1.105 +    cout << "freeing identity_lists...\n";
   1.106 +    free_identity_list(idlist); // will free srcid
   1.107 +    free_identity_list(duplist);
   1.108 +    idlist = NULL;
   1.109 +    duplist = NULL;
   1.110 +    srcid = NULL;
   1.111 +    
   1.112 +    identity_list* p;
   1.113 +    cout << "\ncreating four-element list...\n";
   1.114 +    idlist = identity_list_add(idlist, identity_dup(id_arr[0]));
   1.115 +    for (i = 1; i < 4; i++) {
   1.116 +        p = identity_list_add(idlist, identity_dup(id_arr[i]));
   1.117 +        assert(p);
   1.118 +    }
   1.119 +    
   1.120 +    p = idlist;
   1.121 +    
   1.122 +    for (i = 0; i < 4; i++) {
   1.123 +        assert(p);
   1.124 +        
   1.125 +        srcid = p->ident;
   1.126 +        assert(srcid);
   1.127 +        
   1.128 +        assert(test_identity_equals(srcid, id_arr[i]));
   1.129 +        assert(srcid->address != id_arr[i]->address);   // test deep copies
   1.130 +        assert(srcid->fpr != id_arr[i]->fpr);
   1.131 +        assert(srcid->username != id_arr[i]->username);
   1.132 +
   1.133 +        p = p->next;
   1.134 +    }
   1.135 +    assert(p == NULL);
   1.136 +    
   1.137 +    cout << "\nduplicating four-element list...\n\n";
   1.138 +    duplist = identity_list_dup(idlist);
   1.139 +    
   1.140 +    p = idlist;
   1.141 +    identity_list* dup_p = duplist;
   1.142 +    
   1.143 +    while (dup_p) {
   1.144 +        srcid = p->ident;
   1.145 +        dstid = dup_p->ident;
   1.146 +
   1.147 +        assert(dstid);
   1.148 +        
   1.149 +        assert(test_identity_equals(srcid, dstid));
   1.150 +
   1.151 +        assert(srcid != dstid);   // test deep copies
   1.152 +        assert(srcid->address != dstid->address);   // test deep copies
   1.153 +        assert(srcid->fpr != dstid->fpr);
   1.154 +        assert(srcid->username != dstid->username);
   1.155 +        
   1.156 +        i++;
   1.157 +        p = p->next;
   1.158 +
   1.159 +        dup_p = dup_p->next;
   1.160 +        assert((p == NULL) == (dup_p == NULL));
   1.161 +    }
   1.162 +    cout << "\nfour-element identity_list successfully duplicated.\n\n";
   1.163 +
   1.164 +    cout << "freeing identity_lists...\n";
   1.165 +    free_identity_list(idlist); // will free srcid
   1.166 +    free_identity_list(duplist);
   1.167 +    idlist = NULL;
   1.168 +    duplist = NULL;
   1.169 +    
   1.170 +    cout << "done.\n";
   1.171 +        
   1.172 +    
   1.173 +    return 0;
   1.174 +}
   1.175 +