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