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