pEpObjCTests/PEPSessionTest.m
author Dirk Zimmermann <dirk@pep-project.org>
Wed, 16 May 2018 16:18:01 +0200
changeset 615 52097b5d3b72
parent 613 f33d0b187aea
child 617 060f20084fdb
permissions -rw-r--r--
IOSAD-93 make it clear we test for error (illegal value)
     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 #import "PEPAttachment.h"
    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 setUnEncryptedSubjectEnabled:NO];
    28 
    29     [self pEpCleanUp];
    30 }
    31 
    32 - (void)tearDown
    33 {
    34     [self pEpCleanUp];
    35     [super tearDown];
    36 }
    37 
    38 - (void)testSyncSession
    39 {
    40     PEPSession *session = [PEPSession new];
    41 
    42     // Dummy to set up the DB, since this is currenty only triggered by session use,
    43     // which PEPObjCAdapter.startSync does not trigger.
    44     NSError *error = nil;
    45     XCTAssertNotNil([session getLogWithError:&error]);
    46     XCTAssertNil(error);
    47 
    48     PEPTestSyncDelegate *syncDelegate = [[PEPTestSyncDelegate alloc] init];
    49 
    50     // This should attach session just created
    51     [PEPObjCAdapter startSync:syncDelegate];
    52 
    53     PEPIdentity *identMe = [[PEPIdentity alloc]
    54                             initWithAddress:@"pep.test.iosgenkey@pep-project.org"
    55                             userID:@"Me"
    56                             userName:@"pEp Test iOS GenKey"
    57                             isOwn:YES];
    58 
    59     XCTAssertTrue([session mySelf:identMe error:&error]);
    60     XCTAssertNil(error);
    61 
    62     bool res = [syncDelegate waitUntilSent:1];
    63 
    64     // Can't currently work, engine doesn't contain sync.
    65     XCTAssertFalse(res);
    66 
    67     // This should detach session just created
    68     [PEPObjCAdapter stopSync];
    69 }
    70 
    71 - (void)testTrustWords
    72 {
    73     PEPSession *session = [PEPSession new];
    74 
    75     NSError *error = nil;
    76     NSArray *trustwords = [session
    77                            trustwordsForFingerprint:@"DB47DB47DB47DB47DB47DB47DB47DB47DB47DB47"
    78                            languageID:@"en"
    79                            shortened:false
    80                            error:&error];
    81     XCTAssertNil(error);
    82     XCTAssertEqual([trustwords count], 10);
    83 
    84     for(id word in trustwords)
    85         XCTAssertEqualObjects(word, @"BAPTISMAL");
    86 }
    87 
    88 - (void)testGenKey
    89 {
    90     PEPSession *session = [PEPSession new];
    91 
    92     PEPIdentity *identMe = [[PEPIdentity alloc]
    93                             initWithAddress:@"pep.test.iosgenkey@pep-project.org"
    94                             userID:@"Me"
    95                             userName:@"pEp Test iOS GenKey"
    96                             isOwn:YES];
    97 
    98     NSError *error = nil;
    99     XCTAssertTrue([session mySelf:identMe error:&error]);
   100     XCTAssertNil(error);
   101 
   102     XCTAssertNotNil(identMe.fingerPrint);
   103     XCTAssertNotEqual(identMe.commType, PEP_ct_unknown);
   104 
   105     XCTAssertTrue([identMe isPEPUser:session error:&error]);
   106 }
   107 
   108 - (void)testMySelfCommType
   109 {
   110     PEPSession *session = [PEPSession new];
   111 
   112     PEPIdentity *identMe = [[PEPIdentity alloc]
   113                             initWithAddress:@"pep.test.iosgenkey@pep-project.org"
   114                             userID:@"pep.test.iosgenkey@pep-project.org_userID"
   115                             userName:@"pEp Test iOS GenKey"
   116                             isOwn:YES];
   117 
   118     NSError *error = nil;
   119     XCTAssertTrue([session mySelf:identMe error:&error]);
   120     XCTAssertNil(error);
   121 
   122     XCTAssertNotNil(identMe.fingerPrint);
   123     XCTAssertNotEqual(identMe.commType, PEP_ct_unknown);
   124 
   125     XCTAssertTrue([identMe isPEPUser:session error:&error]);
   126 
   127     dispatch_queue_t queue = dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0);
   128     dispatch_sync(queue, ^{
   129         NSError *innerError = nil;
   130         PEPSession *session2 = [PEPSession new];
   131 
   132         // Now simulate an update from the app, which usually only caches
   133         // kPepUsername, kPepAddress and optionally kPepUserID.
   134         PEPIdentity *identMe2 = [[PEPIdentity alloc]
   135                                  initWithAddress:identMe.address
   136                                  userID:identMe.userID
   137                                  userName:identMe.userName
   138                                  isOwn:NO];
   139 
   140         XCTAssertTrue([session2 mySelf:identMe2 error:&innerError]);
   141         XCTAssertNil(innerError);
   142 
   143         XCTAssertNotNil(identMe2.fingerPrint);
   144         XCTAssertTrue([identMe2 isPEPUser:session error:&innerError]);
   145         XCTAssertEqualObjects(identMe2.fingerPrint, identMe.fingerPrint);
   146 
   147         // Now pretend the app only knows kPepUsername and kPepAddress
   148         PEPIdentity *identMe3 = [PEPTestUtils foreignPepIdentityWithAddress:identMe.address
   149                                                                    userName:identMe.userName];
   150         XCTAssertTrue([session2 mySelf:identMe3 error:&innerError]);
   151         XCTAssertNil(innerError);
   152 
   153         XCTAssertNotNil(identMe3.fingerPrint);
   154         XCTAssertTrue([identMe3 isPEPUser:session error:&innerError]);
   155         XCTAssertEqualObjects(identMe3.fingerPrint, identMe.fingerPrint);
   156 
   157         XCTAssertEqualObjects(identMe.address, identMe2.address);
   158         XCTAssertEqualObjects(identMe.address, identMe3.address);
   159         XCTAssertEqual(identMe.commType, identMe2.commType);
   160         XCTAssertEqual(identMe.commType, identMe3.commType);
   161     });
   162 }
   163 
   164 - (void)testPartnerWithoutFingerPrint
   165 {
   166     PEPSession *session = [PEPSession new];
   167 
   168     PEPIdentity *identRandom = [[PEPIdentity alloc]
   169                                 initWithAddress:@"does_not_exist@example.com"
   170                                 userID:@"got_out"
   171                                 userName:@"No Way Not Even Alice"
   172                                 isOwn:NO];
   173 
   174     NSError *error = nil;
   175     XCTAssertTrue([session updateIdentity:identRandom error:&error]);
   176     XCTAssertNil(error);
   177     XCTAssertNil(identRandom.fingerPrint);
   178 }
   179 
   180 - (void)testImportPartnerKeys
   181 {
   182     XCTAssertNotNil([self checkImportingKeyFilePath:@"6FF00E97_sec.asc"
   183                                             address:@"pep.test.alice@pep-project.org"
   184                                              userID:@"This Is Alice"
   185                                         fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
   186                                             session: nil]);
   187 
   188     XCTAssertNotNil([self checkImportingKeyFilePath:@"0xC9C2EE39.asc"
   189                                             address:@"pep.test.bob@pep-project.org"
   190                                              userID:@"This Is Bob"
   191                                         fingerPrint:@"BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39"
   192                                             session: nil]);
   193 }
   194 
   195 - (void)testIdentityRating
   196 {
   197     PEPSession *session = [PEPSession new];
   198 
   199     PEPIdentity *me = [self
   200                        checkMySelfImportingKeyFilePath:@"6FF00E97_sec.asc"
   201                        address:@"pep.test.alice@pep-project.org"
   202                        userID:@"Alice_User_ID"
   203                        fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
   204                        session:session];
   205     XCTAssertEqual([self ratingForIdentity:me session:session], PEP_rating_trusted_and_anonymized);
   206 
   207     PEPIdentity *alice = [self
   208                           checkImportingKeyFilePath:@"6FF00E97_sec.asc"
   209                           address:@"pep.test.alice@pep-project.org"
   210                           userID:@"This Is Alice"
   211                           fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
   212                           session: session];
   213     XCTAssertNotNil(alice);
   214     XCTAssertEqual([self ratingForIdentity:alice session:session], PEP_rating_reliable);
   215 }
   216 
   217 /** ENGINE-409 */
   218 - (void)testIdentityRatingMistrustReset
   219 {
   220     PEPSession *session = [PEPSession new];
   221 
   222     PEPIdentity *me = [[PEPIdentity alloc]
   223                        initWithAddress:@"me@example.org"
   224                        userID:@"me_myself"
   225                        userName:@"Me Me"
   226                        isOwn:YES];
   227 
   228     NSError *error = nil;
   229     XCTAssertTrue([session mySelf:me error:&error]);
   230     XCTAssertNil(error);
   231 
   232     XCTAssertNotNil(me.fingerPrint);
   233     XCTAssertEqual([self ratingForIdentity:me session:session], PEP_rating_trusted_and_anonymized);
   234 
   235     PEPIdentity *alice = [self
   236                           checkImportingKeyFilePath:@"6FF00E97_sec.asc"
   237                           address:@"pep.test.alice@pep-project.org"
   238                           userID:@"This Is Alice"
   239                           fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
   240                           session: session];
   241     XCTAssertNotNil(alice);
   242     XCTAssertEqual([self ratingForIdentity:alice session:session], PEP_rating_reliable);
   243 
   244     XCTAssertTrue([session keyMistrusted:alice error:&error]);
   245     XCTAssertNil(error);
   246     XCTAssertEqual([self ratingForIdentity:alice session:session], PEP_rating_have_no_key);
   247 
   248     XCTAssertTrue([session keyResetTrust:alice error:&error]);
   249     XCTAssertNil(error);
   250     XCTAssertEqual([self ratingForIdentity:alice session:session], PEP_rating_reliable);
   251 }
   252 
   253 - (void)testIdentityRatingTrustResetMistrustUndo
   254 {
   255     PEPSession *session = [PEPSession new];
   256 
   257     PEPIdentity *me = [[PEPIdentity alloc]
   258                        initWithAddress:@"me@example.org"
   259                        userID:@"me_myself"
   260                        userName:@"Me Me"
   261                        isOwn:YES];
   262 
   263     NSError *error = nil;
   264     XCTAssertTrue([session mySelf:me error:&error]);
   265     XCTAssertNil(error);
   266 
   267     XCTAssertNotNil(me.fingerPrint);
   268     XCTAssertEqual([self ratingForIdentity:me session:session], PEP_rating_trusted_and_anonymized);
   269 
   270     PEPIdentity *alice = [self
   271                           checkImportingKeyFilePath:@"6FF00E97_sec.asc"
   272                           address:@"pep.test.alice@pep-project.org"
   273                           userID:@"This Is Alice"
   274                           fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
   275                           session: session];
   276     XCTAssertNotNil(alice);
   277     XCTAssertEqual([self ratingForIdentity:alice session:session], PEP_rating_reliable);
   278 
   279     XCTAssertTrue([session trustPersonalKey:alice error:&error]);
   280     XCTAssertNil(error);
   281     XCTAssertEqual([self ratingForIdentity:alice session:session], PEP_rating_trusted);
   282 
   283     XCTAssertTrue([session keyResetTrust:alice error:&error]);
   284     XCTAssertNil(error);
   285     XCTAssertEqual([self ratingForIdentity:alice session:session], PEP_rating_reliable);
   286 
   287     XCTAssertTrue([session keyMistrusted:alice error:&error]);
   288     XCTAssertNil(error);
   289     XCTAssertEqual([self ratingForIdentity:alice session:session], PEP_rating_have_no_key);
   290 
   291     XCTAssertTrue([session undoLastMistrustWithError:&error]);
   292     XCTAssertNil(error);
   293 
   294     // After ENGINE-371 has been fixed, this should be just PEP_rating_reliable
   295     XCTAssertEqual([self ratingForIdentity:alice session:session], PEP_rating_trusted);
   296 
   297     XCTAssertTrue([session trustPersonalKey:alice error:&error]);
   298     XCTAssertNil(error);
   299     XCTAssertEqual([self ratingForIdentity:alice session:session], PEP_rating_trusted);
   300 
   301     XCTAssertTrue([session keyResetTrust:alice error:&error]);
   302     XCTAssertNil(error);
   303     XCTAssertEqual([self ratingForIdentity:alice session:session], PEP_rating_reliable);
   304 }
   305 
   306 /** ENGINE-384 */
   307 - (void)testIdentityRatingCrash
   308 {
   309     PEPSession *session = [PEPSession new];
   310 
   311     PEPIdentity *me = [[PEPIdentity alloc]
   312                        initWithAddress:@"me@example.org"
   313                        userID:@"me_myself"
   314                        userName:@"Me Me"
   315                        isOwn:YES];
   316     NSError *error = nil;
   317     XCTAssertTrue([session mySelf:me error:&error]);
   318     XCTAssertNil(error);
   319 
   320     XCTAssertNotNil(me.fingerPrint);
   321     XCTAssertEqual([self ratingForIdentity:me session:session], PEP_rating_trusted_and_anonymized);
   322 
   323     PEPIdentity *alice = [self
   324                           checkImportingKeyFilePath:@"6FF00E97_sec.asc"
   325                           address:@"pep.test.alice@pep-project.org"
   326                           userID:@"This Is Alice"
   327                           fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
   328                           session: session];
   329     XCTAssertNotNil(alice);
   330     XCTAssertEqual([self ratingForIdentity:alice session:session], PEP_rating_reliable);
   331 
   332     XCTAssertTrue([session trustPersonalKey:alice error:&error]);
   333     XCTAssertNil(error);
   334     XCTAssertEqual([self ratingForIdentity:alice session:session], PEP_rating_trusted);
   335 
   336     XCTAssertTrue([session keyResetTrust:alice error:&error]);
   337     XCTAssertNil(error);
   338     XCTAssertEqual([self ratingForIdentity:alice session:session], PEP_rating_reliable);
   339 
   340     XCTAssertTrue([session keyMistrusted:alice error:&error]);
   341     XCTAssertNil(error);
   342     XCTAssertEqual([self ratingForIdentity:alice session:session], PEP_rating_have_no_key);
   343 
   344     XCTAssertTrue([session undoLastMistrustWithError:&error]);
   345     XCTAssertNil(error);
   346 
   347     XCTAssertTrue([session trustPersonalKey:alice error:&error]);
   348     XCTAssertNil(error);
   349     XCTAssertEqual([self ratingForIdentity:alice session:session], PEP_rating_trusted);
   350 
   351     XCTAssertTrue([session keyResetTrust:alice error:&error]);
   352     XCTAssertNil(error);
   353     XCTAssertEqual([self ratingForIdentity:alice session:session], PEP_rating_reliable);
   354 
   355     // This line provoked the crash
   356     XCTAssertTrue([session trustPersonalKey:alice error:&error]);
   357     XCTAssertNil(error);
   358 }
   359 
   360 /**
   361  Try to provoke a SQLITE_BUSY (ENGINE-374)
   362  */
   363 - (void)testIdentityRatingTrustResetMistrustUndoBusy
   364 {
   365     PEPSession *session = [PEPSession new];
   366 
   367     PEPIdentity *me = [[PEPIdentity alloc]
   368                        initWithAddress:@"me@example.org"
   369                        userID:@"me_myself"
   370                        userName:@"Me Me"
   371                        isOwn:YES];
   372     NSError *error = nil;
   373     XCTAssertTrue([session mySelf:me error:&error]);
   374     XCTAssertNil(error);
   375 
   376     XCTAssertNotNil(me.fingerPrint);
   377     XCTAssertEqual([self ratingForIdentity:me session:session], PEP_rating_trusted_and_anonymized);
   378 
   379     PEPIdentity *alice = [self
   380                           checkImportingKeyFilePath:@"6FF00E97_sec.asc"
   381                           address:@"pep.test.alice@pep-project.org"
   382                           userID:@"This Is Alice"
   383                           fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
   384                           session: session];
   385     XCTAssertNotNil(alice);
   386     XCTAssertEqual([self ratingForIdentity:alice session:session], PEP_rating_reliable);
   387 
   388     void (^encryptingBlock)(void) = ^{
   389         PEPSession *innerSession = [PEPSession new];
   390         PEPMessage *msg = [PEPMessage new];
   391         msg.from = me;
   392         msg.to = @[alice];
   393         msg.shortMessage = @"The subject";
   394         msg.longMessage = @"Lots and lots of text";
   395         msg.direction = PEP_dir_outgoing;
   396 
   397         PEP_STATUS status;
   398         NSError *error = nil;
   399         PEPMessage *encMsg = [innerSession
   400                               encryptMessage:msg
   401                               forSelf:me
   402                               extraKeys:nil
   403                               status:&status error:&error];
   404         XCTAssertEqual(status, PEP_STATUS_OK);
   405         XCTAssertNotNil(encMsg);
   406     };
   407 
   408     dispatch_group_t backgroundGroup = dispatch_group_create();
   409     dispatch_group_async(backgroundGroup,
   410                          dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), encryptingBlock);
   411 
   412     XCTAssertTrue([session trustPersonalKey:alice error:&error]);
   413     XCTAssertNil(error);
   414     XCTAssertEqual([self ratingForIdentity:alice session:session], PEP_rating_trusted);
   415 
   416     XCTAssertTrue([session keyResetTrust:alice error:&error]);
   417     XCTAssertNil(error);
   418     XCTAssertEqual([self ratingForIdentity:alice session:session], PEP_rating_reliable);
   419 
   420     XCTAssertTrue([session keyMistrusted:alice error:&error]);
   421     XCTAssertNil(error);
   422     XCTAssertEqual([self ratingForIdentity:alice session:session], PEP_rating_have_no_key);
   423 
   424     XCTAssertTrue([session undoLastMistrustWithError:&error]);
   425     XCTAssertNil(error);
   426 
   427     // After ENGINE-371 has been fixed, this should be just PEP_rating_reliable
   428     XCTAssertEqual([self ratingForIdentity:alice session:session], PEP_rating_trusted);
   429 
   430     XCTAssertTrue([session trustPersonalKey:alice error:&error]);
   431     XCTAssertNil(error);
   432     XCTAssertEqual([self ratingForIdentity:alice session:session], PEP_rating_trusted);
   433 
   434     XCTAssertTrue([session keyResetTrust:alice error:&error]);
   435     XCTAssertNil(error);
   436     XCTAssertEqual([self ratingForIdentity:alice session:session], PEP_rating_reliable);
   437 
   438     dispatch_group_wait(backgroundGroup, DISPATCH_TIME_FOREVER);
   439 }
   440 
   441 - (void)testOutgoingColors
   442 {
   443     PEPSession *session = [PEPSession new];
   444 
   445     // Our test user :
   446     // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
   447     // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
   448     XCTAssertTrue([PEPTestUtils importBundledKey:@"6FF00E97_sec.asc" session:session]);
   449 
   450     // Our test user :
   451     PEPIdentity *identAlice = [self
   452                                checkMySelfImportingKeyFilePath:@"6FF00E97_sec.asc"
   453                                address:@"pep.test.alice@pep-project.org"
   454                                userID:@"Alice_User_ID"
   455                                fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
   456                                session:session];
   457 
   458     //Message
   459 
   460     {
   461         PEPIdentity *identUnknownBob = [[PEPIdentity alloc]
   462                                         initWithAddress:@"pep.test.unknown.bob@pep-project.org"
   463                                         userID:@"4242" userName:@"pEp Test Bob Unknown"
   464                                         isOwn:NO];
   465 
   466         PEPMessage *msgGray = [PEPMessage new];
   467         msgGray.from = identAlice;
   468         msgGray.to = @[identUnknownBob];
   469         msgGray.shortMessage = @"All Gray Test";
   470         msgGray.longMessage = @"This is a text content";
   471         msgGray.direction = PEP_dir_outgoing;
   472 
   473         NSError *error = nil;
   474 
   475         // Test with unknown Bob
   476         NSNumber *numRating = [session outgoingRatingForMessage:msgGray error:&error];
   477         XCTAssertNotNil(numRating);
   478         XCTAssertNil(error);
   479         XCTAssertEqual(numRating.pEpRating, PEP_rating_unencrypted);
   480     }
   481 
   482     PEPIdentity *identBob = [self
   483                              checkImportingKeyFilePath:@"0xC9C2EE39.asc"
   484                              address:@"pep.test.bob@pep-project.org"
   485                              userID:@"42"
   486                              fingerPrint:@"BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39"
   487                              session: session];
   488     XCTAssertNotNil(identBob);
   489 
   490     PEPMessage *msg = [PEPMessage new];
   491     msg.from = identAlice;
   492     msg.to = @[identBob];
   493     msg.shortMessage = @"All Gray Test";
   494     msg.longMessage = @"This is a text content";
   495     msg.direction = PEP_dir_outgoing;
   496 
   497     NSError *error = nil;
   498 
   499     // Should be yellow, since no handshake happened.
   500     NSNumber *numRating = [session outgoingRatingForMessage:msg error:&error];
   501     XCTAssertNotNil(numRating);
   502     XCTAssertNil(error);
   503     XCTAssertEqual(numRating.pEpRating, PEP_rating_reliable);
   504 
   505     PEP_rating rating = [self ratingForIdentity:identBob session:session];
   506     XCTAssertEqual(rating, PEP_rating_reliable);
   507 
   508     // Let' say we got that handshake, set PEP_ct_confirmed in Bob's identity
   509     XCTAssertTrue([session trustPersonalKey:identBob error:&error]);
   510     XCTAssertNil(error);
   511 
   512     // This time it should be green
   513     numRating = [session outgoingRatingForMessage:msg error:&error];
   514     XCTAssertNotNil(numRating);
   515     XCTAssertNil(error);
   516     XCTAssertEqual(numRating.pEpRating, PEP_rating_trusted);
   517 
   518     rating = [self ratingForIdentity:identBob session:session];
   519     XCTAssertEqual(rating, PEP_rating_trusted);
   520 
   521     // Let' say we undo handshake
   522     XCTAssertTrue([session keyResetTrust:identBob error:&error]);
   523     XCTAssertNil(error);
   524 
   525     // Yellow ?
   526     numRating = [session outgoingRatingForMessage:msg error:&error];
   527     XCTAssertNotNil(numRating);
   528     XCTAssertNil(error);
   529     XCTAssertEqual(numRating.pEpRating, PEP_rating_reliable);
   530 
   531     // mistrust Bob
   532     XCTAssertTrue([session keyMistrusted:identBob error:&error]);
   533     XCTAssertNil(error);
   534 
   535     identBob.fingerPrint = nil;
   536     XCTAssertTrue([session updateIdentity:identBob error:&error]);
   537     XCTAssertNil(error);
   538     XCTAssertNil(identBob.fingerPrint);
   539 
   540     // Gray == PEP_rating_unencrypted
   541     numRating = [session outgoingRatingForMessage:msg error:&error];
   542     XCTAssertNotNil(numRating);
   543     XCTAssertNil(error);
   544     XCTAssertEqual(numRating.pEpRating, PEP_rating_unencrypted);
   545 
   546     // Undo
   547     XCTAssertTrue([session undoLastMistrustWithError:&error]);
   548     XCTAssertNil(error);
   549     XCTAssertTrue([session updateIdentity:identBob error:&error]);
   550     XCTAssertNil(error);
   551     XCTAssertNotNil(identBob.fingerPrint);
   552 
   553     // Back to yellow
   554     numRating = [session outgoingRatingForMessage:msg error:&error];
   555     XCTAssertNotNil(numRating);
   556     XCTAssertNil(error);
   557 
   558     // After ENGINE-371 has been fixed, this should be just PEP_rating_reliable
   559     XCTAssertEqual(numRating.pEpRating, PEP_rating_trusted);
   560     XCTAssertEqual([self ratingForIdentity:identBob session:session], PEP_rating_trusted);
   561 
   562     // Trust again
   563     XCTAssertTrue([session trustPersonalKey:identBob error:&error]);
   564     XCTAssertNil(error);
   565 
   566     // Back to green
   567     numRating = [session outgoingRatingForMessage:msg error:&error];
   568     XCTAssertNotNil(numRating);
   569     XCTAssertNil(error);
   570     XCTAssertEqual(numRating.pEpRating, PEP_rating_trusted);
   571 
   572     // Now let see if it turns back yellow if we add an unconfirmed folk.
   573     // pEp Test John (test key, don't use) <pep.test.john@pep-project.org>
   574     // AA2E4BEB93E5FE33DEFD8BE1135CD6D170DCF575
   575     XCTAssertTrue([PEPTestUtils importBundledKey:@"0x70DCF575.asc" session:session]);
   576 
   577     PEPIdentity *identJohn = [[PEPIdentity alloc]
   578                               initWithAddress:@"pep.test.john@pep-project.org"
   579                               userID:@"101" userName:@"pEp Test John"
   580                               isOwn:NO
   581                               fingerPrint:@"AA2E4BEB93E5FE33DEFD8BE1135CD6D170DCF575"];
   582 
   583     XCTAssertTrue([session updateIdentity:identJohn error:&error]);
   584     XCTAssertNil(error);
   585 
   586     msg.cc = @[[PEPTestUtils foreignPepIdentityWithAddress:@"pep.test.john@pep-project.org"
   587                                                   userName:@"pEp Test John"]];
   588     // Yellow ?
   589     numRating = [session outgoingRatingForMessage:msg error:&error];
   590     XCTAssertNotNil(numRating);
   591     XCTAssertNil(error);
   592     XCTAssertEqual(numRating.pEpRating, PEP_rating_reliable);
   593 
   594     PEPMessage *encMsg = [session encryptMessage:msg extraKeys:nil status:nil error:&error];
   595     XCTAssertNotNil(encMsg);
   596     XCTAssertNil(error);
   597 
   598     XCTAssertEqualObjects(encMsg.shortMessage, @"p≡p");
   599     XCTAssertTrue([encMsg.longMessage containsString:@"p≡p"]);
   600 }
   601 
   602 
   603 - (void)testOutgoingBccColors
   604 {
   605     PEPSession *session = [PEPSession new];
   606 
   607     // Our test user :
   608     // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
   609     // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
   610     XCTAssertTrue([PEPTestUtils importBundledKey:@"6FF00E97_sec.asc" session:session]);
   611 
   612     PEPIdentity *identAlice = [[PEPIdentity alloc]
   613                                initWithAddress:@"pep.test.alice@pep-project.org"
   614                                userID:ownUserId
   615                                userName:@"pEp Test Alice"
   616                                isOwn:YES
   617                                fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
   618 
   619     NSError *error = nil;
   620     XCTAssertTrue([session mySelf:identAlice error:&error]);
   621     XCTAssertNil(error);
   622 
   623     PEPMessage *msg = [PEPMessage new];
   624     msg.from = identAlice;
   625     msg.to = @[[[PEPIdentity alloc] initWithAddress:@"pep.test.bob@pep-project.org"
   626                                              userID:@"42" userName:@"pEp Test Bob" isOwn:NO]];
   627     msg.shortMessage = @"All Green Test";
   628     msg.longMessage = @"This is a text content";
   629     msg.direction = PEP_dir_outgoing;
   630 
   631     // Test with unknown Bob
   632     PEP_rating rating;
   633     NSNumber *numRating = [session outgoingRatingForMessage:msg error:&error];
   634     XCTAssertNotNil(numRating);
   635     XCTAssertNil(error);
   636     XCTAssertEqual(numRating.pEpRating, PEP_rating_unencrypted);
   637 
   638     // Now let see with bob's pubkey already known
   639     // pEp Test Bob (test key, don't use) <pep.test.bob@pep-project.org>
   640     // BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39
   641     XCTAssertTrue([PEPTestUtils importBundledKey:@"0xC9C2EE39.asc" session:session]);
   642 
   643     PEPIdentity *identBob = [[PEPIdentity alloc]
   644                              initWithAddress:@"pep.test.bob@pep-project.org"
   645                              userID:@"42" userName:@"pEp Test Bob"
   646                              isOwn:NO
   647                              fingerPrint:@"BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39"];
   648 
   649     XCTAssertTrue([session updateIdentity:identBob error:&error]);
   650     XCTAssertNil(error);
   651 
   652     // Should be yellow, since no handshake happened.
   653     numRating = [session outgoingRatingForMessage:msg error:&error];
   654     XCTAssertNotNil(numRating);
   655     XCTAssertNil(error);
   656     XCTAssertEqual(numRating.pEpRating, PEP_rating_reliable);
   657 
   658     rating = [self ratingForIdentity:identBob session:session];
   659     XCTAssertEqual(rating, PEP_rating_reliable);
   660 
   661     // Let' say we got that handshake, set PEP_ct_confirmed in Bob's identity
   662     XCTAssertTrue([session trustPersonalKey:identBob error:&error]);
   663     XCTAssertNil(error);
   664 
   665     // This time it should be green
   666     numRating = [session outgoingRatingForMessage:msg error:&error];
   667     XCTAssertNotNil(numRating);
   668     XCTAssertNil(error);
   669     XCTAssertEqual(numRating.pEpRating, PEP_rating_trusted);
   670 
   671     rating = [self ratingForIdentity:identBob session:session];
   672     XCTAssertEqual(rating, PEP_rating_trusted);
   673 
   674     // Now let see if it turns back yellow if we add an unconfirmed folk.
   675     // pEp Test John (test key, don't use) <pep.test.john@pep-project.org>
   676     // AA2E4BEB93E5FE33DEFD8BE1135CD6D170DCF575
   677     XCTAssertTrue([PEPTestUtils importBundledKey:@"0x70DCF575.asc" session:session]);
   678 
   679     PEPIdentity *identJohn = [[PEPIdentity alloc]
   680                               initWithAddress:@"pep.test.john@pep-project.org"
   681                               userID:@"101" userName:@"pEp Test John"
   682                               isOwn:NO
   683                               fingerPrint:@"AA2E4BEB93E5FE33DEFD8BE1135CD6D170DCF575"];
   684 
   685     XCTAssertTrue([session updateIdentity:identJohn error:&error]);
   686     XCTAssertNil(error);
   687 
   688     msg.bcc = @[[[PEPIdentity alloc] initWithAddress:@"pep.test.john@pep-project.org"
   689                                               userID:@"101" userName:@"pEp Test John" isOwn:NO]];
   690 
   691     // Yellow ?
   692     numRating = [session outgoingRatingForMessage:msg error:&error];
   693     XCTAssertNotNil(numRating);
   694     XCTAssertNil(error);
   695     XCTAssertEqual(numRating.pEpRating, PEP_rating_reliable);
   696 
   697     XCTAssertTrue([session trustPersonalKey:identJohn error:&error]);
   698     XCTAssertNil(error);
   699 
   700     // This time it should be green
   701     numRating = [session outgoingRatingForMessage:msg error:&error];
   702     XCTAssertNotNil(numRating);
   703     XCTAssertNil(error);
   704     XCTAssertEqual(numRating.pEpRating, PEP_rating_trusted);
   705 
   706     rating = [self ratingForIdentity:identJohn session:session];
   707     XCTAssertEqual(rating, PEP_rating_trusted);
   708 }
   709 
   710 - (void)testDontEncryptForMistrusted
   711 {
   712     PEPSession *session = [PEPSession new];
   713 
   714     // Our test user :
   715     // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
   716     // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
   717     XCTAssertTrue([PEPTestUtils importBundledKey:@"6FF00E97_sec.asc" session:session]);
   718 
   719     PEPIdentity *identAlice = [[PEPIdentity alloc]
   720                                initWithAddress:@"pep.test.alice@pep-project.org"
   721                                userID:ownUserId
   722                                userName:@"pEp Test Alice"
   723                                isOwn:YES
   724                                fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
   725 
   726     NSError *error = nil;
   727     XCTAssertTrue([session mySelf:identAlice error:&error]);
   728     XCTAssertNil(error);
   729 
   730     // pEp Test Bob (test key, don't use) <pep.test.bob@pep-project.org>
   731     // BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39
   732     XCTAssertTrue([PEPTestUtils importBundledKey:@"0xC9C2EE39.asc" session:session]);
   733 
   734     PEPIdentity *identBob = [[PEPIdentity alloc]
   735                              initWithAddress:@"pep.test.bob@pep-project.org"
   736                              userID:@"42" userName:@"pEp Test Bob"
   737                              isOwn:NO
   738                              fingerPrint:@"BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39"];
   739 
   740     XCTAssertTrue([session updateIdentity:identBob error:&error]);
   741     XCTAssertNil(error);
   742 
   743     // mistrust Bob
   744     XCTAssertTrue([session keyMistrusted:identBob error:&error]);
   745     XCTAssertNil(error);
   746 
   747     PEPMessage *msg = [PEPMessage new];
   748     msg.from = identAlice;
   749     msg.to = @[[[PEPIdentity alloc] initWithAddress:@"pep.test.bob@pep-project.org" userID:@"42"
   750                                            userName:@"pEp Test Bob" isOwn:NO]];
   751     msg.shortMessage = @"All Green Test";
   752     msg.longMessage = @"This is a text content";
   753     msg.direction = PEP_dir_outgoing;
   754 
   755     // Gray == PEP_rating_unencrypted
   756     NSNumber *numRating = [session outgoingRatingForMessage:msg error:&error];
   757     XCTAssertNotNil(numRating);
   758     XCTAssertNil(error);
   759     XCTAssertEqual(numRating.pEpRating, PEP_rating_unencrypted);
   760 
   761     PEPMessage *encMsg = [session encryptMessage:msg extraKeys:nil status:nil error:&error];
   762     XCTAssertNotNil(encMsg);
   763     XCTAssertNil(error);
   764 
   765     XCTAssertNotEqualObjects(encMsg.attachments[0].mimeType, @"application/pgp-encrypted");
   766 
   767     [self pEpCleanUp];
   768 }
   769 
   770 - (void)testRevoke
   771 {
   772     PEPSession *session = [PEPSession new];
   773 
   774     // Our test user :
   775     // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
   776     // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
   777     XCTAssertTrue([PEPTestUtils importBundledKey:@"6FF00E97_sec.asc" session:session]);
   778     NSString *fpr = @"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97";
   779 
   780     PEPIdentity *identAlice = [[PEPIdentity alloc]
   781                                initWithAddress:@"pep.test.alice@pep-project.org"
   782                                userID:ownUserId
   783                                userName:@"pEp Test Alice"
   784                                isOwn:YES
   785                                fingerPrint:fpr];
   786 
   787     NSError *error = nil;
   788     XCTAssertTrue([session mySelf:identAlice error:&error]);
   789     XCTAssertNil(error);
   790 
   791     PEPIdentity *identAlice2 = [identAlice mutableCopy];
   792 
   793     // This will revoke key
   794     XCTAssertTrue([session keyMistrusted:identAlice2 error:&error]);
   795     XCTAssertNil(error);
   796     identAlice2.fingerPrint = nil;
   797 
   798     XCTAssertTrue([session mySelf:identAlice error:&error]);
   799     XCTAssertNil(error);
   800 
   801     // Check fingerprint is different
   802     XCTAssertNotEqualObjects(identAlice2.fingerPrint, fpr);
   803 }
   804 
   805 - (void)testMailToMyself
   806 {
   807     PEPSession *session = [PEPSession new];
   808 
   809     // Our test user :
   810     // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
   811     // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
   812     XCTAssertTrue([PEPTestUtils importBundledKey:@"6FF00E97_sec.asc" session:session]);
   813 
   814     PEPIdentity *identAlice = [[PEPIdentity alloc]
   815                                initWithAddress:@"pep.test.alice@pep-project.org"
   816                                userID:ownUserId
   817                                userName:@"pEp Test Alice"
   818                                isOwn:YES
   819                                fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
   820 
   821     NSError *error = nil;
   822     XCTAssertTrue([session mySelf:identAlice error:&error]);
   823     XCTAssertNil(error);
   824 
   825     PEPMessage *msg = [PEPMessage new];
   826     msg.from = identAlice;
   827     msg.to = @[identAlice];
   828     msg.shortMessage = @"Mail to Myself";
   829     msg.longMessage = @"This is a text content";
   830     msg.direction = PEP_dir_outgoing;
   831 
   832     NSNumber *numRating = [session outgoingRatingForMessage:msg error:&error];
   833     XCTAssertNotNil(numRating);
   834     XCTAssertNil(error);
   835     XCTAssertEqual(numRating.pEpRating, PEP_rating_trusted_and_anonymized);
   836 
   837     PEPMessage *encMsg = [session encryptMessage:msg extraKeys:nil status:nil error:&error];
   838     XCTAssertNotNil(encMsg);
   839     XCTAssertNil(error);
   840 
   841     NSArray *keys;
   842 
   843     error = nil;
   844     PEP_rating rating = PEP_rating_undefined;
   845     PEPMessage *decmsg = [session
   846                           decryptMessage:encMsg
   847                           flags:nil
   848                           rating:&rating
   849                           extraKeys:&keys
   850                           status:nil
   851                           error:&error];
   852     XCTAssertNotNil(decmsg);
   853     XCTAssertNil(error);
   854     XCTAssertEqual(rating, PEP_rating_trusted_and_anonymized);
   855 }
   856 
   857 - (void)testEncryptedMailFromMuttWithReencryption
   858 {
   859     PEPSession *session = [PEPSession new];
   860 
   861     // This is the public key for test001@peptest.ch
   862     XCTAssertTrue([PEPTestUtils importBundledKey:@"A3FC7F0A.asc" session:session]);
   863 
   864     // This is the secret key for test001@peptest.ch
   865     XCTAssertTrue([PEPTestUtils importBundledKey:@"A3FC7F0A_sec.asc" session:session]);
   866 
   867     // Mail from mutt, already processed into message dict by the app.
   868     NSMutableDictionary *msgDict = [[PEPTestUtils
   869                                      unarchiveDictionary:@"msg_to_A3FC7F0A_from_mutt.ser"]
   870                                     mutableCopy];
   871     [PEPTestUtils migrateUnarchivedMessageDictionary:msgDict];
   872     [msgDict removeObjectForKey:kPepLongMessage];
   873     [msgDict removeObjectForKey:kPepLongMessageFormatted];
   874 
   875     // Also extracted "live" from the app.
   876     NSMutableDictionary *accountDict = [[PEPTestUtils
   877                                          unarchiveDictionary:@"account_A3FC7F0A.ser"]
   878                                         mutableCopy];
   879     [accountDict removeObjectForKey:kPepCommType];
   880     [accountDict removeObjectForKey:kPepFingerprint];
   881     PEPIdentity *identMe = [[PEPIdentity alloc] initWithDictionary:accountDict];
   882 
   883     NSError *error = nil;
   884     XCTAssertTrue([session mySelf:identMe error:&error]);
   885     XCTAssertNil(error);
   886 
   887     XCTAssertNotNil(identMe.fingerPrint);
   888 
   889     NSArray* keys;
   890     PEPMessage *msg = [PEPMessage new];
   891     [msg setValuesForKeysWithDictionary:msgDict];
   892     PEPMessage *msgOriginal = [PEPMessage new];
   893     [msgOriginal setValuesForKeysWithDictionary:msgDict];
   894 
   895     XCTAssertEqualObjects(msg, msgOriginal);
   896 
   897     PEP_rating rating = PEP_rating_undefined;
   898     PEP_decrypt_flags flags = PEP_decrypt_flag_untrusted_server;
   899 
   900     PEPMessage *pepDecryptedMail = [session
   901                                     decryptMessage:msg
   902                                     flags:&flags
   903                                     rating:&rating
   904                                     extraKeys:&keys
   905                                     status:nil
   906                                     error:&error];
   907     XCTAssertNotNil(pepDecryptedMail);
   908     XCTAssertNil(error);
   909 
   910     // Technically, the mail is encrypted, but the signatures don't match
   911     XCTAssertEqual(rating, PEP_rating_unreliable);
   912 
   913     // Since we're requesting re-encryption, src should have been changed
   914     XCTAssertNotEqualObjects(msg, msgOriginal);
   915 
   916     XCTAssertNotNil(pepDecryptedMail.longMessage);
   917 }
   918 
   919 - (void)testOutgoingContactColor
   920 {
   921     PEPSession *session = [PEPSession new];
   922 
   923     PEPIdentity *partner1Orig = [PEPTestUtils foreignPepIdentityWithAddress:@"partner1@dontcare.me"
   924                                                                    userName:@"Partner 1"];
   925     NSString *pubKeyPartner1 = [PEPTestUtils loadResourceByName:@"partner1_F2D281C2789DD7F6_pub.asc"];
   926     XCTAssertNotNil(pubKeyPartner1);
   927 
   928     NSError *error = nil;
   929     XCTAssertTrue([session importKey:pubKeyPartner1 error:&error]);
   930     XCTAssertNil(error);
   931 
   932     PEP_rating color = [self ratingForIdentity:partner1Orig session:session];
   933     XCTAssertEqual(color, PEP_rating_reliable);
   934 }
   935 
   936 - (void)testGetTrustwords
   937 {
   938     PEPSession *session = [PEPSession new];
   939 
   940     PEPIdentity *partner1Orig = [[PEPIdentity alloc]
   941                                  initWithAddress:@"partner1@dontcare.me" userID:@"partner1"
   942                                  userName:@"partner1"
   943                                  isOwn:NO fingerPrint:@"F0CD3F7B422E5D587ABD885BF2D281C2789DD7F6"];
   944 
   945     PEPIdentity *meOrig = [[PEPIdentity alloc]
   946                            initWithAddress:@"me@dontcare.me" userID:@"me"
   947                            userName:@"me"
   948                            isOwn:NO fingerPrint:@"CC1F73F6FB774BF08B197691E3BFBCA9248FC681"];
   949 
   950     NSString *pubKeyPartner1 = [PEPTestUtils loadResourceByName:@"partner1_F2D281C2789DD7F6_pub.asc"];
   951     XCTAssertNotNil(pubKeyPartner1);
   952     NSString *pubKeyMe = [PEPTestUtils loadResourceByName:@"meATdontcare_E3BFBCA9248FC681_pub.asc"];
   953     XCTAssertNotNil(pubKeyMe);
   954     NSString *secKeyMe = [PEPTestUtils loadResourceByName:@"meATdontcare_E3BFBCA9248FC681_sec.asc"];
   955     XCTAssertNotNil(secKeyMe);
   956 
   957     NSError *error = nil;
   958     NSString *trustwordsFull = [session getTrustwordsIdentity1:meOrig identity2:partner1Orig
   959                                                       language:nil full:YES error:&error];
   960     XCTAssertNil(error);
   961     XCTAssertEqualObjects(trustwordsFull,
   962                           @"EMERSON GASPER TOKENISM BOLUS COLLAGE DESPISE BEDDED ENCRYPTION IMAGINE BEDFORD");
   963 
   964     NSString *trustwordsFullEnglish = [session getTrustwordsIdentity1:meOrig identity2:partner1Orig
   965                                                              language:@"en" full:YES error:&error];
   966     XCTAssertNil(error);
   967     XCTAssertEqualObjects(trustwordsFullEnglish, trustwordsFull);
   968 
   969     NSString *trustwordsUndefined = [session getTrustwordsIdentity1:meOrig identity2:partner1Orig
   970                                                            language:@"ZZ" full:YES error:&error];
   971     XCTAssertNotNil(error);
   972     XCTAssertNil(trustwordsUndefined);
   973 }
   974 
   975 - (void)testStringToRating
   976 {
   977     PEPSession *session = [PEPSession new];
   978     XCTAssertEqual([session ratingFromString:@"cannot_decrypt"], PEP_rating_cannot_decrypt);
   979     XCTAssertEqual([session ratingFromString:@"have_no_key"], PEP_rating_have_no_key);
   980     XCTAssertEqual([session ratingFromString:@"unencrypted"], PEP_rating_unencrypted);
   981     XCTAssertEqual([session ratingFromString:@"unencrypted_for_some"],
   982                    PEP_rating_unencrypted_for_some);
   983     XCTAssertEqual([session ratingFromString:@"unreliable"], PEP_rating_unreliable);
   984     XCTAssertEqual([session ratingFromString:@"reliable"], PEP_rating_reliable);
   985     XCTAssertEqual([session ratingFromString:@"trusted"], PEP_rating_trusted);
   986     XCTAssertEqual([session ratingFromString:@"trusted_and_anonymized"],
   987                    PEP_rating_trusted_and_anonymized);
   988     XCTAssertEqual([session ratingFromString:@"fully_anonymous"], PEP_rating_fully_anonymous);
   989     XCTAssertEqual([session ratingFromString:@"mistrust"], PEP_rating_mistrust);
   990     XCTAssertEqual([session ratingFromString:@"b0rken"], PEP_rating_b0rken);
   991     XCTAssertEqual([session ratingFromString:@"under_attack"], PEP_rating_under_attack);
   992     XCTAssertEqual([session ratingFromString:@"undefined"], PEP_rating_undefined);
   993     XCTAssertEqual([session ratingFromString:@"does not exist111"], PEP_rating_undefined);
   994 }
   995 
   996 - (void)testRatingToString
   997 {
   998     PEPSession *session = [PEPSession new];
   999     XCTAssertEqualObjects([session stringFromRating:PEP_rating_cannot_decrypt], @"cannot_decrypt");
  1000     XCTAssertEqualObjects([session stringFromRating:PEP_rating_have_no_key], @"have_no_key");
  1001     XCTAssertEqualObjects([session stringFromRating:PEP_rating_unencrypted], @"unencrypted");
  1002     XCTAssertEqualObjects([session stringFromRating:PEP_rating_unencrypted_for_some],
  1003                           @"unencrypted_for_some");
  1004     XCTAssertEqualObjects([session stringFromRating:PEP_rating_unreliable], @"unreliable");
  1005     XCTAssertEqualObjects([session stringFromRating:PEP_rating_reliable], @"reliable");
  1006     XCTAssertEqualObjects([session stringFromRating:PEP_rating_trusted], @"trusted");
  1007     XCTAssertEqualObjects([session stringFromRating:PEP_rating_trusted_and_anonymized],
  1008                           @"trusted_and_anonymized");
  1009     XCTAssertEqualObjects([session stringFromRating:PEP_rating_fully_anonymous],
  1010                           @"fully_anonymous");
  1011     XCTAssertEqualObjects([session stringFromRating:PEP_rating_mistrust], @"mistrust");
  1012     XCTAssertEqualObjects([session stringFromRating:PEP_rating_b0rken], @"b0rken");
  1013     XCTAssertEqualObjects([session stringFromRating:PEP_rating_under_attack], @"under_attack");
  1014     XCTAssertEqualObjects([session stringFromRating:PEP_rating_undefined], @"undefined");
  1015     XCTAssertEqualObjects([session stringFromRating:500], @"undefined");
  1016 }
  1017 
  1018 - (void)testIsPEPUser
  1019 {
  1020     PEPSession *session = [PEPSession new];
  1021 
  1022     PEPIdentity *identMe = [[PEPIdentity alloc]
  1023                             initWithAddress:@"me-myself-and-i@pep-project.org"
  1024                             userID:@"me-myself-and-i"
  1025                             userName:@"pEp Me"
  1026                             isOwn:YES];
  1027     NSError *error = nil;
  1028     XCTAssertTrue([session mySelf:identMe error:&error]);
  1029     XCTAssertNil(error);
  1030 
  1031     XCTAssertNotNil(identMe.fingerPrint);
  1032 
  1033     // PEP_CANNOT_FIND_PERSON == 902
  1034     XCTAssertTrue([session isPEPUser:identMe error:&error]);
  1035 }
  1036 
  1037 - (void)testXEncStatusForOutgoingEncryptedMail
  1038 {
  1039     [self helperXEncStatusForOutgoingEncryptdMailToSelf:NO expectedRating:PEP_rating_reliable];
  1040 }
  1041 
  1042 - (void)testXEncStatusForOutgoingSelfEncryptedMail
  1043 {
  1044     [self helperXEncStatusForOutgoingEncryptdMailToSelf:YES
  1045                                          expectedRating:PEP_rating_trusted_and_anonymized];
  1046 }
  1047 
  1048 - (void)testEncryptMessagesWithoutKeys
  1049 {
  1050     PEPSession *session = [PEPSession new];
  1051 
  1052     PEPIdentity *identMe = [[PEPIdentity alloc]
  1053                             initWithAddress:@"me-myself-and-i@pep-project.org"
  1054                             userID:@"me-myself-and-i"
  1055                             userName:@"pEp Me"
  1056                             isOwn:YES];
  1057     NSError *error = nil;
  1058     XCTAssertTrue([session mySelf:identMe error:&error]);
  1059     XCTAssertNil(error);
  1060 
  1061     XCTAssertNotNil(identMe.fingerPrint);
  1062 
  1063     PEPIdentity *identAlice = [[PEPIdentity alloc]
  1064                                initWithAddress:@"alice@pep-project.org"
  1065                                userID:@"alice"
  1066                                userName:@"pEp Test Alice"
  1067                                isOwn:NO];
  1068 
  1069     PEPMessage *msg = [PEPMessage new];
  1070     msg.from = identMe;
  1071     msg.to = @[identAlice];
  1072     msg.shortMessage = @"Mail to Alice";
  1073     msg.longMessage = @"Alice?";
  1074     msg.direction = PEP_dir_outgoing;
  1075 
  1076     NSNumber *numRating = [session outgoingRatingForMessage:msg error:&error];
  1077     XCTAssertNotNil(numRating);
  1078     XCTAssertNil(error);
  1079     XCTAssertEqual(numRating.pEpRating, PEP_rating_unencrypted);
  1080 
  1081     PEPMessage *encMsg = [session encryptMessage:msg extraKeys:nil status:nil error:&error];
  1082     XCTAssertNotNil(encMsg);
  1083     XCTAssertNil(error);
  1084 
  1085     XCTAssertNotNil(encMsg);
  1086 
  1087     PEPStringList *keys;
  1088     PEP_rating pEpRating;
  1089     error = nil;
  1090     PEPMessage *decMsg = [session
  1091                           decryptMessage:encMsg
  1092                           flags:nil
  1093                           rating:&pEpRating
  1094                           extraKeys:&keys
  1095                           status:nil
  1096                           error:&error];
  1097     XCTAssertNotNil(decMsg);
  1098     XCTAssertNil(error);
  1099 
  1100     XCTAssertEqual(pEpRating, PEP_rating_unencrypted);
  1101     XCTAssertNotNil(decMsg);
  1102 }
  1103 
  1104 /**
  1105  ENGINE-364. Tries to invoke trustPersonalKey on an identity without key,
  1106  giving it a fake fingerprint.
  1107  */
  1108 - (void)testTrustPersonalKey
  1109 {
  1110     PEPSession *session = [PEPSession new];
  1111 
  1112     PEPIdentity *identMe = [[PEPIdentity alloc]
  1113                             initWithAddress:@"me-myself-and-i@pep-project.org"
  1114                             userID:@"me-myself-and-i"
  1115                             userName:@"pEp Me"
  1116                             isOwn:YES];
  1117     NSError *error = nil;
  1118     XCTAssertTrue([session mySelf:identMe error:&error]);
  1119     XCTAssertNil(error);
  1120 
  1121     XCTAssertNotNil(identMe.fingerPrint);
  1122 
  1123     // The fingerprint is definitely wrong, we don't have a key
  1124     PEPIdentity *identAlice = [[PEPIdentity alloc]
  1125                                initWithAddress:@"alice@pep-project.org"
  1126                                userID:@"alice"
  1127                                userName:@"pEp Test Alice"
  1128                                isOwn:NO
  1129                                fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
  1130 
  1131     XCTAssertFalse([session trustPersonalKey:identAlice error:&error]);
  1132     XCTAssertNotNil(error);
  1133 }
  1134 
  1135 /**
  1136  ENGINE-381
  1137  */
  1138 - (void)testVolatileIdentityRating
  1139 {
  1140     PEPSession *session = [PEPSession new];
  1141 
  1142     PEPIdentity *identMe = [[PEPIdentity alloc]
  1143                             initWithAddress:@"me-myself-and-i@pep-project.org"
  1144                             userID:@"me-myself-and-i"
  1145                             userName:@"pEp Me"
  1146                             isOwn:YES];
  1147     NSError *error = nil;
  1148     XCTAssertTrue([session mySelf:identMe error:&error]);
  1149     XCTAssertNil(error);
  1150 
  1151     XCTAssertNotNil(identMe.fingerPrint);
  1152 
  1153     PEPIdentity *identAlice = [self
  1154                                checkImportingKeyFilePath:@"6FF00E97_sec.asc"
  1155                                address:@"pep.test.alice@pep-project.org"
  1156                                userID:@"alice_user_id"
  1157                                fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
  1158                                session: session];
  1159     XCTAssertNotNil(identAlice);
  1160 
  1161     dispatch_group_t identityRatingGroup = dispatch_group_create();
  1162 
  1163     void (^ratingBlock)(void) = ^{
  1164         PEPSession *innerSession = [PEPSession new];
  1165         PEP_rating rating = [self ratingForIdentity:identAlice session:innerSession];
  1166         XCTAssertEqual(rating, PEP_rating_reliable);
  1167     };
  1168 
  1169     for (int i = 0; i < 4; ++i) {
  1170         dispatch_group_async(identityRatingGroup,
  1171                              dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0),
  1172                              ratingBlock);
  1173     }
  1174 
  1175     for (int i = 0; i < 4; ++i) {
  1176         ratingBlock();
  1177     }
  1178 
  1179     dispatch_group_wait(identityRatingGroup, DISPATCH_TIME_FOREVER);
  1180 }
  1181 
  1182 /**
  1183  IOSAD-93, testing for easy error case.
  1184  */
  1185 - (void)testIllegalValueEncryptAndAttachPrivateKey
  1186 {
  1187     PEPSession *session = [PEPSession new];
  1188 
  1189     PEPIdentity *identMe = [[PEPIdentity alloc]
  1190                             initWithAddress:@"me-myself-and-i@pep-project.org"
  1191                             userID:@"me-myself-and-i"
  1192                             userName:@"pEp Me"
  1193                             isOwn:YES];
  1194     NSError *error = nil;
  1195     XCTAssertTrue([session mySelf:identMe error:&error]);
  1196     XCTAssertNil(error);
  1197 
  1198     XCTAssertNotNil(identMe.fingerPrint);
  1199 
  1200     NSString *fprAlice = @"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97";
  1201     PEPIdentity *identAlice = [self
  1202                                checkImportingKeyFilePath:@"6FF00E97_sec.asc"
  1203                                address:@"pep.test.alice@pep-project.org"
  1204                                userID:@"alice_user_id"
  1205                                fingerPrint:fprAlice
  1206                                session: session];
  1207     XCTAssertNotNil(identAlice);
  1208 
  1209     NSString *shortMessage = @"whatever it may be";
  1210     NSString *longMessage = [NSString stringWithFormat:@"%@ %@", shortMessage, shortMessage];
  1211     PEPMessage *message = [PEPMessage new];
  1212     message.from = identMe;
  1213     message.to = @[identAlice];
  1214     message.shortMessage = shortMessage;
  1215     message.longMessage = longMessage;
  1216 
  1217     PEP_STATUS status = PEP_KEY_NOT_FOUND;
  1218     error = nil;
  1219     PEPMessage *encrypted = [session
  1220                              encryptMessage:message
  1221                              toFpr:fprAlice
  1222                              encFormat:PEP_enc_PEP
  1223                              flags:0
  1224                              status:&status error:&error];
  1225     XCTAssertEqual(status, PEP_ILLEGAL_VALUE);
  1226     XCTAssertNotNil(error);
  1227     XCTAssertNil(encrypted);
  1228 }
  1229 
  1230 #pragma mark - configUnencryptedSubject
  1231 
  1232 - (void)testConfigUnencryptedSubject
  1233 {
  1234     // Setup Config to encrypt subject
  1235     [PEPObjCAdapter setUnEncryptedSubjectEnabled:NO];
  1236 
  1237     // Write mail to yourself ...
  1238     PEPMessage *encMessage = [self mailWrittenToMySelf];
  1239 
  1240     // ... and assert subject is encrypted
  1241     XCTAssertEqualObjects(encMessage.shortMessage, @"p≡p", @"Subject should be encrypted");
  1242 }
  1243 
  1244 - (void)testConfigUnencryptedSubjectEncryptedSubjectDisabled
  1245 {
  1246     // Setup Config to not encrypt subject
  1247     [PEPObjCAdapter setUnEncryptedSubjectEnabled:YES];
  1248 
  1249     // Write mail to yourself ...
  1250     PEPMessage *encMessage = [self mailWrittenToMySelf];
  1251 
  1252     // pEp to pEp uses message 2.0, which always encrypts subjects (ENGINE-429)
  1253     XCTAssertEqualObjects(encMessage.shortMessage, @"p≡p", @"Subject should be encrypted");
  1254 }
  1255 
  1256 #pragma mark - Helpers
  1257 
  1258 /**
  1259  Determines the rating for the given identity.
  1260  @return PEP_rating_undefined on error
  1261  */
  1262 - (PEP_rating)ratingForIdentity:(PEPIdentity *)identity session:(PEPSession *)session
  1263 {
  1264     NSError *error;
  1265     NSNumber *numRating = [session ratingForIdentity:identity error:&error];
  1266     XCTAssertNil(error);
  1267     return numRating.pEpRating;
  1268 }
  1269 
  1270 - (PEPIdentity *)checkImportingKeyFilePath:(NSString *)filePath address:(NSString *)address
  1271                                     userID:(NSString *)userID
  1272                                fingerPrint:(NSString *)fingerPrint
  1273                                    session:(PEPSession *)session
  1274 {
  1275     if (!session) {
  1276         session = [PEPSession new];
  1277     }
  1278 
  1279     BOOL success = [PEPTestUtils importBundledKey:filePath session:session];
  1280     XCTAssertTrue(success);
  1281 
  1282     if (success) {
  1283         // Our test user:
  1284         PEPIdentity *identTest = [[PEPIdentity alloc]
  1285                                   initWithAddress:address
  1286                                   userID:userID
  1287                                   userName:[NSString stringWithFormat:@"Some User Name %@", userID]
  1288                                   isOwn:NO];
  1289 
  1290         NSError *error = nil;
  1291         XCTAssertTrue([session updateIdentity:identTest error:&error]);
  1292         XCTAssertNil(error);
  1293         XCTAssertNotNil(identTest.fingerPrint);
  1294         XCTAssertEqualObjects(identTest.fingerPrint, fingerPrint);
  1295 
  1296         return identTest;
  1297     } else {
  1298         return nil;
  1299     }
  1300 }
  1301 
  1302 - (PEPIdentity *)checkMySelfImportingKeyFilePath:(NSString *)filePath address:(NSString *)address
  1303                                           userID:(NSString *)userID
  1304                                      fingerPrint:(NSString *)fingerPrint
  1305                                          session:(PEPSession *)session
  1306 {
  1307     XCTAssertTrue([PEPTestUtils importBundledKey:filePath session:session]);
  1308 
  1309     // Our test user:
  1310     PEPIdentity *identTest = [[PEPIdentity alloc]
  1311                               initWithAddress:address
  1312                               userID:userID
  1313                               userName:[NSString stringWithFormat:@"Some User Name %@", userID]
  1314                               isOwn:YES
  1315                               fingerPrint: fingerPrint];
  1316 
  1317     NSError *error;
  1318     XCTAssertTrue([session setOwnKey:identTest fingerprint:fingerPrint error:&error]);
  1319     XCTAssertNil(error);
  1320     XCTAssertNotNil(identTest.fingerPrint);
  1321     XCTAssertEqualObjects(identTest.fingerPrint, fingerPrint);
  1322 
  1323     return identTest;
  1324 }
  1325 
  1326 /**
  1327  Verifies that a partner ID is really a correct Identity.
  1328  Usually used on identities imported as keys, since the engine has problems with them.
  1329  */
  1330 - (void)updateAndVerifyPartnerIdentity:(PEPIdentity *)partnerIdentity session:(PEPSession *)session
  1331 {
  1332     NSError *error = nil;
  1333 
  1334     XCTAssertNotNil(partnerIdentity.fingerPrint);
  1335     XCTAssertTrue([session updateIdentity:partnerIdentity error:&error]);
  1336     XCTAssertNil(error);
  1337     XCTAssertNotNil(partnerIdentity.fingerPrint);
  1338     NSString *fingerprint = partnerIdentity.fingerPrint;
  1339     partnerIdentity.fingerPrint = nil;
  1340     XCTAssertTrue([session updateIdentity:partnerIdentity error:&error]);
  1341     XCTAssertNil(error);
  1342     XCTAssertNotNil(partnerIdentity.fingerPrint);
  1343     XCTAssertEqualObjects(partnerIdentity.fingerPrint, fingerprint);
  1344 }
  1345 
  1346 - (PEPMessage *)mailWrittenToMySelf
  1347 {
  1348     PEPSession *session = [PEPSession new];
  1349 
  1350     // Write a e-mail to yourself ...
  1351     PEPIdentity *me = [PEPTestUtils ownPepIdentityWithAddress:@"me@peptest.ch"
  1352                                                      userName:@"userName"];
  1353     NSError *error = nil;
  1354     XCTAssertTrue([session mySelf:me error:&error]);
  1355     XCTAssertNil(error);
  1356 
  1357     NSString *shortMessage = @"Subject";
  1358     NSString *longMessage = @"Oh, this is a long body text!";
  1359     PEPMessage *mail = [PEPTestUtils mailFrom:me
  1360                                       toIdent:me
  1361                                  shortMessage:shortMessage
  1362                                   longMessage:longMessage
  1363                                      outgoing:YES];
  1364     PEP_STATUS status = PEP_UNKNOWN_ERROR;
  1365     PEPMessage *encMessage = [session
  1366                               encryptMessage:mail
  1367                               forSelf:me
  1368                               extraKeys:nil
  1369                               status:&status
  1370                               error:&error];
  1371     XCTAssertNil(error);
  1372 
  1373     return encMessage;
  1374 }
  1375 
  1376 - (PEPMessage *)internalEncryptToMySelfKeys:(PEPStringList **)keys
  1377 {
  1378     PEPSession *session = [PEPSession new];
  1379     
  1380     PEPIdentity *me = [PEPTestUtils ownPepIdentityWithAddress:@"me@peptest.ch"
  1381                                                      userName:@"userName"];
  1382     NSError *error = nil;
  1383     XCTAssertTrue([session mySelf:me error:&error]);
  1384     XCTAssertNil(error);
  1385 
  1386     XCTAssertNotNil(me.fingerPrint);
  1387 
  1388     // Create draft
  1389     NSString *shortMessage = @"Subject";
  1390     NSString *longMessage = @"Oh, this is a long body text!";
  1391     PEPMessage *mail = [PEPTestUtils mailFrom:me toIdent:me shortMessage:shortMessage longMessage:longMessage outgoing:YES];
  1392 
  1393     PEP_STATUS status;
  1394     PEPMessage *encMessage = [session
  1395                               encryptMessage:mail
  1396                               forSelf:me
  1397                               extraKeys:nil
  1398                               status:&status
  1399                               error:&error];
  1400     XCTAssertEqual(status, 0);
  1401     XCTAssertEqualObjects(encMessage.shortMessage, @"p≡p");
  1402 
  1403     PEP_rating rating;
  1404     error = nil;
  1405     PEPMessage *unencDict = [session
  1406                              decryptMessage:encMessage
  1407                              flags:nil
  1408                              rating:&rating
  1409                              extraKeys:keys
  1410                              status:nil
  1411                              error:&error];
  1412     XCTAssertNotNil(unencDict);
  1413     XCTAssertNil(error);
  1414 
  1415     XCTAssertGreaterThanOrEqual(rating, PEP_rating_reliable);
  1416 
  1417     XCTAssertEqualObjects(unencDict.shortMessage, shortMessage);
  1418     XCTAssertEqualObjects(unencDict.longMessage, longMessage);
  1419 
  1420     return unencDict;
  1421 }
  1422 
  1423 - (void)pEpCleanUp
  1424 {
  1425     [PEPTestUtils cleanUp];
  1426 }
  1427 
  1428 - (void)helperXEncStatusForOutgoingEncryptdMailToSelf:(BOOL)toSelf
  1429                                        expectedRating:(PEP_rating)expectedRating
  1430 {
  1431     PEPSession *session = [PEPSession new];
  1432 
  1433     // Partner pubkey for the test:
  1434     // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
  1435     // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
  1436     XCTAssertTrue([PEPTestUtils importBundledKey:@"0x6FF00E97.asc" session:session]);
  1437 
  1438     PEPIdentity *identAlice = [[PEPIdentity alloc]
  1439                                initWithAddress:@"pep.test.alice@pep-project.org"
  1440                                userID:ownUserId
  1441                                userName:@"pEp Test Alice"
  1442                                isOwn:NO
  1443                                fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
  1444     [self updateAndVerifyPartnerIdentity:identAlice session:session];
  1445 
  1446     PEPIdentity *identMe = [[PEPIdentity alloc]
  1447                                initWithAddress:@"me-myself-and-i@pep-project.org"
  1448                                userID:@"me-myself-and-i"
  1449                                userName:@"pEp Me"
  1450                                isOwn:YES];
  1451     NSError *error = nil;
  1452     XCTAssertTrue([session mySelf:identMe error:&error]);
  1453     XCTAssertNil(error);
  1454 
  1455     XCTAssertNotNil(identMe.fingerPrint);
  1456 
  1457     PEPMessage *msg = [PEPMessage new];
  1458     msg.from = identMe;
  1459     msg.to = @[identAlice];
  1460     msg.shortMessage = @"Mail to Alice";
  1461     msg.longMessage = @"Alice?";
  1462     msg.direction = PEP_dir_outgoing;
  1463 
  1464     NSNumber *numRating = [session outgoingRatingForMessage:msg error:&error];
  1465     XCTAssertNotNil(numRating);
  1466     XCTAssertNil(error);
  1467     XCTAssertEqual(numRating.pEpRating, PEP_rating_reliable);
  1468 
  1469     PEPMessage *encMsg;
  1470 
  1471     PEP_STATUS statusEnc = PEP_VERSION_MISMATCH;
  1472     if (toSelf) {
  1473         encMsg = [session
  1474                   encryptMessage:msg
  1475                   forSelf:identMe
  1476                   extraKeys:nil
  1477                   status:&statusEnc
  1478                   error:&error];
  1479         XCTAssertEqual(statusEnc, PEP_STATUS_OK);
  1480     } else {
  1481         encMsg = [session encryptMessage:msg extraKeys:nil status:nil error:&error];
  1482         XCTAssertNotNil(encMsg);
  1483         XCTAssertNil(error);
  1484     }
  1485     XCTAssertNotNil(encMsg);
  1486 
  1487     PEPStringList *keys;
  1488     PEP_rating pEpRating;
  1489     error = nil;
  1490     PEPMessage *decMsg = [session
  1491                           decryptMessage:encMsg
  1492                           flags:nil
  1493                           rating:&pEpRating
  1494                           extraKeys:&keys
  1495                           status:nil
  1496                           error:&error];
  1497     XCTAssertNil(error);
  1498     XCTAssertNotNil(decMsg);
  1499 
  1500     XCTAssertEqual(pEpRating, expectedRating);
  1501 
  1502     NSArray * encStatusField = nil;
  1503     for (NSArray *field in decMsg.optionalFields) {
  1504         NSString *header = [field[0] lowercaseString];
  1505         if ([header isEqualToString:@"x-encstatus"]) {
  1506             encStatusField = field;
  1507         }
  1508     }
  1509     XCTAssertNotNil(encStatusField);
  1510     if (encStatusField) {
  1511         PEP_rating outgoingRating = [session ratingFromString:encStatusField[1]];
  1512         XCTAssertEqual(outgoingRating, expectedRating);
  1513     }
  1514 }
  1515 
  1516 @end