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