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