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