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