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