pEpObjCTests/PEPSessionTest.m
author Dirk Zimmermann <dz@pep.security>
Tue, 14 Jan 2020 08:20:57 +0100
changeset 1401 7915afcc725c
parent 1399 57298a88699c
permissions -rw-r--r--
merge IOSAD-164
     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     [PEPObjCAdapter setUnEncryptedSubjectEnabled:NO];
    40 }
    41 
    42 - (void)tearDown
    43 {
    44     [self shutdownSync];
    45     [self pEpCleanUp];
    46     [super tearDown];
    47 }
    48 
    49 - (void)testTrustWords
    50 {
    51     PEPSession *session = [PEPSession new];
    52 
    53     NSError *error = nil;
    54     NSArray *trustwords = [session
    55                            trustwordsForFingerprint:@"DB47DB47DB47DB47DB47DB47DB47DB47DB47DB47"
    56                            languageID:@"en"
    57                            shortened:false
    58                            error:&error];
    59     XCTAssertNil(error);
    60     XCTAssertEqual([trustwords count], 10);
    61 
    62     for(id word in trustwords)
    63         XCTAssertEqualObjects(word, @"BAPTISMAL");
    64 }
    65 
    66 - (void)testGenKey
    67 {
    68     PEPSession *session = [PEPSession new];
    69 
    70     PEPIdentity *identMe = [[PEPIdentity alloc]
    71                             initWithAddress:@"pep.test.iosgenkey@pep-project.org"
    72                             userID:@"Me"
    73                             userName:@"pEp Test iOS GenKey"
    74                             isOwn:YES];
    75 
    76     NSError *error = nil;
    77     XCTAssertTrue([session mySelf:identMe error:&error]);
    78     XCTAssertNil(error);
    79 
    80     XCTAssertNotNil(identMe.fingerPrint);
    81     XCTAssertNotEqual(identMe.commType, PEPCommTypeUnknown);
    82 
    83     XCTAssertTrue([identMe isPEPUser:session error:&error]);
    84 }
    85 
    86 - (void)testMySelfCommType
    87 {
    88     PEPSession *session = [PEPSession new];
    89 
    90     PEPIdentity *identMe = [[PEPIdentity alloc]
    91                             initWithAddress:@"pep.test.iosgenkey@pep-project.org"
    92                             userID:@"pep.test.iosgenkey@pep-project.org_userID"
    93                             userName:@"pEp Test iOS GenKey"
    94                             isOwn:YES];
    95 
    96     NSError *error = nil;
    97     XCTAssertTrue([session mySelf:identMe error:&error]);
    98     XCTAssertNil(error);
    99 
   100     XCTAssertNotNil(identMe.fingerPrint);
   101     XCTAssertNotEqual(identMe.commType, PEPCommTypeUnknown);
   102 
   103     XCTAssertTrue([identMe isPEPUser:session error:&error]);
   104 
   105     dispatch_queue_t queue = dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0);
   106     dispatch_sync(queue, ^{
   107         NSError *innerError = nil;
   108         PEPSession *session2 = [PEPSession new];
   109 
   110         // Now simulate an update from the app, which usually only caches
   111         // kPepUsername, kPepAddress and optionally kPepUserID.
   112         PEPIdentity *identMe2 = [[PEPIdentity alloc]
   113                                  initWithAddress:identMe.address
   114                                  userID:identMe.userID
   115                                  userName:identMe.userName
   116                                  isOwn:NO];
   117 
   118         XCTAssertTrue([session2 mySelf:identMe2 error:&innerError]);
   119         XCTAssertNil(innerError);
   120 
   121         XCTAssertNotNil(identMe2.fingerPrint);
   122         XCTAssertTrue([identMe2 isPEPUser:session error:&innerError]);
   123         XCTAssertEqualObjects(identMe2.fingerPrint, identMe.fingerPrint);
   124 
   125         // Now pretend the app only knows kPepUsername and kPepAddress
   126         PEPIdentity *identMe3 = [PEPTestUtils foreignPepIdentityWithAddress:identMe.address
   127                                                                    userName:identMe.userName];
   128         XCTAssertTrue([session2 mySelf:identMe3 error:&innerError]);
   129         XCTAssertNil(innerError);
   130 
   131         XCTAssertNotNil(identMe3.fingerPrint);
   132         XCTAssertTrue([identMe3 isPEPUser:session error:&innerError]);
   133         XCTAssertEqualObjects(identMe3.fingerPrint, identMe.fingerPrint);
   134 
   135         XCTAssertEqualObjects(identMe.address, identMe2.address);
   136         XCTAssertEqualObjects(identMe.address, identMe3.address);
   137         XCTAssertEqual(identMe.commType, identMe2.commType);
   138         XCTAssertEqual(identMe.commType, identMe3.commType);
   139     });
   140 }
   141 
   142 - (void)testPartnerWithoutFingerPrint
   143 {
   144     PEPSession *session = [PEPSession new];
   145 
   146     PEPIdentity *identRandom = [[PEPIdentity alloc]
   147                                 initWithAddress:@"does_not_exist@example.com"
   148                                 userID:@"got_out"
   149                                 userName:@"No Way Not Even Alice"
   150                                 isOwn:NO];
   151 
   152     NSError *error = nil;
   153     XCTAssertTrue([session updateIdentity:identRandom error:&error]);
   154     XCTAssertNil(error);
   155     XCTAssertNil(identRandom.fingerPrint);
   156 }
   157 
   158 - (void)testImportPartnerKeys
   159 {
   160     XCTAssertNotNil([self checkImportingKeyFilePath:@"6FF00E97_sec.asc"
   161                                             address:@"pep.test.alice@pep-project.org"
   162                                              userID:@"This Is Alice"
   163                                         fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
   164                                             session: nil]);
   165 
   166     XCTAssertNotNil([self checkImportingKeyFilePath:@"0xC9C2EE39.asc"
   167                                             address:@"pep.test.bob@pep-project.org"
   168                                              userID:@"This Is Bob"
   169                                         fingerPrint:@"BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39"
   170                                             session: nil]);
   171 }
   172 
   173 - (void)testIdentityRating
   174 {
   175     PEPSession *session = [PEPSession new];
   176 
   177     PEPIdentity *me = [self
   178                        checkMySelfImportingKeyFilePath:@"6FF00E97_sec.asc"
   179                        address:@"pep.test.alice@pep-project.org"
   180                        userID:@"Alice_User_ID"
   181                        fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
   182                        session:session];
   183     XCTAssertEqual([self ratingForIdentity:me session:session], PEPRatingTrustedAndAnonymized);
   184 
   185     PEPIdentity *alice = [self
   186                           checkImportingKeyFilePath:@"6FF00E97_sec.asc"
   187                           address:@"pep.test.alice@pep-project.org"
   188                           userID:@"This Is Alice"
   189                           fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
   190                           session: session];
   191     XCTAssertNotNil(alice);
   192     XCTAssertEqual([self ratingForIdentity:alice session:session], PEPRatingReliable);
   193 }
   194 
   195 /** ENGINE-409 */
   196 - (void)testIdentityRatingMistrustReset
   197 {
   198     PEPSession *session = [PEPSession new];
   199 
   200     PEPIdentity *me = [[PEPIdentity alloc]
   201                        initWithAddress:@"me@example.org"
   202                        userID:@"me_myself"
   203                        userName:@"Me Me"
   204                        isOwn:YES];
   205 
   206     NSError *error = nil;
   207     XCTAssertTrue([session mySelf:me error:&error]);
   208     XCTAssertNil(error);
   209 
   210     XCTAssertNotNil(me.fingerPrint);
   211     XCTAssertEqual([self ratingForIdentity:me session:session], PEPRatingTrustedAndAnonymized);
   212 
   213     PEPIdentity *alice = [self
   214                           checkImportingKeyFilePath:@"6FF00E97_sec.asc"
   215                           address:@"pep.test.alice@pep-project.org"
   216                           userID:@"This Is Alice"
   217                           fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
   218                           session: session];
   219     XCTAssertNotNil(alice);
   220     XCTAssertEqual([self ratingForIdentity:alice session:session], PEPRatingReliable);
   221 
   222     XCTAssertTrue([session keyMistrusted:alice error:&error]);
   223     XCTAssertNil(error);
   224     XCTAssertEqual([self ratingForIdentity:alice session:session], PEPRatingHaveNoKey);
   225 }
   226 
   227 - (void)testIdentityRatingTrustResetMistrustUndo
   228 {
   229     PEPSession *session = [PEPSession new];
   230 
   231     PEPIdentity *me = [[PEPIdentity alloc]
   232                        initWithAddress:@"me@example.org"
   233                        userID:@"me_myself"
   234                        userName:@"Me Me"
   235                        isOwn:YES];
   236 
   237     NSError *error = nil;
   238     XCTAssertTrue([session mySelf:me error:&error]);
   239     XCTAssertNil(error);
   240 
   241     XCTAssertNotNil(me.fingerPrint);
   242     XCTAssertEqual([self ratingForIdentity:me session:session], PEPRatingTrustedAndAnonymized);
   243 
   244     PEPIdentity *alice = [self
   245                           checkImportingKeyFilePath:@"6FF00E97_sec.asc"
   246                           address:@"pep.test.alice@pep-project.org"
   247                           userID:@"This Is Alice"
   248                           fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
   249                           session: session];
   250     XCTAssertNotNil(alice);
   251     XCTAssertEqual([self ratingForIdentity:alice session:session], PEPRatingReliable);
   252 
   253     XCTAssertTrue([session trustPersonalKey:alice error:&error]);
   254     XCTAssertNil(error);
   255     XCTAssertEqual([self ratingForIdentity:alice session:session], PEPRatingTrusted);
   256 
   257     XCTAssertTrue([session keyResetTrust:alice error:&error]);
   258     XCTAssertNil(error);
   259     XCTAssertEqual([self ratingForIdentity:alice session:session], PEPRatingReliable);
   260 
   261     XCTAssertTrue([session keyMistrusted:alice error:&error]);
   262     XCTAssertNil(error);
   263     XCTAssertEqual([self ratingForIdentity:alice session:session], PEPRatingHaveNoKey);
   264 }
   265 
   266 /** ENGINE-384 */
   267 - (void)testIdentityRatingCrash
   268 {
   269     PEPSession *session = [PEPSession new];
   270 
   271     PEPIdentity *me = [[PEPIdentity alloc]
   272                        initWithAddress:@"me@example.org"
   273                        userID:@"me_myself"
   274                        userName:@"Me Me"
   275                        isOwn:YES];
   276     NSError *error = nil;
   277     XCTAssertTrue([session mySelf:me error:&error]);
   278     XCTAssertNil(error);
   279 
   280     XCTAssertNotNil(me.fingerPrint);
   281     XCTAssertEqual([self ratingForIdentity:me session:session], PEPRatingTrustedAndAnonymized);
   282 
   283     PEPIdentity *alice = [self
   284                           checkImportingKeyFilePath:@"6FF00E97_sec.asc"
   285                           address:@"pep.test.alice@pep-project.org"
   286                           userID:@"This Is Alice"
   287                           fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
   288                           session: session];
   289     XCTAssertNotNil(alice);
   290     XCTAssertEqual([self ratingForIdentity:alice session:session], PEPRatingReliable);
   291 
   292     XCTAssertTrue([session trustPersonalKey:alice error:&error]);
   293     XCTAssertNil(error);
   294     XCTAssertEqual([self ratingForIdentity:alice session:session], PEPRatingTrusted);
   295 
   296     XCTAssertTrue([session keyResetTrust:alice error:&error]);
   297     XCTAssertNil(error);
   298     XCTAssertEqual([self ratingForIdentity:alice session:session], PEPRatingReliable);
   299 
   300     XCTAssertTrue([session keyMistrusted:alice error:&error]);
   301     XCTAssertNil(error);
   302     XCTAssertEqual([self ratingForIdentity:alice session:session], PEPRatingHaveNoKey);
   303 }
   304 
   305 /**
   306  Try to provoke a SQLITE_BUSY (ENGINE-374)
   307  */
   308 - (void)testIdentityRatingTrustResetMistrustUndoBusy
   309 {
   310     PEPSession *session = [PEPSession new];
   311 
   312     PEPIdentity *me = [[PEPIdentity alloc]
   313                        initWithAddress:@"me@example.org"
   314                        userID:@"me_myself"
   315                        userName:@"Me Me"
   316                        isOwn:YES];
   317     NSError *error = nil;
   318     XCTAssertTrue([session mySelf:me error:&error]);
   319     XCTAssertNil(error);
   320 
   321     XCTAssertNotNil(me.fingerPrint);
   322     XCTAssertEqual([self ratingForIdentity:me session:session], PEPRatingTrustedAndAnonymized);
   323 
   324     PEPIdentity *alice = [self
   325                           checkImportingKeyFilePath:@"6FF00E97_sec.asc"
   326                           address:@"pep.test.alice@pep-project.org"
   327                           userID:@"This Is Alice"
   328                           fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
   329                           session: session];
   330     XCTAssertNotNil(alice);
   331     XCTAssertEqual([self ratingForIdentity:alice session:session], PEPRatingReliable);
   332 
   333     void (^encryptingBlock)(void) = ^{
   334         PEPSession *innerSession = [PEPSession new];
   335         PEPMessage *msg = [PEPMessage new];
   336         msg.from = me;
   337         msg.to = @[alice];
   338         msg.shortMessage = @"The subject";
   339         msg.longMessage = @"Lots and lots of text";
   340         msg.direction = PEPMsgDirectionIncoming;
   341 
   342         PEPStatus status;
   343         NSError *error = nil;
   344         PEPMessage *encMsg = [innerSession
   345                               encryptMessage:msg
   346                               forSelf:me
   347                               extraKeys:nil
   348                               status:&status error:&error];
   349         XCTAssertEqual(status, PEPStatusOK);
   350         XCTAssertNotNil(encMsg);
   351     };
   352 
   353     dispatch_group_t backgroundGroup = dispatch_group_create();
   354     dispatch_group_async(backgroundGroup,
   355                          dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), encryptingBlock);
   356 
   357     XCTAssertTrue([session trustPersonalKey:alice error:&error]);
   358     XCTAssertNil(error);
   359     XCTAssertEqual([self ratingForIdentity:alice session:session], PEPRatingTrusted);
   360 
   361     XCTAssertTrue([session keyResetTrust:alice error:&error]);
   362     XCTAssertNil(error);
   363     XCTAssertEqual([self ratingForIdentity:alice session:session], PEPRatingReliable);
   364 
   365     XCTAssertTrue([session keyMistrusted:alice error:&error]);
   366     XCTAssertNil(error);
   367     XCTAssertEqual([self ratingForIdentity:alice session:session], PEPRatingHaveNoKey);
   368 }
   369 
   370 - (void)testOutgoingColors
   371 {
   372     PEPSession *session = [PEPSession new];
   373 
   374     // Our test user :
   375     // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
   376     // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
   377     XCTAssertTrue([PEPTestUtils importBundledKey:@"6FF00E97_sec.asc" session:session]);
   378 
   379     // Our test user :
   380     PEPIdentity *identAlice = [self
   381                                checkMySelfImportingKeyFilePath:@"6FF00E97_sec.asc"
   382                                address:@"pep.test.alice@pep-project.org"
   383                                userID:@"Alice_User_ID"
   384                                fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
   385                                session:session];
   386 
   387     //Message
   388 
   389     {
   390         PEPIdentity *identUnknownBob = [[PEPIdentity alloc]
   391                                         initWithAddress:@"pep.test.unknown.bob@pep-project.org"
   392                                         userID:@"4242" userName:@"pEp Test Bob Unknown"
   393                                         isOwn:NO];
   394 
   395         PEPMessage *msgGray = [PEPMessage new];
   396         msgGray.from = identAlice;
   397         msgGray.to = @[identUnknownBob];
   398         msgGray.shortMessage = @"All Gray Test";
   399         msgGray.longMessage = @"This is a text content";
   400         msgGray.direction = PEPMsgDirectionOutgoing;
   401 
   402         NSError *error = nil;
   403 
   404         // Test with unknown Bob
   405         NSNumber *numRating = [self
   406                                testOutgoingRatingForMessage:msgGray
   407                                session:session
   408                                error:&error];
   409         XCTAssertNotNil(numRating);
   410         XCTAssertNil(error);
   411         XCTAssertEqual(numRating.pEpRating, PEPRatingUnencrypted);
   412     }
   413 
   414     PEPIdentity *identBob = [self
   415                              checkImportingKeyFilePath:@"0xC9C2EE39.asc"
   416                              address:@"pep.test.bob@pep-project.org"
   417                              userID:@"42"
   418                              fingerPrint:@"BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39"
   419                              session: session];
   420     XCTAssertNotNil(identBob);
   421 
   422     PEPMessage *msg = [PEPMessage new];
   423     msg.from = identAlice;
   424     msg.to = @[identBob];
   425     msg.shortMessage = @"All Gray Test";
   426     msg.longMessage = @"This is a text content";
   427     msg.direction = PEPMsgDirectionOutgoing;
   428 
   429     NSError *error = nil;
   430 
   431     // Should be yellow, since no handshake happened.
   432     NSNumber *numRating = [self testOutgoingRatingForMessage:msg session:session error:&error];
   433     XCTAssertNotNil(numRating);
   434     XCTAssertNil(error);
   435     XCTAssertEqual(numRating.pEpRating, PEPRatingReliable);
   436 
   437     PEPRating rating = [self ratingForIdentity:identBob session:session];
   438     XCTAssertEqual(rating, PEPRatingReliable);
   439 
   440     // Let' say we got that handshake, set PEP_ct_confirmed in Bob's identity
   441     XCTAssertTrue([session trustPersonalKey:identBob error:&error]);
   442     XCTAssertNil(error);
   443 
   444     // This time it should be green
   445     numRating = [self testOutgoingRatingForMessage:msg session:session error:&error];
   446     XCTAssertNotNil(numRating);
   447     XCTAssertNil(error);
   448     XCTAssertEqual(numRating.pEpRating, PEPRatingTrusted);
   449 
   450     rating = [self ratingForIdentity:identBob session:session];
   451     XCTAssertEqual(rating, PEPRatingTrusted);
   452 
   453     // Let' say we undo handshake
   454     XCTAssertTrue([session keyResetTrust:identBob error:&error]);
   455     XCTAssertNil(error);
   456 
   457     // Yellow ?
   458     numRating = [self testOutgoingRatingForMessage:msg session:session error:&error];
   459     XCTAssertNotNil(numRating);
   460     XCTAssertNil(error);
   461     XCTAssertEqual(numRating.pEpRating, PEPRatingReliable);
   462 
   463     // mistrust Bob
   464     XCTAssertTrue([session keyMistrusted:identBob error:&error]);
   465     XCTAssertNil(error);
   466 
   467     identBob.fingerPrint = nil;
   468     XCTAssertTrue([session updateIdentity:identBob error:&error]);
   469     XCTAssertNil(error);
   470     XCTAssertNil(identBob.fingerPrint);
   471 
   472     // Gray == PEPRatingUnencrypted
   473     numRating = [self testOutgoingRatingForMessage:msg session:session error:&error];
   474     XCTAssertNotNil(numRating);
   475     XCTAssertNil(error);
   476     XCTAssertEqual(numRating.pEpRating, PEPRatingUnencrypted);
   477 }
   478 
   479 
   480 - (void)testOutgoingBccColors
   481 {
   482     PEPSession *session = [PEPSession new];
   483 
   484     // Our test user :
   485     // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
   486     // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
   487     XCTAssertTrue([PEPTestUtils importBundledKey:@"6FF00E97_sec.asc" session:session]);
   488 
   489     PEPIdentity *identAlice = [[PEPIdentity alloc]
   490                                initWithAddress:@"pep.test.alice@pep-project.org"
   491                                userID:ownUserId
   492                                userName:@"pEp Test Alice"
   493                                isOwn:YES
   494                                fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
   495 
   496     NSError *error = nil;
   497     XCTAssertTrue([session mySelf:identAlice error:&error]);
   498     XCTAssertNil(error);
   499 
   500     PEPMessage *msg = [PEPMessage new];
   501     msg.from = identAlice;
   502     msg.to = @[[[PEPIdentity alloc] initWithAddress:@"pep.test.bob@pep-project.org"
   503                                              userID:@"42" userName:@"pEp Test Bob" isOwn:NO]];
   504     msg.shortMessage = @"All Green Test";
   505     msg.longMessage = @"This is a text content";
   506     msg.direction = PEPMsgDirectionOutgoing;
   507 
   508     // Test with unknown Bob
   509     PEPRating rating;
   510     NSNumber *numRating = [self testOutgoingRatingForMessage:msg session:session error:&error];
   511     XCTAssertNotNil(numRating);
   512     XCTAssertNil(error);
   513     XCTAssertEqual(numRating.pEpRating, PEPRatingUnencrypted);
   514 
   515     // Now let see with bob's pubkey already known
   516     // pEp Test Bob (test key, don't use) <pep.test.bob@pep-project.org>
   517     // BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39
   518     XCTAssertTrue([PEPTestUtils importBundledKey:@"0xC9C2EE39.asc" session:session]);
   519 
   520     PEPIdentity *identBob = [[PEPIdentity alloc]
   521                              initWithAddress:@"pep.test.bob@pep-project.org"
   522                              userID:@"42" userName:@"pEp Test Bob"
   523                              isOwn:NO
   524                              fingerPrint:@"BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39"];
   525 
   526     XCTAssertTrue([session updateIdentity:identBob error:&error]);
   527     XCTAssertNil(error);
   528 
   529     // Should be yellow, since no handshake happened.
   530     numRating = [self testOutgoingRatingForMessage:msg session:session error:&error];
   531     XCTAssertNotNil(numRating);
   532     XCTAssertNil(error);
   533     XCTAssertEqual(numRating.pEpRating, PEPRatingReliable);
   534 
   535     rating = [self ratingForIdentity:identBob session:session];
   536     XCTAssertEqual(rating, PEPRatingReliable);
   537 
   538     // Let' say we got that handshake, set PEP_ct_confirmed in Bob's identity
   539     XCTAssertTrue([session trustPersonalKey:identBob error:&error]);
   540     XCTAssertNil(error);
   541 
   542     // This time it should be green
   543     numRating = [self testOutgoingRatingForMessage:msg session:session error:&error];
   544     XCTAssertNotNil(numRating);
   545     XCTAssertNil(error);
   546     XCTAssertEqual(numRating.pEpRating, PEPRatingTrusted);
   547 
   548     rating = [self ratingForIdentity:identBob session:session];
   549     XCTAssertEqual(rating, PEPRatingTrusted);
   550 
   551     // Now let see if it turns back yellow if we add an unconfirmed folk.
   552     // pEp Test John (test key, don't use) <pep.test.john@pep-project.org>
   553     // AA2E4BEB93E5FE33DEFD8BE1135CD6D170DCF575
   554     XCTAssertTrue([PEPTestUtils importBundledKey:@"0x70DCF575.asc" session:session]);
   555 
   556     PEPIdentity *identJohn = [[PEPIdentity alloc]
   557                               initWithAddress:@"pep.test.john@pep-project.org"
   558                               userID:@"101" userName:@"pEp Test John"
   559                               isOwn:NO
   560                               fingerPrint:@"AA2E4BEB93E5FE33DEFD8BE1135CD6D170DCF575"];
   561 
   562     XCTAssertTrue([session updateIdentity:identJohn error:&error]);
   563     XCTAssertNil(error);
   564 
   565     msg.bcc = @[[[PEPIdentity alloc] initWithAddress:@"pep.test.john@pep-project.org"
   566                                               userID:@"101" userName:@"pEp Test John" isOwn:NO]];
   567 
   568     // Yellow ?
   569     numRating = [self testOutgoingRatingForMessage:msg session:session error:&error];
   570     XCTAssertNotNil(numRating);
   571     XCTAssertNil(error);
   572     XCTAssertEqual(numRating.pEpRating, PEPRatingReliable);
   573 
   574     XCTAssertTrue([session trustPersonalKey:identJohn error:&error]);
   575     XCTAssertNil(error);
   576 
   577     // This time it should be green
   578     numRating = [self testOutgoingRatingForMessage:msg session:session error:&error];
   579     XCTAssertNotNil(numRating);
   580     XCTAssertNil(error);
   581     XCTAssertEqual(numRating.pEpRating, PEPRatingTrusted);
   582 
   583     rating = [self ratingForIdentity:identJohn session:session];
   584     XCTAssertEqual(rating, PEPRatingTrusted);
   585 }
   586 
   587 - (void)testDontEncryptForMistrusted
   588 {
   589     PEPSession *session = [PEPSession new];
   590 
   591     // Our test user :
   592     // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
   593     // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
   594     XCTAssertTrue([PEPTestUtils importBundledKey:@"6FF00E97_sec.asc" session:session]);
   595 
   596     PEPIdentity *identAlice = [[PEPIdentity alloc]
   597                                initWithAddress:@"pep.test.alice@pep-project.org"
   598                                userID:ownUserId
   599                                userName:@"pEp Test Alice"
   600                                isOwn:YES
   601                                fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
   602 
   603     NSError *error = nil;
   604     XCTAssertTrue([session mySelf:identAlice error:&error]);
   605     XCTAssertNil(error);
   606 
   607     // pEp Test Bob (test key, don't use) <pep.test.bob@pep-project.org>
   608     // BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39
   609     XCTAssertTrue([PEPTestUtils importBundledKey:@"0xC9C2EE39.asc" session:session]);
   610 
   611     PEPIdentity *identBob = [[PEPIdentity alloc]
   612                              initWithAddress:@"pep.test.bob@pep-project.org"
   613                              userID:@"42" userName:@"pEp Test Bob"
   614                              isOwn:NO
   615                              fingerPrint:@"BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39"];
   616 
   617     XCTAssertTrue([session updateIdentity:identBob error:&error]);
   618     XCTAssertNil(error);
   619 
   620     // mistrust Bob
   621     XCTAssertTrue([session keyMistrusted:identBob error:&error]);
   622     XCTAssertNil(error);
   623 
   624     PEPMessage *msg = [PEPMessage new];
   625     msg.from = identAlice;
   626     msg.to = @[[[PEPIdentity alloc] initWithAddress:@"pep.test.bob@pep-project.org" userID:@"42"
   627                                            userName:@"pEp Test Bob" isOwn:NO]];
   628     msg.shortMessage = @"All Green Test";
   629     msg.longMessage = @"This is a text content";
   630     msg.direction = PEPMsgDirectionOutgoing;
   631 
   632     // Gray == PEPRatingUnencrypted
   633     NSNumber *numRating = [self testOutgoingRatingForMessage:msg session:session error:&error];
   634     XCTAssertNotNil(numRating);
   635     XCTAssertNil(error);
   636     XCTAssertEqual(numRating.pEpRating, PEPRatingUnencrypted);
   637 
   638     PEPMessage *encMsg = [session encryptMessage:msg extraKeys:nil status:nil error:&error];
   639     XCTAssertNotNil(encMsg);
   640     XCTAssertNil(error);
   641 
   642     XCTAssertNotEqualObjects(encMsg.attachments[0].mimeType, @"application/pgp-encrypted");
   643 
   644     [self pEpCleanUp];
   645 }
   646 
   647 - (void)testRevoke
   648 {
   649     PEPSession *session = [PEPSession new];
   650 
   651     // Our test user :
   652     // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
   653     // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
   654     XCTAssertTrue([PEPTestUtils importBundledKey:@"6FF00E97_sec.asc" session:session]);
   655     NSString *fpr = @"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97";
   656 
   657     PEPIdentity *identAlice = [[PEPIdentity alloc]
   658                                initWithAddress:@"pep.test.alice@pep-project.org"
   659                                userID:ownUserId
   660                                userName:@"pEp Test Alice"
   661                                isOwn:YES
   662                                fingerPrint:fpr];
   663 
   664     NSError *error = nil;
   665     XCTAssertTrue([session mySelf:identAlice error:&error]);
   666     XCTAssertNil(error);
   667 
   668     PEPIdentity *identAlice2 = [identAlice mutableCopy];
   669 
   670     // This will revoke key
   671     XCTAssertTrue([session keyMistrusted:identAlice2 error:&error]);
   672     XCTAssertNil(error);
   673     identAlice2.fingerPrint = nil;
   674 
   675     XCTAssertTrue([session mySelf:identAlice error:&error]);
   676     XCTAssertNil(error);
   677 
   678     // Check fingerprint is different
   679     XCTAssertNotEqualObjects(identAlice2.fingerPrint, fpr);
   680 }
   681 
   682 - (void)testMailToMyself
   683 {
   684     PEPSession *session = [PEPSession new];
   685 
   686     // Our test user :
   687     // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
   688     // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
   689     XCTAssertTrue([PEPTestUtils importBundledKey:@"6FF00E97_sec.asc" session:session]);
   690 
   691     PEPIdentity *identAlice = [[PEPIdentity alloc]
   692                                initWithAddress:@"pep.test.alice@pep-project.org"
   693                                userID:ownUserId
   694                                userName:@"pEp Test Alice"
   695                                isOwn:YES
   696                                fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
   697 
   698     NSError *error = nil;
   699     XCTAssertTrue([session mySelf:identAlice error:&error]);
   700     XCTAssertNil(error);
   701 
   702     PEPMessage *msg = [PEPMessage new];
   703     msg.from = identAlice;
   704     msg.to = @[identAlice];
   705     msg.shortMessage = @"Mail to Myself";
   706     msg.longMessage = @"This is a text content";
   707     msg.direction = PEPMsgDirectionOutgoing;
   708 
   709     NSNumber *numRating = [self testOutgoingRatingForMessage:msg session:session error:&error];
   710     XCTAssertNotNil(numRating);
   711     XCTAssertNil(error);
   712     XCTAssertEqual(numRating.pEpRating, PEPRatingTrustedAndAnonymized);
   713 
   714     PEPMessage *encMsg = [session encryptMessage:msg extraKeys:nil status:nil error:&error];
   715     XCTAssertNotNil(encMsg);
   716     XCTAssertNil(error);
   717 
   718     NSArray *keys;
   719 
   720     error = nil;
   721     PEPRating rating = PEPRatingUndefined;
   722     PEPMessage *decmsg = [session
   723                           decryptMessage:encMsg
   724                           flags:nil
   725                           rating:&rating
   726                           extraKeys:&keys
   727                           status:nil
   728                           error:&error];
   729     XCTAssertNotNil(decmsg);
   730     XCTAssertNil(error);
   731     XCTAssertEqual(rating, PEPRatingTrustedAndAnonymized);
   732 }
   733 
   734 - (void)testEncryptedMailFromMuttWithReencryption
   735 {
   736     PEPSession *session = [PEPSession new];
   737 
   738     // This is the public key for test001@peptest.ch
   739     XCTAssertTrue([PEPTestUtils importBundledKey:@"A3FC7F0A.asc" session:session]);
   740 
   741     // This is the secret key for test001@peptest.ch
   742     XCTAssertTrue([PEPTestUtils importBundledKey:@"A3FC7F0A_sec.asc" session:session]);
   743 
   744     // Mail from mutt, already processed into message dict by the app.
   745     NSMutableDictionary *msgDict = [[PEPTestUtils
   746                                      unarchiveDictionary:@"msg_to_A3FC7F0A_from_mutt.ser"]
   747                                     mutableCopy];
   748     [PEPTestUtils migrateUnarchivedMessageDictionary:msgDict];
   749     [msgDict removeObjectForKey:kPepLongMessage];
   750     [msgDict removeObjectForKey:kPepLongMessageFormatted];
   751 
   752     // Also extracted "live" from the app.
   753     NSMutableDictionary *accountDict = [[PEPTestUtils
   754                                          unarchiveDictionary:@"account_A3FC7F0A.ser"]
   755                                         mutableCopy];
   756     [accountDict removeObjectForKey:kPepCommType];
   757     [accountDict removeObjectForKey:kPepFingerprint];
   758     PEPIdentity *identMe = [[PEPIdentity alloc] initWithDictionary:accountDict];
   759 
   760     NSError *error = nil;
   761     XCTAssertTrue([session mySelf:identMe error:&error]);
   762     XCTAssertNil(error);
   763 
   764     XCTAssertNotNil(identMe.fingerPrint);
   765 
   766     NSArray* keys;
   767     PEPMessage *msg = [PEPMessage new];
   768     [msg setValuesForKeysWithDictionary:msgDict];
   769     PEPMessage *msgOriginal = [PEPMessage new];
   770     [msgOriginal setValuesForKeysWithDictionary:msgDict];
   771 
   772     XCTAssertEqualObjects(msg, msgOriginal);
   773 
   774     PEPRating rating = PEPRatingUndefined;
   775     PEPDecryptFlags flags = PEPDecryptFlagsUntrustedServer;
   776 
   777     PEPMessage *pepDecryptedMail = [session
   778                                     decryptMessage:msg
   779                                     flags:&flags
   780                                     rating:&rating
   781                                     extraKeys:&keys
   782                                     status:nil
   783                                     error:&error];
   784     XCTAssertNotNil(pepDecryptedMail);
   785     XCTAssertNil(error);
   786 
   787     // Technically, the mail is encrypted, but the signatures don't match
   788     XCTAssertEqual(rating, PEPRatingUnreliable);
   789 
   790     // Since we're requesting re-encryption, src should have been changed
   791     XCTAssertNotEqualObjects(msg, msgOriginal);
   792 
   793     XCTAssertNotNil(pepDecryptedMail.longMessage);
   794 }
   795 
   796 - (void)testOutgoingContactColor
   797 {
   798     PEPSession *session = [PEPSession new];
   799 
   800     PEPIdentity *partner1Orig = [PEPTestUtils foreignPepIdentityWithAddress:@"partner1@dontcare.me"
   801                                                                    userName:@"Partner 1"];
   802     NSString *pubKeyPartner1 = [PEPTestUtils loadResourceByName:@"partner1_F2D281C2789DD7F6_pub.asc"];
   803     XCTAssertNotNil(pubKeyPartner1);
   804 
   805     NSError *error = nil;
   806     XCTAssertTrue([session importKey:pubKeyPartner1 error:&error]);
   807     XCTAssertNil(error);
   808 
   809     PEPRating color = [self ratingForIdentity:partner1Orig session:session];
   810     XCTAssertEqual(color, PEPRatingReliable);
   811 }
   812 
   813 - (void)testGetTrustwords
   814 {
   815     PEPSession *session = [PEPSession new];
   816 
   817     PEPIdentity *partner1Orig = [[PEPIdentity alloc]
   818                                  initWithAddress:@"partner1@dontcare.me" userID:@"partner1"
   819                                  userName:@"partner1"
   820                                  isOwn:NO fingerPrint:@"F0CD3F7B422E5D587ABD885BF2D281C2789DD7F6"];
   821 
   822     PEPIdentity *meOrig = [[PEPIdentity alloc]
   823                            initWithAddress:@"me@dontcare.me" userID:@"me"
   824                            userName:@"me"
   825                            isOwn:NO fingerPrint:@"CC1F73F6FB774BF08B197691E3BFBCA9248FC681"];
   826 
   827     NSString *pubKeyPartner1 = [PEPTestUtils loadResourceByName:@"partner1_F2D281C2789DD7F6_pub.asc"];
   828     XCTAssertNotNil(pubKeyPartner1);
   829     NSString *pubKeyMe = [PEPTestUtils loadResourceByName:@"meATdontcare_E3BFBCA9248FC681_pub.asc"];
   830     XCTAssertNotNil(pubKeyMe);
   831     NSString *secKeyMe = [PEPTestUtils loadResourceByName:@"meATdontcare_E3BFBCA9248FC681_sec.asc"];
   832     XCTAssertNotNil(secKeyMe);
   833 
   834     NSError *error = nil;
   835     NSString *trustwordsFull = [session getTrustwordsIdentity1:meOrig identity2:partner1Orig
   836                                                       language:@"en" full:YES error:&error];
   837     XCTAssertNil(error);
   838     XCTAssertEqualObjects(trustwordsFull,
   839                           @"EMERSON GASPER TOKENISM BOLUS COLLAGE DESPISE BEDDED ENCRYPTION IMAGINE BEDFORD");
   840 
   841     NSString *trustwordsUndefined = [session getTrustwordsIdentity1:meOrig identity2:partner1Orig
   842                                                            language:@"ZZ" full:YES error:&error];
   843     XCTAssertNotNil(error);
   844     XCTAssertNil(trustwordsUndefined);
   845 }
   846 
   847 - (void)testStringToRating
   848 {
   849     PEPSession *session = [PEPSession new];
   850     XCTAssertEqual([session ratingFromString:@"cannot_decrypt"], PEPRatingCannotDecrypt);
   851     XCTAssertEqual([session ratingFromString:@"have_no_key"], PEPRatingHaveNoKey);
   852     XCTAssertEqual([session ratingFromString:@"unencrypted"], PEPRatingUnencrypted);
   853     XCTAssertEqual([session ratingFromString:@"unencrypted_for_some"],
   854                    PEPRatingUnencryptedForSome);
   855     XCTAssertEqual([session ratingFromString:@"unreliable"], PEPRatingUnreliable);
   856     XCTAssertEqual([session ratingFromString:@"reliable"], PEPRatingReliable);
   857     XCTAssertEqual([session ratingFromString:@"trusted"], PEPRatingTrusted);
   858     XCTAssertEqual([session ratingFromString:@"trusted_and_anonymized"],
   859                    PEPRatingTrustedAndAnonymized);
   860     XCTAssertEqual([session ratingFromString:@"fully_anonymous"], PEPRatingFullyAnonymous);
   861     XCTAssertEqual([session ratingFromString:@"mistrust"], PEPRatingMistrust);
   862     XCTAssertEqual([session ratingFromString:@"b0rken"], PEPRatingB0rken);
   863     XCTAssertEqual([session ratingFromString:@"under_attack"], PEPRatingUnderAttack);
   864     XCTAssertEqual([session ratingFromString:@"undefined"], PEPRatingUndefined);
   865     XCTAssertEqual([session ratingFromString:@"does not exist111"], PEPRatingUndefined);
   866 }
   867 
   868 - (void)testRatingToString
   869 {
   870     PEPSession *session = [PEPSession new];
   871     XCTAssertEqualObjects([session stringFromRating:PEPRatingCannotDecrypt], @"cannot_decrypt");
   872     XCTAssertEqualObjects([session stringFromRating:PEPRatingHaveNoKey], @"have_no_key");
   873     XCTAssertEqualObjects([session stringFromRating:PEPRatingUnencrypted], @"unencrypted");
   874     XCTAssertEqualObjects([session stringFromRating:PEPRatingUnencryptedForSome],
   875                           @"unencrypted_for_some");
   876     XCTAssertEqualObjects([session stringFromRating:PEPRatingUnreliable], @"unreliable");
   877     XCTAssertEqualObjects([session stringFromRating:PEPRatingReliable], @"reliable");
   878     XCTAssertEqualObjects([session stringFromRating:PEPRatingTrusted], @"trusted");
   879     XCTAssertEqualObjects([session stringFromRating:PEPRatingTrustedAndAnonymized],
   880                           @"trusted_and_anonymized");
   881     XCTAssertEqualObjects([session stringFromRating:PEPRatingFullyAnonymous],
   882                           @"fully_anonymous");
   883     XCTAssertEqualObjects([session stringFromRating:PEPRatingMistrust], @"mistrust");
   884     XCTAssertEqualObjects([session stringFromRating:PEPRatingB0rken], @"b0rken");
   885     XCTAssertEqualObjects([session stringFromRating:PEPRatingUnderAttack], @"under_attack");
   886     XCTAssertEqualObjects([session stringFromRating:PEPRatingUndefined], @"undefined");
   887     XCTAssertEqualObjects([session stringFromRating:500], @"undefined");
   888 }
   889 
   890 - (void)testIsPEPUser
   891 {
   892     PEPSession *session = [PEPSession new];
   893 
   894     PEPIdentity *identMe = [[PEPIdentity alloc]
   895                             initWithAddress:@"me-myself-and-i@pep-project.org"
   896                             userID:@"me-myself-and-i"
   897                             userName:@"pEp Me"
   898                             isOwn:YES];
   899     NSError *error = nil;
   900     XCTAssertTrue([session mySelf:identMe error:&error]);
   901     XCTAssertNil(error);
   902 
   903     XCTAssertNotNil(identMe.fingerPrint);
   904 
   905     // PEP_CANNOT_FIND_PERSON == 902
   906     XCTAssertTrue([session isPEPUser:identMe error:&error]);
   907 }
   908 
   909 - (void)testXEncStatusForOutgoingEncryptedMail
   910 {
   911     [self helperXEncStatusForOutgoingEncryptdMailToSelf:NO expectedRating:PEPRatingReliable];
   912 }
   913 
   914 - (void)testXEncStatusForOutgoingSelfEncryptedMail
   915 {
   916     [self helperXEncStatusForOutgoingEncryptdMailToSelf:YES
   917                                          expectedRating:PEPRatingTrustedAndAnonymized];
   918 }
   919 
   920 - (void)testEncryptMessagesWithoutKeys
   921 {
   922     PEPSession *session = [PEPSession new];
   923 
   924     PEPIdentity *identMe = [[PEPIdentity alloc]
   925                             initWithAddress:@"me-myself-and-i@pep-project.org"
   926                             userID:@"me-myself-and-i"
   927                             userName:@"pEp Me"
   928                             isOwn:YES];
   929     NSError *error = nil;
   930     XCTAssertTrue([session mySelf:identMe error:&error]);
   931     XCTAssertNil(error);
   932 
   933     XCTAssertNotNil(identMe.fingerPrint);
   934 
   935     PEPIdentity *identAlice = [[PEPIdentity alloc]
   936                                initWithAddress:@"alice@pep-project.org"
   937                                userID:@"alice"
   938                                userName:@"pEp Test Alice"
   939                                isOwn:NO];
   940 
   941     PEPMessage *msg = [PEPMessage new];
   942     msg.from = identMe;
   943     msg.to = @[identAlice];
   944     msg.shortMessage = @"Mail to Alice";
   945     msg.longMessage = @"Alice?";
   946     msg.direction = PEPMsgDirectionOutgoing;
   947 
   948     NSNumber *numRating = [self testOutgoingRatingForMessage:msg session:session error:&error];
   949     XCTAssertNotNil(numRating);
   950     XCTAssertNil(error);
   951     XCTAssertEqual(numRating.pEpRating, PEPRatingUnencrypted);
   952 
   953     PEPMessage *encMsg = [session encryptMessage:msg extraKeys:nil status:nil error:&error];
   954     XCTAssertNotNil(encMsg);
   955     XCTAssertNil(error);
   956 
   957     XCTAssertNotNil(encMsg);
   958 
   959     PEPStringList *keys;
   960     PEPRating pEpRating;
   961     error = nil;
   962     PEPMessage *decMsg = [session
   963                           decryptMessage:encMsg
   964                           flags:nil
   965                           rating:&pEpRating
   966                           extraKeys:&keys
   967                           status:nil
   968                           error:&error];
   969     XCTAssertNotNil(decMsg);
   970     XCTAssertNil(error);
   971 
   972     XCTAssertEqual(pEpRating, PEPRatingUnencrypted);
   973     XCTAssertNotNil(decMsg);
   974 }
   975 
   976 /**
   977  ENGINE-364. Tries to invoke trustPersonalKey on an identity without key,
   978  giving it a fake fingerprint.
   979  */
   980 - (void)testTrustPersonalKey
   981 {
   982     PEPSession *session = [PEPSession new];
   983 
   984     PEPIdentity *identMe = [[PEPIdentity alloc]
   985                             initWithAddress:@"me-myself-and-i@pep-project.org"
   986                             userID:@"me-myself-and-i"
   987                             userName:@"pEp Me"
   988                             isOwn:YES];
   989     NSError *error = nil;
   990     XCTAssertTrue([session mySelf:identMe error:&error]);
   991     XCTAssertNil(error);
   992 
   993     XCTAssertNotNil(identMe.fingerPrint);
   994 
   995     // The fingerprint is definitely wrong, we don't have a key
   996     PEPIdentity *identAlice = [[PEPIdentity alloc]
   997                                initWithAddress:@"alice@pep-project.org"
   998                                userID:@"alice"
   999                                userName:@"pEp Test Alice"
  1000                                isOwn:NO
  1001                                fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
  1002 
  1003     XCTAssertFalse([session trustPersonalKey:identAlice error:&error]);
  1004     XCTAssertNotNil(error);
  1005 }
  1006 
  1007 /**
  1008  ENGINE-381
  1009  */
  1010 - (void)testVolatileIdentityRating
  1011 {
  1012     PEPSession *session = [PEPSession new];
  1013 
  1014     PEPIdentity *identMe = [[PEPIdentity alloc]
  1015                             initWithAddress:@"me-myself-and-i@pep-project.org"
  1016                             userID:@"me-myself-and-i"
  1017                             userName:@"pEp Me"
  1018                             isOwn:YES];
  1019     NSError *error = nil;
  1020     XCTAssertTrue([session mySelf:identMe error:&error]);
  1021     XCTAssertNil(error);
  1022 
  1023     XCTAssertNotNil(identMe.fingerPrint);
  1024 
  1025     PEPIdentity *identAlice = [self
  1026                                checkImportingKeyFilePath:@"6FF00E97_sec.asc"
  1027                                address:@"pep.test.alice@pep-project.org"
  1028                                userID:@"alice_user_id"
  1029                                fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
  1030                                session: session];
  1031     XCTAssertNotNil(identAlice);
  1032 
  1033     dispatch_group_t identityRatingGroup = dispatch_group_create();
  1034 
  1035     void (^ratingBlock)(void) = ^{
  1036         PEPSession *innerSession = [PEPSession new];
  1037         PEPRating rating = [self ratingForIdentity:identAlice session:innerSession];
  1038         XCTAssertEqual(rating, PEPRatingReliable);
  1039     };
  1040 
  1041     for (int i = 0; i < 4; ++i) {
  1042         dispatch_group_async(identityRatingGroup,
  1043                              dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0),
  1044                              ratingBlock);
  1045     }
  1046 
  1047     for (int i = 0; i < 4; ++i) {
  1048         ratingBlock();
  1049     }
  1050 
  1051     dispatch_group_wait(identityRatingGroup, DISPATCH_TIME_FOREVER);
  1052 }
  1053 
  1054 /**
  1055  IOSAD-93, testing for easy error case.
  1056  */
  1057 - (void)testEncryptAndAttachPrivateKeyIllegalValue
  1058 {
  1059     PEPSession *session = [PEPSession new];
  1060 
  1061     PEPIdentity *identMe = [[PEPIdentity alloc]
  1062                             initWithAddress:@"me-myself-and-i@pep-project.org"
  1063                             userID:@"me-myself-and-i"
  1064                             userName:@"pEp Me"
  1065                             isOwn:YES];
  1066     NSError *error = nil;
  1067     XCTAssertTrue([session mySelf:identMe error:&error]);
  1068     XCTAssertNil(error);
  1069 
  1070     XCTAssertNotNil(identMe.fingerPrint);
  1071 
  1072     NSString *fprAlice = @"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97";
  1073     PEPIdentity *identAlice = [self
  1074                                checkImportingKeyFilePath:@"6FF00E97_sec.asc"
  1075                                address:@"pep.test.alice@pep-project.org"
  1076                                userID:@"alice_user_id"
  1077                                fingerPrint:fprAlice
  1078                                session: session];
  1079     XCTAssertNotNil(identAlice);
  1080 
  1081     NSString *shortMessage = @"whatever it may be";
  1082     NSString *longMessage = [NSString stringWithFormat:@"%@ %@", shortMessage, shortMessage];
  1083     PEPMessage *message = [PEPMessage new];
  1084     message.from = identMe;
  1085     message.to = @[identAlice];
  1086     message.shortMessage = shortMessage;
  1087     message.longMessage = longMessage;
  1088 
  1089     PEPStatus status = PEPStatusKeyNotFound;
  1090     error = nil;
  1091     PEPMessage *encrypted = [session
  1092                              encryptMessage:message
  1093                              toFpr:fprAlice
  1094                              encFormat:PEPEncFormatPEP
  1095                              flags:0
  1096                              status:&status error:&error];
  1097     XCTAssertEqual(status, PEPStatusIllegalValue);
  1098     XCTAssertNotNil(error);
  1099     XCTAssertNil(encrypted);
  1100 }
  1101 
  1102 - (void)testSetIdentityFlags
  1103 {
  1104     PEPSession *session = [PEPSession new];
  1105 
  1106     PEPIdentity *me = [PEPTestUtils ownPepIdentityWithAddress:@"me@peptest.ch"
  1107                                                      userName:@"userName"];
  1108     NSError *error = nil;
  1109     XCTAssertTrue([session mySelf:me error:&error]);
  1110     XCTAssertNil(error);
  1111 
  1112     PEPIdentityFlags theFlags[] = {
  1113         PEPIdentityFlagsNotForSync,
  1114         PEPIdentityFlagsList,
  1115         PEPIdentityFlagsDeviceGroup,
  1116         0
  1117     };
  1118 
  1119     for (int i = 0;; ++i) {
  1120         PEPIdentityFlags aFlag = theFlags[i];
  1121         if (aFlag == 0) {
  1122             break;
  1123         }
  1124         error = nil;
  1125         XCTAssertTrue([session setFlags:(PEPIdentityFlags) aFlag forIdentity:me error:&error]);
  1126         XCTAssertNil(error);
  1127 
  1128         XCTAssertTrue(me.flags & theFlags[i]);
  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 /// Prove that mySelf triggers a message to be sent.
  1234 - (void)testBasicSendMessage
  1235 {
  1236     PEPSession *session = [PEPSession new];
  1237     [self testSendMessageOnSession:session];
  1238 }
  1239 
  1240 - (void)testDeliverHandshakeResult
  1241 {
  1242     PEPSession *session = [PEPSession new];
  1243     [self testSendMessageOnSession:session];
  1244 
  1245     PEPSyncHandshakeResult handshakeResults[] = { PEPSyncHandshakeResultCancel,
  1246         PEPSyncHandshakeResultAccepted, PEPSyncHandshakeResultRejected };
  1247 
  1248     PEPIdentity *forSureNotMe = [[PEPIdentity alloc]
  1249                                  initWithAddress:@"someoneelseentirely@pep-project.org"
  1250                                  userID:@"that_someone_else"
  1251                                  userName:@"other"
  1252                                  isOwn:NO];
  1253 
  1254     for (int i = 0;; ++i) {
  1255         NSError *error = nil;
  1256         XCTAssertFalse([session
  1257                         deliverHandshakeResult:handshakeResults[i]
  1258                         identitiesSharing:@[forSureNotMe]
  1259                         error:&error]);
  1260         XCTAssertNotNil(error);
  1261         XCTAssertEqual([error code], PEPStatusIllegalValue);
  1262 
  1263         if (handshakeResults[i] == PEPSyncHandshakeResultRejected) {
  1264             break;
  1265         }
  1266     }
  1267 }
  1268 
  1269 /// Test creating a new own identity with pEp sync disabled.
  1270 - (void)testNoBeaconOnMyself
  1271 {
  1272     PEPSession *session = [PEPSession new];
  1273 
  1274     XCTAssertEqual(self.sendMessageDelegate.messages.count, 0);
  1275     XCTAssertNil(self.sendMessageDelegate.lastMessage);
  1276 
  1277     PEPIdentity *identMe = [[PEPIdentity alloc]
  1278                             initWithAddress:@"me-myself-and-i@pep-project.org"
  1279                             userID:@"me-myself-and-i"
  1280                             userName:@"pEp Me"
  1281                             isOwn:YES];
  1282     identMe.flags |= PEPIdentityFlagsNotForSync;
  1283 
  1284     NSError *error = nil;
  1285     XCTAssertTrue([session mySelf:identMe error:&error]);
  1286     XCTAssertNil(error);
  1287     XCTAssertTrue([session disableSyncForIdentity:identMe error:&error]);
  1288     XCTAssertNil(error);
  1289 
  1290     [self startSync];
  1291 
  1292     [NSThread sleepForTimeInterval:1];
  1293     XCTAssertNil(self.sendMessageDelegate.lastMessage);
  1294 
  1295     XCTAssertEqual(self.sendMessageDelegate.messages.count, 0);
  1296     [self shutdownSync];
  1297 }
  1298 
  1299 /// Test creating a new own identity with pEp sync disabled,
  1300 /// and then creating one with sync enabled, ensuring that
  1301 /// the beacon (on the 2nd identity with sync enabled) gets send out.
  1302 - (void)testMyselfSyncDisabledMyselfSyncEnabled
  1303 {
  1304     PEPSession *session = [PEPSession new];
  1305 
  1306     XCTAssertEqual(self.sendMessageDelegate.messages.count, 0);
  1307     XCTAssertNil(self.sendMessageDelegate.lastMessage);
  1308 
  1309     PEPIdentity *identMeDisabled = [[PEPIdentity alloc]
  1310                                     initWithAddress:@"me-myself-and-i@pep-project.org"
  1311                                     userID:@"me-myself-and-i"
  1312                                     userName:@"pEp Me"
  1313                                     isOwn:YES];
  1314     identMeDisabled.flags |= PEPIdentityFlagsNotForSync;
  1315 
  1316     NSError *error = nil;
  1317     XCTAssertTrue([session mySelf:identMeDisabled error:&error]);
  1318     XCTAssertNil(error);
  1319     XCTAssertTrue([session disableSyncForIdentity:identMeDisabled error:&error]);
  1320     XCTAssertNil(error);
  1321 
  1322     [self startSync];
  1323 
  1324     [NSThread sleepForTimeInterval:1];
  1325     XCTAssertNil(self.sendMessageDelegate.lastMessage);
  1326 
  1327     XCTAssertEqual(self.sendMessageDelegate.messages.count, 0);
  1328 
  1329     PEPIdentity *identMeEnsabled = [[PEPIdentity alloc]
  1330                                     initWithAddress:@"me-myself-and-i_enabled@pep-project.org"
  1331                                     userID:@"me-myself-and-i_enabled"
  1332                                     userName:@"pEp Me_enabled"
  1333                                     isOwn:YES];
  1334 
  1335     error = nil;
  1336     XCTAssertTrue([session mySelf:identMeEnsabled error:&error]);
  1337     XCTAssertNil(error);
  1338 
  1339     XCTKVOExpectation *expHaveMessage1 = [[XCTKVOExpectation alloc]
  1340                                           initWithKeyPath:@"lastMessage"
  1341                                           object:self.sendMessageDelegate];
  1342     [self waitForExpectations:@[expHaveMessage1] timeout:PEPTestInternalSyncTimeout];
  1343     XCTAssertNotNil(self.sendMessageDelegate.lastMessage);
  1344     XCTAssertGreaterThan(self.sendMessageDelegate.messages.count, 0);
  1345 
  1346     XCTAssertEqualObjects(self.sendMessageDelegate.lastMessage.from.address,
  1347                           identMeEnsabled.address);
  1348 
  1349     [self shutdownSync];
  1350 }
  1351 
  1352 /// ENGINE-684
  1353 - (void)testMyselfWithQueryKeySyncEnabledForIdentity
  1354 {
  1355     PEPSession *session = [PEPSession new];
  1356 
  1357     PEPIdentity *identMeEnabled = [[PEPIdentity alloc]
  1358                                    initWithAddress:@"me-myself-and-i-enabled@pep-project.org"
  1359                                    userID:@"me-myself-and-i-enabled"
  1360                                    userName:@"pEp Me Enabled"
  1361                                    isOwn:YES];
  1362 
  1363     PEPIdentity *identMeDisabled = [[PEPIdentity alloc]
  1364                                     initWithAddress:@"me-myself-and-i-disabled@pep-project.org"
  1365                                     userID:@"me-myself-and-i-disabled"
  1366                                     userName:@"pEp Me Disabled"
  1367                                     isOwn:YES];
  1368 
  1369     for (PEPIdentity *ident in @[identMeEnabled, identMeDisabled]) {
  1370         BOOL expectEnabled = ident == identMeEnabled ? YES : NO;
  1371 
  1372         if (!expectEnabled) {
  1373             ident.flags |= PEPIdentityFlagsNotForSync;
  1374         }
  1375 
  1376         NSError *error = nil;
  1377         XCTAssertTrue([session mySelf:ident error:&error]);
  1378         XCTAssertNil(error);
  1379 
  1380         if (!expectEnabled) {
  1381             XCTAssertTrue([session disableSyncForIdentity:ident error:&error]);
  1382             XCTAssertNil(error);
  1383         }
  1384 
  1385         NSNumber *enabledNum = [session queryKeySyncEnabledForIdentity:ident error:&error];
  1386         XCTAssertNotNil(enabledNum);
  1387         XCTAssertNil(error);
  1388 
  1389         XCTAssertEqual([enabledNum boolValue], expectEnabled);
  1390     }
  1391 }
  1392 
  1393 #pragma mark - key_reset_identity
  1394 
  1395 - (void)testKeyResetIdentity
  1396 {
  1397     PEPSession *session = [PEPSession new];
  1398 
  1399     PEPIdentity *me = [PEPTestUtils ownPepIdentityWithAddress:@"me@peptest.ch"
  1400                                                      userName:@"userName"];
  1401 
  1402     NSError *error = nil;
  1403     XCTAssertTrue([session mySelf:me error:&error]);
  1404     XCTAssertNil(error);
  1405 
  1406     NSString *fprOriginal = me.fingerPrint;
  1407     XCTAssertNotNil(fprOriginal);
  1408 
  1409     XCTAssertTrue([session keyReset:me fingerprint:nil error:&error]);
  1410     XCTAssertNil(error);
  1411 
  1412     XCTAssertTrue([session mySelf:me error:&error]);
  1413     XCTAssertNil(error);
  1414 
  1415     NSString *fprAfterReset = me.fingerPrint;
  1416     XCTAssertNotNil(fprAfterReset);
  1417 
  1418     XCTAssertNotEqual(fprOriginal, fprAfterReset);
  1419 }
  1420 
  1421 #pragma mark - leave_device_group
  1422 
  1423 /** Leaving a device group is successful even though none exists. */
  1424 - (void)testSuccessfulLeaveDeviceGroup
  1425 {
  1426     PEPSession *session = [PEPSession new];
  1427 
  1428     PEPIdentity *identMe = [[PEPIdentity alloc]
  1429                             initWithAddress:@"me-myself-and-i@pep-project.org"
  1430                             userID:@"me-myself-and-i"
  1431                             userName:@"pEp Me"
  1432                             isOwn:YES];
  1433     NSError *error = nil;
  1434     XCTAssertTrue([session mySelf:identMe error:&error]);
  1435     XCTAssertNil(error);
  1436 
  1437     [self startSync];
  1438 
  1439     error = nil;
  1440     XCTAssertTrue([session leaveDeviceGroup:&error]);
  1441     XCTAssertNil(error);
  1442 
  1443     // leaving a device group should disable sync
  1444     XCTAssertTrue(self.notifyHandshakeDelegate.engineDidShutdownKeySync);
  1445 
  1446     [self shutdownSync];
  1447 }
  1448 
  1449 #pragma mark - enable/disable sync
  1450 
  1451 - (void)testEnableDisableFailForSyncOnPartnerIdentity
  1452 {
  1453     PEPIdentity *notMe = [[PEPIdentity alloc]
  1454                           initWithAddress:@"notme@pep-project.org"
  1455                           userID:@"notme_ID"
  1456                           userName:@"notme"
  1457                           isOwn:NO];
  1458 
  1459     NSError *error = nil;
  1460     XCTAssertFalse([notMe enableKeySync:&error]);
  1461     XCTAssertNotNil(error);
  1462 
  1463     error = nil;
  1464     XCTAssertFalse([notMe disableKeySync:&error]);
  1465     XCTAssertNotNil(error);
  1466 
  1467     error = nil;
  1468     XCTAssertNil([notMe queryKeySyncEnabled:&error]);
  1469     XCTAssertNotNil(error);
  1470 }
  1471 
  1472 - (void)testEnableDisableSyncOnOwnIdentityWithQuery
  1473 {
  1474     PEPSession *session = [PEPSession new];
  1475 
  1476     PEPIdentity *identMe1 = [[PEPIdentity alloc]
  1477                              initWithAddress:@"me-myself-and-i@pep-project.org"
  1478                              userID:@"me-myself-and-i"
  1479                              userName:@"pEp Me"
  1480                              isOwn:YES];
  1481     NSError *error = nil;
  1482     XCTAssertTrue([session mySelf:identMe1 error:&error]);
  1483     XCTAssertNil(error);
  1484 
  1485     error = nil;
  1486     PEPIdentity *identMe2 = [[PEPIdentity alloc]
  1487                              initWithAddress:@"me-myself-and-i2@pep-project.org"
  1488                              userID:@"me-myself-and-i2"
  1489                              userName:@"pEp Me2"
  1490                              isOwn:YES];
  1491     XCTAssertTrue([session mySelf:identMe2 error:&error]);
  1492     XCTAssertNil(error);
  1493 
  1494     XCTAssertNotEqualObjects(identMe1.fingerPrint, identMe2.fingerPrint);
  1495 
  1496     for (int i = 0; i < 10; ++i) {
  1497         error = nil;
  1498         BOOL enable = i % 2 == 0; // enable keysync on even numbers (roughly)
  1499         if (enable) {
  1500             XCTAssertTrue([session enableSyncForIdentity:identMe1 error:&error]);
  1501             XCTAssertTrue([identMe2 enableKeySync:&error]);
  1502         } else {
  1503             XCTAssertTrue([session disableSyncForIdentity:identMe1 error:&error]);
  1504             XCTAssertTrue([identMe2 disableKeySync:&error]);
  1505         }
  1506         XCTAssertNil(error);
  1507 
  1508         NSNumber *keySyncState1 = [session queryKeySyncEnabledForIdentity:identMe1 error:&error];
  1509         NSNumber *keySyncState2 = [identMe2 queryKeySyncEnabled:&error];
  1510         XCTAssertNil(error);
  1511         XCTAssertNotNil(keySyncState1);
  1512         XCTAssertNotNil(keySyncState2);
  1513         if (enable) {
  1514             XCTAssertTrue([keySyncState1 boolValue]);
  1515         } else {
  1516             XCTAssertFalse([keySyncState1 boolValue]);
  1517         }
  1518         XCTAssertEqualObjects(keySyncState1, keySyncState2);
  1519     }
  1520 }
  1521 
  1522 /**
  1523  ENGINE-604, just in case.
  1524  */
  1525 - (void)testQueryKeySyncOnOwnIdentityInALoop
  1526 {
  1527     PEPSession *session = [PEPSession new];
  1528 
  1529     PEPIdentity *identMe = [[PEPIdentity alloc]
  1530                             initWithAddress:@"me-myself-and-i@pep-project.org"
  1531                             userID:@"me-myself-and-i"
  1532                             userName:@"pEp Me"
  1533                             isOwn:YES];
  1534     NSError *error = nil;
  1535     XCTAssertTrue([session mySelf:identMe error:&error]);
  1536     XCTAssertNil(error);
  1537 
  1538     for (NSNumber *numBool in @[@YES, @NO]) {
  1539         error = nil;
  1540         if ([numBool boolValue]) {
  1541             XCTAssertTrue([session enableSyncForIdentity:identMe error:&error]);
  1542         } else {
  1543             XCTAssertTrue([session disableSyncForIdentity:identMe error:&error]);
  1544         }
  1545         XCTAssertNil(error);
  1546 
  1547         for (int i = 0; i < 10; ++i) {
  1548             NSNumber *numQuery = [session queryKeySyncEnabledForIdentity:identMe error:&error];
  1549             XCTAssertNotNil(numQuery);
  1550             XCTAssertEqualObjects(numBool, numQuery);
  1551             XCTAssertNil(error);
  1552         }
  1553     }
  1554 }
  1555 
  1556 /**
  1557  Tests [PEPSessionProtocol keyResetAllOwnKeysError:error].
  1558 
  1559  Does the following:
  1560  * Do a mySelf.
  1561  * Catch the sent out sync (beacon?) message.
  1562  * Do a key reset on all own identities ([PEPSessionProtocol keyResetAllOwnKeysError:error]).
  1563  * Catch the sent out sync message.
  1564  * Decrypt the caught sync messages.
  1565  */
  1566 - (void)testDecryptBeaconsAfterKeyReset
  1567 {
  1568     PEPSession *session = [PEPSession new];
  1569 
  1570     XCTAssertEqual(self.sendMessageDelegate.messages.count, 0);
  1571     XCTAssertNil(self.sendMessageDelegate.lastMessage);
  1572 
  1573     PEPIdentity *identMe = [[PEPIdentity alloc]
  1574                             initWithAddress:@"me-myself-and-i@pep-project.org"
  1575                             userID:@"me-myself-and-i"
  1576                             userName:@"pEp Me"
  1577                             isOwn:YES];
  1578     NSError *error = nil;
  1579     XCTAssertTrue([session mySelf:identMe error:&error]);
  1580     XCTAssertNil(error);
  1581     XCTAssertNotNil(identMe.fingerPrint);
  1582 
  1583     [self startSync];
  1584 
  1585     XCTKVOExpectation *expHaveMessage1 = [[XCTKVOExpectation alloc]
  1586                                           initWithKeyPath:@"lastMessage"
  1587                                           object:self.sendMessageDelegate];
  1588     [self waitForExpectations:@[expHaveMessage1] timeout:PEPTestInternalSyncTimeout];
  1589     XCTAssertNotNil(self.sendMessageDelegate.lastMessage);
  1590     XCTAssertEqual(self.sendMessageDelegate.messages.count, 1);
  1591 
  1592     PEPMessage *oldBeacon = self.sendMessageDelegate.lastMessage;
  1593 
  1594     XCTAssertTrue([session keyResetAllOwnKeysError:&error]);
  1595     XCTAssertNil(error);
  1596 
  1597     XCTKVOExpectation *expHaveMessage2 = [[XCTKVOExpectation alloc]
  1598                                           initWithKeyPath:@"lastMessage"
  1599                                           object:self.sendMessageDelegate];
  1600     [self waitForExpectations:@[expHaveMessage2] timeout:PEPTestInternalSyncTimeout];
  1601     XCTAssertNotNil(self.sendMessageDelegate.lastMessage);
  1602     XCTAssertEqual(self.sendMessageDelegate.messages.count, 2);
  1603 
  1604     PEPMessage *newBeacon = self.sendMessageDelegate.lastMessage;
  1605 
  1606     XCTAssertNotEqual(oldBeacon, newBeacon);
  1607 
  1608     PEPRating rating;
  1609     PEPStringList *extraKeys;
  1610     PEPStatus status;
  1611     PEPMessage *decryptedOldBeacon = [session decryptMessage:oldBeacon
  1612                                                        flags:nil
  1613                                                       rating:&rating
  1614                                                    extraKeys:&extraKeys
  1615                                                       status:&status
  1616                                                        error:&error];
  1617     XCTAssertNotNil(decryptedOldBeacon);
  1618     XCTAssertNil(error);
  1619 
  1620     PEPMessage *decryptedNewBeacon = [session decryptMessage:newBeacon
  1621                                                        flags:nil
  1622                                                       rating:&rating
  1623                                                    extraKeys:&extraKeys
  1624                                                       status:&status
  1625                                                        error:&error];
  1626     XCTAssertNotNil(decryptedNewBeacon);
  1627     XCTAssertNil(error);
  1628 
  1629     [self shutdownSync];
  1630 }
  1631 
  1632 #pragma mark - Helpers
  1633 
  1634 - (void)testSendMessageOnSession:(PEPSession *)session
  1635 {
  1636     XCTAssertEqual(self.sendMessageDelegate.messages.count, 0);
  1637     XCTAssertNil(self.sendMessageDelegate.lastMessage);
  1638 
  1639     PEPIdentity *identMe = [[PEPIdentity alloc]
  1640                             initWithAddress:@"me-myself-and-i@pep-project.org"
  1641                             userID:@"me-myself-and-i"
  1642                             userName:@"pEp Me"
  1643                             isOwn:YES];
  1644 
  1645     NSError *error = nil;
  1646     XCTAssertTrue([session mySelf:identMe error:&error]);
  1647     XCTAssertNil(error);
  1648 
  1649     [self startSync];
  1650 
  1651     XCTKVOExpectation *expHaveMessage = [[XCTKVOExpectation alloc]
  1652                                          initWithKeyPath:@"lastMessage"
  1653                                          object:self.sendMessageDelegate];
  1654 
  1655     XCTAssertNotNil(identMe.fingerPrint);
  1656 
  1657     [self waitForExpectations:@[expHaveMessage] timeout:PEPTestInternalSyncTimeout];
  1658     XCTAssertNotNil(self.sendMessageDelegate.lastMessage);
  1659 
  1660     XCTAssertEqual(self.sendMessageDelegate.messages.count, 1);
  1661     [self shutdownSync];
  1662 }
  1663 
  1664 - (void)startSync
  1665 {
  1666     self.sendMessageDelegate = [PEPSessionTestSendMessageDelegate new];
  1667     self.notifyHandshakeDelegate = [PEPSessionTestNotifyHandshakeDelegate new];
  1668 
  1669     self.sync = [[PEPSync alloc]
  1670                  initWithSendMessageDelegate:self.sendMessageDelegate
  1671                  notifyHandshakeDelegate:self.notifyHandshakeDelegate];
  1672     [self.sync startup];
  1673 }
  1674 
  1675 - (void)shutdownSync
  1676 {
  1677     [self.sync shutdown];
  1678 }
  1679 
  1680 - (NSNumber * _Nullable)testOutgoingRatingForMessage:(PEPMessage * _Nonnull)theMessage
  1681                                              session:(PEPSession *)session
  1682                                                error:(NSError * _Nullable * _Nullable)error
  1683 {
  1684     NSNumber *ratingOriginal = [session outgoingRatingForMessage:theMessage error:error];
  1685     NSNumber *ratingPreview = [session outgoingRatingPreviewForMessage:theMessage error:nil];
  1686     XCTAssertEqual(ratingOriginal, ratingPreview);
  1687     return ratingOriginal;
  1688 }
  1689 
  1690 - (void)testPassiveModeEnabled:(BOOL)passiveModeEnabled
  1691 {
  1692     [PEPObjCAdapter setPassiveModeEnabled:passiveModeEnabled];
  1693     PEPSession *session = [PEPSession new];
  1694 
  1695     PEPIdentity *identMe = [[PEPIdentity alloc]
  1696                             initWithAddress:@"me-myself-and-i@pep-project.org"
  1697                             userID:@"me-myself-and-i"
  1698                             userName:@"pEp Me"
  1699                             isOwn:YES];
  1700     NSError *error = nil;
  1701     XCTAssertTrue([session mySelf:identMe error:&error]);
  1702     XCTAssertNil(error);
  1703 
  1704     XCTAssertNotNil(identMe.fingerPrint);
  1705 
  1706     PEPIdentity *identAlice = [[PEPIdentity alloc]
  1707                                initWithAddress:@"pep.test.alice@pep-project.org"
  1708                                userID:@"alice_user_id"
  1709                                userName:@"Alice"
  1710                                isOwn:NO];
  1711 
  1712     NSString *shortMessage = @"whatever it may be";
  1713     NSString *longMessage = [NSString stringWithFormat:@"%@ %@", shortMessage, shortMessage];
  1714     PEPMessage *message = [PEPMessage new];
  1715     message.direction = PEPMsgDirectionOutgoing;
  1716     message.from = identMe;
  1717     message.to = @[identAlice];
  1718     message.shortMessage = shortMessage;
  1719     message.longMessage = longMessage;
  1720 
  1721     PEPStatus status = PEPStatusKeyNotFound;
  1722     PEPMessage *encryptedMessage =  [session encryptMessage:message
  1723                                                   extraKeys:@[]
  1724                                                      status:&status
  1725                                                       error:&error];
  1726     XCTAssertNil(error);
  1727     XCTAssertEqual(status, PEPStatusUnencrypted);
  1728 
  1729     if (passiveModeEnabled) {
  1730         XCTAssertNil(encryptedMessage.attachments);
  1731     } else {
  1732         XCTAssertEqual(encryptedMessage.attachments.count, 1);
  1733     }
  1734 }
  1735 
  1736 /**
  1737  Determines the rating for the given identity.
  1738  @return PEPRatingUndefined on error
  1739  */
  1740 - (PEPRating)ratingForIdentity:(PEPIdentity *)identity session:(PEPSession *)session
  1741 {
  1742     NSError *error;
  1743     NSNumber *numRating = [session ratingForIdentity:identity error:&error];
  1744     XCTAssertNil(error);
  1745     return numRating.pEpRating;
  1746 }
  1747 
  1748 - (PEPIdentity *)checkImportingKeyFilePath:(NSString *)filePath address:(NSString *)address
  1749                                     userID:(NSString *)userID
  1750                                fingerPrint:(NSString *)fingerPrint
  1751                                    session:(PEPSession *)session
  1752 {
  1753     if (!session) {
  1754         session = [PEPSession new];
  1755     }
  1756 
  1757     BOOL success = [PEPTestUtils importBundledKey:filePath session:session];
  1758     XCTAssertTrue(success);
  1759 
  1760     if (success) {
  1761         // Our test user:
  1762         PEPIdentity *identTest = [[PEPIdentity alloc]
  1763                                   initWithAddress:address
  1764                                   userID:userID
  1765                                   userName:[NSString stringWithFormat:@"Some User Name %@", userID]
  1766                                   isOwn:NO];
  1767 
  1768         NSError *error = nil;
  1769         XCTAssertTrue([session updateIdentity:identTest error:&error]);
  1770         XCTAssertNil(error);
  1771         XCTAssertNotNil(identTest.fingerPrint);
  1772         XCTAssertEqualObjects(identTest.fingerPrint, fingerPrint);
  1773 
  1774         return identTest;
  1775     } else {
  1776         return nil;
  1777     }
  1778 }
  1779 
  1780 - (PEPIdentity *)checkMySelfImportingKeyFilePath:(NSString *)filePath
  1781                                          address:(NSString *)address
  1782                                           userID:(NSString *)userID
  1783                                      fingerPrint:(NSString *)fingerPrint
  1784                                          session:(PEPSession *)session
  1785 {
  1786     XCTAssertTrue([PEPTestUtils importBundledKey:filePath session:session]);
  1787 
  1788     // Our test user:
  1789     PEPIdentity *identTest = [[PEPIdentity alloc]
  1790                               initWithAddress:address
  1791                               userID:userID
  1792                               userName:[NSString stringWithFormat:@"Some User Name %@", userID]
  1793                               isOwn:YES
  1794                               fingerPrint: fingerPrint];
  1795 
  1796     NSError *error;
  1797     XCTAssertTrue([session setOwnKey:identTest fingerprint:fingerPrint error:&error]);
  1798     XCTAssertNil(error);
  1799     XCTAssertNotNil(identTest.fingerPrint);
  1800     XCTAssertEqualObjects(identTest.fingerPrint, fingerPrint);
  1801 
  1802     return identTest;
  1803 }
  1804 
  1805 /**
  1806  Verifies that a partner ID is really a correct Identity.
  1807  Usually used on identities imported as keys, since the engine has problems with them.
  1808  */
  1809 - (void)updateAndVerifyPartnerIdentity:(PEPIdentity *)partnerIdentity session:(PEPSession *)session
  1810 {
  1811     NSError *error = nil;
  1812 
  1813     XCTAssertNotNil(partnerIdentity.fingerPrint);
  1814     XCTAssertTrue([session updateIdentity:partnerIdentity error:&error]);
  1815     XCTAssertNil(error);
  1816     XCTAssertNotNil(partnerIdentity.fingerPrint);
  1817     NSString *fingerprint = partnerIdentity.fingerPrint;
  1818     partnerIdentity.fingerPrint = nil;
  1819     XCTAssertTrue([session updateIdentity:partnerIdentity error:&error]);
  1820     XCTAssertNil(error);
  1821     XCTAssertNotNil(partnerIdentity.fingerPrint);
  1822     XCTAssertEqualObjects(partnerIdentity.fingerPrint, fingerprint);
  1823 }
  1824 
  1825 - (PEPMessage *)mailWrittenToMySelf
  1826 {
  1827     PEPSession *session = [PEPSession new];
  1828 
  1829     // Write a e-mail to yourself ...
  1830     PEPIdentity *me = [PEPTestUtils ownPepIdentityWithAddress:@"me@peptest.ch"
  1831                                                      userName:@"userName"];
  1832     NSError *error = nil;
  1833     XCTAssertTrue([session mySelf:me error:&error]);
  1834     XCTAssertNil(error);
  1835 
  1836     NSString *shortMessage = @"Subject";
  1837     NSString *longMessage = @"Oh, this is a long body text!";
  1838     PEPMessage *mail = [PEPTestUtils mailFrom:me
  1839                                       toIdent:me
  1840                                  shortMessage:shortMessage
  1841                                   longMessage:longMessage
  1842                                      outgoing:YES];
  1843     PEPStatus status = PEPStatusUnknownError;
  1844     PEPMessage *encMessage = [session
  1845                               encryptMessage:mail
  1846                               forSelf:me
  1847                               extraKeys:nil
  1848                               status:&status
  1849                               error:&error];
  1850     XCTAssertNil(error);
  1851 
  1852     return encMessage;
  1853 }
  1854 
  1855 - (PEPMessage *)internalEncryptToMySelfKeys:(PEPStringList **)keys
  1856 {
  1857     PEPSession *session = [PEPSession new];
  1858     
  1859     PEPIdentity *me = [PEPTestUtils ownPepIdentityWithAddress:@"me@peptest.ch"
  1860                                                      userName:@"userName"];
  1861     NSError *error = nil;
  1862     XCTAssertTrue([session mySelf:me error:&error]);
  1863     XCTAssertNil(error);
  1864 
  1865     XCTAssertNotNil(me.fingerPrint);
  1866 
  1867     // Create draft
  1868     NSString *shortMessage = @"Subject";
  1869     NSString *longMessage = @"Oh, this is a long body text!";
  1870     PEPMessage *mail = [PEPTestUtils mailFrom:me toIdent:me shortMessage:shortMessage longMessage:longMessage outgoing:YES];
  1871 
  1872     PEPStatus status;
  1873     PEPMessage *encMessage = [session
  1874                               encryptMessage:mail
  1875                               forSelf:me
  1876                               extraKeys:nil
  1877                               status:&status
  1878                               error:&error];
  1879     XCTAssertEqual(status, 0);
  1880     XCTAssertEqualObjects(encMessage.shortMessage, @"p≡p");
  1881 
  1882     PEPRating rating;
  1883     error = nil;
  1884     PEPMessage *unencDict = [session
  1885                              decryptMessage:encMessage
  1886                              flags:nil
  1887                              rating:&rating
  1888                              extraKeys:keys
  1889                              status:nil
  1890                              error:&error];
  1891     XCTAssertNotNil(unencDict);
  1892     XCTAssertNil(error);
  1893 
  1894     XCTAssertGreaterThanOrEqual(rating, PEPRatingReliable);
  1895 
  1896     XCTAssertEqualObjects(unencDict.shortMessage, shortMessage);
  1897     XCTAssertEqualObjects(unencDict.longMessage, longMessage);
  1898 
  1899     return unencDict;
  1900 }
  1901 
  1902 - (void)pEpCleanUp
  1903 {
  1904     [PEPTestUtils cleanUp];
  1905 }
  1906 
  1907 - (void)helperXEncStatusForOutgoingEncryptdMailToSelf:(BOOL)toSelf
  1908                                        expectedRating:(PEPRating)expectedRating
  1909 {
  1910     PEPSession *session = [PEPSession new];
  1911 
  1912     // Partner pubkey for the test:
  1913     // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
  1914     // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
  1915     XCTAssertTrue([PEPTestUtils importBundledKey:@"6FF00E97.asc" session:session]);
  1916 
  1917     PEPIdentity *identAlice = [[PEPIdentity alloc]
  1918                                initWithAddress:@"pep.test.alice@pep-project.org"
  1919                                userID:ownUserId
  1920                                userName:@"pEp Test Alice"
  1921                                isOwn:NO
  1922                                fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
  1923     [self updateAndVerifyPartnerIdentity:identAlice session:session];
  1924 
  1925     PEPIdentity *identMe = [[PEPIdentity alloc]
  1926                                initWithAddress:@"me-myself-and-i@pep-project.org"
  1927                                userID:@"me-myself-and-i"
  1928                                userName:@"pEp Me"
  1929                                isOwn:YES];
  1930     NSError *error = nil;
  1931     XCTAssertTrue([session mySelf:identMe error:&error]);
  1932     XCTAssertNil(error);
  1933 
  1934     XCTAssertNotNil(identMe.fingerPrint);
  1935 
  1936     PEPMessage *msg = [PEPMessage new];
  1937     msg.from = identMe;
  1938     msg.to = @[identAlice];
  1939     msg.shortMessage = @"Mail to Alice";
  1940     msg.longMessage = @"Alice?";
  1941     msg.direction = PEPMsgDirectionOutgoing;
  1942 
  1943     NSNumber *numRating = [self testOutgoingRatingForMessage:msg session:session error:&error];
  1944     XCTAssertNotNil(numRating);
  1945     XCTAssertNil(error);
  1946     XCTAssertEqual(numRating.pEpRating, PEPRatingReliable);
  1947 
  1948     PEPMessage *encMsg;
  1949 
  1950     PEPStatus statusEnc = PEPStatusVersionMismatch;
  1951     if (toSelf) {
  1952         encMsg = [session
  1953                   encryptMessage:msg
  1954                   forSelf:identMe
  1955                   extraKeys:nil
  1956                   status:&statusEnc
  1957                   error:&error];
  1958         XCTAssertEqual(statusEnc, PEPStatusOK);
  1959     } else {
  1960         encMsg = [session encryptMessage:msg extraKeys:nil status:nil error:&error];
  1961         XCTAssertNotNil(encMsg);
  1962         XCTAssertNil(error);
  1963     }
  1964     XCTAssertNotNil(encMsg);
  1965 
  1966     PEPStringList *keys;
  1967     PEPRating pEpRating;
  1968     error = nil;
  1969     PEPMessage *decMsg = [session
  1970                           decryptMessage:encMsg
  1971                           flags:nil
  1972                           rating:&pEpRating
  1973                           extraKeys:&keys
  1974                           status:nil
  1975                           error:&error];
  1976     XCTAssertNil(error);
  1977     XCTAssertNotNil(decMsg);
  1978 
  1979     XCTAssertEqual(pEpRating, expectedRating);
  1980 
  1981     NSArray * encStatusField = nil;
  1982     for (NSArray *field in decMsg.optionalFields) {
  1983         NSString *header = [field[0] lowercaseString];
  1984         if ([header isEqualToString:@"x-encstatus"]) {
  1985             encStatusField = field;
  1986         }
  1987     }
  1988     XCTAssertNotNil(encStatusField);
  1989     if (encStatusField) {
  1990         PEPRating outgoingRating = [session ratingFromString:encStatusField[1]];
  1991         XCTAssertEqual(outgoingRating, expectedRating);
  1992     }
  1993 }
  1994 
  1995 @end