pEpObjCTests/PEPSessionTest.m
author Dirk Zimmermann <dz@pep.security>
Fri, 06 Sep 2019 11:03:27 +0200
branchIOS-1784
changeset 1225 6d89f5f624db
parent 1205 0a1aa847a5a5
child 1237 38d1cefd5ee3
child 1273 780a91298a4c
child 1282 fa7a11ffe1aa
child 1286 671aea65e056
permissions -rw-r--r--
IOS-1784 Separate concerns.
     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 "PEPObjCAdapterFramework.h"
    12 
    13 #import "PEPObjCAdapter.h"
    14 #import "PEPMessage.h"
    15 #import "PEPAttachment.h"
    16 #import "PEPTestUtils.h"
    17 #import "PEPSync.h"
    18 #import "PEPSendMessageDelegate.h"
    19 
    20 #import "PEPSessionTestNotifyHandshakeDelegate.h"
    21 #import "PEPSessionTestSendMessageDelegate.h"
    22 
    23 @interface PEPSessionTest : XCTestCase
    24 
    25 @property (nonatomic) PEPSync *sync;
    26 @property (nonatomic) PEPSessionTestSendMessageDelegate *sendMessageDelegate;
    27 @property (nonatomic) PEPSessionTestNotifyHandshakeDelegate *notifyHandshakeDelegate;
    28 
    29 @end
    30 
    31 @implementation PEPSessionTest
    32 
    33 - (void)setUp
    34 {
    35     [super setUp];
    36 
    37     [self pEpCleanUp];
    38 
    39     [PEPObjCAdapter setUnEncryptedSubjectEnabled:NO];
    40 }
    41 
    42 - (void)tearDown
    43 {
    44     [self shutdownSync];
    45     [self pEpCleanUp];
    46     [super tearDown];
    47 }
    48 
    49 - (void)testTrustWords
    50 {
    51     PEPSession *session = [PEPSession new];
    52 
    53     NSError *error = nil;
    54     NSArray *trustwords = [session
    55                            trustwordsForFingerprint:@"DB47DB47DB47DB47DB47DB47DB47DB47DB47DB47"
    56                            languageID:@"en"
    57                            shortened:false
    58                            error:&error];
    59     XCTAssertNil(error);
    60     XCTAssertEqual([trustwords count], 10);
    61 
    62     for(id word in trustwords)
    63         XCTAssertEqualObjects(word, @"BAPTISMAL");
    64 }
    65 
    66 - (void)testGenKey
    67 {
    68     PEPSession *session = [PEPSession new];
    69 
    70     PEPIdentity *identMe = [[PEPIdentity alloc]
    71                             initWithAddress:@"pep.test.iosgenkey@pep-project.org"
    72                             userID:@"Me"
    73                             userName:@"pEp Test iOS GenKey"
    74                             isOwn:YES];
    75 
    76     NSError *error = nil;
    77     XCTAssertTrue([session mySelf:identMe error:&error]);
    78     XCTAssertNil(error);
    79 
    80     XCTAssertNotNil(identMe.fingerPrint);
    81     XCTAssertNotEqual(identMe.commType, PEPCommTypeUnknown);
    82 
    83     XCTAssertTrue([identMe isPEPUser:session error:&error]);
    84 }
    85 
    86 - (void)testMySelfCommType
    87 {
    88     PEPSession *session = [PEPSession new];
    89 
    90     PEPIdentity *identMe = [[PEPIdentity alloc]
    91                             initWithAddress:@"pep.test.iosgenkey@pep-project.org"
    92                             userID:@"pep.test.iosgenkey@pep-project.org_userID"
    93                             userName:@"pEp Test iOS GenKey"
    94                             isOwn:YES];
    95 
    96     NSError *error = nil;
    97     XCTAssertTrue([session mySelf:identMe error:&error]);
    98     XCTAssertNil(error);
    99 
   100     XCTAssertNotNil(identMe.fingerPrint);
   101     XCTAssertNotEqual(identMe.commType, PEPCommTypeUnknown);
   102 
   103     XCTAssertTrue([identMe isPEPUser:session error:&error]);
   104 
   105     dispatch_queue_t queue = dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0);
   106     dispatch_sync(queue, ^{
   107         NSError *innerError = nil;
   108         PEPSession *session2 = [PEPSession new];
   109 
   110         // Now simulate an update from the app, which usually only caches
   111         // kPepUsername, kPepAddress and optionally kPepUserID.
   112         PEPIdentity *identMe2 = [[PEPIdentity alloc]
   113                                  initWithAddress:identMe.address
   114                                  userID:identMe.userID
   115                                  userName:identMe.userName
   116                                  isOwn:NO];
   117 
   118         XCTAssertTrue([session2 mySelf:identMe2 error:&innerError]);
   119         XCTAssertNil(innerError);
   120 
   121         XCTAssertNotNil(identMe2.fingerPrint);
   122         XCTAssertTrue([identMe2 isPEPUser:session error:&innerError]);
   123         XCTAssertEqualObjects(identMe2.fingerPrint, identMe.fingerPrint);
   124 
   125         // Now pretend the app only knows kPepUsername and kPepAddress
   126         PEPIdentity *identMe3 = [PEPTestUtils foreignPepIdentityWithAddress:identMe.address
   127                                                                    userName:identMe.userName];
   128         XCTAssertTrue([session2 mySelf:identMe3 error:&innerError]);
   129         XCTAssertNil(innerError);
   130 
   131         XCTAssertNotNil(identMe3.fingerPrint);
   132         XCTAssertTrue([identMe3 isPEPUser:session error:&innerError]);
   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     NSError *error = nil;
   153     XCTAssertTrue([session updateIdentity:identRandom error:&error]);
   154     XCTAssertNil(error);
   155     XCTAssertNil(identRandom.fingerPrint);
   156 }
   157 
   158 - (void)testImportPartnerKeys
   159 {
   160     XCTAssertNotNil([self checkImportingKeyFilePath:@"6FF00E97_sec.asc"
   161                                             address:@"pep.test.alice@pep-project.org"
   162                                              userID:@"This Is Alice"
   163                                         fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
   164                                             session: nil]);
   165 
   166     XCTAssertNotNil([self checkImportingKeyFilePath:@"0xC9C2EE39.asc"
   167                                             address:@"pep.test.bob@pep-project.org"
   168                                              userID:@"This Is Bob"
   169                                         fingerPrint:@"BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39"
   170                                             session: nil]);
   171 }
   172 
   173 - (void)testIdentityRating
   174 {
   175     PEPSession *session = [PEPSession new];
   176 
   177     PEPIdentity *me = [self
   178                        checkMySelfImportingKeyFilePath:@"6FF00E97_sec.asc"
   179                        address:@"pep.test.alice@pep-project.org"
   180                        userID:@"Alice_User_ID"
   181                        fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
   182                        session:session];
   183     XCTAssertEqual([self ratingForIdentity:me session:session], PEPRatingTrustedAndAnonymized);
   184 
   185     PEPIdentity *alice = [self
   186                           checkImportingKeyFilePath:@"6FF00E97_sec.asc"
   187                           address:@"pep.test.alice@pep-project.org"
   188                           userID:@"This Is Alice"
   189                           fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
   190                           session: session];
   191     XCTAssertNotNil(alice);
   192     XCTAssertEqual([self ratingForIdentity:alice session:session], PEPRatingReliable);
   193 }
   194 
   195 /** ENGINE-409 */
   196 - (void)testIdentityRatingMistrustReset
   197 {
   198     PEPSession *session = [PEPSession new];
   199 
   200     PEPIdentity *me = [[PEPIdentity alloc]
   201                        initWithAddress:@"me@example.org"
   202                        userID:@"me_myself"
   203                        userName:@"Me Me"
   204                        isOwn:YES];
   205 
   206     NSError *error = nil;
   207     XCTAssertTrue([session mySelf:me error:&error]);
   208     XCTAssertNil(error);
   209 
   210     XCTAssertNotNil(me.fingerPrint);
   211     XCTAssertEqual([self ratingForIdentity:me session:session], PEPRatingTrustedAndAnonymized);
   212 
   213     PEPIdentity *alice = [self
   214                           checkImportingKeyFilePath:@"6FF00E97_sec.asc"
   215                           address:@"pep.test.alice@pep-project.org"
   216                           userID:@"This Is Alice"
   217                           fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
   218                           session: session];
   219     XCTAssertNotNil(alice);
   220     XCTAssertEqual([self ratingForIdentity:alice session:session], PEPRatingReliable);
   221 
   222     XCTAssertTrue([session keyMistrusted:alice error:&error]);
   223     XCTAssertNil(error);
   224     XCTAssertEqual([self ratingForIdentity:alice session:session], PEPRatingHaveNoKey);
   225 }
   226 
   227 - (void)testIdentityRatingTrustResetMistrustUndo
   228 {
   229     PEPSession *session = [PEPSession new];
   230 
   231     PEPIdentity *me = [[PEPIdentity alloc]
   232                        initWithAddress:@"me@example.org"
   233                        userID:@"me_myself"
   234                        userName:@"Me Me"
   235                        isOwn:YES];
   236 
   237     NSError *error = nil;
   238     XCTAssertTrue([session mySelf:me error:&error]);
   239     XCTAssertNil(error);
   240 
   241     XCTAssertNotNil(me.fingerPrint);
   242     XCTAssertEqual([self ratingForIdentity:me session:session], PEPRatingTrustedAndAnonymized);
   243 
   244     PEPIdentity *alice = [self
   245                           checkImportingKeyFilePath:@"6FF00E97_sec.asc"
   246                           address:@"pep.test.alice@pep-project.org"
   247                           userID:@"This Is Alice"
   248                           fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
   249                           session: session];
   250     XCTAssertNotNil(alice);
   251     XCTAssertEqual([self ratingForIdentity:alice session:session], PEPRatingReliable);
   252 
   253     XCTAssertTrue([session trustPersonalKey:alice error:&error]);
   254     XCTAssertNil(error);
   255     XCTAssertEqual([self ratingForIdentity:alice session:session], PEPRatingTrusted);
   256 
   257     XCTAssertTrue([session keyResetTrust:alice error:&error]);
   258     XCTAssertNil(error);
   259     XCTAssertEqual([self ratingForIdentity:alice session:session], PEPRatingReliable);
   260 
   261     XCTAssertTrue([session keyMistrusted:alice error:&error]);
   262     XCTAssertNil(error);
   263     XCTAssertEqual([self ratingForIdentity:alice session:session], PEPRatingHaveNoKey);
   264 }
   265 
   266 /** ENGINE-384 */
   267 - (void)testIdentityRatingCrash
   268 {
   269     PEPSession *session = [PEPSession new];
   270 
   271     PEPIdentity *me = [[PEPIdentity alloc]
   272                        initWithAddress:@"me@example.org"
   273                        userID:@"me_myself"
   274                        userName:@"Me Me"
   275                        isOwn:YES];
   276     NSError *error = nil;
   277     XCTAssertTrue([session mySelf:me error:&error]);
   278     XCTAssertNil(error);
   279 
   280     XCTAssertNotNil(me.fingerPrint);
   281     XCTAssertEqual([self ratingForIdentity:me session:session], PEPRatingTrustedAndAnonymized);
   282 
   283     PEPIdentity *alice = [self
   284                           checkImportingKeyFilePath:@"6FF00E97_sec.asc"
   285                           address:@"pep.test.alice@pep-project.org"
   286                           userID:@"This Is Alice"
   287                           fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
   288                           session: session];
   289     XCTAssertNotNil(alice);
   290     XCTAssertEqual([self ratingForIdentity:alice session:session], PEPRatingReliable);
   291 
   292     XCTAssertTrue([session trustPersonalKey:alice error:&error]);
   293     XCTAssertNil(error);
   294     XCTAssertEqual([self ratingForIdentity:alice session:session], PEPRatingTrusted);
   295 
   296     XCTAssertTrue([session keyResetTrust:alice error:&error]);
   297     XCTAssertNil(error);
   298     XCTAssertEqual([self ratingForIdentity:alice session:session], PEPRatingReliable);
   299 
   300     XCTAssertTrue([session keyMistrusted:alice error:&error]);
   301     XCTAssertNil(error);
   302     XCTAssertEqual([self ratingForIdentity:alice session:session], PEPRatingHaveNoKey);
   303 }
   304 
   305 /**
   306  Try to provoke a SQLITE_BUSY (ENGINE-374)
   307  */
   308 - (void)testIdentityRatingTrustResetMistrustUndoBusy
   309 {
   310     PEPSession *session = [PEPSession new];
   311 
   312     PEPIdentity *me = [[PEPIdentity alloc]
   313                        initWithAddress:@"me@example.org"
   314                        userID:@"me_myself"
   315                        userName:@"Me Me"
   316                        isOwn:YES];
   317     NSError *error = nil;
   318     XCTAssertTrue([session mySelf:me error:&error]);
   319     XCTAssertNil(error);
   320 
   321     XCTAssertNotNil(me.fingerPrint);
   322     XCTAssertEqual([self ratingForIdentity:me session:session], PEPRatingTrustedAndAnonymized);
   323 
   324     PEPIdentity *alice = [self
   325                           checkImportingKeyFilePath:@"6FF00E97_sec.asc"
   326                           address:@"pep.test.alice@pep-project.org"
   327                           userID:@"This Is Alice"
   328                           fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
   329                           session: session];
   330     XCTAssertNotNil(alice);
   331     XCTAssertEqual([self ratingForIdentity:alice session:session], PEPRatingReliable);
   332 
   333     void (^encryptingBlock)(void) = ^{
   334         PEPSession *innerSession = [PEPSession new];
   335         PEPMessage *msg = [PEPMessage new];
   336         msg.from = me;
   337         msg.to = @[alice];
   338         msg.shortMessage = @"The subject";
   339         msg.longMessage = @"Lots and lots of text";
   340         msg.direction = PEPMsgDirectionIncoming;
   341 
   342         PEPStatus status;
   343         NSError *error = nil;
   344         PEPMessage *encMsg = [innerSession
   345                               encryptMessage:msg
   346                               forSelf:me
   347                               extraKeys:nil
   348                               status:&status error:&error];
   349         XCTAssertEqual(status, PEPStatusOK);
   350         XCTAssertNotNil(encMsg);
   351     };
   352 
   353     dispatch_group_t backgroundGroup = dispatch_group_create();
   354     dispatch_group_async(backgroundGroup,
   355                          dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), encryptingBlock);
   356 
   357     XCTAssertTrue([session trustPersonalKey:alice error:&error]);
   358     XCTAssertNil(error);
   359     XCTAssertEqual([self ratingForIdentity:alice session:session], PEPRatingTrusted);
   360 
   361     XCTAssertTrue([session keyResetTrust:alice error:&error]);
   362     XCTAssertNil(error);
   363     XCTAssertEqual([self ratingForIdentity:alice session:session], PEPRatingReliable);
   364 
   365     XCTAssertTrue([session keyMistrusted:alice error:&error]);
   366     XCTAssertNil(error);
   367     XCTAssertEqual([self ratingForIdentity:alice session:session], PEPRatingHaveNoKey);
   368 }
   369 
   370 - (void)testOutgoingColors
   371 {
   372     PEPSession *session = [PEPSession new];
   373 
   374     // Our test user :
   375     // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
   376     // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
   377     XCTAssertTrue([PEPTestUtils importBundledKey:@"6FF00E97_sec.asc" session:session]);
   378 
   379     // Our test user :
   380     PEPIdentity *identAlice = [self
   381                                checkMySelfImportingKeyFilePath:@"6FF00E97_sec.asc"
   382                                address:@"pep.test.alice@pep-project.org"
   383                                userID:@"Alice_User_ID"
   384                                fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
   385                                session:session];
   386 
   387     //Message
   388 
   389     {
   390         PEPIdentity *identUnknownBob = [[PEPIdentity alloc]
   391                                         initWithAddress:@"pep.test.unknown.bob@pep-project.org"
   392                                         userID:@"4242" userName:@"pEp Test Bob Unknown"
   393                                         isOwn:NO];
   394 
   395         PEPMessage *msgGray = [PEPMessage new];
   396         msgGray.from = identAlice;
   397         msgGray.to = @[identUnknownBob];
   398         msgGray.shortMessage = @"All Gray Test";
   399         msgGray.longMessage = @"This is a text content";
   400         msgGray.direction = PEPMsgDirectionOutgoing;
   401 
   402         NSError *error = nil;
   403 
   404         // Test with unknown Bob
   405         NSNumber *numRating = [self
   406                                testOutgoingRatingForMessage:msgGray
   407                                session:session
   408                                error:&error];
   409         XCTAssertNotNil(numRating);
   410         XCTAssertNil(error);
   411         XCTAssertEqual(numRating.pEpRating, PEPRatingUnencrypted);
   412     }
   413 
   414     PEPIdentity *identBob = [self
   415                              checkImportingKeyFilePath:@"0xC9C2EE39.asc"
   416                              address:@"pep.test.bob@pep-project.org"
   417                              userID:@"42"
   418                              fingerPrint:@"BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39"
   419                              session: session];
   420     XCTAssertNotNil(identBob);
   421 
   422     PEPMessage *msg = [PEPMessage new];
   423     msg.from = identAlice;
   424     msg.to = @[identBob];
   425     msg.shortMessage = @"All Gray Test";
   426     msg.longMessage = @"This is a text content";
   427     msg.direction = PEPMsgDirectionOutgoing;
   428 
   429     NSError *error = nil;
   430 
   431     // Should be yellow, since no handshake happened.
   432     NSNumber *numRating = [self testOutgoingRatingForMessage:msg session:session error:&error];
   433     XCTAssertNotNil(numRating);
   434     XCTAssertNil(error);
   435     XCTAssertEqual(numRating.pEpRating, PEPRatingReliable);
   436 
   437     PEPRating rating = [self ratingForIdentity:identBob session:session];
   438     XCTAssertEqual(rating, PEPRatingReliable);
   439 
   440     // Let' say we got that handshake, set PEP_ct_confirmed in Bob's identity
   441     XCTAssertTrue([session trustPersonalKey:identBob error:&error]);
   442     XCTAssertNil(error);
   443 
   444     // This time it should be green
   445     numRating = [self testOutgoingRatingForMessage:msg session:session error:&error];
   446     XCTAssertNotNil(numRating);
   447     XCTAssertNil(error);
   448     XCTAssertEqual(numRating.pEpRating, PEPRatingTrusted);
   449 
   450     rating = [self ratingForIdentity:identBob session:session];
   451     XCTAssertEqual(rating, PEPRatingTrusted);
   452 
   453     // Let' say we undo handshake
   454     XCTAssertTrue([session keyResetTrust:identBob error:&error]);
   455     XCTAssertNil(error);
   456 
   457     // Yellow ?
   458     numRating = [self testOutgoingRatingForMessage:msg session:session error:&error];
   459     XCTAssertNotNil(numRating);
   460     XCTAssertNil(error);
   461     XCTAssertEqual(numRating.pEpRating, PEPRatingReliable);
   462 
   463     // mistrust Bob
   464     XCTAssertTrue([session keyMistrusted:identBob error:&error]);
   465     XCTAssertNil(error);
   466 
   467     identBob.fingerPrint = nil;
   468     XCTAssertTrue([session updateIdentity:identBob error:&error]);
   469     XCTAssertNil(error);
   470     XCTAssertNil(identBob.fingerPrint);
   471 
   472     // Gray == PEPRatingUnencrypted
   473     numRating = [self testOutgoingRatingForMessage:msg session:session error:&error];
   474     XCTAssertNotNil(numRating);
   475     XCTAssertNil(error);
   476     XCTAssertEqual(numRating.pEpRating, PEPRatingUnencrypted);
   477 }
   478 
   479 
   480 - (void)testOutgoingBccColors
   481 {
   482     PEPSession *session = [PEPSession new];
   483 
   484     // Our test user :
   485     // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
   486     // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
   487     XCTAssertTrue([PEPTestUtils importBundledKey:@"6FF00E97_sec.asc" session:session]);
   488 
   489     PEPIdentity *identAlice = [[PEPIdentity alloc]
   490                                initWithAddress:@"pep.test.alice@pep-project.org"
   491                                userID:ownUserId
   492                                userName:@"pEp Test Alice"
   493                                isOwn:YES
   494                                fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
   495 
   496     NSError *error = nil;
   497     XCTAssertTrue([session mySelf:identAlice error:&error]);
   498     XCTAssertNil(error);
   499 
   500     PEPMessage *msg = [PEPMessage new];
   501     msg.from = identAlice;
   502     msg.to = @[[[PEPIdentity alloc] initWithAddress:@"pep.test.bob@pep-project.org"
   503                                              userID:@"42" userName:@"pEp Test Bob" isOwn:NO]];
   504     msg.shortMessage = @"All Green Test";
   505     msg.longMessage = @"This is a text content";
   506     msg.direction = PEPMsgDirectionOutgoing;
   507 
   508     // Test with unknown Bob
   509     PEPRating rating;
   510     NSNumber *numRating = [self testOutgoingRatingForMessage:msg session:session error:&error];
   511     XCTAssertNotNil(numRating);
   512     XCTAssertNil(error);
   513     XCTAssertEqual(numRating.pEpRating, PEPRatingUnencrypted);
   514 
   515     // Now let see with bob's pubkey already known
   516     // pEp Test Bob (test key, don't use) <pep.test.bob@pep-project.org>
   517     // BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39
   518     XCTAssertTrue([PEPTestUtils importBundledKey:@"0xC9C2EE39.asc" session:session]);
   519 
   520     PEPIdentity *identBob = [[PEPIdentity alloc]
   521                              initWithAddress:@"pep.test.bob@pep-project.org"
   522                              userID:@"42" userName:@"pEp Test Bob"
   523                              isOwn:NO
   524                              fingerPrint:@"BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39"];
   525 
   526     XCTAssertTrue([session updateIdentity:identBob error:&error]);
   527     XCTAssertNil(error);
   528 
   529     // Should be yellow, since no handshake happened.
   530     numRating = [self testOutgoingRatingForMessage:msg session:session error:&error];
   531     XCTAssertNotNil(numRating);
   532     XCTAssertNil(error);
   533     XCTAssertEqual(numRating.pEpRating, PEPRatingReliable);
   534 
   535     rating = [self ratingForIdentity:identBob session:session];
   536     XCTAssertEqual(rating, PEPRatingReliable);
   537 
   538     // Let' say we got that handshake, set PEP_ct_confirmed in Bob's identity
   539     XCTAssertTrue([session trustPersonalKey:identBob error:&error]);
   540     XCTAssertNil(error);
   541 
   542     // This time it should be green
   543     numRating = [self testOutgoingRatingForMessage:msg session:session error:&error];
   544     XCTAssertNotNil(numRating);
   545     XCTAssertNil(error);
   546     XCTAssertEqual(numRating.pEpRating, PEPRatingTrusted);
   547 
   548     rating = [self ratingForIdentity:identBob session:session];
   549     XCTAssertEqual(rating, PEPRatingTrusted);
   550 
   551     // Now let see if it turns back yellow if we add an unconfirmed folk.
   552     // pEp Test John (test key, don't use) <pep.test.john@pep-project.org>
   553     // AA2E4BEB93E5FE33DEFD8BE1135CD6D170DCF575
   554     XCTAssertTrue([PEPTestUtils importBundledKey:@"0x70DCF575.asc" session:session]);
   555 
   556     PEPIdentity *identJohn = [[PEPIdentity alloc]
   557                               initWithAddress:@"pep.test.john@pep-project.org"
   558                               userID:@"101" userName:@"pEp Test John"
   559                               isOwn:NO
   560                               fingerPrint:@"AA2E4BEB93E5FE33DEFD8BE1135CD6D170DCF575"];
   561 
   562     XCTAssertTrue([session updateIdentity:identJohn error:&error]);
   563     XCTAssertNil(error);
   564 
   565     msg.bcc = @[[[PEPIdentity alloc] initWithAddress:@"pep.test.john@pep-project.org"
   566                                               userID:@"101" userName:@"pEp Test John" isOwn:NO]];
   567 
   568     // Yellow ?
   569     numRating = [self testOutgoingRatingForMessage:msg session:session error:&error];
   570     XCTAssertNotNil(numRating);
   571     XCTAssertNil(error);
   572     XCTAssertEqual(numRating.pEpRating, PEPRatingReliable);
   573 
   574     XCTAssertTrue([session trustPersonalKey:identJohn error:&error]);
   575     XCTAssertNil(error);
   576 
   577     // This time it should be green
   578     numRating = [self testOutgoingRatingForMessage:msg session:session error:&error];
   579     XCTAssertNotNil(numRating);
   580     XCTAssertNil(error);
   581     XCTAssertEqual(numRating.pEpRating, PEPRatingTrusted);
   582 
   583     rating = [self ratingForIdentity:identJohn session:session];
   584     XCTAssertEqual(rating, PEPRatingTrusted);
   585 }
   586 
   587 - (void)testDontEncryptForMistrusted
   588 {
   589     PEPSession *session = [PEPSession new];
   590 
   591     // Our test user :
   592     // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
   593     // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
   594     XCTAssertTrue([PEPTestUtils importBundledKey:@"6FF00E97_sec.asc" session:session]);
   595 
   596     PEPIdentity *identAlice = [[PEPIdentity alloc]
   597                                initWithAddress:@"pep.test.alice@pep-project.org"
   598                                userID:ownUserId
   599                                userName:@"pEp Test Alice"
   600                                isOwn:YES
   601                                fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
   602 
   603     NSError *error = nil;
   604     XCTAssertTrue([session mySelf:identAlice error:&error]);
   605     XCTAssertNil(error);
   606 
   607     // pEp Test Bob (test key, don't use) <pep.test.bob@pep-project.org>
   608     // BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39
   609     XCTAssertTrue([PEPTestUtils importBundledKey:@"0xC9C2EE39.asc" session:session]);
   610 
   611     PEPIdentity *identBob = [[PEPIdentity alloc]
   612                              initWithAddress:@"pep.test.bob@pep-project.org"
   613                              userID:@"42" userName:@"pEp Test Bob"
   614                              isOwn:NO
   615                              fingerPrint:@"BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39"];
   616 
   617     XCTAssertTrue([session updateIdentity:identBob error:&error]);
   618     XCTAssertNil(error);
   619 
   620     // mistrust Bob
   621     XCTAssertTrue([session keyMistrusted:identBob error:&error]);
   622     XCTAssertNil(error);
   623 
   624     PEPMessage *msg = [PEPMessage new];
   625     msg.from = identAlice;
   626     msg.to = @[[[PEPIdentity alloc] initWithAddress:@"pep.test.bob@pep-project.org" userID:@"42"
   627                                            userName:@"pEp Test Bob" isOwn:NO]];
   628     msg.shortMessage = @"All Green Test";
   629     msg.longMessage = @"This is a text content";
   630     msg.direction = PEPMsgDirectionOutgoing;
   631 
   632     // Gray == PEPRatingUnencrypted
   633     NSNumber *numRating = [self testOutgoingRatingForMessage:msg session:session error:&error];
   634     XCTAssertNotNil(numRating);
   635     XCTAssertNil(error);
   636     XCTAssertEqual(numRating.pEpRating, PEPRatingUnencrypted);
   637 
   638     PEPMessage *encMsg = [session encryptMessage:msg extraKeys:nil status:nil error:&error];
   639     XCTAssertNotNil(encMsg);
   640     XCTAssertNil(error);
   641 
   642     XCTAssertNotEqualObjects(encMsg.attachments[0].mimeType, @"application/pgp-encrypted");
   643 
   644     [self pEpCleanUp];
   645 }
   646 
   647 - (void)testRevoke
   648 {
   649     PEPSession *session = [PEPSession new];
   650 
   651     // Our test user :
   652     // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
   653     // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
   654     XCTAssertTrue([PEPTestUtils importBundledKey:@"6FF00E97_sec.asc" session:session]);
   655     NSString *fpr = @"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97";
   656 
   657     PEPIdentity *identAlice = [[PEPIdentity alloc]
   658                                initWithAddress:@"pep.test.alice@pep-project.org"
   659                                userID:ownUserId
   660                                userName:@"pEp Test Alice"
   661                                isOwn:YES
   662                                fingerPrint:fpr];
   663 
   664     NSError *error = nil;
   665     XCTAssertTrue([session mySelf:identAlice error:&error]);
   666     XCTAssertNil(error);
   667 
   668     PEPIdentity *identAlice2 = [identAlice mutableCopy];
   669 
   670     // This will revoke key
   671     XCTAssertTrue([session keyMistrusted:identAlice2 error:&error]);
   672     XCTAssertNil(error);
   673     identAlice2.fingerPrint = nil;
   674 
   675     XCTAssertTrue([session mySelf:identAlice error:&error]);
   676     XCTAssertNil(error);
   677 
   678     // Check fingerprint is different
   679     XCTAssertNotEqualObjects(identAlice2.fingerPrint, fpr);
   680 }
   681 
   682 - (void)testMailToMyself
   683 {
   684     PEPSession *session = [PEPSession new];
   685 
   686     // Our test user :
   687     // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
   688     // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
   689     XCTAssertTrue([PEPTestUtils importBundledKey:@"6FF00E97_sec.asc" session:session]);
   690 
   691     PEPIdentity *identAlice = [[PEPIdentity alloc]
   692                                initWithAddress:@"pep.test.alice@pep-project.org"
   693                                userID:ownUserId
   694                                userName:@"pEp Test Alice"
   695                                isOwn:YES
   696                                fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
   697 
   698     NSError *error = nil;
   699     XCTAssertTrue([session mySelf:identAlice error:&error]);
   700     XCTAssertNil(error);
   701 
   702     PEPMessage *msg = [PEPMessage new];
   703     msg.from = identAlice;
   704     msg.to = @[identAlice];
   705     msg.shortMessage = @"Mail to Myself";
   706     msg.longMessage = @"This is a text content";
   707     msg.direction = PEPMsgDirectionOutgoing;
   708 
   709     NSNumber *numRating = [self testOutgoingRatingForMessage:msg session:session error:&error];
   710     XCTAssertNotNil(numRating);
   711     XCTAssertNil(error);
   712     XCTAssertEqual(numRating.pEpRating, PEPRatingTrustedAndAnonymized);
   713 
   714     PEPMessage *encMsg = [session encryptMessage:msg extraKeys:nil status:nil error:&error];
   715     XCTAssertNotNil(encMsg);
   716     XCTAssertNil(error);
   717 
   718     NSArray *keys;
   719 
   720     error = nil;
   721     PEPRating rating = PEPRatingUndefined;
   722     PEPMessage *decmsg = [session
   723                           decryptMessage:encMsg
   724                           flags:nil
   725                           rating:&rating
   726                           extraKeys:&keys
   727                           status:nil
   728                           error:&error];
   729     XCTAssertNotNil(decmsg);
   730     XCTAssertNil(error);
   731     XCTAssertEqual(rating, PEPRatingTrustedAndAnonymized);
   732 }
   733 
   734 - (void)testEncryptedMailFromMuttWithReencryption
   735 {
   736     PEPSession *session = [PEPSession new];
   737 
   738     // This is the public key for test001@peptest.ch
   739     XCTAssertTrue([PEPTestUtils importBundledKey:@"A3FC7F0A.asc" session:session]);
   740 
   741     // This is the secret key for test001@peptest.ch
   742     XCTAssertTrue([PEPTestUtils importBundledKey:@"A3FC7F0A_sec.asc" session:session]);
   743 
   744     // Mail from mutt, already processed into message dict by the app.
   745     NSMutableDictionary *msgDict = [[PEPTestUtils
   746                                      unarchiveDictionary:@"msg_to_A3FC7F0A_from_mutt.ser"]
   747                                     mutableCopy];
   748     [PEPTestUtils migrateUnarchivedMessageDictionary:msgDict];
   749     [msgDict removeObjectForKey:kPepLongMessage];
   750     [msgDict removeObjectForKey:kPepLongMessageFormatted];
   751 
   752     // Also extracted "live" from the app.
   753     NSMutableDictionary *accountDict = [[PEPTestUtils
   754                                          unarchiveDictionary:@"account_A3FC7F0A.ser"]
   755                                         mutableCopy];
   756     [accountDict removeObjectForKey:kPepCommType];
   757     [accountDict removeObjectForKey:kPepFingerprint];
   758     PEPIdentity *identMe = [[PEPIdentity alloc] initWithDictionary:accountDict];
   759 
   760     NSError *error = nil;
   761     XCTAssertTrue([session mySelf:identMe error:&error]);
   762     XCTAssertNil(error);
   763 
   764     XCTAssertNotNil(identMe.fingerPrint);
   765 
   766     NSArray* keys;
   767     PEPMessage *msg = [PEPMessage new];
   768     [msg setValuesForKeysWithDictionary:msgDict];
   769     PEPMessage *msgOriginal = [PEPMessage new];
   770     [msgOriginal setValuesForKeysWithDictionary:msgDict];
   771 
   772     XCTAssertEqualObjects(msg, msgOriginal);
   773 
   774     PEPRating rating = PEPRatingUndefined;
   775     PEPDecryptFlags flags = PEPDecryptFlagsUntrustedServer;
   776 
   777     PEPMessage *pepDecryptedMail = [session
   778                                     decryptMessage:msg
   779                                     flags:&flags
   780                                     rating:&rating
   781                                     extraKeys:&keys
   782                                     status:nil
   783                                     error:&error];
   784     XCTAssertNotNil(pepDecryptedMail);
   785     XCTAssertNil(error);
   786 
   787     // Technically, the mail is encrypted, but the signatures don't match
   788     XCTAssertEqual(rating, PEPRatingUnreliable);
   789 
   790     // Since we're requesting re-encryption, src should have been changed
   791     XCTAssertNotEqualObjects(msg, msgOriginal);
   792 
   793     XCTAssertNotNil(pepDecryptedMail.longMessage);
   794 }
   795 
   796 - (void)testOutgoingContactColor
   797 {
   798     PEPSession *session = [PEPSession new];
   799 
   800     PEPIdentity *partner1Orig = [PEPTestUtils foreignPepIdentityWithAddress:@"partner1@dontcare.me"
   801                                                                    userName:@"Partner 1"];
   802     NSString *pubKeyPartner1 = [PEPTestUtils loadResourceByName:@"partner1_F2D281C2789DD7F6_pub.asc"];
   803     XCTAssertNotNil(pubKeyPartner1);
   804 
   805     NSError *error = nil;
   806     XCTAssertTrue([session importKey:pubKeyPartner1 error:&error]);
   807     XCTAssertNil(error);
   808 
   809     PEPRating color = [self ratingForIdentity:partner1Orig session:session];
   810     XCTAssertEqual(color, PEPRatingReliable);
   811 }
   812 
   813 - (void)testGetTrustwords
   814 {
   815     PEPSession *session = [PEPSession new];
   816 
   817     PEPIdentity *partner1Orig = [[PEPIdentity alloc]
   818                                  initWithAddress:@"partner1@dontcare.me" userID:@"partner1"
   819                                  userName:@"partner1"
   820                                  isOwn:NO fingerPrint:@"F0CD3F7B422E5D587ABD885BF2D281C2789DD7F6"];
   821 
   822     PEPIdentity *meOrig = [[PEPIdentity alloc]
   823                            initWithAddress:@"me@dontcare.me" userID:@"me"
   824                            userName:@"me"
   825                            isOwn:NO fingerPrint:@"CC1F73F6FB774BF08B197691E3BFBCA9248FC681"];
   826 
   827     NSString *pubKeyPartner1 = [PEPTestUtils loadResourceByName:@"partner1_F2D281C2789DD7F6_pub.asc"];
   828     XCTAssertNotNil(pubKeyPartner1);
   829     NSString *pubKeyMe = [PEPTestUtils loadResourceByName:@"meATdontcare_E3BFBCA9248FC681_pub.asc"];
   830     XCTAssertNotNil(pubKeyMe);
   831     NSString *secKeyMe = [PEPTestUtils loadResourceByName:@"meATdontcare_E3BFBCA9248FC681_sec.asc"];
   832     XCTAssertNotNil(secKeyMe);
   833 
   834     NSError *error = nil;
   835     NSString *trustwordsFull = [session getTrustwordsIdentity1:meOrig identity2:partner1Orig
   836                                                       language:@"en" full:YES error:&error];
   837     XCTAssertNil(error);
   838     XCTAssertEqualObjects(trustwordsFull,
   839                           @"EMERSON GASPER TOKENISM BOLUS COLLAGE DESPISE BEDDED ENCRYPTION IMAGINE BEDFORD");
   840 
   841     NSString *trustwordsUndefined = [session getTrustwordsIdentity1:meOrig identity2:partner1Orig
   842                                                            language:@"ZZ" full:YES error:&error];
   843     XCTAssertNotNil(error);
   844     XCTAssertNil(trustwordsUndefined);
   845 }
   846 
   847 - (void)testStringToRating
   848 {
   849     PEPSession *session = [PEPSession new];
   850     XCTAssertEqual([session ratingFromString:@"cannot_decrypt"], PEPRatingCannotDecrypt);
   851     XCTAssertEqual([session ratingFromString:@"have_no_key"], PEPRatingHaveNoKey);
   852     XCTAssertEqual([session ratingFromString:@"unencrypted"], PEPRatingUnencrypted);
   853     XCTAssertEqual([session ratingFromString:@"unencrypted_for_some"],
   854                    PEPRatingUnencryptedForSome);
   855     XCTAssertEqual([session ratingFromString:@"unreliable"], PEPRatingUnreliable);
   856     XCTAssertEqual([session ratingFromString:@"reliable"], PEPRatingReliable);
   857     XCTAssertEqual([session ratingFromString:@"trusted"], PEPRatingTrusted);
   858     XCTAssertEqual([session ratingFromString:@"trusted_and_anonymized"],
   859                    PEPRatingTrustedAndAnonymized);
   860     XCTAssertEqual([session ratingFromString:@"fully_anonymous"], PEPRatingFullyAnonymous);
   861     XCTAssertEqual([session ratingFromString:@"mistrust"], PEPRatingMistrust);
   862     XCTAssertEqual([session ratingFromString:@"b0rken"], PEPRatingB0rken);
   863     XCTAssertEqual([session ratingFromString:@"under_attack"], PEPRatingUnderAttack);
   864     XCTAssertEqual([session ratingFromString:@"undefined"], PEPRatingUndefined);
   865     XCTAssertEqual([session ratingFromString:@"does not exist111"], PEPRatingUndefined);
   866 }
   867 
   868 - (void)testRatingToString
   869 {
   870     PEPSession *session = [PEPSession new];
   871     XCTAssertEqualObjects([session stringFromRating:PEPRatingCannotDecrypt], @"cannot_decrypt");
   872     XCTAssertEqualObjects([session stringFromRating:PEPRatingHaveNoKey], @"have_no_key");
   873     XCTAssertEqualObjects([session stringFromRating:PEPRatingUnencrypted], @"unencrypted");
   874     XCTAssertEqualObjects([session stringFromRating:PEPRatingUnencryptedForSome],
   875                           @"unencrypted_for_some");
   876     XCTAssertEqualObjects([session stringFromRating:PEPRatingUnreliable], @"unreliable");
   877     XCTAssertEqualObjects([session stringFromRating:PEPRatingReliable], @"reliable");
   878     XCTAssertEqualObjects([session stringFromRating:PEPRatingTrusted], @"trusted");
   879     XCTAssertEqualObjects([session stringFromRating:PEPRatingTrustedAndAnonymized],
   880                           @"trusted_and_anonymized");
   881     XCTAssertEqualObjects([session stringFromRating:PEPRatingFullyAnonymous],
   882                           @"fully_anonymous");
   883     XCTAssertEqualObjects([session stringFromRating:PEPRatingMistrust], @"mistrust");
   884     XCTAssertEqualObjects([session stringFromRating:PEPRatingB0rken], @"b0rken");
   885     XCTAssertEqualObjects([session stringFromRating:PEPRatingUnderAttack], @"under_attack");
   886     XCTAssertEqualObjects([session stringFromRating:PEPRatingUndefined], @"undefined");
   887     XCTAssertEqualObjects([session stringFromRating:500], @"undefined");
   888 }
   889 
   890 - (void)testIsPEPUser
   891 {
   892     PEPSession *session = [PEPSession new];
   893 
   894     PEPIdentity *identMe = [[PEPIdentity alloc]
   895                             initWithAddress:@"me-myself-and-i@pep-project.org"
   896                             userID:@"me-myself-and-i"
   897                             userName:@"pEp Me"
   898                             isOwn:YES];
   899     NSError *error = nil;
   900     XCTAssertTrue([session mySelf:identMe error:&error]);
   901     XCTAssertNil(error);
   902 
   903     XCTAssertNotNil(identMe.fingerPrint);
   904 
   905     // PEP_CANNOT_FIND_PERSON == 902
   906     XCTAssertTrue([session isPEPUser:identMe error:&error]);
   907 }
   908 
   909 - (void)testXEncStatusForOutgoingEncryptedMail
   910 {
   911     [self helperXEncStatusForOutgoingEncryptdMailToSelf:NO expectedRating:PEPRatingReliable];
   912 }
   913 
   914 - (void)testXEncStatusForOutgoingSelfEncryptedMail
   915 {
   916     [self helperXEncStatusForOutgoingEncryptdMailToSelf:YES
   917                                          expectedRating:PEPRatingTrustedAndAnonymized];
   918 }
   919 
   920 - (void)testEncryptMessagesWithoutKeys
   921 {
   922     PEPSession *session = [PEPSession new];
   923 
   924     PEPIdentity *identMe = [[PEPIdentity alloc]
   925                             initWithAddress:@"me-myself-and-i@pep-project.org"
   926                             userID:@"me-myself-and-i"
   927                             userName:@"pEp Me"
   928                             isOwn:YES];
   929     NSError *error = nil;
   930     XCTAssertTrue([session mySelf:identMe error:&error]);
   931     XCTAssertNil(error);
   932 
   933     XCTAssertNotNil(identMe.fingerPrint);
   934 
   935     PEPIdentity *identAlice = [[PEPIdentity alloc]
   936                                initWithAddress:@"alice@pep-project.org"
   937                                userID:@"alice"
   938                                userName:@"pEp Test Alice"
   939                                isOwn:NO];
   940 
   941     PEPMessage *msg = [PEPMessage new];
   942     msg.from = identMe;
   943     msg.to = @[identAlice];
   944     msg.shortMessage = @"Mail to Alice";
   945     msg.longMessage = @"Alice?";
   946     msg.direction = PEPMsgDirectionOutgoing;
   947 
   948     NSNumber *numRating = [self testOutgoingRatingForMessage:msg session:session error:&error];
   949     XCTAssertNotNil(numRating);
   950     XCTAssertNil(error);
   951     XCTAssertEqual(numRating.pEpRating, PEPRatingUnencrypted);
   952 
   953     PEPMessage *encMsg = [session encryptMessage:msg extraKeys:nil status:nil error:&error];
   954     XCTAssertNotNil(encMsg);
   955     XCTAssertNil(error);
   956 
   957     XCTAssertNotNil(encMsg);
   958 
   959     PEPStringList *keys;
   960     PEPRating pEpRating;
   961     error = nil;
   962     PEPMessage *decMsg = [session
   963                           decryptMessage:encMsg
   964                           flags:nil
   965                           rating:&pEpRating
   966                           extraKeys:&keys
   967                           status:nil
   968                           error:&error];
   969     XCTAssertNotNil(decMsg);
   970     XCTAssertNil(error);
   971 
   972     XCTAssertEqual(pEpRating, PEPRatingUnencrypted);
   973     XCTAssertNotNil(decMsg);
   974 }
   975 
   976 /**
   977  ENGINE-364. Tries to invoke trustPersonalKey on an identity without key,
   978  giving it a fake fingerprint.
   979  */
   980 - (void)testTrustPersonalKey
   981 {
   982     PEPSession *session = [PEPSession new];
   983 
   984     PEPIdentity *identMe = [[PEPIdentity alloc]
   985                             initWithAddress:@"me-myself-and-i@pep-project.org"
   986                             userID:@"me-myself-and-i"
   987                             userName:@"pEp Me"
   988                             isOwn:YES];
   989     NSError *error = nil;
   990     XCTAssertTrue([session mySelf:identMe error:&error]);
   991     XCTAssertNil(error);
   992 
   993     XCTAssertNotNil(identMe.fingerPrint);
   994 
   995     // The fingerprint is definitely wrong, we don't have a key
   996     PEPIdentity *identAlice = [[PEPIdentity alloc]
   997                                initWithAddress:@"alice@pep-project.org"
   998                                userID:@"alice"
   999                                userName:@"pEp Test Alice"
  1000                                isOwn:NO
  1001                                fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
  1002 
  1003     XCTAssertFalse([session trustPersonalKey:identAlice error:&error]);
  1004     XCTAssertNotNil(error);
  1005 }
  1006 
  1007 /**
  1008  ENGINE-381
  1009  */
  1010 - (void)testVolatileIdentityRating
  1011 {
  1012     PEPSession *session = [PEPSession new];
  1013 
  1014     PEPIdentity *identMe = [[PEPIdentity alloc]
  1015                             initWithAddress:@"me-myself-and-i@pep-project.org"
  1016                             userID:@"me-myself-and-i"
  1017                             userName:@"pEp Me"
  1018                             isOwn:YES];
  1019     NSError *error = nil;
  1020     XCTAssertTrue([session mySelf:identMe error:&error]);
  1021     XCTAssertNil(error);
  1022 
  1023     XCTAssertNotNil(identMe.fingerPrint);
  1024 
  1025     PEPIdentity *identAlice = [self
  1026                                checkImportingKeyFilePath:@"6FF00E97_sec.asc"
  1027                                address:@"pep.test.alice@pep-project.org"
  1028                                userID:@"alice_user_id"
  1029                                fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
  1030                                session: session];
  1031     XCTAssertNotNil(identAlice);
  1032 
  1033     dispatch_group_t identityRatingGroup = dispatch_group_create();
  1034 
  1035     void (^ratingBlock)(void) = ^{
  1036         PEPSession *innerSession = [PEPSession new];
  1037         PEPRating rating = [self ratingForIdentity:identAlice session:innerSession];
  1038         XCTAssertEqual(rating, PEPRatingReliable);
  1039     };
  1040 
  1041     for (int i = 0; i < 4; ++i) {
  1042         dispatch_group_async(identityRatingGroup,
  1043                              dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0),
  1044                              ratingBlock);
  1045     }
  1046 
  1047     for (int i = 0; i < 4; ++i) {
  1048         ratingBlock();
  1049     }
  1050 
  1051     dispatch_group_wait(identityRatingGroup, DISPATCH_TIME_FOREVER);
  1052 }
  1053 
  1054 /**
  1055  IOSAD-93, testing for easy error case.
  1056  */
  1057 - (void)testEncryptAndAttachPrivateKeyIllegalValue
  1058 {
  1059     PEPSession *session = [PEPSession new];
  1060 
  1061     PEPIdentity *identMe = [[PEPIdentity alloc]
  1062                             initWithAddress:@"me-myself-and-i@pep-project.org"
  1063                             userID:@"me-myself-and-i"
  1064                             userName:@"pEp Me"
  1065                             isOwn:YES];
  1066     NSError *error = nil;
  1067     XCTAssertTrue([session mySelf:identMe error:&error]);
  1068     XCTAssertNil(error);
  1069 
  1070     XCTAssertNotNil(identMe.fingerPrint);
  1071 
  1072     NSString *fprAlice = @"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97";
  1073     PEPIdentity *identAlice = [self
  1074                                checkImportingKeyFilePath:@"6FF00E97_sec.asc"
  1075                                address:@"pep.test.alice@pep-project.org"
  1076                                userID:@"alice_user_id"
  1077                                fingerPrint:fprAlice
  1078                                session: session];
  1079     XCTAssertNotNil(identAlice);
  1080 
  1081     NSString *shortMessage = @"whatever it may be";
  1082     NSString *longMessage = [NSString stringWithFormat:@"%@ %@", shortMessage, shortMessage];
  1083     PEPMessage *message = [PEPMessage new];
  1084     message.from = identMe;
  1085     message.to = @[identAlice];
  1086     message.shortMessage = shortMessage;
  1087     message.longMessage = longMessage;
  1088 
  1089     PEPStatus status = PEPStatusKeyNotFound;
  1090     error = nil;
  1091     PEPMessage *encrypted = [session
  1092                              encryptMessage:message
  1093                              toFpr:fprAlice
  1094                              encFormat:PEPEncFormatPEP
  1095                              flags:0
  1096                              status:&status error:&error];
  1097     XCTAssertEqual(status, PEPStatusIllegalValue);
  1098     XCTAssertNotNil(error);
  1099     XCTAssertNil(encrypted);
  1100 }
  1101 
  1102 - (void)testSetIdentityFlags
  1103 {
  1104     PEPSession *session = [PEPSession new];
  1105 
  1106     PEPIdentity *me = [PEPTestUtils ownPepIdentityWithAddress:@"me@peptest.ch"
  1107                                                      userName:@"userName"];
  1108     NSError *error = nil;
  1109     XCTAssertTrue([session mySelf:me error:&error]);
  1110     XCTAssertNil(error);
  1111 
  1112     PEPIdentityFlags theFlags[] = {
  1113         PEPIdentityFlagsNotForSync,
  1114         PEPIdentityFlagsList,
  1115         PEPIdentityFlagsDeviceGroup,
  1116         0
  1117     };
  1118 
  1119     for (int i = 0;; ++i) {
  1120         PEPIdentityFlags aFlag = theFlags[i];
  1121         if (aFlag == 0) {
  1122             break;
  1123         }
  1124         error = nil;
  1125         XCTAssertTrue([session setFlags:(PEPIdentityFlags) aFlag forIdentity:me error:&error]);
  1126         XCTAssertNil(error);
  1127     }
  1128 }
  1129 
  1130 - (void)testTrustOwnKey
  1131 {
  1132     PEPSession *session = [PEPSession new];
  1133 
  1134     PEPIdentity *me = [PEPTestUtils ownPepIdentityWithAddress:@"me@peptest.ch"
  1135                                                      userName:@"userName"];
  1136     NSError *error = nil;
  1137     XCTAssertTrue([session mySelf:me error:&error]);
  1138     XCTAssertNil(error);
  1139 
  1140     XCTAssertTrue([session trustOwnKeyIdentity:me error:&error]);
  1141     XCTAssertNil(error);
  1142 }
  1143 
  1144 #pragma mark - configUnencryptedSubject
  1145 
  1146 - (void)testConfigUnencryptedSubject
  1147 {
  1148     // Setup Config to encrypt subject
  1149     [PEPObjCAdapter setUnEncryptedSubjectEnabled:NO];
  1150 
  1151     // Write mail to yourself ...
  1152     PEPMessage *encMessage = [self mailWrittenToMySelf];
  1153 
  1154     // ... and assert subject is encrypted
  1155     XCTAssertEqualObjects(encMessage.shortMessage, @"p≡p", @"Subject should be encrypted");
  1156 }
  1157 
  1158 - (void)testConfigUnencryptedSubjectEncryptedSubjectDisabled
  1159 {
  1160     // Setup Config to not encrypt subject
  1161     [PEPObjCAdapter setUnEncryptedSubjectEnabled:YES];
  1162 
  1163     // Write mail to yourself ...
  1164     PEPMessage *encMessage = [self mailWrittenToMySelf];
  1165 
  1166     // pEp to pEp uses message 2.0, which always encrypts subjects (ENGINE-429)
  1167     XCTAssertEqualObjects(encMessage.shortMessage, @"p≡p", @"Subject should be encrypted");
  1168 }
  1169 
  1170 #pragma mark - Passive mode
  1171 
  1172 - (void)testPassiveMode
  1173 {
  1174     [self testPassiveModeEnabled:NO];
  1175     [self testPassiveModeEnabled:YES];
  1176 }
  1177 
  1178 #pragma mark - Decryption
  1179 
  1180 - (void)testDecryptionOfUnencryptedMessageWithOdtAttachmentContainingSpace
  1181 {
  1182     PEPSession *session = [PEPSession new];
  1183 
  1184     PEPIdentity *me = [PEPTestUtils ownPepIdentityWithAddress:@"me@peptest.ch"
  1185                                                      userName:@"userName"];
  1186     NSError *error = nil;
  1187     XCTAssertTrue([session mySelf:me error:&error]);
  1188     XCTAssertNil(error);
  1189 
  1190     const char *dataString = "blahblah";
  1191     const size_t dataSize = strlen(dataString);
  1192     char *rawData = strndup(dataString, dataSize);
  1193 
  1194     PEPAttachment *attachment = [[PEPAttachment alloc]
  1195                                  initWithData:[NSData
  1196                                                dataWithBytesNoCopy:rawData length:dataSize]];
  1197     attachment.filename = @"Someone andTextIncludingTheSpace.odt";
  1198     attachment.mimeType = @"application/vnd.oasis.opendocument.text";
  1199 
  1200     NSString *shortMessage = @"Subject";
  1201     NSString *longMessage = @"Oh, this is a long body text!";
  1202     PEPMessage *mail = [PEPTestUtils mailFrom:me
  1203                                       toIdent:me
  1204                                  shortMessage:shortMessage
  1205                                   longMessage:longMessage
  1206                                      outgoing:YES];
  1207 
  1208     mail.attachments = @[attachment];
  1209 
  1210     error = nil;
  1211     PEPStringList *keys;
  1212     PEPRating rating = PEPRatingUndefined;
  1213     PEPMessage *decmsg = [session
  1214                           decryptMessage:mail
  1215                           flags:nil
  1216                           rating:&rating
  1217                           extraKeys:&keys
  1218                           status:nil
  1219                           error:&error];
  1220     XCTAssertNotNil(decmsg);
  1221     XCTAssertNil(error);
  1222     XCTAssertEqual(rating, PEPRatingUnencrypted);
  1223 
  1224     PEPAttachment *decryptedAttachment = [decmsg.attachments objectAtIndex:0];
  1225     XCTAssertEqualObjects(decryptedAttachment.mimeType, attachment.mimeType);
  1226     XCTAssertEqualObjects(decryptedAttachment.filename, attachment.filename);
  1227 }
  1228 
  1229 #pragma mark - Sync
  1230 
  1231 /**
  1232  Prove that mySelf triggers a message to be sent.
  1233  */
  1234 - (void)testBasicSendMessage
  1235 {
  1236     PEPSession *session = [PEPSession new];
  1237     [self testSendMessageOnSession:session];
  1238 }
  1239 
  1240 - (void)testDeliverHandshakeResult
  1241 {
  1242     PEPSession *session = [PEPSession new];
  1243     [self testSendMessageOnSession:session];
  1244 
  1245     PEPSyncHandshakeResult handshakeResults[] = { PEPSyncHandshakeResultCancel,
  1246         PEPSyncHandshakeResultAccepted, PEPSyncHandshakeResultRejected };
  1247 
  1248     PEPIdentity *forSureNotMe = [[PEPIdentity alloc]
  1249                                  initWithAddress:@"someoneelseentirely@pep-project.org"
  1250                                  userID:@"that_someone_else"
  1251                                  userName:@"other"
  1252                                  isOwn:NO];
  1253 
  1254     for (int i = 0;; ++i) {
  1255         NSError *error = nil;
  1256         XCTAssertFalse([session
  1257                         deliverHandshakeResult:handshakeResults[i]
  1258                         identitiesSharing:@[forSureNotMe]
  1259                         error:&error]);
  1260         XCTAssertNotNil(error);
  1261         XCTAssertEqual([error code], PEPStatusIllegalValue);
  1262 
  1263         if (handshakeResults[i] == PEPSyncHandshakeResultRejected) {
  1264             break;
  1265         }
  1266     }
  1267 }
  1268 
  1269 #pragma mark - key_reset_identity
  1270 
  1271 - (void)testKeyResetIdentity
  1272 {
  1273     PEPSession *session = [PEPSession new];
  1274 
  1275     PEPIdentity *me = [PEPTestUtils ownPepIdentityWithAddress:@"me@peptest.ch"
  1276                                                      userName:@"userName"];
  1277 
  1278     NSError *error = nil;
  1279     XCTAssertTrue([session mySelf:me error:&error]);
  1280     XCTAssertNil(error);
  1281 
  1282     NSString *fprOriginal = me.fingerPrint;
  1283     XCTAssertNotNil(fprOriginal);
  1284 
  1285     XCTAssertTrue([session keyReset:me fingerprint:nil error:&error]);
  1286     XCTAssertNil(error);
  1287 
  1288     XCTAssertTrue([session mySelf:me error:&error]);
  1289     XCTAssertNil(error);
  1290 
  1291     NSString *fprAfterReset = me.fingerPrint;
  1292     XCTAssertNotNil(fprAfterReset);
  1293 
  1294     XCTAssertNotEqual(fprOriginal, fprAfterReset);
  1295 }
  1296 
  1297 #pragma mark - leave_device_group
  1298 
  1299 /** Leaving a device group is successful even though none exists. */
  1300 - (void)testSuccessfulLeaveDeviceGroup
  1301 {
  1302     PEPSession *session = [PEPSession new];
  1303 
  1304     PEPIdentity *identMe = [[PEPIdentity alloc]
  1305                             initWithAddress:@"me-myself-and-i@pep-project.org"
  1306                             userID:@"me-myself-and-i"
  1307                             userName:@"pEp Me"
  1308                             isOwn:YES];
  1309     NSError *error = nil;
  1310     XCTAssertTrue([session mySelf:identMe error:&error]);
  1311     XCTAssertNil(error);
  1312 
  1313     [self startSync];
  1314 
  1315     error = nil;
  1316     XCTAssertTrue([session leaveDeviceGroupError:&error]);
  1317     XCTAssertNil(error);
  1318 
  1319     [self shutdownSync];
  1320 }
  1321 
  1322 #pragma mark - enable/disable sync
  1323 
  1324 - (void)testEnableDisableFailForSyncOnPartnerIdentity
  1325 {
  1326     PEPIdentity *notMe = [[PEPIdentity alloc]
  1327                           initWithAddress:@"notme@pep-project.org"
  1328                           userID:@"notme_ID"
  1329                           userName:@"notme"
  1330                           isOwn:NO];
  1331 
  1332     NSError *error = nil;
  1333     XCTAssertFalse([notMe enableKeySync:&error]);
  1334     XCTAssertNotNil(error);
  1335 
  1336     error = nil;
  1337     XCTAssertFalse([notMe disableKeySync:&error]);
  1338     XCTAssertNotNil(error);
  1339 
  1340     error = nil;
  1341     XCTAssertNil([notMe queryKeySyncEnabled:&error]);
  1342     XCTAssertNotNil(error);
  1343 }
  1344 
  1345 - (void)testEnableDisableSyncOnOwnIdentityWithQuery
  1346 {
  1347     PEPSession *session = [PEPSession new];
  1348 
  1349     PEPIdentity *identMe1 = [[PEPIdentity alloc]
  1350                              initWithAddress:@"me-myself-and-i@pep-project.org"
  1351                              userID:@"me-myself-and-i"
  1352                              userName:@"pEp Me"
  1353                              isOwn:YES];
  1354     NSError *error = nil;
  1355     XCTAssertTrue([session mySelf:identMe1 error:&error]);
  1356     XCTAssertNil(error);
  1357 
  1358     error = nil;
  1359     PEPIdentity *identMe2 = [[PEPIdentity alloc]
  1360                              initWithAddress:@"me-myself-and-i2@pep-project.org"
  1361                              userID:@"me-myself-and-i2"
  1362                              userName:@"pEp Me2"
  1363                              isOwn:YES];
  1364     XCTAssertTrue([session mySelf:identMe2 error:&error]);
  1365     XCTAssertNil(error);
  1366 
  1367     XCTAssertNotEqualObjects(identMe1.fingerPrint, identMe2.fingerPrint);
  1368 
  1369     for (int i = 0; i < 10; ++i) {
  1370         error = nil;
  1371         BOOL enable = i % 2 == 0; // enable keysync on even numbers (roughly)
  1372         if (enable) {
  1373             XCTAssertTrue([session enableSyncForIdentity:identMe1 error:&error]);
  1374             XCTAssertTrue([identMe2 enableKeySync:&error]);
  1375         } else {
  1376             XCTAssertTrue([session disableSyncForIdentity:identMe1 error:&error]);
  1377             XCTAssertTrue([identMe2 disableKeySync:&error]);
  1378         }
  1379         XCTAssertNil(error);
  1380 
  1381         NSNumber *keySyncState1 = [session queryKeySyncEnabledForIdentity:identMe1 error:&error];
  1382         NSNumber *keySyncState2 = [identMe2 queryKeySyncEnabled:&error];
  1383         XCTAssertNil(error);
  1384         XCTAssertNotNil(keySyncState1);
  1385         XCTAssertNotNil(keySyncState2);
  1386         if (enable) {
  1387             XCTAssertTrue([keySyncState1 boolValue]);
  1388         } else {
  1389             XCTAssertFalse([keySyncState1 boolValue]);
  1390         }
  1391         XCTAssertEqualObjects(keySyncState1, keySyncState2);
  1392     }
  1393 }
  1394 
  1395 /**
  1396  ENGINE-604, just in case.
  1397  */
  1398 - (void)testQueryKeySyncOnOwnIdentityInALoop
  1399 {
  1400     PEPSession *session = [PEPSession new];
  1401 
  1402     PEPIdentity *identMe = [[PEPIdentity alloc]
  1403                             initWithAddress:@"me-myself-and-i@pep-project.org"
  1404                             userID:@"me-myself-and-i"
  1405                             userName:@"pEp Me"
  1406                             isOwn:YES];
  1407     NSError *error = nil;
  1408     XCTAssertTrue([session mySelf:identMe error:&error]);
  1409     XCTAssertNil(error);
  1410 
  1411     for (NSNumber *numBool in @[@YES, @NO]) {
  1412         error = nil;
  1413         if ([numBool boolValue]) {
  1414             XCTAssertTrue([session enableSyncForIdentity:identMe error:&error]);
  1415         } else {
  1416             XCTAssertTrue([session disableSyncForIdentity:identMe error:&error]);
  1417         }
  1418         XCTAssertNil(error);
  1419 
  1420         for (int i = 0; i < 10; ++i) {
  1421             NSNumber *numQuery = [session queryKeySyncEnabledForIdentity:identMe error:&error];
  1422             XCTAssertNotNil(numQuery);
  1423             XCTAssertEqualObjects(numBool, numQuery);
  1424             XCTAssertNil(error);
  1425         }
  1426     }
  1427 }
  1428 
  1429 #pragma mark - Helpers
  1430 
  1431 - (void)testSendMessageOnSession:(PEPSession *)session
  1432 {
  1433     XCTAssertEqual(self.sendMessageDelegate.messages.count, 0);
  1434     XCTAssertNil(self.sendMessageDelegate.lastMessage);
  1435 
  1436     PEPIdentity *identMe = [[PEPIdentity alloc]
  1437                             initWithAddress:@"me-myself-and-i@pep-project.org"
  1438                             userID:@"me-myself-and-i"
  1439                             userName:@"pEp Me"
  1440                             isOwn:YES];
  1441     NSError *error = nil;
  1442     XCTAssertTrue([session mySelf:identMe error:&error]);
  1443     XCTAssertNil(error);
  1444 
  1445     [self startSync];
  1446 
  1447     XCTKVOExpectation *expHaveMessage = [[XCTKVOExpectation alloc]
  1448                                          initWithKeyPath:@"lastMessage"
  1449                                          object:self.sendMessageDelegate];
  1450 
  1451     XCTAssertNotNil(identMe.fingerPrint);
  1452 
  1453     [self waitForExpectations:@[expHaveMessage] timeout:PEPTestInternalSyncTimeout];
  1454     XCTAssertNotNil(self.sendMessageDelegate.lastMessage);
  1455 
  1456     XCTAssertEqual(self.sendMessageDelegate.messages.count, 1);
  1457     [self shutdownSync];
  1458 }
  1459 
  1460 - (void)startSync
  1461 {
  1462     self.sendMessageDelegate = [PEPSessionTestSendMessageDelegate new];
  1463     self.notifyHandshakeDelegate = [PEPSessionTestNotifyHandshakeDelegate new];
  1464 
  1465     self.sync = [[PEPSync alloc]
  1466                  initWithSendMessageDelegate:self.sendMessageDelegate
  1467                  notifyHandshakeDelegate:self.notifyHandshakeDelegate];
  1468     [self.sync startup];
  1469 }
  1470 
  1471 - (void)shutdownSync
  1472 {
  1473     [self.sync shutdown];
  1474 }
  1475 
  1476 - (NSNumber * _Nullable)testOutgoingRatingForMessage:(PEPMessage * _Nonnull)theMessage
  1477                                              session:(PEPSession *)session
  1478                                                error:(NSError * _Nullable * _Nullable)error
  1479 {
  1480     NSNumber *ratingOriginal = [session outgoingRatingForMessage:theMessage error:error];
  1481     NSNumber *ratingPreview = [session outgoingRatingPreviewForMessage:theMessage error:nil];
  1482     XCTAssertEqual(ratingOriginal, ratingPreview);
  1483     return ratingOriginal;
  1484 }
  1485 
  1486 - (void)testPassiveModeEnabled:(BOOL)passiveModeEnabled
  1487 {
  1488     [PEPObjCAdapter setPassiveModeEnabled:passiveModeEnabled];
  1489     PEPSession *session = [PEPSession new];
  1490 
  1491     PEPIdentity *identMe = [[PEPIdentity alloc]
  1492                             initWithAddress:@"me-myself-and-i@pep-project.org"
  1493                             userID:@"me-myself-and-i"
  1494                             userName:@"pEp Me"
  1495                             isOwn:YES];
  1496     NSError *error = nil;
  1497     XCTAssertTrue([session mySelf:identMe error:&error]);
  1498     XCTAssertNil(error);
  1499 
  1500     XCTAssertNotNil(identMe.fingerPrint);
  1501 
  1502     PEPIdentity *identAlice = [[PEPIdentity alloc]
  1503                                initWithAddress:@"pep.test.alice@pep-project.org"
  1504                                userID:@"alice_user_id"
  1505                                userName:@"Alice"
  1506                                isOwn:NO];
  1507 
  1508     NSString *shortMessage = @"whatever it may be";
  1509     NSString *longMessage = [NSString stringWithFormat:@"%@ %@", shortMessage, shortMessage];
  1510     PEPMessage *message = [PEPMessage new];
  1511     message.direction = PEPMsgDirectionOutgoing;
  1512     message.from = identMe;
  1513     message.to = @[identAlice];
  1514     message.shortMessage = shortMessage;
  1515     message.longMessage = longMessage;
  1516 
  1517     PEPStatus status = PEPStatusKeyNotFound;
  1518     PEPMessage *encryptedMessage =  [session encryptMessage:message
  1519                                                   extraKeys:@[]
  1520                                                      status:&status
  1521                                                       error:&error];
  1522     XCTAssertNil(error);
  1523     XCTAssertEqual(status, PEPStatusUnencrypted);
  1524 
  1525     if (passiveModeEnabled) {
  1526         XCTAssertNil(encryptedMessage.attachments);
  1527     } else {
  1528         XCTAssertEqual(encryptedMessage.attachments.count, 1);
  1529     }
  1530 }
  1531 
  1532 /**
  1533  Determines the rating for the given identity.
  1534  @return PEPRatingUndefined on error
  1535  */
  1536 - (PEPRating)ratingForIdentity:(PEPIdentity *)identity session:(PEPSession *)session
  1537 {
  1538     NSError *error;
  1539     NSNumber *numRating = [session ratingForIdentity:identity error:&error];
  1540     XCTAssertNil(error);
  1541     return numRating.pEpRating;
  1542 }
  1543 
  1544 - (PEPIdentity *)checkImportingKeyFilePath:(NSString *)filePath address:(NSString *)address
  1545                                     userID:(NSString *)userID
  1546                                fingerPrint:(NSString *)fingerPrint
  1547                                    session:(PEPSession *)session
  1548 {
  1549     if (!session) {
  1550         session = [PEPSession new];
  1551     }
  1552 
  1553     BOOL success = [PEPTestUtils importBundledKey:filePath session:session];
  1554     XCTAssertTrue(success);
  1555 
  1556     if (success) {
  1557         // Our test user:
  1558         PEPIdentity *identTest = [[PEPIdentity alloc]
  1559                                   initWithAddress:address
  1560                                   userID:userID
  1561                                   userName:[NSString stringWithFormat:@"Some User Name %@", userID]
  1562                                   isOwn:NO];
  1563 
  1564         NSError *error = nil;
  1565         XCTAssertTrue([session updateIdentity:identTest error:&error]);
  1566         XCTAssertNil(error);
  1567         XCTAssertNotNil(identTest.fingerPrint);
  1568         XCTAssertEqualObjects(identTest.fingerPrint, fingerPrint);
  1569 
  1570         return identTest;
  1571     } else {
  1572         return nil;
  1573     }
  1574 }
  1575 
  1576 - (PEPIdentity *)checkMySelfImportingKeyFilePath:(NSString *)filePath
  1577                                          address:(NSString *)address
  1578                                           userID:(NSString *)userID
  1579                                      fingerPrint:(NSString *)fingerPrint
  1580                                          session:(PEPSession *)session
  1581 {
  1582     XCTAssertTrue([PEPTestUtils importBundledKey:filePath session:session]);
  1583 
  1584     // Our test user:
  1585     PEPIdentity *identTest = [[PEPIdentity alloc]
  1586                               initWithAddress:address
  1587                               userID:userID
  1588                               userName:[NSString stringWithFormat:@"Some User Name %@", userID]
  1589                               isOwn:YES
  1590                               fingerPrint: fingerPrint];
  1591 
  1592     NSError *error;
  1593     XCTAssertTrue([session setOwnKey:identTest fingerprint:fingerPrint error:&error]);
  1594     XCTAssertNil(error);
  1595     XCTAssertNotNil(identTest.fingerPrint);
  1596     XCTAssertEqualObjects(identTest.fingerPrint, fingerPrint);
  1597 
  1598     return identTest;
  1599 }
  1600 
  1601 /**
  1602  Verifies that a partner ID is really a correct Identity.
  1603  Usually used on identities imported as keys, since the engine has problems with them.
  1604  */
  1605 - (void)updateAndVerifyPartnerIdentity:(PEPIdentity *)partnerIdentity session:(PEPSession *)session
  1606 {
  1607     NSError *error = nil;
  1608 
  1609     XCTAssertNotNil(partnerIdentity.fingerPrint);
  1610     XCTAssertTrue([session updateIdentity:partnerIdentity error:&error]);
  1611     XCTAssertNil(error);
  1612     XCTAssertNotNil(partnerIdentity.fingerPrint);
  1613     NSString *fingerprint = partnerIdentity.fingerPrint;
  1614     partnerIdentity.fingerPrint = nil;
  1615     XCTAssertTrue([session updateIdentity:partnerIdentity error:&error]);
  1616     XCTAssertNil(error);
  1617     XCTAssertNotNil(partnerIdentity.fingerPrint);
  1618     XCTAssertEqualObjects(partnerIdentity.fingerPrint, fingerprint);
  1619 }
  1620 
  1621 - (PEPMessage *)mailWrittenToMySelf
  1622 {
  1623     PEPSession *session = [PEPSession new];
  1624 
  1625     // Write a e-mail to yourself ...
  1626     PEPIdentity *me = [PEPTestUtils ownPepIdentityWithAddress:@"me@peptest.ch"
  1627                                                      userName:@"userName"];
  1628     NSError *error = nil;
  1629     XCTAssertTrue([session mySelf:me error:&error]);
  1630     XCTAssertNil(error);
  1631 
  1632     NSString *shortMessage = @"Subject";
  1633     NSString *longMessage = @"Oh, this is a long body text!";
  1634     PEPMessage *mail = [PEPTestUtils mailFrom:me
  1635                                       toIdent:me
  1636                                  shortMessage:shortMessage
  1637                                   longMessage:longMessage
  1638                                      outgoing:YES];
  1639     PEPStatus status = PEPStatusUnknownError;
  1640     PEPMessage *encMessage = [session
  1641                               encryptMessage:mail
  1642                               forSelf:me
  1643                               extraKeys:nil
  1644                               status:&status
  1645                               error:&error];
  1646     XCTAssertNil(error);
  1647 
  1648     return encMessage;
  1649 }
  1650 
  1651 - (PEPMessage *)internalEncryptToMySelfKeys:(PEPStringList **)keys
  1652 {
  1653     PEPSession *session = [PEPSession new];
  1654     
  1655     PEPIdentity *me = [PEPTestUtils ownPepIdentityWithAddress:@"me@peptest.ch"
  1656                                                      userName:@"userName"];
  1657     NSError *error = nil;
  1658     XCTAssertTrue([session mySelf:me error:&error]);
  1659     XCTAssertNil(error);
  1660 
  1661     XCTAssertNotNil(me.fingerPrint);
  1662 
  1663     // Create draft
  1664     NSString *shortMessage = @"Subject";
  1665     NSString *longMessage = @"Oh, this is a long body text!";
  1666     PEPMessage *mail = [PEPTestUtils mailFrom:me toIdent:me shortMessage:shortMessage longMessage:longMessage outgoing:YES];
  1667 
  1668     PEPStatus status;
  1669     PEPMessage *encMessage = [session
  1670                               encryptMessage:mail
  1671                               forSelf:me
  1672                               extraKeys:nil
  1673                               status:&status
  1674                               error:&error];
  1675     XCTAssertEqual(status, 0);
  1676     XCTAssertEqualObjects(encMessage.shortMessage, @"p≡p");
  1677 
  1678     PEPRating rating;
  1679     error = nil;
  1680     PEPMessage *unencDict = [session
  1681                              decryptMessage:encMessage
  1682                              flags:nil
  1683                              rating:&rating
  1684                              extraKeys:keys
  1685                              status:nil
  1686                              error:&error];
  1687     XCTAssertNotNil(unencDict);
  1688     XCTAssertNil(error);
  1689 
  1690     XCTAssertGreaterThanOrEqual(rating, PEPRatingReliable);
  1691 
  1692     XCTAssertEqualObjects(unencDict.shortMessage, shortMessage);
  1693     XCTAssertEqualObjects(unencDict.longMessage, longMessage);
  1694 
  1695     return unencDict;
  1696 }
  1697 
  1698 - (void)pEpCleanUp
  1699 {
  1700     [PEPTestUtils cleanUp];
  1701 }
  1702 
  1703 - (void)helperXEncStatusForOutgoingEncryptdMailToSelf:(BOOL)toSelf
  1704                                        expectedRating:(PEPRating)expectedRating
  1705 {
  1706     PEPSession *session = [PEPSession new];
  1707 
  1708     // Partner pubkey for the test:
  1709     // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
  1710     // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
  1711     XCTAssertTrue([PEPTestUtils importBundledKey:@"6FF00E97.asc" session:session]);
  1712 
  1713     PEPIdentity *identAlice = [[PEPIdentity alloc]
  1714                                initWithAddress:@"pep.test.alice@pep-project.org"
  1715                                userID:ownUserId
  1716                                userName:@"pEp Test Alice"
  1717                                isOwn:NO
  1718                                fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
  1719     [self updateAndVerifyPartnerIdentity:identAlice session:session];
  1720 
  1721     PEPIdentity *identMe = [[PEPIdentity alloc]
  1722                                initWithAddress:@"me-myself-and-i@pep-project.org"
  1723                                userID:@"me-myself-and-i"
  1724                                userName:@"pEp Me"
  1725                                isOwn:YES];
  1726     NSError *error = nil;
  1727     XCTAssertTrue([session mySelf:identMe error:&error]);
  1728     XCTAssertNil(error);
  1729 
  1730     XCTAssertNotNil(identMe.fingerPrint);
  1731 
  1732     PEPMessage *msg = [PEPMessage new];
  1733     msg.from = identMe;
  1734     msg.to = @[identAlice];
  1735     msg.shortMessage = @"Mail to Alice";
  1736     msg.longMessage = @"Alice?";
  1737     msg.direction = PEPMsgDirectionOutgoing;
  1738 
  1739     NSNumber *numRating = [self testOutgoingRatingForMessage:msg session:session error:&error];
  1740     XCTAssertNotNil(numRating);
  1741     XCTAssertNil(error);
  1742     XCTAssertEqual(numRating.pEpRating, PEPRatingReliable);
  1743 
  1744     PEPMessage *encMsg;
  1745 
  1746     PEPStatus statusEnc = PEPStatusVersionMismatch;
  1747     if (toSelf) {
  1748         encMsg = [session
  1749                   encryptMessage:msg
  1750                   forSelf:identMe
  1751                   extraKeys:nil
  1752                   status:&statusEnc
  1753                   error:&error];
  1754         XCTAssertEqual(statusEnc, PEPStatusOK);
  1755     } else {
  1756         encMsg = [session encryptMessage:msg extraKeys:nil status:nil error:&error];
  1757         XCTAssertNotNil(encMsg);
  1758         XCTAssertNil(error);
  1759     }
  1760     XCTAssertNotNil(encMsg);
  1761 
  1762     PEPStringList *keys;
  1763     PEPRating pEpRating;
  1764     error = nil;
  1765     PEPMessage *decMsg = [session
  1766                           decryptMessage:encMsg
  1767                           flags:nil
  1768                           rating:&pEpRating
  1769                           extraKeys:&keys
  1770                           status:nil
  1771                           error:&error];
  1772     XCTAssertNil(error);
  1773     XCTAssertNotNil(decMsg);
  1774 
  1775     XCTAssertEqual(pEpRating, expectedRating);
  1776 
  1777     NSArray * encStatusField = nil;
  1778     for (NSArray *field in decMsg.optionalFields) {
  1779         NSString *header = [field[0] lowercaseString];
  1780         if ([header isEqualToString:@"x-encstatus"]) {
  1781             encStatusField = field;
  1782         }
  1783     }
  1784     XCTAssertNotNil(encStatusField);
  1785     if (encStatusField) {
  1786         PEPRating outgoingRating = [session ratingFromString:encStatusField[1]];
  1787         XCTAssertEqual(outgoingRating, expectedRating);
  1788     }
  1789 }
  1790 
  1791 @end