pEpObjCTests/PEPSessionTest.m
author Dirk Zimmermann <dz@pep.security>
Tue, 03 Dec 2019 14:07:27 +0100
branchIOSAD-160
changeset 1375 693a6506fb50
parent 1358 8266b8194627
child 1376 8e4f3ff6694b
permissions -rw-r--r--
IOSAD-160 Test new myself version
     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 /// Test the disable of sync via myself.
  1270 - (void)testNoBeaconOnMyself
  1271 {
  1272     PEPSession *session = [PEPSession new];
  1273 
  1274     XCTAssertEqual(self.sendMessageDelegate.messages.count, 0);
  1275     XCTAssertNil(self.sendMessageDelegate.lastMessage);
  1276 
  1277     PEPIdentity *identMe = [[PEPIdentity alloc]
  1278                             initWithAddress:@"me-myself-and-i@pep-project.org"
  1279                             userID:@"me-myself-and-i"
  1280                             userName:@"pEp Me"
  1281                             isOwn:YES];
  1282 
  1283     NSError *error = nil;
  1284     XCTAssertTrue([session mySelf:identMe pEpSyncEnabled:NO error:&error]);
  1285     XCTAssertNil(error);
  1286 
  1287     [self startSync];
  1288 
  1289     [NSThread sleepForTimeInterval:2];
  1290     XCTAssertNil(self.sendMessageDelegate.lastMessage);
  1291 
  1292     XCTAssertEqual(self.sendMessageDelegate.messages.count, 0);
  1293     [self shutdownSync];
  1294 }
  1295 
  1296 #pragma mark - key_reset_identity
  1297 
  1298 - (void)testKeyResetIdentity
  1299 {
  1300     PEPSession *session = [PEPSession new];
  1301 
  1302     PEPIdentity *me = [PEPTestUtils ownPepIdentityWithAddress:@"me@peptest.ch"
  1303                                                      userName:@"userName"];
  1304 
  1305     NSError *error = nil;
  1306     XCTAssertTrue([session mySelf:me error:&error]);
  1307     XCTAssertNil(error);
  1308 
  1309     NSString *fprOriginal = me.fingerPrint;
  1310     XCTAssertNotNil(fprOriginal);
  1311 
  1312     XCTAssertTrue([session keyReset:me fingerprint:nil error:&error]);
  1313     XCTAssertNil(error);
  1314 
  1315     XCTAssertTrue([session mySelf:me error:&error]);
  1316     XCTAssertNil(error);
  1317 
  1318     NSString *fprAfterReset = me.fingerPrint;
  1319     XCTAssertNotNil(fprAfterReset);
  1320 
  1321     XCTAssertNotEqual(fprOriginal, fprAfterReset);
  1322 }
  1323 
  1324 #pragma mark - leave_device_group
  1325 
  1326 /** Leaving a device group is successful even though none exists. */
  1327 - (void)testSuccessfulLeaveDeviceGroup
  1328 {
  1329     PEPSession *session = [PEPSession new];
  1330 
  1331     PEPIdentity *identMe = [[PEPIdentity alloc]
  1332                             initWithAddress:@"me-myself-and-i@pep-project.org"
  1333                             userID:@"me-myself-and-i"
  1334                             userName:@"pEp Me"
  1335                             isOwn:YES];
  1336     NSError *error = nil;
  1337     XCTAssertTrue([session mySelf:identMe error:&error]);
  1338     XCTAssertNil(error);
  1339 
  1340     [self startSync];
  1341 
  1342     error = nil;
  1343     XCTAssertTrue([session leaveDeviceGroup:&error]);
  1344     XCTAssertNil(error);
  1345 
  1346     // leaving a device group should disable sync
  1347     XCTAssertTrue(self.notifyHandshakeDelegate.engineDidShutdownKeySync);
  1348 
  1349     [self shutdownSync];
  1350 }
  1351 
  1352 #pragma mark - enable/disable sync
  1353 
  1354 - (void)testEnableDisableFailForSyncOnPartnerIdentity
  1355 {
  1356     PEPIdentity *notMe = [[PEPIdentity alloc]
  1357                           initWithAddress:@"notme@pep-project.org"
  1358                           userID:@"notme_ID"
  1359                           userName:@"notme"
  1360                           isOwn:NO];
  1361 
  1362     NSError *error = nil;
  1363     XCTAssertFalse([notMe enableKeySync:&error]);
  1364     XCTAssertNotNil(error);
  1365 
  1366     error = nil;
  1367     XCTAssertFalse([notMe disableKeySync:&error]);
  1368     XCTAssertNotNil(error);
  1369 
  1370     error = nil;
  1371     XCTAssertNil([notMe queryKeySyncEnabled:&error]);
  1372     XCTAssertNotNil(error);
  1373 }
  1374 
  1375 - (void)testEnableDisableSyncOnOwnIdentityWithQuery
  1376 {
  1377     PEPSession *session = [PEPSession new];
  1378 
  1379     PEPIdentity *identMe1 = [[PEPIdentity alloc]
  1380                              initWithAddress:@"me-myself-and-i@pep-project.org"
  1381                              userID:@"me-myself-and-i"
  1382                              userName:@"pEp Me"
  1383                              isOwn:YES];
  1384     NSError *error = nil;
  1385     XCTAssertTrue([session mySelf:identMe1 error:&error]);
  1386     XCTAssertNil(error);
  1387 
  1388     error = nil;
  1389     PEPIdentity *identMe2 = [[PEPIdentity alloc]
  1390                              initWithAddress:@"me-myself-and-i2@pep-project.org"
  1391                              userID:@"me-myself-and-i2"
  1392                              userName:@"pEp Me2"
  1393                              isOwn:YES];
  1394     XCTAssertTrue([session mySelf:identMe2 error:&error]);
  1395     XCTAssertNil(error);
  1396 
  1397     XCTAssertNotEqualObjects(identMe1.fingerPrint, identMe2.fingerPrint);
  1398 
  1399     for (int i = 0; i < 10; ++i) {
  1400         error = nil;
  1401         BOOL enable = i % 2 == 0; // enable keysync on even numbers (roughly)
  1402         if (enable) {
  1403             XCTAssertTrue([session enableSyncForIdentity:identMe1 error:&error]);
  1404             XCTAssertTrue([identMe2 enableKeySync:&error]);
  1405         } else {
  1406             XCTAssertTrue([session disableSyncForIdentity:identMe1 error:&error]);
  1407             XCTAssertTrue([identMe2 disableKeySync:&error]);
  1408         }
  1409         XCTAssertNil(error);
  1410 
  1411         NSNumber *keySyncState1 = [session queryKeySyncEnabledForIdentity:identMe1 error:&error];
  1412         NSNumber *keySyncState2 = [identMe2 queryKeySyncEnabled:&error];
  1413         XCTAssertNil(error);
  1414         XCTAssertNotNil(keySyncState1);
  1415         XCTAssertNotNil(keySyncState2);
  1416         if (enable) {
  1417             XCTAssertTrue([keySyncState1 boolValue]);
  1418         } else {
  1419             XCTAssertFalse([keySyncState1 boolValue]);
  1420         }
  1421         XCTAssertEqualObjects(keySyncState1, keySyncState2);
  1422     }
  1423 }
  1424 
  1425 /**
  1426  ENGINE-604, just in case.
  1427  */
  1428 - (void)testQueryKeySyncOnOwnIdentityInALoop
  1429 {
  1430     PEPSession *session = [PEPSession new];
  1431 
  1432     PEPIdentity *identMe = [[PEPIdentity alloc]
  1433                             initWithAddress:@"me-myself-and-i@pep-project.org"
  1434                             userID:@"me-myself-and-i"
  1435                             userName:@"pEp Me"
  1436                             isOwn:YES];
  1437     NSError *error = nil;
  1438     XCTAssertTrue([session mySelf:identMe error:&error]);
  1439     XCTAssertNil(error);
  1440 
  1441     for (NSNumber *numBool in @[@YES, @NO]) {
  1442         error = nil;
  1443         if ([numBool boolValue]) {
  1444             XCTAssertTrue([session enableSyncForIdentity:identMe error:&error]);
  1445         } else {
  1446             XCTAssertTrue([session disableSyncForIdentity:identMe error:&error]);
  1447         }
  1448         XCTAssertNil(error);
  1449 
  1450         for (int i = 0; i < 10; ++i) {
  1451             NSNumber *numQuery = [session queryKeySyncEnabledForIdentity:identMe error:&error];
  1452             XCTAssertNotNil(numQuery);
  1453             XCTAssertEqualObjects(numBool, numQuery);
  1454             XCTAssertNil(error);
  1455         }
  1456     }
  1457 }
  1458 
  1459 /**
  1460  Tests [PEPSessionProtocol keyResetAllOwnKeysError:error].
  1461 
  1462  Does the following:
  1463  * Do a mySelf.
  1464  * Catch the sent out sync (beacon?) message.
  1465  * Do a key reset on all own identities ([PEPSessionProtocol keyResetAllOwnKeysError:error]).
  1466  * Catch the sent out sync message.
  1467  * Decrypt the caught sync messages.
  1468  */
  1469 - (void)testDecryptBeaconsAfterKeyReset
  1470 {
  1471     PEPSession *session = [PEPSession new];
  1472 
  1473     XCTAssertEqual(self.sendMessageDelegate.messages.count, 0);
  1474     XCTAssertNil(self.sendMessageDelegate.lastMessage);
  1475 
  1476     PEPIdentity *identMe = [[PEPIdentity alloc]
  1477                             initWithAddress:@"me-myself-and-i@pep-project.org"
  1478                             userID:@"me-myself-and-i"
  1479                             userName:@"pEp Me"
  1480                             isOwn:YES];
  1481     NSError *error = nil;
  1482     XCTAssertTrue([session mySelf:identMe error:&error]);
  1483     XCTAssertNil(error);
  1484     XCTAssertNotNil(identMe.fingerPrint);
  1485 
  1486     [self startSync];
  1487 
  1488     XCTKVOExpectation *expHaveMessage1 = [[XCTKVOExpectation alloc]
  1489                                           initWithKeyPath:@"lastMessage"
  1490                                           object:self.sendMessageDelegate];
  1491     [self waitForExpectations:@[expHaveMessage1] timeout:PEPTestInternalSyncTimeout];
  1492     XCTAssertNotNil(self.sendMessageDelegate.lastMessage);
  1493     XCTAssertEqual(self.sendMessageDelegate.messages.count, 1);
  1494 
  1495     PEPMessage *oldBeacon = self.sendMessageDelegate.lastMessage;
  1496 
  1497     XCTAssertTrue([session keyResetAllOwnKeysError:&error]);
  1498     XCTAssertNil(error);
  1499 
  1500     XCTKVOExpectation *expHaveMessage2 = [[XCTKVOExpectation alloc]
  1501                                           initWithKeyPath:@"lastMessage"
  1502                                           object:self.sendMessageDelegate];
  1503     [self waitForExpectations:@[expHaveMessage2] timeout:PEPTestInternalSyncTimeout];
  1504     XCTAssertNotNil(self.sendMessageDelegate.lastMessage);
  1505     XCTAssertEqual(self.sendMessageDelegate.messages.count, 2);
  1506 
  1507     PEPMessage *newBeacon = self.sendMessageDelegate.lastMessage;
  1508 
  1509     XCTAssertNotEqual(oldBeacon, newBeacon);
  1510 
  1511     PEPRating rating;
  1512     PEPStringList *extraKeys;
  1513     PEPStatus status;
  1514     PEPMessage *decryptedOldBeacon = [session decryptMessage:oldBeacon
  1515                                                        flags:nil
  1516                                                       rating:&rating
  1517                                                    extraKeys:&extraKeys
  1518                                                       status:&status
  1519                                                        error:&error];
  1520     XCTAssertNotNil(decryptedOldBeacon);
  1521     XCTAssertNil(error);
  1522 
  1523     PEPMessage *decryptedNewBeacon = [session decryptMessage:newBeacon
  1524                                                        flags:nil
  1525                                                       rating:&rating
  1526                                                    extraKeys:&extraKeys
  1527                                                       status:&status
  1528                                                        error:&error];
  1529     XCTAssertNotNil(decryptedNewBeacon);
  1530     XCTAssertNil(error);
  1531 
  1532     [self shutdownSync];
  1533 }
  1534 
  1535 #pragma mark - Helpers
  1536 
  1537 - (void)testSendMessageOnSession:(PEPSession *)session
  1538 {
  1539     XCTAssertEqual(self.sendMessageDelegate.messages.count, 0);
  1540     XCTAssertNil(self.sendMessageDelegate.lastMessage);
  1541 
  1542     PEPIdentity *identMe = [[PEPIdentity alloc]
  1543                             initWithAddress:@"me-myself-and-i@pep-project.org"
  1544                             userID:@"me-myself-and-i"
  1545                             userName:@"pEp Me"
  1546                             isOwn:YES];
  1547 
  1548     NSError *error = nil;
  1549     XCTAssertTrue([session mySelf:identMe error:&error]);
  1550     XCTAssertNil(error);
  1551 
  1552     [self startSync];
  1553 
  1554     XCTKVOExpectation *expHaveMessage = [[XCTKVOExpectation alloc]
  1555                                          initWithKeyPath:@"lastMessage"
  1556                                          object:self.sendMessageDelegate];
  1557 
  1558     XCTAssertNotNil(identMe.fingerPrint);
  1559 
  1560     [self waitForExpectations:@[expHaveMessage] timeout:PEPTestInternalSyncTimeout];
  1561     XCTAssertNotNil(self.sendMessageDelegate.lastMessage);
  1562 
  1563     XCTAssertEqual(self.sendMessageDelegate.messages.count, 1);
  1564     [self shutdownSync];
  1565 }
  1566 
  1567 - (void)startSync
  1568 {
  1569     self.sendMessageDelegate = [PEPSessionTestSendMessageDelegate new];
  1570     self.notifyHandshakeDelegate = [PEPSessionTestNotifyHandshakeDelegate new];
  1571 
  1572     self.sync = [[PEPSync alloc]
  1573                  initWithSendMessageDelegate:self.sendMessageDelegate
  1574                  notifyHandshakeDelegate:self.notifyHandshakeDelegate];
  1575     [self.sync startup];
  1576 }
  1577 
  1578 - (void)shutdownSync
  1579 {
  1580     [self.sync shutdown];
  1581 }
  1582 
  1583 - (NSNumber * _Nullable)testOutgoingRatingForMessage:(PEPMessage * _Nonnull)theMessage
  1584                                              session:(PEPSession *)session
  1585                                                error:(NSError * _Nullable * _Nullable)error
  1586 {
  1587     NSNumber *ratingOriginal = [session outgoingRatingForMessage:theMessage error:error];
  1588     NSNumber *ratingPreview = [session outgoingRatingPreviewForMessage:theMessage error:nil];
  1589     XCTAssertEqual(ratingOriginal, ratingPreview);
  1590     return ratingOriginal;
  1591 }
  1592 
  1593 - (void)testPassiveModeEnabled:(BOOL)passiveModeEnabled
  1594 {
  1595     [PEPObjCAdapter setPassiveModeEnabled:passiveModeEnabled];
  1596     PEPSession *session = [PEPSession new];
  1597 
  1598     PEPIdentity *identMe = [[PEPIdentity alloc]
  1599                             initWithAddress:@"me-myself-and-i@pep-project.org"
  1600                             userID:@"me-myself-and-i"
  1601                             userName:@"pEp Me"
  1602                             isOwn:YES];
  1603     NSError *error = nil;
  1604     XCTAssertTrue([session mySelf:identMe error:&error]);
  1605     XCTAssertNil(error);
  1606 
  1607     XCTAssertNotNil(identMe.fingerPrint);
  1608 
  1609     PEPIdentity *identAlice = [[PEPIdentity alloc]
  1610                                initWithAddress:@"pep.test.alice@pep-project.org"
  1611                                userID:@"alice_user_id"
  1612                                userName:@"Alice"
  1613                                isOwn:NO];
  1614 
  1615     NSString *shortMessage = @"whatever it may be";
  1616     NSString *longMessage = [NSString stringWithFormat:@"%@ %@", shortMessage, shortMessage];
  1617     PEPMessage *message = [PEPMessage new];
  1618     message.direction = PEPMsgDirectionOutgoing;
  1619     message.from = identMe;
  1620     message.to = @[identAlice];
  1621     message.shortMessage = shortMessage;
  1622     message.longMessage = longMessage;
  1623 
  1624     PEPStatus status = PEPStatusKeyNotFound;
  1625     PEPMessage *encryptedMessage =  [session encryptMessage:message
  1626                                                   extraKeys:@[]
  1627                                                      status:&status
  1628                                                       error:&error];
  1629     XCTAssertNil(error);
  1630     XCTAssertEqual(status, PEPStatusUnencrypted);
  1631 
  1632     if (passiveModeEnabled) {
  1633         XCTAssertNil(encryptedMessage.attachments);
  1634     } else {
  1635         XCTAssertEqual(encryptedMessage.attachments.count, 1);
  1636     }
  1637 }
  1638 
  1639 /**
  1640  Determines the rating for the given identity.
  1641  @return PEPRatingUndefined on error
  1642  */
  1643 - (PEPRating)ratingForIdentity:(PEPIdentity *)identity session:(PEPSession *)session
  1644 {
  1645     NSError *error;
  1646     NSNumber *numRating = [session ratingForIdentity:identity error:&error];
  1647     XCTAssertNil(error);
  1648     return numRating.pEpRating;
  1649 }
  1650 
  1651 - (PEPIdentity *)checkImportingKeyFilePath:(NSString *)filePath address:(NSString *)address
  1652                                     userID:(NSString *)userID
  1653                                fingerPrint:(NSString *)fingerPrint
  1654                                    session:(PEPSession *)session
  1655 {
  1656     if (!session) {
  1657         session = [PEPSession new];
  1658     }
  1659 
  1660     BOOL success = [PEPTestUtils importBundledKey:filePath session:session];
  1661     XCTAssertTrue(success);
  1662 
  1663     if (success) {
  1664         // Our test user:
  1665         PEPIdentity *identTest = [[PEPIdentity alloc]
  1666                                   initWithAddress:address
  1667                                   userID:userID
  1668                                   userName:[NSString stringWithFormat:@"Some User Name %@", userID]
  1669                                   isOwn:NO];
  1670 
  1671         NSError *error = nil;
  1672         XCTAssertTrue([session updateIdentity:identTest error:&error]);
  1673         XCTAssertNil(error);
  1674         XCTAssertNotNil(identTest.fingerPrint);
  1675         XCTAssertEqualObjects(identTest.fingerPrint, fingerPrint);
  1676 
  1677         return identTest;
  1678     } else {
  1679         return nil;
  1680     }
  1681 }
  1682 
  1683 - (PEPIdentity *)checkMySelfImportingKeyFilePath:(NSString *)filePath
  1684                                          address:(NSString *)address
  1685                                           userID:(NSString *)userID
  1686                                      fingerPrint:(NSString *)fingerPrint
  1687                                          session:(PEPSession *)session
  1688 {
  1689     XCTAssertTrue([PEPTestUtils importBundledKey:filePath session:session]);
  1690 
  1691     // Our test user:
  1692     PEPIdentity *identTest = [[PEPIdentity alloc]
  1693                               initWithAddress:address
  1694                               userID:userID
  1695                               userName:[NSString stringWithFormat:@"Some User Name %@", userID]
  1696                               isOwn:YES
  1697                               fingerPrint: fingerPrint];
  1698 
  1699     NSError *error;
  1700     XCTAssertTrue([session setOwnKey:identTest fingerprint:fingerPrint error:&error]);
  1701     XCTAssertNil(error);
  1702     XCTAssertNotNil(identTest.fingerPrint);
  1703     XCTAssertEqualObjects(identTest.fingerPrint, fingerPrint);
  1704 
  1705     return identTest;
  1706 }
  1707 
  1708 /**
  1709  Verifies that a partner ID is really a correct Identity.
  1710  Usually used on identities imported as keys, since the engine has problems with them.
  1711  */
  1712 - (void)updateAndVerifyPartnerIdentity:(PEPIdentity *)partnerIdentity session:(PEPSession *)session
  1713 {
  1714     NSError *error = nil;
  1715 
  1716     XCTAssertNotNil(partnerIdentity.fingerPrint);
  1717     XCTAssertTrue([session updateIdentity:partnerIdentity error:&error]);
  1718     XCTAssertNil(error);
  1719     XCTAssertNotNil(partnerIdentity.fingerPrint);
  1720     NSString *fingerprint = partnerIdentity.fingerPrint;
  1721     partnerIdentity.fingerPrint = nil;
  1722     XCTAssertTrue([session updateIdentity:partnerIdentity error:&error]);
  1723     XCTAssertNil(error);
  1724     XCTAssertNotNil(partnerIdentity.fingerPrint);
  1725     XCTAssertEqualObjects(partnerIdentity.fingerPrint, fingerprint);
  1726 }
  1727 
  1728 - (PEPMessage *)mailWrittenToMySelf
  1729 {
  1730     PEPSession *session = [PEPSession new];
  1731 
  1732     // Write a e-mail to yourself ...
  1733     PEPIdentity *me = [PEPTestUtils ownPepIdentityWithAddress:@"me@peptest.ch"
  1734                                                      userName:@"userName"];
  1735     NSError *error = nil;
  1736     XCTAssertTrue([session mySelf:me error:&error]);
  1737     XCTAssertNil(error);
  1738 
  1739     NSString *shortMessage = @"Subject";
  1740     NSString *longMessage = @"Oh, this is a long body text!";
  1741     PEPMessage *mail = [PEPTestUtils mailFrom:me
  1742                                       toIdent:me
  1743                                  shortMessage:shortMessage
  1744                                   longMessage:longMessage
  1745                                      outgoing:YES];
  1746     PEPStatus status = PEPStatusUnknownError;
  1747     PEPMessage *encMessage = [session
  1748                               encryptMessage:mail
  1749                               forSelf:me
  1750                               extraKeys:nil
  1751                               status:&status
  1752                               error:&error];
  1753     XCTAssertNil(error);
  1754 
  1755     return encMessage;
  1756 }
  1757 
  1758 - (PEPMessage *)internalEncryptToMySelfKeys:(PEPStringList **)keys
  1759 {
  1760     PEPSession *session = [PEPSession new];
  1761     
  1762     PEPIdentity *me = [PEPTestUtils ownPepIdentityWithAddress:@"me@peptest.ch"
  1763                                                      userName:@"userName"];
  1764     NSError *error = nil;
  1765     XCTAssertTrue([session mySelf:me error:&error]);
  1766     XCTAssertNil(error);
  1767 
  1768     XCTAssertNotNil(me.fingerPrint);
  1769 
  1770     // Create draft
  1771     NSString *shortMessage = @"Subject";
  1772     NSString *longMessage = @"Oh, this is a long body text!";
  1773     PEPMessage *mail = [PEPTestUtils mailFrom:me toIdent:me shortMessage:shortMessage longMessage:longMessage outgoing:YES];
  1774 
  1775     PEPStatus status;
  1776     PEPMessage *encMessage = [session
  1777                               encryptMessage:mail
  1778                               forSelf:me
  1779                               extraKeys:nil
  1780                               status:&status
  1781                               error:&error];
  1782     XCTAssertEqual(status, 0);
  1783     XCTAssertEqualObjects(encMessage.shortMessage, @"p≡p");
  1784 
  1785     PEPRating rating;
  1786     error = nil;
  1787     PEPMessage *unencDict = [session
  1788                              decryptMessage:encMessage
  1789                              flags:nil
  1790                              rating:&rating
  1791                              extraKeys:keys
  1792                              status:nil
  1793                              error:&error];
  1794     XCTAssertNotNil(unencDict);
  1795     XCTAssertNil(error);
  1796 
  1797     XCTAssertGreaterThanOrEqual(rating, PEPRatingReliable);
  1798 
  1799     XCTAssertEqualObjects(unencDict.shortMessage, shortMessage);
  1800     XCTAssertEqualObjects(unencDict.longMessage, longMessage);
  1801 
  1802     return unencDict;
  1803 }
  1804 
  1805 - (void)pEpCleanUp
  1806 {
  1807     [PEPTestUtils cleanUp];
  1808 }
  1809 
  1810 - (void)helperXEncStatusForOutgoingEncryptdMailToSelf:(BOOL)toSelf
  1811                                        expectedRating:(PEPRating)expectedRating
  1812 {
  1813     PEPSession *session = [PEPSession new];
  1814 
  1815     // Partner pubkey for the test:
  1816     // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
  1817     // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
  1818     XCTAssertTrue([PEPTestUtils importBundledKey:@"6FF00E97.asc" session:session]);
  1819 
  1820     PEPIdentity *identAlice = [[PEPIdentity alloc]
  1821                                initWithAddress:@"pep.test.alice@pep-project.org"
  1822                                userID:ownUserId
  1823                                userName:@"pEp Test Alice"
  1824                                isOwn:NO
  1825                                fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
  1826     [self updateAndVerifyPartnerIdentity:identAlice session:session];
  1827 
  1828     PEPIdentity *identMe = [[PEPIdentity alloc]
  1829                                initWithAddress:@"me-myself-and-i@pep-project.org"
  1830                                userID:@"me-myself-and-i"
  1831                                userName:@"pEp Me"
  1832                                isOwn:YES];
  1833     NSError *error = nil;
  1834     XCTAssertTrue([session mySelf:identMe error:&error]);
  1835     XCTAssertNil(error);
  1836 
  1837     XCTAssertNotNil(identMe.fingerPrint);
  1838 
  1839     PEPMessage *msg = [PEPMessage new];
  1840     msg.from = identMe;
  1841     msg.to = @[identAlice];
  1842     msg.shortMessage = @"Mail to Alice";
  1843     msg.longMessage = @"Alice?";
  1844     msg.direction = PEPMsgDirectionOutgoing;
  1845 
  1846     NSNumber *numRating = [self testOutgoingRatingForMessage:msg session:session error:&error];
  1847     XCTAssertNotNil(numRating);
  1848     XCTAssertNil(error);
  1849     XCTAssertEqual(numRating.pEpRating, PEPRatingReliable);
  1850 
  1851     PEPMessage *encMsg;
  1852 
  1853     PEPStatus statusEnc = PEPStatusVersionMismatch;
  1854     if (toSelf) {
  1855         encMsg = [session
  1856                   encryptMessage:msg
  1857                   forSelf:identMe
  1858                   extraKeys:nil
  1859                   status:&statusEnc
  1860                   error:&error];
  1861         XCTAssertEqual(statusEnc, PEPStatusOK);
  1862     } else {
  1863         encMsg = [session encryptMessage:msg extraKeys:nil status:nil error:&error];
  1864         XCTAssertNotNil(encMsg);
  1865         XCTAssertNil(error);
  1866     }
  1867     XCTAssertNotNil(encMsg);
  1868 
  1869     PEPStringList *keys;
  1870     PEPRating pEpRating;
  1871     error = nil;
  1872     PEPMessage *decMsg = [session
  1873                           decryptMessage:encMsg
  1874                           flags:nil
  1875                           rating:&pEpRating
  1876                           extraKeys:&keys
  1877                           status:nil
  1878                           error:&error];
  1879     XCTAssertNil(error);
  1880     XCTAssertNotNil(decMsg);
  1881 
  1882     XCTAssertEqual(pEpRating, expectedRating);
  1883 
  1884     NSArray * encStatusField = nil;
  1885     for (NSArray *field in decMsg.optionalFields) {
  1886         NSString *header = [field[0] lowercaseString];
  1887         if ([header isEqualToString:@"x-encstatus"]) {
  1888             encStatusField = field;
  1889         }
  1890     }
  1891     XCTAssertNotNil(encStatusField);
  1892     if (encStatusField) {
  1893         PEPRating outgoingRating = [session ratingFromString:encStatusField[1]];
  1894         XCTAssertEqual(outgoingRating, expectedRating);
  1895     }
  1896 }
  1897 
  1898 @end