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