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