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