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