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