pEpObjCTests/PEPSessionTest.m
author Dirk Zimmermann <dirk@pep-project.org>
Tue, 13 Mar 2018 09:49:50 +0100
changeset 524 60dbfc88af73
parent 523 3d167bef8afa
child 525 4ac6cca59c29
permissions -rw-r--r--
IOSAD-72 re-integrate PEP_STATUS
     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 status: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 status: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 status: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                           extraKeys:&keys
   692                           status:nil
   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                                     extraKeys:&keys
   737                                     status:nil
   738                                     error:&error];
   739     XCTAssertNotNil(pepDecryptedMail);
   740     XCTAssertNil(error);
   741 
   742     XCTAssertNotNil(pepDecryptedMail.longMessage);
   743 }
   744 
   745 - (void)testOutgoingContactColor
   746 {
   747     PEPSession *session = [PEPSession new];
   748 
   749     PEPIdentity *partner1Orig = [PEPTestUtils foreignPepIdentityWithAddress:@"partner1@dontcare.me"
   750                                                                    userName:@"Partner 1"];
   751     NSString *pubKeyPartner1 = [PEPTestUtils loadResourceByName:@"partner1_F2D281C2789DD7F6_pub.asc"];
   752     XCTAssertNotNil(pubKeyPartner1);
   753     [session importKey:pubKeyPartner1];
   754 
   755     PEP_rating color = [session identityRating:partner1Orig];
   756     XCTAssertEqual(color, PEP_rating_reliable);
   757 }
   758 
   759 - (void)testGetTrustwords
   760 {
   761     PEPSession *session = [PEPSession new];
   762 
   763     PEPIdentity *partner1Orig = [[PEPIdentity alloc]
   764                                  initWithAddress:@"partner1@dontcare.me" userID:@"partner1"
   765                                  userName:@"partner1"
   766                                  isOwn:NO fingerPrint:@"F0CD3F7B422E5D587ABD885BF2D281C2789DD7F6"];
   767 
   768     PEPIdentity *meOrig = [[PEPIdentity alloc]
   769                            initWithAddress:@"me@dontcare.me" userID:@"me"
   770                            userName:@"me"
   771                            isOwn:NO fingerPrint:@"CC1F73F6FB774BF08B197691E3BFBCA9248FC681"];
   772 
   773     NSString *pubKeyPartner1 = [PEPTestUtils loadResourceByName:@"partner1_F2D281C2789DD7F6_pub.asc"];
   774     XCTAssertNotNil(pubKeyPartner1);
   775     NSString *pubKeyMe = [PEPTestUtils loadResourceByName:@"meATdontcare_E3BFBCA9248FC681_pub.asc"];
   776     XCTAssertNotNil(pubKeyMe);
   777     NSString *secKeyMe = [PEPTestUtils loadResourceByName:@"meATdontcare_E3BFBCA9248FC681_sec.asc"];
   778     XCTAssertNotNil(secKeyMe);
   779 
   780     NSString *trustwordsFull = [session getTrustwordsIdentity1:meOrig identity2:partner1Orig
   781                                                       language:nil full:YES];
   782     XCTAssertEqualObjects(trustwordsFull,
   783                           @"EMERSON GASPER TOKENISM BOLUS COLLAGE DESPISE BEDDED ENCRYPTION IMAGINE BEDFORD");
   784 
   785     NSString *trustwordsFullEnglish = [session getTrustwordsIdentity1:meOrig identity2:partner1Orig
   786                                                              language:@"en" full:YES];
   787     XCTAssertEqualObjects(trustwordsFullEnglish, trustwordsFull);
   788 
   789     NSString *trustwordsUndefined = [session getTrustwordsIdentity1:meOrig identity2:partner1Orig
   790                                                            language:@"ZZ" full:YES];
   791     XCTAssertNil(trustwordsUndefined);
   792 }
   793 
   794 - (void)testStringToRating
   795 {
   796     PEPSession *session = [PEPSession new];
   797     XCTAssertEqual([session ratingFromString:@"cannot_decrypt"], PEP_rating_cannot_decrypt);
   798     XCTAssertEqual([session ratingFromString:@"have_no_key"], PEP_rating_have_no_key);
   799     XCTAssertEqual([session ratingFromString:@"unencrypted"], PEP_rating_unencrypted);
   800     XCTAssertEqual([session ratingFromString:@"unencrypted_for_some"],
   801                    PEP_rating_unencrypted_for_some);
   802     XCTAssertEqual([session ratingFromString:@"unreliable"], PEP_rating_unreliable);
   803     XCTAssertEqual([session ratingFromString:@"reliable"], PEP_rating_reliable);
   804     XCTAssertEqual([session ratingFromString:@"trusted"], PEP_rating_trusted);
   805     XCTAssertEqual([session ratingFromString:@"trusted_and_anonymized"],
   806                    PEP_rating_trusted_and_anonymized);
   807     XCTAssertEqual([session ratingFromString:@"fully_anonymous"], PEP_rating_fully_anonymous);
   808     XCTAssertEqual([session ratingFromString:@"mistrust"], PEP_rating_mistrust);
   809     XCTAssertEqual([session ratingFromString:@"b0rken"], PEP_rating_b0rken);
   810     XCTAssertEqual([session ratingFromString:@"under_attack"], PEP_rating_under_attack);
   811     XCTAssertEqual([session ratingFromString:@"undefined"], PEP_rating_undefined);
   812     XCTAssertEqual([session ratingFromString:@"does not exist111"], PEP_rating_undefined);
   813 }
   814 
   815 - (void)testRatingToString
   816 {
   817     PEPSession *session = [PEPSession new];
   818     XCTAssertEqualObjects([session stringFromRating:PEP_rating_cannot_decrypt], @"cannot_decrypt");
   819     XCTAssertEqualObjects([session stringFromRating:PEP_rating_have_no_key], @"have_no_key");
   820     XCTAssertEqualObjects([session stringFromRating:PEP_rating_unencrypted], @"unencrypted");
   821     XCTAssertEqualObjects([session stringFromRating:PEP_rating_unencrypted_for_some],
   822                           @"unencrypted_for_some");
   823     XCTAssertEqualObjects([session stringFromRating:PEP_rating_unreliable], @"unreliable");
   824     XCTAssertEqualObjects([session stringFromRating:PEP_rating_reliable], @"reliable");
   825     XCTAssertEqualObjects([session stringFromRating:PEP_rating_trusted], @"trusted");
   826     XCTAssertEqualObjects([session stringFromRating:PEP_rating_trusted_and_anonymized],
   827                           @"trusted_and_anonymized");
   828     XCTAssertEqualObjects([session stringFromRating:PEP_rating_fully_anonymous],
   829                           @"fully_anonymous");
   830     XCTAssertEqualObjects([session stringFromRating:PEP_rating_mistrust], @"mistrust");
   831     XCTAssertEqualObjects([session stringFromRating:PEP_rating_b0rken], @"b0rken");
   832     XCTAssertEqualObjects([session stringFromRating:PEP_rating_under_attack], @"under_attack");
   833     XCTAssertEqualObjects([session stringFromRating:PEP_rating_undefined], @"undefined");
   834     XCTAssertEqualObjects([session stringFromRating:500], @"undefined");
   835 }
   836 
   837 - (void)testIsPEPUser
   838 {
   839     PEPSession *session = [PEPSession new];
   840 
   841     PEPIdentity *identMe = [[PEPIdentity alloc]
   842                             initWithAddress:@"me-myself-and-i@pep-project.org"
   843                             userID:@"me-myself-and-i"
   844                             userName:@"pEp Me"
   845                             isOwn:YES];
   846     [session mySelf:identMe];
   847     XCTAssertNotNil(identMe.fingerPrint);
   848 
   849     // PEP_CANNOT_FIND_PERSON == 902
   850     XCTAssertTrue([session isPEPUser:identMe]);
   851 }
   852 
   853 - (void)testXEncStatusForOutgoingEncryptedMail
   854 {
   855     [self helperXEncStatusForOutgoingEncryptdMailToSelf:NO expectedRating:PEP_rating_reliable];
   856 }
   857 
   858 - (void)testXEncStatusForOutgoingSelfEncryptedMail
   859 {
   860     [self helperXEncStatusForOutgoingEncryptdMailToSelf:YES
   861                                          expectedRating:PEP_rating_trusted_and_anonymized];
   862 }
   863 
   864 - (void)testEncryptMessagesWithoutKeys
   865 {
   866     PEPSession *session = [PEPSession new];
   867 
   868     PEPIdentity *identMe = [[PEPIdentity alloc]
   869                             initWithAddress:@"me-myself-and-i@pep-project.org"
   870                             userID:@"me-myself-and-i"
   871                             userName:@"pEp Me"
   872                             isOwn:YES];
   873     [session mySelf:identMe];
   874     XCTAssertNotNil(identMe.fingerPrint);
   875 
   876     PEPIdentity *identAlice = [[PEPIdentity alloc]
   877                                initWithAddress:@"alice@pep-project.org"
   878                                userID:@"alice"
   879                                userName:@"pEp Test Alice"
   880                                isOwn:NO];
   881 
   882     PEPMessage *msg = [PEPMessage new];
   883     msg.from = identMe;
   884     msg.to = @[identAlice];
   885     msg.shortMessage = @"Mail to Alice";
   886     msg.longMessage = @"Alice?";
   887     msg.direction = PEP_dir_outgoing;
   888 
   889     PEP_rating clr = [session outgoingColorForMessage:msg];
   890     XCTAssertEqual(clr, PEP_rating_unencrypted);
   891 
   892     NSError *error = nil;
   893     PEPMessage *encMsg = [session encryptMessage:msg extraKeys:nil status:nil error:&error];
   894     XCTAssertNotNil(encMsg);
   895     XCTAssertNil(error);
   896 
   897     XCTAssertNotNil(encMsg);
   898 
   899     PEPStringList *keys;
   900     PEP_rating pEpRating;
   901     error = nil;
   902     PEPMessage *decMsg = [session
   903                           decryptMessage:encMsg
   904                           rating:&pEpRating
   905                           extraKeys:&keys
   906                           status:nil
   907                           error:&error];
   908     XCTAssertNotNil(decMsg);
   909     XCTAssertNil(error);
   910 
   911     XCTAssertEqual(pEpRating, PEP_rating_unencrypted);
   912     XCTAssertNotNil(decMsg);
   913 }
   914 
   915 /**
   916  ENGINE-364. Tries to invoke trustPersonalKey on an identity without key,
   917  giving it a fake fingerprint.
   918  */
   919 - (void)testTrustPersonalKey
   920 {
   921     PEPSession *session = [PEPSession new];
   922 
   923     PEPIdentity *identMe = [[PEPIdentity alloc]
   924                             initWithAddress:@"me-myself-and-i@pep-project.org"
   925                             userID:@"me-myself-and-i"
   926                             userName:@"pEp Me"
   927                             isOwn:YES];
   928     [session mySelf:identMe];
   929     XCTAssertNotNil(identMe.fingerPrint);
   930 
   931     // The fingerprint is definitely wrong, we don't have a key
   932     PEPIdentity *identAlice = [[PEPIdentity alloc]
   933                                initWithAddress:@"alice@pep-project.org"
   934                                userID:@"alice"
   935                                userName:@"pEp Test Alice"
   936                                isOwn:NO
   937                                fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
   938 
   939     [session trustPersonalKey:identAlice];
   940 }
   941 
   942 /**
   943  ENGINE-381
   944  */
   945 - (void)testVolatileIdentityRating
   946 {
   947     PEPSession *session = [PEPSession new];
   948 
   949     PEPIdentity *identMe = [[PEPIdentity alloc]
   950                             initWithAddress:@"me-myself-and-i@pep-project.org"
   951                             userID:@"me-myself-and-i"
   952                             userName:@"pEp Me"
   953                             isOwn:YES];
   954     [session mySelf:identMe];
   955     XCTAssertNotNil(identMe.fingerPrint);
   956 
   957     PEPIdentity *identAlice = [self
   958                                checkImportingKeyFilePath:@"6FF00E97_sec.asc"
   959                                address:@"pep.test.alice@pep-project.org"
   960                                userID:@"alice_user_id"
   961                                fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
   962                                session: session];
   963     XCTAssertNotNil(identAlice);
   964 
   965     dispatch_group_t identityRatingGroup = dispatch_group_create();
   966 
   967     void (^ratingBlock)(void) = ^{
   968         PEPSession *innerSession = [PEPSession new];
   969         PEP_rating rating = [innerSession identityRating:identAlice];
   970         XCTAssertEqual(rating, PEP_rating_reliable);
   971     };
   972 
   973     for (int i = 0; i < 4; ++i) {
   974         dispatch_group_async(identityRatingGroup,
   975                              dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0),
   976                              ratingBlock);
   977     }
   978 
   979     for (int i = 0; i < 4; ++i) {
   980         ratingBlock();
   981     }
   982 
   983     dispatch_group_wait(identityRatingGroup, DISPATCH_TIME_FOREVER);
   984 }
   985 
   986 #pragma mark - configUnencryptedSubject
   987 
   988 - (void)testConfigUnencryptedSubject
   989 {
   990     // Setup Config to encrypt subject
   991     [PEPObjCAdapter setUnecryptedSubjectEnabled:NO];
   992 
   993     // Write mail to yourself ...
   994     PEPMessage *encMessage = [self mailWrittenToMySelf];
   995 
   996     // ... and assert subject is encrypted
   997     XCTAssertEqualObjects(encMessage.shortMessage, @"p≡p", @"Subject should be encrypted");
   998 }
   999 
  1000 - (void)testConfigUnencryptedSubject_encryptedSubjectDisabled
  1001 {
  1002     // Setup Config to not encrypt subject
  1003     [PEPObjCAdapter setUnecryptedSubjectEnabled:YES];
  1004 
  1005     // Write mail to yourself ...
  1006     PEPMessage *encMessage = [self mailWrittenToMySelf];
  1007 
  1008     // ... and assert the subject is not encrypted
  1009     XCTAssertNotEqualObjects(encMessage.shortMessage, @"p≡p", @"Subject should not be encrypted");
  1010 }
  1011 
  1012 #pragma mark - Helpers
  1013 
  1014 - (PEPIdentity *)checkImportingKeyFilePath:(NSString *)filePath address:(NSString *)address
  1015                                     userID:(NSString *)userID
  1016                                fingerPrint:(NSString *)fingerPrint
  1017                                    session:(PEPSession *)session
  1018 {
  1019     if (!session) {
  1020         session = [PEPSession new];
  1021     }
  1022 
  1023     BOOL success = [PEPTestUtils importBundledKey:filePath session:session];
  1024     XCTAssertTrue(success);
  1025 
  1026     if (success) {
  1027         // Our test user:
  1028         PEPIdentity *identTest = [[PEPIdentity alloc]
  1029                                   initWithAddress:address
  1030                                   userID:userID
  1031                                   userName:[NSString stringWithFormat:@"Some User Name %@", userID]
  1032                                   isOwn:NO];
  1033 
  1034         [session updateIdentity:identTest];
  1035         XCTAssertNotNil(identTest.fingerPrint);
  1036         XCTAssertEqualObjects(identTest.fingerPrint, fingerPrint);
  1037 
  1038         return identTest;
  1039     } else {
  1040         return nil;
  1041     }
  1042 }
  1043 
  1044 - (PEPIdentity *)checkMySelfImportingKeyFilePath:(NSString *)filePath address:(NSString *)address
  1045                                           userID:(NSString *)userID
  1046                                      fingerPrint:(NSString *)fingerPrint
  1047                                          session:(PEPSession *)session
  1048 {
  1049     XCTAssertTrue([PEPTestUtils importBundledKey:filePath session:session]);
  1050 
  1051     // Our test user:
  1052     PEPIdentity *identTest = [[PEPIdentity alloc]
  1053                               initWithAddress:address
  1054                               userID:userID
  1055                               userName:[NSString stringWithFormat:@"Some User Name %@", userID]
  1056                               isOwn:YES
  1057                               fingerPrint: fingerPrint];
  1058 
  1059     NSError *error;
  1060     XCTAssertTrue([session setOwnKey:identTest fingerprint:fingerPrint error:&error]);
  1061     XCTAssertNil(error);
  1062     XCTAssertNotNil(identTest.fingerPrint);
  1063     XCTAssertEqualObjects(identTest.fingerPrint, fingerPrint);
  1064 
  1065     return identTest;
  1066 }
  1067 
  1068 /**
  1069  Verifies that a partner ID is really a correct Identity.
  1070  Usually used on identities imported as keys, since the engine has problems with them.
  1071  */
  1072 - (void)updateAndVerifyPartnerIdentity:(PEPIdentity *)partnerIdentity session:(PEPSession *)session
  1073 {
  1074     XCTAssertNotNil(partnerIdentity.fingerPrint);
  1075     [session updateIdentity:partnerIdentity];
  1076     XCTAssertNotNil(partnerIdentity.fingerPrint);
  1077     NSString *fingerprint = partnerIdentity.fingerPrint;
  1078     partnerIdentity.fingerPrint = nil;
  1079     [session updateIdentity:partnerIdentity];
  1080     XCTAssertNotNil(partnerIdentity.fingerPrint);
  1081     XCTAssertEqualObjects(partnerIdentity.fingerPrint, fingerprint);
  1082 }
  1083 
  1084 - (PEPMessage *)mailWrittenToMySelf
  1085 {
  1086     PEPSession *session = [PEPSession new];
  1087 
  1088     // Write a e-mail to yourself ...
  1089     PEPIdentity *me = [PEPTestUtils ownPepIdentityWithAddress:@"me@peptest.ch"
  1090                                                      userName:@"userName"];
  1091     [session mySelf:me];
  1092 
  1093     NSString *shortMessage = @"Subject";
  1094     NSString *longMessage = @"Oh, this is a long body text!";
  1095     PEPMessage *mail = [PEPTestUtils mailFrom:me
  1096                                       toIdent:me
  1097                                  shortMessage:shortMessage
  1098                                   longMessage:longMessage
  1099                                      outgoing:YES];
  1100     PEPMessage *encMessage;
  1101     [session encryptMessage:mail identity:me dest:&encMessage];
  1102 
  1103     return encMessage;
  1104 }
  1105 
  1106 - (PEPMessage *)internalEncryptToMySelfKeys:(PEPStringList **)keys
  1107 {
  1108     PEPSession *session = [PEPSession new];
  1109     
  1110     PEPIdentity *me = [PEPTestUtils ownPepIdentityWithAddress:@"me@peptest.ch"
  1111                                                      userName:@"userName"];
  1112     [session mySelf:me];
  1113     XCTAssertNotNil(me.fingerPrint);
  1114 
  1115     // Create draft
  1116     NSString *shortMessage = @"Subject";
  1117     NSString *longMessage = @"Oh, this is a long body text!";
  1118     PEPMessage *mail = [PEPTestUtils mailFrom:me toIdent:me shortMessage:shortMessage longMessage:longMessage outgoing:YES];
  1119 
  1120     PEPMessage *encMessage;
  1121     PEP_STATUS status = [session encryptMessage:mail identity:me dest:&encMessage];
  1122     XCTAssertEqual(status, 0);
  1123     XCTAssertEqualObjects(encMessage.shortMessage, @"p≡p");
  1124 
  1125     PEP_rating rating;
  1126     NSError *error;
  1127     PEPMessage *unencDict = [session
  1128                              decryptMessage:encMessage
  1129                              rating:&rating
  1130                              extraKeys:keys
  1131                              status:nil
  1132                              error:&error];
  1133     XCTAssertNotNil(unencDict);
  1134     XCTAssertNil(error);
  1135 
  1136     XCTAssertGreaterThanOrEqual(rating, PEP_rating_reliable);
  1137 
  1138     XCTAssertEqualObjects(unencDict.shortMessage, shortMessage);
  1139     XCTAssertEqualObjects(unencDict.longMessage, longMessage);
  1140 
  1141     return unencDict;
  1142 }
  1143 
  1144 - (void)pEpCleanUp
  1145 {
  1146     [PEPTestUtils cleanUp];
  1147 }
  1148 
  1149 - (void)helperXEncStatusForOutgoingEncryptdMailToSelf:(BOOL)toSelf
  1150                                        expectedRating:(PEP_rating)expectedRating
  1151 {
  1152     PEPSession *session = [PEPSession new];
  1153 
  1154     // Partner pubkey for the test:
  1155     // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
  1156     // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
  1157     XCTAssertTrue([PEPTestUtils importBundledKey:@"0x6FF00E97.asc" session:session]);
  1158 
  1159     PEPIdentity *identAlice = [[PEPIdentity alloc]
  1160                                initWithAddress:@"pep.test.alice@pep-project.org"
  1161                                userID:ownUserId
  1162                                userName:@"pEp Test Alice"
  1163                                isOwn:NO
  1164                                fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
  1165     [self updateAndVerifyPartnerIdentity:identAlice session:session];
  1166 
  1167     PEPIdentity *identMe = [[PEPIdentity alloc]
  1168                                initWithAddress:@"me-myself-and-i@pep-project.org"
  1169                                userID:@"me-myself-and-i"
  1170                                userName:@"pEp Me"
  1171                                isOwn:YES];
  1172     [session mySelf:identMe];
  1173     XCTAssertNotNil(identMe.fingerPrint);
  1174 
  1175     PEPMessage *msg = [PEPMessage new];
  1176     msg.from = identMe;
  1177     msg.to = @[identAlice];
  1178     msg.shortMessage = @"Mail to Alice";
  1179     msg.longMessage = @"Alice?";
  1180     msg.direction = PEP_dir_outgoing;
  1181 
  1182     PEP_rating clr = [session outgoingColorForMessage:msg];
  1183     XCTAssertEqual(clr, PEP_rating_reliable);
  1184 
  1185     PEPMessage *encMsg;
  1186 
  1187     PEP_STATUS statusEnc = PEP_VERSION_MISMATCH;
  1188     if (toSelf) {
  1189         statusEnc = [session encryptMessage:msg identity:identMe dest:&encMsg];
  1190         XCTAssertEqual(statusEnc, PEP_STATUS_OK);
  1191     } else {
  1192         NSError *error = nil;
  1193         encMsg = [session encryptMessage:msg extraKeys:nil status:nil error:&error];
  1194         XCTAssertNotNil(encMsg);
  1195         XCTAssertNil(error);
  1196     }
  1197     XCTAssertNotNil(encMsg);
  1198 
  1199     PEPStringList *keys;
  1200     PEP_rating pEpRating;
  1201     NSError *error;
  1202     PEPMessage *decMsg = [session
  1203                           decryptMessage:encMsg
  1204                           rating:&pEpRating
  1205                           extraKeys:&keys
  1206                           status:nil
  1207                           error:&error];
  1208     XCTAssertNil(error);
  1209     XCTAssertNotNil(decMsg);
  1210 
  1211     XCTAssertEqual(pEpRating, expectedRating);
  1212 
  1213     NSArray * encStatusField = nil;
  1214     for (NSArray *field in decMsg.optionalFields) {
  1215         NSString *header = [field[0] lowercaseString];
  1216         if ([header isEqualToString:@"x-encstatus"]) {
  1217             encStatusField = field;
  1218         }
  1219     }
  1220     XCTAssertNotNil(encStatusField);
  1221     if (encStatusField) {
  1222         PEP_rating outgoingRating = [session ratingFromString:encStatusField[1]];
  1223         XCTAssertEqual(outgoingRating, expectedRating);
  1224     }
  1225 }
  1226 
  1227 @end