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