pEpObjCTests/PEPSessionTest.m
author Dirk Zimmermann <dirk@pep-project.org>
Mon, 12 Mar 2018 12:08:38 +0100
changeset 522 534d277b410d
parent 517 510fa8a4ff7f
child 523 3d167bef8afa
permissions -rw-r--r--
IOSAD-72 encrypt (outgoing) error handling
     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     NSError *error = nil;
   468     PEPMessage *encMsg = [session encryptMessage:msg extraKeys:nil error:&error];
   469     XCTAssertNotNil(encMsg);
   470     XCTAssertNil(error);
   471 
   472     XCTAssertEqualObjects(encMsg.shortMessage, @"p≡p");
   473     XCTAssertTrue([encMsg.longMessage containsString:@"p≡p"]);
   474 }
   475 
   476 
   477 - (void)testOutgoingBccColors
   478 {
   479     PEPSession *session = [PEPSession new];
   480 
   481     // Our test user :
   482     // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
   483     // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
   484     XCTAssertTrue([PEPTestUtils importBundledKey:@"6FF00E97_sec.asc" session:session]);
   485 
   486     PEPIdentity *identAlice = [[PEPIdentity alloc]
   487                                initWithAddress:@"pep.test.alice@pep-project.org"
   488                                userID:ownUserId
   489                                userName:@"pEp Test Alice"
   490                                isOwn:YES
   491                                fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
   492 
   493     [session mySelf:identAlice];
   494 
   495     PEPMessage *msg = [PEPMessage new];
   496     msg.from = identAlice;
   497     msg.to = @[[[PEPIdentity alloc] initWithAddress:@"pep.test.bob@pep-project.org"
   498                                              userID:@"42" userName:@"pEp Test Bob" isOwn:NO]];
   499     msg.shortMessage = @"All Green Test";
   500     msg.longMessage = @"This is a text content";
   501     msg.direction = PEP_dir_outgoing;
   502 
   503     // Test with unknown Bob
   504     PEP_rating clr = [session outgoingColorForMessage:msg];
   505     XCTAssertEqual(clr, PEP_rating_unencrypted);
   506 
   507     // Now let see with bob's pubkey already known
   508     // pEp Test Bob (test key, don't use) <pep.test.bob@pep-project.org>
   509     // BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39
   510     XCTAssertTrue([PEPTestUtils importBundledKey:@"0xC9C2EE39.asc" session:session]);
   511 
   512     PEPIdentity *identBob = [[PEPIdentity alloc]
   513                              initWithAddress:@"pep.test.bob@pep-project.org"
   514                              userID:@"42" userName:@"pEp Test Bob"
   515                              isOwn:NO
   516                              fingerPrint:@"BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39"];
   517 
   518     [session updateIdentity:identBob];
   519 
   520     // Should be yellow, since no handshake happened.
   521     clr = [session outgoingColorForMessage:msg];
   522     XCTAssertEqual(clr, PEP_rating_reliable);
   523 
   524     clr = [session identityRating:identBob];
   525     XCTAssertEqual(clr, PEP_rating_reliable);
   526 
   527     // Let' say we got that handshake, set PEP_ct_confirmed in Bob's identity
   528     [session trustPersonalKey:identBob];
   529 
   530     // This time it should be green
   531     clr = [session outgoingColorForMessage:msg];
   532     XCTAssertEqual(clr, PEP_rating_trusted);
   533 
   534     clr = [session identityRating:identBob];
   535     XCTAssertEqual(clr, PEP_rating_trusted);
   536 
   537     // Now let see if it turns back yellow if we add an unconfirmed folk.
   538     // pEp Test John (test key, don't use) <pep.test.john@pep-project.org>
   539     // AA2E4BEB93E5FE33DEFD8BE1135CD6D170DCF575
   540     XCTAssertTrue([PEPTestUtils importBundledKey:@"0x70DCF575.asc" session:session]);
   541 
   542     PEPIdentity *identJohn = [[PEPIdentity alloc]
   543                               initWithAddress:@"pep.test.john@pep-project.org"
   544                               userID:@"101" userName:@"pEp Test John"
   545                               isOwn:NO
   546                               fingerPrint:@"AA2E4BEB93E5FE33DEFD8BE1135CD6D170DCF575"];
   547 
   548     [session updateIdentity:identJohn];
   549 
   550     msg.bcc = @[[[PEPIdentity alloc] initWithAddress:@"pep.test.john@pep-project.org"
   551                                               userID:@"101" userName:@"pEp Test John" isOwn:NO]];
   552 
   553     // Yellow ?
   554     clr = [session outgoingColorForMessage:msg];
   555     XCTAssertEqual(clr, PEP_rating_reliable);
   556 
   557     [session trustPersonalKey:identJohn];
   558 
   559     // This time it should be green
   560     clr = [session outgoingColorForMessage:msg];
   561     XCTAssertEqual(clr, PEP_rating_trusted);
   562 
   563     clr = [session identityRating:identJohn];
   564     XCTAssertEqual(clr, PEP_rating_trusted);
   565 }
   566 
   567 - (void)testDontEncryptForMistrusted
   568 {
   569     PEPSession *session = [PEPSession new];
   570 
   571     // Our test user :
   572     // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
   573     // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
   574     XCTAssertTrue([PEPTestUtils importBundledKey:@"6FF00E97_sec.asc" session:session]);
   575 
   576     PEPIdentity *identAlice = [[PEPIdentity alloc]
   577                                initWithAddress:@"pep.test.alice@pep-project.org"
   578                                userID:ownUserId
   579                                userName:@"pEp Test Alice"
   580                                isOwn:YES
   581                                fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
   582 
   583     [session mySelf:identAlice];
   584 
   585     // pEp Test Bob (test key, don't use) <pep.test.bob@pep-project.org>
   586     // BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39
   587     XCTAssertTrue([PEPTestUtils importBundledKey:@"0xC9C2EE39.asc" session:session]);
   588 
   589     PEPIdentity *identBob = [[PEPIdentity alloc]
   590                              initWithAddress:@"pep.test.bob@pep-project.org"
   591                              userID:@"42" userName:@"pEp Test Bob"
   592                              isOwn:NO
   593                              fingerPrint:@"BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39"];
   594 
   595     [session updateIdentity:identBob];
   596 
   597     // mistrust Bob
   598     [session keyMistrusted:identBob];
   599 
   600     PEPMessage *msg = [PEPMessage new];
   601     msg.from = identAlice;
   602     msg.to = @[[[PEPIdentity alloc] initWithAddress:@"pep.test.bob@pep-project.org" userID:@"42"
   603                                            userName:@"pEp Test Bob" isOwn:NO]];
   604     msg.shortMessage = @"All Green Test";
   605     msg.longMessage = @"This is a text content";
   606     msg.direction = PEP_dir_outgoing;
   607 
   608     // Gray == PEP_rating_unencrypted
   609     PEP_rating clr = [session outgoingColorForMessage:msg];
   610     XCTAssertEqual(clr, PEP_rating_unencrypted);
   611 
   612     NSError *error = nil;
   613     PEPMessage *encMsg = [session encryptMessage:msg extraKeys:nil error:&error];
   614     XCTAssertNotNil(encMsg);
   615     XCTAssertNil(error);
   616 
   617     XCTAssertNotEqualObjects(encMsg.attachments[0][@"mimeType"], @"application/pgp-encrypted");
   618 
   619     [self pEpCleanUp];
   620 }
   621 
   622 - (void)testRevoke
   623 {
   624     PEPSession *session = [PEPSession new];
   625 
   626     // Our test user :
   627     // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
   628     // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
   629     XCTAssertTrue([PEPTestUtils importBundledKey:@"6FF00E97_sec.asc" session:session]);
   630     NSString *fpr = @"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97";
   631 
   632     PEPIdentity *identAlice = [[PEPIdentity alloc]
   633                                initWithAddress:@"pep.test.alice@pep-project.org"
   634                                userID:ownUserId
   635                                userName:@"pEp Test Alice"
   636                                isOwn:YES
   637                                fingerPrint:fpr];
   638 
   639     [session mySelf:identAlice];
   640 
   641     PEPIdentity *identAlice2 = [identAlice mutableCopy];
   642 
   643     // This will revoke key
   644     [session keyMistrusted:identAlice2];
   645     identAlice2.fingerPrint = nil;
   646     [session mySelf:identAlice];
   647 
   648     // Check fingerprint is different
   649     XCTAssertNotEqualObjects(identAlice2.fingerPrint, fpr);
   650 }
   651 
   652 - (void)testMailToMyself
   653 {
   654     PEPSession *session = [PEPSession new];
   655 
   656     // Our test user :
   657     // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
   658     // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
   659     XCTAssertTrue([PEPTestUtils importBundledKey:@"6FF00E97_sec.asc" session:session]);
   660 
   661     PEPIdentity *identAlice = [[PEPIdentity alloc]
   662                                initWithAddress:@"pep.test.alice@pep-project.org"
   663                                userID:ownUserId
   664                                userName:@"pEp Test Alice"
   665                                isOwn:YES
   666                                fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
   667 
   668     [session mySelf:identAlice];
   669 
   670     PEPMessage *msg = [PEPMessage new];
   671     msg.from = identAlice;
   672     msg.to = @[identAlice];
   673     msg.shortMessage = @"Mail to Myself";
   674     msg.longMessage = @"This is a text content";
   675     msg.direction = PEP_dir_outgoing;
   676 
   677     PEP_rating clr = [session outgoingColorForMessage:msg];
   678     XCTAssertEqual(clr, PEP_rating_trusted_and_anonymized);
   679 
   680     NSError *error = nil;
   681     PEPMessage *encMsg = [session encryptMessage:msg extraKeys:nil error:&error];
   682     XCTAssertNotNil(encMsg);
   683     XCTAssertNil(error);
   684 
   685     NSArray *keys;
   686 
   687     error = nil;
   688     PEPMessage *decmsg = [session
   689                           decryptMessage:encMsg
   690                           rating:&clr
   691                           keys:&keys
   692                           error:&error];
   693     XCTAssertNotNil(decmsg);
   694     XCTAssertNil(error);
   695     XCTAssertEqual(clr, PEP_rating_trusted_and_anonymized);
   696 }
   697 
   698 - (void)testEncryptedMailFromMutt
   699 {
   700     PEPSession *session = [PEPSession new];
   701 
   702     // This is the public key for test001@peptest.ch
   703     XCTAssertTrue([PEPTestUtils importBundledKey:@"A3FC7F0A.asc" session:session]);
   704 
   705     // This is the secret key for test001@peptest.ch
   706     XCTAssertTrue([PEPTestUtils importBundledKey:@"A3FC7F0A_sec.asc" session:session]);
   707 
   708     // Mail from mutt, already processed into message dict by the app.
   709     NSMutableDictionary *msgDict = [[PEPTestUtils
   710                                      unarchiveDictionary:@"msg_to_A3FC7F0A_from_mutt.ser"]
   711                                     mutableCopy];
   712     [msgDict removeObjectForKey:kPepLongMessage];
   713     [msgDict removeObjectForKey:kPepLongMessageFormatted];
   714 
   715     // Also extracted "live" from the app.
   716     NSMutableDictionary *accountDict = [[PEPTestUtils
   717                                          unarchiveDictionary:@"account_A3FC7F0A.ser"]
   718                                         mutableCopy];
   719     [accountDict removeObjectForKey:kPepCommType];
   720     [accountDict removeObjectForKey:kPepFingerprint];
   721     PEPIdentity *identMe = [[PEPIdentity alloc] initWithDictionary:accountDict];
   722 
   723     [session mySelf:identMe];
   724     XCTAssertNotNil(identMe.fingerPrint);
   725 
   726     NSArray* keys;
   727     PEPMessage *msg = [PEPMessage new];
   728     [msg setValuesForKeysWithDictionary:msgDict];
   729 
   730     // Technically, the mail is encrypted, but the signatures don't match
   731     NSError *error;
   732     PEPMessage *pepDecryptedMail = [session
   733                                     decryptMessage:msg
   734                                     rating:nil
   735                                     keys:&keys
   736                                     error:&error];
   737     XCTAssertNotNil(pepDecryptedMail);
   738     XCTAssertNil(error);
   739 
   740     XCTAssertNotNil(pepDecryptedMail.longMessage);
   741 }
   742 
   743 - (void)testOutgoingContactColor
   744 {
   745     PEPSession *session = [PEPSession new];
   746 
   747     PEPIdentity *partner1Orig = [PEPTestUtils foreignPepIdentityWithAddress:@"partner1@dontcare.me"
   748                                                                    userName:@"Partner 1"];
   749     NSString *pubKeyPartner1 = [PEPTestUtils loadResourceByName:@"partner1_F2D281C2789DD7F6_pub.asc"];
   750     XCTAssertNotNil(pubKeyPartner1);
   751     [session importKey:pubKeyPartner1];
   752 
   753     PEP_rating color = [session identityRating:partner1Orig];
   754     XCTAssertEqual(color, PEP_rating_reliable);
   755 }
   756 
   757 - (void)testGetTrustwords
   758 {
   759     PEPSession *session = [PEPSession new];
   760 
   761     PEPIdentity *partner1Orig = [[PEPIdentity alloc]
   762                                  initWithAddress:@"partner1@dontcare.me" userID:@"partner1"
   763                                  userName:@"partner1"
   764                                  isOwn:NO fingerPrint:@"F0CD3F7B422E5D587ABD885BF2D281C2789DD7F6"];
   765 
   766     PEPIdentity *meOrig = [[PEPIdentity alloc]
   767                            initWithAddress:@"me@dontcare.me" userID:@"me"
   768                            userName:@"me"
   769                            isOwn:NO fingerPrint:@"CC1F73F6FB774BF08B197691E3BFBCA9248FC681"];
   770 
   771     NSString *pubKeyPartner1 = [PEPTestUtils loadResourceByName:@"partner1_F2D281C2789DD7F6_pub.asc"];
   772     XCTAssertNotNil(pubKeyPartner1);
   773     NSString *pubKeyMe = [PEPTestUtils loadResourceByName:@"meATdontcare_E3BFBCA9248FC681_pub.asc"];
   774     XCTAssertNotNil(pubKeyMe);
   775     NSString *secKeyMe = [PEPTestUtils loadResourceByName:@"meATdontcare_E3BFBCA9248FC681_sec.asc"];
   776     XCTAssertNotNil(secKeyMe);
   777 
   778     NSString *trustwordsFull = [session getTrustwordsIdentity1:meOrig identity2:partner1Orig
   779                                                       language:nil full:YES];
   780     XCTAssertEqualObjects(trustwordsFull,
   781                           @"EMERSON GASPER TOKENISM BOLUS COLLAGE DESPISE BEDDED ENCRYPTION IMAGINE BEDFORD");
   782 
   783     NSString *trustwordsFullEnglish = [session getTrustwordsIdentity1:meOrig identity2:partner1Orig
   784                                                              language:@"en" full:YES];
   785     XCTAssertEqualObjects(trustwordsFullEnglish, trustwordsFull);
   786 
   787     NSString *trustwordsUndefined = [session getTrustwordsIdentity1:meOrig identity2:partner1Orig
   788                                                            language:@"ZZ" full:YES];
   789     XCTAssertNil(trustwordsUndefined);
   790 }
   791 
   792 - (void)testStringToRating
   793 {
   794     PEPSession *session = [PEPSession new];
   795     XCTAssertEqual([session ratingFromString:@"cannot_decrypt"], PEP_rating_cannot_decrypt);
   796     XCTAssertEqual([session ratingFromString:@"have_no_key"], PEP_rating_have_no_key);
   797     XCTAssertEqual([session ratingFromString:@"unencrypted"], PEP_rating_unencrypted);
   798     XCTAssertEqual([session ratingFromString:@"unencrypted_for_some"],
   799                    PEP_rating_unencrypted_for_some);
   800     XCTAssertEqual([session ratingFromString:@"unreliable"], PEP_rating_unreliable);
   801     XCTAssertEqual([session ratingFromString:@"reliable"], PEP_rating_reliable);
   802     XCTAssertEqual([session ratingFromString:@"trusted"], PEP_rating_trusted);
   803     XCTAssertEqual([session ratingFromString:@"trusted_and_anonymized"],
   804                    PEP_rating_trusted_and_anonymized);
   805     XCTAssertEqual([session ratingFromString:@"fully_anonymous"], PEP_rating_fully_anonymous);
   806     XCTAssertEqual([session ratingFromString:@"mistrust"], PEP_rating_mistrust);
   807     XCTAssertEqual([session ratingFromString:@"b0rken"], PEP_rating_b0rken);
   808     XCTAssertEqual([session ratingFromString:@"under_attack"], PEP_rating_under_attack);
   809     XCTAssertEqual([session ratingFromString:@"undefined"], PEP_rating_undefined);
   810     XCTAssertEqual([session ratingFromString:@"does not exist111"], PEP_rating_undefined);
   811 }
   812 
   813 - (void)testRatingToString
   814 {
   815     PEPSession *session = [PEPSession new];
   816     XCTAssertEqualObjects([session stringFromRating:PEP_rating_cannot_decrypt], @"cannot_decrypt");
   817     XCTAssertEqualObjects([session stringFromRating:PEP_rating_have_no_key], @"have_no_key");
   818     XCTAssertEqualObjects([session stringFromRating:PEP_rating_unencrypted], @"unencrypted");
   819     XCTAssertEqualObjects([session stringFromRating:PEP_rating_unencrypted_for_some],
   820                           @"unencrypted_for_some");
   821     XCTAssertEqualObjects([session stringFromRating:PEP_rating_unreliable], @"unreliable");
   822     XCTAssertEqualObjects([session stringFromRating:PEP_rating_reliable], @"reliable");
   823     XCTAssertEqualObjects([session stringFromRating:PEP_rating_trusted], @"trusted");
   824     XCTAssertEqualObjects([session stringFromRating:PEP_rating_trusted_and_anonymized],
   825                           @"trusted_and_anonymized");
   826     XCTAssertEqualObjects([session stringFromRating:PEP_rating_fully_anonymous],
   827                           @"fully_anonymous");
   828     XCTAssertEqualObjects([session stringFromRating:PEP_rating_mistrust], @"mistrust");
   829     XCTAssertEqualObjects([session stringFromRating:PEP_rating_b0rken], @"b0rken");
   830     XCTAssertEqualObjects([session stringFromRating:PEP_rating_under_attack], @"under_attack");
   831     XCTAssertEqualObjects([session stringFromRating:PEP_rating_undefined], @"undefined");
   832     XCTAssertEqualObjects([session stringFromRating:500], @"undefined");
   833 }
   834 
   835 - (void)testIsPEPUser
   836 {
   837     PEPSession *session = [PEPSession new];
   838 
   839     PEPIdentity *identMe = [[PEPIdentity alloc]
   840                             initWithAddress:@"me-myself-and-i@pep-project.org"
   841                             userID:@"me-myself-and-i"
   842                             userName:@"pEp Me"
   843                             isOwn:YES];
   844     [session mySelf:identMe];
   845     XCTAssertNotNil(identMe.fingerPrint);
   846 
   847     // PEP_CANNOT_FIND_PERSON == 902
   848     XCTAssertTrue([session isPEPUser:identMe]);
   849 }
   850 
   851 - (void)testXEncStatusForOutgoingEncryptedMail
   852 {
   853     [self helperXEncStatusForOutgoingEncryptdMailToSelf:NO expectedRating:PEP_rating_reliable];
   854 }
   855 
   856 - (void)testXEncStatusForOutgoingSelfEncryptedMail
   857 {
   858     [self helperXEncStatusForOutgoingEncryptdMailToSelf:YES
   859                                          expectedRating:PEP_rating_trusted_and_anonymized];
   860 }
   861 
   862 - (void)testEncryptMessagesWithoutKeys
   863 {
   864     PEPSession *session = [PEPSession new];
   865 
   866     PEPIdentity *identMe = [[PEPIdentity alloc]
   867                             initWithAddress:@"me-myself-and-i@pep-project.org"
   868                             userID:@"me-myself-and-i"
   869                             userName:@"pEp Me"
   870                             isOwn:YES];
   871     [session mySelf:identMe];
   872     XCTAssertNotNil(identMe.fingerPrint);
   873 
   874     PEPIdentity *identAlice = [[PEPIdentity alloc]
   875                                initWithAddress:@"alice@pep-project.org"
   876                                userID:@"alice"
   877                                userName:@"pEp Test Alice"
   878                                isOwn:NO];
   879 
   880     PEPMessage *msg = [PEPMessage new];
   881     msg.from = identMe;
   882     msg.to = @[identAlice];
   883     msg.shortMessage = @"Mail to Alice";
   884     msg.longMessage = @"Alice?";
   885     msg.direction = PEP_dir_outgoing;
   886 
   887     PEP_rating clr = [session outgoingColorForMessage:msg];
   888     XCTAssertEqual(clr, PEP_rating_unencrypted);
   889 
   890     NSError *error = nil;
   891     PEPMessage *encMsg = [session encryptMessage:msg extraKeys:nil error:&error];
   892     XCTAssertNotNil(encMsg);
   893     XCTAssertNil(error);
   894 
   895     XCTAssertNotNil(encMsg);
   896 
   897     PEPStringList *keys;
   898     PEP_rating pEpRating;
   899     error = nil;
   900     PEPMessage *decMsg = [session
   901                           decryptMessage:encMsg
   902                           rating:&pEpRating
   903                           keys:&keys
   904                           error:&error];
   905     XCTAssertNotNil(decMsg);
   906     XCTAssertNil(error);
   907 
   908     XCTAssertEqual(pEpRating, PEP_rating_unencrypted);
   909     XCTAssertNotNil(decMsg);
   910 }
   911 
   912 /**
   913  ENGINE-364. Tries to invoke trustPersonalKey on an identity without key,
   914  giving it a fake fingerprint.
   915  */
   916 - (void)testTrustPersonalKey
   917 {
   918     PEPSession *session = [PEPSession new];
   919 
   920     PEPIdentity *identMe = [[PEPIdentity alloc]
   921                             initWithAddress:@"me-myself-and-i@pep-project.org"
   922                             userID:@"me-myself-and-i"
   923                             userName:@"pEp Me"
   924                             isOwn:YES];
   925     [session mySelf:identMe];
   926     XCTAssertNotNil(identMe.fingerPrint);
   927 
   928     // The fingerprint is definitely wrong, we don't have a key
   929     PEPIdentity *identAlice = [[PEPIdentity alloc]
   930                                initWithAddress:@"alice@pep-project.org"
   931                                userID:@"alice"
   932                                userName:@"pEp Test Alice"
   933                                isOwn:NO
   934                                fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
   935 
   936     [session trustPersonalKey:identAlice];
   937 }
   938 
   939 /**
   940  ENGINE-381
   941  */
   942 - (void)testVolatileIdentityRating
   943 {
   944     PEPSession *session = [PEPSession new];
   945 
   946     PEPIdentity *identMe = [[PEPIdentity alloc]
   947                             initWithAddress:@"me-myself-and-i@pep-project.org"
   948                             userID:@"me-myself-and-i"
   949                             userName:@"pEp Me"
   950                             isOwn:YES];
   951     [session mySelf:identMe];
   952     XCTAssertNotNil(identMe.fingerPrint);
   953 
   954     PEPIdentity *identAlice = [self
   955                                checkImportingKeyFilePath:@"6FF00E97_sec.asc"
   956                                address:@"pep.test.alice@pep-project.org"
   957                                userID:@"alice_user_id"
   958                                fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
   959                                session: session];
   960     XCTAssertNotNil(identAlice);
   961 
   962     dispatch_group_t identityRatingGroup = dispatch_group_create();
   963 
   964     void (^ratingBlock)(void) = ^{
   965         PEPSession *innerSession = [PEPSession new];
   966         PEP_rating rating = [innerSession identityRating:identAlice];
   967         XCTAssertEqual(rating, PEP_rating_reliable);
   968     };
   969 
   970     for (int i = 0; i < 4; ++i) {
   971         dispatch_group_async(identityRatingGroup,
   972                              dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0),
   973                              ratingBlock);
   974     }
   975 
   976     for (int i = 0; i < 4; ++i) {
   977         ratingBlock();
   978     }
   979 
   980     dispatch_group_wait(identityRatingGroup, DISPATCH_TIME_FOREVER);
   981 }
   982 
   983 #pragma mark - configUnencryptedSubject
   984 
   985 - (void)testConfigUnencryptedSubject
   986 {
   987     // Setup Config to encrypt subject
   988     [PEPObjCAdapter setUnecryptedSubjectEnabled:NO];
   989 
   990     // Write mail to yourself ...
   991     PEPMessage *encMessage = [self mailWrittenToMySelf];
   992 
   993     // ... and assert subject is encrypted
   994     XCTAssertEqualObjects(encMessage.shortMessage, @"p≡p", @"Subject should be encrypted");
   995 }
   996 
   997 - (void)testConfigUnencryptedSubject_encryptedSubjectDisabled
   998 {
   999     // Setup Config to not encrypt subject
  1000     [PEPObjCAdapter setUnecryptedSubjectEnabled:YES];
  1001 
  1002     // Write mail to yourself ...
  1003     PEPMessage *encMessage = [self mailWrittenToMySelf];
  1004 
  1005     // ... and assert the subject is not encrypted
  1006     XCTAssertNotEqualObjects(encMessage.shortMessage, @"p≡p", @"Subject should not be encrypted");
  1007 }
  1008 
  1009 #pragma mark - Helpers
  1010 
  1011 - (PEPIdentity *)checkImportingKeyFilePath:(NSString *)filePath address:(NSString *)address
  1012                                     userID:(NSString *)userID
  1013                                fingerPrint:(NSString *)fingerPrint
  1014                                    session:(PEPSession *)session
  1015 {
  1016     if (!session) {
  1017         session = [PEPSession new];
  1018     }
  1019 
  1020     BOOL success = [PEPTestUtils importBundledKey:filePath session:session];
  1021     XCTAssertTrue(success);
  1022 
  1023     if (success) {
  1024         // Our test user:
  1025         PEPIdentity *identTest = [[PEPIdentity alloc]
  1026                                   initWithAddress:address
  1027                                   userID:userID
  1028                                   userName:[NSString stringWithFormat:@"Some User Name %@", userID]
  1029                                   isOwn:NO];
  1030 
  1031         [session updateIdentity:identTest];
  1032         XCTAssertNotNil(identTest.fingerPrint);
  1033         XCTAssertEqualObjects(identTest.fingerPrint, fingerPrint);
  1034 
  1035         return identTest;
  1036     } else {
  1037         return nil;
  1038     }
  1039 }
  1040 
  1041 - (PEPIdentity *)checkMySelfImportingKeyFilePath:(NSString *)filePath address:(NSString *)address
  1042                                           userID:(NSString *)userID
  1043                                      fingerPrint:(NSString *)fingerPrint
  1044                                          session:(PEPSession *)session
  1045 {
  1046     XCTAssertTrue([PEPTestUtils importBundledKey:filePath session:session]);
  1047 
  1048     // Our test user:
  1049     PEPIdentity *identTest = [[PEPIdentity alloc]
  1050                               initWithAddress:address
  1051                               userID:userID
  1052                               userName:[NSString stringWithFormat:@"Some User Name %@", userID]
  1053                               isOwn:YES
  1054                               fingerPrint: fingerPrint];
  1055 
  1056     NSError *error;
  1057     XCTAssertTrue([session setOwnKey:identTest fingerprint:fingerPrint error:&error]);
  1058     XCTAssertNil(error);
  1059     XCTAssertNotNil(identTest.fingerPrint);
  1060     XCTAssertEqualObjects(identTest.fingerPrint, fingerPrint);
  1061 
  1062     return identTest;
  1063 }
  1064 
  1065 /**
  1066  Verifies that a partner ID is really a correct Identity.
  1067  Usually used on identities imported as keys, since the engine has problems with them.
  1068  */
  1069 - (void)updateAndVerifyPartnerIdentity:(PEPIdentity *)partnerIdentity session:(PEPSession *)session
  1070 {
  1071     XCTAssertNotNil(partnerIdentity.fingerPrint);
  1072     [session updateIdentity:partnerIdentity];
  1073     XCTAssertNotNil(partnerIdentity.fingerPrint);
  1074     NSString *fingerprint = partnerIdentity.fingerPrint;
  1075     partnerIdentity.fingerPrint = nil;
  1076     [session updateIdentity:partnerIdentity];
  1077     XCTAssertNotNil(partnerIdentity.fingerPrint);
  1078     XCTAssertEqualObjects(partnerIdentity.fingerPrint, fingerprint);
  1079 }
  1080 
  1081 - (PEPMessage *)mailWrittenToMySelf
  1082 {
  1083     PEPSession *session = [PEPSession new];
  1084 
  1085     // Write a e-mail to yourself ...
  1086     PEPIdentity *me = [PEPTestUtils ownPepIdentityWithAddress:@"me@peptest.ch"
  1087                                                      userName:@"userName"];
  1088     [session mySelf:me];
  1089 
  1090     NSString *shortMessage = @"Subject";
  1091     NSString *longMessage = @"Oh, this is a long body text!";
  1092     PEPMessage *mail = [PEPTestUtils mailFrom:me
  1093                                       toIdent:me
  1094                                  shortMessage:shortMessage
  1095                                   longMessage:longMessage
  1096                                      outgoing:YES];
  1097     PEPMessage *encMessage;
  1098     [session encryptMessage:mail identity:me dest:&encMessage];
  1099 
  1100     return encMessage;
  1101 }
  1102 
  1103 - (PEPMessage *)internalEncryptToMySelfKeys:(PEPStringList **)keys
  1104 {
  1105     PEPSession *session = [PEPSession new];
  1106     
  1107     PEPIdentity *me = [PEPTestUtils ownPepIdentityWithAddress:@"me@peptest.ch"
  1108                                                      userName:@"userName"];
  1109     [session mySelf:me];
  1110     XCTAssertNotNil(me.fingerPrint);
  1111 
  1112     // Create draft
  1113     NSString *shortMessage = @"Subject";
  1114     NSString *longMessage = @"Oh, this is a long body text!";
  1115     PEPMessage *mail = [PEPTestUtils mailFrom:me toIdent:me shortMessage:shortMessage longMessage:longMessage outgoing:YES];
  1116 
  1117     PEPMessage *encMessage;
  1118     PEP_STATUS status = [session encryptMessage:mail identity:me dest:&encMessage];
  1119     XCTAssertEqual(status, 0);
  1120     XCTAssertEqualObjects(encMessage.shortMessage, @"p≡p");
  1121 
  1122     PEP_rating rating;
  1123     NSError *error;
  1124     PEPMessage *unencDict = [session
  1125                              decryptMessage:encMessage
  1126                              rating:&rating
  1127                              keys:keys
  1128                              error:&error];
  1129     XCTAssertNotNil(unencDict);
  1130     XCTAssertNil(error);
  1131 
  1132     XCTAssertGreaterThanOrEqual(rating, PEP_rating_reliable);
  1133 
  1134     XCTAssertEqualObjects(unencDict.shortMessage, shortMessage);
  1135     XCTAssertEqualObjects(unencDict.longMessage, longMessage);
  1136 
  1137     return unencDict;
  1138 }
  1139 
  1140 - (void)pEpCleanUp
  1141 {
  1142     [PEPTestUtils cleanUp];
  1143 }
  1144 
  1145 - (void)helperXEncStatusForOutgoingEncryptdMailToSelf:(BOOL)toSelf
  1146                                        expectedRating:(PEP_rating)expectedRating
  1147 {
  1148     PEPSession *session = [PEPSession new];
  1149 
  1150     // Partner pubkey for the test:
  1151     // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
  1152     // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
  1153     XCTAssertTrue([PEPTestUtils importBundledKey:@"0x6FF00E97.asc" session:session]);
  1154 
  1155     PEPIdentity *identAlice = [[PEPIdentity alloc]
  1156                                initWithAddress:@"pep.test.alice@pep-project.org"
  1157                                userID:ownUserId
  1158                                userName:@"pEp Test Alice"
  1159                                isOwn:NO
  1160                                fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
  1161     [self updateAndVerifyPartnerIdentity:identAlice session:session];
  1162 
  1163     PEPIdentity *identMe = [[PEPIdentity alloc]
  1164                                initWithAddress:@"me-myself-and-i@pep-project.org"
  1165                                userID:@"me-myself-and-i"
  1166                                userName:@"pEp Me"
  1167                                isOwn:YES];
  1168     [session mySelf:identMe];
  1169     XCTAssertNotNil(identMe.fingerPrint);
  1170 
  1171     PEPMessage *msg = [PEPMessage new];
  1172     msg.from = identMe;
  1173     msg.to = @[identAlice];
  1174     msg.shortMessage = @"Mail to Alice";
  1175     msg.longMessage = @"Alice?";
  1176     msg.direction = PEP_dir_outgoing;
  1177 
  1178     PEP_rating clr = [session outgoingColorForMessage:msg];
  1179     XCTAssertEqual(clr, PEP_rating_reliable);
  1180 
  1181     PEPMessage *encMsg;
  1182 
  1183     PEP_STATUS statusEnc = PEP_VERSION_MISMATCH;
  1184     if (toSelf) {
  1185         statusEnc = [session encryptMessage:msg identity:identMe dest:&encMsg];
  1186         XCTAssertEqual(statusEnc, PEP_STATUS_OK);
  1187     } else {
  1188         NSError *error = nil;
  1189         encMsg = [session encryptMessage:msg extraKeys:nil error:&error];
  1190         XCTAssertNotNil(encMsg);
  1191         XCTAssertNil(error);
  1192     }
  1193     XCTAssertNotNil(encMsg);
  1194 
  1195     PEPStringList *keys;
  1196     PEP_rating pEpRating;
  1197     NSError *error;
  1198     PEPMessage *decMsg = [session
  1199                           decryptMessage:encMsg
  1200                           rating:&pEpRating
  1201                           keys:&keys
  1202                           error:&error];
  1203     XCTAssertNil(error);
  1204     XCTAssertNotNil(decMsg);
  1205 
  1206     XCTAssertEqual(pEpRating, expectedRating);
  1207 
  1208     NSArray * encStatusField = nil;
  1209     for (NSArray *field in decMsg.optionalFields) {
  1210         NSString *header = [field[0] lowercaseString];
  1211         if ([header isEqualToString:@"x-encstatus"]) {
  1212             encStatusField = field;
  1213         }
  1214     }
  1215     XCTAssertNotNil(encStatusField);
  1216     if (encStatusField) {
  1217         PEP_rating outgoingRating = [session ratingFromString:encStatusField[1]];
  1218         XCTAssertEqual(outgoingRating, expectedRating);
  1219     }
  1220 }
  1221 
  1222 @end