IOSAD-72 encrypt (outgoing) error handling
authorDirk Zimmermann <dirk@pep-project.org>
Mon, 12 Mar 2018 12:08:38 +0100
changeset 522534d277b410d
parent 521 2a389a31c6a8
child 523 3d167bef8afa
IOSAD-72 encrypt (outgoing) error handling
pEpObjCAdapter/PEPInternalSession.m
pEpObjCAdapter/PEPSession.m
pEpObjCAdapter/PEPSessionProtocol.h
pEpObjCTests/PEPSessionTest.m
     1.1 --- a/pEpObjCAdapter/PEPInternalSession.m	Fri Mar 09 11:58:42 2018 +0100
     1.2 +++ b/pEpObjCAdapter/PEPInternalSession.m	Mon Mar 12 12:08:38 2018 +0100
     1.3 @@ -96,7 +96,7 @@
     1.4  
     1.5  #pragma mark - PEPSessionProtocol
     1.6  
     1.7 -- (PEPDict * _Nullable)decryptMessageDict:(nonnull PEPDict *)src
     1.8 +- (PEPDict * _Nullable)decryptMessageDict:(nonnull PEPDict *)messageDict
     1.9                                     rating:(PEP_rating * _Nullable)rating
    1.10                                       keys:(PEPStringList * _Nullable * _Nullable)keys
    1.11                                      error:(NSError * _Nullable * _Nullable)error
    1.12 @@ -105,7 +105,7 @@
    1.13          *rating = PEP_rating_undefined;
    1.14      }
    1.15  
    1.16 -    message *_src = PEP_messageDictToStruct(src);
    1.17 +    message *_src = PEP_messageDictToStruct(messageDict);
    1.18      message *_dst = NULL;
    1.19      stringlist_t *_keys = NULL;
    1.20      PEP_decrypt_flags_t flags = 0;
    1.21 @@ -218,22 +218,25 @@
    1.22      return dest;
    1.23  }
    1.24  
    1.25 -- (PEP_STATUS)encryptMessageDict:(nonnull PEPDict *)src
    1.26 -                           extra:(nullable NSArray *)keys
    1.27 -                       encFormat:(PEP_enc_format)encFormat
    1.28 -                            dest:(PEPDict * _Nullable * _Nullable)dst
    1.29 +- (PEPDict * _Nullable)encryptMessageDict:(nonnull PEPDict *)messageDict
    1.30 +                                extraKeys:(nullable PEPStringList *)extraKeys
    1.31 +                                encFormat:(PEP_enc_format)encFormat
    1.32 +                                    error:(NSError * _Nullable * _Nullable)error
    1.33  {
    1.34 -    PEP_STATUS status;
    1.35      PEP_encrypt_flags_t flags = 0;
    1.36  
    1.37 -    message *_src = PEP_messageDictToStruct([self removeEmptyRecipients:src]);
    1.38 +    message *_src = PEP_messageDictToStruct([self removeEmptyRecipients:messageDict]);
    1.39      message *_dst = NULL;
    1.40 -    stringlist_t *_keys = PEP_arrayToStringlist(keys);
    1.41 +    stringlist_t *_keys = PEP_arrayToStringlist(extraKeys);
    1.42  
    1.43      [self lockWrite];
    1.44 -    status = encrypt_message(_session, _src, _keys, &_dst, encFormat, flags);
    1.45 +    PEP_STATUS status = encrypt_message(_session, _src, _keys, &_dst, encFormat, flags);
    1.46      [self unlockWrite];
    1.47  
    1.48 +    if ([NSError setError:error fromPEPStatus:status]) {
    1.49 +        return nil;
    1.50 +    }
    1.51 +
    1.52      NSDictionary *dst_;
    1.53  
    1.54      if (_dst) {
    1.55 @@ -242,40 +245,33 @@
    1.56      else {
    1.57          dst_ = PEP_messageDictFromStruct(_src);
    1.58      }
    1.59 -    if (dst) {
    1.60 -        *dst = dst_;
    1.61 -    }
    1.62  
    1.63      free_message(_src);
    1.64      free_message(_dst);
    1.65      free_stringlist(_keys);
    1.66  
    1.67 -    return status;
    1.68 +    return dst_;
    1.69  }
    1.70  
    1.71 -- (PEP_STATUS)encryptMessage:(nonnull PEPMessage *)src
    1.72 -                       extra:(nullable PEPStringList *)keys
    1.73 -                   encFormat:(PEP_enc_format)encFormat
    1.74 -                        dest:(PEPMessage * _Nullable * _Nullable)dst
    1.75 +- (PEPMessage * _Nullable)encryptMessage:(nonnull PEPMessage *)message
    1.76 +                                   extraKeys:(nullable PEPStringList *)extraKeys
    1.77 +                               encFormat:(PEP_enc_format)encFormat
    1.78 +                                   error:(NSError * _Nullable * _Nullable)error
    1.79  {
    1.80 -    PEPDict *target;
    1.81 -    PEP_STATUS status = [self encryptMessageDict:(NSDictionary *) src
    1.82 -                                           extra:keys
    1.83 -                                       encFormat: encFormat
    1.84 -                                            dest:&target];
    1.85 -    if (dst) {
    1.86 -        PEPMessage *encrypted = [PEPMessage new];
    1.87 -        [encrypted setValuesForKeysWithDictionary:target];
    1.88 -        *dst = encrypted;
    1.89 -    }
    1.90 -    return status;
    1.91 +    PEPDict *encryptedDict = [self encryptMessageDict:(NSDictionary *) message
    1.92 +                                                extraKeys:extraKeys
    1.93 +                                            encFormat:encFormat
    1.94 +                                                error:error];
    1.95 +    PEPMessage *encrypted = [PEPMessage new];
    1.96 +    [encrypted setValuesForKeysWithDictionary:encryptedDict];
    1.97 +    return encrypted;
    1.98  }
    1.99  
   1.100 -- (PEP_STATUS)encryptMessage:(nonnull PEPMessage *)src
   1.101 -                       extra:(nullable PEPStringList *)keys
   1.102 -                        dest:(PEPMessage * _Nullable * _Nullable)dst
   1.103 +- (PEPMessage * _Nullable)encryptMessage:(nonnull PEPMessage *)message
   1.104 +                                   extraKeys:(nullable PEPStringList *)extraKeys
   1.105 +                                   error:(NSError * _Nullable * _Nullable)error
   1.106  {
   1.107 -    return [self encryptMessage:src extra:keys encFormat:PEP_enc_PEP dest:dst];
   1.108 +    return [self encryptMessage:message extraKeys:extraKeys encFormat:PEP_enc_PEP error:error];
   1.109  }
   1.110  
   1.111  - (PEP_STATUS)encryptMessageDict:(nonnull PEPDict *)src
     2.1 --- a/pEpObjCAdapter/PEPSession.m	Fri Mar 09 11:58:42 2018 +0100
     2.2 +++ b/pEpObjCAdapter/PEPSession.m	Mon Mar 12 12:08:38 2018 +0100
     2.3 @@ -55,30 +55,30 @@
     2.4      return [session reEvaluateMessage:message rating:rating error:error];
     2.5  }
     2.6  
     2.7 -- (PEP_STATUS)encryptMessageDict:(nonnull PEPDict *)src
     2.8 -                           extra:(nullable PEPStringList *)keys
     2.9 -                       encFormat:(PEP_enc_format)encFormat
    2.10 -                            dest:(PEPDict * _Nullable * _Nullable)dst
    2.11 +- (PEPDict * _Nullable)encryptMessageDict:(nonnull PEPDict *)src
    2.12 +                                    extraKeys:(nullable PEPStringList *)extraKeys
    2.13 +                                encFormat:(PEP_enc_format)encFormat
    2.14 +                                    error:(NSError * _Nullable * _Nullable)error
    2.15  {
    2.16      PEPInternalSession *session = [PEPSessionProvider session];
    2.17 -    return [session encryptMessageDict:src extra:keys encFormat:encFormat dest:dst];
    2.18 +    return [session encryptMessageDict:src extraKeys:extraKeys encFormat:encFormat error:error];
    2.19  }
    2.20  
    2.21 -- (PEP_STATUS)encryptMessage:(nonnull PEPMessage *)src
    2.22 -                       extra:(nullable PEPStringList *)keys
    2.23 -                        dest:(PEPMessage * _Nullable * _Nullable)dst
    2.24 +- (PEPMessage * _Nullable)encryptMessage:(nonnull PEPMessage *)src
    2.25 +                                   extraKeys:(nullable PEPStringList *)extraKeys
    2.26 +                               encFormat:(PEP_enc_format)encFormat
    2.27 +                                   error:(NSError * _Nullable * _Nullable)error
    2.28  {
    2.29      PEPInternalSession *session = [PEPSessionProvider session];
    2.30 -    return [session encryptMessage:src extra:keys dest:dst];
    2.31 +    return [session encryptMessage:src extraKeys:extraKeys encFormat:encFormat error:error];
    2.32  }
    2.33  
    2.34 -- (PEP_STATUS)encryptMessage:(nonnull PEPMessage *)src
    2.35 -                       extra:(nullable PEPStringList *)keys
    2.36 -                   encFormat:(PEP_enc_format)encFormat
    2.37 -                        dest:(PEPMessage * _Nullable * _Nullable)dst
    2.38 +- (PEPMessage * _Nullable)encryptMessage:(nonnull PEPMessage *)src
    2.39 +                                   extraKeys:(nullable PEPStringList *)extraKeys
    2.40 +                                   error:(NSError * _Nullable * _Nullable)error
    2.41  {
    2.42      PEPInternalSession *session = [PEPSessionProvider session];
    2.43 -    return [session encryptMessage:src extra:keys encFormat:encFormat dest:dst];
    2.44 +    return [session encryptMessage:src extraKeys:extraKeys error:error];
    2.45  }
    2.46  
    2.47  - (PEP_STATUS)encryptMessageDict:(nonnull PEPDict *)src
     3.1 --- a/pEpObjCAdapter/PEPSessionProtocol.h	Fri Mar 09 11:58:42 2018 +0100
     3.2 +++ b/pEpObjCAdapter/PEPSessionProtocol.h	Mon Mar 12 12:08:38 2018 +0100
     3.3 @@ -42,22 +42,28 @@
     3.4                     rating:(PEP_rating * _Nullable)rating
     3.5                      error:(NSError * _Nullable * _Nullable)error;
     3.6  
     3.7 -/** Encrypt a message dictionary, indicating the encoding format */
     3.8 -- (PEP_STATUS)encryptMessageDict:(nonnull PEPDict *)src
     3.9 -                           extra:(nullable PEPStringList *)keys
    3.10 -                       encFormat:(PEP_enc_format)encFormat
    3.11 -                            dest:(PEPDict * _Nullable * _Nullable)dst __deprecated;
    3.12 +/**
    3.13 + Encrypt a message dictionary, indicating the encoding format.
    3.14 + @note The resulting message dict could be the input one.
    3.15 + */
    3.16 +- (PEPDict * _Nullable)encryptMessageDict:(nonnull PEPDict *)src
    3.17 +                                extraKeys:(nullable PEPStringList *)extraKeys
    3.18 +                                encFormat:(PEP_enc_format)encFormat
    3.19 +                                    error:(NSError * _Nullable * _Nullable)error __deprecated;
    3.20  
    3.21 -/** Encrypt a message, indicating the encoding format */
    3.22 -- (PEP_STATUS)encryptMessage:(nonnull PEPMessage *)src
    3.23 -                       extra:(nullable PEPStringList *)keys
    3.24 -                   encFormat:(PEP_enc_format)encFormat
    3.25 -                        dest:(PEPMessage * _Nullable * _Nullable)dst;
    3.26 +/**
    3.27 + Encrypt a message, indicating the encoding format
    3.28 + @note The resulting message dict could be the input one.
    3.29 + */
    3.30 +- (PEPMessage * _Nullable)encryptMessage:(nonnull PEPMessage *)src
    3.31 +                               extraKeys:(nullable PEPStringList *)extraKeys
    3.32 +                               encFormat:(PEP_enc_format)encFormat
    3.33 +                                   error:(NSError * _Nullable * _Nullable)error;
    3.34  
    3.35  /** Encrypt a message with default encryption format (PEP_enc_PEP) */
    3.36 -- (PEP_STATUS)encryptMessage:(nonnull PEPMessage *)src
    3.37 -                       extra:(nullable PEPStringList *)keys
    3.38 -                        dest:(PEPMessage * _Nullable * _Nullable)dst;
    3.39 +- (PEPMessage * _Nullable)encryptMessage:(nonnull PEPMessage *)src
    3.40 +                               extraKeys:(nullable PEPStringList *)extraKeys
    3.41 +                                   error:(NSError * _Nullable * _Nullable)error;
    3.42  
    3.43  /** Encrypt a message for the given own identity */
    3.44  - (PEP_STATUS)encryptMessageDict:(nonnull PEPDict *)src
    3.45 @@ -66,8 +72,8 @@
    3.46  
    3.47  /** Encrypt a message for the given own identity */
    3.48  - (PEP_STATUS)encryptMessage:(nonnull PEPMessage *)src
    3.49 -                        identity:(nonnull PEPIdentity *)identity
    3.50 -                            dest:(PEPMessage * _Nullable * _Nullable)dst;
    3.51 +                    identity:(nonnull PEPIdentity *)identity
    3.52 +                        dest:(PEPMessage * _Nullable * _Nullable)dst;
    3.53  
    3.54  /** Determine the status color of a message to be sent */
    3.55  - (PEP_rating)outgoingColorForMessage:(nonnull PEPMessage *)message;
     4.1 --- a/pEpObjCTests/PEPSessionTest.m	Fri Mar 09 11:58:42 2018 +0100
     4.2 +++ b/pEpObjCTests/PEPSessionTest.m	Mon Mar 12 12:08:38 2018 +0100
     4.3 @@ -464,14 +464,13 @@
     4.4      clr = [session outgoingColorForMessage:msg];
     4.5      XCTAssertEqual(clr, PEP_rating_reliable);
     4.6  
     4.7 -    PEPMessage *encmsg;
     4.8 -    PEP_STATUS status = [session encryptMessage:msg extra:@[] dest:&encmsg];
     4.9 +    NSError *error = nil;
    4.10 +    PEPMessage *encMsg = [session encryptMessage:msg extraKeys:nil error:&error];
    4.11 +    XCTAssertNotNil(encMsg);
    4.12 +    XCTAssertNil(error);
    4.13  
    4.14 -    XCTAssertNotNil(encmsg);
    4.15 -    XCTAssertEqualObjects(encmsg.shortMessage, @"p≡p");
    4.16 -    XCTAssertTrue([encmsg.longMessage containsString:@"p≡p"]);
    4.17 -
    4.18 -    XCTAssertEqual(status, PEP_STATUS_OK);
    4.19 +    XCTAssertEqualObjects(encMsg.shortMessage, @"p≡p");
    4.20 +    XCTAssertTrue([encMsg.longMessage containsString:@"p≡p"]);
    4.21  }
    4.22  
    4.23  
    4.24 @@ -610,12 +609,12 @@
    4.25      PEP_rating clr = [session outgoingColorForMessage:msg];
    4.26      XCTAssertEqual(clr, PEP_rating_unencrypted);
    4.27  
    4.28 -    PEPMessage *encmsg;
    4.29 -    PEP_STATUS status = [session encryptMessage:msg extra:@[] dest:&encmsg];
    4.30 +    NSError *error = nil;
    4.31 +    PEPMessage *encMsg = [session encryptMessage:msg extraKeys:nil error:&error];
    4.32 +    XCTAssertNotNil(encMsg);
    4.33 +    XCTAssertNil(error);
    4.34  
    4.35 -    XCTAssertEqual(status, PEP_UNENCRYPTED);
    4.36 -
    4.37 -    XCTAssertNotEqualObjects(encmsg.attachments[0][@"mimeType"], @"application/pgp-encrypted");
    4.38 +    XCTAssertNotEqualObjects(encMsg.attachments[0][@"mimeType"], @"application/pgp-encrypted");
    4.39  
    4.40      [self pEpCleanUp];
    4.41  }
    4.42 @@ -678,16 +677,16 @@
    4.43      PEP_rating clr = [session outgoingColorForMessage:msg];
    4.44      XCTAssertEqual(clr, PEP_rating_trusted_and_anonymized);
    4.45  
    4.46 -    PEPMessage *encmsg;
    4.47 -    PEP_STATUS status = [session encryptMessage:msg extra:@[] dest:&encmsg];
    4.48 +    NSError *error = nil;
    4.49 +    PEPMessage *encMsg = [session encryptMessage:msg extraKeys:nil error:&error];
    4.50 +    XCTAssertNotNil(encMsg);
    4.51 +    XCTAssertNil(error);
    4.52  
    4.53 -    XCTAssertEqual(status, PEP_STATUS_OK);
    4.54 +    NSArray *keys;
    4.55  
    4.56 -    NSArray* keys;
    4.57 -
    4.58 -    NSError *error;
    4.59 +    error = nil;
    4.60      PEPMessage *decmsg = [session
    4.61 -                          decryptMessage:encmsg
    4.62 +                          decryptMessage:encMsg
    4.63                            rating:&clr
    4.64                            keys:&keys
    4.65                            error:&error];
    4.66 @@ -888,17 +887,16 @@
    4.67      PEP_rating clr = [session outgoingColorForMessage:msg];
    4.68      XCTAssertEqual(clr, PEP_rating_unencrypted);
    4.69  
    4.70 -    PEPMessage *encMsg;
    4.71 -
    4.72 -    PEP_STATUS statusEnc = statusEnc = [session encryptMessage:msg extra:@[] dest:&encMsg];
    4.73 -
    4.74 -    XCTAssertEqual(statusEnc, PEP_UNENCRYPTED);
    4.75 +    NSError *error = nil;
    4.76 +    PEPMessage *encMsg = [session encryptMessage:msg extraKeys:nil error:&error];
    4.77 +    XCTAssertNotNil(encMsg);
    4.78 +    XCTAssertNil(error);
    4.79  
    4.80      XCTAssertNotNil(encMsg);
    4.81  
    4.82      PEPStringList *keys;
    4.83      PEP_rating pEpRating;
    4.84 -    NSError *error;
    4.85 +    error = nil;
    4.86      PEPMessage *decMsg = [session
    4.87                            decryptMessage:encMsg
    4.88                            rating:&pEpRating
    4.89 @@ -1185,12 +1183,13 @@
    4.90      PEP_STATUS statusEnc = PEP_VERSION_MISMATCH;
    4.91      if (toSelf) {
    4.92          statusEnc = [session encryptMessage:msg identity:identMe dest:&encMsg];
    4.93 +        XCTAssertEqual(statusEnc, PEP_STATUS_OK);
    4.94      } else {
    4.95 -         statusEnc = [session encryptMessage:msg extra:@[] dest:&encMsg];
    4.96 +        NSError *error = nil;
    4.97 +        encMsg = [session encryptMessage:msg extraKeys:nil error:&error];
    4.98 +        XCTAssertNotNil(encMsg);
    4.99 +        XCTAssertNil(error);
   4.100      }
   4.101 -
   4.102 -    XCTAssertEqual(statusEnc, PEP_STATUS_OK);
   4.103 -
   4.104      XCTAssertNotNil(encMsg);
   4.105  
   4.106      PEPStringList *keys;