test/identity_list_test.cc
author Krista Bennett <krista@pep-project.org>
Fri, 01 Jun 2018 10:30:21 +0200
branchENGINE-217
changeset 2732 2d177ec3804b
parent 1513 e7f7e42385b5
child 1945 9503c962f746
permissions -rw-r--r--
close branch
     1 // This file is under GNU General Public License 3.0
     2 // see LICENSE.txt
     3 
     4 #include <stdlib.h>
     5 #include <string.h>
     6 #include "platform.h"
     7 #include <iostream>
     8 #include <fstream>
     9 #include <assert.h>
    10 
    11 #include "identity_list.h"
    12 
    13 using namespace std;
    14 
    15 /*
    16  *     char *address;              // C string with address UTF-8 encoded
    17     char *fpr;                  // C string with fingerprint UTF-8 encoded
    18     char *user_id;              // C string with user ID UTF-8 encoded
    19     char *username;             // C string with user name UTF-8 encoded
    20     PEP_comm_type comm_type;    // type of communication with this ID
    21     char lang[3];               // language of conversation
    22                                 // ISO 639-1 ALPHA-2, last byte is 0
    23     bool me;                    // if this is the local user herself/himself
    24     */
    25 
    26 int test_identity_equals(pEp_identity* val1, pEp_identity* val2) {
    27     assert(val1);
    28     assert(val2);
    29     assert(val1->address);
    30     assert(val2->address);
    31     assert(val1->fpr);
    32     assert(val2->fpr);
    33     assert(val1->username);
    34     assert(val2->username);
    35     return((strcmp(val1->address, val2->address) == 0) && (strcmp(val1->fpr, val2->fpr) == 0)
    36         && (strcmp(val1->username, val2->username) == 0) && (val1->comm_type == val2->comm_type)
    37         && (val1->lang[0] == val2->lang[0]) && (val1->lang[1] == val2->lang[1])
    38         && (val1->lang[2] == val2->lang[2]) && (val1->me == val2->me));
    39 }
    40 
    41 int main() {
    42     cout << "\n*** data structures: identity_list_test ***\n\n";
    43 
    44     pEp_identity* id1 = new_identity(
    45         "leon.schumacher@digitalekho.com",
    46         "8BD08954C74D830EEFFB5DEB2682A17F7C87F73D",
    47         "23",
    48         "Leon Schumacher"
    49     );
    50     id1->comm_type = PEP_ct_pEp;
    51     
    52     pEp_identity* id2 = new_identity(
    53         "krista@kgrothoff.org",
    54         "62D4932086185C15917B72D30571AFBCA5493553",
    55         "42",
    56         "Krista Bennett Grothoff"
    57     );
    58     
    59     id2->comm_type = PEP_ct_OpenPGP;
    60 
    61     pEp_identity* id3 = new_identity(
    62         "krista@pep-project.org",
    63         "51BF42D25BB5B154D71BF6CD3CF25B776D149247",
    64         "10",
    65         "Krista Grothoff"
    66     );
    67     
    68     id3->comm_type = PEP_ct_OTR;
    69 
    70     pEp_identity* id4 = new_identity(
    71         "papa@smurf.lu",
    72         "00001111222233334444555566667777DEADBEEF",
    73         "667",
    74         "Papa Smurf"
    75     );
    76     
    77     id4->comm_type = PEP_ct_key_b0rken;
    78     
    79     pEp_identity* id_arr[4] = {id1, id2, id3, id4};
    80         
    81     int i;
    82         
    83     cout << "creating one-element identity_list...\n";
    84     
    85     pEp_identity* new_id = identity_dup(id1);
    86     assert(new_id);
    87     identity_list* idlist = new_identity_list(new_id);
    88     assert(idlist->ident);
    89     assert(test_identity_equals(id1, idlist->ident));
    90     assert(idlist->next == NULL);
    91     cout << "one-element identity_list created, next element is NULL\n\n";
    92     
    93     cout << "duplicating one-element list...\n";
    94     identity_list* duplist = identity_list_dup(idlist);
    95     pEp_identity* srcid = idlist->ident;
    96     pEp_identity* dstid = duplist->ident;
    97     assert(dstid);
    98     assert(test_identity_equals(srcid, dstid));
    99     assert(srcid->address != dstid->address);   // test deep copies 
   100     assert(srcid->fpr != dstid->fpr);
   101     assert(srcid->username != dstid->username);
   102     assert(duplist->next == NULL);
   103     cout << "one-element identity_list duplicated.\n\n";
   104     
   105     cout << "freeing identity_lists...\n";
   106     free_identity_list(idlist); // will free srcid
   107     free_identity_list(duplist);
   108     idlist = NULL;
   109     duplist = NULL;
   110     srcid = NULL;
   111     
   112     identity_list* p;
   113     cout << "\ncreating four-element list...\n";
   114     idlist = identity_list_add(idlist, identity_dup(id_arr[0]));
   115     for (i = 1; i < 4; i++) {
   116         p = identity_list_add(idlist, identity_dup(id_arr[i]));
   117         assert(p);
   118     }
   119     
   120     p = idlist;
   121     
   122     for (i = 0; i < 4; i++) {
   123         assert(p);
   124         
   125         srcid = p->ident;
   126         assert(srcid);
   127         
   128         assert(test_identity_equals(srcid, id_arr[i]));
   129         assert(srcid->address != id_arr[i]->address);   // test deep copies
   130         assert(srcid->fpr != id_arr[i]->fpr);
   131         assert(srcid->username != id_arr[i]->username);
   132 
   133         p = p->next;
   134     }
   135     assert(p == NULL);
   136     
   137     cout << "\nduplicating four-element list...\n\n";
   138     duplist = identity_list_dup(idlist);
   139     
   140     p = idlist;
   141     identity_list* dup_p = duplist;
   142     
   143     while (dup_p) {
   144         srcid = p->ident;
   145         dstid = dup_p->ident;
   146 
   147         assert(dstid);
   148         
   149         assert(test_identity_equals(srcid, dstid));
   150 
   151         assert(srcid != dstid);   // test deep copies
   152         assert(srcid->address != dstid->address);   // test deep copies
   153         assert(srcid->fpr != dstid->fpr);
   154         assert(srcid->username != dstid->username);
   155         
   156         i++;
   157         p = p->next;
   158 
   159         dup_p = dup_p->next;
   160         assert((p == NULL) == (dup_p == NULL));
   161     }
   162     cout << "\nfour-element identity_list successfully duplicated.\n\n";
   163 
   164     cout << "freeing identity_lists...\n";
   165     free_identity_list(idlist); // will free srcid
   166     free_identity_list(duplist);
   167     idlist = NULL;
   168     duplist = NULL;
   169     
   170     cout << "done.\n";
   171         
   172     
   173     return 0;
   174 }
   175