pEpObjCTests/PEPSessionTest.m
author Dirk Zimmermann <dz@pep.security>
Mon, 25 Mar 2019 09:24:05 +0100
branchrefactor
changeset 906 68a503e49d2e
parent 902 32760267cd59
child 907 e4e89231f90e
permissions -rw-r--r--
IOSAD-107 Adapt to changed deliverHandshakeResult.
     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     sync_handshake_result handshakeResults[] = { SYNC_HANDSHAKE_CANCEL,
  1248         SYNC_HANDSHAKE_ACCEPTED, SYNC_HANDSHAKE_REJECTED };
  1249 
  1250     for (int i = 0;; ++i) {
  1251         NSError *error = nil;
  1252         XCTAssertFalse([session
  1253                         deliverHandshakeResult:handshakeResults[i]
  1254                         error:&error]);
  1255         XCTAssertNotNil(error);
  1256         XCTAssertEqual([error code], PEP_ILLEGAL_VALUE);
  1257 
  1258         if (handshakeResults[i] == SYNC_HANDSHAKE_REJECTED) {
  1259             break;
  1260         }
  1261     }
  1262 }
  1263 
  1264 #pragma mark - Helpers
  1265 
  1266 - (void)testSendMessageOnSession:(PEPSession *)session
  1267 {
  1268     XCTAssertEqual(self.sendMessageDelegate.messages.count, 0);
  1269     XCTAssertNil(self.sendMessageDelegate.lastMessage);
  1270 
  1271     PEPIdentity *identMe = [[PEPIdentity alloc]
  1272                             initWithAddress:@"me-myself-and-i@pep-project.org"
  1273                             userID:@"me-myself-and-i"
  1274                             userName:@"pEp Me"
  1275                             isOwn:YES];
  1276     NSError *error = nil;
  1277     XCTAssertTrue([session mySelf:identMe error:&error]);
  1278     XCTAssertNil(error);
  1279 
  1280     [self reStartSync];
  1281 
  1282     XCTKVOExpectation *expHaveMessage = [[XCTKVOExpectation alloc]
  1283                                          initWithKeyPath:@"lastMessage"
  1284                                          object:self.sendMessageDelegate];
  1285 
  1286     XCTAssertNotNil(identMe.fingerPrint);
  1287 
  1288     [self waitForExpectations:@[expHaveMessage] timeout:PEPTestInternalSyncTimeout];
  1289     XCTAssertNotNil(self.sendMessageDelegate.lastMessage);
  1290 
  1291     XCTAssertEqual(self.sendMessageDelegate.messages.count, 1);
  1292 }
  1293 
  1294 - (void)reStartSync
  1295 {
  1296     [self.sync shutdown];
  1297     [self startSync];
  1298 }
  1299 
  1300 - (void)startSync
  1301 {
  1302     self.sendMessageDelegate = [PEPSessionTestSendMessageDelegate new];
  1303     self.notifyHandshakeDelegate = [PEPSessionTestNotifyHandshakeDelegate new];
  1304 
  1305     self.sync = [[PEPSync alloc]
  1306                  initWithSendMessageDelegate:self.sendMessageDelegate
  1307                  notifyHandshakeDelegate:self.notifyHandshakeDelegate];
  1308     [self.sync startup];
  1309 }
  1310 
  1311 - (NSNumber * _Nullable)testOutgoingRatingForMessage:(PEPMessage * _Nonnull)theMessage
  1312                                              session:(PEPSession *)session
  1313                                                error:(NSError * _Nullable * _Nullable)error
  1314 {
  1315     NSNumber *ratingOriginal = [session outgoingRatingForMessage:theMessage error:error];
  1316     NSNumber *ratingPreview = [session outgoingRatingPreviewForMessage:theMessage error:nil];
  1317     XCTAssertEqual(ratingOriginal, ratingPreview);
  1318     return ratingOriginal;
  1319 }
  1320 
  1321 - (void)testPassiveModeEnabled:(BOOL)passiveModeEnabled
  1322 {
  1323     [PEPObjCAdapter setPassiveModeEnabled:passiveModeEnabled];
  1324     PEPSession *session = [PEPSession new];
  1325 
  1326     PEPIdentity *identMe = [[PEPIdentity alloc]
  1327                             initWithAddress:@"me-myself-and-i@pep-project.org"
  1328                             userID:@"me-myself-and-i"
  1329                             userName:@"pEp Me"
  1330                             isOwn:YES];
  1331     NSError *error = nil;
  1332     XCTAssertTrue([session mySelf:identMe error:&error]);
  1333     XCTAssertNil(error);
  1334 
  1335     XCTAssertNotNil(identMe.fingerPrint);
  1336 
  1337     PEPIdentity *identAlice = [[PEPIdentity alloc]
  1338                                initWithAddress:@"pep.test.alice@pep-project.org"
  1339                                userID:@"alice_user_id"
  1340                                userName:@"Alice"
  1341                                isOwn:NO];
  1342 
  1343     NSString *shortMessage = @"whatever it may be";
  1344     NSString *longMessage = [NSString stringWithFormat:@"%@ %@", shortMessage, shortMessage];
  1345     PEPMessage *message = [PEPMessage new];
  1346     message.direction = PEP_dir_outgoing;
  1347     message.from = identMe;
  1348     message.to = @[identAlice];
  1349     message.shortMessage = shortMessage;
  1350     message.longMessage = longMessage;
  1351 
  1352     PEPStatus status = PEPStatusKeyNotFound;
  1353     PEPMessage *encryptedMessage =  [session encryptMessage:message
  1354                                                   extraKeys:@[]
  1355                                                      status:&status
  1356                                                       error:&error];
  1357     XCTAssertNil(error);
  1358     XCTAssertEqual(status, PEP_UNENCRYPTED);
  1359 
  1360     if (passiveModeEnabled) {
  1361         XCTAssertNil(encryptedMessage.attachments);
  1362     } else {
  1363         XCTAssertEqual(encryptedMessage.attachments.count, 1);
  1364     }
  1365 }
  1366 
  1367 /**
  1368  Determines the rating for the given identity.
  1369  @return PEPRatingUndefined on error
  1370  */
  1371 - (PEPRating)ratingForIdentity:(PEPIdentity *)identity session:(PEPSession *)session
  1372 {
  1373     NSError *error;
  1374     NSNumber *numRating = [session ratingForIdentity:identity error:&error];
  1375     XCTAssertNil(error);
  1376     return numRating.pEpRating;
  1377 }
  1378 
  1379 - (PEPIdentity *)checkImportingKeyFilePath:(NSString *)filePath address:(NSString *)address
  1380                                     userID:(NSString *)userID
  1381                                fingerPrint:(NSString *)fingerPrint
  1382                                    session:(PEPSession *)session
  1383 {
  1384     if (!session) {
  1385         session = [PEPSession new];
  1386     }
  1387 
  1388     BOOL success = [PEPTestUtils importBundledKey:filePath session:session];
  1389     XCTAssertTrue(success);
  1390 
  1391     if (success) {
  1392         // Our test user:
  1393         PEPIdentity *identTest = [[PEPIdentity alloc]
  1394                                   initWithAddress:address
  1395                                   userID:userID
  1396                                   userName:[NSString stringWithFormat:@"Some User Name %@", userID]
  1397                                   isOwn:NO];
  1398 
  1399         NSError *error = nil;
  1400         XCTAssertTrue([session updateIdentity:identTest error:&error]);
  1401         XCTAssertNil(error);
  1402         XCTAssertNotNil(identTest.fingerPrint);
  1403         XCTAssertEqualObjects(identTest.fingerPrint, fingerPrint);
  1404 
  1405         return identTest;
  1406     } else {
  1407         return nil;
  1408     }
  1409 }
  1410 
  1411 - (PEPIdentity *)checkMySelfImportingKeyFilePath:(NSString *)filePath address:(NSString *)address
  1412                                           userID:(NSString *)userID
  1413                                      fingerPrint:(NSString *)fingerPrint
  1414                                          session:(PEPSession *)session
  1415 {
  1416     XCTAssertTrue([PEPTestUtils importBundledKey:filePath session:session]);
  1417 
  1418     // Our test user:
  1419     PEPIdentity *identTest = [[PEPIdentity alloc]
  1420                               initWithAddress:address
  1421                               userID:userID
  1422                               userName:[NSString stringWithFormat:@"Some User Name %@", userID]
  1423                               isOwn:YES
  1424                               fingerPrint: fingerPrint];
  1425 
  1426     NSError *error;
  1427     XCTAssertTrue([session setOwnKey:identTest fingerprint:fingerPrint error:&error]);
  1428     XCTAssertNil(error);
  1429     XCTAssertNotNil(identTest.fingerPrint);
  1430     XCTAssertEqualObjects(identTest.fingerPrint, fingerPrint);
  1431 
  1432     return identTest;
  1433 }
  1434 
  1435 /**
  1436  Verifies that a partner ID is really a correct Identity.
  1437  Usually used on identities imported as keys, since the engine has problems with them.
  1438  */
  1439 - (void)updateAndVerifyPartnerIdentity:(PEPIdentity *)partnerIdentity session:(PEPSession *)session
  1440 {
  1441     NSError *error = nil;
  1442 
  1443     XCTAssertNotNil(partnerIdentity.fingerPrint);
  1444     XCTAssertTrue([session updateIdentity:partnerIdentity error:&error]);
  1445     XCTAssertNil(error);
  1446     XCTAssertNotNil(partnerIdentity.fingerPrint);
  1447     NSString *fingerprint = partnerIdentity.fingerPrint;
  1448     partnerIdentity.fingerPrint = nil;
  1449     XCTAssertTrue([session updateIdentity:partnerIdentity error:&error]);
  1450     XCTAssertNil(error);
  1451     XCTAssertNotNil(partnerIdentity.fingerPrint);
  1452     XCTAssertEqualObjects(partnerIdentity.fingerPrint, fingerprint);
  1453 }
  1454 
  1455 - (PEPMessage *)mailWrittenToMySelf
  1456 {
  1457     PEPSession *session = [PEPSession new];
  1458 
  1459     // Write a e-mail to yourself ...
  1460     PEPIdentity *me = [PEPTestUtils ownPepIdentityWithAddress:@"me@peptest.ch"
  1461                                                      userName:@"userName"];
  1462     NSError *error = nil;
  1463     XCTAssertTrue([session mySelf:me error:&error]);
  1464     XCTAssertNil(error);
  1465 
  1466     NSString *shortMessage = @"Subject";
  1467     NSString *longMessage = @"Oh, this is a long body text!";
  1468     PEPMessage *mail = [PEPTestUtils mailFrom:me
  1469                                       toIdent:me
  1470                                  shortMessage:shortMessage
  1471                                   longMessage:longMessage
  1472                                      outgoing:YES];
  1473     PEPStatus status = PEP_UNKNOWN_ERROR;
  1474     PEPMessage *encMessage = [session
  1475                               encryptMessage:mail
  1476                               forSelf:me
  1477                               extraKeys:nil
  1478                               status:&status
  1479                               error:&error];
  1480     XCTAssertNil(error);
  1481 
  1482     return encMessage;
  1483 }
  1484 
  1485 - (PEPMessage *)internalEncryptToMySelfKeys:(PEPStringList **)keys
  1486 {
  1487     PEPSession *session = [PEPSession new];
  1488     
  1489     PEPIdentity *me = [PEPTestUtils ownPepIdentityWithAddress:@"me@peptest.ch"
  1490                                                      userName:@"userName"];
  1491     NSError *error = nil;
  1492     XCTAssertTrue([session mySelf:me error:&error]);
  1493     XCTAssertNil(error);
  1494 
  1495     XCTAssertNotNil(me.fingerPrint);
  1496 
  1497     // Create draft
  1498     NSString *shortMessage = @"Subject";
  1499     NSString *longMessage = @"Oh, this is a long body text!";
  1500     PEPMessage *mail = [PEPTestUtils mailFrom:me toIdent:me shortMessage:shortMessage longMessage:longMessage outgoing:YES];
  1501 
  1502     PEP_STATUS status;
  1503     PEPMessage *encMessage = [session
  1504                               encryptMessage:mail
  1505                               forSelf:me
  1506                               extraKeys:nil
  1507                               status:&status
  1508                               error:&error];
  1509     XCTAssertEqual(status, 0);
  1510     XCTAssertEqualObjects(encMessage.shortMessage, @"p≡p");
  1511 
  1512     PEPRating rating;
  1513     error = nil;
  1514     PEPMessage *unencDict = [session
  1515                              decryptMessage:encMessage
  1516                              flags:nil
  1517                              rating:&rating
  1518                              extraKeys:keys
  1519                              status:nil
  1520                              error:&error];
  1521     XCTAssertNotNil(unencDict);
  1522     XCTAssertNil(error);
  1523 
  1524     XCTAssertGreaterThanOrEqual(rating, PEPRatingReliable);
  1525 
  1526     XCTAssertEqualObjects(unencDict.shortMessage, shortMessage);
  1527     XCTAssertEqualObjects(unencDict.longMessage, longMessage);
  1528 
  1529     return unencDict;
  1530 }
  1531 
  1532 - (void)pEpCleanUp
  1533 {
  1534     [PEPTestUtils cleanUp];
  1535 }
  1536 
  1537 - (void)helperXEncStatusForOutgoingEncryptdMailToSelf:(BOOL)toSelf
  1538                                        expectedRating:(PEPRating)expectedRating
  1539 {
  1540     PEPSession *session = [PEPSession new];
  1541 
  1542     // Partner pubkey for the test:
  1543     // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
  1544     // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
  1545     XCTAssertTrue([PEPTestUtils importBundledKey:@"6FF00E97.asc" session:session]);
  1546 
  1547     PEPIdentity *identAlice = [[PEPIdentity alloc]
  1548                                initWithAddress:@"pep.test.alice@pep-project.org"
  1549                                userID:ownUserId
  1550                                userName:@"pEp Test Alice"
  1551                                isOwn:NO
  1552                                fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
  1553     [self updateAndVerifyPartnerIdentity:identAlice session:session];
  1554 
  1555     PEPIdentity *identMe = [[PEPIdentity alloc]
  1556                                initWithAddress:@"me-myself-and-i@pep-project.org"
  1557                                userID:@"me-myself-and-i"
  1558                                userName:@"pEp Me"
  1559                                isOwn:YES];
  1560     NSError *error = nil;
  1561     XCTAssertTrue([session mySelf:identMe error:&error]);
  1562     XCTAssertNil(error);
  1563 
  1564     XCTAssertNotNil(identMe.fingerPrint);
  1565 
  1566     PEPMessage *msg = [PEPMessage new];
  1567     msg.from = identMe;
  1568     msg.to = @[identAlice];
  1569     msg.shortMessage = @"Mail to Alice";
  1570     msg.longMessage = @"Alice?";
  1571     msg.direction = PEP_dir_outgoing;
  1572 
  1573     NSNumber *numRating = [self testOutgoingRatingForMessage:msg session:session error:&error];
  1574     XCTAssertNotNil(numRating);
  1575     XCTAssertNil(error);
  1576     XCTAssertEqual(numRating.pEpRating, PEPRatingReliable);
  1577 
  1578     PEPMessage *encMsg;
  1579 
  1580     PEPStatus statusEnc = PEP_VERSION_MISMATCH;
  1581     if (toSelf) {
  1582         encMsg = [session
  1583                   encryptMessage:msg
  1584                   forSelf:identMe
  1585                   extraKeys:nil
  1586                   status:&statusEnc
  1587                   error:&error];
  1588         XCTAssertEqual(statusEnc, PEP_STATUS_OK);
  1589     } else {
  1590         encMsg = [session encryptMessage:msg extraKeys:nil status:nil error:&error];
  1591         XCTAssertNotNil(encMsg);
  1592         XCTAssertNil(error);
  1593     }
  1594     XCTAssertNotNil(encMsg);
  1595 
  1596     PEPStringList *keys;
  1597     PEPRating pEpRating;
  1598     error = nil;
  1599     PEPMessage *decMsg = [session
  1600                           decryptMessage:encMsg
  1601                           flags:nil
  1602                           rating:&pEpRating
  1603                           extraKeys:&keys
  1604                           status:nil
  1605                           error:&error];
  1606     XCTAssertNil(error);
  1607     XCTAssertNotNil(decMsg);
  1608 
  1609     XCTAssertEqual(pEpRating, expectedRating);
  1610 
  1611     NSArray * encStatusField = nil;
  1612     for (NSArray *field in decMsg.optionalFields) {
  1613         NSString *header = [field[0] lowercaseString];
  1614         if ([header isEqualToString:@"x-encstatus"]) {
  1615             encStatusField = field;
  1616         }
  1617     }
  1618     XCTAssertNotNil(encStatusField);
  1619     if (encStatusField) {
  1620         PEPRating outgoingRating = [session ratingFromString:encStatusField[1]];
  1621         XCTAssertEqual(outgoingRating, expectedRating);
  1622     }
  1623 }
  1624 
  1625 @end