pEpObjCTests/PEPSessionTest.m
author Dirk Zimmermann <dirk@pep-project.org>
Mon, 19 Feb 2018 10:30:59 +0100
changeset 470 3d39b8fc939d
parent 465 c119e90af091
child 471 b19faa4c131f
permissions -rw-r--r--
IOSAD-78 rm test code that manipulates DB files during tests
     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)testRevoke
   568 {
   569     PEPSession *session = [PEPSession new];
   570 
   571     // Our test user :
   572     // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
   573     // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
   574     [PEPTestUtils importBundledKey:@"6FF00E97_sec.asc"];
   575     NSString *fpr = @"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97";
   576 
   577     PEPIdentity *identAlice = [[PEPIdentity alloc]
   578                                initWithAddress:@"pep.test.alice@pep-project.org"
   579                                userID:ownUserId
   580                                userName:@"pEp Test Alice"
   581                                isOwn:YES
   582                                fingerPrint:fpr];
   583 
   584     [session mySelf:identAlice];
   585 
   586     PEPIdentity *identAlice2 = [identAlice mutableCopy];
   587 
   588     // This will revoke key
   589     [session keyMistrusted:identAlice2];
   590     identAlice2.fingerPrint = nil;
   591     [session mySelf:identAlice];
   592 
   593     // Check fingerprint is different
   594     XCTAssertNotEqualObjects(identAlice2.fingerPrint, fpr);
   595 }
   596 
   597 - (void)testMailToMyself
   598 {
   599     PEPSession *session = [PEPSession new];
   600 
   601     // Our test user :
   602     // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
   603     // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
   604     [PEPTestUtils importBundledKey:@"6FF00E97_sec.asc"];
   605 
   606     PEPIdentity *identAlice = [[PEPIdentity alloc]
   607                                initWithAddress:@"pep.test.alice@pep-project.org"
   608                                userID:ownUserId
   609                                userName:@"pEp Test Alice"
   610                                isOwn:YES
   611                                fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
   612 
   613     [session mySelf:identAlice];
   614 
   615     PEPMessage *msg = [PEPMessage new];
   616     msg.from = identAlice;
   617     msg.to = @[identAlice];
   618     msg.shortMessage = @"Mail to Myself";
   619     msg.longMessage = @"This is a text content";
   620     msg.direction = PEP_dir_outgoing;
   621 
   622     PEP_rating clr = [session outgoingColorForMessage:msg];
   623     XCTAssertEqual(clr, PEP_rating_trusted_and_anonymized);
   624 
   625     PEPMessage *encmsg;
   626     PEP_STATUS status = [session encryptMessage:msg extra:@[] dest:&encmsg];
   627 
   628     XCTAssertEqual(status, PEP_STATUS_OK);
   629 
   630     NSArray* keys;
   631     PEPMessage *decmsg;
   632 
   633     clr = [session decryptMessage:encmsg dest:&decmsg keys:&keys];
   634     XCTAssertEqual(clr, PEP_rating_trusted_and_anonymized);
   635 }
   636 
   637 - (void)testEncryptedMailFromMutt
   638 {
   639     PEPSession *session = [PEPSession new];
   640 
   641     // This is the public key for test001@peptest.ch
   642     [PEPTestUtils importBundledKey:@"A3FC7F0A.asc"];
   643 
   644     // This is the secret key for test001@peptest.ch
   645     [PEPTestUtils importBundledKey:@"A3FC7F0A_sec.asc"];
   646 
   647     // Mail from mutt, already processed into message dict by the app.
   648     NSMutableDictionary *msgDict = [[PEPTestUtils
   649                                      unarchiveDictionary:@"msg_to_A3FC7F0A_from_mutt.ser"]
   650                                     mutableCopy];
   651     [msgDict removeObjectForKey:kPepLongMessage];
   652     [msgDict removeObjectForKey:kPepLongMessageFormatted];
   653 
   654     // Also extracted "live" from the app.
   655     NSMutableDictionary *accountDict = [[PEPTestUtils
   656                                          unarchiveDictionary:@"account_A3FC7F0A.ser"]
   657                                         mutableCopy];
   658     [accountDict removeObjectForKey:kPepCommType];
   659     [accountDict removeObjectForKey:kPepFingerprint];
   660     PEPIdentity *identMe = [[PEPIdentity alloc] initWithDictionary:accountDict];
   661 
   662     [session mySelf:identMe];
   663     XCTAssertNotNil(identMe.fingerPrint);
   664 
   665     NSArray* keys;
   666     PEPMessage *msg = [PEPMessage new];
   667     [msg setValuesForKeysWithDictionary:msgDict];
   668     PEPMessage *pepDecryptedMail;
   669     PEP_rating rating = [session decryptMessage:msg dest:&pepDecryptedMail keys:&keys];
   670     XCTAssertNotNil(pepDecryptedMail.longMessage);
   671 }
   672 
   673 - (void)testOutgoingContactColor
   674 {
   675     PEPSession *session = [PEPSession new];
   676 
   677     PEPIdentity *partner1Orig = [PEPTestUtils foreignPepIdentityWithAddress:@"partner1@dontcare.me"
   678                                                                    userName:@"Partner 1"];
   679     NSString *pubKeyPartner1 = [PEPTestUtils loadResourceByName:@"partner1_F2D281C2789DD7F6_pub.asc"];
   680     XCTAssertNotNil(pubKeyPartner1);
   681     [session importKey:pubKeyPartner1];
   682 
   683     PEP_rating color = [session identityRating:partner1Orig];
   684     XCTAssertEqual(color, PEP_rating_reliable);
   685 }
   686 
   687 - (void)testMessageTrustwordsWithMySelf
   688 {
   689     PEPSession *session = [PEPSession new];
   690 
   691     PEPStringList *keys = nil;
   692     PEPMessage *decryptedDict = [self internalEncryptToMySelfKeys:&keys];
   693     XCTAssertNotNil(keys);
   694     XCTAssert(keys.count > 0);
   695 
   696     PEPIdentity *receiver = decryptedDict.to[0];
   697     [session updateIdentity:receiver];
   698     XCTAssertNotNil(receiver);
   699     PEP_STATUS trustwordsStatus;
   700 
   701     NSString *trustwords = [session getTrustwordsForMessage:decryptedDict
   702                                                    receiver:receiver
   703                                                   keysArray:keys language:@"en"
   704                                                        full:YES
   705                                             resultingStatus: &trustwordsStatus];
   706     // No trustwords with yourself
   707     XCTAssertEqual(trustwordsStatus, PEP_TRUSTWORDS_DUPLICATE_FPR);
   708     XCTAssertNil(trustwords);
   709 }
   710 
   711 - (void)testGetTrustwords
   712 {
   713     PEPSession *session = [PEPSession new];
   714 
   715     PEPIdentity *partner1Orig = [[PEPIdentity alloc]
   716                                  initWithAddress:@"partner1@dontcare.me" userID:@"partner1"
   717                                  userName:@"partner1"
   718                                  isOwn:NO fingerPrint:@"F0CD3F7B422E5D587ABD885BF2D281C2789DD7F6"];
   719 
   720     PEPIdentity *meOrig = [[PEPIdentity alloc]
   721                            initWithAddress:@"me@dontcare.me" userID:@"me"
   722                            userName:@"me"
   723                            isOwn:NO fingerPrint:@"CC1F73F6FB774BF08B197691E3BFBCA9248FC681"];
   724 
   725     NSString *pubKeyPartner1 = [PEPTestUtils loadResourceByName:@"partner1_F2D281C2789DD7F6_pub.asc"];
   726     XCTAssertNotNil(pubKeyPartner1);
   727     NSString *pubKeyMe = [PEPTestUtils loadResourceByName:@"meATdontcare_E3BFBCA9248FC681_pub.asc"];
   728     XCTAssertNotNil(pubKeyMe);
   729     NSString *secKeyMe = [PEPTestUtils loadResourceByName:@"meATdontcare_E3BFBCA9248FC681_sec.asc"];
   730     XCTAssertNotNil(secKeyMe);
   731 
   732     NSString *trustwordsFull = [session getTrustwordsIdentity1:meOrig identity2:partner1Orig
   733                                                       language:nil full:YES];
   734     XCTAssertEqualObjects(trustwordsFull,
   735                           @"EMERSON GASPER TOKENISM BOLUS COLLAGE DESPISE BEDDED ENCRYPTION IMAGINE BEDFORD");
   736 
   737     NSString *trustwordsFullEnglish = [session getTrustwordsIdentity1:meOrig identity2:partner1Orig
   738                                                              language:@"en" full:YES];
   739     XCTAssertEqualObjects(trustwordsFullEnglish, trustwordsFull);
   740 
   741     NSString *trustwordsUndefined = [session getTrustwordsIdentity1:meOrig identity2:partner1Orig
   742                                                            language:@"ZZ" full:YES];
   743     XCTAssertNil(trustwordsUndefined);
   744 }
   745 
   746 - (void)testStringToRating
   747 {
   748     PEPSession *session = [PEPSession new];
   749     XCTAssertEqual([session ratingFromString:@"cannot_decrypt"], PEP_rating_cannot_decrypt);
   750     XCTAssertEqual([session ratingFromString:@"have_no_key"], PEP_rating_have_no_key);
   751     XCTAssertEqual([session ratingFromString:@"unencrypted"], PEP_rating_unencrypted);
   752     XCTAssertEqual([session ratingFromString:@"unencrypted_for_some"],
   753                    PEP_rating_unencrypted_for_some);
   754     XCTAssertEqual([session ratingFromString:@"unreliable"], PEP_rating_unreliable);
   755     XCTAssertEqual([session ratingFromString:@"reliable"], PEP_rating_reliable);
   756     XCTAssertEqual([session ratingFromString:@"trusted"], PEP_rating_trusted);
   757     XCTAssertEqual([session ratingFromString:@"trusted_and_anonymized"],
   758                    PEP_rating_trusted_and_anonymized);
   759     XCTAssertEqual([session ratingFromString:@"fully_anonymous"], PEP_rating_fully_anonymous);
   760     XCTAssertEqual([session ratingFromString:@"mistrust"], PEP_rating_mistrust);
   761     XCTAssertEqual([session ratingFromString:@"b0rken"], PEP_rating_b0rken);
   762     XCTAssertEqual([session ratingFromString:@"under_attack"], PEP_rating_under_attack);
   763     XCTAssertEqual([session ratingFromString:@"undefined"], PEP_rating_undefined);
   764     XCTAssertEqual([session ratingFromString:@"does not exist111"], PEP_rating_undefined);
   765 }
   766 
   767 - (void)testRatingToString
   768 {
   769     PEPSession *session = [PEPSession new];
   770     XCTAssertEqualObjects([session stringFromRating:PEP_rating_cannot_decrypt], @"cannot_decrypt");
   771     XCTAssertEqualObjects([session stringFromRating:PEP_rating_have_no_key], @"have_no_key");
   772     XCTAssertEqualObjects([session stringFromRating:PEP_rating_unencrypted], @"unencrypted");
   773     XCTAssertEqualObjects([session stringFromRating:PEP_rating_unencrypted_for_some],
   774                           @"unencrypted_for_some");
   775     XCTAssertEqualObjects([session stringFromRating:PEP_rating_unreliable], @"unreliable");
   776     XCTAssertEqualObjects([session stringFromRating:PEP_rating_reliable], @"reliable");
   777     XCTAssertEqualObjects([session stringFromRating:PEP_rating_trusted], @"trusted");
   778     XCTAssertEqualObjects([session stringFromRating:PEP_rating_trusted_and_anonymized],
   779                           @"trusted_and_anonymized");
   780     XCTAssertEqualObjects([session stringFromRating:PEP_rating_fully_anonymous],
   781                           @"fully_anonymous");
   782     XCTAssertEqualObjects([session stringFromRating:PEP_rating_mistrust], @"mistrust");
   783     XCTAssertEqualObjects([session stringFromRating:PEP_rating_b0rken], @"b0rken");
   784     XCTAssertEqualObjects([session stringFromRating:PEP_rating_under_attack], @"under_attack");
   785     XCTAssertEqualObjects([session stringFromRating:PEP_rating_undefined], @"undefined");
   786     XCTAssertEqualObjects([session stringFromRating:500], @"undefined");
   787 }
   788 
   789 - (void)testIsPEPUser
   790 {
   791     PEPSession *session = [PEPSession new];
   792 
   793     PEPIdentity *identMe = [[PEPIdentity alloc]
   794                             initWithAddress:@"me-myself-and-i@pep-project.org"
   795                             userID:@"me-myself-and-i"
   796                             userName:@"pEp Me"
   797                             isOwn:YES];
   798     [session mySelf:identMe];
   799     XCTAssertNotNil(identMe.fingerPrint);
   800 
   801     // PEP_CANNOT_FIND_PERSON == 902
   802     XCTAssertTrue([session isPEPUser:identMe]);
   803 }
   804 
   805 - (void)testXEncStatusForOutgoingEncryptedMail
   806 {
   807     [self helperXEncStatusForOutgoingEncryptdMailToSelf:NO expectedRating:PEP_rating_reliable];
   808 }
   809 
   810 - (void)testXEncStatusForOutgoingSelfEncryptedMail
   811 {
   812     [self helperXEncStatusForOutgoingEncryptdMailToSelf:YES
   813                                          expectedRating:PEP_rating_trusted_and_anonymized];
   814 }
   815 
   816 - (void)testEncryptMessagesWithoutKeys
   817 {
   818     PEPSession *session = [PEPSession new];
   819 
   820     PEPIdentity *identMe = [[PEPIdentity alloc]
   821                             initWithAddress:@"me-myself-and-i@pep-project.org"
   822                             userID:@"me-myself-and-i"
   823                             userName:@"pEp Me"
   824                             isOwn:YES];
   825     [session mySelf:identMe];
   826     XCTAssertNotNil(identMe.fingerPrint);
   827 
   828     PEPIdentity *identAlice = [[PEPIdentity alloc]
   829                                initWithAddress:@"alice@pep-project.org"
   830                                userID:@"alice"
   831                                userName:@"pEp Test Alice"
   832                                isOwn:NO];
   833 
   834     PEPMessage *msg = [PEPMessage new];
   835     msg.from = identMe;
   836     msg.to = @[identAlice];
   837     msg.shortMessage = @"Mail to Alice";
   838     msg.longMessage = @"Alice?";
   839     msg.direction = PEP_dir_outgoing;
   840 
   841     PEP_rating clr = [session outgoingColorForMessage:msg];
   842     XCTAssertEqual(clr, PEP_rating_unencrypted);
   843 
   844     PEPMessage *encMsg;
   845 
   846     PEP_STATUS statusEnc = statusEnc = [session encryptMessage:msg extra:@[] dest:&encMsg];
   847 
   848     XCTAssertEqual(statusEnc, PEP_UNENCRYPTED);
   849 
   850     XCTAssertNotNil(encMsg);
   851 
   852     PEPMessage *decMsg;
   853     PEPStringList *keys;
   854     PEP_rating pEpRating = [session decryptMessage:encMsg dest:&decMsg keys:&keys];
   855     XCTAssertEqual(pEpRating, PEP_rating_unencrypted);
   856     XCTAssertNotNil(decMsg);
   857 }
   858 
   859 /**
   860  ENGINE-364. Tries to invoke trustPersonalKey on an identity without key,
   861  giving it a fake fingerprint.
   862  */
   863 - (void)testTrustPersonalKey
   864 {
   865     PEPSession *session = [PEPSession new];
   866 
   867     PEPIdentity *identMe = [[PEPIdentity alloc]
   868                             initWithAddress:@"me-myself-and-i@pep-project.org"
   869                             userID:@"me-myself-and-i"
   870                             userName:@"pEp Me"
   871                             isOwn:YES];
   872     [session mySelf:identMe];
   873     XCTAssertNotNil(identMe.fingerPrint);
   874 
   875     // The fingerprint is definitely wrong, we don't have a key
   876     PEPIdentity *identAlice = [[PEPIdentity alloc]
   877                                initWithAddress:@"alice@pep-project.org"
   878                                userID:@"alice"
   879                                userName:@"pEp Test Alice"
   880                                isOwn:NO
   881                                fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
   882 
   883     [session trustPersonalKey:identAlice];
   884 }
   885 
   886 /**
   887  ENGINE-381
   888  */
   889 - (void)testVolatileIdentityRating
   890 {
   891     PEPSession *session = [PEPSession new];
   892 
   893     PEPIdentity *identMe = [[PEPIdentity alloc]
   894                             initWithAddress:@"me-myself-and-i@pep-project.org"
   895                             userID:@"me-myself-and-i"
   896                             userName:@"pEp Me"
   897                             isOwn:YES];
   898     [session mySelf:identMe];
   899     XCTAssertNotNil(identMe.fingerPrint);
   900 
   901     PEPIdentity *identAlice = [self
   902                                checkImportingKeyFilePath:@"6FF00E97_sec.asc"
   903                                address:@"pep.test.alice@pep-project.org"
   904                                userID:@"alice_user_id"
   905                                fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
   906                                session: session];
   907 
   908     dispatch_group_t identityRatingGroup = dispatch_group_create();
   909 
   910     void (^ratingBlock)(void) = ^{
   911         PEPSession *innerSession = [PEPSession new];
   912         PEP_rating rating = [innerSession identityRating:identAlice];
   913         XCTAssertEqual(rating, PEP_rating_reliable);
   914     };
   915 
   916     for (int i = 0; i < 2; ++i) {
   917         dispatch_group_async(identityRatingGroup,
   918                              dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0),
   919                              ratingBlock);
   920     }
   921 
   922     for (int i = 0; i < 2; ++i) {
   923         ratingBlock();
   924     }
   925 
   926     dispatch_group_wait(identityRatingGroup, DISPATCH_TIME_FOREVER);
   927 }
   928 
   929 #pragma mark - configUnencryptedSubject
   930 
   931 - (void)testConfigUnencryptedSubject
   932 {
   933     // Setup Config to encrypt subject
   934     [PEPObjCAdapter setUnecryptedSubjectEnabled:NO];
   935 
   936     // Write mail to yourself ...
   937     PEPMessage *encMessage = [self mailWrittenToMySelf];
   938 
   939     // ... and assert subject is encrypted
   940     XCTAssertEqualObjects(encMessage.shortMessage, @"p≡p", @"Subject should be encrypted");
   941 }
   942 
   943 - (void)testConfigUnencryptedSubject_encryptedSubjectDisabled
   944 {
   945     // Setup Config to not encrypt subject
   946     [PEPObjCAdapter setUnecryptedSubjectEnabled:YES];
   947 
   948     // Write mail to yourself ...
   949     PEPMessage *encMessage = [self mailWrittenToMySelf];
   950 
   951     // ... and assert the subject is not encrypted
   952     XCTAssertNotEqualObjects(encMessage.shortMessage, @"p≡p", @"Subject should not be encrypted");
   953 }
   954 
   955 #pragma mark - Helpers
   956 
   957 - (PEPIdentity *)checkImportingKeyFilePath:(NSString *)filePath address:(NSString *)address
   958                                     userID:(NSString *)userID
   959                                fingerPrint:(NSString *)fingerPrint
   960                                    session:(PEPSession *)session
   961 {
   962     if (!session) {
   963         session = [PEPSession new];
   964     }
   965 
   966     [PEPTestUtils importBundledKey:filePath];
   967 
   968     // Our test user:
   969     PEPIdentity *identTest = [[PEPIdentity alloc]
   970                               initWithAddress:address
   971                               userID:userID
   972                               userName:[NSString stringWithFormat:@"Some User Name %@", userID]
   973                               isOwn:NO];
   974 
   975     [session updateIdentity:identTest];
   976     XCTAssertNotNil(identTest.fingerPrint);
   977     XCTAssertEqualObjects(identTest.fingerPrint, fingerPrint);
   978 
   979     return identTest;
   980 }
   981 
   982 - (PEPIdentity *)checkMySelfImportingKeyFilePath:(NSString *)filePath address:(NSString *)address
   983                                           userID:(NSString *)userID
   984                                      fingerPrint:(NSString *)fingerPrint
   985 {
   986     PEPSession *session = [PEPSession new];
   987 
   988     [PEPTestUtils importBundledKey:filePath];
   989 
   990     // Our test user:
   991     PEPIdentity *identTest = [[PEPIdentity alloc]
   992                               initWithAddress:address
   993                               userID:userID
   994                               userName:[NSString stringWithFormat:@"Some User Name %@", userID]
   995                               isOwn:YES
   996                               fingerPrint: fingerPrint];
   997 
   998     [session mySelf:identTest];
   999     XCTAssertNotNil(identTest.fingerPrint);
  1000     XCTAssertEqualObjects(identTest.fingerPrint, fingerPrint);
  1001 
  1002     return identTest;
  1003 }
  1004 
  1005 /**
  1006  Verifies that a partner ID is really a correct Identity.
  1007  Usually used on identities imported as keys, since the engine has problems with them.
  1008  */
  1009 - (void)updateAndVerifyPartnerIdentity:(PEPIdentity *)partnerIdentity session:(PEPSession *)session
  1010 {
  1011     XCTAssertNotNil(partnerIdentity.fingerPrint);
  1012     [session updateIdentity:partnerIdentity];
  1013     XCTAssertNotNil(partnerIdentity.fingerPrint);
  1014     NSString *fingerprint = partnerIdentity.fingerPrint;
  1015     partnerIdentity.fingerPrint = nil;
  1016     [session updateIdentity:partnerIdentity];
  1017     XCTAssertNotNil(partnerIdentity.fingerPrint);
  1018     XCTAssertEqualObjects(partnerIdentity.fingerPrint, fingerprint);
  1019 }
  1020 
  1021 - (PEPMessage *)mailWrittenToMySelf
  1022 {
  1023     PEPSession *session = [PEPSession new];
  1024 
  1025     // Write a e-mail to yourself ...
  1026     PEPIdentity *me = [PEPTestUtils ownPepIdentityWithAddress:@"me@peptest.ch"
  1027                                                      userName:@"userName"];
  1028     [session mySelf:me];
  1029 
  1030     NSString *shortMessage = @"Subject";
  1031     NSString *longMessage = @"Oh, this is a long body text!";
  1032     PEPMessage *mail = [PEPTestUtils mailFrom:me
  1033                                       toIdent:me
  1034                                  shortMessage:shortMessage
  1035                                   longMessage:longMessage
  1036                                      outgoing:YES];
  1037     PEPMessage *encMessage;
  1038     [session encryptMessage:mail identity:me dest:&encMessage];
  1039 
  1040     return encMessage;
  1041 }
  1042 
  1043 - (PEPMessage *)internalEncryptToMySelfKeys:(PEPStringList **)keys
  1044 {
  1045     PEPSession *session = [PEPSession new];
  1046     
  1047     PEPIdentity *me = [PEPTestUtils ownPepIdentityWithAddress:@"me@peptest.ch"
  1048                                                      userName:@"userName"];
  1049     [session mySelf:me];
  1050     XCTAssertNotNil(me.fingerPrint);
  1051 
  1052     // Create draft
  1053     NSString *shortMessage = @"Subject";
  1054     NSString *longMessage = @"Oh, this is a long body text!";
  1055     PEPMessage *mail = [PEPTestUtils mailFrom:me toIdent:me shortMessage:shortMessage longMessage:longMessage outgoing:YES];
  1056 
  1057     PEPMessage *encMessage;
  1058     PEP_STATUS status = [session encryptMessage:mail identity:me dest:&encMessage];
  1059     XCTAssertEqual(status, 0);
  1060     XCTAssertEqualObjects(encMessage.shortMessage, @"p≡p");
  1061 
  1062     PEPMessage *unencDict;
  1063     PEP_rating rating = [session decryptMessage:encMessage dest:&unencDict keys:keys];
  1064     XCTAssertGreaterThanOrEqual(rating, PEP_rating_reliable);
  1065 
  1066     XCTAssertEqualObjects(unencDict.shortMessage, shortMessage);
  1067     XCTAssertEqualObjects(unencDict.longMessage, longMessage);
  1068 
  1069     return unencDict;
  1070 }
  1071 
  1072 - (void)pEpCleanUp
  1073 {
  1074     [PEPTestUtils cleanUp];
  1075 }
  1076 
  1077 - (void)pEpSetUp
  1078 {
  1079 }
  1080 
  1081 - (void)helperXEncStatusForOutgoingEncryptdMailToSelf:(BOOL)toSelf
  1082                                        expectedRating:(PEP_rating)expectedRating
  1083 {
  1084     PEPSession *session = [PEPSession new];
  1085 
  1086     // Partner pubkey for the test:
  1087     // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
  1088     // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
  1089     [PEPTestUtils importBundledKey:@"0x6FF00E97.asc"];
  1090 
  1091     PEPIdentity *identAlice = [[PEPIdentity alloc]
  1092                                initWithAddress:@"pep.test.alice@pep-project.org"
  1093                                userID:ownUserId
  1094                                userName:@"pEp Test Alice"
  1095                                isOwn:NO
  1096                                fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
  1097     [self updateAndVerifyPartnerIdentity:identAlice session:session];
  1098 
  1099     PEPIdentity *identMe = [[PEPIdentity alloc]
  1100                                initWithAddress:@"me-myself-and-i@pep-project.org"
  1101                                userID:@"me-myself-and-i"
  1102                                userName:@"pEp Me"
  1103                                isOwn:YES];
  1104     [session mySelf:identMe];
  1105     XCTAssertNotNil(identMe.fingerPrint);
  1106 
  1107     PEPMessage *msg = [PEPMessage new];
  1108     msg.from = identMe;
  1109     msg.to = @[identAlice];
  1110     msg.shortMessage = @"Mail to Alice";
  1111     msg.longMessage = @"Alice?";
  1112     msg.direction = PEP_dir_outgoing;
  1113 
  1114     PEP_rating clr = [session outgoingColorForMessage:msg];
  1115     XCTAssertEqual(clr, PEP_rating_reliable);
  1116 
  1117     PEPMessage *encMsg;
  1118 
  1119     PEP_STATUS statusEnc = PEP_VERSION_MISMATCH;
  1120     if (toSelf) {
  1121         statusEnc = [session encryptMessage:msg identity:identMe dest:&encMsg];
  1122     } else {
  1123          statusEnc = [session encryptMessage:msg extra:@[] dest:&encMsg];
  1124     }
  1125 
  1126     XCTAssertEqual(statusEnc, PEP_STATUS_OK);
  1127 
  1128     XCTAssertNotNil(encMsg);
  1129 
  1130     PEPMessage *decMsg;
  1131     PEPStringList *keys;
  1132     PEP_rating pEpRating = [session decryptMessage:encMsg dest:&decMsg keys:&keys];
  1133     XCTAssertEqual(pEpRating, expectedRating);
  1134     XCTAssertNotNil(decMsg);
  1135 
  1136     NSArray * encStatusField = nil;
  1137     for (NSArray *field in decMsg.optionalFields) {
  1138         NSString *header = [field[0] lowercaseString];
  1139         if ([header isEqualToString:@"x-encstatus"]) {
  1140             encStatusField = field;
  1141         }
  1142     }
  1143     XCTAssertNotNil(encStatusField);
  1144     if (encStatusField) {
  1145         PEP_rating outgoingRating = [session ratingFromString:encStatusField[1]];
  1146         XCTAssertEqual(outgoingRating, expectedRating);
  1147     }
  1148 }
  1149 
  1150 @end