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