IOSAD-72 outgoingRating
authorDirk Zimmermann <dirk@pep-project.org>
Wed, 14 Mar 2018 10:57:20 +0100
changeset 528fabf8b220913
parent 527 83824eeb44ba
child 529 51fc488efa4f
IOSAD-72 outgoingRating
pEpObjCAdapter/PEPInternalSession.m
pEpObjCAdapter/PEPSession.m
pEpObjCAdapter/PEPSessionProtocol.h
pEpObjCTests/PEPSessionTest.m
     1.1 --- a/pEpObjCAdapter/PEPInternalSession.m	Wed Mar 14 08:33:05 2018 +0100
     1.2 +++ b/pEpObjCAdapter/PEPInternalSession.m	Wed Mar 14 10:57:20 2018 +0100
     1.3 @@ -363,23 +363,31 @@
     1.4      return encrypted;
     1.5  }
     1.6  
     1.7 -- (PEP_rating)outgoingMessageColor:(PEPDict *)msg
     1.8 +- (BOOL)outgoingRating:(PEP_rating * _Nonnull)rating
     1.9 +        forMessageDict:(PEPDict * _Nonnull)messageDict
    1.10 +                 error:(NSError * _Nullable * _Nullable)error
    1.11  {
    1.12 -    message *_msg = PEP_messageDictToStruct(msg);
    1.13 -    PEP_rating color = PEP_rating_undefined;
    1.14 +    message *_msg = PEP_messageDictToStruct(messageDict);
    1.15 +    *rating = PEP_rating_undefined;
    1.16  
    1.17      [self lockWrite];
    1.18 -    outgoing_message_rating(_session, _msg, &color);
    1.19 +    PEP_STATUS status = outgoing_message_rating(_session, _msg, rating);
    1.20      [self unlockWrite];
    1.21  
    1.22      free_message(_msg);
    1.23  
    1.24 -    return color;
    1.25 +    if ([NSError setError:error fromPEPStatus:status]) {
    1.26 +        return NO;
    1.27 +    }
    1.28 +
    1.29 +    return YES;
    1.30  }
    1.31  
    1.32 -- (PEP_rating)outgoingColorForMessage:(nonnull PEPMessage *)message
    1.33 +- (BOOL)outgoingRating:(PEP_rating * _Nonnull)rating
    1.34 +            forMessage:(PEPMessage * _Nonnull)message
    1.35 +                 error:(NSError * _Nullable * _Nullable)error
    1.36  {
    1.37 -    return [self outgoingMessageColor:(NSDictionary *) message];
    1.38 +    return [self outgoingRating:rating forMessageDict:(NSDictionary *) message error:error];
    1.39  }
    1.40  
    1.41  - (PEP_rating)identityRating:(nonnull PEPIdentity *)identity
     2.1 --- a/pEpObjCAdapter/PEPSession.m	Wed Mar 14 08:33:05 2018 +0100
     2.2 +++ b/pEpObjCAdapter/PEPSession.m	Wed Mar 14 10:57:20 2018 +0100
     2.3 @@ -126,10 +126,12 @@
     2.4      return [session encryptMessage:message identity:identity status:status error:error];
     2.5  }
     2.6  
     2.7 -- (PEP_rating)outgoingColorForMessage:(nonnull PEPMessage *)message
     2.8 +- (BOOL)outgoingRating:(PEP_rating * _Nonnull)rating
     2.9 +            forMessage:(PEPMessage * _Nonnull)message
    2.10 +                 error:(NSError * _Nullable * _Nullable)error
    2.11  {
    2.12      PEPInternalSession *session = [PEPSessionProvider session];
    2.13 -    return [session outgoingColorForMessage:message];
    2.14 +    return [session outgoingRating:rating forMessage:message error:error];
    2.15  }
    2.16  
    2.17  - (PEP_rating)identityRating:(nonnull PEPIdentity *)identity
     3.1 --- a/pEpObjCAdapter/PEPSessionProtocol.h	Wed Mar 14 08:33:05 2018 +0100
     3.2 +++ b/pEpObjCAdapter/PEPSessionProtocol.h	Wed Mar 14 10:57:20 2018 +0100
     3.3 @@ -85,7 +85,9 @@
     3.4                                     error:(NSError * _Nullable * _Nullable)error;
     3.5  
     3.6  /** Determine the status color of a message to be sent */
     3.7 -- (PEP_rating)outgoingColorForMessage:(nonnull PEPMessage *)message;
     3.8 +- (BOOL)outgoingRating:(PEP_rating * _Nonnull)rating
     3.9 +            forMessage:(PEPMessage * _Nonnull)message
    3.10 +                 error:(NSError * _Nullable * _Nullable)error;
    3.11  
    3.12  /**
    3.13   Determine the rating of an identity.
     4.1 --- a/pEpObjCTests/PEPSessionTest.m	Wed Mar 14 08:33:05 2018 +0100
     4.2 +++ b/pEpObjCTests/PEPSessionTest.m	Wed Mar 14 10:57:20 2018 +0100
     4.3 @@ -384,9 +384,12 @@
     4.4          msgGray.longMessage = @"This is a text content";
     4.5          msgGray.direction = PEP_dir_outgoing;
     4.6  
     4.7 +        NSError *error = nil;
     4.8 +
     4.9          // Test with unknown Bob
    4.10 -        PEP_rating clr = [session outgoingColorForMessage:msgGray];
    4.11 -        XCTAssertEqual(clr, PEP_rating_unencrypted);
    4.12 +        PEP_rating rating;
    4.13 +        XCTAssertTrue([session outgoingRating:&rating forMessage:msgGray error:&error]);
    4.14 +        XCTAssertEqual(rating, PEP_rating_unencrypted);
    4.15      }
    4.16  
    4.17      PEPIdentity *identBob = [self
    4.18 @@ -404,29 +407,33 @@
    4.19      msg.longMessage = @"This is a text content";
    4.20      msg.direction = PEP_dir_outgoing;
    4.21  
    4.22 +    NSError *error = nil;
    4.23 +
    4.24      // Should be yellow, since no handshake happened.
    4.25 -    PEP_rating clr = [session outgoingColorForMessage:msg];
    4.26 -    XCTAssertEqual(clr, PEP_rating_reliable);
    4.27 +    PEP_rating rating;
    4.28 +    XCTAssertTrue([session outgoingRating:&rating forMessage:msg error:&error]);
    4.29 +    XCTAssertNil(error);
    4.30 +    XCTAssertEqual(rating, PEP_rating_reliable);
    4.31  
    4.32 -    clr = [session identityRating:identBob];
    4.33 -    XCTAssertEqual(clr, PEP_rating_reliable);
    4.34 +    rating = [session identityRating:identBob];
    4.35 +    XCTAssertEqual(rating, PEP_rating_reliable);
    4.36  
    4.37      // Let' say we got that handshake, set PEP_ct_confirmed in Bob's identity
    4.38      [session trustPersonalKey:identBob];
    4.39  
    4.40      // This time it should be green
    4.41 -    clr = [session outgoingColorForMessage:msg];
    4.42 -    XCTAssertEqual(clr, PEP_rating_trusted);
    4.43 +    XCTAssertTrue([session outgoingRating:&rating forMessage:msg error:&error]);
    4.44 +    XCTAssertEqual(rating, PEP_rating_trusted);
    4.45  
    4.46 -    clr = [session identityRating:identBob];
    4.47 -    XCTAssertEqual(clr, PEP_rating_trusted);
    4.48 +    rating = [session identityRating:identBob];
    4.49 +    XCTAssertEqual(rating, PEP_rating_trusted);
    4.50  
    4.51      // Let' say we undo handshake
    4.52      [session keyResetTrust:identBob];
    4.53  
    4.54      // Yellow ?
    4.55 -    clr = [session outgoingColorForMessage:msg];
    4.56 -    XCTAssertEqual(clr, PEP_rating_reliable);
    4.57 +    XCTAssertTrue([session outgoingRating:&rating forMessage:msg error:&error]);
    4.58 +    XCTAssertEqual(rating, PEP_rating_reliable);
    4.59  
    4.60      // mistrust Bob
    4.61      [session keyMistrusted:identBob];
    4.62 @@ -436,8 +443,8 @@
    4.63      XCTAssertNil(identBob.fingerPrint);
    4.64  
    4.65      // Gray == PEP_rating_unencrypted
    4.66 -    clr = [session outgoingColorForMessage:msg];
    4.67 -    XCTAssertEqual(clr, PEP_rating_unencrypted);
    4.68 +    XCTAssertTrue([session outgoingRating:&rating forMessage:msg error:&error]);
    4.69 +    XCTAssertEqual(rating, PEP_rating_unencrypted);
    4.70  
    4.71      // Undo
    4.72      [session undoLastMistrust];
    4.73 @@ -445,18 +452,18 @@
    4.74      XCTAssertNotNil(identBob.fingerPrint);
    4.75  
    4.76      // Back to yellow
    4.77 -    clr = [session outgoingColorForMessage:msg];
    4.78 +    XCTAssertTrue([session outgoingRating:&rating forMessage:msg error:&error]);
    4.79  
    4.80      // After ENGINE-371 has been fixed, this should be just PEP_rating_reliable
    4.81 -    XCTAssertEqual(clr, PEP_rating_trusted);
    4.82 +    XCTAssertEqual(rating, PEP_rating_trusted);
    4.83      XCTAssertEqual([session identityRating:identBob], PEP_rating_trusted);
    4.84  
    4.85      // Trust again
    4.86      [session trustPersonalKey:identBob];
    4.87  
    4.88      // Back to green
    4.89 -    clr = [session outgoingColorForMessage:msg];
    4.90 -    XCTAssertEqual(clr, PEP_rating_trusted);
    4.91 +    XCTAssertTrue([session outgoingRating:&rating forMessage:msg error:&error]);
    4.92 +    XCTAssertEqual(rating, PEP_rating_trusted);
    4.93  
    4.94      // Now let see if it turns back yellow if we add an unconfirmed folk.
    4.95      // pEp Test John (test key, don't use) <pep.test.john@pep-project.org>
    4.96 @@ -474,10 +481,9 @@
    4.97      msg.cc = @[[PEPTestUtils foreignPepIdentityWithAddress:@"pep.test.john@pep-project.org"
    4.98                                                    userName:@"pEp Test John"]];
    4.99      // Yellow ?
   4.100 -    clr = [session outgoingColorForMessage:msg];
   4.101 -    XCTAssertEqual(clr, PEP_rating_reliable);
   4.102 +    XCTAssertTrue([session outgoingRating:&rating forMessage:msg error:&error]);
   4.103 +    XCTAssertEqual(rating, PEP_rating_reliable);
   4.104  
   4.105 -    NSError *error = nil;
   4.106      PEPMessage *encMsg = [session encryptMessage:msg extraKeys:nil status:nil error:&error];
   4.107      XCTAssertNotNil(encMsg);
   4.108      XCTAssertNil(error);
   4.109 @@ -513,9 +519,12 @@
   4.110      msg.longMessage = @"This is a text content";
   4.111      msg.direction = PEP_dir_outgoing;
   4.112  
   4.113 +    NSError *error = nil;
   4.114 +
   4.115      // Test with unknown Bob
   4.116 -    PEP_rating clr = [session outgoingColorForMessage:msg];
   4.117 -    XCTAssertEqual(clr, PEP_rating_unencrypted);
   4.118 +    PEP_rating rating;
   4.119 +    XCTAssertTrue([session outgoingRating:&rating forMessage:msg error:&error]);
   4.120 +    XCTAssertEqual(rating, PEP_rating_unencrypted);
   4.121  
   4.122      // Now let see with bob's pubkey already known
   4.123      // pEp Test Bob (test key, don't use) <pep.test.bob@pep-project.org>
   4.124 @@ -531,21 +540,21 @@
   4.125      [session updateIdentity:identBob];
   4.126  
   4.127      // Should be yellow, since no handshake happened.
   4.128 -    clr = [session outgoingColorForMessage:msg];
   4.129 -    XCTAssertEqual(clr, PEP_rating_reliable);
   4.130 +    XCTAssertTrue([session outgoingRating:&rating forMessage:msg error:&error]);
   4.131 +    XCTAssertEqual(rating, PEP_rating_reliable);
   4.132  
   4.133 -    clr = [session identityRating:identBob];
   4.134 -    XCTAssertEqual(clr, PEP_rating_reliable);
   4.135 +    rating = [session identityRating:identBob];
   4.136 +    XCTAssertEqual(rating, PEP_rating_reliable);
   4.137  
   4.138      // Let' say we got that handshake, set PEP_ct_confirmed in Bob's identity
   4.139      [session trustPersonalKey:identBob];
   4.140  
   4.141      // This time it should be green
   4.142 -    clr = [session outgoingColorForMessage:msg];
   4.143 -    XCTAssertEqual(clr, PEP_rating_trusted);
   4.144 +    XCTAssertTrue([session outgoingRating:&rating forMessage:msg error:&error]);
   4.145 +    XCTAssertEqual(rating, PEP_rating_trusted);
   4.146  
   4.147 -    clr = [session identityRating:identBob];
   4.148 -    XCTAssertEqual(clr, PEP_rating_trusted);
   4.149 +    rating = [session identityRating:identBob];
   4.150 +    XCTAssertEqual(rating, PEP_rating_trusted);
   4.151  
   4.152      // Now let see if it turns back yellow if we add an unconfirmed folk.
   4.153      // pEp Test John (test key, don't use) <pep.test.john@pep-project.org>
   4.154 @@ -564,17 +573,17 @@
   4.155                                                userID:@"101" userName:@"pEp Test John" isOwn:NO]];
   4.156  
   4.157      // Yellow ?
   4.158 -    clr = [session outgoingColorForMessage:msg];
   4.159 -    XCTAssertEqual(clr, PEP_rating_reliable);
   4.160 +    XCTAssertTrue([session outgoingRating:&rating forMessage:msg error:&error]);
   4.161 +    XCTAssertEqual(rating, PEP_rating_reliable);
   4.162  
   4.163      [session trustPersonalKey:identJohn];
   4.164  
   4.165      // This time it should be green
   4.166 -    clr = [session outgoingColorForMessage:msg];
   4.167 -    XCTAssertEqual(clr, PEP_rating_trusted);
   4.168 +    XCTAssertTrue([session outgoingRating:&rating forMessage:msg error:&error]);
   4.169 +    XCTAssertEqual(rating, PEP_rating_trusted);
   4.170  
   4.171 -    clr = [session identityRating:identJohn];
   4.172 -    XCTAssertEqual(clr, PEP_rating_trusted);
   4.173 +    rating = [session identityRating:identJohn];
   4.174 +    XCTAssertEqual(rating, PEP_rating_trusted);
   4.175  }
   4.176  
   4.177  - (void)testDontEncryptForMistrusted
   4.178 @@ -618,11 +627,13 @@
   4.179      msg.longMessage = @"This is a text content";
   4.180      msg.direction = PEP_dir_outgoing;
   4.181  
   4.182 +    NSError *error = nil;
   4.183 +
   4.184      // Gray == PEP_rating_unencrypted
   4.185 -    PEP_rating clr = [session outgoingColorForMessage:msg];
   4.186 -    XCTAssertEqual(clr, PEP_rating_unencrypted);
   4.187 +    PEP_rating rating;
   4.188 +    XCTAssertTrue([session outgoingRating:&rating forMessage:msg error:&error]);
   4.189 +    XCTAssertEqual(rating, PEP_rating_unencrypted);
   4.190  
   4.191 -    NSError *error = nil;
   4.192      PEPMessage *encMsg = [session encryptMessage:msg extraKeys:nil status:nil error:&error];
   4.193      XCTAssertNotNil(encMsg);
   4.194      XCTAssertNil(error);
   4.195 @@ -687,10 +698,12 @@
   4.196      msg.longMessage = @"This is a text content";
   4.197      msg.direction = PEP_dir_outgoing;
   4.198  
   4.199 -    PEP_rating clr = [session outgoingColorForMessage:msg];
   4.200 -    XCTAssertEqual(clr, PEP_rating_trusted_and_anonymized);
   4.201 +    NSError *error = nil;
   4.202  
   4.203 -    NSError *error = nil;
   4.204 +    PEP_rating rating;
   4.205 +    XCTAssertTrue([session outgoingRating:&rating forMessage:msg error:&error]);
   4.206 +    XCTAssertEqual(rating, PEP_rating_trusted_and_anonymized);
   4.207 +
   4.208      PEPMessage *encMsg = [session encryptMessage:msg extraKeys:nil status:nil error:&error];
   4.209      XCTAssertNotNil(encMsg);
   4.210      XCTAssertNil(error);
   4.211 @@ -700,13 +713,13 @@
   4.212      error = nil;
   4.213      PEPMessage *decmsg = [session
   4.214                            decryptMessage:encMsg
   4.215 -                          rating:&clr
   4.216 +                          rating:&rating
   4.217                            extraKeys:&keys
   4.218                            status:nil
   4.219                            error:&error];
   4.220      XCTAssertNotNil(decmsg);
   4.221      XCTAssertNil(error);
   4.222 -    XCTAssertEqual(clr, PEP_rating_trusted_and_anonymized);
   4.223 +    XCTAssertEqual(rating, PEP_rating_trusted_and_anonymized);
   4.224  }
   4.225  
   4.226  - (void)testEncryptedMailFromMutt
   4.227 @@ -899,10 +912,12 @@
   4.228      msg.longMessage = @"Alice?";
   4.229      msg.direction = PEP_dir_outgoing;
   4.230  
   4.231 -    PEP_rating clr = [session outgoingColorForMessage:msg];
   4.232 -    XCTAssertEqual(clr, PEP_rating_unencrypted);
   4.233 +    NSError *error = nil;
   4.234  
   4.235 -    NSError *error = nil;
   4.236 +    PEP_rating rating;
   4.237 +    XCTAssertTrue([session outgoingRating:&rating forMessage:msg error:&error]);
   4.238 +    XCTAssertEqual(rating, PEP_rating_unencrypted);
   4.239 +
   4.240      PEPMessage *encMsg = [session encryptMessage:msg extraKeys:nil status:nil error:&error];
   4.241      XCTAssertNotNil(encMsg);
   4.242      XCTAssertNil(error);
   4.243 @@ -1195,13 +1210,15 @@
   4.244      msg.longMessage = @"Alice?";
   4.245      msg.direction = PEP_dir_outgoing;
   4.246  
   4.247 -    PEP_rating clr = [session outgoingColorForMessage:msg];
   4.248 -    XCTAssertEqual(clr, PEP_rating_reliable);
   4.249 +    NSError *error = nil;
   4.250 +
   4.251 +    PEP_rating rating;
   4.252 +    XCTAssertTrue([session outgoingRating:&rating forMessage:msg error:&error]);
   4.253 +    XCTAssertEqual(rating, PEP_rating_reliable);
   4.254  
   4.255      PEPMessage *encMsg;
   4.256  
   4.257      PEP_STATUS statusEnc = PEP_VERSION_MISMATCH;
   4.258 -    NSError *error = nil;
   4.259      if (toSelf) {
   4.260          encMsg = [session encryptMessage:msg identity:identMe status:&statusEnc error:&error];
   4.261          XCTAssertEqual(statusEnc, PEP_STATUS_OK);