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