IOSAD-72 encrypt for identity error handling
authorDirk Zimmermann <dirk@pep-project.org>
Wed, 14 Mar 2018 08:33:04 +0100
changeset 52693eed7194515
parent 525 4ac6cca59c29
child 527 83824eeb44ba
IOSAD-72 encrypt for identity error handling
pEpObjCAdapter/PEPInternalSession.m
pEpObjCAdapter/PEPSession.m
pEpObjCAdapter/PEPSessionProtocol.h
pEpObjCTests/PEPSessionTest.m
     1.1 --- a/pEpObjCAdapter/PEPInternalSession.m	Tue Mar 13 10:51:59 2018 +0100
     1.2 +++ b/pEpObjCAdapter/PEPInternalSession.m	Wed Mar 14 08:33:04 2018 +0100
     1.3 @@ -303,21 +303,34 @@
     1.4              error:error];
     1.5  }
     1.6  
     1.7 -- (PEP_STATUS)encryptMessageDict:(nonnull PEPDict *)src
     1.8 -                        identity:(nonnull PEPIdentity *)identity
     1.9 -                            dest:(PEPDict * _Nullable * _Nullable)dst
    1.10 +- (PEPDict * _Nullable)encryptMessageDict:(nonnull PEPDict *)messageDict
    1.11 +                                 identity:(nonnull PEPIdentity *)identity
    1.12 +                                   status:(PEP_STATUS * _Nullable)status
    1.13 +                                    error:(NSError * _Nullable * _Nullable)error
    1.14  {
    1.15 -    PEP_STATUS status;
    1.16      PEP_encrypt_flags_t flags = 0;
    1.17  
    1.18 -    message *_src = PEP_messageDictToStruct([self removeEmptyRecipients:src]);
    1.19 +    message *_src = PEP_messageDictToStruct([self removeEmptyRecipients:messageDict]);
    1.20      pEp_identity *ident = PEP_identityToStruct(identity);
    1.21      message *_dst = NULL;
    1.22  
    1.23      [self lockWrite];
    1.24 -    status = encrypt_message_for_self(_session, ident, _src, &_dst, PEP_enc_PGP_MIME, flags);
    1.25 +    PEP_STATUS theStatus = encrypt_message_for_self(_session,
    1.26 +                                                    ident,
    1.27 +                                                    _src,
    1.28 +                                                    &_dst,
    1.29 +                                                    PEP_enc_PGP_MIME,
    1.30 +                                                    flags);
    1.31      [self unlockWrite];
    1.32  
    1.33 +    if (status) {
    1.34 +        *status = theStatus;
    1.35 +    }
    1.36 +
    1.37 +    if ([NSError setError:error fromPEPStatus:theStatus]) {
    1.38 +        return nil;
    1.39 +    }
    1.40 +
    1.41      NSDictionary *dst_;
    1.42  
    1.43      if (_dst) {
    1.44 @@ -327,29 +340,27 @@
    1.45          dst_ = PEP_messageDictFromStruct(_src);
    1.46      }
    1.47  
    1.48 -    if (dst) {
    1.49 -        *dst = dst_;
    1.50 -    }
    1.51 -
    1.52      free_message(_src);
    1.53      free_message(_dst);
    1.54      free_identity(ident);
    1.55  
    1.56 -    return status;
    1.57 +    return dst_;
    1.58  }
    1.59  
    1.60 -- (PEP_STATUS)encryptMessage:(nonnull PEPMessage *)src
    1.61 -                    identity:(nonnull PEPIdentity *)identity
    1.62 -                        dest:(PEPMessage * _Nullable * _Nullable)dst
    1.63 +- (PEPMessage * _Nullable)encryptMessage:(nonnull PEPMessage *)message
    1.64 +                                identity:(nonnull PEPIdentity *)identity
    1.65 +                                  status:(PEP_STATUS * _Nullable)status
    1.66 +                                   error:(NSError * _Nullable * _Nullable)error
    1.67  {
    1.68 -    PEPDict *target;
    1.69 -    PEP_STATUS status = [self encryptMessageDict:src.dictionary identity:identity dest:&target];
    1.70 -    if (dst) {
    1.71 -        PEPMessage *encrypted = [PEPMessage new];
    1.72 -        [encrypted setValuesForKeysWithDictionary:target];
    1.73 -        *dst = encrypted;
    1.74 -    }
    1.75 -    return status;
    1.76 +    PEPDict *target = [self
    1.77 +                       encryptMessageDict:message.dictionary
    1.78 +                       identity:identity
    1.79 +                       status:status
    1.80 +                       error:error];
    1.81 +
    1.82 +    PEPMessage *encrypted = [PEPMessage new];
    1.83 +    [encrypted setValuesForKeysWithDictionary:target];
    1.84 +    return encrypted;
    1.85  }
    1.86  
    1.87  - (PEP_rating)outgoingMessageColor:(PEPDict *)msg
     2.1 --- a/pEpObjCAdapter/PEPSession.m	Tue Mar 13 10:51:59 2018 +0100
     2.2 +++ b/pEpObjCAdapter/PEPSession.m	Wed Mar 14 08:33:04 2018 +0100
     2.3 @@ -108,20 +108,22 @@
     2.4      return [session encryptMessage:message extraKeys:extraKeys status:status error:error];
     2.5  }
     2.6  
     2.7 -- (PEP_STATUS)encryptMessageDict:(nonnull PEPDict *)messageDict
     2.8 -                        identity:(nonnull PEPIdentity *)identity
     2.9 -                            dest:(PEPDict * _Nullable * _Nullable)dst
    2.10 +- (PEPDict * _Nullable)encryptMessageDict:(nonnull PEPDict *)messageDict
    2.11 +                                 identity:(nonnull PEPIdentity *)identity
    2.12 +                                   status:(PEP_STATUS * _Nullable)status
    2.13 +                                    error:(NSError * _Nullable * _Nullable)error
    2.14  {
    2.15      PEPInternalSession *session = [PEPSessionProvider session];
    2.16 -    return [session encryptMessageDict:messageDict identity:identity dest:dst];
    2.17 +    return [session encryptMessageDict:messageDict identity:identity status:status error:error];
    2.18  }
    2.19  
    2.20 -- (PEP_STATUS)encryptMessage:(nonnull PEPMessage *)message
    2.21 -                    identity:(nonnull PEPIdentity *)identity
    2.22 -                        dest:(PEPMessage * _Nullable * _Nullable)dst
    2.23 +- (PEPMessage * _Nullable)encryptMessage:(nonnull PEPMessage *)message
    2.24 +                                identity:(nonnull PEPIdentity *)identity
    2.25 +                                  status:(PEP_STATUS * _Nullable)status
    2.26 +                                   error:(NSError * _Nullable * _Nullable)error
    2.27  {
    2.28      PEPInternalSession *session = [PEPSessionProvider session];
    2.29 -    return [session encryptMessage:message identity:identity dest:dst];
    2.30 +    return [session encryptMessage:message identity:identity status:status error:error];
    2.31  }
    2.32  
    2.33  - (PEP_rating)outgoingColorForMessage:(nonnull PEPMessage *)message
     3.1 --- a/pEpObjCAdapter/PEPSessionProtocol.h	Tue Mar 13 10:51:59 2018 +0100
     3.2 +++ b/pEpObjCAdapter/PEPSessionProtocol.h	Wed Mar 14 08:33:04 2018 +0100
     3.3 @@ -72,15 +72,17 @@
     3.4                                    status:(PEP_STATUS * _Nullable)status
     3.5                                     error:(NSError * _Nullable * _Nullable)error;
     3.6  
     3.7 -/** Encrypt a message for the given own identity */
     3.8 -- (PEP_STATUS)encryptMessageDict:(nonnull PEPDict *)mesageDict
     3.9 -                        identity:(nonnull PEPIdentity *)identity
    3.10 -                            dest:(PEPDict * _Nullable * _Nullable)dst __deprecated;
    3.11 +/** Encrypt a message dict for the given own identity */
    3.12 +- (PEPDict * _Nullable)encryptMessageDict:(nonnull PEPDict *)messageDict
    3.13 +                                 identity:(nonnull PEPIdentity *)identity
    3.14 +                                   status:(PEP_STATUS * _Nullable)status
    3.15 +                                    error:(NSError * _Nullable * _Nullable)error __deprecated;
    3.16  
    3.17  /** Encrypt a message for the given own identity */
    3.18 -- (PEP_STATUS)encryptMessage:(nonnull PEPMessage *)message
    3.19 -                    identity:(nonnull PEPIdentity *)identity
    3.20 -                        dest:(PEPMessage * _Nullable * _Nullable)dst;
    3.21 +- (PEPMessage * _Nullable)encryptMessage:(nonnull PEPMessage *)message
    3.22 +                                identity:(nonnull PEPIdentity *)identity
    3.23 +                                  status:(PEP_STATUS * _Nullable)status
    3.24 +                                   error:(NSError * _Nullable * _Nullable)error;
    3.25  
    3.26  /** Determine the status color of a message to be sent */
    3.27  - (PEP_rating)outgoingColorForMessage:(nonnull PEPMessage *)message;
     4.1 --- a/pEpObjCTests/PEPSessionTest.m	Tue Mar 13 10:51:59 2018 +0100
     4.2 +++ b/pEpObjCTests/PEPSessionTest.m	Wed Mar 14 08:33:04 2018 +0100
     4.3 @@ -312,9 +312,15 @@
     4.4          msg.shortMessage = @"The subject";
     4.5          msg.longMessage = @"Lots and lots of text";
     4.6          msg.direction = PEP_dir_outgoing;
     4.7 -        PEPMessage *encMsg;
     4.8 -        PEP_STATUS status = [innerSession encryptMessage:msg identity:me dest:&encMsg];
     4.9 +
    4.10 +        PEP_STATUS status;
    4.11 +        NSError *error = nil;
    4.12 +        PEPMessage *encMsg = [innerSession
    4.13 +                              encryptMessage:msg
    4.14 +                              identity:me
    4.15 +                              status:&status error:&error];
    4.16          XCTAssertEqual(status, PEP_STATUS_OK);
    4.17 +        XCTAssertNotNil(encMsg);
    4.18      };
    4.19  
    4.20      dispatch_group_t backgroundGroup = dispatch_group_create();
    4.21 @@ -1098,8 +1104,10 @@
    4.22                                   shortMessage:shortMessage
    4.23                                    longMessage:longMessage
    4.24                                       outgoing:YES];
    4.25 -    PEPMessage *encMessage;
    4.26 -    [session encryptMessage:mail identity:me dest:&encMessage];
    4.27 +    NSError *error = nil;
    4.28 +    PEP_STATUS status = PEP_UNKNOWN_ERROR;
    4.29 +    PEPMessage *encMessage = [session encryptMessage:mail identity:me status:&status error:&error];
    4.30 +    XCTAssertNil(error);
    4.31  
    4.32      return encMessage;
    4.33  }
    4.34 @@ -1118,13 +1126,14 @@
    4.35      NSString *longMessage = @"Oh, this is a long body text!";
    4.36      PEPMessage *mail = [PEPTestUtils mailFrom:me toIdent:me shortMessage:shortMessage longMessage:longMessage outgoing:YES];
    4.37  
    4.38 -    PEPMessage *encMessage;
    4.39 -    PEP_STATUS status = [session encryptMessage:mail identity:me dest:&encMessage];
    4.40 +    PEP_STATUS status;
    4.41 +    NSError *error = nil;
    4.42 +    PEPMessage *encMessage = [session encryptMessage:mail identity:me status:&status error:&error];
    4.43      XCTAssertEqual(status, 0);
    4.44      XCTAssertEqualObjects(encMessage.shortMessage, @"p≡p");
    4.45  
    4.46      PEP_rating rating;
    4.47 -    NSError *error;
    4.48 +    error = nil;
    4.49      PEPMessage *unencDict = [session
    4.50                               decryptMessage:encMessage
    4.51                               rating:&rating
    4.52 @@ -1186,11 +1195,11 @@
    4.53      PEPMessage *encMsg;
    4.54  
    4.55      PEP_STATUS statusEnc = PEP_VERSION_MISMATCH;
    4.56 +    NSError *error = nil;
    4.57      if (toSelf) {
    4.58 -        statusEnc = [session encryptMessage:msg identity:identMe dest:&encMsg];
    4.59 +        encMsg = [session encryptMessage:msg identity:identMe status:&statusEnc error:&error];
    4.60          XCTAssertEqual(statusEnc, PEP_STATUS_OK);
    4.61      } else {
    4.62 -        NSError *error = nil;
    4.63          encMsg = [session encryptMessage:msg extraKeys:nil status:nil error:&error];
    4.64          XCTAssertNotNil(encMsg);
    4.65          XCTAssertNil(error);
    4.66 @@ -1199,7 +1208,7 @@
    4.67  
    4.68      PEPStringList *keys;
    4.69      PEP_rating pEpRating;
    4.70 -    NSError *error;
    4.71 +    error = nil;
    4.72      PEPMessage *decMsg = [session
    4.73                            decryptMessage:encMsg
    4.74                            rating:&pEpRating