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