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