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