pEpObjCTests/PEPSessionTest.m
author Dirk Zimmermann <dirk@pep-project.org>
Fri, 09 Feb 2018 16:08:10 +0100
changeset 446 70bb6386c76f
parent 445 3027e25306ea
child 449 21a193e38885
permissions -rw-r--r--
IOS-937 testIdentityRatingTrustResetMistrustUndo
     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 
   162     [self checkImportingKeyFilePath:@"0xC9C2EE39.asc" address:@"pep.test.bob@pep-project.org"
   163                              userID:@"This Is Bob"
   164                         fingerPrint:@"BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39"];
   165 }
   166 
   167 - (void)testIdentityRating
   168 {
   169     PEPSession *session = [PEPSession new];
   170 
   171     PEPIdentity *me = [self
   172                        checkMySelfImportingKeyFilePath:@"6FF00E97_sec.asc"
   173                        address:@"pep.test.alice@pep-project.org"
   174                        userID:@"Alice_User_ID"
   175                        fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
   176     XCTAssertEqual([session identityRating:me], PEP_rating_trusted_and_anonymized);
   177 
   178     PEPIdentity *alice = [self
   179                           checkImportingKeyFilePath:@"6FF00E97_sec.asc"
   180                           address:@"pep.test.alice@pep-project.org"
   181                           userID:@"This Is Alice"
   182                           fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
   183     XCTAssertEqual([session identityRating:alice], PEP_rating_reliable);
   184 }
   185 
   186 - (void)testIdentityRatingTrustResetMistrustUndo
   187 {
   188     PEPSession *session = [PEPSession new];
   189 
   190     PEPIdentity *me = [self
   191                        checkMySelfImportingKeyFilePath:@"6FF00E97_sec.asc"
   192                        address:@"pep.test.alice@pep-project.org"
   193                        userID:@"Alice_User_ID"
   194                        fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
   195     XCTAssertEqual([session identityRating:me], PEP_rating_trusted_and_anonymized);
   196 
   197     PEPIdentity *alice = [self
   198                           checkImportingKeyFilePath:@"6FF00E97_sec.asc"
   199                           address:@"pep.test.alice@pep-project.org"
   200                           userID:@"This Is Alice"
   201                           fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
   202     XCTAssertEqual([session identityRating:alice], PEP_rating_reliable);
   203 
   204     [session trustPersonalKey:alice];
   205     XCTAssertEqual([session identityRating:alice], PEP_rating_trusted);
   206 
   207     [session keyResetTrust:alice];
   208     XCTAssertEqual([session identityRating:alice], PEP_rating_reliable);
   209 
   210     [session keyMistrusted:alice];
   211     XCTAssertEqual([session identityRating:alice], PEP_rating_have_no_key);
   212 
   213     [session undoLastMistrust];
   214     XCTAssertEqual([session identityRating:alice], PEP_rating_reliable);
   215 
   216     [session trustPersonalKey:alice];
   217     XCTAssertEqual([session identityRating:alice], PEP_rating_trusted);
   218 
   219     [session keyResetTrust:alice];
   220     XCTAssertEqual([session identityRating:alice], PEP_rating_reliable);
   221 }
   222 
   223 - (void)testOutgoingColors
   224 {
   225     PEPSession *session = [PEPSession new];
   226 
   227     // Our test user :
   228     // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
   229     // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
   230     [PEPTestUtils importBundledKey:@"6FF00E97_sec.asc"];
   231 
   232     // Our test user :
   233     PEPIdentity *identAlice = [self
   234                                checkMySelfImportingKeyFilePath:@"6FF00E97_sec.asc"
   235                                address:@"pep.test.alice@pep-project.org"
   236                                userID:@"Alice_User_ID"
   237                                fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
   238 
   239     //Message
   240 
   241     {
   242         PEPIdentity *identUnknownBob = [[PEPIdentity alloc]
   243                                         initWithAddress:@"pep.test.unknown.bob@pep-project.org"
   244                                         userID:@"4242" userName:@"pEp Test Bob Unknown"
   245                                         isOwn:NO];
   246 
   247         PEPMessage *msgGray = [PEPMessage new];
   248         msgGray.from = identAlice;
   249         msgGray.to = @[identUnknownBob];
   250         msgGray.shortMessage = @"All Gray Test";
   251         msgGray.longMessage = @"This is a text content";
   252         msgGray.direction = PEP_dir_outgoing;
   253 
   254         // Test with unknown Bob
   255         PEP_rating clr = [session outgoingColorForMessage:msgGray];
   256         XCTAssertEqual(clr, PEP_rating_unencrypted);
   257     }
   258 
   259     PEPIdentity *identBob = [self
   260                              checkImportingKeyFilePath:@"0xC9C2EE39.asc"
   261                              address:@"pep.test.bob@pep-project.org"
   262                              userID:@"42"
   263                              fingerPrint:@"BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39"];
   264 
   265     PEPMessage *msg = [PEPMessage new];
   266     msg.from = identAlice;
   267     msg.to = @[identBob];
   268     msg.shortMessage = @"All Gray Test";
   269     msg.longMessage = @"This is a text content";
   270     msg.direction = PEP_dir_outgoing;
   271 
   272     // Should be yellow, since no handshake happened.
   273     PEP_rating clr = [session outgoingColorForMessage:msg];
   274     XCTAssertEqual(clr, PEP_rating_reliable);
   275 
   276     clr = [session identityRating:identBob];
   277     XCTAssertEqual(clr, PEP_rating_reliable);
   278 
   279     // Let' say we got that handshake, set PEP_ct_confirmed in Bob's identity
   280     [session trustPersonalKey:identBob];
   281 
   282     // This time it should be green
   283     clr = [session outgoingColorForMessage:msg];
   284     XCTAssertEqual(clr, PEP_rating_trusted);
   285 
   286     clr = [session identityRating:identBob];
   287     XCTAssertEqual(clr, PEP_rating_trusted);
   288 
   289     // Let' say we undo handshake
   290     [session keyResetTrust:identBob];
   291 
   292     // Yellow ?
   293     clr = [session outgoingColorForMessage:msg];
   294     XCTAssertEqual(clr, PEP_rating_reliable);
   295 
   296     // mistrust Bob
   297     [session keyMistrusted:identBob];
   298 
   299     identBob.fingerPrint = nil;
   300     [session updateIdentity:identBob];
   301     XCTAssertNil(identBob.fingerPrint);
   302 
   303     // Gray == PEP_rating_unencrypted
   304     clr = [session outgoingColorForMessage:msg];
   305     XCTAssertEqual(clr, PEP_rating_unencrypted);
   306 
   307     // Undo
   308     [session undoLastMistrust];
   309     [session updateIdentity:identBob];
   310     XCTAssertNotNil(identBob.fingerPrint);
   311 
   312     // Back to yellow
   313     clr = [session outgoingColorForMessage:msg];
   314     XCTAssertEqual(clr, PEP_rating_reliable);
   315     XCTAssertEqual([session identityRating:identBob], PEP_rating_reliable);
   316 
   317     // Trust again
   318     [session trustPersonalKey:identBob];
   319 
   320     // Back to green
   321     clr = [session outgoingColorForMessage:msg];
   322     XCTAssertEqual(clr, PEP_rating_trusted);
   323 
   324     // Now let see if it turns back yellow if we add an unconfirmed folk.
   325     // pEp Test John (test key, don't use) <pep.test.john@pep-project.org>
   326     // AA2E4BEB93E5FE33DEFD8BE1135CD6D170DCF575
   327     [PEPTestUtils importBundledKey:@"0x70DCF575.asc"];
   328 
   329     PEPIdentity *identJohn = [[PEPIdentity alloc]
   330                               initWithAddress:@"pep.test.john@pep-project.org"
   331                               userID:@"101" userName:@"pEp Test John"
   332                               isOwn:NO
   333                               fingerPrint:@"AA2E4BEB93E5FE33DEFD8BE1135CD6D170DCF575"];
   334 
   335     [session updateIdentity:identJohn];
   336 
   337     msg.cc = @[[PEPTestUtils foreignPepIdentityWithAddress:@"pep.test.john@pep-project.org"
   338                                                   userName:@"pEp Test John"]];
   339     // Yellow ?
   340     clr = [session outgoingColorForMessage:msg];
   341     XCTAssertEqual(clr, PEP_rating_reliable);
   342 
   343     PEPMessage *encmsg;
   344     PEP_STATUS status = [session encryptMessage:msg extra:@[] dest:&encmsg];
   345 
   346     XCTAssertNotNil(encmsg);
   347     XCTAssertEqualObjects(encmsg.shortMessage, @"p≡p");
   348     XCTAssertTrue([encmsg.longMessage containsString:@"p≡p"]);
   349 
   350     XCTAssertEqual(status, PEP_STATUS_OK);
   351 }
   352 
   353 
   354 - (void)testOutgoingBccColors
   355 {
   356     PEPSession *session = [PEPSession new];
   357 
   358     // Our test user :
   359     // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
   360     // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
   361     [PEPTestUtils importBundledKey:@"6FF00E97_sec.asc"];
   362 
   363     PEPIdentity *identAlice = [[PEPIdentity alloc]
   364                                initWithAddress:@"pep.test.alice@pep-project.org"
   365                                userID:ownUserId
   366                                userName:@"pEp Test Alice"
   367                                isOwn:YES
   368                                fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
   369 
   370     [session mySelf:identAlice];
   371 
   372     PEPMessage *msg = [PEPMessage new];
   373     msg.from = identAlice;
   374     msg.to = @[[[PEPIdentity alloc] initWithAddress:@"pep.test.bob@pep-project.org"
   375                                              userID:@"42" userName:@"pEp Test Bob" isOwn:NO]];
   376     msg.shortMessage = @"All Green Test";
   377     msg.longMessage = @"This is a text content";
   378     msg.direction = PEP_dir_outgoing;
   379 
   380     // Test with unknown Bob
   381     PEP_rating clr = [session outgoingColorForMessage:msg];
   382     XCTAssertEqual(clr, PEP_rating_unencrypted);
   383 
   384     // Now let see with bob's pubkey already known
   385     // pEp Test Bob (test key, don't use) <pep.test.bob@pep-project.org>
   386     // BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39
   387     [PEPTestUtils importBundledKey:@"0xC9C2EE39.asc"];
   388 
   389     PEPIdentity *identBob = [[PEPIdentity alloc]
   390                              initWithAddress:@"pep.test.bob@pep-project.org"
   391                              userID:@"42" userName:@"pEp Test Bob"
   392                              isOwn:NO
   393                              fingerPrint:@"BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39"];
   394 
   395     [session updateIdentity:identBob];
   396 
   397     // Should be yellow, since no handshake happened.
   398     clr = [session outgoingColorForMessage:msg];
   399     XCTAssertEqual(clr, PEP_rating_reliable);
   400 
   401     clr = [session identityRating:identBob];
   402     XCTAssertEqual(clr, PEP_rating_reliable);
   403 
   404     // Let' say we got that handshake, set PEP_ct_confirmed in Bob's identity
   405     [session trustPersonalKey:identBob];
   406 
   407     // This time it should be green
   408     clr = [session outgoingColorForMessage:msg];
   409     XCTAssertEqual(clr, PEP_rating_trusted);
   410 
   411     clr = [session identityRating:identBob];
   412     XCTAssertEqual(clr, PEP_rating_trusted);
   413 
   414     // Now let see if it turns back yellow if we add an unconfirmed folk.
   415     // pEp Test John (test key, don't use) <pep.test.john@pep-project.org>
   416     // AA2E4BEB93E5FE33DEFD8BE1135CD6D170DCF575
   417     [PEPTestUtils importBundledKey:@"0x70DCF575.asc"];
   418 
   419     PEPIdentity *identJohn = [[PEPIdentity alloc]
   420                               initWithAddress:@"pep.test.john@pep-project.org"
   421                               userID:@"101" userName:@"pEp Test John"
   422                               isOwn:NO
   423                               fingerPrint:@"AA2E4BEB93E5FE33DEFD8BE1135CD6D170DCF575"];
   424 
   425     [session updateIdentity:identJohn];
   426 
   427     msg.bcc = @[[[PEPIdentity alloc] initWithAddress:@"pep.test.john@pep-project.org"
   428                                               userID:@"101" userName:@"pEp Test John" isOwn:NO]];
   429 
   430     // Yellow ?
   431     clr = [session outgoingColorForMessage:msg];
   432     XCTAssertEqual(clr, PEP_rating_reliable);
   433 
   434     [session trustPersonalKey:identJohn];
   435 
   436     // This time it should be green
   437     clr = [session outgoingColorForMessage:msg];
   438     XCTAssertEqual(clr, PEP_rating_trusted);
   439 
   440     clr = [session identityRating:identJohn];
   441     XCTAssertEqual(clr, PEP_rating_trusted);
   442 }
   443 
   444 - (void)testDontEncryptForMistrusted
   445 {
   446     PEPSession *session = [PEPSession new];
   447 
   448     // Our test user :
   449     // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
   450     // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
   451     [PEPTestUtils importBundledKey:@"6FF00E97_sec.asc"];
   452 
   453     PEPIdentity *identAlice = [[PEPIdentity alloc]
   454                                initWithAddress:@"pep.test.alice@pep-project.org"
   455                                userID:ownUserId
   456                                userName:@"pEp Test Alice"
   457                                isOwn:YES
   458                                fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
   459 
   460     [session mySelf:identAlice];
   461 
   462     // pEp Test Bob (test key, don't use) <pep.test.bob@pep-project.org>
   463     // BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39
   464     [PEPTestUtils importBundledKey:@"0xC9C2EE39.asc"];
   465 
   466     PEPIdentity *identBob = [[PEPIdentity alloc]
   467                              initWithAddress:@"pep.test.bob@pep-project.org"
   468                              userID:@"42" userName:@"pEp Test Bob"
   469                              isOwn:NO
   470                              fingerPrint:@"BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39"];
   471 
   472     [session updateIdentity:identBob];
   473 
   474     // mistrust Bob
   475     [session keyMistrusted:identBob];
   476 
   477     PEPMessage *msg = [PEPMessage new];
   478     msg.from = identAlice;
   479     msg.to = @[[[PEPIdentity alloc] initWithAddress:@"pep.test.bob@pep-project.org" userID:@"42"
   480                                            userName:@"pEp Test Bob" isOwn:NO]];
   481     msg.shortMessage = @"All Green Test";
   482     msg.longMessage = @"This is a text content";
   483     msg.direction = PEP_dir_outgoing;
   484 
   485     // Gray == PEP_rating_unencrypted
   486     PEP_rating clr = [session outgoingColorForMessage:msg];
   487     XCTAssertEqual(clr, PEP_rating_unencrypted);
   488 
   489     PEPMessage *encmsg;
   490     PEP_STATUS status = [session encryptMessage:msg extra:@[] dest:&encmsg];
   491 
   492     XCTAssertEqual(status, PEP_UNENCRYPTED);
   493 
   494     XCTAssertNotEqualObjects(encmsg.attachments[0][@"mimeType"], @"application/pgp-encrypted");
   495 
   496     [self pEpCleanUp];
   497 }
   498 
   499 - (void)testRenewExpired
   500 {
   501     PEPSession *session = [PEPSession new];
   502 
   503     // Our expired test user :
   504     // pEp Test Hector (old test key don't use) <pep.test.hector@pep-project.org>
   505     [PEPTestUtils importBundledKey:@"5CB2C182_sec.asc"];
   506 
   507     PEPIdentity *identHector = [[PEPIdentity alloc]
   508                                 initWithAddress:@"pep.test.hector@pep-project.org"
   509                                 userID:@"fc2d33" userName:@"pEp Test Hector"
   510                                 isOwn:NO
   511                                 fingerPrint:@"EEA655839E347EC9E10A5DE2E80CB3FD5CB2C182"];
   512 
   513     // Check that this key is indeed expired
   514     [session updateIdentity:identHector];
   515     XCTAssertEqual(PEP_ct_key_expired, identHector.commType);
   516 
   517     PEPIdentity *identHectorOwn = [[PEPIdentity alloc]
   518                                    initWithAddress:@"pep.test.hector@pep-project.org"
   519                                    userID:ownUserId userName:@"pEp Test Hector"
   520                                    isOwn:YES
   521                                    fingerPrint:@"EEA655839E347EC9E10A5DE2E80CB3FD5CB2C182"];
   522 
   523     // Myself automatically renew expired key.
   524     [session mySelf:identHectorOwn];
   525     XCTAssertEqual(PEP_ct_pEp, identHectorOwn.commType);
   526 
   527     [self pEpCleanUpRestoringBackupNamed:@"Bob"];
   528 
   529 
   530     [self pEpSetUp:@"Bob"];
   531 
   532     PEPIdentity *_identHector = [[PEPIdentity alloc]
   533                                  initWithAddress:@"pep.test.hector@pep-project.org"
   534                                  userID:@"khkhkh" userName:@"pEp Test Hector"
   535                                  isOwn:NO
   536                                  fingerPrint:@"EEA655839E347EC9E10A5DE2E80CB3FD5CB2C182"];
   537 
   538     [session updateIdentity:_identHector];
   539     XCTAssertEqual(PEP_ct_OpenPGP_unconfirmed, _identHector.commType);
   540 }
   541 
   542 - (void)testRevoke
   543 {
   544     PEPSession *session = [PEPSession new];
   545 
   546     // Our test user :
   547     // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
   548     // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
   549     [PEPTestUtils importBundledKey:@"6FF00E97_sec.asc"];
   550     NSString *fpr = @"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97";
   551 
   552     PEPIdentity *identAlice = [[PEPIdentity alloc]
   553                                initWithAddress:@"pep.test.alice@pep-project.org"
   554                                userID:ownUserId
   555                                userName:@"pEp Test Alice"
   556                                isOwn:YES
   557                                fingerPrint:fpr];
   558 
   559     [session mySelf:identAlice];
   560 
   561     PEPIdentity *identAlice2 = [identAlice mutableCopy];
   562 
   563     // This will revoke key
   564     [session keyMistrusted:identAlice2];
   565     identAlice2.fingerPrint = nil;
   566     [session mySelf:identAlice];
   567 
   568 
   569     // Check fingerprint is different
   570     XCTAssertNotEqualObjects(identAlice2.fingerPrint, fpr);
   571 }
   572 
   573 - (void)testMailToMyself
   574 {
   575     PEPSession *session = [PEPSession new];
   576 
   577     // Our test user :
   578     // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
   579     // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
   580     [PEPTestUtils importBundledKey:@"6FF00E97_sec.asc"];
   581 
   582     PEPIdentity *identAlice = [[PEPIdentity alloc]
   583                                initWithAddress:@"pep.test.alice@pep-project.org"
   584                                userID:ownUserId
   585                                userName:@"pEp Test Alice"
   586                                isOwn:YES
   587                                fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
   588 
   589     [session mySelf:identAlice];
   590 
   591     PEPMessage *msg = [PEPMessage new];
   592     msg.from = identAlice;
   593     msg.to = @[identAlice];
   594     msg.shortMessage = @"Mail to Myself";
   595     msg.longMessage = @"This is a text content";
   596     msg.direction = PEP_dir_outgoing;
   597 
   598     PEP_rating clr = [session outgoingColorForMessage:msg];
   599     XCTAssertEqual(clr, PEP_rating_trusted_and_anonymized);
   600 
   601     PEPMessage *encmsg;
   602     PEP_STATUS status = [session encryptMessage:msg extra:@[] dest:&encmsg];
   603 
   604     XCTAssertEqual(status, PEP_STATUS_OK);
   605 
   606     NSArray* keys;
   607     PEPMessage *decmsg;
   608 
   609     clr = [session decryptMessage:encmsg dest:&decmsg keys:&keys];
   610     XCTAssertEqual(clr, PEP_rating_trusted_and_anonymized);
   611 }
   612 
   613 - (void)testEncryptedMailFromMutt
   614 {
   615     PEPSession *session = [PEPSession new];
   616 
   617     // This is the public key for test001@peptest.ch
   618     [PEPTestUtils importBundledKey:@"A3FC7F0A.asc"];
   619 
   620     // This is the secret key for test001@peptest.ch
   621     [PEPTestUtils importBundledKey:@"A3FC7F0A_sec.asc"];
   622 
   623     // Mail from mutt, already processed into message dict by the app.
   624     NSMutableDictionary *msgDict = [PEPTestUtils
   625                                     unarchiveDictionary:@"msg_to_A3FC7F0A_from_mutt.ser"].mutableCopy;
   626     [msgDict removeObjectForKey:kPepLongMessage];
   627     [msgDict removeObjectForKey:kPepLongMessageFormatted];
   628 
   629     // Also extracted "live" from the app.
   630     NSMutableDictionary *accountDict = [PEPTestUtils
   631                                         unarchiveDictionary:@"account_A3FC7F0A.ser"].mutableCopy;
   632     [accountDict removeObjectForKey:kPepCommType];
   633     [accountDict removeObjectForKey:kPepFingerprint];
   634     PEPIdentity *identMe = [[PEPIdentity alloc] initWithDictionary:accountDict];
   635 
   636     [session mySelf:identMe];
   637     XCTAssertNotNil(identMe.fingerPrint);
   638 
   639     NSArray* keys;
   640     PEPMessage *msg = [PEPMessage new];
   641     [msg setValuesForKeysWithDictionary:msgDict];
   642     PEPMessage *pepDecryptedMail;
   643     [session decryptMessage:msg dest:&pepDecryptedMail keys:&keys];
   644     XCTAssertNotNil(pepDecryptedMail.longMessage);
   645 }
   646 
   647 - (void)testOutgoingContactColor
   648 {
   649     PEPSession *session = [PEPSession new];
   650 
   651     PEPIdentity *partner1Orig = [PEPTestUtils foreignPepIdentityWithAddress:@"partner1@dontcare.me"
   652                                                                    userName:@"Partner 1"];
   653     NSString *pubKeyPartner1 = [PEPTestUtils loadResourceByName:@"partner1_F2D281C2789DD7F6_pub.asc"];
   654     XCTAssertNotNil(pubKeyPartner1);
   655     [session importKey:pubKeyPartner1];
   656 
   657     PEP_rating color = [session identityRating:partner1Orig];
   658     XCTAssertEqual(color, PEP_rating_reliable);
   659 }
   660 
   661 - (void)testMessageTrustwordsWithMySelf
   662 {
   663     PEPSession *session = [PEPSession new];
   664 
   665     PEPStringList *keys = nil;
   666     PEPMessage *decryptedDict = [self internalEncryptToMySelfKeys:&keys];
   667     XCTAssertNotNil(keys);
   668     XCTAssert(keys.count > 0);
   669 
   670     PEPIdentity *receiver = decryptedDict.to[0];
   671     [session updateIdentity:receiver];
   672     XCTAssertNotNil(receiver);
   673     PEP_STATUS trustwordsStatus;
   674 
   675     NSString *trustwords = [session getTrustwordsForMessage:decryptedDict
   676                                                    receiver:receiver
   677                                                   keysArray:keys language:@"en"
   678                                                        full:YES
   679                                             resultingStatus: &trustwordsStatus];
   680     // No trustwords with yourself
   681     XCTAssertEqual(trustwordsStatus, PEP_TRUSTWORDS_DUPLICATE_FPR);
   682     XCTAssertNil(trustwords);
   683 }
   684 
   685 - (void)testGetTrustwords
   686 {
   687     PEPSession *session = [PEPSession new];
   688 
   689     PEPIdentity *partner1Orig = [[PEPIdentity alloc]
   690                                  initWithAddress:@"partner1@dontcare.me" userID:@"partner1"
   691                                  userName:@"partner1"
   692                                  isOwn:NO fingerPrint:@"F0CD3F7B422E5D587ABD885BF2D281C2789DD7F6"];
   693 
   694     PEPIdentity *meOrig = [[PEPIdentity alloc]
   695                            initWithAddress:@"me@dontcare.me" userID:@"me"
   696                            userName:@"me"
   697                            isOwn:NO fingerPrint:@"CC1F73F6FB774BF08B197691E3BFBCA9248FC681"];
   698 
   699     NSString *pubKeyPartner1 = [PEPTestUtils loadResourceByName:@"partner1_F2D281C2789DD7F6_pub.asc"];
   700     XCTAssertNotNil(pubKeyPartner1);
   701     NSString *pubKeyMe = [PEPTestUtils loadResourceByName:@"meATdontcare_E3BFBCA9248FC681_pub.asc"];
   702     XCTAssertNotNil(pubKeyMe);
   703     NSString *secKeyMe = [PEPTestUtils loadResourceByName:@"meATdontcare_E3BFBCA9248FC681_sec.asc"];
   704     XCTAssertNotNil(secKeyMe);
   705 
   706     NSString *trustwordsFull = [session getTrustwordsIdentity1:meOrig identity2:partner1Orig
   707                                                       language:nil full:YES];
   708     XCTAssertEqualObjects(trustwordsFull,
   709                           @"EMERSON GASPER TOKENISM BOLUS COLLAGE DESPISE BEDDED ENCRYPTION IMAGINE BEDFORD");
   710 
   711     NSString *trustwordsFullEnglish = [session getTrustwordsIdentity1:meOrig identity2:partner1Orig
   712                                                              language:@"en" full:YES];
   713     XCTAssertEqualObjects(trustwordsFullEnglish, trustwordsFull);
   714 
   715     NSString *trustwordsUndefined = [session getTrustwordsIdentity1:meOrig identity2:partner1Orig
   716                                                            language:@"ZZ" full:YES];
   717     XCTAssertNil(trustwordsUndefined);
   718 }
   719 
   720 #pragma mark - configUnencryptedSubject
   721 
   722 - (void)testConfigUnencryptedSubject
   723 {
   724     // Setup Config to encrypt subject
   725     [PEPObjCAdapter setUnecryptedSubjectEnabled:NO];
   726 
   727     // Write mail to yourself ...
   728     PEPMessage *encMessage = [self mailWrittenToMySelf];
   729 
   730     // ... and assert subject is encrypted
   731     XCTAssertEqualObjects(encMessage.shortMessage, @"p≡p", @"Subject should be encrypted");
   732 }
   733 
   734 - (void)testConfigUnencryptedSubject_encryptedSubjectDisabled
   735 {
   736     // Setup Config to not encrypt subject
   737     [PEPObjCAdapter setUnecryptedSubjectEnabled:YES];
   738 
   739     // Write mail to yourself ...
   740     PEPMessage *encMessage = [self mailWrittenToMySelf];
   741 
   742     // ... and assert the subject is not encrypted
   743     XCTAssertNotEqualObjects(encMessage.shortMessage, @"p≡p", @"Subject should not be encrypted");
   744 }
   745 
   746 - (void)testStringToRating
   747 {
   748     PEPSession *session = [PEPSession new];
   749     XCTAssertEqual([session ratingFromString:@"cannot_decrypt"], PEP_rating_cannot_decrypt);
   750     XCTAssertEqual([session ratingFromString:@"have_no_key"], PEP_rating_have_no_key);
   751     XCTAssertEqual([session ratingFromString:@"unencrypted"], PEP_rating_unencrypted);
   752     XCTAssertEqual([session ratingFromString:@"unencrypted_for_some"],
   753                    PEP_rating_unencrypted_for_some);
   754     XCTAssertEqual([session ratingFromString:@"unreliable"], PEP_rating_unreliable);
   755     XCTAssertEqual([session ratingFromString:@"reliable"], PEP_rating_reliable);
   756     XCTAssertEqual([session ratingFromString:@"trusted"], PEP_rating_trusted);
   757     XCTAssertEqual([session ratingFromString:@"trusted_and_anonymized"],
   758                    PEP_rating_trusted_and_anonymized);
   759     XCTAssertEqual([session ratingFromString:@"fully_anonymous"], PEP_rating_fully_anonymous);
   760     XCTAssertEqual([session ratingFromString:@"mistrust"], PEP_rating_mistrust);
   761     XCTAssertEqual([session ratingFromString:@"b0rken"], PEP_rating_b0rken);
   762     XCTAssertEqual([session ratingFromString:@"under_attack"], PEP_rating_under_attack);
   763     XCTAssertEqual([session ratingFromString:@"undefined"], PEP_rating_undefined);
   764     XCTAssertEqual([session ratingFromString:@"does not exist111"], PEP_rating_undefined);
   765 }
   766 
   767 - (void)testRatingToString
   768 {
   769     PEPSession *session = [PEPSession new];
   770     XCTAssertEqualObjects([session stringFromRating:PEP_rating_cannot_decrypt], @"cannot_decrypt");
   771     XCTAssertEqualObjects([session stringFromRating:PEP_rating_have_no_key], @"have_no_key");
   772     XCTAssertEqualObjects([session stringFromRating:PEP_rating_unencrypted], @"unencrypted");
   773     XCTAssertEqualObjects([session stringFromRating:PEP_rating_unencrypted_for_some],
   774                           @"unencrypted_for_some");
   775     XCTAssertEqualObjects([session stringFromRating:PEP_rating_unreliable], @"unreliable");
   776     XCTAssertEqualObjects([session stringFromRating:PEP_rating_reliable], @"reliable");
   777     XCTAssertEqualObjects([session stringFromRating:PEP_rating_trusted], @"trusted");
   778     XCTAssertEqualObjects([session stringFromRating:PEP_rating_trusted_and_anonymized],
   779                           @"trusted_and_anonymized");
   780     XCTAssertEqualObjects([session stringFromRating:PEP_rating_fully_anonymous],
   781                           @"fully_anonymous");
   782     XCTAssertEqualObjects([session stringFromRating:PEP_rating_mistrust], @"mistrust");
   783     XCTAssertEqualObjects([session stringFromRating:PEP_rating_b0rken], @"b0rken");
   784     XCTAssertEqualObjects([session stringFromRating:PEP_rating_under_attack], @"under_attack");
   785     XCTAssertEqualObjects([session stringFromRating:PEP_rating_undefined], @"undefined");
   786     XCTAssertEqualObjects([session stringFromRating:500], @"undefined");
   787 }
   788 
   789 - (void)testIsPEPUser
   790 {
   791     PEPSession *session = [PEPSession new];
   792 
   793     PEPIdentity *identMe = [[PEPIdentity alloc]
   794                             initWithAddress:@"me-myself-and-i@pep-project.org"
   795                             userID:@"me-myself-and-i"
   796                             userName:@"pEp Test Alice"
   797                             isOwn:YES];
   798     [session mySelf:identMe];
   799     XCTAssertNotNil(identMe.fingerPrint);
   800 
   801     // PEP_CANNOT_FIND_PERSON == 902
   802     NSError *error;
   803     XCTAssertTrue([session isPEPUser:identMe error:&error]);
   804     XCTAssertNil(error);
   805 }
   806 
   807 #pragma mark - Helpers
   808 
   809 - (PEPIdentity *)checkImportingKeyFilePath:(NSString *)filePath address:(NSString *)address
   810                                     userID:(NSString *)userID
   811                                fingerPrint:(NSString *)fingerPrint
   812 {
   813     PEPSession *session = [PEPSession new];
   814 
   815     [PEPTestUtils importBundledKey:filePath];
   816 
   817     // Our test user:
   818     PEPIdentity *identTest = [[PEPIdentity alloc]
   819                               initWithAddress:address
   820                               userID:userID
   821                               userName:[NSString stringWithFormat:@"Some User Name %@", userID]
   822                               isOwn:NO];
   823 
   824     [session updateIdentity:identTest];
   825     XCTAssertNotNil(identTest.fingerPrint);
   826     XCTAssertEqualObjects(identTest.fingerPrint, fingerPrint);
   827 
   828     return identTest;
   829 }
   830 
   831 - (PEPIdentity *)checkMySelfImportingKeyFilePath:(NSString *)filePath address:(NSString *)address
   832                                           userID:(NSString *)userID
   833                                      fingerPrint:(NSString *)fingerPrint
   834 {
   835     PEPSession *session = [PEPSession new];
   836 
   837     [PEPTestUtils importBundledKey:filePath];
   838 
   839     // Our test user:
   840     PEPIdentity *identTest = [[PEPIdentity alloc]
   841                               initWithAddress:address
   842                               userID:userID
   843                               userName:[NSString stringWithFormat:@"Some User Name %@", userID]
   844                               isOwn:YES
   845                               fingerPrint: fingerPrint];
   846 
   847     [session mySelf:identTest];
   848     XCTAssertNotNil(identTest.fingerPrint);
   849     XCTAssertEqualObjects(identTest.fingerPrint, fingerPrint);
   850 
   851     return identTest;
   852 }
   853 
   854 /**
   855  Verifies that a partner ID is really a correct Identity.
   856  Usually used on identities imported as keys, since the engine has problems with them.
   857  */
   858 - (void)updateAndVerifyPartnerIdentity:(PEPIdentity *)partnerIdentity session:(PEPSession *)session
   859 {
   860     XCTAssertNotNil(partnerIdentity.fingerPrint);
   861     [session updateIdentity:partnerIdentity];
   862     XCTAssertNotNil(partnerIdentity.fingerPrint);
   863     NSString *fingerprint = partnerIdentity.fingerPrint;
   864     partnerIdentity.fingerPrint = nil;
   865     [session updateIdentity:partnerIdentity];
   866     XCTAssertNotNil(partnerIdentity.fingerPrint);
   867     XCTAssertEqualObjects(partnerIdentity.fingerPrint, fingerprint);
   868 }
   869 
   870 - (PEPMessage *)mailWrittenToMySelf
   871 {
   872     PEPSession *session = [PEPSession new];
   873 
   874     // Write a e-mail to yourself ...
   875     PEPIdentity *me = [PEPTestUtils ownPepIdentityWithAddress:@"me@peptest.ch"
   876                                                      userName:@"userName"];
   877     [session mySelf:me];
   878 
   879     NSString *shortMessage = @"Subject";
   880     NSString *longMessage = @"Oh, this is a long body text!";
   881     PEPMessage *mail = [PEPTestUtils mailFrom:me
   882                                       toIdent:me
   883                                  shortMessage:shortMessage
   884                                   longMessage:longMessage
   885                                      outgoing:YES];
   886     PEPMessage *encMessage;
   887     [session encryptMessage:mail identity:me dest:&encMessage];
   888 
   889     return encMessage;
   890 }
   891 
   892 - (PEPMessage *)internalEncryptToMySelfKeys:(PEPStringList **)keys
   893 {
   894     PEPSession *session = [PEPSession new];
   895     
   896     PEPIdentity *me = [PEPTestUtils ownPepIdentityWithAddress:@"me@peptest.ch"
   897                                                      userName:@"userName"];
   898     [session mySelf:me];
   899     XCTAssertNotNil(me.fingerPrint);
   900 
   901     // Create draft
   902     NSString *shortMessage = @"Subject";
   903     NSString *longMessage = @"Oh, this is a long body text!";
   904     PEPMessage *mail = [PEPTestUtils mailFrom:me toIdent:me shortMessage:shortMessage longMessage:longMessage outgoing:YES];
   905 
   906     PEPMessage *encMessage;
   907     PEP_STATUS status = [session encryptMessage:mail identity:me dest:&encMessage];
   908     XCTAssertEqual(status, 0);
   909     XCTAssertEqualObjects(encMessage.shortMessage, @"p≡p");
   910 
   911     PEPMessage *unencDict;
   912     PEP_rating rating = [session decryptMessage:encMessage dest:&unencDict keys:keys];
   913     XCTAssertGreaterThanOrEqual(rating, PEP_rating_reliable);
   914 
   915     XCTAssertEqualObjects(unencDict.shortMessage, shortMessage);
   916     XCTAssertEqualObjects(unencDict.longMessage, longMessage);
   917 
   918     return unencDict;
   919 }
   920 
   921 - (void)pEpCleanUpRestoringBackupNamed:(NSString *)backup {
   922     [PEPTestUtils deleteWorkFilesAfterBackingUpWithBackupName:backup];
   923 }
   924 
   925 - (void)pEpCleanUp
   926 {
   927     [PEPSession cleanup];
   928     [self pEpCleanUpRestoringBackupNamed:NULL];
   929 }
   930 
   931 - (void)pEpSetUp:(NSString *)restore
   932 {
   933     // Must be the first thing you do before using anything pEp-related
   934     // ... but this is now done in session, with a "dispatch_once"
   935     // [PEPObjCAdapter setupTrustWordsDB:[NSBundle bundleForClass:[self class]]];
   936 
   937     [PEPTestUtils deleteWorkFilesAfterBackingUpWithBackupName:nil];
   938     [PEPTestUtils restoreWorkFilesFromBackupNamed:restore];
   939 }
   940 
   941 - (void)pEpSetUp
   942 {
   943     [self pEpSetUp:NULL];
   944 }
   945 
   946 - (void)helperXEncStatusForOutgoingEncryptdMailToSelf:(BOOL)toSelf
   947                                        expectedRating:(PEP_rating)expectedRating
   948 {
   949     PEPSession *session = [PEPSession new];
   950 
   951     // Partner pubkey for the test:
   952     // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
   953     // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
   954     [PEPTestUtils importBundledKey:@"0x6FF00E97.asc"];
   955 
   956     PEPIdentity *identAlice = [[PEPIdentity alloc]
   957                                initWithAddress:@"pep.test.alice@pep-project.org"
   958                                userID:ownUserId
   959                                userName:@"pEp Test Alice"
   960                                isOwn:NO
   961                                fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
   962     [self updateAndVerifyPartnerIdentity:identAlice session:session];
   963 
   964     PEPIdentity *identMe = [[PEPIdentity alloc]
   965                                initWithAddress:@"me-myself-and-i@pep-project.org"
   966                                userID:@"me-myself-and-i"
   967                                userName:@"pEp Test Alice"
   968                                isOwn:YES];
   969     [session mySelf:identMe];
   970     XCTAssertNotNil(identMe.fingerPrint);
   971 
   972     PEPMessage *msg = [PEPMessage new];
   973     msg.from = identMe;
   974     msg.to = @[identAlice];
   975     msg.shortMessage = @"Mail to Alice";
   976     msg.longMessage = @"Alice?";
   977     msg.direction = PEP_dir_outgoing;
   978 
   979     PEP_rating clr = [session outgoingColorForMessage:msg];
   980     XCTAssertEqual(clr, PEP_rating_reliable);
   981 
   982     PEPMessage *encMsg;
   983 
   984     PEP_STATUS statusEnc = PEP_VERSION_MISMATCH;
   985     if (toSelf) {
   986         statusEnc = [session encryptMessage:msg identity:identMe dest:&encMsg];
   987     } else {
   988          statusEnc = [session encryptMessage:msg extra:@[] dest:&encMsg];
   989     }
   990 
   991     XCTAssertEqual(statusEnc, PEP_STATUS_OK);
   992 
   993     XCTAssertNotNil(encMsg);
   994 
   995     PEPMessage *decMsg;
   996     PEPStringList *keys;
   997     PEP_rating pEpRating = [session decryptMessage:encMsg dest:&decMsg keys:&keys];
   998     XCTAssertEqual(pEpRating, expectedRating);
   999     XCTAssertNotNil(decMsg);
  1000 
  1001     NSArray * encStatusField = nil;
  1002     for (NSArray *field in decMsg.optionalFields) {
  1003         NSString *header = [field[0] lowercaseString];
  1004         if ([header isEqualToString:@"x-encstatus"]) {
  1005             encStatusField = field;
  1006         }
  1007     }
  1008     XCTAssertNotNil(encStatusField);
  1009     if (encStatusField) {
  1010         PEP_rating outgoingRating = [session ratingFromString:encStatusField[1]];
  1011         XCTAssertEqual(outgoingRating, expectedRating);
  1012     }
  1013 }
  1014 
  1015 - (void)testXEncStatusForOutgoingEncryptedMail
  1016 {
  1017     [self helperXEncStatusForOutgoingEncryptdMailToSelf:NO expectedRating:PEP_rating_reliable];
  1018 }
  1019 
  1020 - (void)testXEncStatusForOutgoingSelfEncryptedMail
  1021 {
  1022     [self helperXEncStatusForOutgoingEncryptdMailToSelf:YES
  1023                                          expectedRating:PEP_rating_trusted_and_anonymized];
  1024 }
  1025 
  1026 @end