pEpObjCTests/iOSTests.m
author Dirk Zimmermann <dirk@pep-project.org>
Thu, 02 Nov 2017 15:41:42 +0100
branchIOSAD-61
changeset 319 4800efd10ac6
parent 318 33426789a1b0
child 321 c03b1efd1c8e
permissions -rw-r--r--
IOSAD-61 testMySelfCommType compiles
     1 //
     2 //  iOSTests.m
     3 //  iOSTests
     4 //
     5 //  Created by Edouard Tisserant on 03/11/15.
     6 //  Copyright © 2015 p≡p. All rights reserved.
     7 //
     8 
     9 #import <XCTest/XCTest.h>
    10 
    11 #import "PEPObjCAdapter.h"
    12 #import "PEPInternalSession.h"
    13 
    14 #import "NSDictionary+Extension.h"
    15 #import "PEPIdentity.h"
    16 
    17 // MARK: - Helpers
    18 
    19 PEPDict * _Nonnull mailFromTo(PEPDict * _Nullable fromDict, PEPDict * _Nullable toDict,
    20                               NSString *shortMessage, NSString *longMessage, BOOL outgoing) {
    21     PEPMutableDict *dict = [NSMutableDictionary dictionary];
    22     if (fromDict) {
    23         dict[kPepFrom] = fromDict;
    24     }
    25     if (toDict) {
    26         dict[kPepTo] = @[toDict];
    27     }
    28     if (outgoing) {
    29         dict[kPepOutgoing] = @YES;
    30     } else {
    31         dict[kPepOutgoing] = @NO;
    32     }
    33     dict[kPepShortMessage] = shortMessage;
    34     dict[kPepLongMessage] = longMessage;
    35     return [NSDictionary dictionaryWithDictionary:dict];
    36 }
    37 
    38 // MARK: - PEPSyncDelegate
    39 
    40 @interface SomeSyncDelegate : NSObject<PEPSyncDelegate>
    41 
    42 - (BOOL)waitUntilSent:(time_t)maxSec;
    43 
    44 @property (nonatomic) bool sendWasCalled;
    45 @property (nonatomic, strong) NSCondition *cond;
    46 
    47 @end
    48 
    49 @implementation SomeSyncDelegate
    50 
    51 - (id)init
    52 {
    53     if (self = [super init])  {
    54         self.sendWasCalled = false;
    55         self.cond = [[NSCondition alloc] init];
    56     }
    57     return self;
    58 }
    59 
    60 - (PEP_STATUS)notifyHandshakeWithSignal:(sync_handshake_signal)signal me:(id)me
    61                                 partner:(id)partner
    62 {
    63     return PEP_STATUS_OK;
    64 }
    65 
    66 - (PEP_STATUS)sendMessage:(id)msg
    67 {
    68     [_cond lock];
    69 
    70     _sendWasCalled = true;
    71     [_cond signal];
    72     [_cond unlock];
    73 
    74     return PEP_STATUS_OK;
    75 }
    76 
    77 - (PEP_STATUS)fastPolling:(bool)isfast
    78 {
    79     return PEP_STATUS_OK;
    80 }
    81 
    82 - (BOOL)waitUntilSent:(time_t)maxSec
    83 {
    84     bool res;
    85     [_cond lock];
    86     [_cond waitUntilDate:[NSDate dateWithTimeIntervalSinceNow: 2]];
    87     res = _sendWasCalled;
    88     [_cond unlock];
    89     return res;
    90 }
    91 
    92 @end
    93 
    94 // MARK: - iOSTests
    95 
    96 @interface iOSTests : XCTestCase
    97 
    98 @end
    99 
   100 @implementation iOSTests
   101 
   102 PEPInternalSession *session;
   103 
   104 - (void)setUp
   105 {
   106     [PEPSession cleanup];
   107 }
   108 
   109 #pragma mark -- Helpers
   110 
   111 - (void)delFilePath:(NSString *)path backupAs:(NSString *)bkpsfx {
   112     NSFileManager *fileManager = [NSFileManager defaultManager];
   113     NSError *error;
   114     if ([fileManager fileExistsAtPath:path]) {
   115         BOOL success;
   116         if (!bkpsfx) {
   117             success = [fileManager removeItemAtPath:path error:&error];
   118         } else {
   119             NSString *toPath = [path stringByAppendingString:bkpsfx];
   120             
   121             if ([fileManager fileExistsAtPath:toPath]) {
   122                 [fileManager removeItemAtPath:toPath error:&error];
   123             }
   124             
   125             success = [fileManager moveItemAtPath:path toPath:toPath error:&error];
   126         }
   127         if (!success) {
   128             NSLog(@"Error: %@", [error localizedDescription]);
   129         }
   130     }
   131 }
   132 
   133 - (void)undelFileWithPath:(NSString *)path backup:(NSString *)backup {
   134     NSFileManager *fileManager = [NSFileManager defaultManager];
   135     NSError *error;
   136     NSString* bpath = [path stringByAppendingString:backup];
   137     BOOL fileExists = [fileManager fileExistsAtPath:bpath];
   138     if (fileExists) {
   139         BOOL success;
   140         success = [fileManager moveItemAtPath:bpath toPath:path error:&error];
   141         if (!success) {
   142             NSLog(@"Error: %@", [error localizedDescription]);
   143         }
   144     }
   145 }
   146 
   147 - (NSArray *)pEpWorkFiles
   148 {
   149     // Only files whose content is affected by tests.
   150     NSString* home = [[[NSProcessInfo processInfo]environment]objectForKey:@"HOME"];
   151     NSString* gpgHome = [home stringByAppendingPathComponent:@".gnupg"];
   152     return @[[home stringByAppendingPathComponent:@".pEp_management.db"],
   153              [home stringByAppendingPathComponent:@".pEp_management.db-shm"],
   154              [home stringByAppendingPathComponent:@".pEp_management.db-wal"],
   155              [gpgHome stringByAppendingPathComponent:@"pubring.gpg"],
   156              [gpgHome stringByAppendingPathComponent:@"secring.gpg"]];
   157     
   158 }
   159 
   160 - (void)pEpCleanUp:(NSString *)backup {
   161     session = nil;
   162     [PEPSession cleanup];
   163     
   164     for (id path in [self pEpWorkFiles]) {
   165         [self delFilePath:path backupAs:backup];
   166     }
   167 }
   168 
   169 - (void)pEpCleanUp
   170 {
   171     [self pEpCleanUp:NULL];
   172 }
   173 
   174 - (void)pEpSetUp:(NSString *)restore
   175 {
   176     // Must be the first thing you do before using anything pEp-related
   177     // ... but this is now done in session, with a "dispatch_once"
   178     // [PEPObjCAdapter setupTrustWordsDB:[NSBundle bundleForClass:[self class]]];
   179 
   180     for (id path in [self pEpWorkFiles]) {
   181         [self delFilePath:path backupAs:nil];
   182     }
   183 
   184     if (restore) {
   185         for (id path in [self pEpWorkFiles]) {
   186             [self undelFileWithPath:path backup:restore];
   187         }
   188     }
   189 
   190     session = [[PEPInternalSession alloc] init];
   191     XCTAssert(session);
   192 }
   193 
   194 - (void)pEpSetUp
   195 {
   196     [self pEpSetUp:NULL];
   197 }
   198 
   199 - (void)importBundledKey:(NSString *)item
   200 {
   201     [self importBundledKey:item intoSession:session];
   202 }
   203 
   204 - (NSString *)loadStringFromFileName:(NSString *)fileName
   205 {
   206     NSString *txtFilePath = [[[NSBundle bundleForClass:[self class]] resourcePath]
   207                              stringByAppendingPathComponent:fileName];
   208     NSString *txtFileContents = [NSString stringWithContentsOfFile:txtFilePath
   209                                                           encoding:NSUTF8StringEncoding error:NULL];
   210     return txtFileContents;
   211 }
   212 
   213 - (void)importBundledKey:(NSString *)item intoSession:(PEPInternalSession *)theSession
   214 {
   215 
   216     NSString *txtFileContents = [self loadStringFromFileName:item];
   217     [theSession importKey:txtFileContents];
   218 }
   219 
   220 - (NSDictionary *)unarchiveDictionary:(NSString *)fileName
   221 {
   222     NSString *filePath = [[[NSBundle bundleForClass:[self class]]
   223                            resourcePath] stringByAppendingPathComponent:fileName];
   224     NSMutableData *data = [NSMutableData dataWithContentsOfFile:filePath];
   225     NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
   226     NSDictionary *dict = [unarchiver decodeObject];
   227     [unarchiver finishDecoding];
   228     return dict;
   229 }
   230 
   231 #pragma mark -- Tests
   232 
   233 - (void)testEmptySession
   234 {
   235     [self pEpSetUp];
   236 
   237     // Do nothing.
   238 
   239     [self pEpCleanUp];
   240 }
   241 
   242 
   243 - (void)testNestedSessions
   244 {
   245     [self pEpSetUp];
   246 
   247     PEPInternalSession *session2 = [[PEPInternalSession alloc] init];
   248 
   249     session2 = nil;
   250 
   251     [self pEpCleanUp];
   252 }
   253 
   254 - (void)testShortKeyServerLookup
   255 {
   256     [self pEpSetUp];
   257     [PEPObjCAdapter startKeyserverLookup];
   258     
   259     // Do nothing.
   260     
   261     [PEPObjCAdapter stopKeyserverLookup];
   262     [self pEpCleanUp];
   263 }
   264 
   265 - (void)testLongKeyServerLookup
   266 {
   267     [self pEpSetUp];
   268     [PEPObjCAdapter startKeyserverLookup];
   269     
   270     NSMutableDictionary *ident = [NSMutableDictionary dictionaryWithObjectsAndKeys:
   271                                     @"pEpDontAssert", kPepUsername,
   272                                     @"vb@ulm.ccc.de", kPepAddress,
   273                                     @"SsI6H9", kPepUserID,
   274                                     nil];
   275     
   276     [session updateIdentity:ident];
   277     
   278     sleep(2);
   279 
   280     // FIXME: updateIdentity should not assert if username is not provided
   281     [session updateIdentity:ident];
   282     
   283     XCTAssert(ident[kPepFingerprint]);
   284     
   285     [PEPObjCAdapter stopKeyserverLookup];
   286     [self pEpCleanUp];
   287 }
   288 
   289 - (void)testSyncSession
   290 {
   291     SomeSyncDelegate *syncDelegate = [[SomeSyncDelegate alloc] init];
   292     [self pEpSetUp];
   293     
   294     // This should attach session just created
   295     [PEPObjCAdapter startSync:syncDelegate];
   296 
   297     PEPIdentity *identMe = [[PEPIdentity alloc]
   298                             initWithAddress:@"pep.test.iosgenkey@pep-project.org"
   299                             userName:@"pEp Test iOS GenKey"];
   300     
   301     [session mySelf:identMe];
   302     
   303     bool res = [syncDelegate waitUntilSent:2];
   304     
   305     XCTAssert(res);
   306     
   307     // This should detach session just created
   308     [PEPObjCAdapter stopSync];
   309     
   310     [self pEpCleanUp];
   311 }
   312 
   313 - (void)testTrustWords
   314 {
   315     [self pEpSetUp];
   316 
   317     NSArray *trustwords = [session trustwords:@"DB47DB47DB47DB47DB47DB47DB47DB47DB47DB47" forLanguage:@"en" shortened:false];
   318     XCTAssertEqual([trustwords count], 10);
   319     
   320     for(id word in trustwords)
   321         XCTAssertEqualObjects(word, @"BAPTISMAL");
   322 
   323     [self pEpCleanUp];
   324 }
   325 
   326 - (void)testGenKey
   327 {
   328     [self pEpSetUp];
   329 
   330     PEPIdentity *identMe = [[PEPIdentity alloc]
   331                             initWithAddress:@"pep.test.iosgenkey@pep-project.org" userID:@"Me"
   332                             userName:@"pEp Test iOS GenKey"];
   333 
   334     [session mySelf:identMe];
   335 
   336     XCTAssertNotNil(identMe.fingerPrint);
   337     XCTAssertNotEqual(identMe.commType, PEP_ct_unknown);
   338 
   339     // check that the comm type is not a PGP one
   340     XCTAssertFalse([identMe containsPGPCommType]);
   341 
   342     [self pEpCleanUp];
   343 }
   344 
   345 - (void)testMySelfCommType
   346 {
   347     [self pEpSetUp];
   348 
   349     PEPIdentity *identMe = [[PEPIdentity alloc]
   350                             initWithAddress:@"pep.test.iosgenkey@pep-project.org"
   351                             userID:@"Me"
   352                             userName:@"pEp Test iOS GenKey"];
   353 
   354     [session mySelf:identMe];
   355 
   356     XCTAssertNotNil(identMe.fingerPrint);
   357     XCTAssertNotEqual(identMe.commType, PEP_ct_unknown);
   358 
   359     // check that the comm type is not a PGP one
   360     XCTAssertFalse([identMe containsPGPCommType]);
   361 
   362     dispatch_queue_t queue = dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0);
   363     dispatch_sync(queue, ^{
   364         PEPInternalSession *session2 = [[PEPInternalSession alloc] init];
   365 
   366         // Now simulate an update from the app, which usually only caches
   367         // kPepUsername, kPepAddress and optionally kPepUserID.
   368         PEPIdentity *identMe2 = [[PEPIdentity alloc]
   369                                  initWithAddress:identMe.address
   370                                  userID:identMe.userID
   371                                  userName:identMe.userName];
   372         [session2 mySelf:identMe2];
   373         XCTAssertNotNil(identMe2.fingerPrint);
   374         XCTAssertFalse([identMe2 containsPGPCommType]);
   375         XCTAssertEqual(identMe2.fingerPrint, identMe.fingerPrint);
   376 
   377         // Now pretend the app only knows kPepUsername and kPepAddress
   378         PEPIdentity *identMe3 = [[PEPIdentity alloc]
   379                                  initWithAddress:identMe.address
   380                                  userName:identMe.userName];
   381         [session2 mySelf:identMe3];
   382         XCTAssertNotNil(identMe3.fingerPrint);
   383         XCTAssertFalse([identMe3 containsPGPCommType]);
   384         XCTAssertEqual(identMe3.fingerPrint, identMe.fingerPrint);
   385 
   386         XCTAssertEqualObjects(identMe.address, identMe2.address);
   387         XCTAssertEqualObjects(identMe.address, identMe3.address);
   388         XCTAssertEqual(identMe.commType, identMe2.commType);
   389         XCTAssertEqual(identMe.commType, identMe3.commType);
   390     });
   391 
   392     [self pEpCleanUp];
   393 }
   394 
   395 - (void)testOutgoingColors
   396 {
   397     [self pEpSetUp];
   398 
   399     // Our test user :
   400     // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
   401     // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
   402     [self importBundledKey:@"6FF00E97_sec.asc"];
   403     
   404     NSMutableDictionary *identAlice = [NSMutableDictionary dictionaryWithObjectsAndKeys:
   405                                   @"pEp Test Alice", kPepUsername,
   406                                   @"pep.test.alice@pep-project.org", kPepAddress,
   407                                   @"23", kPepUserID,
   408                                   @"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97",kPepFingerprint,
   409                                   nil];
   410  
   411     [session mySelf:identAlice];
   412     
   413     NSMutableDictionary *msg = [NSMutableDictionary dictionaryWithObjectsAndKeys:
   414                                        identAlice, kPepFrom,
   415                                        [NSMutableArray arrayWithObjects:
   416                                             [NSMutableDictionary dictionaryWithObjectsAndKeys:
   417                                                  @"pEp Test Bob", kPepUsername,
   418                                                  @"pep.test.bob@pep-project.org", kPepAddress,
   419                                                  @"42", kPepUserID,
   420                                                  nil],
   421                                             nil], @"to",
   422                                         @"All Green Test", @"shortmsg",
   423                                         @"This is a text content", @"longmsg",
   424                                         @YES, @"outgoing",
   425                                        nil];
   426 
   427     // Test with unknown Bob
   428     PEP_rating clr = [session outgoingMessageColor:msg];
   429     XCTAssert( clr == PEP_rating_unencrypted);
   430 
   431     // Now let see with bob's pubkey already known
   432     // pEp Test Bob (test key, don't use) <pep.test.bob@pep-project.org>
   433     // BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39
   434     [self importBundledKey:@"0xC9C2EE39.asc"];
   435     
   436     NSMutableDictionary *identBob = [NSMutableDictionary dictionaryWithObjectsAndKeys:
   437                                      @"pEp Test Bob", kPepUsername,
   438                                      @"pep.test.bob@pep-project.org", kPepAddress,
   439                                      @"42", kPepUserID,
   440                                      @"BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39",kPepFingerprint,
   441                                      nil];
   442     
   443     [session updateIdentity:identBob];
   444 
   445     // Should be yellow, since no handshake happened.
   446     clr = [session outgoingMessageColor:msg];
   447     XCTAssert( clr == PEP_rating_reliable);
   448 
   449     clr = [session identityRating:identBob];
   450     XCTAssert( clr == PEP_rating_reliable);
   451     
   452     // Let' say we got that handshake, set PEP_ct_confirmed in Bob's identity
   453     [session trustPersonalKey:identBob];
   454 
   455     // This time it should be green
   456     clr = [session outgoingMessageColor:msg];
   457     XCTAssert( clr == PEP_rating_trusted);
   458 
   459     clr = [session identityRating:identBob];
   460     XCTAssert( clr == PEP_rating_trusted);
   461 
   462     // Let' say we undo handshake
   463     [session keyResetTrust:identBob];
   464     
   465     // Yellow ?
   466     clr = [session outgoingMessageColor:msg];
   467     XCTAssert( clr == PEP_rating_reliable);
   468 
   469     // mistrust Bob
   470     [session keyMistrusted:identBob];
   471     
   472     // Gray == PEP_rating_unencrypted
   473     clr = [session outgoingMessageColor:msg];
   474     XCTAssert( clr == PEP_rating_unencrypted);
   475     
   476     /*
   477     identBob = [NSMutableDictionary dictionaryWithObjectsAndKeys:
   478                 @"pEp Test Bob", kPepUsername,
   479                 @"pep.test.bob@pep-project.org", kPepAddress,
   480                 @"42", kPepUserID,
   481                 @"BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39",kPepFingerprint,
   482                 nil];
   483 */
   484     // Forget
   485     [session keyResetTrust:identBob];
   486     
   487     // Back to yellow
   488     clr = [session outgoingMessageColor:msg];
   489     XCTAssert( clr == PEP_rating_reliable);
   490 
   491     // Trust again
   492     [session trustPersonalKey:identBob];
   493     
   494     // Back to green
   495     clr = [session outgoingMessageColor:msg];
   496     XCTAssert( clr == PEP_rating_trusted);
   497     
   498     // Now let see if it turns back yellow if we add an unconfirmed folk.
   499     // pEp Test John (test key, don't use) <pep.test.john@pep-project.org>
   500     // AA2E4BEB93E5FE33DEFD8BE1135CD6D170DCF575
   501     [self importBundledKey:@"0x70DCF575.asc"];
   502     
   503     NSMutableDictionary *identJohn = [NSMutableDictionary dictionaryWithObjectsAndKeys:
   504                                       @"pEp Test John", kPepUsername,
   505                                       @"pep.test.john@pep-project.org", kPepAddress,
   506                                       @"101", kPepUserID,
   507                                       @"AA2E4BEB93E5FE33DEFD8BE1135CD6D170DCF575",kPepFingerprint,
   508                                       nil];
   509     
   510     [session updateIdentity:identJohn];
   511 
   512     [msg setObject:[NSMutableArray arrayWithObjects:
   513      [NSMutableDictionary dictionaryWithObjectsAndKeys:
   514       @"pEp Test John", kPepUsername,
   515       @"pep.test.john@pep-project.org", kPepAddress,
   516       nil], nil] forKey:@"cc"];
   517 
   518     // Yellow ?
   519     clr = [session outgoingMessageColor:msg];
   520     XCTAssert( clr == PEP_rating_reliable);
   521 
   522     NSMutableDictionary *encmsg;
   523     PEP_STATUS status = [session encryptMessageDict:msg extra:@[] dest:&encmsg];
   524     
   525     XCTAssert(status == PEP_STATUS_OK);
   526     
   527     [self pEpCleanUp];
   528 }
   529 
   530 
   531 - (void)testOutgoingBccColors
   532 {
   533     [self pEpSetUp];
   534     
   535     // Our test user :
   536     // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
   537     // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
   538     [self importBundledKey:@"6FF00E97_sec.asc"];
   539     
   540     NSMutableDictionary *identAlice = [NSMutableDictionary dictionaryWithObjectsAndKeys:
   541                                        @"pEp Test Alice", kPepUsername,
   542                                        @"pep.test.alice@pep-project.org", kPepAddress,
   543                                        @"23", kPepUserID,
   544                                        @"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97",kPepFingerprint,
   545                                        nil];
   546     
   547     [session mySelf:identAlice];
   548     
   549     NSMutableDictionary *msg = [NSMutableDictionary dictionaryWithObjectsAndKeys:
   550                                 identAlice, kPepFrom,
   551                                 [NSMutableArray arrayWithObjects:
   552                                  [NSMutableDictionary dictionaryWithObjectsAndKeys:
   553                                   @"pEp Test Bob", kPepUsername,
   554                                   @"pep.test.bob@pep-project.org", kPepAddress,
   555                                   @"42", kPepUserID,
   556                                   nil],
   557                                  nil], @"to",
   558                                 @"All Green Test", @"shortmsg",
   559                                 @"This is a text content", @"longmsg",
   560                                 @YES, @"outgoing",
   561                                 nil];
   562     
   563     // Test with unknown Bob
   564     PEP_rating clr = [session outgoingMessageColor:msg];
   565     XCTAssert( clr == PEP_rating_unencrypted);
   566     
   567     // Now let see with bob's pubkey already known
   568     // pEp Test Bob (test key, don't use) <pep.test.bob@pep-project.org>
   569     // BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39
   570     [self importBundledKey:@"0xC9C2EE39.asc"];
   571     
   572     NSMutableDictionary *identBob = [NSMutableDictionary dictionaryWithObjectsAndKeys:
   573                                      @"pEp Test Bob", kPepUsername,
   574                                      @"pep.test.bob@pep-project.org", kPepAddress,
   575                                      @"42", kPepUserID,
   576                                      @"BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39",kPepFingerprint,
   577                                      nil];
   578     
   579     [session updateIdentity:identBob];
   580     
   581     // Should be yellow, since no handshake happened.
   582     clr = [session outgoingMessageColor:msg];
   583     XCTAssert( clr == PEP_rating_reliable);
   584     
   585     clr = [session identityRating:identBob];
   586     XCTAssert( clr == PEP_rating_reliable);
   587     
   588     // Let' say we got that handshake, set PEP_ct_confirmed in Bob's identity
   589     [session trustPersonalKey:identBob];
   590     
   591     // This time it should be green
   592     clr = [session outgoingMessageColor:msg];
   593     XCTAssert( clr == PEP_rating_trusted);
   594     
   595     clr = [session identityRating:identBob];
   596     XCTAssert( clr == PEP_rating_trusted);
   597 
   598     // Now let see if it turns back yellow if we add an unconfirmed folk.
   599     // pEp Test John (test key, don't use) <pep.test.john@pep-project.org>
   600     // AA2E4BEB93E5FE33DEFD8BE1135CD6D170DCF575
   601     [self importBundledKey:@"0x70DCF575.asc"];
   602     
   603     NSMutableDictionary *identJohn = [NSMutableDictionary dictionaryWithObjectsAndKeys:
   604                                       @"pEp Test John", kPepUsername,
   605                                       @"pep.test.john@pep-project.org", kPepAddress,
   606                                       @"101", kPepUserID,
   607                                       @"AA2E4BEB93E5FE33DEFD8BE1135CD6D170DCF575",kPepFingerprint,
   608                                       nil];
   609     
   610     [session updateIdentity:identJohn];
   611     
   612     [msg setObject:[NSMutableArray arrayWithObjects:
   613                     [NSMutableDictionary dictionaryWithObjectsAndKeys:
   614                      @"pEp Test John", kPepUsername,
   615                      @"pep.test.john@pep-project.org", kPepAddress,
   616                      @"101", kPepUserID,
   617                      nil], nil] forKey:@"bcc"];
   618     
   619     // Yellow ?
   620     clr = [session outgoingMessageColor:msg];
   621     XCTAssert( clr == PEP_rating_reliable);
   622     
   623     [session trustPersonalKey:identJohn];
   624     
   625     // This time it should be green
   626     clr = [session outgoingMessageColor:msg];
   627     XCTAssert( clr == PEP_rating_trusted);
   628     
   629     clr = [session identityRating:identJohn];
   630     XCTAssert( clr == PEP_rating_trusted);
   631 
   632     /* 
   633      
   634     
   635     NSMutableDictionary *encmsg;
   636     PEP_STATUS status = [session encryptMessageDict:msg extra:@[] dest:&encmsg];
   637     
   638     XCTAssert(status == PEP_STATUS_OK);
   639     */
   640     
   641     [self pEpCleanUp];
   642 }
   643 
   644 - (void)testDontEncryptForMistrusted
   645 {
   646     [self pEpSetUp];
   647     
   648     // Our test user :
   649     // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
   650     // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
   651     [self importBundledKey:@"6FF00E97_sec.asc"];
   652     
   653     PEPIdentity *identAlice = [[PEPIdentity alloc]
   654                                initWithAddress:@"pep.test.alice@pep-project.org"
   655                                userID:@"23" userName:@"pEp Test Alice"
   656                                fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
   657 
   658     [session mySelf:identAlice];
   659     
   660     // pEp Test Bob (test key, don't use) <pep.test.bob@pep-project.org>
   661     // BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39
   662     [self importBundledKey:@"0xC9C2EE39.asc"];
   663     
   664     NSMutableDictionary *identBob = [NSMutableDictionary dictionaryWithObjectsAndKeys:
   665                                      @"pEp Test Bob", kPepUsername,
   666                                      @"pep.test.bob@pep-project.org", kPepAddress,
   667                                      @"42", kPepUserID,
   668                                      @"BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39",kPepFingerprint,
   669                                      nil];
   670     
   671     [session updateIdentity:identBob];
   672 
   673     // mistrust Bob
   674     [session keyMistrusted:identBob];
   675     
   676     NSMutableDictionary *msg = [NSMutableDictionary dictionaryWithObjectsAndKeys:
   677                                 identAlice, kPepFrom,
   678                                 [NSMutableArray arrayWithObjects:
   679                                  [NSMutableDictionary dictionaryWithObjectsAndKeys:
   680                                   @"pEp Test Bob", kPepUsername,
   681                                   @"pep.test.bob@pep-project.org", kPepAddress,
   682                                   @"42", kPepUserID,
   683                                   nil],
   684                                  nil], @"to",
   685                                 @"All Green Test", @"shortmsg",
   686                                 @"This is a text content", @"longmsg",
   687                                 @YES, @"outgoing",
   688                                 nil];
   689     
   690 
   691     // Gray == PEP_rating_unencrypted
   692     PEP_rating clr = [session outgoingMessageColor:msg];
   693     XCTAssert( clr == PEP_rating_unencrypted);
   694 
   695     NSMutableDictionary *encmsg;
   696     PEP_STATUS status = [session encryptMessageDict:msg extra:@[] dest:&encmsg];
   697     
   698     XCTAssert(status == PEP_UNENCRYPTED);
   699 
   700     XCTAssert(![(NSString *)(encmsg[@"attachments"][0][@"mimeType"]) isEqualToString: @"application/pgp-encrypted"]);
   701 
   702     [self pEpCleanUp];
   703 }
   704 
   705 - (void)testRenewExpired
   706 {
   707     [self pEpSetUp];
   708     
   709     // Our expired test user :
   710     // pEp Test Hector (old test key don't use) <pep.test.hector@pep-project.org>
   711     [self importBundledKey:@"5CB2C182_sec.asc"];
   712     
   713     NSMutableDictionary *identHector = [NSMutableDictionary dictionaryWithObjectsAndKeys:
   714                                        @"pEp Test Hector", kPepUsername,
   715                                        @"pep.test.hector@pep-project.org", kPepAddress,
   716                                        @"fc2d33", kPepUserID,
   717                                        @"EEA655839E347EC9E10A5DE2E80CB3FD5CB2C182",kPepFingerprint,
   718                                        nil];
   719     
   720     // Check that this key is indeed expired
   721     [session updateIdentity:identHector];
   722     XCTAssertEqual(PEP_ct_key_expired, [identHector[kPepCommType] integerValue]);
   723 
   724     NSMutableDictionary *identHectorOwn = [NSMutableDictionary dictionaryWithObjectsAndKeys:
   725                                         @"pEp Test Hector", kPepUsername,
   726                                         @"pep.test.hector@pep-project.org", kPepAddress,
   727                                         @PEP_OWN_USERID, kPepUserID,
   728                                         @"EEA655839E347EC9E10A5DE2E80CB3FD5CB2C182",kPepFingerprint,
   729                                         nil];
   730 
   731     // Myself automatically renew expired key.
   732     [session mySelf:identHectorOwn];
   733     XCTAssertEqual(PEP_ct_pEp, [identHectorOwn[kPepCommType] integerValue]);
   734     
   735     [self pEpCleanUp:@"Bob"];
   736     
   737     
   738     [self pEpSetUp:@"Bob"];
   739     
   740     NSMutableDictionary *_identHector = [NSMutableDictionary dictionaryWithObjectsAndKeys:
   741                                          @"pEp Test Hector", kPepUsername,
   742                                          @"pep.test.hector@pep-project.org", kPepAddress,
   743                                          @"khkhkh", kPepUserID,
   744                                          @"EEA655839E347EC9E10A5DE2E80CB3FD5CB2C182",kPepFingerprint,
   745                                          nil];
   746     
   747     [session updateIdentity:_identHector];
   748     XCTAssertEqual(PEP_ct_OpenPGP_unconfirmed, [_identHector[kPepCommType] integerValue]);
   749     
   750     [self pEpCleanUp];
   751 }
   752 
   753 - (void)testRevoke
   754 {
   755     [self pEpSetUp];
   756     
   757     // Our test user :
   758     // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
   759     // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
   760     [self importBundledKey:@"6FF00E97_sec.asc"];
   761     
   762     NSMutableDictionary *identAlice = [NSMutableDictionary dictionaryWithObjectsAndKeys:
   763                                        @"pEp Test Alice", kPepUsername,
   764                                        @"pep.test.alice@pep-project.org", kPepAddress,
   765                                        @"23", kPepUserID,
   766                                        @"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97",kPepFingerprint,
   767                                        nil];
   768     
   769     [session mySelf:identAlice];
   770     
   771     // This will revoke key
   772     [session keyMistrusted:identAlice];
   773     
   774     // Check fingerprint is different
   775     XCTAssertNotEqual(identAlice[kPepFingerprint], @"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97");
   776 
   777     [self pEpCleanUp];
   778 }
   779 
   780 - (void)testMailToMyself
   781 {
   782     [self pEpSetUp];
   783     
   784     // Our test user :
   785     // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
   786     // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
   787     [self importBundledKey:@"6FF00E97_sec.asc"];
   788     
   789     NSMutableDictionary *identAlice = [NSMutableDictionary dictionaryWithObjectsAndKeys:
   790                                        @"pEp Test Alice", kPepUsername,
   791                                        @"pep.test.alice@pep-project.org", kPepAddress,
   792                                        @"23", kPepUserID,
   793                                        @"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97",kPepFingerprint,
   794                                        nil];
   795     
   796     [session mySelf:identAlice];
   797     
   798     NSMutableDictionary *msg = [NSMutableDictionary dictionaryWithObjectsAndKeys:
   799                                 identAlice, kPepFrom,
   800                                 [NSMutableArray arrayWithObjects: identAlice,
   801                                  nil], @"to",
   802                                 @"Mail to Myself", @"shortmsg",
   803                                 @"This is a text content", @"longmsg",
   804                                 @YES, @"outgoing",
   805                                 nil];
   806     
   807     // Test with unknown Bob
   808     PEP_rating clr = [session outgoingMessageColor:msg];
   809     XCTAssert( clr == PEP_rating_trusted_and_anonymized);
   810     
   811     NSMutableDictionary *encmsg;
   812     PEP_STATUS status = [session encryptMessageDict:msg extra:@[] dest:&encmsg];
   813     
   814     XCTAssert(status == PEP_STATUS_OK);
   815     
   816     NSArray* keys;
   817     NSMutableDictionary *decmsg;
   818 
   819     clr = [session decryptMessageDict:encmsg dest:&decmsg keys:&keys];
   820     XCTAssert( clr == PEP_rating_trusted_and_anonymized);
   821     
   822     [self pEpCleanUp];
   823 }
   824 
   825 #if 0
   826 - (void)testMessMisTrust
   827 {
   828 NSMutableDictionary *encmsg;
   829 {
   830     
   831     [self pEpSetUp];
   832     
   833     // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
   834     // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
   835     [self importBundledKey:@"6FF00E97_sec.asc"];
   836     
   837     NSMutableDictionary *identAlice = [NSMutableDictionary dictionaryWithObjectsAndKeys:
   838                                        @"pEp Test Alice", kPepUsername,
   839                                        @"pep.test.alice@pep-project.org", kPepAddress,
   840                                        @"23", kPepUserID,
   841                                        @"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97",kPepFingerprint,
   842                                        nil];
   843     
   844     [session mySelf:identAlice];
   845 
   846     // pEp Test Bob (test key, don't use) <pep.test.bob@pep-project.org>
   847     // BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39
   848     [self importBundledKey:@"0xC9C2EE39.asc"];
   849     
   850     NSMutableDictionary *identBob = [NSMutableDictionary dictionaryWithObjectsAndKeys:
   851                                      @"pEp Test Bob", kPepUsername,
   852                                      @"pep.test.bob@pep-project.org", kPepAddress,
   853                                      @"42", kPepUserID,
   854                                      @"BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39",kPepFingerprint,
   855                                      nil];
   856     
   857     [session updateIdentity:identBob];
   858     
   859     
   860     NSMutableDictionary *msg = [NSMutableDictionary dictionaryWithObjectsAndKeys:
   861                                 identAlice, kPepFrom,
   862                                 [NSMutableArray arrayWithObjects:
   863                                  identBob,
   864                                  nil], @"to",
   865                                 @"All Green Test", @"shortmsg",
   866                                 @"This is a text content", @"longmsg",
   867                                 @YES, @"outgoing",
   868                                 nil];
   869     
   870     PEP_STATUS status = [session encryptMessageDict:msg extra:@[] dest:&encmsg];
   871     XCTAssert(status == PEP_STATUS_OK);
   872     
   873     [self pEpCleanUp];
   874 }
   875 
   876 encmsg[@"outgoing"] = @NO;
   877 [encmsg[kPepFrom] removeObjectForKey:kPepFingerprint];
   878 [encmsg[kPepFrom] removeObjectForKey:kPepUserID];
   879 [encmsg[@"to"][0] removeObjectForKey:kPepFingerprint];
   880 [encmsg[@"to"][0] removeObjectForKey:kPepUserID];
   881 
   882 {
   883     NSMutableDictionary *msg = [encmsg copy];
   884 
   885     [self pEpSetUp];
   886     
   887     
   888     // pEp Test Bob (test key, don't use) <pep.test.bob@pep-project.org>
   889     // BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39
   890     [self importBundledKey:@"C9C2EE39_sec.asc"];
   891     
   892     NSMutableDictionary *identBob = [NSMutableDictionary dictionaryWithObjectsAndKeys:
   893                                      @"pEp Test Bob", kPepUsername,
   894                                      @"pep.test.bob@pep-project.org", kPepAddress,
   895                                      @"42", kPepUserID,
   896                                      @"BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39",kPepFingerprint,
   897                                      nil];
   898     
   899     [session mySelf:identBob];
   900 
   901     msg[kPepFrom][kPepUserID] = @"new_id_from_mail";
   902     
   903     NSMutableDictionary *decmsg;
   904     NSArray* keys;
   905     PEP_rating clr = [session decryptMessageDict:msg dest:&decmsg keys:&keys];
   906     XCTAssert(clr == PEP_rating_reliable);
   907     
   908     NSMutableDictionary *identAlice = [NSMutableDictionary dictionaryWithObjectsAndKeys:
   909                                        @"pEp Test Alice", kPepUsername,
   910                                        @"pep.test.alice@pep-project.org", kPepAddress,
   911                                        @"new_id_from_mail", kPepUserID,
   912                                        nil];
   913     
   914     [session updateIdentity:identAlice];
   915     clr = [session identityRating:identAlice];
   916     XCTAssert( clr == PEP_rating_reliable);
   917 
   918     [session trustPersonalKey:identAlice];
   919     clr = [session identityRating:identAlice];
   920     XCTAssert( clr == PEP_rating_trusted);
   921     
   922     [session keyResetTrust:identAlice];
   923     clr = [session identityRating:identAlice];
   924     XCTAssert( clr == PEP_rating_reliable);
   925     
   926     [self pEpCleanUp:@"Bob"];
   927     
   928 }{ // This is simulating a shutdown.
   929     NSMutableDictionary *msg = [encmsg copy];
   930 
   931     msg[kPepFrom][kPepUserID] = @"new_id_from_mail";
   932 
   933     [self pEpSetUp:@"Bob"];
   934     
   935     PEP_rating clr;
   936     {
   937         NSArray* keys;
   938         NSMutableDictionary *decmsg;
   939         clr = [session decryptMessageDict:msg dest:&decmsg keys:&keys];
   940     }
   941     XCTAssert(clr == PEP_rating_reliable);
   942     
   943     NSMutableDictionary *identAlice = [NSMutableDictionary dictionaryWithObjectsAndKeys:
   944                                        @"pEp Test Alice", kPepUsername,
   945                                        @"pep.test.alice@pep-project.org", kPepAddress,
   946                                        @"new_id_from_mail", kPepUserID,
   947                                        nil];
   948     
   949     [session updateIdentity:identAlice];
   950     clr = [session identityRating:identAlice];
   951     XCTAssert( clr == PEP_rating_reliable);
   952     
   953     [session keyMistrusted:identAlice];
   954     clr = [session identityRating:identAlice];
   955     XCTAssert( clr == PEP_rating_mistrust);
   956     
   957     [session keyResetTrust:identAlice];
   958     clr = [session identityRating:identAlice];
   959     XCTAssert( clr == PEP_rating_reliable);
   960     
   961     [session trustPersonalKey:identAlice];
   962     clr = [session identityRating:identAlice];
   963     XCTAssert( clr == PEP_rating_trusted);
   964     
   965 }{
   966     NSMutableDictionary *msg = [encmsg copy];
   967     PEP_rating clr;
   968 
   969     msg[kPepFrom][kPepUserID] = @"new_id_from_mail";
   970     {
   971         NSArray* keys;
   972         NSMutableDictionary *decmsg;
   973         clr = [session decryptMessageDict:msg dest:&decmsg keys:&keys];
   974     }
   975     XCTAssert(clr == PEP_rating_trusted);
   976     
   977     [self pEpCleanUp];
   978     
   979 }}
   980 #endif
   981 
   982 - (void)testTwoNewUsers
   983 {
   984     NSMutableDictionary* petrasMsg;
   985     NSMutableDictionary *identMiroAtPetra = @{ kPepUsername: @"Miro",
   986                                                kPepAddress: @"pep.test.miro@pep-project.org",
   987                                                kPepUserID: @"Him" }.mutableCopy;
   988 
   989     [self pEpSetUp];
   990     {
   991         NSMutableDictionary *identPetra = @{ kPepUsername: @"Petra",
   992                                              kPepAddress: @"pep.test.petra@pep-project.org",
   993                                              kPepUserID: @"Me" }.mutableCopy;
   994         
   995         [session mySelf:identPetra];
   996         XCTAssert(identPetra[kPepFingerprint]);
   997         
   998         NSMutableDictionary *msg = @{ kPepFrom: identPetra,
   999                                       kPepTo: @[identMiroAtPetra],
  1000                                       kPepShortMessage: @"Lets use pEp",
  1001                                       kPepLongMessage: @"Dear, I just installed pEp, you should do the same !",
  1002                                       kPepOutgoing: @YES }.mutableCopy;
  1003         
  1004         PEP_STATUS status = [session encryptMessageDict:msg extra:@[] dest:&petrasMsg];
  1005         XCTAssert(status == PEP_UNENCRYPTED);
  1006     }
  1007     
  1008     [self pEpCleanUp:@"Petra"];
  1009 
  1010     // Meanwhile, Petra's outgoing message goes through the Internet,
  1011     // and becomes incomming message to Miro
  1012     petrasMsg[kPepOutgoing] = @NO;
  1013 
  1014     NSMutableDictionary* mirosMsg;
  1015     
  1016     [self pEpSetUp];
  1017     
  1018     {
  1019         NSMutableDictionary *identMiro = @{ kPepUsername: @"Miro",
  1020                                             kPepAddress: @"pep.test.miro@pep-project.org",
  1021                                             kPepUserID: @"Me" }.mutableCopy;
  1022     
  1023         [session mySelf:identMiro];
  1024         XCTAssert(identMiro[kPepFingerprint]);
  1025     
  1026         NSMutableDictionary *decmsg;
  1027         NSArray* keys;
  1028         PEP_rating clr = [session decryptMessageDict:petrasMsg dest:&decmsg keys:&keys];
  1029         XCTAssert(clr == PEP_rating_unencrypted);
  1030 
  1031         NSMutableDictionary *msg = @{ kPepFrom: identMiro,
  1032                                       kPepTo:
  1033                                           @[ @{ kPepUsername: @"Petra",
  1034                                                 kPepAddress: @"pep.test.petra@pep-project.org" }],
  1035                                       kPepShortMessage: @"re:Lets use pEp",
  1036                                       kPepLongMessage: @"That was so easy !",
  1037                                       kPepOutgoing: @YES }.mutableCopy;
  1038         
  1039         PEP_STATUS status = [session encryptMessageDict:msg extra:@[] dest:&mirosMsg];
  1040         XCTAssert(status == PEP_STATUS_OK);
  1041     }
  1042     
  1043     [self pEpCleanUp:@"Miro"];
  1044     
  1045     // Again, outgoing flips into incoming
  1046     mirosMsg[kPepOutgoing] = @NO;
  1047     
  1048     [self pEpSetUp:@"Petra"];
  1049     {
  1050         NSMutableDictionary *decmsg;
  1051         NSArray* keys;
  1052         NSMutableDictionary *encmsg = mirosMsg.mutableCopy;
  1053         [encmsg setObject:identMiroAtPetra.mutableCopy forKey:kPepFrom];
  1054 
  1055         PEP_rating clr = [session decryptMessageDict:encmsg dest:&decmsg keys:&keys];
  1056 
  1057         XCTAssertEqual(clr, PEP_rating_reliable);
  1058 
  1059         PEP_rating secondclr = [session reEvaluateMessageRating:decmsg];
  1060 
  1061         XCTAssertEqual(secondclr, PEP_rating_reliable);
  1062 
  1063         // Check Miro is in DB
  1064         [session updateIdentity:identMiroAtPetra];
  1065         
  1066         XCTAssertNotNil(identMiroAtPetra[kPepFingerprint]);
  1067         
  1068         NSLog(@"Test fpr %@",identMiroAtPetra[kPepFingerprint]);
  1069 
  1070         // Trust to that identity
  1071         [session trustPersonalKey:identMiroAtPetra];
  1072 
  1073         secondclr = [session reEvaluateMessageRating:decmsg];
  1074         XCTAssertEqual(secondclr, PEP_rating_trusted_and_anonymized, @"Not trusted");
  1075         
  1076         clr = [session decryptMessageDict:encmsg dest:&decmsg keys:&keys];
  1077         XCTAssertEqual(clr, PEP_rating_trusted_and_anonymized, @"Not trusted");
  1078 
  1079         // Undo trust
  1080         [session keyResetTrust:identMiroAtPetra];
  1081         
  1082         clr = [session decryptMessageDict:encmsg dest:&decmsg keys:&keys];
  1083         XCTAssertEqual(clr, PEP_rating_reliable, @"keyResetTrust didn't work?");
  1084         
  1085         // Try compromized
  1086         [session keyMistrusted:identMiroAtPetra];
  1087 
  1088         clr = [session decryptMessageDict:encmsg dest:&decmsg keys:&keys];
  1089         XCTAssertEqual(clr, PEP_rating_mistrust, @"Not mistrusted");
  1090         
  1091         // Regret
  1092         [session keyResetTrust:identMiroAtPetra];
  1093         
  1094         clr = [session decryptMessageDict:encmsg dest:&decmsg keys:&keys];
  1095         XCTAssertEqual(clr, PEP_rating_reliable, @"keyResetTrust didn't work?");
  1096         
  1097         // Trust again.
  1098         [session trustPersonalKey:identMiroAtPetra];
  1099         
  1100         clr = [session decryptMessageDict:encmsg dest:&decmsg keys:&keys];
  1101         XCTAssertEqual(clr, PEP_rating_trusted_and_anonymized, @"Not trusted");
  1102 
  1103         XCTAssertEqualObjects(decmsg[@"longmsg"], @"That was so easy !");
  1104     }
  1105     [self pEpCleanUp:@"Petra"];
  1106 }
  1107 
  1108 #if 0
  1109 - (void)testEncryptedMailFromOutlook
  1110 {
  1111 
  1112     [self pEpSetUp];
  1113 
  1114     // This is the secret key for test001@peptest.ch
  1115     [self importBundledKey:@"B623F674_sec.asc"];
  1116 
  1117     NSMutableDictionary *identMe = @{ kPepUsername: @"Test 001",
  1118                                      kPepAddress: @"test001@peptest.ch",
  1119                                      kPepUserID: @"B623F674" }.mutableCopy;
  1120     NSMutableDictionary *identMeOutlook = @{ kPepUsername: @"Outlook 1",
  1121                                              kPepAddress: @"outlook1@peptest.ch",
  1122                                              kPepUserID: @"outlook1" }.mutableCopy;
  1123 
  1124     NSString *msgFilePath = [[[NSBundle bundleForClass:[self class]] resourcePath]
  1125                              stringByAppendingPathComponent:@"msg_to_B623F674.asc"];
  1126     NSString *msgFileContents = [NSString stringWithContentsOfFile:msgFilePath
  1127                                                           encoding:NSASCIIStringEncoding error:NULL];
  1128 
  1129     NSMutableDictionary *msg = @{ kPepFrom: identMe,
  1130                                   @"to": @[identMeOutlook],
  1131                                   @"shortmsg": @"Some subject",
  1132                                   @"longmsg": msgFileContents,
  1133                                   @"incoming": @YES }.mutableCopy;
  1134 
  1135     // Should happen quite fast, since test001@peptest.ch already has a secret key
  1136     [session mySelf:identMe];
  1137     XCTAssert(identMe[kPepFingerprint]);
  1138 
  1139     [session updateIdentity:identMeOutlook];
  1140 
  1141     NSArray *keys;
  1142     NSMutableDictionary *decMsg;
  1143     PEP_rating clr = [session decryptMessage:msg dest:&decMsg keys:&keys];
  1144     XCTAssertEqual(clr, PEP_rating_reliable);
  1145 
  1146     [self pEpCleanUp];
  1147 }
  1148 #endif
  1149 
  1150 - (void)testEncryptedMailFromMutt
  1151 {
  1152     [self pEpSetUp];
  1153 
  1154     // This is the public key for test001@peptest.ch
  1155     [self importBundledKey:@"A3FC7F0A.asc"];
  1156 
  1157     // This is the secret key for test001@peptest.ch
  1158     [self importBundledKey:@"A3FC7F0A_sec.asc"];
  1159 
  1160     // Mail from mutt, already processed into message dict by the app.
  1161     NSMutableDictionary *msgDict = [self unarchiveDictionary:@"msg_to_A3FC7F0A_from_mutt.ser"].mutableCopy;
  1162     [msgDict removeObjectForKey:kPepLongMessage];
  1163     [msgDict removeObjectForKey:kPepLongMessageFormatted];
  1164 
  1165     // Also extracted "live" from the app.
  1166     NSMutableDictionary *accountDict = [self unarchiveDictionary:@"account_A3FC7F0A.ser"].mutableCopy;
  1167     [accountDict removeObjectForKey:kPepCommType];
  1168     [accountDict removeObjectForKey:kPepFingerprint];
  1169 
  1170     [session mySelf:accountDict];
  1171     XCTAssertNotNil(accountDict[kPepFingerprint]);
  1172 
  1173     NSArray* keys;
  1174     NSMutableDictionary *pepDecryptedMail;
  1175     [session decryptMessageDict:msgDict dest:&pepDecryptedMail keys:&keys];
  1176     XCTAssertNotNil(pepDecryptedMail[kPepLongMessage]);
  1177 
  1178     [self pEpCleanUp];
  1179 }
  1180 
  1181 - (NSString *)loadStringByName:(NSString *)name
  1182 {
  1183     NSURL *url = [[NSBundle bundleForClass:[self class]] URLForResource:name withExtension:nil];
  1184     return [NSString stringWithContentsOfURL:url usedEncoding:nil error:nil];
  1185 }
  1186 
  1187 /**
  1188  Checks reduced BCCs support when encrypting.
  1189  Currently only one BCCs recipient is supported for encrypting.
  1190  In that case no single TO oc CC are allowed.
  1191  */
  1192 - (void)testEncryptBcc
  1193 {
  1194     NSString *theMessage = @"THE MESSAGE";
  1195 
  1196     [self pEpSetUp];
  1197 
  1198     NSMutableDictionary *partner1Orig =
  1199     @{ kPepAddress: @"partner1@dontcare.me",
  1200        kPepUserID: @"partner1",
  1201        kPepFingerprint: @"F0CD3F7B422E5D587ABD885BF2D281C2789DD7F6",
  1202        kPepUsername: @"partner1" }.mutableCopy;
  1203 
  1204     NSMutableDictionary *meOrig =
  1205     @{ kPepAddress: @"me@dontcare.me",
  1206        kPepUserID: @"me",
  1207        kPepFingerprint: @"CC1F73F6FB774BF08B197691E3BFBCA9248FC681",
  1208        kPepUsername: @"me" }.mutableCopy;
  1209 
  1210     NSString *pubKeyPartner1 = [self loadStringByName:@"partner1_F2D281C2789DD7F6_pub.asc"];
  1211     XCTAssertNotNil(pubKeyPartner1);
  1212     NSString *pubKeyMe = [self loadStringByName:@"meATdontcare_E3BFBCA9248FC681_pub.asc"];
  1213     XCTAssertNotNil(pubKeyMe);
  1214     NSString *secKeyMe = [self loadStringByName:@"meATdontcare_E3BFBCA9248FC681_sec.asc"];
  1215     XCTAssertNotNil(secKeyMe);
  1216 
  1217     __block NSMutableDictionary *pepEncMail;
  1218     {
  1219         NSMutableDictionary *me = meOrig.mutableCopy;
  1220 
  1221         NSMutableDictionary *partner1 = partner1Orig.mutableCopy;
  1222 
  1223         NSMutableDictionary *mail = @{ kPepFrom: me,
  1224                                        kPepOutgoing: @YES,
  1225                                        kPepLongMessage: theMessage,
  1226                                        kPepBCC: @[partner1] }.mutableCopy;
  1227 
  1228         [session importKey:pubKeyMe];
  1229         [session importKey:secKeyMe];
  1230         [session mySelf:me];
  1231         XCTAssertNotNil(me[kPepFingerprint]);
  1232         XCTAssertEqualObjects(me[kPepFingerprint], meOrig[kPepFingerprint]);
  1233         [session importKey:pubKeyPartner1];
  1234         PEP_STATUS status = [session encryptMessageDict:mail extra:nil dest:&pepEncMail];
  1235         XCTAssertEqual(status, PEP_STATUS_OK);
  1236     }
  1237 
  1238     [self pEpCleanUp];
  1239 
  1240     [self pEpSetUp];
  1241     {
  1242         NSMutableDictionary *partner1 = partner1Orig.mutableCopy;
  1243 
  1244         NSString *privateKeyPartner1 = [self
  1245                                         loadStringByName:@"partner1_F2D281C2789DD7F6_sec.asc"];
  1246         [session importKey:privateKeyPartner1];
  1247         XCTAssertNotNil(privateKeyPartner1);
  1248 
  1249         [session importKey:pubKeyPartner1];
  1250         [session importKey:pubKeyMe];
  1251 
  1252         [session mySelf:partner1];
  1253         XCTAssertNotNil(partner1[kPepFingerprint]);
  1254         XCTAssertEqualObjects(partner1[kPepFingerprint], partner1Orig[kPepFingerprint]);
  1255 
  1256         NSMutableDictionary *me = meOrig.mutableCopy;
  1257         [session updateIdentity:me];
  1258 
  1259         NSMutableDictionary *pepDecryptedMail;
  1260         NSArray *keys = [NSArray array];
  1261         [session decryptMessageDict:pepEncMail dest:&pepDecryptedMail keys:&keys];
  1262 
  1263         // If this assert holds, then the engine ignores BCCs when encrypting
  1264         XCTAssertEqualObjects(pepDecryptedMail[kPepLongMessage], theMessage);
  1265     }
  1266 
  1267     [self pEpCleanUp];
  1268 }
  1269 
  1270 - (void)doSomeWorkOnSession:(PEPInternalSession *)session count:(NSInteger)count
  1271 {
  1272     NSMutableDictionary *me = @{ kPepAddress: [NSString stringWithFormat:@"me%ld@dontcare.me",
  1273                                                (long)count],
  1274                                  kPepUserID: [NSString stringWithFormat:@"me%ld", (long)count],
  1275                                  kPepUsername: [NSString stringWithFormat:@"me%ld",
  1276                                                 (long)count] }.mutableCopy;
  1277     [session mySelf:me];
  1278     XCTAssertNotNil(me[kPepFingerprint]);
  1279 }
  1280 
  1281 - (void)testParallelSessions
  1282 {
  1283     //[PEPObjCAdapter setupTrustWordsDB:[NSBundle bundleForClass:[self class]]];
  1284 
  1285     // Currently, the first session use MUST be on the main thread
  1286     [self pEpSetUp];
  1287     [self doSomeWorkOnSession:session count:0];
  1288 
  1289     dispatch_group_t group = dispatch_group_create();
  1290     dispatch_queue_t queue = dispatch_queue_create("test", DISPATCH_QUEUE_CONCURRENT);
  1291 
  1292     for (int i = 1; i < 3; ++i) {
  1293         dispatch_group_async(group, queue, ^{
  1294             PEPInternalSession *innerSession = [[PEPInternalSession alloc] init];
  1295             [self doSomeWorkOnSession:innerSession count:i];
  1296             innerSession = nil;
  1297         });
  1298     }
  1299 
  1300     long result = dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
  1301     XCTAssertEqual(result, 0);
  1302 
  1303     [self pEpCleanUp];
  1304 }
  1305 
  1306 #if 0 // This test assert fails
  1307 - (void)testParallelDecryptionTest
  1308 {
  1309     // Have one session open at all times, from main thread
  1310     [self pEpSetUp];
  1311 
  1312     // Mail from outlook1@peptest.ch to test001@peptest.ch, extracted from the app
  1313     NSDictionary *msgDict = [self unarchiveDictionary:@"msg_to_78EE1DBC_from_outlook.ser"];
  1314 
  1315     // Also extracted "live" from the app.
  1316     NSDictionary *accountDict = [self unarchiveDictionary:@"account_78EE1DBC.ser"];
  1317 
  1318     PEPSession *someSession = [[PEPSession alloc] init];
  1319 
  1320     // This is the public key for test001@peptest.ch
  1321     [self importBundledKey:@"78EE1DBC.asc" intoSession:someSession];
  1322 
  1323     // This is the secret key for test001@peptest.ch
  1324     [self importBundledKey:@"78EE1DBC_sec.asc" intoSession:someSession];
  1325 
  1326     someSession = nil;
  1327 
  1328     dispatch_queue_t queue = dispatch_queue_create("Concurrent test queue",
  1329                                                    DISPATCH_QUEUE_CONCURRENT);
  1330     dispatch_group_t group = dispatch_group_create();
  1331 
  1332     void (^decryptionBlock)(int) = ^(int index) {
  1333         PEPSession *innerSession = [[PEPSession alloc] init];
  1334 
  1335         NSMutableDictionary *innerAccountDict = [accountDict mutableCopy];
  1336         [innerSession mySelf:innerAccountDict];
  1337         XCTAssertNotNil(innerAccountDict[kPepFingerprint]);
  1338 
  1339         NSArray* keys;
  1340         NSMutableDictionary *pepDecryptedMail;
  1341         PEP_rating color = [innerSession decryptMessageDict:msgDict dest:&pepDecryptedMail
  1342                                                       keys:&keys];
  1343         XCTAssertEqual(color, PEP_rating_reliable);
  1344         NSLog(@"%d: decryption color -> %d", index, color);
  1345 
  1346     };
  1347 
  1348     // Test single decryption on main thread
  1349     decryptionBlock(0);
  1350 
  1351     for (int i = 1; i < 21; ++i) {
  1352         dispatch_group_async(group, queue, ^{
  1353             decryptionBlock(i);
  1354         });
  1355     }
  1356 
  1357     dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
  1358 
  1359     [self pEpCleanUp];
  1360 }
  1361 #endif
  1362 
  1363 /**
  1364  Simulate accessing a sent folder with about 20 messages in it, and trying to decrypt them
  1365  all at once.
  1366  */
  1367 #if 0 // This test assert fails
  1368 - (void)testLoadMassiveSentFolder
  1369 {
  1370     // Have one session open at all times, from main thread
  1371     [self pEpSetUp];
  1372 
  1373     NSDictionary *meOrig = @{ kPepAddress: @"test000@dontcare.me",
  1374                               kPepUserID: @"test000",
  1375                               kPepUsername: @"Test 000" };
  1376 
  1377     NSDictionary *partner = @{ kPepAddress: @"test001@peptest.ch",
  1378                                kPepUserID: @"test001",
  1379                                kPepFingerprint: @"FEBFEAC4AB3E870C447C8427BD7B7A3478EE1DBC",
  1380                                kPepUsername: @"Test 001" };
  1381 
  1382     dispatch_queue_t queue = dispatch_queue_create("Concurrent test queue",
  1383                                                    DISPATCH_QUEUE_CONCURRENT);
  1384     dispatch_group_t group = dispatch_group_create();
  1385 
  1386     // Set up keys in a background thread
  1387     dispatch_group_enter(group);
  1388     dispatch_async(queue, ^{
  1389         PEPSession *someSession = [[PEPSession alloc] init];
  1390         NSMutableDictionary *mySelf = meOrig.mutableCopy;
  1391         [someSession mySelf:mySelf];
  1392         XCTAssertNotNil(mySelf[kPepFingerprint]);
  1393 
  1394         // This is the public key for test001@peptest.ch (partner)
  1395         [self importBundledKey:@"78EE1DBC.asc" intoSession:someSession];
  1396         dispatch_group_leave(group);
  1397     });
  1398     dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
  1399 
  1400     // Write a couple of mails to 78EE1DBC
  1401     NSMutableArray *sentMails = @[].mutableCopy;
  1402     dispatch_goup_async(group, queue, ^{
  1403         PEPSession *someSession = [[PEPSession alloc] init];
  1404         NSMutableDictionary *mySelf = meOrig.mutableCopy;
  1405         [someSession mySelf:mySelf];
  1406         XCTAssertNotNil(mySelf[kPepFingerprint]);
  1407 
  1408         for (int i = 0; i < 20; i++) {
  1409             NSDictionary *mail = @{ kPepFrom: mySelf,
  1410                                     kPepTo: @[partner],
  1411                                     kPepShortMessage: [NSString
  1412                                                        stringWithFormat:@"Message %d",
  1413                                                        i + 1],
  1414                                     kPepLongMessage: [NSString
  1415                                                       stringWithFormat:@"Message Content %d",
  1416                                                        i + 1],
  1417                                     kPepOutgoing: @YES};
  1418 
  1419             NSDictionary *encryptedMail;
  1420             PEP_STATUS status = [someSession encryptMessageDict:mail extra:@[] dest:&encryptedMail];
  1421             XCTAssertEqual(status, PEP_STATUS_OK);
  1422 
  1423             [sentMails addObject:encryptedMail];
  1424         }
  1425 
  1426     });
  1427     dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
  1428 
  1429     // massively decrypt
  1430     for (NSDictionary *sentMail in sentMails) {
  1431         dispatch_group_async(group, queue, ^{
  1432             PEPSession *someSession = [[PEPSession alloc] init];
  1433             NSDictionary *decryptedMail;
  1434             NSArray *keys;
  1435             PEP_rating color = [someSession decryptMessageDict:sentMail dest:&decryptedMail
  1436                                                          keys:&keys];
  1437             NSLog(@"Decrypted %@: %d", decryptedMail[kPepShortMessage], color);
  1438             XCTAssertGreaterThanOrEqual(color, PEP_rating_reliable);
  1439         });
  1440     }
  1441     dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
  1442 
  1443     [self pEpCleanUp];
  1444 }
  1445 #endif
  1446 
  1447 - (void)testOutgoingContactColor
  1448 {
  1449     [self pEpSetUp];
  1450 
  1451     NSMutableDictionary *partner1Orig =
  1452     @{kPepAddress: @"partner1@dontcare.me",
  1453       kPepUsername: @"Partner 1"}.mutableCopy;
  1454 
  1455     NSString *pubKeyPartner1 = [self loadStringByName:@"partner1_F2D281C2789DD7F6_pub.asc"];
  1456     XCTAssertNotNil(pubKeyPartner1);
  1457     [session importKey:pubKeyPartner1];
  1458 
  1459     PEP_rating color = [session identityRating:partner1Orig];
  1460     XCTAssertEqual(color, PEP_rating_reliable);
  1461 
  1462     [self pEpCleanUp];
  1463 
  1464 }
  1465 
  1466 - (PEPDict *)internalEncryptToMySelfKeys:(PEPStringList **)keys
  1467 {
  1468     NSMutableDictionary *me = @{kPepUsername: kPepUsername,
  1469                                 kPepAddress: @"me@peptest.ch"}.mutableCopy;
  1470     [session mySelf:me];
  1471     XCTAssertNotNil(me[kPepFingerprint]);
  1472 
  1473     // Create draft
  1474     NSString *shortMessage = @"Subject";
  1475     NSString *longMessage = @"Oh, this is a long body text!";
  1476     PEPDict *mail = mailFromTo(me, me, shortMessage, longMessage, YES);
  1477 
  1478     NSMutableDictionary *encDict;
  1479     PEP_STATUS status = [session encryptMessageDict:mail identity:me dest:&encDict];
  1480     XCTAssertEqual(status, 0);
  1481     XCTAssertEqualObjects(encDict[kPepShortMessage], @"p≡p");
  1482 
  1483     NSMutableDictionary *unencDict;
  1484     PEP_rating rating = [session decryptMessageDict:encDict dest:&unencDict keys:keys];
  1485     XCTAssertGreaterThanOrEqual(rating, PEP_rating_reliable);
  1486 
  1487     XCTAssertEqualObjects(unencDict[kPepShortMessage], shortMessage);
  1488     XCTAssertEqualObjects(unencDict[kPepLongMessage], longMessage);
  1489 
  1490     return unencDict;
  1491 }
  1492 
  1493 - (void)testEncryptToMySelf
  1494 {
  1495     [self pEpSetUp];
  1496     [self internalEncryptToMySelfKeys:nil];
  1497     [self pEpCleanUp];
  1498 }
  1499 
  1500 - (void)testMessageTrustwordsWithMySelf
  1501 {
  1502     [self pEpSetUp];
  1503 
  1504     PEPStringList *keys = nil;
  1505     PEPDict *decryptedDict = [self internalEncryptToMySelfKeys:&keys];
  1506     XCTAssertNotNil(keys);
  1507     XCTAssert(keys.count > 0);
  1508 
  1509     NSMutableDictionary *receiver = [decryptedDict[kPepTo][0] mutableCopy];
  1510     [session updateIdentity:receiver];
  1511     XCTAssertNotNil(receiver);
  1512     PEP_STATUS trustwordsStatus;
  1513 
  1514     NSString *trustwords = [session getTrustwordsMessageDict:decryptedDict
  1515                                                 receiverDict:receiver
  1516                                                    keysArray:keys language:@"en"
  1517                                                         full:YES
  1518                                              resultingStatus: &trustwordsStatus];
  1519 
  1520     // No trustwords with yourself
  1521     XCTAssertEqual(trustwordsStatus, PEP_TRUSTWORDS_DUPLICATE_FPR);
  1522     XCTAssertNil(trustwords);
  1523 
  1524     [self pEpCleanUp];
  1525 }
  1526 
  1527 - (void)testGetTrustwords
  1528 {
  1529     [self pEpSetUp];
  1530 
  1531     NSDictionary *partner1Orig =
  1532     @{ kPepAddress: @"partner1@dontcare.me",
  1533        kPepUserID: @"partner1",
  1534        kPepFingerprint: @"F0CD3F7B422E5D587ABD885BF2D281C2789DD7F6",
  1535        kPepUsername: @"partner1" };
  1536 
  1537     NSDictionary *meOrig =
  1538     @{ kPepAddress: @"me@dontcare.me",
  1539        kPepUserID: @"me",
  1540        kPepFingerprint: @"CC1F73F6FB774BF08B197691E3BFBCA9248FC681",
  1541        kPepUsername: @"me" };
  1542 
  1543     NSString *pubKeyPartner1 = [self loadStringByName:@"partner1_F2D281C2789DD7F6_pub.asc"];
  1544     XCTAssertNotNil(pubKeyPartner1);
  1545     NSString *pubKeyMe = [self loadStringByName:@"meATdontcare_E3BFBCA9248FC681_pub.asc"];
  1546     XCTAssertNotNil(pubKeyMe);
  1547     NSString *secKeyMe = [self loadStringByName:@"meATdontcare_E3BFBCA9248FC681_sec.asc"];
  1548     XCTAssertNotNil(secKeyMe);
  1549 
  1550     NSString *trustwordsFull = [session getTrustwordsIdentity1:meOrig identity2:partner1Orig
  1551                                                   language:nil full:YES];
  1552     XCTAssertEqualObjects(trustwordsFull,
  1553                           @"EMERSON GASPER TOKENISM BOLUS COLLAGE DESPISE BEDDED ENCRYPTION IMAGINE BEDFORD");
  1554 
  1555     NSString *trustwordsFullEnglish = [session getTrustwordsIdentity1:meOrig identity2:partner1Orig
  1556                                                              language:@"en" full:YES];
  1557     XCTAssertEqualObjects(trustwordsFullEnglish, trustwordsFull);
  1558 
  1559     NSString *trustwordsUndefined = [session getTrustwordsIdentity1:meOrig identity2:partner1Orig
  1560                                                            language:@"ZZ" full:YES];
  1561     XCTAssertNil(trustwordsUndefined);
  1562 
  1563     [self pEpCleanUp];
  1564 }
  1565 // Temporary tests that made to reproduce concurrency issues, i.e. to fail
  1566 // TODO: delete this tests after a final solution/approach is found for our concurrency isues
  1567 // TODO: re-write those tests using the found solution
  1568 
  1569 //#pragma mark - Concurrent Calls
  1570 //
  1571 //// Tests trying to reproduce IOSAD-35 (IOSAD-23)
  1572 //// Assumption: multiple sessions in one thread are OK
  1573 ///*
  1574 // This tests crashes often (but not always) here.
  1575 // Assertion failed: (status == PEP_STATUS_OK), function _myself, file /Users/buff/workspace/pEp/src/pEpEngine/src/keymanagement.c, line 619.
  1576 // If you can not reproduce it, comment the marked line, run, run agin, uncomment the marked line, run.
  1577 // */
  1578 //- (void)testParallelDecryptionOneThreadMultipleSessions
  1579 //{
  1580 //    // Have one session open at all times, from main thread
  1581 //    [self pEpSetUp];
  1582 //    // An unecrypted Mail
  1583 //    NSMutableDictionary *msgDict = [[self unarchiveDictionary:@"msg_to_A3FC7F0A_from_mutt.ser"] mutableCopy];
  1584 //    [msgDict removeObjectForKey:@"attachments"]; // toggle comment/uncomment this line in between runs helps to reproduce the issue
  1585 //    msgDict[kPepAddress] = @"some.unkown@user.com";
  1586 //    msgDict[kPepUsername] = @"some unkown user";
  1587 //    // me
  1588 //    NSDictionary *accountDict = [self unarchiveDictionary:@"account_A3FC7F0A.ser"];
  1589 //
  1590 //    PEPInternalSession *someSession = [[PEPInternalSession alloc] initInternal];
  1591 //    //Some key
  1592 //    [self importBundledKey:@"5CB2C182.asc" intoSession:someSession];
  1593 //    // This is the secret key for test001@peptest.ch
  1594 //    [self importBundledKey:@"5CB2C182_sec.asc" intoSession:someSession];
  1595 //    someSession = nil;
  1596 //
  1597 //    dispatch_queue_t queue = dispatch_queue_create("Concurrent test queue",
  1598 //                                                   DISPATCH_QUEUE_CONCURRENT);
  1599 //    dispatch_group_t group = dispatch_group_create();
  1600 //    void (^decryptionBlock)(int) = ^(int index) {
  1601 //        PEPInternalSession *innerSession = [[PEPInternalSession alloc] initInternal];
  1602 //        NSMutableDictionary *innerAccountDict = [accountDict mutableCopy];
  1603 //        [innerSession mySelf:innerAccountDict]; //Random Assertion failed: (status == PEP_STATUS_OK), function _myself, file /Users/buff/workspace/pEp/src/pEpEngine/src/keymanagement.c, line 619.
  1604 //        XCTAssertNotNil(innerAccountDict[kPepFingerprint]);
  1605 //        NSArray* keys;
  1606 //        NSMutableDictionary *pepDecryptedMail;
  1607 //        [innerSession decryptMessageDict:msgDict dest:&pepDecryptedMail
  1608 //                                                       keys:&keys];
  1609 //        innerSession = nil;
  1610 //    };
  1611 //
  1612 //    decryptionBlock(0);
  1613 //
  1614 //    for (int i = 1; i < 15; ++i) {
  1615 //        dispatch_group_async(group, queue, ^{
  1616 //            decryptionBlock(i);
  1617 //        });
  1618 //    }
  1619 //
  1620 //    dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
  1621 //
  1622 //    [self pEpCleanUp];
  1623 //}
  1624 //
  1625 //- (void)testParallelDecryptionOneThreadOneSessionCopiedToBlock
  1626 //{
  1627 //    // Have one session open at all times, from main thread
  1628 //    [self pEpSetUp];
  1629 //
  1630 //    // An unecrypted Mail
  1631 //    NSMutableDictionary *msgDict = [[self unarchiveDictionary:@"msg_to_A3FC7F0A_from_mutt.ser"] mutableCopy];
  1632 //    msgDict[kPepAddress] = @"some.unkown@user.com";
  1633 //    msgDict[kPepUsername] = @"some unkown user";
  1634 //    // me
  1635 //    NSDictionary *accountDict = [self unarchiveDictionary:@"account_A3FC7F0A.ser"];
  1636 //
  1637 //    PEPInternalSession *someSession = [[PEPInternalSession alloc] initInternal];
  1638 //    //Some key
  1639 //    [self importBundledKey:@"5CB2C182.asc" intoSession:someSession];
  1640 //    // This is the secret key for test001@peptest.ch
  1641 //    [self importBundledKey:@"5CB2C182_sec.asc" intoSession:someSession];
  1642 //    someSession = nil;
  1643 //
  1644 //    dispatch_queue_t queue = dispatch_queue_create("Concurrent test queue",
  1645 //                                                   DISPATCH_QUEUE_CONCURRENT);
  1646 //    dispatch_group_t group = dispatch_group_create();
  1647 //    PEPInternalSession *oneSessionCopiedToBlock = [[PEPInternalSession alloc] initInternal];
  1648 //    void (^decryptionBlock)(int) = ^(int index) {
  1649 //        NSMutableDictionary *innerAccountDict = [accountDict mutableCopy];
  1650 //        [oneSessionCopiedToBlock mySelf:innerAccountDict];        XCTAssertNotNil(innerAccountDict[kPepFingerprint]);
  1651 //        NSArray* keys;
  1652 //        NSMutableDictionary *pepDecryptedMail;
  1653 //        [oneSessionCopiedToBlock decryptMessageDict:msgDict dest:&pepDecryptedMail
  1654 //                                                                  keys:&keys];
  1655 //    };
  1656 //    decryptionBlock(0);
  1657 //    for (int i = 1; i < 84; ++i) {
  1658 //        dispatch_group_async(group, queue, ^{
  1659 //            decryptionBlock(i);
  1660 //        });
  1661 //    }
  1662 //    dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
  1663 //
  1664 //    [self pEpCleanUp];
  1665 //}
  1666 //
  1667 //- (void)testParallelDecryptionOneThreadOneSessionBlockReference
  1668 //{
  1669 //    // Have one session open at all times, from main thread
  1670 //    [self pEpSetUp];
  1671 //
  1672 //    // An unecrypted Mail
  1673 //    NSMutableDictionary *msgDict = [[self unarchiveDictionary:@"msg_to_A3FC7F0A_from_mutt.ser"] mutableCopy];
  1674 //    msgDict[kPepAddress] = @"some.unkown@user.com";
  1675 //    msgDict[kPepUsername] = @"some unkown user";
  1676 //    // me
  1677 //    NSDictionary *accountDict = [self unarchiveDictionary:@"account_A3FC7F0A.ser"];
  1678 //
  1679 //    PEPInternalSession *someSession = [[PEPInternalSession alloc] initInternal];
  1680 //    //Some key
  1681 //    [self importBundledKey:@"5CB2C182.asc" intoSession:someSession];
  1682 //    // This is the secret key for test001@peptest.ch
  1683 //    [self importBundledKey:@"5CB2C182_sec.asc" intoSession:someSession];
  1684 //    someSession = nil;
  1685 //
  1686 //    dispatch_queue_t queue = dispatch_queue_create("Concurrent test queue",
  1687 //                                                   DISPATCH_QUEUE_CONCURRENT);
  1688 //    dispatch_group_t group = dispatch_group_create();
  1689 //    __block PEPInternalSession *oneSessionCopiedToBlock = [[PEPInternalSession alloc] initInternal];
  1690 //    void (^decryptionBlock)(int) = ^(int index) {
  1691 //        NSMutableDictionary *innerAccountDict = [accountDict mutableCopy];
  1692 //        [oneSessionCopiedToBlock mySelf:innerAccountDict];        XCTAssertNotNil(innerAccountDict[kPepFingerprint]);
  1693 //
  1694 //        NSArray* keys;
  1695 //        NSMutableDictionary *pepDecryptedMail;
  1696 //        [oneSessionCopiedToBlock decryptMessageDict:msgDict dest:&pepDecryptedMail
  1697 //                                                                  keys:&keys];
  1698 //    };
  1699 //
  1700 //    decryptionBlock(0);
  1701 //    for (int i = 1; i < 84; ++i) {
  1702 //        dispatch_group_async(group, queue, ^{
  1703 //            decryptionBlock(i);
  1704 //        });
  1705 //    }
  1706 //
  1707 //    dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
  1708 //
  1709 //    [self pEpCleanUp];
  1710 //}
  1711 //
  1712 //// IOSAD-34
  1713 //- (void)testParallelDecryptionPlusParallelInitOneThreadMultipleSessions
  1714 //{
  1715 //    // Have one session open at all times, from main thread
  1716 //    [self pEpSetUp];
  1717 //    // An unecrypted Mail
  1718 //    NSMutableDictionary *msgDict = [[self unarchiveDictionary:@"msg_to_A3FC7F0A_from_mutt.ser"] mutableCopy];
  1719 //    msgDict[kPepAddress] = @"some.unkown@user.com";
  1720 //    msgDict[kPepUsername] = @"some unkown user";
  1721 //    // me
  1722 //    NSDictionary *accountDict = [self unarchiveDictionary:@"account_A3FC7F0A.ser"];
  1723 //    PEPInternalSession *someSession = [[PEPInternalSession alloc] initInternal];
  1724 //    //Some key
  1725 //    [self importBundledKey:@"5CB2C182.asc" intoSession:someSession];
  1726 //    // This is the secret key for test001@peptest.ch
  1727 //    [self importBundledKey:@"5CB2C182_sec.asc" intoSession:someSession];
  1728 //    someSession = nil;
  1729 //
  1730 //    dispatch_queue_t queue = dispatch_queue_create("Concurrent test queue",
  1731 //                                                   DISPATCH_QUEUE_CONCURRENT);
  1732 //    __block dispatch_group_t group = dispatch_group_create();
  1733 //    PEPInternalSession *decryptSession = [[PEPInternalSession alloc] initInternal];
  1734 //    void (^decryptionBlock)(int) = ^(int index) {
  1735 //        NSMutableDictionary *innerAccountDict = [accountDict mutableCopy];
  1736 //        [decryptSession mySelf:innerAccountDict];         XCTAssertNotNil(innerAccountDict[kPepFingerprint]);
  1737 //        NSArray* keys;
  1738 //        NSMutableDictionary *pepDecryptedMail;
  1739 //        [decryptSession decryptMessageDict:msgDict dest:&pepDecryptedMail
  1740 //                                                         keys:&keys];
  1741 //    };
  1742 //
  1743 //    PEPInternalSession *decryptSession2 = [[PEPInternalSession alloc] initInternal];
  1744 //    void (^decryptionBlock2)(int) = ^(int index) {
  1745 //        NSMutableDictionary *innerAccountDict = [accountDict mutableCopy];
  1746 //        [decryptSession2 mySelf:innerAccountDict];         XCTAssertNotNil(innerAccountDict[kPepFingerprint]);
  1747 //        NSArray* keys;
  1748 //        NSMutableDictionary *pepDecryptedMail;
  1749 //        [decryptSession2 decryptMessageDict:msgDict dest:&pepDecryptedMail
  1750 //                                                          keys:&keys];
  1751 //    };
  1752 //
  1753 //    void (^initBlock)(void) = ^() {
  1754 //        for (int i = 0; i < 100; ++i) {
  1755 //            PEPInternalSession *tmp = [[PEPInternalSession alloc] initInternal];
  1756 //            tmp = nil;
  1757 //        }
  1758 //    };
  1759 //
  1760 //    for (int i = 1; i < 15; ++i) {
  1761 //        dispatch_group_async(group, queue, ^{
  1762 //            decryptionBlock(i);
  1763 //        });
  1764 //        dispatch_group_async(group, queue, ^{
  1765 //            decryptionBlock2(i);
  1766 //        });
  1767 //        dispatch_group_async(group, queue, ^{
  1768 //            initBlock();
  1769 //        });
  1770 //        dispatch_group_async(group, queue, ^{
  1771 //            initBlock();
  1772 //        });
  1773 //        dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
  1774 //    }
  1775 //
  1776 //    XCTAssertTrue(YES, @"We are done and did not crash.");
  1777 //    [self pEpCleanUp];
  1778 //}
  1779 
  1780 @end