merged in sync ENGINE-551
authorKrista 'DarthMama' Bennett <krista@pep.foundation>
Tue, 09 Jul 2019 11:24:18 +0200
branchENGINE-551
changeset 3914eec042946bec
parent 3879 e8bf8bb51582
parent 3910 1e9cd9e6cd29
child 3915 c3fa76972af5
child 3949 571afb5abe5f
merged in sync
src/message_api.c
src/message_api.h
src/pEpEngine.h
src/pEp_internal.h
test/src/SuiteMaker.cc
     1.1 --- a/build-mac/pEpEngine.xcodeproj/project.pbxproj	Wed Jun 26 11:35:54 2019 +0200
     1.2 +++ b/build-mac/pEpEngine.xcodeproj/project.pbxproj	Tue Jul 09 11:24:18 2019 +0200
     1.3 @@ -1155,7 +1155,7 @@
     1.4  				ONLY_ACTIVE_ARCH = YES;
     1.5  				OTHER_CFLAGS = "-DSQLITE3_FROM_OS";
     1.6  				SDKROOT = iphoneos;
     1.7 -				VALID_ARCHS = "arm64 armv7s armv7";
     1.8 +				VALID_ARCHS = "$(VALID_ARCHS) x86_64";
     1.9  			};
    1.10  			name = Debug;
    1.11  		};
    1.12 @@ -1214,7 +1214,7 @@
    1.13  				OTHER_CFLAGS = "-DSQLITE3_FROM_OS";
    1.14  				SDKROOT = iphoneos;
    1.15  				VALIDATE_PRODUCT = YES;
    1.16 -				VALID_ARCHS = "arm64 armv7s armv7";
    1.17 +				VALID_ARCHS = "$(VALID_ARCHS) x86_64";
    1.18  			};
    1.19  			name = Release;
    1.20  		};
     2.1 --- a/src/identity_list.c	Wed Jun 26 11:35:54 2019 +0200
     2.2 +++ b/src/identity_list.c	Tue Jul 09 11:24:18 2019 +0200
     2.3 @@ -108,6 +108,23 @@
     2.4      return list_curr->next;
     2.5  }
     2.6  
     2.7 +// returns *head* of list
     2.8 +DYNAMIC_API identity_list* identity_list_join(identity_list *first_list, identity_list *second_list) {
     2.9 +    if (!first_list) {
    2.10 +        if (!second_list)
    2.11 +            return NULL;
    2.12 +        return second_list;
    2.13 +    }
    2.14 +    if (second_list) {
    2.15 +        identity_list* list_curr = first_list;
    2.16 +        while (list_curr->next)
    2.17 +            list_curr = list_curr->next;    
    2.18 +            
    2.19 +        list_curr->next = second_list;
    2.20 +    }        
    2.21 +    return first_list;    
    2.22 +}
    2.23 +
    2.24  DYNAMIC_API int identity_list_length(const identity_list *id_list)
    2.25  {
    2.26      int len = 0;
     3.1 --- a/src/identity_list.h	Wed Jun 26 11:35:54 2019 +0200
     3.2 +++ b/src/identity_list.h	Tue Jul 09 11:24:18 2019 +0200
     3.3 @@ -63,6 +63,16 @@
     3.4  
     3.5  DYNAMIC_API identity_list *identity_list_add(identity_list *id_list, pEp_identity *ident);
     3.6  
     3.7 +// identity_list_add - join second identity_list to the first.
     3.8 +//
     3.9 +//  parameters:
    3.10 +//      first_list (in)             identity_list to add to
    3.11 +//      second_list (in)            identity list to add
    3.12 +//
    3.13 +//  return value:
    3.14 +//      pointer to the HEAD of the new list, or NULL if both lists are empty.
    3.15 +//
    3.16 +DYNAMIC_API identity_list *identity_list_join(identity_list *first_list, identity_list* second_list);
    3.17  
    3.18  // identity_list_length() - get length of identity_list
    3.19  //
    3.20 @@ -73,9 +83,7 @@
    3.21  //      length of identity_list in number of elements
    3.22  
    3.23  DYNAMIC_API int identity_list_length(const identity_list *id_list);
    3.24 -
    3.25 -
    3.26 +    
    3.27  #ifdef __cplusplus
    3.28  }
    3.29  #endif
    3.30 -
     4.1 --- a/src/keymanagement.c	Wed Jun 26 11:35:54 2019 +0200
     4.2 +++ b/src/keymanagement.c	Tue Jul 09 11:24:18 2019 +0200
     4.3 @@ -470,6 +470,8 @@
     4.4      PEP_STATUS status;
     4.5      
     4.6      bool is_identity_default, is_user_default, is_address_default;
     4.7 +    bool no_stored_default = EMPTYSTR(stored_ident->fpr);
     4.8 +    
     4.9      status = get_valid_pubkey(session, stored_ident,
    4.10                                  &is_identity_default,
    4.11                                  &is_user_default,
    4.12 @@ -488,7 +490,12 @@
    4.13              stored_ident->comm_type = ct;
    4.14          }
    4.15      }
    4.16 -    else {
    4.17 +    else if (status != PEP_STATUS_OK) {
    4.18 +        free(stored_ident->fpr);
    4.19 +        stored_ident->fpr = NULL;
    4.20 +        stored_ident->comm_type = PEP_ct_key_not_found;        
    4.21 +    }
    4.22 +    else { // no key returned, but status ok?
    4.23          if (stored_ident->comm_type == PEP_ct_unknown)
    4.24              stored_ident->comm_type = PEP_ct_key_not_found;
    4.25      }
    4.26 @@ -536,8 +543,17 @@
    4.27           // or identity AND is valid for this address, set in DB
    4.28           // as default
    4.29           status = set_identity(session, return_id);
    4.30 +    } 
    4.31 +    else if (no_stored_default && !EMPTYSTR(return_id->fpr) 
    4.32 +             && return_id->comm_type != PEP_ct_key_revoked
    4.33 +             && return_id->comm_type != PEP_ct_key_expired
    4.34 +             && return_id->comm_type != PEP_ct_key_expired_but_confirmed
    4.35 +             && return_id->comm_type != PEP_ct_mistrusted 
    4.36 +             && return_id->comm_type != PEP_ct_key_b0rken) { 
    4.37 +        // We would have stored this anyway for a first-time elected key. We just have an ident w/ no default already.
    4.38 +        status = set_identity(session, return_id);
    4.39      }
    4.40 -    else {
    4.41 +    else { // this is a key other than the default, but there IS a default (FIXME: fdik, do we really want behaviour below?)
    4.42          // Store without default fpr/ct, but return the fpr and ct 
    4.43          // for current use
    4.44          char* save_fpr = return_id->fpr;
     5.1 --- a/src/message_api.c	Wed Jun 26 11:35:54 2019 +0200
     5.2 +++ b/src/message_api.c	Tue Jul 09 11:24:18 2019 +0200
     5.3 @@ -3092,7 +3092,6 @@
     5.4  }
     5.5  
     5.6  static PEP_STATUS reconcile_sent_and_recv_info(message* src, message* inner_message) {
     5.7 -    PEP_STATUS status = PEP_STATUS_OK;
     5.8      if (!src || !inner_message)
     5.9          return PEP_ILLEGAL_VALUE;
    5.10          
     6.1 --- a/src/pEpEngine.c	Wed Jun 26 11:35:54 2019 +0200
     6.2 +++ b/src/pEpEngine.c	Tue Jul 09 11:24:18 2019 +0200
     6.3 @@ -4267,8 +4267,8 @@
     6.4              identity->username))
     6.5          return PEP_ILLEGAL_VALUE;
     6.6  
     6.7 -    const char* saved_username = NULL;
     6.8 -    const char* at = NULL;
     6.9 +    char* saved_username = NULL;
    6.10 +    char* at = NULL;
    6.11      size_t uname_len = strlen(identity->username);
    6.12      
    6.13      if (uname_len > 0)
     7.1 --- a/src/pEpEngine.h	Wed Jun 26 11:35:54 2019 +0200
     7.2 +++ b/src/pEpEngine.h	Tue Jul 09 11:24:18 2019 +0200
     7.3 @@ -60,6 +60,7 @@
     7.4      PEP_KEY_IMPORTED                                = 0x0220,
     7.5      PEP_NO_KEY_IMPORTED                             = 0x0221,
     7.6      PEP_KEY_IMPORT_STATUS_UNKNOWN                   = 0x0222,
     7.7 +    PEP_SOME_KEYS_IMPORTED                          = 0x0223,
     7.8      
     7.9      PEP_CANNOT_FIND_IDENTITY                        = 0x0301,
    7.10      PEP_CANNOT_SET_PERSON                           = 0x0381,
     8.1 --- a/src/pgp_sequoia.c	Wed Jun 26 11:35:54 2019 +0200
     8.2 +++ b/src/pgp_sequoia.c	Tue Jul 09 11:24:18 2019 +0200
     8.3 @@ -1447,6 +1447,33 @@
     8.4      if (size == 0 || sig_size == 0)
     8.5          return PEP_DECRYPT_WRONG_FORMAT;
     8.6  
     8.7 +#if TRACING > 0
     8.8 +    {
     8.9 +        int cr = 0;
    8.10 +        int crlf = 0;
    8.11 +        int lf = 0;
    8.12 +
    8.13 +        for (int i = 0; i < size; i ++) {
    8.14 +            // CR
    8.15 +            if (text[i] == '\r') {
    8.16 +                cr ++;
    8.17 +            }
    8.18 +            // LF
    8.19 +            if (text[i] == '\n') {
    8.20 +                if (i > 0 && text[i - 1] == '\r') {
    8.21 +                    cr --;
    8.22 +                    crlf ++;
    8.23 +                } else {
    8.24 +                    lf ++;
    8.25 +                }
    8.26 +            }
    8.27 +        }
    8.28 +
    8.29 +        T("Text to verify: %zd bytes with %d crlfs, %d bare crs and %d bare lfs",
    8.30 +          size, crlf, cr, lf);
    8.31 +    }
    8.32 +#endif
    8.33 +
    8.34      cookie.recipient_keylist = new_stringlist(NULL);
    8.35      if (!cookie.recipient_keylist)
    8.36          ERROR_OUT(NULL, PEP_OUT_OF_MEMORY, "out of memory");
    8.37 @@ -1854,7 +1881,27 @@
    8.38      return status;
    8.39  }
    8.40  
    8.41 -PEP_STATUS pgp_import_keydata(PEP_SESSION session, const char *key_data,
    8.42 +static unsigned int count_keydata_parts(const char* key_data, size_t size) {
    8.43 +    unsigned int retval = 0;
    8.44 +    
    8.45 +    const char* pgp_begin = "-----BEGIN PGP";
    8.46 +    size_t prefix_len = strlen(pgp_begin);
    8.47 +    size_t size_remaining = size;
    8.48 +    
    8.49 +    while (key_data) {
    8.50 +        if (size_remaining <= prefix_len || key_data[0] == '\0')
    8.51 +            break;
    8.52 +        key_data = strnstr(key_data, pgp_begin, size_remaining);
    8.53 +        if (key_data) {
    8.54 +            retval++;
    8.55 +            key_data += prefix_len;
    8.56 +            size_remaining -= prefix_len;
    8.57 +        }
    8.58 +    }
    8.59 +    return retval;
    8.60 + }
    8.61 +
    8.62 +PEP_STATUS _pgp_import_keydata(PEP_SESSION session, const char *key_data,
    8.63                                size_t size, identity_list **private_idents)
    8.64  {
    8.65      PEP_STATUS status = PEP_NO_KEY_IMPORTED;
    8.66 @@ -1986,6 +2033,78 @@
    8.67      return status;
    8.68  }
    8.69  
    8.70 +PEP_STATUS pgp_import_keydata(PEP_SESSION session, const char *key_data,
    8.71 +                              size_t size, identity_list **private_idents)
    8.72 +{
    8.73 +    unsigned int keycount = count_keydata_parts(key_data, size);
    8.74 +    if (keycount < 2)
    8.75 +        return(_pgp_import_keydata(session, key_data, size, private_idents));
    8.76 +
    8.77 +    const char* pgp_begin = "-----BEGIN PGP";
    8.78 +    size_t prefix_len = strlen(pgp_begin);
    8.79 +        
    8.80 +    unsigned int i;
    8.81 +    const char* curr_begin;
    8.82 +    size_t curr_size;
    8.83 +    
    8.84 +    identity_list* collected_idents = NULL;        
    8.85 +    
    8.86 +    PEP_STATUS retval = PEP_KEY_IMPORTED;
    8.87 +    
    8.88 +    for (i = 0, curr_begin = key_data; i < keycount; i++) {
    8.89 +        const char* next_begin = NULL;
    8.90 +
    8.91 +        // This is assured to be OK because the count function above 
    8.92 +        // made sure that THIS round contains at least prefix_len chars
    8.93 +        // We used strnstr to count, so we know that strstr will be ok.
    8.94 +        if (strlen(curr_begin + prefix_len) > prefix_len)
    8.95 +            next_begin = strstr(curr_begin + prefix_len, pgp_begin);
    8.96 +
    8.97 +        if (next_begin)
    8.98 +            curr_size = next_begin - curr_begin;
    8.99 +        else
   8.100 +            curr_size = (key_data + size) - curr_begin;
   8.101 +        
   8.102 +        PEP_STATUS curr_status = _pgp_import_keydata(session, curr_begin, curr_size, private_idents);
   8.103 +        if (private_idents && *private_idents) {
   8.104 +            if (!collected_idents)
   8.105 +                collected_idents = *private_idents;
   8.106 +            else 
   8.107 +                identity_list_join(collected_idents, *private_idents);
   8.108 +            *private_idents = NULL;    
   8.109 +        }
   8.110 +        
   8.111 +        if (curr_status != retval) {
   8.112 +            switch (curr_status) {
   8.113 +                case PEP_NO_KEY_IMPORTED:
   8.114 +                case PEP_KEY_NOT_FOUND:
   8.115 +                case PEP_UNKNOWN_ERROR:
   8.116 +                    switch (retval) {
   8.117 +                        case PEP_KEY_IMPORTED:
   8.118 +                            retval = PEP_SOME_KEYS_IMPORTED;
   8.119 +                            break;
   8.120 +                        case PEP_UNKNOWN_ERROR:
   8.121 +                            retval = curr_status;
   8.122 +                            break;
   8.123 +                        default:
   8.124 +                            break;
   8.125 +                    }
   8.126 +                    break;
   8.127 +                case PEP_KEY_IMPORTED:
   8.128 +                    retval = PEP_SOME_KEYS_IMPORTED;
   8.129 +                default:
   8.130 +                    break;
   8.131 +            }        
   8.132 +        }        
   8.133 +        curr_begin = next_begin;     
   8.134 +    }
   8.135 +    
   8.136 +    if (private_idents)
   8.137 +        *private_idents = collected_idents;
   8.138 +    
   8.139 +    return retval;    
   8.140 +}
   8.141 +
   8.142  PEP_STATUS pgp_export_keydata(
   8.143          PEP_SESSION session, const char *fpr, char **key_data, size_t *size,
   8.144          bool secret)
   8.145 @@ -2498,6 +2617,22 @@
   8.146  
   8.147      // Is the TPK live?
   8.148      *expired = !pgp_tpk_alive_at(tpk, when);
   8.149 +#ifdef TRACING
   8.150 +    {
   8.151 +        char buffer[26];
   8.152 +        time_t now = time(NULL);
   8.153 +
   8.154 +        if (when == now || when == now - 1) {
   8.155 +            sprintf(buffer, "now");
   8.156 +        } else {
   8.157 +            struct tm tm;
   8.158 +            gmtime_r(&when, &tm);
   8.159 +            strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", &tm);
   8.160 +        }
   8.161 +
   8.162 +        T("TPK is %slive as of %s", *expired ? "not " : "", buffer);
   8.163 +    }
   8.164 +#endif
   8.165      if (*expired)
   8.166          goto out;
   8.167  
   8.168 @@ -2528,9 +2663,15 @@
   8.169  
   8.170      *expired = !(can_encrypt && can_sign && can_certify);
   8.171  
   8.172 +    T("Key can%s encrypt, can%s sign, can%s certify => %sexpired",
   8.173 +      can_encrypt ? "" : "not",
   8.174 +      can_sign ? "" : "not",
   8.175 +      can_certify ? "" : "not",
   8.176 +      *expired ? "" : "not ");
   8.177 +
   8.178   out:
   8.179      pgp_tpk_free(tpk);
   8.180 -    T("(%s) -> %s", fpr, pEp_status_to_string(status));
   8.181 +    T("(%s) -> %s (expired: %d)", fpr, pEp_status_to_string(status), *expired);
   8.182      return status;
   8.183  }
   8.184  
     9.1 --- a/src/platform_unix.c	Wed Jun 26 11:35:54 2019 +0200
     9.2 +++ b/src/platform_unix.c	Tue Jul 09 11:24:18 2019 +0200
     9.3 @@ -198,6 +198,43 @@
     9.4      return retval;
     9.5  }
     9.6  
     9.7 +char *strnstr(const char *big, const char *little, size_t len) {
     9.8 +    if (big == NULL || little == NULL)
     9.9 +        return NULL;
    9.10 +        
    9.11 +    if (*little == '\0')
    9.12 +        return (char*)big;
    9.13 +        
    9.14 +    const char* curr_big = big;
    9.15 +    
    9.16 +    size_t little_len = strlen(little);
    9.17 +    size_t remaining = len;
    9.18 +
    9.19 +    const char* retval = NULL;
    9.20 +    
    9.21 +    for (remaining = len; remaining >= little_len && *curr_big != '\0'; remaining--, curr_big++) {
    9.22 +        // find first-char match
    9.23 +        if (*curr_big != *little) {
    9.24 +            continue;
    9.25 +        }
    9.26 +        retval = curr_big;
    9.27 +
    9.28 +        const char* inner_big = retval + 1;
    9.29 +        const char* curr_little = little + 1;
    9.30 +        int j;
    9.31 +        for (j = 1; j < little_len; j++, inner_big++, curr_little++) {
    9.32 +            if (*inner_big != *curr_little) {
    9.33 +                retval = NULL;
    9.34 +                break;
    9.35 +            }    
    9.36 +        }
    9.37 +        if (retval)
    9.38 +            break;
    9.39 +    }
    9.40 +    return (char*)retval;
    9.41 +}
    9.42 +
    9.43 +
    9.44  #ifdef USE_NETPGP
    9.45  // FIXME: This may cause problems - this is a quick compatibility fix for netpgp code
    9.46  int regnexec(const regex_t* preg, const char* string,
    9.47 @@ -449,4 +486,3 @@
    9.48      return NULL;
    9.49  }
    9.50  #endif
    9.51 -
    10.1 --- a/src/platform_unix.h	Wed Jun 26 11:35:54 2019 +0200
    10.2 +++ b/src/platform_unix.h	Tue Jul 09 11:24:18 2019 +0200
    10.3 @@ -67,6 +67,7 @@
    10.4  #if !defined(BSD) && !defined(__APPLE__)
    10.5  size_t strlcpy(char* dst, const	char* src, size_t size);
    10.6  size_t strlcat(char* dst, const	char* src, size_t size);
    10.7 +char *strnstr(const char *big, const char *little, size_t len);
    10.8  
    10.9  // N.B. This is ifdef'd out because NDK users sometimes have trouble finding regex functions in
   10.10  //      the library in spite of the inclusion of regex.h - this is a FIXME, but since iOS is
    11.1 --- a/src/platform_windows.cpp	Wed Jun 26 11:35:54 2019 +0200
    11.2 +++ b/src/platform_windows.cpp	Tue Jul 09 11:24:18 2019 +0200
    11.3 @@ -283,6 +283,41 @@
    11.4      dst[start_len + size_to_copy] = '\0';
    11.5      return retval;
    11.6  }
    11.7 +char *strnstr(const char *big, const char *little, size_t len) {
    11.8 +    if (big == NULL || little == NULL)
    11.9 +        return NULL;
   11.10 +        
   11.11 +    if (*little == '\0')
   11.12 +        return (char*)big;
   11.13 +        
   11.14 +    const char* curr_big = big;
   11.15 +    
   11.16 +    size_t little_len = strlen(little);
   11.17 +    size_t remaining = len;
   11.18 +
   11.19 +    const char* retval = NULL;
   11.20 +    
   11.21 +    for (remaining = len; remaining >= little_len && *curr_big != '\0'; remaining--, curr_big++) {
   11.22 +        // find first-char match
   11.23 +        if (*curr_big != *little) {
   11.24 +            continue;
   11.25 +        }
   11.26 +        retval = curr_big;
   11.27 +
   11.28 +        const char* inner_big = retval + 1;
   11.29 +        const char* curr_little = little + 1;
   11.30 +        int j;
   11.31 +        for (j = 1; j < little_len; j++, inner_big++, curr_little++) {
   11.32 +            if (*inner_big != *curr_little) {
   11.33 +                retval = NULL;
   11.34 +                break;
   11.35 +            }    
   11.36 +        }
   11.37 +        if (retval)
   11.38 +            break;
   11.39 +    }
   11.40 +    return (char*)retval;
   11.41 +}
   11.42  
   11.43  int mkstemp(char *templ)
   11.44  {
    12.1 --- a/src/platform_windows.h	Wed Jun 26 11:35:54 2019 +0200
    12.2 +++ b/src/platform_windows.h	Tue Jul 09 11:24:18 2019 +0200
    12.3 @@ -74,6 +74,8 @@
    12.4  
    12.5  size_t strlcpy(char* dst, const	char* src, size_t size);
    12.6  size_t strlcat(char* dst, const	char* src, size_t size);
    12.7 +char *strnstr(const char *big, const char *little, size_t len);
    12.8 +
    12.9  
   12.10  const char *windoze_local_db(void);
   12.11  const char *windoze_system_db(void);
    13.1 --- a/src/status_to_string.h	Wed Jun 26 11:35:54 2019 +0200
    13.2 +++ b/src/status_to_string.h	Tue Jul 09 11:24:18 2019 +0200
    13.3 @@ -35,6 +35,7 @@
    13.4      case PEP_KEY_IMPORTED: return "PEP_KEY_IMPORTED";
    13.5      case PEP_NO_KEY_IMPORTED: return "PEP_NO_KEY_IMPORTED";
    13.6      case PEP_KEY_IMPORT_STATUS_UNKNOWN: return "PEP_KEY_IMPORT_STATUS_UNKNOWN";
    13.7 +    case PEP_SOME_KEYS_IMPORTED: return "PEP_SOME_KEYS_IMPORTED";
    13.8      
    13.9      case PEP_CANNOT_FIND_IDENTITY: return "PEP_CANNOT_FIND_IDENTITY";
   13.10      case PEP_CANNOT_SET_PERSON: return "PEP_CANNOT_SET_PERSON";
    14.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    14.2 +++ b/test/include/StrnstrTests.h	Tue Jul 09 11:24:18 2019 +0200
    14.3 @@ -0,0 +1,35 @@
    14.4 +// This file is under GNU General Public License 3.0
    14.5 +// see LICENSE.txt
    14.6 +
    14.7 +#ifndef STRNSTR_H
    14.8 +#define STRNSTR_H
    14.9 +
   14.10 +#include <string>
   14.11 +#include "EngineTestIndividualSuite.h"
   14.12 +
   14.13 +using namespace std;
   14.14 +
   14.15 +class StrnstrTests : public EngineTestIndividualSuite {
   14.16 +    public:
   14.17 +        StrnstrTests(string test_suite, string test_home_dir);
   14.18 +    private:
   14.19 +        void check_strnstr_equal();
   14.20 +        void check_strnstr_first_null();
   14.21 +        void check_strnstr_second_null();
   14.22 +        void check_strnstr_both_null();
   14.23 +        void check_strnstr_first_empty();
   14.24 +        void check_strnstr_second_empty();
   14.25 +        void check_strnstr_both_empty();
   14.26 +        void check_strnstr_first_letter_only();
   14.27 +        void check_strnstr_first_two_only();
   14.28 +        void check_strnstr_all_but_last();
   14.29 +        void check_strnstr_same_len_all_but_last();
   14.30 +        void check_strnstr_same_len_none();
   14.31 +        void check_strnstr_same_big_smaller();
   14.32 +        void check_strnstr_shift_one_no_match();
   14.33 +        void check_strnstr_shift_to_end();
   14.34 +        void check_strnstr_match_after_end();
   14.35 +        void check_strnstr_equal_but_size_too_small();
   14.36 +};
   14.37 +
   14.38 +#endif
    15.1 --- a/test/src/SuiteMaker.cc	Wed Jun 26 11:35:54 2019 +0200
    15.2 +++ b/test/src/SuiteMaker.cc	Tue Jul 09 11:24:18 2019 +0200
    15.3 @@ -71,6 +71,7 @@
    15.4  #include "KeyAttachmentTests.h"
    15.5  #include "OwnKeysRetrieveTests.h"
    15.6  #include "TrustManipulationTests.h"
    15.7 +#include "StrnstrTests.h"
    15.8  #include "SyncTests.h"
    15.9  #include "SenderFPRTests.h"
   15.10  #include "RevocationTests.h"
   15.11 @@ -137,6 +138,7 @@
   15.12      "KeyAttachmentTests",
   15.13      "OwnKeysRetrieveTests",
   15.14      "TrustManipulationTests",
   15.15 +    "StrnstrTests",
   15.16      "SyncTests",
   15.17      "SenderFPRTests",
   15.18      "RevocationTests",
   15.19 @@ -265,6 +267,8 @@
   15.20          *test_suite = new OwnKeysRetrieveTests(test_class_name, test_home);
   15.21      else if (strcmp(test_class_name, "TrustManipulationTests") == 0)
   15.22          *test_suite = new TrustManipulationTests(test_class_name, test_home);
   15.23 +    else if (strcmp(test_class_name, "StrnstrTests") == 0)
   15.24 +        *test_suite = new StrnstrTests(test_class_name, test_home);
   15.25      else if (strcmp(test_class_name, "SyncTests") == 0)
   15.26          *test_suite = new SyncTests(test_class_name, test_home);
   15.27      else if (strcmp(test_class_name, "SenderFPRTests") == 0)
    16.1 --- a/test/src/engine_tests/CheckRenewedExpiredKeyTrustStatusTests.cc	Wed Jun 26 11:35:54 2019 +0200
    16.2 +++ b/test/src/engine_tests/CheckRenewedExpiredKeyTrustStatusTests.cc	Tue Jul 09 11:24:18 2019 +0200
    16.3 @@ -108,6 +108,13 @@
    16.4      status = update_identity(session, expired_inquisitor);
    16.5      TEST_ASSERT_MSG((status == PEP_KEY_UNSUITABLE), tl_status_string(status));
    16.6      PEP_comm_type ct = expired_inquisitor->comm_type;    
    16.7 +    TEST_ASSERT_MSG(ct == PEP_ct_key_not_found, tl_ct_string(ct));
    16.8 +    TEST_ASSERT(!expired_inquisitor->fpr);
    16.9 +    
   16.10 +    expired_inquisitor->fpr = strdup(inquisitor_fpr);
   16.11 +    status = get_trust(session, expired_inquisitor);
   16.12 +    ct = expired_inquisitor->comm_type;    
   16.13 +    TEST_ASSERT(status == PEP_STATUS_OK);
   16.14      TEST_ASSERT_MSG(ct == PEP_ct_key_expired_but_confirmed, tl_ct_string(ct));
   16.15      
   16.16      // Ok, so I want to make sure we make an entry, so I'll try to decrypt the message WITH
   16.17 @@ -250,6 +257,13 @@
   16.18      status = update_identity(session, expired_inquisitor);
   16.19      TEST_ASSERT_MSG((status == PEP_KEY_UNSUITABLE), tl_status_string(status));
   16.20      PEP_comm_type ct = expired_inquisitor->comm_type;    
   16.21 +    TEST_ASSERT_MSG(ct == PEP_ct_key_not_found, tl_ct_string(ct));
   16.22 +    TEST_ASSERT(!expired_inquisitor->fpr);
   16.23 +    
   16.24 +    expired_inquisitor->fpr = strdup(inquisitor_fpr);
   16.25 +    status = get_trust(session, expired_inquisitor);
   16.26 +    ct = expired_inquisitor->comm_type;    
   16.27 +    TEST_ASSERT(status == PEP_STATUS_OK);
   16.28      TEST_ASSERT_MSG(ct == PEP_ct_key_expired_but_confirmed, tl_ct_string(ct));
   16.29      
   16.30      // Ok, so I want to make sure we make an entry, so I'll try to decrypt the message WITH
    17.1 --- a/test/src/engine_tests/ExternalRevokeTests.cc	Wed Jun 26 11:35:54 2019 +0200
    17.2 +++ b/test/src/engine_tests/ExternalRevokeTests.cc	Tue Jul 09 11:24:18 2019 +0200
    17.3 @@ -255,8 +255,9 @@
    17.4      
    17.5      status = get_trust(session, recip1);
    17.6  
    17.7 -    cout << "Recip's trust DB comm_type (should be unknown, as we're using a keyring-only key, not in DB) = "  << tl_ct_string(recip1->comm_type) << endl;
    17.8 -    TEST_ASSERT_MSG((recip1->comm_type != PEP_ct_OpenPGP_unconfirmed), "recip1->comm_type != PEP_ct_OpenPGP_unconfirmed");
    17.9 +//    cout << "Recip's trust DB comm_type (should be unknown, as we're using a keyring-only key, not in DB) = "  << tl_ct_string(recip1->comm_type) << endl;
   17.10 +    cout << "Recip's trust DB comm_type (should PEP_ct_OpenPGP_unconfirmed), as we now record this when using update_identity on no-default idents = "  << tl_ct_string(recip1->comm_type) << endl;
   17.11 +    TEST_ASSERT_MSG((recip1->comm_type == PEP_ct_OpenPGP_unconfirmed), tl_ct_string(recip1->comm_type));
   17.12  
   17.13      // decrypt message
   17.14  //    free_message(outgoing_msg);
   17.15 @@ -285,8 +286,9 @@
   17.16      
   17.17      status = get_trust(session, recip1);
   17.18      
   17.19 -    cout << "Recip's trust DB comm_type (should be unknown - there's nothing in the DB) = "  << tl_ct_string(recip1->comm_type) << endl;
   17.20 -    TEST_ASSERT_MSG((recip1->comm_type == PEP_ct_unknown), "recip1->comm_type == PEP_ct_unknown");
   17.21 +//    cout << "Recip's trust DB comm_type (should be unknown - there's nothing in the DB) = "  << tl_ct_string(recip1->comm_type) << endl;
   17.22 +    cout << "Recip's trust DB comm_type (should be PEP_ct_OpenPGP_unconfirmed, as we now store it.) = "  << tl_ct_string(recip1->comm_type) << endl;
   17.23 +    TEST_ASSERT_MSG((recip1->comm_type == PEP_ct_OpenPGP_unconfirmed), tl_ct_string(recip1->comm_type));
   17.24  
   17.25      free_message(encrypted_outgoing_msg);
   17.26      free_message(decrypted_msg);
    18.1 --- a/test/src/engine_tests/IOS1664Tests.cc	Wed Jun 26 11:35:54 2019 +0200
    18.2 +++ b/test/src/engine_tests/IOS1664Tests.cc	Tue Jul 09 11:24:18 2019 +0200
    18.3 @@ -48,12 +48,23 @@
    18.4      
    18.5      pEp_identity* you = new_identity("superxat@gmail.com", NULL, NULL, NULL);
    18.6      
    18.7 +    // N.B. while obviously it would be better to write the test expecting us to 
    18.8 +    // accept the key, I'm actually testing that we don't get the wrong status
    18.9 +    // based on the presumption of rejection
   18.10 +    
   18.11      message* out_msg = new_message(PEP_dir_outgoing);
   18.12      out_msg->from = me;
   18.13      out_msg->to = new_identity_list(you);
   18.14      out_msg->shortmsg = strdup("Hussidente 2020!");
   18.15      out_msg->longmsg = strdup("A Huss in every office!");
   18.16      
   18.17 +    status = identity_rating(session, out_msg->from, &rating);
   18.18 +    TEST_ASSERT(status == PEP_STATUS_OK);
   18.19 +    TEST_ASSERT_MSG(rating == PEP_rating_trusted_and_anonymized, tl_rating_string(rating));
   18.20 +    status = identity_rating(session, out_msg->to->ident, &rating);
   18.21 +    TEST_ASSERT_MSG(status == PEP_KEY_NOT_FOUND, tl_status_string(status));
   18.22 +    TEST_ASSERT_MSG(rating == PEP_rating_undefined, tl_rating_string(rating));
   18.23 +
   18.24      status = outgoing_message_rating(session, out_msg, &rating);
   18.25      TEST_ASSERT(rating == PEP_rating_unencrypted);
   18.26      
    19.1 --- a/test/src/engine_tests/NewUpdateIdAndMyselfTests.cc	Wed Jun 26 11:35:54 2019 +0200
    19.2 +++ b/test/src/engine_tests/NewUpdateIdAndMyselfTests.cc	Tue Jul 09 11:24:18 2019 +0200
    19.3 @@ -500,10 +500,10 @@
    19.4      TEST_ASSERT_MSG((bernd->user_id), "bernd->user_id");
    19.5      TEST_ASSERT_MSG((strcmp(bernd->user_id, bernd_userid) == 0), "strcmp(bernd->user_id, bernd_userid) == 0"); // ???
    19.6      TEST_ASSERT_MSG((!bernd->me), "!bernd->me"); 
    19.7 -    TEST_ASSERT_MSG((bernd->comm_type == PEP_ct_key_expired), "bernd->comm_type == PEP_ct_key_expired");
    19.8 +    TEST_ASSERT_MSG((bernd->comm_type == PEP_ct_key_not_found), tl_ct_string(bernd->comm_type));
    19.9      TEST_ASSERT_MSG((strcmp(bernd->address, bernd_address) == 0), "strcmp(bernd->address, bernd_address) == 0");
   19.10  
   19.11 -    cout << "PASS: update_identity() correctly rejected expired key with PEP_KEY_UNSUITABLE and PEP_ct_key_expired" << endl << endl;
   19.12 +    cout << "PASS: update_identity() correctly rejected expired key with PEP_KEY_UNSUITABLE and PEP_ct_key_not_found" << endl << endl;
   19.13      free_identity(bernd);
   19.14      
   19.15  }
    20.1 --- a/test/src/engine_tests/RevocationTests.cc	Wed Jun 26 11:35:54 2019 +0200
    20.2 +++ b/test/src/engine_tests/RevocationTests.cc	Tue Jul 09 11:24:18 2019 +0200
    20.3 @@ -52,8 +52,24 @@
    20.4      TEST_ASSERT_MSG((status == PEP_TEST_KEY_IMPORT_SUCCESS), "status == PEP_STATUS_OK");
    20.5  
    20.6      pEp_identity* post = new_identity("linda@example.org", NULL, NULL, NULL);
    20.7 +    
    20.8 +//    string save_fpr = post->fpr;
    20.9 +
   20.10 +    stringlist_t* keylist = NULL;
   20.11 +    
   20.12 +    status = find_keys(session, "linda@example.org", &keylist);
   20.13 +    TEST_ASSERT(status == PEP_STATUS_OK);
   20.14 +    
   20.15      status = update_identity(session, post);
   20.16      // PEP_KEY_UNSUITABLE => revoked (or something similar).
   20.17      TEST_ASSERT_MSG((status == PEP_KEY_UNSUITABLE), tl_status_string(status));
   20.18 +    TEST_ASSERT_MSG((post->comm_type == PEP_ct_key_not_found), tl_ct_string(post->comm_type));
   20.19 +    free(post->fpr);
   20.20 +    post->fpr = strdup(keylist->value);
   20.21 +    status = get_trust(session, post);
   20.22 +    TEST_ASSERT(status == PEP_STATUS_OK);
   20.23      TEST_ASSERT_MSG((post->comm_type == PEP_ct_key_revoked), tl_ct_string(post->comm_type));
   20.24 +    free_identity(pre);
   20.25 +    free_identity(post);
   20.26 +    free_stringlist(keylist);    
   20.27  }
    21.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    21.2 +++ b/test/src/engine_tests/StrnstrTests.cc	Tue Jul 09 11:24:18 2019 +0200
    21.3 @@ -0,0 +1,154 @@
    21.4 +// This file is under GNU General Public License 3.0
    21.5 +// see LICENSE.txt
    21.6 +
    21.7 +#include <stdlib.h>
    21.8 +#include <cstring>
    21.9 +#include <string>
   21.10 +
   21.11 +#include <cpptest.h>
   21.12 +#include "test_util.h"
   21.13 +
   21.14 +#include "pEpEngine.h"
   21.15 +#include "platform_unix.h"
   21.16 +
   21.17 +#include "EngineTestIndividualSuite.h"
   21.18 +#include "StrnstrTests.h"
   21.19 +
   21.20 +using namespace std;
   21.21 +
   21.22 +StrnstrTests::StrnstrTests(string suitename, string test_home_dir) :
   21.23 +    EngineTestIndividualSuite::EngineTestIndividualSuite(suitename, test_home_dir) {
   21.24 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("StrnstrTests::check_strnstr_equal"),
   21.25 +                                                                      static_cast<Func>(&StrnstrTests::check_strnstr_equal)));
   21.26 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("StrnstrTests::check_strnstr_first_empty"),
   21.27 +                                                                      static_cast<Func>(&StrnstrTests::check_strnstr_first_empty)));
   21.28 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("StrnstrTests::check_strnstr_second_empty"),
   21.29 +                                                                      static_cast<Func>(&StrnstrTests::check_strnstr_second_empty)));
   21.30 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("StrnstrTests::check_strnstr_both_empty"),
   21.31 +                                                                      static_cast<Func>(&StrnstrTests::check_strnstr_both_empty)));
   21.32 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("StrnstrTests::check_strnstr_first_letter_only"),
   21.33 +                                                                      static_cast<Func>(&StrnstrTests::check_strnstr_first_letter_only)));
   21.34 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("StrnstrTests::check_strnstr_first_two_only"),
   21.35 +                                                                      static_cast<Func>(&StrnstrTests::check_strnstr_first_two_only)));
   21.36 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("StrnstrTests::check_strnstr_all_but_last"),
   21.37 +                                                                      static_cast<Func>(&StrnstrTests::check_strnstr_all_but_last)));
   21.38 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("StrnstrTests::check_strnstr_same_len_all_but_last"),
   21.39 +                                                                      static_cast<Func>(&StrnstrTests::check_strnstr_same_len_all_but_last)));
   21.40 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("StrnstrTests::check_strnstr_same_len_none"),
   21.41 +                                                                      static_cast<Func>(&StrnstrTests::check_strnstr_same_len_none)));
   21.42 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("StrnstrTests::check_strnstr_same_big_smaller"),
   21.43 +                                                                      static_cast<Func>(&StrnstrTests::check_strnstr_same_big_smaller)));
   21.44 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("StrnstrTests::check_strnstr_shift_one_no_match"),
   21.45 +                                                                      static_cast<Func>(&StrnstrTests::check_strnstr_shift_one_no_match)));
   21.46 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("StrnstrTests::check_strnstr_shift_to_end"),
   21.47 +                                                                      static_cast<Func>(&StrnstrTests::check_strnstr_shift_to_end)));
   21.48 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("StrnstrTests::check_strnstr_match_after_end"),
   21.49 +                                                                      static_cast<Func>(&StrnstrTests::check_strnstr_match_after_end)));
   21.50 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("StrnstrTests::check_strnstr_equal_but_size_too_small"),
   21.51 +                                                                      static_cast<Func>(&StrnstrTests::check_strnstr_equal_but_size_too_small)));
   21.52 +}
   21.53 +
   21.54 +void StrnstrTests::check_strnstr_equal() {
   21.55 +    const char* big = "Bob123";
   21.56 +    const char* little = "Bob123";
   21.57 +    size_t size = strlen(big);
   21.58 +    const char* result = strnstr(big, little, size);
   21.59 +    TEST_ASSERT_MSG(result == big, result);
   21.60 +}
   21.61 +
   21.62 +void StrnstrTests::check_strnstr_first_empty() {
   21.63 +    const char* big = "";
   21.64 +    const char* little = "Bob123";
   21.65 +    size_t size = strlen(big);
   21.66 +    const char* result = strnstr(big, little, size);
   21.67 +    TEST_ASSERT_MSG(result == NULL, result);
   21.68 +}
   21.69 +void StrnstrTests::check_strnstr_second_empty() {
   21.70 +    const char* big = "YerMama";
   21.71 +    const char* little = "";
   21.72 +    size_t size = strlen(big);
   21.73 +    const char* result = strnstr(big, little, size);
   21.74 +    TEST_ASSERT_MSG(result == big, result);
   21.75 +    TEST_ASSERT(true);
   21.76 +}
   21.77 +
   21.78 +void StrnstrTests::check_strnstr_both_empty() {
   21.79 +    const char* big = "";
   21.80 +    const char* little = "";
   21.81 +    size_t size = strlen(big);
   21.82 +    const char* result = strnstr(big, little, size);
   21.83 +    TEST_ASSERT_MSG(result == big, result);
   21.84 +    TEST_ASSERT(true);
   21.85 +}
   21.86 +
   21.87 +void StrnstrTests::check_strnstr_first_letter_only() {
   21.88 +    const char* big = "Bob123";
   21.89 +    const char* little = "Beef";
   21.90 +    size_t size = strlen(big);
   21.91 +    const char* result = strnstr(big, little, size);
   21.92 +    TEST_ASSERT_MSG(result == NULL, result);    
   21.93 +}
   21.94 +void StrnstrTests::check_strnstr_first_two_only() {
   21.95 +    const char* big = "Bob123";
   21.96 +    const char* little = "Boof";
   21.97 +    size_t size = strlen(big);
   21.98 +    const char* result = strnstr(big, little, size);
   21.99 +    TEST_ASSERT_MSG(result == NULL, result);    
  21.100 +}
  21.101 +void StrnstrTests::check_strnstr_all_but_last() {
  21.102 +    const char* big = "BeesBeesBees";
  21.103 +    const char* little = "Beef";
  21.104 +    size_t size = strlen(big);
  21.105 +    const char* result = strnstr(big, little, size);
  21.106 +    TEST_ASSERT_MSG(result == NULL, result);    
  21.107 +}
  21.108 +void StrnstrTests::check_strnstr_same_len_all_but_last() {
  21.109 +    const char* big = "Bees";
  21.110 +    const char* little = "Beef";
  21.111 +    size_t size = strlen(big);
  21.112 +    const char* result = strnstr(big, little, size);
  21.113 +    TEST_ASSERT_MSG(result == NULL, result);    
  21.114 +}
  21.115 +void StrnstrTests::check_strnstr_same_len_none() {
  21.116 +    const char* big = "1234";
  21.117 +    const char* little = "Beef";
  21.118 +    size_t size = strlen(big);
  21.119 +    const char* result = strnstr(big, little, size);
  21.120 +    TEST_ASSERT_MSG(result == NULL, result);    
  21.121 +}
  21.122 +void StrnstrTests::check_strnstr_same_big_smaller() {
  21.123 +    const char* big = "Bee";
  21.124 +    const char* little = "Bees";
  21.125 +    size_t size = strlen(big);
  21.126 +    const char* result = strnstr(big, little, size);
  21.127 +    TEST_ASSERT_MSG(result == NULL, result);    
  21.128 +}
  21.129 +void StrnstrTests::check_strnstr_shift_one_no_match() {
  21.130 +    const char* big = "1Bee";
  21.131 +    const char* little = "Bees";
  21.132 +    size_t size = strlen(big);
  21.133 +    const char* result = strnstr(big, little, size);
  21.134 +    TEST_ASSERT_MSG(result == NULL, result);    
  21.135 +}
  21.136 +void StrnstrTests::check_strnstr_shift_to_end() {
  21.137 +    const char* big = "BigBeeWithExtraBeef";
  21.138 +    const char* little = "Beef";
  21.139 +    size_t size = strlen(big);
  21.140 +    const char* result = strnstr(big, little, size);
  21.141 +    TEST_ASSERT_MSG(result == big + 15, result);    
  21.142 +    TEST_ASSERT(true);
  21.143 +}
  21.144 +void StrnstrTests::check_strnstr_match_after_end() {
  21.145 +    const char* big = "EatMoreBeef";
  21.146 +    const char* little = "Beef";
  21.147 +    size_t size = strlen(big);
  21.148 +    const char* result = strnstr(big, little, size - 1);
  21.149 +    TEST_ASSERT_MSG(result == NULL, result);
  21.150 +}
  21.151 +void StrnstrTests::check_strnstr_equal_but_size_too_small() {
  21.152 +    const char* big = "Bob123";
  21.153 +    const char* little = "Bob123";
  21.154 +    size_t size = strlen(big);
  21.155 +    const char* result = strnstr(big, little, size - 1);
  21.156 +    TEST_ASSERT_MSG(result == NULL, result);
  21.157 +}