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