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