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