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