pEpObjCTests/PEPSessionTest.m
author Dirk Zimmermann <dz@pep.security>
Fri, 25 Oct 2019 12:05:24 +0200
branchIOS-1943
changeset 1325 79fbbc66bd39
parent 1324 fe4c440db4df
permissions -rw-r--r--
IOS-1943 Minimal test reproducing the problem.
     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 }
  1129 
  1130 - (void)testTrustOwnKey
  1131 {
  1132     PEPSession *session = [PEPSession new];
  1133 
  1134     PEPIdentity *me = [PEPTestUtils ownPepIdentityWithAddress:@"me@peptest.ch"
  1135                                                      userName:@"userName"];
  1136     NSError *error = nil;
  1137     XCTAssertTrue([session mySelf:me error:&error]);
  1138     XCTAssertNil(error);
  1139 
  1140     XCTAssertTrue([session trustOwnKeyIdentity:me error:&error]);
  1141     XCTAssertNil(error);
  1142 }
  1143 
  1144 #pragma mark - configUnencryptedSubject
  1145 
  1146 - (void)testConfigUnencryptedSubject
  1147 {
  1148     // Setup Config to encrypt subject
  1149     [PEPObjCAdapter setUnEncryptedSubjectEnabled:NO];
  1150 
  1151     // Write mail to yourself ...
  1152     PEPMessage *encMessage = [self mailWrittenToMySelf];
  1153 
  1154     // ... and assert subject is encrypted
  1155     XCTAssertEqualObjects(encMessage.shortMessage, @"p≡p", @"Subject should be encrypted");
  1156 }
  1157 
  1158 - (void)testConfigUnencryptedSubjectEncryptedSubjectDisabled
  1159 {
  1160     // Setup Config to not encrypt subject
  1161     [PEPObjCAdapter setUnEncryptedSubjectEnabled:YES];
  1162 
  1163     // Write mail to yourself ...
  1164     PEPMessage *encMessage = [self mailWrittenToMySelf];
  1165 
  1166     // pEp to pEp uses message 2.0, which always encrypts subjects (ENGINE-429)
  1167     XCTAssertEqualObjects(encMessage.shortMessage, @"p≡p", @"Subject should be encrypted");
  1168 }
  1169 
  1170 #pragma mark - Passive mode
  1171 
  1172 - (void)testPassiveMode
  1173 {
  1174     [self testPassiveModeEnabled:NO];
  1175     [self testPassiveModeEnabled:YES];
  1176 }
  1177 
  1178 #pragma mark - Decryption
  1179 
  1180 - (void)testDecryptionOfUnencryptedMessageWithOdtAttachmentContainingSpace
  1181 {
  1182     PEPSession *session = [PEPSession new];
  1183 
  1184     PEPIdentity *me = [PEPTestUtils ownPepIdentityWithAddress:@"me@peptest.ch"
  1185                                                      userName:@"userName"];
  1186     NSError *error = nil;
  1187     XCTAssertTrue([session mySelf:me error:&error]);
  1188     XCTAssertNil(error);
  1189 
  1190     const char *dataString = "blahblah";
  1191     const size_t dataSize = strlen(dataString);
  1192     char *rawData = strndup(dataString, dataSize);
  1193 
  1194     PEPAttachment *attachment = [[PEPAttachment alloc]
  1195                                  initWithData:[NSData
  1196                                                dataWithBytesNoCopy:rawData length:dataSize]];
  1197     attachment.filename = @"Someone andTextIncludingTheSpace.odt";
  1198     attachment.mimeType = @"application/vnd.oasis.opendocument.text";
  1199 
  1200     NSString *shortMessage = @"Subject";
  1201     NSString *longMessage = @"Oh, this is a long body text!";
  1202     PEPMessage *mail = [PEPTestUtils mailFrom:me
  1203                                       toIdent:me
  1204                                  shortMessage:shortMessage
  1205                                   longMessage:longMessage
  1206                                      outgoing:YES];
  1207 
  1208     mail.attachments = @[attachment];
  1209 
  1210     error = nil;
  1211     PEPStringList *keys;
  1212     PEPRating rating = PEPRatingUndefined;
  1213     PEPMessage *decmsg = [session
  1214                           decryptMessage:mail
  1215                           flags:nil
  1216                           rating:&rating
  1217                           extraKeys:&keys
  1218                           status:nil
  1219                           error:&error];
  1220     XCTAssertNotNil(decmsg);
  1221     XCTAssertNil(error);
  1222     XCTAssertEqual(rating, PEPRatingUnencrypted);
  1223 
  1224     PEPAttachment *decryptedAttachment = [decmsg.attachments objectAtIndex:0];
  1225     XCTAssertEqualObjects(decryptedAttachment.mimeType, attachment.mimeType);
  1226     XCTAssertEqualObjects(decryptedAttachment.filename, attachment.filename);
  1227 }
  1228 
  1229 #pragma mark - Sync
  1230 
  1231 /**
  1232  Prove that mySelf triggers a message to be sent.
  1233  */
  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 #pragma mark - key_reset_identity
  1270 
  1271 - (void)testKeyResetIdentity
  1272 {
  1273     PEPSession *session = [PEPSession new];
  1274 
  1275     PEPIdentity *me = [PEPTestUtils ownPepIdentityWithAddress:@"me@peptest.ch"
  1276                                                      userName:@"userName"];
  1277 
  1278     NSError *error = nil;
  1279     XCTAssertTrue([session mySelf:me error:&error]);
  1280     XCTAssertNil(error);
  1281 
  1282     NSString *fprOriginal = me.fingerPrint;
  1283     XCTAssertNotNil(fprOriginal);
  1284 
  1285     XCTAssertTrue([session keyReset:me fingerprint:nil error:&error]);
  1286     XCTAssertNil(error);
  1287 
  1288     XCTAssertTrue([session mySelf:me error:&error]);
  1289     XCTAssertNil(error);
  1290 
  1291     NSString *fprAfterReset = me.fingerPrint;
  1292     XCTAssertNotNil(fprAfterReset);
  1293 
  1294     XCTAssertNotEqual(fprOriginal, fprAfterReset);
  1295 }
  1296 
  1297 #pragma mark - leave_device_group
  1298 
  1299 /** Leaving a device group is successful even though none exists. */
  1300 - (void)testSuccessfulLeaveDeviceGroup
  1301 {
  1302     PEPSession *session = [PEPSession new];
  1303 
  1304     PEPIdentity *identMe = [[PEPIdentity alloc]
  1305                             initWithAddress:@"me-myself-and-i@pep-project.org"
  1306                             userID:@"me-myself-and-i"
  1307                             userName:@"pEp Me"
  1308                             isOwn:YES];
  1309     NSError *error = nil;
  1310     XCTAssertTrue([session mySelf:identMe error:&error]);
  1311     XCTAssertNil(error);
  1312 
  1313     [self startSync];
  1314 
  1315     error = nil;
  1316     XCTAssertTrue([session leaveDeviceGroupError:&error]);
  1317     XCTAssertNil(error);
  1318 
  1319     [self shutdownSync];
  1320 }
  1321 
  1322 #pragma mark - enable/disable sync
  1323 
  1324 - (void)testEnableDisableFailForSyncOnPartnerIdentity
  1325 {
  1326     PEPIdentity *notMe = [[PEPIdentity alloc]
  1327                           initWithAddress:@"notme@pep-project.org"
  1328                           userID:@"notme_ID"
  1329                           userName:@"notme"
  1330                           isOwn:NO];
  1331 
  1332     NSError *error = nil;
  1333     XCTAssertFalse([notMe enableKeySync:&error]);
  1334     XCTAssertNotNil(error);
  1335 
  1336     error = nil;
  1337     XCTAssertFalse([notMe disableKeySync:&error]);
  1338     XCTAssertNotNil(error);
  1339 
  1340     error = nil;
  1341     XCTAssertNil([notMe queryKeySyncEnabled:&error]);
  1342     XCTAssertNotNil(error);
  1343 }
  1344 
  1345 - (void)testEnableDisableSyncOnOwnIdentityWithQuery
  1346 {
  1347     PEPSession *session = [PEPSession new];
  1348 
  1349     PEPIdentity *identMe1 = [[PEPIdentity alloc]
  1350                              initWithAddress:@"me-myself-and-i@pep-project.org"
  1351                              userID:@"me-myself-and-i"
  1352                              userName:@"pEp Me"
  1353                              isOwn:YES];
  1354     NSError *error = nil;
  1355     XCTAssertTrue([session mySelf:identMe1 error:&error]);
  1356     XCTAssertNil(error);
  1357 
  1358     error = nil;
  1359     PEPIdentity *identMe2 = [[PEPIdentity alloc]
  1360                              initWithAddress:@"me-myself-and-i2@pep-project.org"
  1361                              userID:@"me-myself-and-i2"
  1362                              userName:@"pEp Me2"
  1363                              isOwn:YES];
  1364     XCTAssertTrue([session mySelf:identMe2 error:&error]);
  1365     XCTAssertNil(error);
  1366 
  1367     XCTAssertNotEqualObjects(identMe1.fingerPrint, identMe2.fingerPrint);
  1368 
  1369     for (int i = 0; i < 10; ++i) {
  1370         error = nil;
  1371         BOOL enable = i % 2 == 0; // enable keysync on even numbers (roughly)
  1372         if (enable) {
  1373             XCTAssertTrue([session enableSyncForIdentity:identMe1 error:&error]);
  1374             XCTAssertTrue([identMe2 enableKeySync:&error]);
  1375         } else {
  1376             XCTAssertTrue([session disableSyncForIdentity:identMe1 error:&error]);
  1377             XCTAssertTrue([identMe2 disableKeySync:&error]);
  1378         }
  1379         XCTAssertNil(error);
  1380 
  1381         NSNumber *keySyncState1 = [session queryKeySyncEnabledForIdentity:identMe1 error:&error];
  1382         NSNumber *keySyncState2 = [identMe2 queryKeySyncEnabled:&error];
  1383         XCTAssertNil(error);
  1384         XCTAssertNotNil(keySyncState1);
  1385         XCTAssertNotNil(keySyncState2);
  1386         if (enable) {
  1387             XCTAssertTrue([keySyncState1 boolValue]);
  1388         } else {
  1389             XCTAssertFalse([keySyncState1 boolValue]);
  1390         }
  1391         XCTAssertEqualObjects(keySyncState1, keySyncState2);
  1392     }
  1393 }
  1394 
  1395 /**
  1396  ENGINE-604, just in case.
  1397  */
  1398 - (void)testQueryKeySyncOnOwnIdentityInALoop
  1399 {
  1400     PEPSession *session = [PEPSession new];
  1401 
  1402     PEPIdentity *identMe = [[PEPIdentity alloc]
  1403                             initWithAddress:@"me-myself-and-i@pep-project.org"
  1404                             userID:@"me-myself-and-i"
  1405                             userName:@"pEp Me"
  1406                             isOwn:YES];
  1407     NSError *error = nil;
  1408     XCTAssertTrue([session mySelf:identMe error:&error]);
  1409     XCTAssertNil(error);
  1410 
  1411     for (NSNumber *numBool in @[@YES, @NO]) {
  1412         error = nil;
  1413         if ([numBool boolValue]) {
  1414             XCTAssertTrue([session enableSyncForIdentity:identMe error:&error]);
  1415         } else {
  1416             XCTAssertTrue([session disableSyncForIdentity:identMe error:&error]);
  1417         }
  1418         XCTAssertNil(error);
  1419 
  1420         for (int i = 0; i < 10; ++i) {
  1421             NSNumber *numQuery = [session queryKeySyncEnabledForIdentity:identMe error:&error];
  1422             XCTAssertNotNil(numQuery);
  1423             XCTAssertEqualObjects(numBool, numQuery);
  1424             XCTAssertNil(error);
  1425         }
  1426     }
  1427 }
  1428 
  1429 /**
  1430  Tests [PEPSessionProtocol keyResetAllOwnKeysError:error].
  1431 
  1432  Does the following:
  1433  * Do a mySelf.
  1434  * Catch the sent out sync (beacon?) message.
  1435  * Do a key reset on all own identities ([PEPSessionProtocol keyResetAllOwnKeysError:error]).
  1436  * Catch the sent out sync message.
  1437  * Decrypt the caught sync messages.
  1438  */
  1439 - (void)testDecryptBeaconsAfterKeyReset
  1440 {
  1441     PEPSession *session = [PEPSession new];
  1442 
  1443     XCTAssertEqual(self.sendMessageDelegate.messages.count, 0);
  1444     XCTAssertNil(self.sendMessageDelegate.lastMessage);
  1445 
  1446     PEPIdentity *identMe = [[PEPIdentity alloc]
  1447                             initWithAddress:@"me-myself-and-i@pep-project.org"
  1448                             userID:@"me-myself-and-i"
  1449                             userName:@"pEp Me"
  1450                             isOwn:YES];
  1451     NSError *error = nil;
  1452     XCTAssertTrue([session mySelf:identMe error:&error]);
  1453     XCTAssertNil(error);
  1454     XCTAssertNotNil(identMe.fingerPrint);
  1455 
  1456     [self startSync];
  1457 
  1458     XCTKVOExpectation *expHaveMessage1 = [[XCTKVOExpectation alloc]
  1459                                           initWithKeyPath:@"lastMessage"
  1460                                           object:self.sendMessageDelegate];
  1461     [self waitForExpectations:@[expHaveMessage1] timeout:PEPTestInternalSyncTimeout];
  1462     XCTAssertNotNil(self.sendMessageDelegate.lastMessage);
  1463     XCTAssertEqual(self.sendMessageDelegate.messages.count, 1);
  1464 
  1465     PEPMessage *oldBeacon = self.sendMessageDelegate.lastMessage;
  1466 
  1467     XCTAssertTrue([session keyResetAllOwnKeysError:&error]);
  1468     XCTAssertNil(error);
  1469 
  1470     XCTKVOExpectation *expHaveMessage2 = [[XCTKVOExpectation alloc]
  1471                                           initWithKeyPath:@"lastMessage"
  1472                                           object:self.sendMessageDelegate];
  1473     [self waitForExpectations:@[expHaveMessage2] timeout:PEPTestInternalSyncTimeout];
  1474     XCTAssertNotNil(self.sendMessageDelegate.lastMessage);
  1475     XCTAssertEqual(self.sendMessageDelegate.messages.count, 2);
  1476 
  1477     PEPMessage *newBeacon = self.sendMessageDelegate.lastMessage;
  1478 
  1479     XCTAssertNotEqual(oldBeacon, newBeacon);
  1480 
  1481     PEPRating rating;
  1482     PEPStringList *extraKeys;
  1483     PEPStatus status;
  1484     PEPMessage *decryptedOldBeacon = [session decryptMessage:oldBeacon
  1485                                                        flags:nil
  1486                                                       rating:&rating
  1487                                                    extraKeys:&extraKeys
  1488                                                       status:&status
  1489                                                        error:&error];
  1490     XCTAssertNotNil(decryptedOldBeacon);
  1491     XCTAssertNil(error);
  1492 
  1493     PEPMessage *decryptedNewBeacon = [session decryptMessage:newBeacon
  1494                                                        flags:nil
  1495                                                       rating:&rating
  1496                                                    extraKeys:&extraKeys
  1497                                                       status:&status
  1498                                                        error:&error];
  1499     XCTAssertNotNil(decryptedNewBeacon);
  1500     XCTAssertNil(error);
  1501 
  1502     [self shutdownSync];
  1503 }
  1504 
  1505 /// Reproduces the essential parts of IOS-1943.
  1506 - (void)testExtraKeysRating
  1507 {
  1508     PEPSession *session = [PEPSession new];
  1509 
  1510     PEPIdentity *identMe = [[PEPIdentity alloc]
  1511                             initWithAddress:@"me-myself-and-i@pep-project.org"
  1512                             userID:@"me-myself-and-i"
  1513                             userName:@"pEp Me"
  1514                             isOwn:YES];
  1515     NSError *error = nil;
  1516     XCTAssertTrue([session mySelf:identMe error:&error]);
  1517     XCTAssertNil(error);
  1518 
  1519     // Our extra keys user:
  1520     // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
  1521     // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
  1522     XCTAssertTrue([PEPTestUtils importBundledKey:@"6FF00E97_sec.asc" session:session]);
  1523 
  1524     NSString *aliceFingerprint = @"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97";
  1525     PEPIdentity *identAlice = [[PEPIdentity alloc]
  1526                                initWithAddress:@"pep.test.alice@pep-project.org"
  1527                                userID:ownUserId
  1528                                userName:@"pEp Test Alice"
  1529                                isOwn:NO
  1530                                fingerPrint:aliceFingerprint];
  1531     [self updateAndVerifyPartnerIdentity:identAlice session:session];
  1532 
  1533     PEPMessage *myMsg = [PEPMessage new];
  1534     myMsg.direction = PEPMsgDirectionOutgoing;
  1535     myMsg.from = identMe;
  1536     myMsg.to = @[identMe];
  1537 
  1538     NSNumber *outRating = [session outgoingRatingForMessage:myMsg error:&error];
  1539     XCTAssertNotNil(outRating);
  1540     XCTAssertNil(error);
  1541     XCTAssertEqual(outRating.intValue, PEPRatingTrustedAndAnonymized);
  1542 
  1543     PEPStatus status;
  1544     PEPMessage *encryptedMsgWithoutExtraKeys = [session
  1545                                                 encryptMessage:myMsg
  1546                                                 extraKeys:@[]
  1547                                                 status:&status
  1548                                                 error:&error];
  1549     XCTAssertNotNil(encryptedMsgWithoutExtraKeys);
  1550     XCTAssertNil(error);
  1551     XCTAssertEqual(status, PEPStatusOK);
  1552 
  1553     PEPMessage *encryptedMsgWithExtraKeys = [session
  1554                                              encryptMessage:myMsg
  1555                                              extraKeys:@[aliceFingerprint]
  1556                                              status:&status
  1557                                              error:&error];
  1558     XCTAssertNotNil(encryptedMsgWithExtraKeys);
  1559     XCTAssertNil(error);
  1560     XCTAssertEqual(status, PEPStatusOK);
  1561 
  1562     PEPDecryptFlags flags;
  1563     PEPRating rating;
  1564     NSArray *extraKeysWithoutExtraKeys;
  1565     PEPMessage *decryptedMsgWithoutExtraKeys = [session
  1566                                                 decryptMessage:encryptedMsgWithoutExtraKeys
  1567                                                 flags:&flags
  1568                                                 rating:&rating
  1569                                                 extraKeys:&extraKeysWithoutExtraKeys
  1570                                                 status:&status
  1571                                                 error:&error];
  1572     XCTAssertNotNil(decryptedMsgWithoutExtraKeys);
  1573     XCTAssertEqual(rating, PEPRatingTrustedAndAnonymized);
  1574 
  1575     NSArray *extraKeysWithExtraKeys;
  1576     PEPMessage *decryptedMsgWithExtraKeys = [session
  1577                                              decryptMessage:encryptedMsgWithExtraKeys
  1578                                              flags:&flags
  1579                                              rating:&rating
  1580                                              extraKeys:&extraKeysWithExtraKeys
  1581                                              status:&status
  1582                                              error:&error];
  1583     XCTAssertNotNil(decryptedMsgWithExtraKeys);
  1584     XCTAssertEqual(rating, PEPRatingTrustedAndAnonymized);
  1585 }
  1586 
  1587 #pragma mark - Helpers
  1588 
  1589 - (void)testSendMessageOnSession:(PEPSession *)session
  1590 {
  1591     XCTAssertEqual(self.sendMessageDelegate.messages.count, 0);
  1592     XCTAssertNil(self.sendMessageDelegate.lastMessage);
  1593 
  1594     PEPIdentity *identMe = [[PEPIdentity alloc]
  1595                             initWithAddress:@"me-myself-and-i@pep-project.org"
  1596                             userID:@"me-myself-and-i"
  1597                             userName:@"pEp Me"
  1598                             isOwn:YES];
  1599     NSError *error = nil;
  1600     XCTAssertTrue([session mySelf:identMe error:&error]);
  1601     XCTAssertNil(error);
  1602 
  1603     [self startSync];
  1604 
  1605     XCTKVOExpectation *expHaveMessage = [[XCTKVOExpectation alloc]
  1606                                          initWithKeyPath:@"lastMessage"
  1607                                          object:self.sendMessageDelegate];
  1608 
  1609     XCTAssertNotNil(identMe.fingerPrint);
  1610 
  1611     [self waitForExpectations:@[expHaveMessage] timeout:PEPTestInternalSyncTimeout];
  1612     XCTAssertNotNil(self.sendMessageDelegate.lastMessage);
  1613 
  1614     XCTAssertEqual(self.sendMessageDelegate.messages.count, 1);
  1615     [self shutdownSync];
  1616 }
  1617 
  1618 - (void)startSync
  1619 {
  1620     self.sendMessageDelegate = [PEPSessionTestSendMessageDelegate new];
  1621     self.notifyHandshakeDelegate = [PEPSessionTestNotifyHandshakeDelegate new];
  1622 
  1623     self.sync = [[PEPSync alloc]
  1624                  initWithSendMessageDelegate:self.sendMessageDelegate
  1625                  notifyHandshakeDelegate:self.notifyHandshakeDelegate];
  1626     [self.sync startup];
  1627 }
  1628 
  1629 - (void)shutdownSync
  1630 {
  1631     [self.sync shutdown];
  1632 }
  1633 
  1634 - (NSNumber * _Nullable)testOutgoingRatingForMessage:(PEPMessage * _Nonnull)theMessage
  1635                                              session:(PEPSession *)session
  1636                                                error:(NSError * _Nullable * _Nullable)error
  1637 {
  1638     NSNumber *ratingOriginal = [session outgoingRatingForMessage:theMessage error:error];
  1639     NSNumber *ratingPreview = [session outgoingRatingPreviewForMessage:theMessage error:nil];
  1640     XCTAssertEqual(ratingOriginal, ratingPreview);
  1641     return ratingOriginal;
  1642 }
  1643 
  1644 - (void)testPassiveModeEnabled:(BOOL)passiveModeEnabled
  1645 {
  1646     [PEPObjCAdapter setPassiveModeEnabled:passiveModeEnabled];
  1647     PEPSession *session = [PEPSession new];
  1648 
  1649     PEPIdentity *identMe = [[PEPIdentity alloc]
  1650                             initWithAddress:@"me-myself-and-i@pep-project.org"
  1651                             userID:@"me-myself-and-i"
  1652                             userName:@"pEp Me"
  1653                             isOwn:YES];
  1654     NSError *error = nil;
  1655     XCTAssertTrue([session mySelf:identMe error:&error]);
  1656     XCTAssertNil(error);
  1657 
  1658     XCTAssertNotNil(identMe.fingerPrint);
  1659 
  1660     PEPIdentity *identAlice = [[PEPIdentity alloc]
  1661                                initWithAddress:@"pep.test.alice@pep-project.org"
  1662                                userID:@"alice_user_id"
  1663                                userName:@"Alice"
  1664                                isOwn:NO];
  1665 
  1666     NSString *shortMessage = @"whatever it may be";
  1667     NSString *longMessage = [NSString stringWithFormat:@"%@ %@", shortMessage, shortMessage];
  1668     PEPMessage *message = [PEPMessage new];
  1669     message.direction = PEPMsgDirectionOutgoing;
  1670     message.from = identMe;
  1671     message.to = @[identAlice];
  1672     message.shortMessage = shortMessage;
  1673     message.longMessage = longMessage;
  1674 
  1675     PEPStatus status = PEPStatusKeyNotFound;
  1676     PEPMessage *encryptedMessage =  [session encryptMessage:message
  1677                                                   extraKeys:@[]
  1678                                                      status:&status
  1679                                                       error:&error];
  1680     XCTAssertNil(error);
  1681     XCTAssertEqual(status, PEPStatusUnencrypted);
  1682 
  1683     if (passiveModeEnabled) {
  1684         XCTAssertNil(encryptedMessage.attachments);
  1685     } else {
  1686         XCTAssertEqual(encryptedMessage.attachments.count, 1);
  1687     }
  1688 }
  1689 
  1690 /**
  1691  Determines the rating for the given identity.
  1692  @return PEPRatingUndefined on error
  1693  */
  1694 - (PEPRating)ratingForIdentity:(PEPIdentity *)identity session:(PEPSession *)session
  1695 {
  1696     NSError *error;
  1697     NSNumber *numRating = [session ratingForIdentity:identity error:&error];
  1698     XCTAssertNil(error);
  1699     return numRating.pEpRating;
  1700 }
  1701 
  1702 - (PEPIdentity *)checkImportingKeyFilePath:(NSString *)filePath address:(NSString *)address
  1703                                     userID:(NSString *)userID
  1704                                fingerPrint:(NSString *)fingerPrint
  1705                                    session:(PEPSession *)session
  1706 {
  1707     if (!session) {
  1708         session = [PEPSession new];
  1709     }
  1710 
  1711     BOOL success = [PEPTestUtils importBundledKey:filePath session:session];
  1712     XCTAssertTrue(success);
  1713 
  1714     if (success) {
  1715         // Our test user:
  1716         PEPIdentity *identTest = [[PEPIdentity alloc]
  1717                                   initWithAddress:address
  1718                                   userID:userID
  1719                                   userName:[NSString stringWithFormat:@"Some User Name %@", userID]
  1720                                   isOwn:NO];
  1721 
  1722         NSError *error = nil;
  1723         XCTAssertTrue([session updateIdentity:identTest error:&error]);
  1724         XCTAssertNil(error);
  1725         XCTAssertNotNil(identTest.fingerPrint);
  1726         XCTAssertEqualObjects(identTest.fingerPrint, fingerPrint);
  1727 
  1728         return identTest;
  1729     } else {
  1730         return nil;
  1731     }
  1732 }
  1733 
  1734 - (PEPIdentity *)checkMySelfImportingKeyFilePath:(NSString *)filePath
  1735                                          address:(NSString *)address
  1736                                           userID:(NSString *)userID
  1737                                      fingerPrint:(NSString *)fingerPrint
  1738                                          session:(PEPSession *)session
  1739 {
  1740     XCTAssertTrue([PEPTestUtils importBundledKey:filePath session:session]);
  1741 
  1742     // Our test user:
  1743     PEPIdentity *identTest = [[PEPIdentity alloc]
  1744                               initWithAddress:address
  1745                               userID:userID
  1746                               userName:[NSString stringWithFormat:@"Some User Name %@", userID]
  1747                               isOwn:YES
  1748                               fingerPrint: fingerPrint];
  1749 
  1750     NSError *error;
  1751     XCTAssertTrue([session setOwnKey:identTest fingerprint:fingerPrint error:&error]);
  1752     XCTAssertNil(error);
  1753     XCTAssertNotNil(identTest.fingerPrint);
  1754     XCTAssertEqualObjects(identTest.fingerPrint, fingerPrint);
  1755 
  1756     return identTest;
  1757 }
  1758 
  1759 /**
  1760  Verifies that a partner ID is really a correct Identity.
  1761  Usually used on identities imported as keys, since the engine has problems with them.
  1762  */
  1763 - (void)updateAndVerifyPartnerIdentity:(PEPIdentity *)partnerIdentity session:(PEPSession *)session
  1764 {
  1765     NSError *error = nil;
  1766 
  1767     XCTAssertNotNil(partnerIdentity.fingerPrint);
  1768     XCTAssertTrue([session updateIdentity:partnerIdentity error:&error]);
  1769     XCTAssertNil(error);
  1770     XCTAssertNotNil(partnerIdentity.fingerPrint);
  1771     NSString *fingerprint = partnerIdentity.fingerPrint;
  1772     partnerIdentity.fingerPrint = nil;
  1773     XCTAssertTrue([session updateIdentity:partnerIdentity error:&error]);
  1774     XCTAssertNil(error);
  1775     XCTAssertNotNil(partnerIdentity.fingerPrint);
  1776     XCTAssertEqualObjects(partnerIdentity.fingerPrint, fingerprint);
  1777 }
  1778 
  1779 - (PEPMessage *)mailWrittenToMySelf
  1780 {
  1781     PEPSession *session = [PEPSession new];
  1782 
  1783     // Write a e-mail to yourself ...
  1784     PEPIdentity *me = [PEPTestUtils ownPepIdentityWithAddress:@"me@peptest.ch"
  1785                                                      userName:@"userName"];
  1786     NSError *error = nil;
  1787     XCTAssertTrue([session mySelf:me error:&error]);
  1788     XCTAssertNil(error);
  1789 
  1790     NSString *shortMessage = @"Subject";
  1791     NSString *longMessage = @"Oh, this is a long body text!";
  1792     PEPMessage *mail = [PEPTestUtils mailFrom:me
  1793                                       toIdent:me
  1794                                  shortMessage:shortMessage
  1795                                   longMessage:longMessage
  1796                                      outgoing:YES];
  1797     PEPStatus status = PEPStatusUnknownError;
  1798     PEPMessage *encMessage = [session
  1799                               encryptMessage:mail
  1800                               forSelf:me
  1801                               extraKeys:nil
  1802                               status:&status
  1803                               error:&error];
  1804     XCTAssertNil(error);
  1805 
  1806     return encMessage;
  1807 }
  1808 
  1809 - (PEPMessage *)internalEncryptToMySelfKeys:(PEPStringList **)keys
  1810 {
  1811     PEPSession *session = [PEPSession new];
  1812     
  1813     PEPIdentity *me = [PEPTestUtils ownPepIdentityWithAddress:@"me@peptest.ch"
  1814                                                      userName:@"userName"];
  1815     NSError *error = nil;
  1816     XCTAssertTrue([session mySelf:me error:&error]);
  1817     XCTAssertNil(error);
  1818 
  1819     XCTAssertNotNil(me.fingerPrint);
  1820 
  1821     // Create draft
  1822     NSString *shortMessage = @"Subject";
  1823     NSString *longMessage = @"Oh, this is a long body text!";
  1824     PEPMessage *mail = [PEPTestUtils mailFrom:me toIdent:me shortMessage:shortMessage longMessage:longMessage outgoing:YES];
  1825 
  1826     PEPStatus status;
  1827     PEPMessage *encMessage = [session
  1828                               encryptMessage:mail
  1829                               forSelf:me
  1830                               extraKeys:nil
  1831                               status:&status
  1832                               error:&error];
  1833     XCTAssertEqual(status, 0);
  1834     XCTAssertEqualObjects(encMessage.shortMessage, @"p≡p");
  1835 
  1836     PEPRating rating;
  1837     error = nil;
  1838     PEPMessage *unencDict = [session
  1839                              decryptMessage:encMessage
  1840                              flags:nil
  1841                              rating:&rating
  1842                              extraKeys:keys
  1843                              status:nil
  1844                              error:&error];
  1845     XCTAssertNotNil(unencDict);
  1846     XCTAssertNil(error);
  1847 
  1848     XCTAssertGreaterThanOrEqual(rating, PEPRatingReliable);
  1849 
  1850     XCTAssertEqualObjects(unencDict.shortMessage, shortMessage);
  1851     XCTAssertEqualObjects(unencDict.longMessage, longMessage);
  1852 
  1853     return unencDict;
  1854 }
  1855 
  1856 - (void)pEpCleanUp
  1857 {
  1858     [PEPTestUtils cleanUp];
  1859 }
  1860 
  1861 - (void)helperXEncStatusForOutgoingEncryptdMailToSelf:(BOOL)toSelf
  1862                                        expectedRating:(PEPRating)expectedRating
  1863 {
  1864     PEPSession *session = [PEPSession new];
  1865 
  1866     // Partner pubkey for the test:
  1867     // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
  1868     // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
  1869     XCTAssertTrue([PEPTestUtils importBundledKey:@"6FF00E97.asc" session:session]);
  1870 
  1871     PEPIdentity *identAlice = [[PEPIdentity alloc]
  1872                                initWithAddress:@"pep.test.alice@pep-project.org"
  1873                                userID:ownUserId
  1874                                userName:@"pEp Test Alice"
  1875                                isOwn:NO
  1876                                fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
  1877     [self updateAndVerifyPartnerIdentity:identAlice session:session];
  1878 
  1879     PEPIdentity *identMe = [[PEPIdentity alloc]
  1880                                initWithAddress:@"me-myself-and-i@pep-project.org"
  1881                                userID:@"me-myself-and-i"
  1882                                userName:@"pEp Me"
  1883                                isOwn:YES];
  1884     NSError *error = nil;
  1885     XCTAssertTrue([session mySelf:identMe error:&error]);
  1886     XCTAssertNil(error);
  1887 
  1888     XCTAssertNotNil(identMe.fingerPrint);
  1889 
  1890     PEPMessage *msg = [PEPMessage new];
  1891     msg.from = identMe;
  1892     msg.to = @[identAlice];
  1893     msg.shortMessage = @"Mail to Alice";
  1894     msg.longMessage = @"Alice?";
  1895     msg.direction = PEPMsgDirectionOutgoing;
  1896 
  1897     NSNumber *numRating = [self testOutgoingRatingForMessage:msg session:session error:&error];
  1898     XCTAssertNotNil(numRating);
  1899     XCTAssertNil(error);
  1900     XCTAssertEqual(numRating.pEpRating, PEPRatingReliable);
  1901 
  1902     PEPMessage *encMsg;
  1903 
  1904     PEPStatus statusEnc = PEPStatusVersionMismatch;
  1905     if (toSelf) {
  1906         encMsg = [session
  1907                   encryptMessage:msg
  1908                   forSelf:identMe
  1909                   extraKeys:nil
  1910                   status:&statusEnc
  1911                   error:&error];
  1912         XCTAssertEqual(statusEnc, PEPStatusOK);
  1913     } else {
  1914         encMsg = [session encryptMessage:msg extraKeys:nil status:nil error:&error];
  1915         XCTAssertNotNil(encMsg);
  1916         XCTAssertNil(error);
  1917     }
  1918     XCTAssertNotNil(encMsg);
  1919 
  1920     PEPStringList *keys;
  1921     PEPRating pEpRating;
  1922     error = nil;
  1923     PEPMessage *decMsg = [session
  1924                           decryptMessage:encMsg
  1925                           flags:nil
  1926                           rating:&pEpRating
  1927                           extraKeys:&keys
  1928                           status:nil
  1929                           error:&error];
  1930     XCTAssertNil(error);
  1931     XCTAssertNotNil(decMsg);
  1932 
  1933     XCTAssertEqual(pEpRating, expectedRating);
  1934 
  1935     NSArray * encStatusField = nil;
  1936     for (NSArray *field in decMsg.optionalFields) {
  1937         NSString *header = [field[0] lowercaseString];
  1938         if ([header isEqualToString:@"x-encstatus"]) {
  1939             encStatusField = field;
  1940         }
  1941     }
  1942     XCTAssertNotNil(encStatusField);
  1943     if (encStatusField) {
  1944         PEPRating outgoingRating = [session ratingFromString:encStatusField[1]];
  1945         XCTAssertEqual(outgoingRating, expectedRating);
  1946     }
  1947 }
  1948 
  1949 @end