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