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