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