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