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