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