pEpObjCTests/PEPSessionTest.m
author Dirk Zimmermann <dirk@pep-project.org>
Thu, 12 Apr 2018 10:05:06 +0200
changeset 567 b9439926c09f
parent 565 9ff3a0bfc8f6
child 573 bc2c03efe133
permissions -rw-r--r--
IOSAD-72 isPEPUser error handling
     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 
    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         PEP_rating rating;
   477         XCTAssertTrue([session outgoingRating:&rating forMessage:msgGray error:&error]);
   478         XCTAssertEqual(rating, PEP_rating_unencrypted);
   479     }
   480 
   481     PEPIdentity *identBob = [self
   482                              checkImportingKeyFilePath:@"0xC9C2EE39.asc"
   483                              address:@"pep.test.bob@pep-project.org"
   484                              userID:@"42"
   485                              fingerPrint:@"BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39"
   486                              session: session];
   487     XCTAssertNotNil(identBob);
   488 
   489     PEPMessage *msg = [PEPMessage new];
   490     msg.from = identAlice;
   491     msg.to = @[identBob];
   492     msg.shortMessage = @"All Gray Test";
   493     msg.longMessage = @"This is a text content";
   494     msg.direction = PEP_dir_outgoing;
   495 
   496     NSError *error = nil;
   497 
   498     // Should be yellow, since no handshake happened.
   499     PEP_rating rating;
   500     XCTAssertTrue([session outgoingRating:&rating forMessage:msg error:&error]);
   501     XCTAssertNil(error);
   502     XCTAssertEqual(rating, PEP_rating_reliable);
   503 
   504     rating = [self ratingForIdentity:identBob session:session];
   505     XCTAssertEqual(rating, PEP_rating_reliable);
   506 
   507     // Let' say we got that handshake, set PEP_ct_confirmed in Bob's identity
   508     XCTAssertTrue([session trustPersonalKey:identBob error:&error]);
   509     XCTAssertNil(error);
   510 
   511     // This time it should be green
   512     XCTAssertTrue([session outgoingRating:&rating forMessage:msg error:&error]);
   513     XCTAssertEqual(rating, PEP_rating_trusted);
   514 
   515     rating = [self ratingForIdentity:identBob session:session];
   516     XCTAssertEqual(rating, PEP_rating_trusted);
   517 
   518     // Let' say we undo handshake
   519     XCTAssertTrue([session keyResetTrust:identBob error:&error]);
   520     XCTAssertNil(error);
   521 
   522     // Yellow ?
   523     XCTAssertTrue([session outgoingRating:&rating forMessage:msg error:&error]);
   524     XCTAssertEqual(rating, PEP_rating_reliable);
   525 
   526     // mistrust Bob
   527     XCTAssertTrue([session keyMistrusted:identBob error:&error]);
   528     XCTAssertNil(error);
   529 
   530     identBob.fingerPrint = nil;
   531     XCTAssertTrue([session updateIdentity:identBob error:&error]);
   532     XCTAssertNil(error);
   533     XCTAssertNil(identBob.fingerPrint);
   534 
   535     // Gray == PEP_rating_unencrypted
   536     XCTAssertTrue([session outgoingRating:&rating forMessage:msg error:&error]);
   537     XCTAssertEqual(rating, PEP_rating_unencrypted);
   538 
   539     // Undo
   540     XCTAssertTrue([session undoLastMistrustWithError:&error]);
   541     XCTAssertNil(error);
   542     XCTAssertTrue([session updateIdentity:identBob error:&error]);
   543     XCTAssertNil(error);
   544     XCTAssertNotNil(identBob.fingerPrint);
   545 
   546     // Back to yellow
   547     XCTAssertTrue([session outgoingRating:&rating forMessage:msg error:&error]);
   548 
   549     // After ENGINE-371 has been fixed, this should be just PEP_rating_reliable
   550     XCTAssertEqual(rating, PEP_rating_trusted);
   551     XCTAssertEqual([self ratingForIdentity:identBob session:session], PEP_rating_trusted);
   552 
   553     // Trust again
   554     XCTAssertTrue([session trustPersonalKey:identBob error:&error]);
   555     XCTAssertNil(error);
   556 
   557     // Back to green
   558     XCTAssertTrue([session outgoingRating:&rating forMessage:msg error:&error]);
   559     XCTAssertEqual(rating, PEP_rating_trusted);
   560 
   561     // Now let see if it turns back yellow if we add an unconfirmed folk.
   562     // pEp Test John (test key, don't use) <pep.test.john@pep-project.org>
   563     // AA2E4BEB93E5FE33DEFD8BE1135CD6D170DCF575
   564     XCTAssertTrue([PEPTestUtils importBundledKey:@"0x70DCF575.asc" session:session]);
   565 
   566     PEPIdentity *identJohn = [[PEPIdentity alloc]
   567                               initWithAddress:@"pep.test.john@pep-project.org"
   568                               userID:@"101" userName:@"pEp Test John"
   569                               isOwn:NO
   570                               fingerPrint:@"AA2E4BEB93E5FE33DEFD8BE1135CD6D170DCF575"];
   571 
   572     XCTAssertTrue([session updateIdentity:identJohn error:&error]);
   573     XCTAssertNil(error);
   574 
   575     msg.cc = @[[PEPTestUtils foreignPepIdentityWithAddress:@"pep.test.john@pep-project.org"
   576                                                   userName:@"pEp Test John"]];
   577     // Yellow ?
   578     XCTAssertTrue([session outgoingRating:&rating forMessage:msg error:&error]);
   579     XCTAssertEqual(rating, PEP_rating_reliable);
   580 
   581     PEPMessage *encMsg = [session encryptMessage:msg extraKeys:nil status:nil error:&error];
   582     XCTAssertNotNil(encMsg);
   583     XCTAssertNil(error);
   584 
   585     XCTAssertEqualObjects(encMsg.shortMessage, @"p≡p");
   586     XCTAssertTrue([encMsg.longMessage containsString:@"p≡p"]);
   587 }
   588 
   589 
   590 - (void)testOutgoingBccColors
   591 {
   592     PEPSession *session = [PEPSession new];
   593 
   594     // Our test user :
   595     // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
   596     // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
   597     XCTAssertTrue([PEPTestUtils importBundledKey:@"6FF00E97_sec.asc" session:session]);
   598 
   599     PEPIdentity *identAlice = [[PEPIdentity alloc]
   600                                initWithAddress:@"pep.test.alice@pep-project.org"
   601                                userID:ownUserId
   602                                userName:@"pEp Test Alice"
   603                                isOwn:YES
   604                                fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
   605 
   606     NSError *error = nil;
   607     XCTAssertTrue([session mySelf:identAlice error:&error]);
   608     XCTAssertNil(error);
   609 
   610     PEPMessage *msg = [PEPMessage new];
   611     msg.from = identAlice;
   612     msg.to = @[[[PEPIdentity alloc] initWithAddress:@"pep.test.bob@pep-project.org"
   613                                              userID:@"42" userName:@"pEp Test Bob" isOwn:NO]];
   614     msg.shortMessage = @"All Green Test";
   615     msg.longMessage = @"This is a text content";
   616     msg.direction = PEP_dir_outgoing;
   617 
   618     // Test with unknown Bob
   619     PEP_rating rating;
   620     XCTAssertTrue([session outgoingRating:&rating forMessage:msg error:&error]);
   621     XCTAssertEqual(rating, PEP_rating_unencrypted);
   622 
   623     // Now let see with bob's pubkey already known
   624     // pEp Test Bob (test key, don't use) <pep.test.bob@pep-project.org>
   625     // BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39
   626     XCTAssertTrue([PEPTestUtils importBundledKey:@"0xC9C2EE39.asc" session:session]);
   627 
   628     PEPIdentity *identBob = [[PEPIdentity alloc]
   629                              initWithAddress:@"pep.test.bob@pep-project.org"
   630                              userID:@"42" userName:@"pEp Test Bob"
   631                              isOwn:NO
   632                              fingerPrint:@"BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39"];
   633 
   634     XCTAssertTrue([session updateIdentity:identBob error:&error]);
   635     XCTAssertNil(error);
   636 
   637     // Should be yellow, since no handshake happened.
   638     XCTAssertTrue([session outgoingRating:&rating forMessage:msg error:&error]);
   639     XCTAssertEqual(rating, PEP_rating_reliable);
   640 
   641     rating = [self ratingForIdentity:identBob session:session];
   642     XCTAssertEqual(rating, PEP_rating_reliable);
   643 
   644     // Let' say we got that handshake, set PEP_ct_confirmed in Bob's identity
   645     XCTAssertTrue([session trustPersonalKey:identBob error:&error]);
   646     XCTAssertNil(error);
   647 
   648     // This time it should be green
   649     XCTAssertTrue([session outgoingRating:&rating forMessage:msg error:&error]);
   650     XCTAssertEqual(rating, PEP_rating_trusted);
   651 
   652     rating = [self ratingForIdentity:identBob session:session];
   653     XCTAssertEqual(rating, PEP_rating_trusted);
   654 
   655     // Now let see if it turns back yellow if we add an unconfirmed folk.
   656     // pEp Test John (test key, don't use) <pep.test.john@pep-project.org>
   657     // AA2E4BEB93E5FE33DEFD8BE1135CD6D170DCF575
   658     XCTAssertTrue([PEPTestUtils importBundledKey:@"0x70DCF575.asc" session:session]);
   659 
   660     PEPIdentity *identJohn = [[PEPIdentity alloc]
   661                               initWithAddress:@"pep.test.john@pep-project.org"
   662                               userID:@"101" userName:@"pEp Test John"
   663                               isOwn:NO
   664                               fingerPrint:@"AA2E4BEB93E5FE33DEFD8BE1135CD6D170DCF575"];
   665 
   666     XCTAssertTrue([session updateIdentity:identJohn error:&error]);
   667     XCTAssertNil(error);
   668 
   669     msg.bcc = @[[[PEPIdentity alloc] initWithAddress:@"pep.test.john@pep-project.org"
   670                                               userID:@"101" userName:@"pEp Test John" isOwn:NO]];
   671 
   672     // Yellow ?
   673     XCTAssertTrue([session outgoingRating:&rating forMessage:msg error:&error]);
   674     XCTAssertEqual(rating, PEP_rating_reliable);
   675 
   676     XCTAssertTrue([session trustPersonalKey:identJohn error:&error]);
   677     XCTAssertNil(error);
   678 
   679     // This time it should be green
   680     XCTAssertTrue([session outgoingRating:&rating forMessage:msg error:&error]);
   681     XCTAssertEqual(rating, PEP_rating_trusted);
   682 
   683     rating = [self ratingForIdentity:identJohn session:session];
   684     XCTAssertEqual(rating, PEP_rating_trusted);
   685 }
   686 
   687 - (void)testDontEncryptForMistrusted
   688 {
   689     PEPSession *session = [PEPSession new];
   690 
   691     // Our test user :
   692     // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
   693     // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
   694     XCTAssertTrue([PEPTestUtils importBundledKey:@"6FF00E97_sec.asc" session:session]);
   695 
   696     PEPIdentity *identAlice = [[PEPIdentity alloc]
   697                                initWithAddress:@"pep.test.alice@pep-project.org"
   698                                userID:ownUserId
   699                                userName:@"pEp Test Alice"
   700                                isOwn:YES
   701                                fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
   702 
   703     NSError *error = nil;
   704     XCTAssertTrue([session mySelf:identAlice error:&error]);
   705     XCTAssertNil(error);
   706 
   707     // pEp Test Bob (test key, don't use) <pep.test.bob@pep-project.org>
   708     // BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39
   709     XCTAssertTrue([PEPTestUtils importBundledKey:@"0xC9C2EE39.asc" session:session]);
   710 
   711     PEPIdentity *identBob = [[PEPIdentity alloc]
   712                              initWithAddress:@"pep.test.bob@pep-project.org"
   713                              userID:@"42" userName:@"pEp Test Bob"
   714                              isOwn:NO
   715                              fingerPrint:@"BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39"];
   716 
   717     XCTAssertTrue([session updateIdentity:identBob error:&error]);
   718     XCTAssertNil(error);
   719 
   720     // mistrust Bob
   721     XCTAssertTrue([session keyMistrusted:identBob error:&error]);
   722     XCTAssertNil(error);
   723 
   724     PEPMessage *msg = [PEPMessage new];
   725     msg.from = identAlice;
   726     msg.to = @[[[PEPIdentity alloc] initWithAddress:@"pep.test.bob@pep-project.org" userID:@"42"
   727                                            userName:@"pEp Test Bob" isOwn:NO]];
   728     msg.shortMessage = @"All Green Test";
   729     msg.longMessage = @"This is a text content";
   730     msg.direction = PEP_dir_outgoing;
   731 
   732     // Gray == PEP_rating_unencrypted
   733     PEP_rating rating;
   734     XCTAssertTrue([session outgoingRating:&rating forMessage:msg error:&error]);
   735     XCTAssertEqual(rating, PEP_rating_unencrypted);
   736 
   737     PEPMessage *encMsg = [session encryptMessage:msg extraKeys:nil status:nil error:&error];
   738     XCTAssertNotNil(encMsg);
   739     XCTAssertNil(error);
   740 
   741     XCTAssertNotEqualObjects(encMsg.attachments[0][@"mimeType"], @"application/pgp-encrypted");
   742 
   743     [self pEpCleanUp];
   744 }
   745 
   746 - (void)testRevoke
   747 {
   748     PEPSession *session = [PEPSession new];
   749 
   750     // Our test user :
   751     // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
   752     // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
   753     XCTAssertTrue([PEPTestUtils importBundledKey:@"6FF00E97_sec.asc" session:session]);
   754     NSString *fpr = @"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97";
   755 
   756     PEPIdentity *identAlice = [[PEPIdentity alloc]
   757                                initWithAddress:@"pep.test.alice@pep-project.org"
   758                                userID:ownUserId
   759                                userName:@"pEp Test Alice"
   760                                isOwn:YES
   761                                fingerPrint:fpr];
   762 
   763     NSError *error = nil;
   764     XCTAssertTrue([session mySelf:identAlice error:&error]);
   765     XCTAssertNil(error);
   766 
   767     PEPIdentity *identAlice2 = [identAlice mutableCopy];
   768 
   769     // This will revoke key
   770     XCTAssertTrue([session keyMistrusted:identAlice2 error:&error]);
   771     XCTAssertNil(error);
   772     identAlice2.fingerPrint = nil;
   773 
   774     XCTAssertTrue([session mySelf:identAlice error:&error]);
   775     XCTAssertNil(error);
   776 
   777     // Check fingerprint is different
   778     XCTAssertNotEqualObjects(identAlice2.fingerPrint, fpr);
   779 }
   780 
   781 - (void)testMailToMyself
   782 {
   783     PEPSession *session = [PEPSession new];
   784 
   785     // Our test user :
   786     // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
   787     // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
   788     XCTAssertTrue([PEPTestUtils importBundledKey:@"6FF00E97_sec.asc" session:session]);
   789 
   790     PEPIdentity *identAlice = [[PEPIdentity alloc]
   791                                initWithAddress:@"pep.test.alice@pep-project.org"
   792                                userID:ownUserId
   793                                userName:@"pEp Test Alice"
   794                                isOwn:YES
   795                                fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
   796 
   797     NSError *error = nil;
   798     XCTAssertTrue([session mySelf:identAlice error:&error]);
   799     XCTAssertNil(error);
   800 
   801     PEPMessage *msg = [PEPMessage new];
   802     msg.from = identAlice;
   803     msg.to = @[identAlice];
   804     msg.shortMessage = @"Mail to Myself";
   805     msg.longMessage = @"This is a text content";
   806     msg.direction = PEP_dir_outgoing;
   807 
   808     PEP_rating rating;
   809     XCTAssertTrue([session outgoingRating:&rating forMessage:msg error:&error]);
   810     XCTAssertEqual(rating, PEP_rating_trusted_and_anonymized);
   811 
   812     PEPMessage *encMsg = [session encryptMessage:msg extraKeys:nil status:nil error:&error];
   813     XCTAssertNotNil(encMsg);
   814     XCTAssertNil(error);
   815 
   816     NSArray *keys;
   817 
   818     error = nil;
   819     PEPMessage *decmsg = [session
   820                           decryptMessage:encMsg
   821                           rating:&rating
   822                           extraKeys:&keys
   823                           status:nil
   824                           error:&error];
   825     XCTAssertNotNil(decmsg);
   826     XCTAssertNil(error);
   827     XCTAssertEqual(rating, PEP_rating_trusted_and_anonymized);
   828 }
   829 
   830 - (void)testEncryptedMailFromMutt
   831 {
   832     PEPSession *session = [PEPSession new];
   833 
   834     // This is the public key for test001@peptest.ch
   835     XCTAssertTrue([PEPTestUtils importBundledKey:@"A3FC7F0A.asc" session:session]);
   836 
   837     // This is the secret key for test001@peptest.ch
   838     XCTAssertTrue([PEPTestUtils importBundledKey:@"A3FC7F0A_sec.asc" session:session]);
   839 
   840     // Mail from mutt, already processed into message dict by the app.
   841     NSMutableDictionary *msgDict = [[PEPTestUtils
   842                                      unarchiveDictionary:@"msg_to_A3FC7F0A_from_mutt.ser"]
   843                                     mutableCopy];
   844     [msgDict removeObjectForKey:kPepLongMessage];
   845     [msgDict removeObjectForKey:kPepLongMessageFormatted];
   846 
   847     // Also extracted "live" from the app.
   848     NSMutableDictionary *accountDict = [[PEPTestUtils
   849                                          unarchiveDictionary:@"account_A3FC7F0A.ser"]
   850                                         mutableCopy];
   851     [accountDict removeObjectForKey:kPepCommType];
   852     [accountDict removeObjectForKey:kPepFingerprint];
   853     PEPIdentity *identMe = [[PEPIdentity alloc] initWithDictionary:accountDict];
   854 
   855     NSError *error = nil;
   856     XCTAssertTrue([session mySelf:identMe error:&error]);
   857     XCTAssertNil(error);
   858 
   859     XCTAssertNotNil(identMe.fingerPrint);
   860 
   861     NSArray* keys;
   862     PEPMessage *msg = [PEPMessage new];
   863     [msg setValuesForKeysWithDictionary:msgDict];
   864 
   865     // Technically, the mail is encrypted, but the signatures don't match
   866     PEPMessage *pepDecryptedMail = [session
   867                                     decryptMessage:msg
   868                                     rating:nil
   869                                     extraKeys:&keys
   870                                     status:nil
   871                                     error:&error];
   872     XCTAssertNotNil(pepDecryptedMail);
   873     XCTAssertNil(error);
   874 
   875     XCTAssertNotNil(pepDecryptedMail.longMessage);
   876 }
   877 
   878 - (void)testOutgoingContactColor
   879 {
   880     PEPSession *session = [PEPSession new];
   881 
   882     PEPIdentity *partner1Orig = [PEPTestUtils foreignPepIdentityWithAddress:@"partner1@dontcare.me"
   883                                                                    userName:@"Partner 1"];
   884     NSString *pubKeyPartner1 = [PEPTestUtils loadResourceByName:@"partner1_F2D281C2789DD7F6_pub.asc"];
   885     XCTAssertNotNil(pubKeyPartner1);
   886 
   887     NSError *error = nil;
   888     XCTAssertTrue([session importKey:pubKeyPartner1 error:&error]);
   889     XCTAssertNil(error);
   890 
   891     PEP_rating color = [self ratingForIdentity:partner1Orig session:session];
   892     XCTAssertEqual(color, PEP_rating_reliable);
   893 }
   894 
   895 - (void)testGetTrustwords
   896 {
   897     PEPSession *session = [PEPSession new];
   898 
   899     PEPIdentity *partner1Orig = [[PEPIdentity alloc]
   900                                  initWithAddress:@"partner1@dontcare.me" userID:@"partner1"
   901                                  userName:@"partner1"
   902                                  isOwn:NO fingerPrint:@"F0CD3F7B422E5D587ABD885BF2D281C2789DD7F6"];
   903 
   904     PEPIdentity *meOrig = [[PEPIdentity alloc]
   905                            initWithAddress:@"me@dontcare.me" userID:@"me"
   906                            userName:@"me"
   907                            isOwn:NO fingerPrint:@"CC1F73F6FB774BF08B197691E3BFBCA9248FC681"];
   908 
   909     NSString *pubKeyPartner1 = [PEPTestUtils loadResourceByName:@"partner1_F2D281C2789DD7F6_pub.asc"];
   910     XCTAssertNotNil(pubKeyPartner1);
   911     NSString *pubKeyMe = [PEPTestUtils loadResourceByName:@"meATdontcare_E3BFBCA9248FC681_pub.asc"];
   912     XCTAssertNotNil(pubKeyMe);
   913     NSString *secKeyMe = [PEPTestUtils loadResourceByName:@"meATdontcare_E3BFBCA9248FC681_sec.asc"];
   914     XCTAssertNotNil(secKeyMe);
   915 
   916     NSError *error = nil;
   917     NSString *trustwordsFull = [session getTrustwordsIdentity1:meOrig identity2:partner1Orig
   918                                                       language:nil full:YES error:&error];
   919     XCTAssertNil(error);
   920     XCTAssertEqualObjects(trustwordsFull,
   921                           @"EMERSON GASPER TOKENISM BOLUS COLLAGE DESPISE BEDDED ENCRYPTION IMAGINE BEDFORD");
   922 
   923     NSString *trustwordsFullEnglish = [session getTrustwordsIdentity1:meOrig identity2:partner1Orig
   924                                                              language:@"en" full:YES error:&error];
   925     XCTAssertNil(error);
   926     XCTAssertEqualObjects(trustwordsFullEnglish, trustwordsFull);
   927 
   928     NSString *trustwordsUndefined = [session getTrustwordsIdentity1:meOrig identity2:partner1Orig
   929                                                            language:@"ZZ" full:YES error:&error];
   930     XCTAssertNotNil(error);
   931     XCTAssertNil(trustwordsUndefined);
   932 }
   933 
   934 - (void)testStringToRating
   935 {
   936     PEPSession *session = [PEPSession new];
   937     XCTAssertEqual([session ratingFromString:@"cannot_decrypt"], PEP_rating_cannot_decrypt);
   938     XCTAssertEqual([session ratingFromString:@"have_no_key"], PEP_rating_have_no_key);
   939     XCTAssertEqual([session ratingFromString:@"unencrypted"], PEP_rating_unencrypted);
   940     XCTAssertEqual([session ratingFromString:@"unencrypted_for_some"],
   941                    PEP_rating_unencrypted_for_some);
   942     XCTAssertEqual([session ratingFromString:@"unreliable"], PEP_rating_unreliable);
   943     XCTAssertEqual([session ratingFromString:@"reliable"], PEP_rating_reliable);
   944     XCTAssertEqual([session ratingFromString:@"trusted"], PEP_rating_trusted);
   945     XCTAssertEqual([session ratingFromString:@"trusted_and_anonymized"],
   946                    PEP_rating_trusted_and_anonymized);
   947     XCTAssertEqual([session ratingFromString:@"fully_anonymous"], PEP_rating_fully_anonymous);
   948     XCTAssertEqual([session ratingFromString:@"mistrust"], PEP_rating_mistrust);
   949     XCTAssertEqual([session ratingFromString:@"b0rken"], PEP_rating_b0rken);
   950     XCTAssertEqual([session ratingFromString:@"under_attack"], PEP_rating_under_attack);
   951     XCTAssertEqual([session ratingFromString:@"undefined"], PEP_rating_undefined);
   952     XCTAssertEqual([session ratingFromString:@"does not exist111"], PEP_rating_undefined);
   953 }
   954 
   955 - (void)testRatingToString
   956 {
   957     PEPSession *session = [PEPSession new];
   958     XCTAssertEqualObjects([session stringFromRating:PEP_rating_cannot_decrypt], @"cannot_decrypt");
   959     XCTAssertEqualObjects([session stringFromRating:PEP_rating_have_no_key], @"have_no_key");
   960     XCTAssertEqualObjects([session stringFromRating:PEP_rating_unencrypted], @"unencrypted");
   961     XCTAssertEqualObjects([session stringFromRating:PEP_rating_unencrypted_for_some],
   962                           @"unencrypted_for_some");
   963     XCTAssertEqualObjects([session stringFromRating:PEP_rating_unreliable], @"unreliable");
   964     XCTAssertEqualObjects([session stringFromRating:PEP_rating_reliable], @"reliable");
   965     XCTAssertEqualObjects([session stringFromRating:PEP_rating_trusted], @"trusted");
   966     XCTAssertEqualObjects([session stringFromRating:PEP_rating_trusted_and_anonymized],
   967                           @"trusted_and_anonymized");
   968     XCTAssertEqualObjects([session stringFromRating:PEP_rating_fully_anonymous],
   969                           @"fully_anonymous");
   970     XCTAssertEqualObjects([session stringFromRating:PEP_rating_mistrust], @"mistrust");
   971     XCTAssertEqualObjects([session stringFromRating:PEP_rating_b0rken], @"b0rken");
   972     XCTAssertEqualObjects([session stringFromRating:PEP_rating_under_attack], @"under_attack");
   973     XCTAssertEqualObjects([session stringFromRating:PEP_rating_undefined], @"undefined");
   974     XCTAssertEqualObjects([session stringFromRating:500], @"undefined");
   975 }
   976 
   977 - (void)testIsPEPUser
   978 {
   979     PEPSession *session = [PEPSession new];
   980 
   981     PEPIdentity *identMe = [[PEPIdentity alloc]
   982                             initWithAddress:@"me-myself-and-i@pep-project.org"
   983                             userID:@"me-myself-and-i"
   984                             userName:@"pEp Me"
   985                             isOwn:YES];
   986     NSError *error = nil;
   987     XCTAssertTrue([session mySelf:identMe error:&error]);
   988     XCTAssertNil(error);
   989 
   990     XCTAssertNotNil(identMe.fingerPrint);
   991 
   992     // PEP_CANNOT_FIND_PERSON == 902
   993     XCTAssertTrue([session isPEPUser:identMe error:&error]);
   994 }
   995 
   996 - (void)testXEncStatusForOutgoingEncryptedMail
   997 {
   998     [self helperXEncStatusForOutgoingEncryptdMailToSelf:NO expectedRating:PEP_rating_reliable];
   999 }
  1000 
  1001 - (void)testXEncStatusForOutgoingSelfEncryptedMail
  1002 {
  1003     [self helperXEncStatusForOutgoingEncryptdMailToSelf:YES
  1004                                          expectedRating:PEP_rating_trusted_and_anonymized];
  1005 }
  1006 
  1007 - (void)testEncryptMessagesWithoutKeys
  1008 {
  1009     PEPSession *session = [PEPSession new];
  1010 
  1011     PEPIdentity *identMe = [[PEPIdentity alloc]
  1012                             initWithAddress:@"me-myself-and-i@pep-project.org"
  1013                             userID:@"me-myself-and-i"
  1014                             userName:@"pEp Me"
  1015                             isOwn:YES];
  1016     NSError *error = nil;
  1017     XCTAssertTrue([session mySelf:identMe error:&error]);
  1018     XCTAssertNil(error);
  1019 
  1020     XCTAssertNotNil(identMe.fingerPrint);
  1021 
  1022     PEPIdentity *identAlice = [[PEPIdentity alloc]
  1023                                initWithAddress:@"alice@pep-project.org"
  1024                                userID:@"alice"
  1025                                userName:@"pEp Test Alice"
  1026                                isOwn:NO];
  1027 
  1028     PEPMessage *msg = [PEPMessage new];
  1029     msg.from = identMe;
  1030     msg.to = @[identAlice];
  1031     msg.shortMessage = @"Mail to Alice";
  1032     msg.longMessage = @"Alice?";
  1033     msg.direction = PEP_dir_outgoing;
  1034 
  1035     PEP_rating rating;
  1036     XCTAssertTrue([session outgoingRating:&rating forMessage:msg error:&error]);
  1037     XCTAssertEqual(rating, PEP_rating_unencrypted);
  1038 
  1039     PEPMessage *encMsg = [session encryptMessage:msg extraKeys:nil status:nil error:&error];
  1040     XCTAssertNotNil(encMsg);
  1041     XCTAssertNil(error);
  1042 
  1043     XCTAssertNotNil(encMsg);
  1044 
  1045     PEPStringList *keys;
  1046     PEP_rating pEpRating;
  1047     error = nil;
  1048     PEPMessage *decMsg = [session
  1049                           decryptMessage:encMsg
  1050                           rating:&pEpRating
  1051                           extraKeys:&keys
  1052                           status:nil
  1053                           error:&error];
  1054     XCTAssertNotNil(decMsg);
  1055     XCTAssertNil(error);
  1056 
  1057     XCTAssertEqual(pEpRating, PEP_rating_unencrypted);
  1058     XCTAssertNotNil(decMsg);
  1059 }
  1060 
  1061 /**
  1062  ENGINE-364. Tries to invoke trustPersonalKey on an identity without key,
  1063  giving it a fake fingerprint.
  1064  */
  1065 - (void)testTrustPersonalKey
  1066 {
  1067     PEPSession *session = [PEPSession new];
  1068 
  1069     PEPIdentity *identMe = [[PEPIdentity alloc]
  1070                             initWithAddress:@"me-myself-and-i@pep-project.org"
  1071                             userID:@"me-myself-and-i"
  1072                             userName:@"pEp Me"
  1073                             isOwn:YES];
  1074     NSError *error = nil;
  1075     XCTAssertTrue([session mySelf:identMe error:&error]);
  1076     XCTAssertNil(error);
  1077 
  1078     XCTAssertNotNil(identMe.fingerPrint);
  1079 
  1080     // The fingerprint is definitely wrong, we don't have a key
  1081     PEPIdentity *identAlice = [[PEPIdentity alloc]
  1082                                initWithAddress:@"alice@pep-project.org"
  1083                                userID:@"alice"
  1084                                userName:@"pEp Test Alice"
  1085                                isOwn:NO
  1086                                fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
  1087 
  1088     XCTAssertFalse([session trustPersonalKey:identAlice error:&error]);
  1089     XCTAssertNotNil(error);
  1090 }
  1091 
  1092 /**
  1093  ENGINE-381
  1094  */
  1095 - (void)testVolatileIdentityRating
  1096 {
  1097     PEPSession *session = [PEPSession new];
  1098 
  1099     PEPIdentity *identMe = [[PEPIdentity alloc]
  1100                             initWithAddress:@"me-myself-and-i@pep-project.org"
  1101                             userID:@"me-myself-and-i"
  1102                             userName:@"pEp Me"
  1103                             isOwn:YES];
  1104     NSError *error = nil;
  1105     XCTAssertTrue([session mySelf:identMe error:&error]);
  1106     XCTAssertNil(error);
  1107 
  1108     XCTAssertNotNil(identMe.fingerPrint);
  1109 
  1110     PEPIdentity *identAlice = [self
  1111                                checkImportingKeyFilePath:@"6FF00E97_sec.asc"
  1112                                address:@"pep.test.alice@pep-project.org"
  1113                                userID:@"alice_user_id"
  1114                                fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
  1115                                session: session];
  1116     XCTAssertNotNil(identAlice);
  1117 
  1118     dispatch_group_t identityRatingGroup = dispatch_group_create();
  1119 
  1120     void (^ratingBlock)(void) = ^{
  1121         PEPSession *innerSession = [PEPSession new];
  1122         PEP_rating rating = [self ratingForIdentity:identAlice session:innerSession];
  1123         XCTAssertEqual(rating, PEP_rating_reliable);
  1124     };
  1125 
  1126     for (int i = 0; i < 4; ++i) {
  1127         dispatch_group_async(identityRatingGroup,
  1128                              dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0),
  1129                              ratingBlock);
  1130     }
  1131 
  1132     for (int i = 0; i < 4; ++i) {
  1133         ratingBlock();
  1134     }
  1135 
  1136     dispatch_group_wait(identityRatingGroup, DISPATCH_TIME_FOREVER);
  1137 }
  1138 
  1139 #pragma mark - configUnencryptedSubject
  1140 
  1141 - (void)testConfigUnencryptedSubject
  1142 {
  1143     // Setup Config to encrypt subject
  1144     [PEPObjCAdapter setUnecryptedSubjectEnabled:NO];
  1145 
  1146     // Write mail to yourself ...
  1147     PEPMessage *encMessage = [self mailWrittenToMySelf];
  1148 
  1149     // ... and assert subject is encrypted
  1150     XCTAssertEqualObjects(encMessage.shortMessage, @"p≡p", @"Subject should be encrypted");
  1151 }
  1152 
  1153 - (void)testConfigUnencryptedSubject_encryptedSubjectDisabled
  1154 {
  1155     // Setup Config to not encrypt subject
  1156     [PEPObjCAdapter setUnecryptedSubjectEnabled:YES];
  1157 
  1158     // Write mail to yourself ...
  1159     PEPMessage *encMessage = [self mailWrittenToMySelf];
  1160 
  1161     // ... and assert the subject is not encrypted
  1162     XCTAssertNotEqualObjects(encMessage.shortMessage, @"p≡p", @"Subject should not be encrypted");
  1163 }
  1164 
  1165 #pragma mark - Helpers
  1166 
  1167 /**
  1168  Determines the rating for the given identity.
  1169  @return PEP_rating_undefined on error
  1170  */
  1171 - (PEP_rating)ratingForIdentity:(PEPIdentity *)identity session:(PEPSession *)session
  1172 {
  1173     NSError *error;
  1174     PEP_rating rating;
  1175     XCTAssertTrue([session rating:&rating forIdentity:identity error:&error]);
  1176     XCTAssertNil(error);
  1177     return rating;
  1178 }
  1179 
  1180 - (PEPIdentity *)checkImportingKeyFilePath:(NSString *)filePath address:(NSString *)address
  1181                                     userID:(NSString *)userID
  1182                                fingerPrint:(NSString *)fingerPrint
  1183                                    session:(PEPSession *)session
  1184 {
  1185     if (!session) {
  1186         session = [PEPSession new];
  1187     }
  1188 
  1189     BOOL success = [PEPTestUtils importBundledKey:filePath session:session];
  1190     XCTAssertTrue(success);
  1191 
  1192     if (success) {
  1193         // Our test user:
  1194         PEPIdentity *identTest = [[PEPIdentity alloc]
  1195                                   initWithAddress:address
  1196                                   userID:userID
  1197                                   userName:[NSString stringWithFormat:@"Some User Name %@", userID]
  1198                                   isOwn:NO];
  1199 
  1200         NSError *error = nil;
  1201         XCTAssertTrue([session updateIdentity:identTest error:&error]);
  1202         XCTAssertNil(error);
  1203         XCTAssertNotNil(identTest.fingerPrint);
  1204         XCTAssertEqualObjects(identTest.fingerPrint, fingerPrint);
  1205 
  1206         return identTest;
  1207     } else {
  1208         return nil;
  1209     }
  1210 }
  1211 
  1212 - (PEPIdentity *)checkMySelfImportingKeyFilePath:(NSString *)filePath address:(NSString *)address
  1213                                           userID:(NSString *)userID
  1214                                      fingerPrint:(NSString *)fingerPrint
  1215                                          session:(PEPSession *)session
  1216 {
  1217     XCTAssertTrue([PEPTestUtils importBundledKey:filePath session:session]);
  1218 
  1219     // Our test user:
  1220     PEPIdentity *identTest = [[PEPIdentity alloc]
  1221                               initWithAddress:address
  1222                               userID:userID
  1223                               userName:[NSString stringWithFormat:@"Some User Name %@", userID]
  1224                               isOwn:YES
  1225                               fingerPrint: fingerPrint];
  1226 
  1227     NSError *error;
  1228     XCTAssertTrue([session setOwnKey:identTest fingerprint:fingerPrint error:&error]);
  1229     XCTAssertNil(error);
  1230     XCTAssertNotNil(identTest.fingerPrint);
  1231     XCTAssertEqualObjects(identTest.fingerPrint, fingerPrint);
  1232 
  1233     return identTest;
  1234 }
  1235 
  1236 /**
  1237  Verifies that a partner ID is really a correct Identity.
  1238  Usually used on identities imported as keys, since the engine has problems with them.
  1239  */
  1240 - (void)updateAndVerifyPartnerIdentity:(PEPIdentity *)partnerIdentity session:(PEPSession *)session
  1241 {
  1242     NSError *error = nil;
  1243 
  1244     XCTAssertNotNil(partnerIdentity.fingerPrint);
  1245     XCTAssertTrue([session updateIdentity:partnerIdentity error:&error]);
  1246     XCTAssertNil(error);
  1247     XCTAssertNotNil(partnerIdentity.fingerPrint);
  1248     NSString *fingerprint = partnerIdentity.fingerPrint;
  1249     partnerIdentity.fingerPrint = nil;
  1250     XCTAssertTrue([session updateIdentity:partnerIdentity error:&error]);
  1251     XCTAssertNil(error);
  1252     XCTAssertNotNil(partnerIdentity.fingerPrint);
  1253     XCTAssertEqualObjects(partnerIdentity.fingerPrint, fingerprint);
  1254 }
  1255 
  1256 - (PEPMessage *)mailWrittenToMySelf
  1257 {
  1258     PEPSession *session = [PEPSession new];
  1259 
  1260     // Write a e-mail to yourself ...
  1261     PEPIdentity *me = [PEPTestUtils ownPepIdentityWithAddress:@"me@peptest.ch"
  1262                                                      userName:@"userName"];
  1263     NSError *error = nil;
  1264     XCTAssertTrue([session mySelf:me error:&error]);
  1265     XCTAssertNil(error);
  1266 
  1267     NSString *shortMessage = @"Subject";
  1268     NSString *longMessage = @"Oh, this is a long body text!";
  1269     PEPMessage *mail = [PEPTestUtils mailFrom:me
  1270                                       toIdent:me
  1271                                  shortMessage:shortMessage
  1272                                   longMessage:longMessage
  1273                                      outgoing:YES];
  1274     PEP_STATUS status = PEP_UNKNOWN_ERROR;
  1275     PEPMessage *encMessage = [session
  1276                               encryptMessage:mail
  1277                               forSelf:me
  1278                               extraKeys:nil
  1279                               status:&status
  1280                               error:&error];
  1281     XCTAssertNil(error);
  1282 
  1283     return encMessage;
  1284 }
  1285 
  1286 - (PEPMessage *)internalEncryptToMySelfKeys:(PEPStringList **)keys
  1287 {
  1288     PEPSession *session = [PEPSession new];
  1289     
  1290     PEPIdentity *me = [PEPTestUtils ownPepIdentityWithAddress:@"me@peptest.ch"
  1291                                                      userName:@"userName"];
  1292     NSError *error = nil;
  1293     XCTAssertTrue([session mySelf:me error:&error]);
  1294     XCTAssertNil(error);
  1295 
  1296     XCTAssertNotNil(me.fingerPrint);
  1297 
  1298     // Create draft
  1299     NSString *shortMessage = @"Subject";
  1300     NSString *longMessage = @"Oh, this is a long body text!";
  1301     PEPMessage *mail = [PEPTestUtils mailFrom:me toIdent:me shortMessage:shortMessage longMessage:longMessage outgoing:YES];
  1302 
  1303     PEP_STATUS status;
  1304     PEPMessage *encMessage = [session
  1305                               encryptMessage:mail
  1306                               forSelf:me
  1307                               extraKeys:nil
  1308                               status:&status
  1309                               error:&error];
  1310     XCTAssertEqual(status, 0);
  1311     XCTAssertEqualObjects(encMessage.shortMessage, @"p≡p");
  1312 
  1313     PEP_rating rating;
  1314     error = nil;
  1315     PEPMessage *unencDict = [session
  1316                              decryptMessage:encMessage
  1317                              rating:&rating
  1318                              extraKeys:keys
  1319                              status:nil
  1320                              error:&error];
  1321     XCTAssertNotNil(unencDict);
  1322     XCTAssertNil(error);
  1323 
  1324     XCTAssertGreaterThanOrEqual(rating, PEP_rating_reliable);
  1325 
  1326     XCTAssertEqualObjects(unencDict.shortMessage, shortMessage);
  1327     XCTAssertEqualObjects(unencDict.longMessage, longMessage);
  1328 
  1329     return unencDict;
  1330 }
  1331 
  1332 - (void)pEpCleanUp
  1333 {
  1334     [PEPTestUtils cleanUp];
  1335 }
  1336 
  1337 - (void)helperXEncStatusForOutgoingEncryptdMailToSelf:(BOOL)toSelf
  1338                                        expectedRating:(PEP_rating)expectedRating
  1339 {
  1340     PEPSession *session = [PEPSession new];
  1341 
  1342     // Partner pubkey for the test:
  1343     // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
  1344     // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
  1345     XCTAssertTrue([PEPTestUtils importBundledKey:@"0x6FF00E97.asc" session:session]);
  1346 
  1347     PEPIdentity *identAlice = [[PEPIdentity alloc]
  1348                                initWithAddress:@"pep.test.alice@pep-project.org"
  1349                                userID:ownUserId
  1350                                userName:@"pEp Test Alice"
  1351                                isOwn:NO
  1352                                fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
  1353     [self updateAndVerifyPartnerIdentity:identAlice session:session];
  1354 
  1355     PEPIdentity *identMe = [[PEPIdentity alloc]
  1356                                initWithAddress:@"me-myself-and-i@pep-project.org"
  1357                                userID:@"me-myself-and-i"
  1358                                userName:@"pEp Me"
  1359                                isOwn:YES];
  1360     NSError *error = nil;
  1361     XCTAssertTrue([session mySelf:identMe error:&error]);
  1362     XCTAssertNil(error);
  1363 
  1364     XCTAssertNotNil(identMe.fingerPrint);
  1365 
  1366     PEPMessage *msg = [PEPMessage new];
  1367     msg.from = identMe;
  1368     msg.to = @[identAlice];
  1369     msg.shortMessage = @"Mail to Alice";
  1370     msg.longMessage = @"Alice?";
  1371     msg.direction = PEP_dir_outgoing;
  1372 
  1373     PEP_rating rating;
  1374     XCTAssertTrue([session outgoingRating:&rating forMessage:msg error:&error]);
  1375     XCTAssertEqual(rating, PEP_rating_reliable);
  1376 
  1377     PEPMessage *encMsg;
  1378 
  1379     PEP_STATUS statusEnc = PEP_VERSION_MISMATCH;
  1380     if (toSelf) {
  1381         encMsg = [session
  1382                   encryptMessage:msg
  1383                   forSelf:identMe
  1384                   extraKeys:nil
  1385                   status:&statusEnc
  1386                   error:&error];
  1387         XCTAssertEqual(statusEnc, PEP_STATUS_OK);
  1388     } else {
  1389         encMsg = [session encryptMessage:msg extraKeys:nil status:nil error:&error];
  1390         XCTAssertNotNil(encMsg);
  1391         XCTAssertNil(error);
  1392     }
  1393     XCTAssertNotNil(encMsg);
  1394 
  1395     PEPStringList *keys;
  1396     PEP_rating pEpRating;
  1397     error = nil;
  1398     PEPMessage *decMsg = [session
  1399                           decryptMessage:encMsg
  1400                           rating:&pEpRating
  1401                           extraKeys:&keys
  1402                           status:nil
  1403                           error:&error];
  1404     XCTAssertNil(error);
  1405     XCTAssertNotNil(decMsg);
  1406 
  1407     XCTAssertEqual(pEpRating, expectedRating);
  1408 
  1409     NSArray * encStatusField = nil;
  1410     for (NSArray *field in decMsg.optionalFields) {
  1411         NSString *header = [field[0] lowercaseString];
  1412         if ([header isEqualToString:@"x-encstatus"]) {
  1413             encStatusField = field;
  1414         }
  1415     }
  1416     XCTAssertNotNil(encStatusField);
  1417     if (encStatusField) {
  1418         PEP_rating outgoingRating = [session ratingFromString:encStatusField[1]];
  1419         XCTAssertEqual(outgoingRating, expectedRating);
  1420     }
  1421 }
  1422 
  1423 @end