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