pEpObjCTests/PEPSessionTest.m
author Dirk Zimmermann <dz@pep.security>
Wed, 20 Mar 2019 16:40:35 +0100
branchIOS-1480
changeset 902 32760267cd59
parent 868 227dc2349e1a
child 905 929095fe2998
child 906 68a503e49d2e
permissions -rw-r--r--
IOS-1480 Avoid framework imports.
     1 //
     2 //  PEPSessionTest.m
     3 //  pEpObjCAdapterTests
     4 //
     5 //  Created by Andreas Buff on 18.01.18.
     6 //  Copyright © 2018 p≡p. All rights reserved.
     7 //
     8 
     9 #import <XCTest/XCTest.h>
    10 
    11 #import "PEPObjCAdapterFramework.h"
    12 
    13 #import "PEPObjCAdapter.h"
    14 #import "PEPMessage.h"
    15 #import "PEPAttachment.h"
    16 #import "PEPTestUtils.h"
    17 #import "PEPSync.h"
    18 #import "PEPSendMessageDelegate.h"
    19 
    20 #import "PEPSessionTestNotifyHandshakeDelegate.h"
    21 #import "PEPSessionTestSendMessageDelegate.h"
    22 
    23 @interface PEPSessionTest : XCTestCase
    24 
    25 @property (nonatomic) PEPSync *sync;
    26 @property (nonatomic) PEPSessionTestSendMessageDelegate *sendMessageDelegate;
    27 @property (nonatomic) PEPSessionTestNotifyHandshakeDelegate *notifyHandshakeDelegate;
    28 
    29 @end
    30 
    31 @implementation PEPSessionTest
    32 
    33 - (void)setUp
    34 {
    35     [super setUp];
    36 
    37     [self pEpCleanUp];
    38 
    39     [self startSync];
    40 
    41     [PEPObjCAdapter setUnEncryptedSubjectEnabled:NO];
    42 }
    43 
    44 - (void)tearDown
    45 {
    46     [self.sync shutdown];
    47 
    48     [self pEpCleanUp];
    49     [super tearDown];
    50 }
    51 
    52 - (void)testTrustWords
    53 {
    54     PEPSession *session = [PEPSession new];
    55 
    56     NSError *error = nil;
    57     NSArray *trustwords = [session
    58                            trustwordsForFingerprint:@"DB47DB47DB47DB47DB47DB47DB47DB47DB47DB47"
    59                            languageID:@"en"
    60                            shortened:false
    61                            error:&error];
    62     XCTAssertNil(error);
    63     XCTAssertEqual([trustwords count], 10);
    64 
    65     for(id word in trustwords)
    66         XCTAssertEqualObjects(word, @"BAPTISMAL");
    67 }
    68 
    69 - (void)testGenKey
    70 {
    71     PEPSession *session = [PEPSession new];
    72 
    73     PEPIdentity *identMe = [[PEPIdentity alloc]
    74                             initWithAddress:@"pep.test.iosgenkey@pep-project.org"
    75                             userID:@"Me"
    76                             userName:@"pEp Test iOS GenKey"
    77                             isOwn:YES];
    78 
    79     NSError *error = nil;
    80     XCTAssertTrue([session mySelf:identMe error:&error]);
    81     XCTAssertNil(error);
    82 
    83     XCTAssertNotNil(identMe.fingerPrint);
    84     XCTAssertNotEqual(identMe.commType, PEP_ct_unknown);
    85 
    86     XCTAssertTrue([identMe isPEPUser:session error:&error]);
    87 }
    88 
    89 - (void)testMySelfCommType
    90 {
    91     PEPSession *session = [PEPSession new];
    92 
    93     PEPIdentity *identMe = [[PEPIdentity alloc]
    94                             initWithAddress:@"pep.test.iosgenkey@pep-project.org"
    95                             userID:@"pep.test.iosgenkey@pep-project.org_userID"
    96                             userName:@"pEp Test iOS GenKey"
    97                             isOwn:YES];
    98 
    99     NSError *error = nil;
   100     XCTAssertTrue([session mySelf:identMe error:&error]);
   101     XCTAssertNil(error);
   102 
   103     XCTAssertNotNil(identMe.fingerPrint);
   104     XCTAssertNotEqual(identMe.commType, PEP_ct_unknown);
   105 
   106     XCTAssertTrue([identMe isPEPUser:session error:&error]);
   107 
   108     dispatch_queue_t queue = dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0);
   109     dispatch_sync(queue, ^{
   110         NSError *innerError = nil;
   111         PEPSession *session2 = [PEPSession new];
   112 
   113         // Now simulate an update from the app, which usually only caches
   114         // kPepUsername, kPepAddress and optionally kPepUserID.
   115         PEPIdentity *identMe2 = [[PEPIdentity alloc]
   116                                  initWithAddress:identMe.address
   117                                  userID:identMe.userID
   118                                  userName:identMe.userName
   119                                  isOwn:NO];
   120 
   121         XCTAssertTrue([session2 mySelf:identMe2 error:&innerError]);
   122         XCTAssertNil(innerError);
   123 
   124         XCTAssertNotNil(identMe2.fingerPrint);
   125         XCTAssertTrue([identMe2 isPEPUser:session error:&innerError]);
   126         XCTAssertEqualObjects(identMe2.fingerPrint, identMe.fingerPrint);
   127 
   128         // Now pretend the app only knows kPepUsername and kPepAddress
   129         PEPIdentity *identMe3 = [PEPTestUtils foreignPepIdentityWithAddress:identMe.address
   130                                                                    userName:identMe.userName];
   131         XCTAssertTrue([session2 mySelf:identMe3 error:&innerError]);
   132         XCTAssertNil(innerError);
   133 
   134         XCTAssertNotNil(identMe3.fingerPrint);
   135         XCTAssertTrue([identMe3 isPEPUser:session error:&innerError]);
   136         XCTAssertEqualObjects(identMe3.fingerPrint, identMe.fingerPrint);
   137 
   138         XCTAssertEqualObjects(identMe.address, identMe2.address);
   139         XCTAssertEqualObjects(identMe.address, identMe3.address);
   140         XCTAssertEqual(identMe.commType, identMe2.commType);
   141         XCTAssertEqual(identMe.commType, identMe3.commType);
   142     });
   143 }
   144 
   145 - (void)testPartnerWithoutFingerPrint
   146 {
   147     PEPSession *session = [PEPSession new];
   148 
   149     PEPIdentity *identRandom = [[PEPIdentity alloc]
   150                                 initWithAddress:@"does_not_exist@example.com"
   151                                 userID:@"got_out"
   152                                 userName:@"No Way Not Even Alice"
   153                                 isOwn:NO];
   154 
   155     NSError *error = nil;
   156     XCTAssertTrue([session updateIdentity:identRandom error:&error]);
   157     XCTAssertNil(error);
   158     XCTAssertNil(identRandom.fingerPrint);
   159 }
   160 
   161 - (void)testImportPartnerKeys
   162 {
   163     XCTAssertNotNil([self checkImportingKeyFilePath:@"6FF00E97_sec.asc"
   164                                             address:@"pep.test.alice@pep-project.org"
   165                                              userID:@"This Is Alice"
   166                                         fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
   167                                             session: nil]);
   168 
   169     XCTAssertNotNil([self checkImportingKeyFilePath:@"0xC9C2EE39.asc"
   170                                             address:@"pep.test.bob@pep-project.org"
   171                                              userID:@"This Is Bob"
   172                                         fingerPrint:@"BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39"
   173                                             session: nil]);
   174 }
   175 
   176 - (void)testIdentityRating
   177 {
   178     PEPSession *session = [PEPSession new];
   179 
   180     PEPIdentity *me = [self
   181                        checkMySelfImportingKeyFilePath:@"6FF00E97_sec.asc"
   182                        address:@"pep.test.alice@pep-project.org"
   183                        userID:@"Alice_User_ID"
   184                        fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
   185                        session:session];
   186     XCTAssertEqual([self ratingForIdentity:me session:session], PEPRatingTrustedAndAnonymized);
   187 
   188     PEPIdentity *alice = [self
   189                           checkImportingKeyFilePath:@"6FF00E97_sec.asc"
   190                           address:@"pep.test.alice@pep-project.org"
   191                           userID:@"This Is Alice"
   192                           fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
   193                           session: session];
   194     XCTAssertNotNil(alice);
   195     XCTAssertEqual([self ratingForIdentity:alice session:session], PEPRatingReliable);
   196 }
   197 
   198 /** ENGINE-409 */
   199 - (void)testIdentityRatingMistrustReset
   200 {
   201     PEPSession *session = [PEPSession new];
   202 
   203     PEPIdentity *me = [[PEPIdentity alloc]
   204                        initWithAddress:@"me@example.org"
   205                        userID:@"me_myself"
   206                        userName:@"Me Me"
   207                        isOwn:YES];
   208 
   209     NSError *error = nil;
   210     XCTAssertTrue([session mySelf:me error:&error]);
   211     XCTAssertNil(error);
   212 
   213     XCTAssertNotNil(me.fingerPrint);
   214     XCTAssertEqual([self ratingForIdentity:me session:session], PEPRatingTrustedAndAnonymized);
   215 
   216     PEPIdentity *alice = [self
   217                           checkImportingKeyFilePath:@"6FF00E97_sec.asc"
   218                           address:@"pep.test.alice@pep-project.org"
   219                           userID:@"This Is Alice"
   220                           fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
   221                           session: session];
   222     XCTAssertNotNil(alice);
   223     XCTAssertEqual([self ratingForIdentity:alice session:session], PEPRatingReliable);
   224 
   225     XCTAssertTrue([session keyMistrusted:alice error:&error]);
   226     XCTAssertNil(error);
   227     XCTAssertEqual([self ratingForIdentity:alice session:session], PEPRatingHaveNoKey);
   228 }
   229 
   230 - (void)testIdentityRatingTrustResetMistrustUndo
   231 {
   232     PEPSession *session = [PEPSession new];
   233 
   234     PEPIdentity *me = [[PEPIdentity alloc]
   235                        initWithAddress:@"me@example.org"
   236                        userID:@"me_myself"
   237                        userName:@"Me Me"
   238                        isOwn:YES];
   239 
   240     NSError *error = nil;
   241     XCTAssertTrue([session mySelf:me error:&error]);
   242     XCTAssertNil(error);
   243 
   244     XCTAssertNotNil(me.fingerPrint);
   245     XCTAssertEqual([self ratingForIdentity:me session:session], PEPRatingTrustedAndAnonymized);
   246 
   247     PEPIdentity *alice = [self
   248                           checkImportingKeyFilePath:@"6FF00E97_sec.asc"
   249                           address:@"pep.test.alice@pep-project.org"
   250                           userID:@"This Is Alice"
   251                           fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
   252                           session: session];
   253     XCTAssertNotNil(alice);
   254     XCTAssertEqual([self ratingForIdentity:alice session:session], PEPRatingReliable);
   255 
   256     XCTAssertTrue([session trustPersonalKey:alice error:&error]);
   257     XCTAssertNil(error);
   258     XCTAssertEqual([self ratingForIdentity:alice session:session], PEPRatingTrusted);
   259 
   260     XCTAssertTrue([session keyResetTrust:alice error:&error]);
   261     XCTAssertNil(error);
   262     XCTAssertEqual([self ratingForIdentity:alice session:session], PEPRatingReliable);
   263 
   264     XCTAssertTrue([session keyMistrusted:alice error:&error]);
   265     XCTAssertNil(error);
   266     XCTAssertEqual([self ratingForIdentity:alice session:session], PEPRatingHaveNoKey);
   267 }
   268 
   269 /** ENGINE-384 */
   270 - (void)testIdentityRatingCrash
   271 {
   272     PEPSession *session = [PEPSession new];
   273 
   274     PEPIdentity *me = [[PEPIdentity alloc]
   275                        initWithAddress:@"me@example.org"
   276                        userID:@"me_myself"
   277                        userName:@"Me Me"
   278                        isOwn:YES];
   279     NSError *error = nil;
   280     XCTAssertTrue([session mySelf:me error:&error]);
   281     XCTAssertNil(error);
   282 
   283     XCTAssertNotNil(me.fingerPrint);
   284     XCTAssertEqual([self ratingForIdentity:me session:session], PEPRatingTrustedAndAnonymized);
   285 
   286     PEPIdentity *alice = [self
   287                           checkImportingKeyFilePath:@"6FF00E97_sec.asc"
   288                           address:@"pep.test.alice@pep-project.org"
   289                           userID:@"This Is Alice"
   290                           fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
   291                           session: session];
   292     XCTAssertNotNil(alice);
   293     XCTAssertEqual([self ratingForIdentity:alice session:session], PEPRatingReliable);
   294 
   295     XCTAssertTrue([session trustPersonalKey:alice error:&error]);
   296     XCTAssertNil(error);
   297     XCTAssertEqual([self ratingForIdentity:alice session:session], PEPRatingTrusted);
   298 
   299     XCTAssertTrue([session keyResetTrust:alice error:&error]);
   300     XCTAssertNil(error);
   301     XCTAssertEqual([self ratingForIdentity:alice session:session], PEPRatingReliable);
   302 
   303     XCTAssertTrue([session keyMistrusted:alice error:&error]);
   304     XCTAssertNil(error);
   305     XCTAssertEqual([self ratingForIdentity:alice session:session], PEPRatingHaveNoKey);
   306 }
   307 
   308 /**
   309  Try to provoke a SQLITE_BUSY (ENGINE-374)
   310  */
   311 - (void)testIdentityRatingTrustResetMistrustUndoBusy
   312 {
   313     PEPSession *session = [PEPSession new];
   314 
   315     PEPIdentity *me = [[PEPIdentity alloc]
   316                        initWithAddress:@"me@example.org"
   317                        userID:@"me_myself"
   318                        userName:@"Me Me"
   319                        isOwn:YES];
   320     NSError *error = nil;
   321     XCTAssertTrue([session mySelf:me error:&error]);
   322     XCTAssertNil(error);
   323 
   324     XCTAssertNotNil(me.fingerPrint);
   325     XCTAssertEqual([self ratingForIdentity:me session:session], PEPRatingTrustedAndAnonymized);
   326 
   327     PEPIdentity *alice = [self
   328                           checkImportingKeyFilePath:@"6FF00E97_sec.asc"
   329                           address:@"pep.test.alice@pep-project.org"
   330                           userID:@"This Is Alice"
   331                           fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
   332                           session: session];
   333     XCTAssertNotNil(alice);
   334     XCTAssertEqual([self ratingForIdentity:alice session:session], PEPRatingReliable);
   335 
   336     void (^encryptingBlock)(void) = ^{
   337         PEPSession *innerSession = [PEPSession new];
   338         PEPMessage *msg = [PEPMessage new];
   339         msg.from = me;
   340         msg.to = @[alice];
   341         msg.shortMessage = @"The subject";
   342         msg.longMessage = @"Lots and lots of text";
   343         msg.direction = PEP_dir_outgoing;
   344 
   345         PEPStatus status;
   346         NSError *error = nil;
   347         PEPMessage *encMsg = [innerSession
   348                               encryptMessage:msg
   349                               forSelf:me
   350                               extraKeys:nil
   351                               status:&status error:&error];
   352         XCTAssertEqual(status, PEPStatusOK);
   353         XCTAssertNotNil(encMsg);
   354     };
   355 
   356     dispatch_group_t backgroundGroup = dispatch_group_create();
   357     dispatch_group_async(backgroundGroup,
   358                          dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), encryptingBlock);
   359 
   360     XCTAssertTrue([session trustPersonalKey:alice error:&error]);
   361     XCTAssertNil(error);
   362     XCTAssertEqual([self ratingForIdentity:alice session:session], PEPRatingTrusted);
   363 
   364     XCTAssertTrue([session keyResetTrust:alice error:&error]);
   365     XCTAssertNil(error);
   366     XCTAssertEqual([self ratingForIdentity:alice session:session], PEPRatingReliable);
   367 
   368     XCTAssertTrue([session keyMistrusted:alice error:&error]);
   369     XCTAssertNil(error);
   370     XCTAssertEqual([self ratingForIdentity:alice session:session], PEPRatingHaveNoKey);
   371 }
   372 
   373 - (void)testOutgoingColors
   374 {
   375     PEPSession *session = [PEPSession new];
   376 
   377     // Our test user :
   378     // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
   379     // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
   380     XCTAssertTrue([PEPTestUtils importBundledKey:@"6FF00E97_sec.asc" session:session]);
   381 
   382     // Our test user :
   383     PEPIdentity *identAlice = [self
   384                                checkMySelfImportingKeyFilePath:@"6FF00E97_sec.asc"
   385                                address:@"pep.test.alice@pep-project.org"
   386                                userID:@"Alice_User_ID"
   387                                fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
   388                                session:session];
   389 
   390     //Message
   391 
   392     {
   393         PEPIdentity *identUnknownBob = [[PEPIdentity alloc]
   394                                         initWithAddress:@"pep.test.unknown.bob@pep-project.org"
   395                                         userID:@"4242" userName:@"pEp Test Bob Unknown"
   396                                         isOwn:NO];
   397 
   398         PEPMessage *msgGray = [PEPMessage new];
   399         msgGray.from = identAlice;
   400         msgGray.to = @[identUnknownBob];
   401         msgGray.shortMessage = @"All Gray Test";
   402         msgGray.longMessage = @"This is a text content";
   403         msgGray.direction = PEP_dir_outgoing;
   404 
   405         NSError *error = nil;
   406 
   407         // Test with unknown Bob
   408         NSNumber *numRating = [self
   409                                testOutgoingRatingForMessage:msgGray
   410                                session:session
   411                                error:&error];
   412         XCTAssertNotNil(numRating);
   413         XCTAssertNil(error);
   414         XCTAssertEqual(numRating.pEpRating, PEPRatingUnencrypted);
   415     }
   416 
   417     PEPIdentity *identBob = [self
   418                              checkImportingKeyFilePath:@"0xC9C2EE39.asc"
   419                              address:@"pep.test.bob@pep-project.org"
   420                              userID:@"42"
   421                              fingerPrint:@"BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39"
   422                              session: session];
   423     XCTAssertNotNil(identBob);
   424 
   425     PEPMessage *msg = [PEPMessage new];
   426     msg.from = identAlice;
   427     msg.to = @[identBob];
   428     msg.shortMessage = @"All Gray Test";
   429     msg.longMessage = @"This is a text content";
   430     msg.direction = PEP_dir_outgoing;
   431 
   432     NSError *error = nil;
   433 
   434     // Should be yellow, since no handshake happened.
   435     NSNumber *numRating = [self testOutgoingRatingForMessage:msg session:session error:&error];
   436     XCTAssertNotNil(numRating);
   437     XCTAssertNil(error);
   438     XCTAssertEqual(numRating.pEpRating, PEPRatingReliable);
   439 
   440     PEPRating rating = [self ratingForIdentity:identBob session:session];
   441     XCTAssertEqual(rating, PEPRatingReliable);
   442 
   443     // Let' say we got that handshake, set PEP_ct_confirmed in Bob's identity
   444     XCTAssertTrue([session trustPersonalKey:identBob error:&error]);
   445     XCTAssertNil(error);
   446 
   447     // This time it should be green
   448     numRating = [self testOutgoingRatingForMessage:msg session:session error:&error];
   449     XCTAssertNotNil(numRating);
   450     XCTAssertNil(error);
   451     XCTAssertEqual(numRating.pEpRating, PEPRatingTrusted);
   452 
   453     rating = [self ratingForIdentity:identBob session:session];
   454     XCTAssertEqual(rating, PEPRatingTrusted);
   455 
   456     // Let' say we undo handshake
   457     XCTAssertTrue([session keyResetTrust:identBob error:&error]);
   458     XCTAssertNil(error);
   459 
   460     // Yellow ?
   461     numRating = [self testOutgoingRatingForMessage:msg session:session error:&error];
   462     XCTAssertNotNil(numRating);
   463     XCTAssertNil(error);
   464     XCTAssertEqual(numRating.pEpRating, PEPRatingReliable);
   465 
   466     // mistrust Bob
   467     XCTAssertTrue([session keyMistrusted:identBob error:&error]);
   468     XCTAssertNil(error);
   469 
   470     identBob.fingerPrint = nil;
   471     XCTAssertTrue([session updateIdentity:identBob error:&error]);
   472     XCTAssertNil(error);
   473     XCTAssertNil(identBob.fingerPrint);
   474 
   475     // Gray == PEPRatingUnencrypted
   476     numRating = [self testOutgoingRatingForMessage:msg session:session error:&error];
   477     XCTAssertNotNil(numRating);
   478     XCTAssertNil(error);
   479     XCTAssertEqual(numRating.pEpRating, PEPRatingUnencrypted);
   480 }
   481 
   482 
   483 - (void)testOutgoingBccColors
   484 {
   485     PEPSession *session = [PEPSession new];
   486 
   487     // Our test user :
   488     // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
   489     // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
   490     XCTAssertTrue([PEPTestUtils importBundledKey:@"6FF00E97_sec.asc" session:session]);
   491 
   492     PEPIdentity *identAlice = [[PEPIdentity alloc]
   493                                initWithAddress:@"pep.test.alice@pep-project.org"
   494                                userID:ownUserId
   495                                userName:@"pEp Test Alice"
   496                                isOwn:YES
   497                                fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
   498 
   499     NSError *error = nil;
   500     XCTAssertTrue([session mySelf:identAlice error:&error]);
   501     XCTAssertNil(error);
   502 
   503     PEPMessage *msg = [PEPMessage new];
   504     msg.from = identAlice;
   505     msg.to = @[[[PEPIdentity alloc] initWithAddress:@"pep.test.bob@pep-project.org"
   506                                              userID:@"42" userName:@"pEp Test Bob" isOwn:NO]];
   507     msg.shortMessage = @"All Green Test";
   508     msg.longMessage = @"This is a text content";
   509     msg.direction = PEP_dir_outgoing;
   510 
   511     // Test with unknown Bob
   512     PEPRating rating;
   513     NSNumber *numRating = [self testOutgoingRatingForMessage:msg session:session error:&error];
   514     XCTAssertNotNil(numRating);
   515     XCTAssertNil(error);
   516     XCTAssertEqual(numRating.pEpRating, PEPRatingUnencrypted);
   517 
   518     // Now let see with bob's pubkey already known
   519     // pEp Test Bob (test key, don't use) <pep.test.bob@pep-project.org>
   520     // BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39
   521     XCTAssertTrue([PEPTestUtils importBundledKey:@"0xC9C2EE39.asc" session:session]);
   522 
   523     PEPIdentity *identBob = [[PEPIdentity alloc]
   524                              initWithAddress:@"pep.test.bob@pep-project.org"
   525                              userID:@"42" userName:@"pEp Test Bob"
   526                              isOwn:NO
   527                              fingerPrint:@"BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39"];
   528 
   529     XCTAssertTrue([session updateIdentity:identBob error:&error]);
   530     XCTAssertNil(error);
   531 
   532     // Should be yellow, since no handshake happened.
   533     numRating = [self testOutgoingRatingForMessage:msg session:session error:&error];
   534     XCTAssertNotNil(numRating);
   535     XCTAssertNil(error);
   536     XCTAssertEqual(numRating.pEpRating, PEPRatingReliable);
   537 
   538     rating = [self ratingForIdentity:identBob session:session];
   539     XCTAssertEqual(rating, PEPRatingReliable);
   540 
   541     // Let' say we got that handshake, set PEP_ct_confirmed in Bob's identity
   542     XCTAssertTrue([session trustPersonalKey:identBob error:&error]);
   543     XCTAssertNil(error);
   544 
   545     // This time it should be green
   546     numRating = [self testOutgoingRatingForMessage:msg session:session error:&error];
   547     XCTAssertNotNil(numRating);
   548     XCTAssertNil(error);
   549     XCTAssertEqual(numRating.pEpRating, PEPRatingTrusted);
   550 
   551     rating = [self ratingForIdentity:identBob session:session];
   552     XCTAssertEqual(rating, PEPRatingTrusted);
   553 
   554     // Now let see if it turns back yellow if we add an unconfirmed folk.
   555     // pEp Test John (test key, don't use) <pep.test.john@pep-project.org>
   556     // AA2E4BEB93E5FE33DEFD8BE1135CD6D170DCF575
   557     XCTAssertTrue([PEPTestUtils importBundledKey:@"0x70DCF575.asc" session:session]);
   558 
   559     PEPIdentity *identJohn = [[PEPIdentity alloc]
   560                               initWithAddress:@"pep.test.john@pep-project.org"
   561                               userID:@"101" userName:@"pEp Test John"
   562                               isOwn:NO
   563                               fingerPrint:@"AA2E4BEB93E5FE33DEFD8BE1135CD6D170DCF575"];
   564 
   565     XCTAssertTrue([session updateIdentity:identJohn error:&error]);
   566     XCTAssertNil(error);
   567 
   568     msg.bcc = @[[[PEPIdentity alloc] initWithAddress:@"pep.test.john@pep-project.org"
   569                                               userID:@"101" userName:@"pEp Test John" isOwn:NO]];
   570 
   571     // Yellow ?
   572     numRating = [self testOutgoingRatingForMessage:msg session:session error:&error];
   573     XCTAssertNotNil(numRating);
   574     XCTAssertNil(error);
   575     XCTAssertEqual(numRating.pEpRating, PEPRatingReliable);
   576 
   577     XCTAssertTrue([session trustPersonalKey:identJohn error:&error]);
   578     XCTAssertNil(error);
   579 
   580     // This time it should be green
   581     numRating = [self testOutgoingRatingForMessage:msg session:session error:&error];
   582     XCTAssertNotNil(numRating);
   583     XCTAssertNil(error);
   584     XCTAssertEqual(numRating.pEpRating, PEPRatingTrusted);
   585 
   586     rating = [self ratingForIdentity:identJohn session:session];
   587     XCTAssertEqual(rating, PEPRatingTrusted);
   588 }
   589 
   590 - (void)testDontEncryptForMistrusted
   591 {
   592     PEPSession *session = [PEPSession new];
   593 
   594     // Our test user :
   595     // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
   596     // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
   597     XCTAssertTrue([PEPTestUtils importBundledKey:@"6FF00E97_sec.asc" session:session]);
   598 
   599     PEPIdentity *identAlice = [[PEPIdentity alloc]
   600                                initWithAddress:@"pep.test.alice@pep-project.org"
   601                                userID:ownUserId
   602                                userName:@"pEp Test Alice"
   603                                isOwn:YES
   604                                fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
   605 
   606     NSError *error = nil;
   607     XCTAssertTrue([session mySelf:identAlice error:&error]);
   608     XCTAssertNil(error);
   609 
   610     // pEp Test Bob (test key, don't use) <pep.test.bob@pep-project.org>
   611     // BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39
   612     XCTAssertTrue([PEPTestUtils importBundledKey:@"0xC9C2EE39.asc" session:session]);
   613 
   614     PEPIdentity *identBob = [[PEPIdentity alloc]
   615                              initWithAddress:@"pep.test.bob@pep-project.org"
   616                              userID:@"42" userName:@"pEp Test Bob"
   617                              isOwn:NO
   618                              fingerPrint:@"BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39"];
   619 
   620     XCTAssertTrue([session updateIdentity:identBob error:&error]);
   621     XCTAssertNil(error);
   622 
   623     // mistrust Bob
   624     XCTAssertTrue([session keyMistrusted:identBob error:&error]);
   625     XCTAssertNil(error);
   626 
   627     PEPMessage *msg = [PEPMessage new];
   628     msg.from = identAlice;
   629     msg.to = @[[[PEPIdentity alloc] initWithAddress:@"pep.test.bob@pep-project.org" userID:@"42"
   630                                            userName:@"pEp Test Bob" isOwn:NO]];
   631     msg.shortMessage = @"All Green Test";
   632     msg.longMessage = @"This is a text content";
   633     msg.direction = PEP_dir_outgoing;
   634 
   635     // Gray == PEPRatingUnencrypted
   636     NSNumber *numRating = [self testOutgoingRatingForMessage:msg session:session error:&error];
   637     XCTAssertNotNil(numRating);
   638     XCTAssertNil(error);
   639     XCTAssertEqual(numRating.pEpRating, PEPRatingUnencrypted);
   640 
   641     PEPMessage *encMsg = [session encryptMessage:msg extraKeys:nil status:nil error:&error];
   642     XCTAssertNotNil(encMsg);
   643     XCTAssertNil(error);
   644 
   645     XCTAssertNotEqualObjects(encMsg.attachments[0].mimeType, @"application/pgp-encrypted");
   646 
   647     [self pEpCleanUp];
   648 }
   649 
   650 - (void)testRevoke
   651 {
   652     PEPSession *session = [PEPSession new];
   653 
   654     // Our test user :
   655     // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
   656     // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
   657     XCTAssertTrue([PEPTestUtils importBundledKey:@"6FF00E97_sec.asc" session:session]);
   658     NSString *fpr = @"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97";
   659 
   660     PEPIdentity *identAlice = [[PEPIdentity alloc]
   661                                initWithAddress:@"pep.test.alice@pep-project.org"
   662                                userID:ownUserId
   663                                userName:@"pEp Test Alice"
   664                                isOwn:YES
   665                                fingerPrint:fpr];
   666 
   667     NSError *error = nil;
   668     XCTAssertTrue([session mySelf:identAlice error:&error]);
   669     XCTAssertNil(error);
   670 
   671     PEPIdentity *identAlice2 = [identAlice mutableCopy];
   672 
   673     // This will revoke key
   674     XCTAssertTrue([session keyMistrusted:identAlice2 error:&error]);
   675     XCTAssertNil(error);
   676     identAlice2.fingerPrint = nil;
   677 
   678     XCTAssertTrue([session mySelf:identAlice error:&error]);
   679     XCTAssertNil(error);
   680 
   681     // Check fingerprint is different
   682     XCTAssertNotEqualObjects(identAlice2.fingerPrint, fpr);
   683 }
   684 
   685 - (void)testMailToMyself
   686 {
   687     PEPSession *session = [PEPSession new];
   688 
   689     // Our test user :
   690     // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
   691     // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
   692     XCTAssertTrue([PEPTestUtils importBundledKey:@"6FF00E97_sec.asc" session:session]);
   693 
   694     PEPIdentity *identAlice = [[PEPIdentity alloc]
   695                                initWithAddress:@"pep.test.alice@pep-project.org"
   696                                userID:ownUserId
   697                                userName:@"pEp Test Alice"
   698                                isOwn:YES
   699                                fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
   700 
   701     NSError *error = nil;
   702     XCTAssertTrue([session mySelf:identAlice error:&error]);
   703     XCTAssertNil(error);
   704 
   705     PEPMessage *msg = [PEPMessage new];
   706     msg.from = identAlice;
   707     msg.to = @[identAlice];
   708     msg.shortMessage = @"Mail to Myself";
   709     msg.longMessage = @"This is a text content";
   710     msg.direction = PEP_dir_outgoing;
   711 
   712     NSNumber *numRating = [self testOutgoingRatingForMessage:msg session:session error:&error];
   713     XCTAssertNotNil(numRating);
   714     XCTAssertNil(error);
   715     XCTAssertEqual(numRating.pEpRating, PEPRatingTrustedAndAnonymized);
   716 
   717     PEPMessage *encMsg = [session encryptMessage:msg extraKeys:nil status:nil error:&error];
   718     XCTAssertNotNil(encMsg);
   719     XCTAssertNil(error);
   720 
   721     NSArray *keys;
   722 
   723     error = nil;
   724     PEPRating rating = PEPRatingUndefined;
   725     PEPMessage *decmsg = [session
   726                           decryptMessage:encMsg
   727                           flags:nil
   728                           rating:&rating
   729                           extraKeys:&keys
   730                           status:nil
   731                           error:&error];
   732     XCTAssertNotNil(decmsg);
   733     XCTAssertNil(error);
   734     XCTAssertEqual(rating, PEPRatingTrustedAndAnonymized);
   735 }
   736 
   737 - (void)testEncryptedMailFromMuttWithReencryption
   738 {
   739     PEPSession *session = [PEPSession new];
   740 
   741     // This is the public key for test001@peptest.ch
   742     XCTAssertTrue([PEPTestUtils importBundledKey:@"A3FC7F0A.asc" session:session]);
   743 
   744     // This is the secret key for test001@peptest.ch
   745     XCTAssertTrue([PEPTestUtils importBundledKey:@"A3FC7F0A_sec.asc" session:session]);
   746 
   747     // Mail from mutt, already processed into message dict by the app.
   748     NSMutableDictionary *msgDict = [[PEPTestUtils
   749                                      unarchiveDictionary:@"msg_to_A3FC7F0A_from_mutt.ser"]
   750                                     mutableCopy];
   751     [PEPTestUtils migrateUnarchivedMessageDictionary:msgDict];
   752     [msgDict removeObjectForKey:kPepLongMessage];
   753     [msgDict removeObjectForKey:kPepLongMessageFormatted];
   754 
   755     // Also extracted "live" from the app.
   756     NSMutableDictionary *accountDict = [[PEPTestUtils
   757                                          unarchiveDictionary:@"account_A3FC7F0A.ser"]
   758                                         mutableCopy];
   759     [accountDict removeObjectForKey:kPepCommType];
   760     [accountDict removeObjectForKey:kPepFingerprint];
   761     PEPIdentity *identMe = [[PEPIdentity alloc] initWithDictionary:accountDict];
   762 
   763     NSError *error = nil;
   764     XCTAssertTrue([session mySelf:identMe error:&error]);
   765     XCTAssertNil(error);
   766 
   767     XCTAssertNotNil(identMe.fingerPrint);
   768 
   769     NSArray* keys;
   770     PEPMessage *msg = [PEPMessage new];
   771     [msg setValuesForKeysWithDictionary:msgDict];
   772     PEPMessage *msgOriginal = [PEPMessage new];
   773     [msgOriginal setValuesForKeysWithDictionary:msgDict];
   774 
   775     XCTAssertEqualObjects(msg, msgOriginal);
   776 
   777     PEPRating rating = PEPRatingUndefined;
   778     PEPDecryptFlags flags = PEPDecryptFlagsUntrustedServer;
   779 
   780     PEPMessage *pepDecryptedMail = [session
   781                                     decryptMessage:msg
   782                                     flags:&flags
   783                                     rating:&rating
   784                                     extraKeys:&keys
   785                                     status:nil
   786                                     error:&error];
   787     XCTAssertNotNil(pepDecryptedMail);
   788     XCTAssertNil(error);
   789 
   790     // Technically, the mail is encrypted, but the signatures don't match
   791     XCTAssertEqual(rating, PEPRatingUnreliable);
   792 
   793     // Since we're requesting re-encryption, src should have been changed
   794     XCTAssertNotEqualObjects(msg, msgOriginal);
   795 
   796     XCTAssertNotNil(pepDecryptedMail.longMessage);
   797 }
   798 
   799 - (void)testOutgoingContactColor
   800 {
   801     PEPSession *session = [PEPSession new];
   802 
   803     PEPIdentity *partner1Orig = [PEPTestUtils foreignPepIdentityWithAddress:@"partner1@dontcare.me"
   804                                                                    userName:@"Partner 1"];
   805     NSString *pubKeyPartner1 = [PEPTestUtils loadResourceByName:@"partner1_F2D281C2789DD7F6_pub.asc"];
   806     XCTAssertNotNil(pubKeyPartner1);
   807 
   808     NSError *error = nil;
   809     XCTAssertTrue([session importKey:pubKeyPartner1 error:&error]);
   810     XCTAssertNil(error);
   811 
   812     PEPRating color = [self ratingForIdentity:partner1Orig session:session];
   813     XCTAssertEqual(color, PEPRatingReliable);
   814 }
   815 
   816 - (void)testGetTrustwords
   817 {
   818     PEPSession *session = [PEPSession new];
   819 
   820     PEPIdentity *partner1Orig = [[PEPIdentity alloc]
   821                                  initWithAddress:@"partner1@dontcare.me" userID:@"partner1"
   822                                  userName:@"partner1"
   823                                  isOwn:NO fingerPrint:@"F0CD3F7B422E5D587ABD885BF2D281C2789DD7F6"];
   824 
   825     PEPIdentity *meOrig = [[PEPIdentity alloc]
   826                            initWithAddress:@"me@dontcare.me" userID:@"me"
   827                            userName:@"me"
   828                            isOwn:NO fingerPrint:@"CC1F73F6FB774BF08B197691E3BFBCA9248FC681"];
   829 
   830     NSString *pubKeyPartner1 = [PEPTestUtils loadResourceByName:@"partner1_F2D281C2789DD7F6_pub.asc"];
   831     XCTAssertNotNil(pubKeyPartner1);
   832     NSString *pubKeyMe = [PEPTestUtils loadResourceByName:@"meATdontcare_E3BFBCA9248FC681_pub.asc"];
   833     XCTAssertNotNil(pubKeyMe);
   834     NSString *secKeyMe = [PEPTestUtils loadResourceByName:@"meATdontcare_E3BFBCA9248FC681_sec.asc"];
   835     XCTAssertNotNil(secKeyMe);
   836 
   837     NSError *error = nil;
   838     NSString *trustwordsFull = [session getTrustwordsIdentity1:meOrig identity2:partner1Orig
   839                                                       language:nil full:YES error:&error];
   840     XCTAssertNil(error);
   841     XCTAssertEqualObjects(trustwordsFull,
   842                           @"EMERSON GASPER TOKENISM BOLUS COLLAGE DESPISE BEDDED ENCRYPTION IMAGINE BEDFORD");
   843 
   844     NSString *trustwordsFullEnglish = [session getTrustwordsIdentity1:meOrig identity2:partner1Orig
   845                                                              language:@"en" full:YES error:&error];
   846     XCTAssertNil(error);
   847     XCTAssertEqualObjects(trustwordsFullEnglish, trustwordsFull);
   848 
   849     NSString *trustwordsUndefined = [session getTrustwordsIdentity1:meOrig identity2:partner1Orig
   850                                                            language:@"ZZ" full:YES error:&error];
   851     XCTAssertNotNil(error);
   852     XCTAssertNil(trustwordsUndefined);
   853 }
   854 
   855 - (void)testStringToRating
   856 {
   857     PEPSession *session = [PEPSession new];
   858     XCTAssertEqual([session ratingFromString:@"cannot_decrypt"], PEPRatingCannotDecrypt);
   859     XCTAssertEqual([session ratingFromString:@"have_no_key"], PEPRatingHaveNoKey);
   860     XCTAssertEqual([session ratingFromString:@"unencrypted"], PEPRatingUnencrypted);
   861     XCTAssertEqual([session ratingFromString:@"unencrypted_for_some"],
   862                    PEPRatingUnencryptedForSome);
   863     XCTAssertEqual([session ratingFromString:@"unreliable"], PEPRatingUnreliable);
   864     XCTAssertEqual([session ratingFromString:@"reliable"], PEPRatingReliable);
   865     XCTAssertEqual([session ratingFromString:@"trusted"], PEPRatingTrusted);
   866     XCTAssertEqual([session ratingFromString:@"trusted_and_anonymized"],
   867                    PEPRatingTrustedAndAnonymized);
   868     XCTAssertEqual([session ratingFromString:@"fully_anonymous"], PEPRatingFullyAnonymous);
   869     XCTAssertEqual([session ratingFromString:@"mistrust"], PEPRatingMistrust);
   870     XCTAssertEqual([session ratingFromString:@"b0rken"], PEPRatingB0rken);
   871     XCTAssertEqual([session ratingFromString:@"under_attack"], PEPRatingUnderAttack);
   872     XCTAssertEqual([session ratingFromString:@"undefined"], PEPRatingUndefined);
   873     XCTAssertEqual([session ratingFromString:@"does not exist111"], PEPRatingUndefined);
   874 }
   875 
   876 - (void)testRatingToString
   877 {
   878     PEPSession *session = [PEPSession new];
   879     XCTAssertEqualObjects([session stringFromRating:PEPRatingCannotDecrypt], @"cannot_decrypt");
   880     XCTAssertEqualObjects([session stringFromRating:PEPRatingHaveNoKey], @"have_no_key");
   881     XCTAssertEqualObjects([session stringFromRating:PEPRatingUnencrypted], @"unencrypted");
   882     XCTAssertEqualObjects([session stringFromRating:PEPRatingUnencryptedForSome],
   883                           @"unencrypted_for_some");
   884     XCTAssertEqualObjects([session stringFromRating:PEPRatingUnreliable], @"unreliable");
   885     XCTAssertEqualObjects([session stringFromRating:PEPRatingReliable], @"reliable");
   886     XCTAssertEqualObjects([session stringFromRating:PEPRatingTrusted], @"trusted");
   887     XCTAssertEqualObjects([session stringFromRating:PEPRatingTrustedAndAnonymized],
   888                           @"trusted_and_anonymized");
   889     XCTAssertEqualObjects([session stringFromRating:PEPRatingFullyAnonymous],
   890                           @"fully_anonymous");
   891     XCTAssertEqualObjects([session stringFromRating:PEPRatingMistrust], @"mistrust");
   892     XCTAssertEqualObjects([session stringFromRating:PEPRatingB0rken], @"b0rken");
   893     XCTAssertEqualObjects([session stringFromRating:PEPRatingUnderAttack], @"under_attack");
   894     XCTAssertEqualObjects([session stringFromRating:PEPRatingUndefined], @"undefined");
   895     XCTAssertEqualObjects([session stringFromRating:500], @"undefined");
   896 }
   897 
   898 - (void)testIsPEPUser
   899 {
   900     PEPSession *session = [PEPSession new];
   901 
   902     PEPIdentity *identMe = [[PEPIdentity alloc]
   903                             initWithAddress:@"me-myself-and-i@pep-project.org"
   904                             userID:@"me-myself-and-i"
   905                             userName:@"pEp Me"
   906                             isOwn:YES];
   907     NSError *error = nil;
   908     XCTAssertTrue([session mySelf:identMe error:&error]);
   909     XCTAssertNil(error);
   910 
   911     XCTAssertNotNil(identMe.fingerPrint);
   912 
   913     // PEP_CANNOT_FIND_PERSON == 902
   914     XCTAssertTrue([session isPEPUser:identMe error:&error]);
   915 }
   916 
   917 - (void)testXEncStatusForOutgoingEncryptedMail
   918 {
   919     [self helperXEncStatusForOutgoingEncryptdMailToSelf:NO expectedRating:PEPRatingReliable];
   920 }
   921 
   922 - (void)testXEncStatusForOutgoingSelfEncryptedMail
   923 {
   924     [self helperXEncStatusForOutgoingEncryptdMailToSelf:YES
   925                                          expectedRating:PEPRatingTrustedAndAnonymized];
   926 }
   927 
   928 - (void)testEncryptMessagesWithoutKeys
   929 {
   930     PEPSession *session = [PEPSession new];
   931 
   932     PEPIdentity *identMe = [[PEPIdentity alloc]
   933                             initWithAddress:@"me-myself-and-i@pep-project.org"
   934                             userID:@"me-myself-and-i"
   935                             userName:@"pEp Me"
   936                             isOwn:YES];
   937     NSError *error = nil;
   938     XCTAssertTrue([session mySelf:identMe error:&error]);
   939     XCTAssertNil(error);
   940 
   941     XCTAssertNotNil(identMe.fingerPrint);
   942 
   943     PEPIdentity *identAlice = [[PEPIdentity alloc]
   944                                initWithAddress:@"alice@pep-project.org"
   945                                userID:@"alice"
   946                                userName:@"pEp Test Alice"
   947                                isOwn:NO];
   948 
   949     PEPMessage *msg = [PEPMessage new];
   950     msg.from = identMe;
   951     msg.to = @[identAlice];
   952     msg.shortMessage = @"Mail to Alice";
   953     msg.longMessage = @"Alice?";
   954     msg.direction = PEP_dir_outgoing;
   955 
   956     NSNumber *numRating = [self testOutgoingRatingForMessage:msg session:session error:&error];
   957     XCTAssertNotNil(numRating);
   958     XCTAssertNil(error);
   959     XCTAssertEqual(numRating.pEpRating, PEPRatingUnencrypted);
   960 
   961     PEPMessage *encMsg = [session encryptMessage:msg extraKeys:nil status:nil error:&error];
   962     XCTAssertNotNil(encMsg);
   963     XCTAssertNil(error);
   964 
   965     XCTAssertNotNil(encMsg);
   966 
   967     PEPStringList *keys;
   968     PEPRating pEpRating;
   969     error = nil;
   970     PEPMessage *decMsg = [session
   971                           decryptMessage:encMsg
   972                           flags:nil
   973                           rating:&pEpRating
   974                           extraKeys:&keys
   975                           status:nil
   976                           error:&error];
   977     XCTAssertNotNil(decMsg);
   978     XCTAssertNil(error);
   979 
   980     XCTAssertEqual(pEpRating, PEPRatingUnencrypted);
   981     XCTAssertNotNil(decMsg);
   982 }
   983 
   984 /**
   985  ENGINE-364. Tries to invoke trustPersonalKey on an identity without key,
   986  giving it a fake fingerprint.
   987  */
   988 - (void)testTrustPersonalKey
   989 {
   990     PEPSession *session = [PEPSession new];
   991 
   992     PEPIdentity *identMe = [[PEPIdentity alloc]
   993                             initWithAddress:@"me-myself-and-i@pep-project.org"
   994                             userID:@"me-myself-and-i"
   995                             userName:@"pEp Me"
   996                             isOwn:YES];
   997     NSError *error = nil;
   998     XCTAssertTrue([session mySelf:identMe error:&error]);
   999     XCTAssertNil(error);
  1000 
  1001     XCTAssertNotNil(identMe.fingerPrint);
  1002 
  1003     // The fingerprint is definitely wrong, we don't have a key
  1004     PEPIdentity *identAlice = [[PEPIdentity alloc]
  1005                                initWithAddress:@"alice@pep-project.org"
  1006                                userID:@"alice"
  1007                                userName:@"pEp Test Alice"
  1008                                isOwn:NO
  1009                                fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
  1010 
  1011     XCTAssertFalse([session trustPersonalKey:identAlice error:&error]);
  1012     XCTAssertNotNil(error);
  1013 }
  1014 
  1015 /**
  1016  ENGINE-381
  1017  */
  1018 - (void)testVolatileIdentityRating
  1019 {
  1020     PEPSession *session = [PEPSession new];
  1021 
  1022     PEPIdentity *identMe = [[PEPIdentity alloc]
  1023                             initWithAddress:@"me-myself-and-i@pep-project.org"
  1024                             userID:@"me-myself-and-i"
  1025                             userName:@"pEp Me"
  1026                             isOwn:YES];
  1027     NSError *error = nil;
  1028     XCTAssertTrue([session mySelf:identMe error:&error]);
  1029     XCTAssertNil(error);
  1030 
  1031     XCTAssertNotNil(identMe.fingerPrint);
  1032 
  1033     PEPIdentity *identAlice = [self
  1034                                checkImportingKeyFilePath:@"6FF00E97_sec.asc"
  1035                                address:@"pep.test.alice@pep-project.org"
  1036                                userID:@"alice_user_id"
  1037                                fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
  1038                                session: session];
  1039     XCTAssertNotNil(identAlice);
  1040 
  1041     dispatch_group_t identityRatingGroup = dispatch_group_create();
  1042 
  1043     void (^ratingBlock)(void) = ^{
  1044         PEPSession *innerSession = [PEPSession new];
  1045         PEPRating rating = [self ratingForIdentity:identAlice session:innerSession];
  1046         XCTAssertEqual(rating, PEPRatingReliable);
  1047     };
  1048 
  1049     for (int i = 0; i < 4; ++i) {
  1050         dispatch_group_async(identityRatingGroup,
  1051                              dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0),
  1052                              ratingBlock);
  1053     }
  1054 
  1055     for (int i = 0; i < 4; ++i) {
  1056         ratingBlock();
  1057     }
  1058 
  1059     dispatch_group_wait(identityRatingGroup, DISPATCH_TIME_FOREVER);
  1060 }
  1061 
  1062 /**
  1063  IOSAD-93, testing for easy error case.
  1064  */
  1065 - (void)testEncryptAndAttachPrivateKeyIllegalValue
  1066 {
  1067     PEPSession *session = [PEPSession new];
  1068 
  1069     PEPIdentity *identMe = [[PEPIdentity alloc]
  1070                             initWithAddress:@"me-myself-and-i@pep-project.org"
  1071                             userID:@"me-myself-and-i"
  1072                             userName:@"pEp Me"
  1073                             isOwn:YES];
  1074     NSError *error = nil;
  1075     XCTAssertTrue([session mySelf:identMe error:&error]);
  1076     XCTAssertNil(error);
  1077 
  1078     XCTAssertNotNil(identMe.fingerPrint);
  1079 
  1080     NSString *fprAlice = @"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97";
  1081     PEPIdentity *identAlice = [self
  1082                                checkImportingKeyFilePath:@"6FF00E97_sec.asc"
  1083                                address:@"pep.test.alice@pep-project.org"
  1084                                userID:@"alice_user_id"
  1085                                fingerPrint:fprAlice
  1086                                session: session];
  1087     XCTAssertNotNil(identAlice);
  1088 
  1089     NSString *shortMessage = @"whatever it may be";
  1090     NSString *longMessage = [NSString stringWithFormat:@"%@ %@", shortMessage, shortMessage];
  1091     PEPMessage *message = [PEPMessage new];
  1092     message.from = identMe;
  1093     message.to = @[identAlice];
  1094     message.shortMessage = shortMessage;
  1095     message.longMessage = longMessage;
  1096 
  1097     PEPStatus status = PEPStatusKeyNotFound;
  1098     error = nil;
  1099     PEPMessage *encrypted = [session
  1100                              encryptMessage:message
  1101                              toFpr:fprAlice
  1102                              encFormat:PEPEncFormatPEP
  1103                              flags:0
  1104                              status:&status error:&error];
  1105     XCTAssertEqual(status, PEP_ILLEGAL_VALUE);
  1106     XCTAssertNotNil(error);
  1107     XCTAssertNil(encrypted);
  1108 }
  1109 
  1110 - (void)testSetIdentityFlags
  1111 {
  1112     PEPSession *session = [PEPSession new];
  1113 
  1114     PEPIdentity *me = [PEPTestUtils ownPepIdentityWithAddress:@"me@peptest.ch"
  1115                                                      userName:@"userName"];
  1116     NSError *error = nil;
  1117     XCTAssertTrue([session mySelf:me error:&error]);
  1118     XCTAssertNil(error);
  1119 
  1120     identity_flags theFlags[] = { PEP_idf_not_for_sync, PEP_idf_list, PEP_idf_devicegroup, 0 };
  1121     for (int i = 0;; ++i) {
  1122         identity_flags aFlag = theFlags[i];
  1123         if (aFlag == 0) {
  1124             break;
  1125         }
  1126         error = nil;
  1127         XCTAssertTrue([session setFlags:aFlag forIdentity:me error:&error]);
  1128         XCTAssertNil(error);
  1129     }
  1130 }
  1131 
  1132 - (void)testTrustOwnKey
  1133 {
  1134     PEPSession *session = [PEPSession new];
  1135 
  1136     PEPIdentity *me = [PEPTestUtils ownPepIdentityWithAddress:@"me@peptest.ch"
  1137                                                      userName:@"userName"];
  1138     NSError *error = nil;
  1139     XCTAssertTrue([session mySelf:me error:&error]);
  1140     XCTAssertNil(error);
  1141 
  1142     XCTAssertTrue([session trustOwnKeyIdentity:me error:&error]);
  1143     XCTAssertNil(error);
  1144 }
  1145 
  1146 #pragma mark - configUnencryptedSubject
  1147 
  1148 - (void)testConfigUnencryptedSubject
  1149 {
  1150     // Setup Config to encrypt subject
  1151     [PEPObjCAdapter setUnEncryptedSubjectEnabled:NO];
  1152 
  1153     // Write mail to yourself ...
  1154     PEPMessage *encMessage = [self mailWrittenToMySelf];
  1155 
  1156     // ... and assert subject is encrypted
  1157     XCTAssertEqualObjects(encMessage.shortMessage, @"p≡p", @"Subject should be encrypted");
  1158 }
  1159 
  1160 - (void)testConfigUnencryptedSubjectEncryptedSubjectDisabled
  1161 {
  1162     // Setup Config to not encrypt subject
  1163     [PEPObjCAdapter setUnEncryptedSubjectEnabled:YES];
  1164 
  1165     // Write mail to yourself ...
  1166     PEPMessage *encMessage = [self mailWrittenToMySelf];
  1167 
  1168     // pEp to pEp uses message 2.0, which always encrypts subjects (ENGINE-429)
  1169     XCTAssertEqualObjects(encMessage.shortMessage, @"p≡p", @"Subject should be encrypted");
  1170 }
  1171 
  1172 #pragma mark - Passive mode
  1173 
  1174 - (void)testPassiveMode
  1175 {
  1176     [self testPassiveModeEnabled:NO];
  1177     [self testPassiveModeEnabled:YES];
  1178 }
  1179 
  1180 #pragma mark - Decryption
  1181 
  1182 - (void)testDecryptionOfUnencryptedMessageWithOdtAttachmentContainingSpace
  1183 {
  1184     PEPSession *session = [PEPSession new];
  1185 
  1186     PEPIdentity *me = [PEPTestUtils ownPepIdentityWithAddress:@"me@peptest.ch"
  1187                                                      userName:@"userName"];
  1188     NSError *error = nil;
  1189     XCTAssertTrue([session mySelf:me error:&error]);
  1190     XCTAssertNil(error);
  1191 
  1192     const char *dataString = "blahblah";
  1193     const size_t dataSize = strlen(dataString);
  1194     char *rawData = strndup(dataString, dataSize);
  1195 
  1196     PEPAttachment *attachment = [[PEPAttachment alloc]
  1197                                  initWithData:[NSData
  1198                                                dataWithBytesNoCopy:rawData length:dataSize]];
  1199     attachment.filename = @"Someone andTextIncludingTheSpace.odt";
  1200     attachment.mimeType = @"application/vnd.oasis.opendocument.text";
  1201 
  1202     NSString *shortMessage = @"Subject";
  1203     NSString *longMessage = @"Oh, this is a long body text!";
  1204     PEPMessage *mail = [PEPTestUtils mailFrom:me
  1205                                       toIdent:me
  1206                                  shortMessage:shortMessage
  1207                                   longMessage:longMessage
  1208                                      outgoing:YES];
  1209 
  1210     mail.attachments = @[attachment];
  1211 
  1212     error = nil;
  1213     PEPStringList *keys;
  1214     PEPRating rating = PEPRatingUndefined;
  1215     PEPMessage *decmsg = [session
  1216                           decryptMessage:mail
  1217                           flags:nil
  1218                           rating:&rating
  1219                           extraKeys:&keys
  1220                           status:nil
  1221                           error:&error];
  1222     XCTAssertNotNil(decmsg);
  1223     XCTAssertNil(error);
  1224     XCTAssertEqual(rating, PEPRatingUnencrypted);
  1225 
  1226     PEPAttachment *decryptedAttachment = [decmsg.attachments objectAtIndex:0];
  1227     XCTAssertEqualObjects(decryptedAttachment.mimeType, attachment.mimeType);
  1228     XCTAssertEqualObjects(decryptedAttachment.filename, attachment.filename);
  1229 }
  1230 
  1231 #pragma mark - Sync
  1232 
  1233 /**
  1234  Prove that mySelf triggers a message to be sent.
  1235  */
  1236 - (void)testBasicSendMessage
  1237 {
  1238     PEPSession *session = [PEPSession new];
  1239     [self testSendMessageOnSession:session];
  1240 }
  1241 
  1242 - (void)testDeliverHandshakeResult
  1243 {
  1244     PEPSession *session = [PEPSession new];
  1245     [self testSendMessageOnSession:session];
  1246 
  1247     PEPIdentity *forSureNotMe = [[PEPIdentity alloc]
  1248                                  initWithAddress:@"someoneelseentirely@pep-project.org"
  1249                                  userID:@"that_someone_else"
  1250                                  userName:@"other"
  1251                                  isOwn:NO];
  1252 
  1253     sync_handshake_result handshakeResults[] = { SYNC_HANDSHAKE_CANCEL,
  1254         SYNC_HANDSHAKE_ACCEPTED, SYNC_HANDSHAKE_REJECTED };
  1255 
  1256     for (int i = 0;; ++i) {
  1257         NSError *error = nil;
  1258         XCTAssertFalse([session
  1259                         deliverHandshakeResult:handshakeResults[i]
  1260                         forPartner:forSureNotMe
  1261                         error:&error]);
  1262         XCTAssertNotNil(error);
  1263         XCTAssertEqual([error code], PEP_ILLEGAL_VALUE);
  1264 
  1265         if (handshakeResults[i] == SYNC_HANDSHAKE_REJECTED) {
  1266             break;
  1267         }
  1268     }
  1269 }
  1270 
  1271 #pragma mark - Helpers
  1272 
  1273 - (void)testSendMessageOnSession:(PEPSession *)session
  1274 {
  1275     XCTAssertEqual(self.sendMessageDelegate.messages.count, 0);
  1276     XCTAssertNil(self.sendMessageDelegate.lastMessage);
  1277 
  1278     PEPIdentity *identMe = [[PEPIdentity alloc]
  1279                             initWithAddress:@"me-myself-and-i@pep-project.org"
  1280                             userID:@"me-myself-and-i"
  1281                             userName:@"pEp Me"
  1282                             isOwn:YES];
  1283     NSError *error = nil;
  1284     XCTAssertTrue([session mySelf:identMe error:&error]);
  1285     XCTAssertNil(error);
  1286 
  1287     [self reStartSync];
  1288 
  1289     XCTKVOExpectation *expHaveMessage = [[XCTKVOExpectation alloc]
  1290                                          initWithKeyPath:@"lastMessage"
  1291                                          object:self.sendMessageDelegate];
  1292 
  1293     XCTAssertNotNil(identMe.fingerPrint);
  1294 
  1295     [self waitForExpectations:@[expHaveMessage] timeout:PEPTestInternalSyncTimeout];
  1296     XCTAssertNotNil(self.sendMessageDelegate.lastMessage);
  1297 
  1298     XCTAssertEqual(self.sendMessageDelegate.messages.count, 1);
  1299 }
  1300 
  1301 - (void)reStartSync
  1302 {
  1303     [self.sync shutdown];
  1304     [self startSync];
  1305 }
  1306 
  1307 - (void)startSync
  1308 {
  1309     self.sendMessageDelegate = [PEPSessionTestSendMessageDelegate new];
  1310     self.notifyHandshakeDelegate = [PEPSessionTestNotifyHandshakeDelegate new];
  1311 
  1312     self.sync = [[PEPSync alloc]
  1313                  initWithSendMessageDelegate:self.sendMessageDelegate
  1314                  notifyHandshakeDelegate:self.notifyHandshakeDelegate];
  1315     [self.sync startup];
  1316 }
  1317 
  1318 - (NSNumber * _Nullable)testOutgoingRatingForMessage:(PEPMessage * _Nonnull)theMessage
  1319                                              session:(PEPSession *)session
  1320                                                error:(NSError * _Nullable * _Nullable)error
  1321 {
  1322     NSNumber *ratingOriginal = [session outgoingRatingForMessage:theMessage error:error];
  1323     NSNumber *ratingPreview = [session outgoingRatingPreviewForMessage:theMessage error:nil];
  1324     XCTAssertEqual(ratingOriginal, ratingPreview);
  1325     return ratingOriginal;
  1326 }
  1327 
  1328 - (void)testPassiveModeEnabled:(BOOL)passiveModeEnabled
  1329 {
  1330     [PEPObjCAdapter setPassiveModeEnabled:passiveModeEnabled];
  1331     PEPSession *session = [PEPSession new];
  1332 
  1333     PEPIdentity *identMe = [[PEPIdentity alloc]
  1334                             initWithAddress:@"me-myself-and-i@pep-project.org"
  1335                             userID:@"me-myself-and-i"
  1336                             userName:@"pEp Me"
  1337                             isOwn:YES];
  1338     NSError *error = nil;
  1339     XCTAssertTrue([session mySelf:identMe error:&error]);
  1340     XCTAssertNil(error);
  1341 
  1342     XCTAssertNotNil(identMe.fingerPrint);
  1343 
  1344     PEPIdentity *identAlice = [[PEPIdentity alloc]
  1345                                initWithAddress:@"pep.test.alice@pep-project.org"
  1346                                userID:@"alice_user_id"
  1347                                userName:@"Alice"
  1348                                isOwn:NO];
  1349 
  1350     NSString *shortMessage = @"whatever it may be";
  1351     NSString *longMessage = [NSString stringWithFormat:@"%@ %@", shortMessage, shortMessage];
  1352     PEPMessage *message = [PEPMessage new];
  1353     message.direction = PEP_dir_outgoing;
  1354     message.from = identMe;
  1355     message.to = @[identAlice];
  1356     message.shortMessage = shortMessage;
  1357     message.longMessage = longMessage;
  1358 
  1359     PEPStatus status = PEPStatusKeyNotFound;
  1360     PEPMessage *encryptedMessage =  [session encryptMessage:message
  1361                                                   extraKeys:@[]
  1362                                                      status:&status
  1363                                                       error:&error];
  1364     XCTAssertNil(error);
  1365     XCTAssertEqual(status, PEP_UNENCRYPTED);
  1366 
  1367     if (passiveModeEnabled) {
  1368         XCTAssertNil(encryptedMessage.attachments);
  1369     } else {
  1370         XCTAssertEqual(encryptedMessage.attachments.count, 1);
  1371     }
  1372 }
  1373 
  1374 /**
  1375  Determines the rating for the given identity.
  1376  @return PEPRatingUndefined on error
  1377  */
  1378 - (PEPRating)ratingForIdentity:(PEPIdentity *)identity session:(PEPSession *)session
  1379 {
  1380     NSError *error;
  1381     NSNumber *numRating = [session ratingForIdentity:identity error:&error];
  1382     XCTAssertNil(error);
  1383     return numRating.pEpRating;
  1384 }
  1385 
  1386 - (PEPIdentity *)checkImportingKeyFilePath:(NSString *)filePath address:(NSString *)address
  1387                                     userID:(NSString *)userID
  1388                                fingerPrint:(NSString *)fingerPrint
  1389                                    session:(PEPSession *)session
  1390 {
  1391     if (!session) {
  1392         session = [PEPSession new];
  1393     }
  1394 
  1395     BOOL success = [PEPTestUtils importBundledKey:filePath session:session];
  1396     XCTAssertTrue(success);
  1397 
  1398     if (success) {
  1399         // Our test user:
  1400         PEPIdentity *identTest = [[PEPIdentity alloc]
  1401                                   initWithAddress:address
  1402                                   userID:userID
  1403                                   userName:[NSString stringWithFormat:@"Some User Name %@", userID]
  1404                                   isOwn:NO];
  1405 
  1406         NSError *error = nil;
  1407         XCTAssertTrue([session updateIdentity:identTest error:&error]);
  1408         XCTAssertNil(error);
  1409         XCTAssertNotNil(identTest.fingerPrint);
  1410         XCTAssertEqualObjects(identTest.fingerPrint, fingerPrint);
  1411 
  1412         return identTest;
  1413     } else {
  1414         return nil;
  1415     }
  1416 }
  1417 
  1418 - (PEPIdentity *)checkMySelfImportingKeyFilePath:(NSString *)filePath address:(NSString *)address
  1419                                           userID:(NSString *)userID
  1420                                      fingerPrint:(NSString *)fingerPrint
  1421                                          session:(PEPSession *)session
  1422 {
  1423     XCTAssertTrue([PEPTestUtils importBundledKey:filePath session:session]);
  1424 
  1425     // Our test user:
  1426     PEPIdentity *identTest = [[PEPIdentity alloc]
  1427                               initWithAddress:address
  1428                               userID:userID
  1429                               userName:[NSString stringWithFormat:@"Some User Name %@", userID]
  1430                               isOwn:YES
  1431                               fingerPrint: fingerPrint];
  1432 
  1433     NSError *error;
  1434     XCTAssertTrue([session setOwnKey:identTest fingerprint:fingerPrint error:&error]);
  1435     XCTAssertNil(error);
  1436     XCTAssertNotNil(identTest.fingerPrint);
  1437     XCTAssertEqualObjects(identTest.fingerPrint, fingerPrint);
  1438 
  1439     return identTest;
  1440 }
  1441 
  1442 /**
  1443  Verifies that a partner ID is really a correct Identity.
  1444  Usually used on identities imported as keys, since the engine has problems with them.
  1445  */
  1446 - (void)updateAndVerifyPartnerIdentity:(PEPIdentity *)partnerIdentity session:(PEPSession *)session
  1447 {
  1448     NSError *error = nil;
  1449 
  1450     XCTAssertNotNil(partnerIdentity.fingerPrint);
  1451     XCTAssertTrue([session updateIdentity:partnerIdentity error:&error]);
  1452     XCTAssertNil(error);
  1453     XCTAssertNotNil(partnerIdentity.fingerPrint);
  1454     NSString *fingerprint = partnerIdentity.fingerPrint;
  1455     partnerIdentity.fingerPrint = nil;
  1456     XCTAssertTrue([session updateIdentity:partnerIdentity error:&error]);
  1457     XCTAssertNil(error);
  1458     XCTAssertNotNil(partnerIdentity.fingerPrint);
  1459     XCTAssertEqualObjects(partnerIdentity.fingerPrint, fingerprint);
  1460 }
  1461 
  1462 - (PEPMessage *)mailWrittenToMySelf
  1463 {
  1464     PEPSession *session = [PEPSession new];
  1465 
  1466     // Write a e-mail to yourself ...
  1467     PEPIdentity *me = [PEPTestUtils ownPepIdentityWithAddress:@"me@peptest.ch"
  1468                                                      userName:@"userName"];
  1469     NSError *error = nil;
  1470     XCTAssertTrue([session mySelf:me error:&error]);
  1471     XCTAssertNil(error);
  1472 
  1473     NSString *shortMessage = @"Subject";
  1474     NSString *longMessage = @"Oh, this is a long body text!";
  1475     PEPMessage *mail = [PEPTestUtils mailFrom:me
  1476                                       toIdent:me
  1477                                  shortMessage:shortMessage
  1478                                   longMessage:longMessage
  1479                                      outgoing:YES];
  1480     PEPStatus status = PEP_UNKNOWN_ERROR;
  1481     PEPMessage *encMessage = [session
  1482                               encryptMessage:mail
  1483                               forSelf:me
  1484                               extraKeys:nil
  1485                               status:&status
  1486                               error:&error];
  1487     XCTAssertNil(error);
  1488 
  1489     return encMessage;
  1490 }
  1491 
  1492 - (PEPMessage *)internalEncryptToMySelfKeys:(PEPStringList **)keys
  1493 {
  1494     PEPSession *session = [PEPSession new];
  1495     
  1496     PEPIdentity *me = [PEPTestUtils ownPepIdentityWithAddress:@"me@peptest.ch"
  1497                                                      userName:@"userName"];
  1498     NSError *error = nil;
  1499     XCTAssertTrue([session mySelf:me error:&error]);
  1500     XCTAssertNil(error);
  1501 
  1502     XCTAssertNotNil(me.fingerPrint);
  1503 
  1504     // Create draft
  1505     NSString *shortMessage = @"Subject";
  1506     NSString *longMessage = @"Oh, this is a long body text!";
  1507     PEPMessage *mail = [PEPTestUtils mailFrom:me toIdent:me shortMessage:shortMessage longMessage:longMessage outgoing:YES];
  1508 
  1509     PEP_STATUS status;
  1510     PEPMessage *encMessage = [session
  1511                               encryptMessage:mail
  1512                               forSelf:me
  1513                               extraKeys:nil
  1514                               status:&status
  1515                               error:&error];
  1516     XCTAssertEqual(status, 0);
  1517     XCTAssertEqualObjects(encMessage.shortMessage, @"p≡p");
  1518 
  1519     PEPRating rating;
  1520     error = nil;
  1521     PEPMessage *unencDict = [session
  1522                              decryptMessage:encMessage
  1523                              flags:nil
  1524                              rating:&rating
  1525                              extraKeys:keys
  1526                              status:nil
  1527                              error:&error];
  1528     XCTAssertNotNil(unencDict);
  1529     XCTAssertNil(error);
  1530 
  1531     XCTAssertGreaterThanOrEqual(rating, PEPRatingReliable);
  1532 
  1533     XCTAssertEqualObjects(unencDict.shortMessage, shortMessage);
  1534     XCTAssertEqualObjects(unencDict.longMessage, longMessage);
  1535 
  1536     return unencDict;
  1537 }
  1538 
  1539 - (void)pEpCleanUp
  1540 {
  1541     [PEPTestUtils cleanUp];
  1542 }
  1543 
  1544 - (void)helperXEncStatusForOutgoingEncryptdMailToSelf:(BOOL)toSelf
  1545                                        expectedRating:(PEPRating)expectedRating
  1546 {
  1547     PEPSession *session = [PEPSession new];
  1548 
  1549     // Partner pubkey for the test:
  1550     // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
  1551     // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
  1552     XCTAssertTrue([PEPTestUtils importBundledKey:@"6FF00E97.asc" session:session]);
  1553 
  1554     PEPIdentity *identAlice = [[PEPIdentity alloc]
  1555                                initWithAddress:@"pep.test.alice@pep-project.org"
  1556                                userID:ownUserId
  1557                                userName:@"pEp Test Alice"
  1558                                isOwn:NO
  1559                                fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
  1560     [self updateAndVerifyPartnerIdentity:identAlice session:session];
  1561 
  1562     PEPIdentity *identMe = [[PEPIdentity alloc]
  1563                                initWithAddress:@"me-myself-and-i@pep-project.org"
  1564                                userID:@"me-myself-and-i"
  1565                                userName:@"pEp Me"
  1566                                isOwn:YES];
  1567     NSError *error = nil;
  1568     XCTAssertTrue([session mySelf:identMe error:&error]);
  1569     XCTAssertNil(error);
  1570 
  1571     XCTAssertNotNil(identMe.fingerPrint);
  1572 
  1573     PEPMessage *msg = [PEPMessage new];
  1574     msg.from = identMe;
  1575     msg.to = @[identAlice];
  1576     msg.shortMessage = @"Mail to Alice";
  1577     msg.longMessage = @"Alice?";
  1578     msg.direction = PEP_dir_outgoing;
  1579 
  1580     NSNumber *numRating = [self testOutgoingRatingForMessage:msg session:session error:&error];
  1581     XCTAssertNotNil(numRating);
  1582     XCTAssertNil(error);
  1583     XCTAssertEqual(numRating.pEpRating, PEPRatingReliable);
  1584 
  1585     PEPMessage *encMsg;
  1586 
  1587     PEPStatus statusEnc = PEP_VERSION_MISMATCH;
  1588     if (toSelf) {
  1589         encMsg = [session
  1590                   encryptMessage:msg
  1591                   forSelf:identMe
  1592                   extraKeys:nil
  1593                   status:&statusEnc
  1594                   error:&error];
  1595         XCTAssertEqual(statusEnc, PEP_STATUS_OK);
  1596     } else {
  1597         encMsg = [session encryptMessage:msg extraKeys:nil status:nil error:&error];
  1598         XCTAssertNotNil(encMsg);
  1599         XCTAssertNil(error);
  1600     }
  1601     XCTAssertNotNil(encMsg);
  1602 
  1603     PEPStringList *keys;
  1604     PEPRating pEpRating;
  1605     error = nil;
  1606     PEPMessage *decMsg = [session
  1607                           decryptMessage:encMsg
  1608                           flags:nil
  1609                           rating:&pEpRating
  1610                           extraKeys:&keys
  1611                           status:nil
  1612                           error:&error];
  1613     XCTAssertNil(error);
  1614     XCTAssertNotNil(decMsg);
  1615 
  1616     XCTAssertEqual(pEpRating, expectedRating);
  1617 
  1618     NSArray * encStatusField = nil;
  1619     for (NSArray *field in decMsg.optionalFields) {
  1620         NSString *header = [field[0] lowercaseString];
  1621         if ([header isEqualToString:@"x-encstatus"]) {
  1622             encStatusField = field;
  1623         }
  1624     }
  1625     XCTAssertNotNil(encStatusField);
  1626     if (encStatusField) {
  1627         PEPRating outgoingRating = [session ratingFromString:encStatusField[1]];
  1628         XCTAssertEqual(outgoingRating, expectedRating);
  1629     }
  1630 }
  1631 
  1632 @end