pEpObjCTests/PEPSessionTest.m
author Dirk Zimmermann <dirk@pep-project.org>
Sat, 14 Apr 2018 14:19:03 +0200
changeset 573 bc2c03efe133
parent 567 b9439926c09f
child 574 03a019b2533b
permissions -rw-r--r--
IOSAD-72 outgoingRatingForMessage -> NSNumber
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
#import "PEPTestUtils.h"
andreas@392
    16
#import "PEPTestSyncDelegate.h"
andreas@392
    17
andreas@392
    18
@interface PEPSessionTest : XCTestCase
andreas@392
    19
@end
andreas@392
    20
andreas@392
    21
@implementation PEPSessionTest
andreas@392
    22
andreas@392
    23
- (void)setUp
andreas@392
    24
{
andreas@392
    25
    [super setUp];
andreas@416
    26
    [PEPObjCAdapter setUnecryptedSubjectEnabled:NO];
andreas@416
    27
andreas@392
    28
    [self pEpCleanUp];
andreas@392
    29
}
andreas@392
    30
dirk@525
    31
- (void)tearDown
dirk@525
    32
{
andreas@392
    33
    [self pEpCleanUp];
andreas@392
    34
    [super tearDown];
andreas@392
    35
}
andreas@392
    36
andreas@392
    37
- (void)testSyncSession
andreas@392
    38
{
andreas@392
    39
    PEPSession *session = [PEPSession new];
dirk@474
    40
dirk@474
    41
    // Dummy to set up the DB, since this is currenty only triggered by session use,
dirk@474
    42
    // which PEPObjCAdapter.startSync does not trigger.
dirk@552
    43
    NSError *error = nil;
dirk@552
    44
    XCTAssertNotNil([session getLogWithError:&error]);
dirk@552
    45
    XCTAssertNil(error);
dirk@474
    46
andreas@392
    47
    PEPTestSyncDelegate *syncDelegate = [[PEPTestSyncDelegate alloc] init];
andreas@392
    48
andreas@392
    49
    // This should attach session just created
andreas@392
    50
    [PEPObjCAdapter startSync:syncDelegate];
andreas@392
    51
andreas@392
    52
    PEPIdentity *identMe = [[PEPIdentity alloc]
andreas@392
    53
                            initWithAddress:@"pep.test.iosgenkey@pep-project.org"
andreas@392
    54
                            userID:@"Me"
andreas@392
    55
                            userName:@"pEp Test iOS GenKey"
andreas@392
    56
                            isOwn:YES];
andreas@392
    57
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@567
   104
    XCTAssertTrue([identMe isPEPUser:session error:&error]);
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@567
   124
    XCTAssertTrue([identMe isPEPUser:session error:&error]);
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@567
   143
        XCTAssertTrue([identMe2 isPEPUser:session error:&innerError]);
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@567
   153
        XCTAssertTrue([identMe3 isPEPUser:session error:&innerError]);
andreas@392
   154
        XCTAssertEqualObjects(identMe3.fingerPrint, identMe.fingerPrint);
andreas@392
   155
andreas@392
   156
        XCTAssertEqualObjects(identMe.address, identMe2.address);
andreas@392
   157
        XCTAssertEqualObjects(identMe.address, identMe3.address);
andreas@392
   158
        XCTAssertEqual(identMe.commType, identMe2.commType);
andreas@392
   159
        XCTAssertEqual(identMe.commType, identMe3.commType);
andreas@392
   160
    });
andreas@392
   161
}
andreas@392
   162
dirk@435
   163
- (void)testPartnerWithoutFingerPrint
dirk@435
   164
{
dirk@435
   165
    PEPSession *session = [PEPSession new];
dirk@435
   166
dirk@435
   167
    PEPIdentity *identRandom = [[PEPIdentity alloc]
dirk@435
   168
                                initWithAddress:@"does_not_exist@example.com"
dirk@435
   169
                                userID:@"got_out"
dirk@435
   170
                                userName:@"No Way Not Even Alice"
dirk@435
   171
                                isOwn:NO];
dirk@435
   172
dirk@532
   173
    NSError *error = nil;
dirk@532
   174
    XCTAssertTrue([session updateIdentity:identRandom error:&error]);
dirk@532
   175
    XCTAssertNil(error);
dirk@435
   176
    XCTAssertNil(identRandom.fingerPrint);
dirk@435
   177
}
dirk@435
   178
dirk@437
   179
- (void)testImportPartnerKeys
dirk@436
   180
{
dirk@488
   181
    XCTAssertNotNil([self checkImportingKeyFilePath:@"6FF00E97_sec.asc"
dirk@488
   182
                                            address:@"pep.test.alice@pep-project.org"
dirk@488
   183
                                             userID:@"This Is Alice"
dirk@488
   184
                                        fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
dirk@488
   185
                                            session: nil]);
dirk@436
   186
dirk@488
   187
    XCTAssertNotNil([self checkImportingKeyFilePath:@"0xC9C2EE39.asc"
dirk@488
   188
                                            address:@"pep.test.bob@pep-project.org"
dirk@488
   189
                                             userID:@"This Is Bob"
dirk@488
   190
                                        fingerPrint:@"BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39"
dirk@488
   191
                                            session: nil]);
dirk@436
   192
}
dirk@436
   193
dirk@439
   194
- (void)testIdentityRating
dirk@439
   195
{
dirk@439
   196
    PEPSession *session = [PEPSession new];
dirk@439
   197
dirk@439
   198
    PEPIdentity *me = [self
dirk@439
   199
                       checkMySelfImportingKeyFilePath:@"6FF00E97_sec.asc"
dirk@439
   200
                       address:@"pep.test.alice@pep-project.org"
dirk@439
   201
                       userID:@"Alice_User_ID"
dirk@491
   202
                       fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
dirk@491
   203
                       session:session];
dirk@529
   204
    XCTAssertEqual([self ratingForIdentity:me session:session], PEP_rating_trusted_and_anonymized);
dirk@439
   205
dirk@439
   206
    PEPIdentity *alice = [self
dirk@439
   207
                          checkImportingKeyFilePath:@"6FF00E97_sec.asc"
dirk@439
   208
                          address:@"pep.test.alice@pep-project.org"
dirk@439
   209
                          userID:@"This Is Alice"
dirk@456
   210
                          fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
dirk@456
   211
                          session: session];
dirk@488
   212
    XCTAssertNotNil(alice);
dirk@529
   213
    XCTAssertEqual([self ratingForIdentity:alice session:session], PEP_rating_reliable);
dirk@439
   214
}
dirk@439
   215
dirk@546
   216
/** ENGINE-409 */
dirk@546
   217
- (void)testIdentityRatingMistrustReset
dirk@546
   218
{
dirk@546
   219
    PEPSession *session = [PEPSession new];
dirk@546
   220
dirk@546
   221
    PEPIdentity *me = [[PEPIdentity alloc]
dirk@546
   222
                       initWithAddress:@"me@example.org"
dirk@546
   223
                       userID:@"me_myself"
dirk@546
   224
                       userName:@"Me Me"
dirk@546
   225
                       isOwn:YES];
dirk@546
   226
dirk@546
   227
    NSError *error = nil;
dirk@546
   228
    XCTAssertTrue([session mySelf:me error:&error]);
dirk@546
   229
    XCTAssertNil(error);
dirk@546
   230
dirk@546
   231
    XCTAssertNotNil(me.fingerPrint);
dirk@546
   232
    XCTAssertEqual([self ratingForIdentity:me session:session], PEP_rating_trusted_and_anonymized);
dirk@546
   233
dirk@546
   234
    PEPIdentity *alice = [self
dirk@546
   235
                          checkImportingKeyFilePath:@"6FF00E97_sec.asc"
dirk@546
   236
                          address:@"pep.test.alice@pep-project.org"
dirk@546
   237
                          userID:@"This Is Alice"
dirk@546
   238
                          fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
dirk@546
   239
                          session: session];
dirk@546
   240
    XCTAssertNotNil(alice);
dirk@546
   241
    XCTAssertEqual([self ratingForIdentity:alice session:session], PEP_rating_reliable);
dirk@546
   242
dirk@546
   243
    XCTAssertTrue([session keyMistrusted:alice error:&error]);
dirk@546
   244
    XCTAssertNil(error);
dirk@546
   245
    XCTAssertEqual([self ratingForIdentity:alice session:session], PEP_rating_have_no_key);
dirk@546
   246
dirk@554
   247
    XCTAssertTrue([session keyResetTrust:alice error:&error]);
dirk@554
   248
    XCTAssertNil(error);
dirk@554
   249
    XCTAssertEqual([self ratingForIdentity:alice session:session], PEP_rating_reliable);
dirk@546
   250
}
dirk@546
   251
dirk@446
   252
- (void)testIdentityRatingTrustResetMistrustUndo
dirk@445
   253
{
dirk@445
   254
    PEPSession *session = [PEPSession new];
dirk@445
   255
dirk@460
   256
    PEPIdentity *me = [[PEPIdentity alloc]
dirk@460
   257
                       initWithAddress:@"me@example.org"
dirk@460
   258
                       userID:@"me_myself"
dirk@460
   259
                       userName:@"Me Me"
dirk@460
   260
                       isOwn:YES];
dirk@532
   261
dirk@532
   262
    NSError *error = nil;
dirk@532
   263
    XCTAssertTrue([session mySelf:me error:&error]);
dirk@532
   264
    XCTAssertNil(error);
dirk@532
   265
dirk@460
   266
    XCTAssertNotNil(me.fingerPrint);
dirk@529
   267
    XCTAssertEqual([self ratingForIdentity:me session:session], PEP_rating_trusted_and_anonymized);
dirk@445
   268
dirk@445
   269
    PEPIdentity *alice = [self
dirk@445
   270
                          checkImportingKeyFilePath:@"6FF00E97_sec.asc"
dirk@445
   271
                          address:@"pep.test.alice@pep-project.org"
dirk@445
   272
                          userID:@"This Is Alice"
dirk@456
   273
                          fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
dirk@456
   274
                          session: session];
dirk@488
   275
    XCTAssertNotNil(alice);
dirk@529
   276
    XCTAssertEqual([self ratingForIdentity:alice session:session], PEP_rating_reliable);
dirk@445
   277
dirk@534
   278
    XCTAssertTrue([session trustPersonalKey:alice error:&error]);
dirk@534
   279
    XCTAssertNil(error);
dirk@529
   280
    XCTAssertEqual([self ratingForIdentity:alice session:session], PEP_rating_trusted);
dirk@446
   281
dirk@545
   282
    XCTAssertTrue([session keyResetTrust:alice error:&error]);
dirk@545
   283
    XCTAssertNil(error);
dirk@529
   284
    XCTAssertEqual([self ratingForIdentity:alice session:session], PEP_rating_reliable);
dirk@446
   285
dirk@536
   286
    XCTAssertTrue([session keyMistrusted:alice error:&error]);
dirk@536
   287
    XCTAssertNil(error);
dirk@529
   288
    XCTAssertEqual([self ratingForIdentity:alice session:session], PEP_rating_have_no_key);
dirk@445
   289
dirk@565
   290
    XCTAssertTrue([session undoLastMistrustWithError:&error]);
dirk@565
   291
    XCTAssertNil(error);
dirk@527
   292
dirk@527
   293
    // After ENGINE-371 has been fixed, this should be just PEP_rating_reliable
dirk@529
   294
    XCTAssertEqual([self ratingForIdentity:alice session:session], PEP_rating_trusted);
dirk@446
   295
dirk@534
   296
    XCTAssertTrue([session trustPersonalKey:alice error:&error]);
dirk@534
   297
    XCTAssertNil(error);
dirk@529
   298
    XCTAssertEqual([self ratingForIdentity:alice session:session], PEP_rating_trusted);
dirk@446
   299
dirk@545
   300
    XCTAssertTrue([session keyResetTrust:alice error:&error]);
dirk@545
   301
    XCTAssertNil(error);
dirk@554
   302
    XCTAssertEqual([self ratingForIdentity:alice session:session], PEP_rating_reliable);
dirk@445
   303
}
dirk@445
   304
dirk@478
   305
/** ENGINE-384 */
dirk@472
   306
- (void)testIdentityRatingCrash
dirk@472
   307
{
dirk@472
   308
    PEPSession *session = [PEPSession new];
dirk@472
   309
dirk@472
   310
    PEPIdentity *me = [[PEPIdentity alloc]
dirk@472
   311
                       initWithAddress:@"me@example.org"
dirk@472
   312
                       userID:@"me_myself"
dirk@472
   313
                       userName:@"Me Me"
dirk@472
   314
                       isOwn:YES];
dirk@532
   315
    NSError *error = nil;
dirk@532
   316
    XCTAssertTrue([session mySelf:me error:&error]);
dirk@532
   317
    XCTAssertNil(error);
dirk@532
   318
dirk@472
   319
    XCTAssertNotNil(me.fingerPrint);
dirk@529
   320
    XCTAssertEqual([self ratingForIdentity:me session:session], PEP_rating_trusted_and_anonymized);
dirk@472
   321
dirk@472
   322
    PEPIdentity *alice = [self
dirk@472
   323
                          checkImportingKeyFilePath:@"6FF00E97_sec.asc"
dirk@472
   324
                          address:@"pep.test.alice@pep-project.org"
dirk@472
   325
                          userID:@"This Is Alice"
dirk@472
   326
                          fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
dirk@472
   327
                          session: session];
dirk@488
   328
    XCTAssertNotNil(alice);
dirk@529
   329
    XCTAssertEqual([self ratingForIdentity:alice session:session], PEP_rating_reliable);
dirk@472
   330
dirk@534
   331
    XCTAssertTrue([session trustPersonalKey:alice error:&error]);
dirk@534
   332
    XCTAssertNil(error);
dirk@529
   333
    XCTAssertEqual([self ratingForIdentity:alice session:session], PEP_rating_trusted);
dirk@472
   334
dirk@545
   335
    XCTAssertTrue([session keyResetTrust:alice error:&error]);
dirk@545
   336
    XCTAssertNil(error);
dirk@529
   337
    XCTAssertEqual([self ratingForIdentity:alice session:session], PEP_rating_reliable);
dirk@472
   338
dirk@536
   339
    XCTAssertTrue([session keyMistrusted:alice error:&error]);
dirk@536
   340
    XCTAssertNil(error);
dirk@529
   341
    XCTAssertEqual([self ratingForIdentity:alice session:session], PEP_rating_have_no_key);
dirk@472
   342
dirk@565
   343
    XCTAssertTrue([session undoLastMistrustWithError:&error]);
dirk@565
   344
    XCTAssertNil(error);
dirk@472
   345
dirk@534
   346
    XCTAssertTrue([session trustPersonalKey:alice error:&error]);
dirk@534
   347
    XCTAssertNil(error);
dirk@529
   348
    XCTAssertEqual([self ratingForIdentity:alice session:session], PEP_rating_trusted);
dirk@472
   349
dirk@545
   350
    XCTAssertTrue([session keyResetTrust:alice error:&error]);
dirk@545
   351
    XCTAssertNil(error);
dirk@554
   352
    XCTAssertEqual([self ratingForIdentity:alice session:session], PEP_rating_reliable);
dirk@472
   353
dirk@478
   354
    // This line provoked the crash
dirk@534
   355
    XCTAssertTrue([session trustPersonalKey:alice error:&error]);
dirk@534
   356
    XCTAssertNil(error);
dirk@472
   357
}
dirk@472
   358
dirk@458
   359
/**
dirk@458
   360
 Try to provoke a SQLITE_BUSY (ENGINE-374)
dirk@458
   361
 */
dirk@458
   362
- (void)testIdentityRatingTrustResetMistrustUndoBusy
dirk@458
   363
{
dirk@458
   364
    PEPSession *session = [PEPSession new];
dirk@458
   365
dirk@459
   366
    PEPIdentity *me = [[PEPIdentity alloc]
dirk@459
   367
                       initWithAddress:@"me@example.org"
dirk@459
   368
                       userID:@"me_myself"
dirk@459
   369
                       userName:@"Me Me"
dirk@459
   370
                       isOwn:YES];
dirk@532
   371
    NSError *error = nil;
dirk@532
   372
    XCTAssertTrue([session mySelf:me error:&error]);
dirk@532
   373
    XCTAssertNil(error);
dirk@532
   374
dirk@459
   375
    XCTAssertNotNil(me.fingerPrint);
dirk@529
   376
    XCTAssertEqual([self ratingForIdentity:me session:session], PEP_rating_trusted_and_anonymized);
dirk@458
   377
dirk@458
   378
    PEPIdentity *alice = [self
dirk@458
   379
                          checkImportingKeyFilePath:@"6FF00E97_sec.asc"
dirk@458
   380
                          address:@"pep.test.alice@pep-project.org"
dirk@458
   381
                          userID:@"This Is Alice"
dirk@458
   382
                          fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
dirk@458
   383
                          session: session];
dirk@488
   384
    XCTAssertNotNil(alice);
dirk@529
   385
    XCTAssertEqual([self ratingForIdentity:alice session:session], PEP_rating_reliable);
dirk@458
   386
dirk@458
   387
    void (^encryptingBlock)(void) = ^{
dirk@458
   388
        PEPSession *innerSession = [PEPSession new];
dirk@458
   389
        PEPMessage *msg = [PEPMessage new];
dirk@458
   390
        msg.from = me;
dirk@458
   391
        msg.to = @[alice];
dirk@458
   392
        msg.shortMessage = @"The subject";
dirk@458
   393
        msg.longMessage = @"Lots and lots of text";
dirk@476
   394
        msg.direction = PEP_dir_outgoing;
dirk@526
   395
dirk@526
   396
        PEP_STATUS status;
dirk@526
   397
        NSError *error = nil;
dirk@526
   398
        PEPMessage *encMsg = [innerSession
dirk@526
   399
                              encryptMessage:msg
dirk@557
   400
                              forSelf:me
dirk@556
   401
                              extraKeys:nil
dirk@526
   402
                              status:&status error:&error];
dirk@458
   403
        XCTAssertEqual(status, PEP_STATUS_OK);
dirk@526
   404
        XCTAssertNotNil(encMsg);
dirk@458
   405
    };
dirk@458
   406
dirk@458
   407
    dispatch_group_t backgroundGroup = dispatch_group_create();
dirk@458
   408
    dispatch_group_async(backgroundGroup,
dirk@458
   409
                         dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), encryptingBlock);
dirk@458
   410
dirk@534
   411
    XCTAssertTrue([session trustPersonalKey:alice error:&error]);
dirk@534
   412
    XCTAssertNil(error);
dirk@529
   413
    XCTAssertEqual([self ratingForIdentity:alice session:session], PEP_rating_trusted);
dirk@458
   414
dirk@545
   415
    XCTAssertTrue([session keyResetTrust:alice error:&error]);
dirk@545
   416
    XCTAssertNil(error);
dirk@529
   417
    XCTAssertEqual([self ratingForIdentity:alice session:session], PEP_rating_reliable);
dirk@458
   418
dirk@536
   419
    XCTAssertTrue([session keyMistrusted:alice error:&error]);
dirk@536
   420
    XCTAssertNil(error);
dirk@529
   421
    XCTAssertEqual([self ratingForIdentity:alice session:session], PEP_rating_have_no_key);
dirk@458
   422
dirk@565
   423
    XCTAssertTrue([session undoLastMistrustWithError:&error]);
dirk@565
   424
    XCTAssertNil(error);
dirk@527
   425
dirk@527
   426
    // After ENGINE-371 has been fixed, this should be just PEP_rating_reliable
dirk@529
   427
    XCTAssertEqual([self ratingForIdentity:alice session:session], PEP_rating_trusted);
dirk@458
   428
dirk@534
   429
    XCTAssertTrue([session trustPersonalKey:alice error:&error]);
dirk@534
   430
    XCTAssertNil(error);
dirk@529
   431
    XCTAssertEqual([self ratingForIdentity:alice session:session], PEP_rating_trusted);
dirk@458
   432
dirk@545
   433
    XCTAssertTrue([session keyResetTrust:alice error:&error]);
dirk@545
   434
    XCTAssertNil(error);
dirk@554
   435
    XCTAssertEqual([self ratingForIdentity:alice session:session], PEP_rating_reliable);
dirk@458
   436
dirk@458
   437
    dispatch_group_wait(backgroundGroup, DISPATCH_TIME_FOREVER);
dirk@458
   438
}
dirk@458
   439
andreas@392
   440
- (void)testOutgoingColors
andreas@392
   441
{
andreas@392
   442
    PEPSession *session = [PEPSession new];
andreas@392
   443
andreas@392
   444
    // Our test user :
andreas@392
   445
    // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
andreas@392
   446
    // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
dirk@488
   447
    XCTAssertTrue([PEPTestUtils importBundledKey:@"6FF00E97_sec.asc" session:session]);
andreas@392
   448
andreas@392
   449
    // Our test user :
dirk@440
   450
    PEPIdentity *identAlice = [self
dirk@440
   451
                               checkMySelfImportingKeyFilePath:@"6FF00E97_sec.asc"
dirk@440
   452
                               address:@"pep.test.alice@pep-project.org"
dirk@440
   453
                               userID:@"Alice_User_ID"
dirk@491
   454
                               fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
dirk@491
   455
                               session:session];
andreas@392
   456
andreas@392
   457
    //Message
andreas@392
   458
dirk@441
   459
    {
dirk@441
   460
        PEPIdentity *identUnknownBob = [[PEPIdentity alloc]
dirk@441
   461
                                        initWithAddress:@"pep.test.unknown.bob@pep-project.org"
dirk@441
   462
                                        userID:@"4242" userName:@"pEp Test Bob Unknown"
dirk@441
   463
                                        isOwn:NO];
dirk@441
   464
dirk@441
   465
        PEPMessage *msgGray = [PEPMessage new];
dirk@441
   466
        msgGray.from = identAlice;
dirk@441
   467
        msgGray.to = @[identUnknownBob];
dirk@441
   468
        msgGray.shortMessage = @"All Gray Test";
dirk@441
   469
        msgGray.longMessage = @"This is a text content";
dirk@441
   470
        msgGray.direction = PEP_dir_outgoing;
dirk@441
   471
dirk@528
   472
        NSError *error = nil;
dirk@528
   473
dirk@441
   474
        // Test with unknown Bob
dirk@573
   475
        NSNumber *numRating = [session outgoingRatingForMessage:msgGray error:&error];
dirk@573
   476
        XCTAssertNotNil(numRating);
dirk@573
   477
        XCTAssertNil(error);
dirk@573
   478
        XCTAssertEqual(numRating.pEpRating, PEP_rating_unencrypted);
dirk@441
   479
    }
dirk@441
   480
dirk@441
   481
    PEPIdentity *identBob = [self
dirk@441
   482
                             checkImportingKeyFilePath:@"0xC9C2EE39.asc"
dirk@441
   483
                             address:@"pep.test.bob@pep-project.org"
dirk@441
   484
                             userID:@"42"
dirk@456
   485
                             fingerPrint:@"BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39"
dirk@456
   486
                             session: session];
dirk@488
   487
    XCTAssertNotNil(identBob);
dirk@438
   488
andreas@392
   489
    PEPMessage *msg = [PEPMessage new];
andreas@392
   490
    msg.from = identAlice;
dirk@438
   491
    msg.to = @[identBob];
dirk@441
   492
    msg.shortMessage = @"All Gray Test";
andreas@392
   493
    msg.longMessage = @"This is a text content";
andreas@392
   494
    msg.direction = PEP_dir_outgoing;
andreas@392
   495
dirk@528
   496
    NSError *error = nil;
dirk@528
   497
dirk@441
   498
    // Should be yellow, since no handshake happened.
dirk@573
   499
    NSNumber *numRating = [session outgoingRatingForMessage:msg error:&error];
dirk@573
   500
    XCTAssertNotNil(numRating);
dirk@528
   501
    XCTAssertNil(error);
dirk@573
   502
    XCTAssertEqual(numRating.pEpRating, PEP_rating_reliable);
andreas@392
   503
dirk@573
   504
    PEP_rating rating = [self ratingForIdentity:identBob session:session];
dirk@528
   505
    XCTAssertEqual(rating, PEP_rating_reliable);
andreas@392
   506
andreas@392
   507
    // Let' say we got that handshake, set PEP_ct_confirmed in Bob's identity
dirk@534
   508
    XCTAssertTrue([session trustPersonalKey:identBob error:&error]);
dirk@534
   509
    XCTAssertNil(error);
andreas@392
   510
andreas@392
   511
    // This time it should be green
dirk@573
   512
    numRating = [session outgoingRatingForMessage:msg error:&error];
dirk@573
   513
    XCTAssertNotNil(numRating);
dirk@573
   514
    XCTAssertNil(error);
dirk@573
   515
    XCTAssertEqual(numRating.pEpRating, PEP_rating_trusted);
andreas@392
   516
dirk@529
   517
    rating = [self ratingForIdentity:identBob session:session];
dirk@528
   518
    XCTAssertEqual(rating, PEP_rating_trusted);
andreas@392
   519
andreas@392
   520
    // Let' say we undo handshake
dirk@545
   521
    XCTAssertTrue([session keyResetTrust:identBob error:&error]);
dirk@545
   522
    XCTAssertNil(error);
andreas@392
   523
andreas@392
   524
    // Yellow ?
dirk@573
   525
    numRating = [session outgoingRatingForMessage:msg error:&error];
dirk@573
   526
    XCTAssertNotNil(numRating);
dirk@573
   527
    XCTAssertNil(error);
dirk@573
   528
    XCTAssertEqual(numRating.pEpRating, PEP_rating_reliable);
andreas@392
   529
andreas@392
   530
    // mistrust Bob
dirk@536
   531
    XCTAssertTrue([session keyMistrusted:identBob error:&error]);
dirk@536
   532
    XCTAssertNil(error);
andreas@392
   533
dirk@444
   534
    identBob.fingerPrint = nil;
dirk@532
   535
    XCTAssertTrue([session updateIdentity:identBob error:&error]);
dirk@532
   536
    XCTAssertNil(error);
dirk@441
   537
    XCTAssertNil(identBob.fingerPrint);
dirk@441
   538
andreas@392
   539
    // Gray == PEP_rating_unencrypted
dirk@573
   540
    numRating = [session outgoingRatingForMessage:msg error:&error];
dirk@573
   541
    XCTAssertNotNil(numRating);
dirk@573
   542
    XCTAssertNil(error);
dirk@573
   543
    XCTAssertEqual(numRating.pEpRating, PEP_rating_unencrypted);
andreas@392
   544
dirk@444
   545
    // Undo
dirk@565
   546
    XCTAssertTrue([session undoLastMistrustWithError:&error]);
dirk@565
   547
    XCTAssertNil(error);
dirk@532
   548
    XCTAssertTrue([session updateIdentity:identBob error:&error]);
dirk@532
   549
    XCTAssertNil(error);
dirk@444
   550
    XCTAssertNotNil(identBob.fingerPrint);
andreas@392
   551
andreas@392
   552
    // Back to yellow
dirk@573
   553
    numRating = [session outgoingRatingForMessage:msg error:&error];
dirk@573
   554
    XCTAssertNotNil(numRating);
dirk@573
   555
    XCTAssertNil(error);
dirk@527
   556
dirk@527
   557
    // After ENGINE-371 has been fixed, this should be just PEP_rating_reliable
dirk@573
   558
    XCTAssertEqual(numRating.pEpRating, PEP_rating_trusted);
dirk@529
   559
    XCTAssertEqual([self ratingForIdentity:identBob session:session], PEP_rating_trusted);
andreas@392
   560
andreas@392
   561
    // Trust again
dirk@534
   562
    XCTAssertTrue([session trustPersonalKey:identBob error:&error]);
dirk@534
   563
    XCTAssertNil(error);
andreas@392
   564
andreas@392
   565
    // Back to green
dirk@573
   566
    numRating = [session outgoingRatingForMessage:msg error:&error];
dirk@573
   567
    XCTAssertNotNil(numRating);
dirk@573
   568
    XCTAssertNil(error);
dirk@573
   569
    XCTAssertEqual(numRating.pEpRating, PEP_rating_trusted);
andreas@392
   570
andreas@392
   571
    // Now let see if it turns back yellow if we add an unconfirmed folk.
andreas@392
   572
    // pEp Test John (test key, don't use) <pep.test.john@pep-project.org>
andreas@392
   573
    // AA2E4BEB93E5FE33DEFD8BE1135CD6D170DCF575
dirk@488
   574
    XCTAssertTrue([PEPTestUtils importBundledKey:@"0x70DCF575.asc" session:session]);
andreas@392
   575
andreas@392
   576
    PEPIdentity *identJohn = [[PEPIdentity alloc]
andreas@392
   577
                              initWithAddress:@"pep.test.john@pep-project.org"
andreas@392
   578
                              userID:@"101" userName:@"pEp Test John"
andreas@392
   579
                              isOwn:NO
andreas@392
   580
                              fingerPrint:@"AA2E4BEB93E5FE33DEFD8BE1135CD6D170DCF575"];
andreas@392
   581
dirk@532
   582
    XCTAssertTrue([session updateIdentity:identJohn error:&error]);
dirk@532
   583
    XCTAssertNil(error);
andreas@392
   584
andreas@411
   585
    msg.cc = @[[PEPTestUtils foreignPepIdentityWithAddress:@"pep.test.john@pep-project.org"
andreas@411
   586
                                                  userName:@"pEp Test John"]];
andreas@392
   587
    // Yellow ?
dirk@573
   588
    numRating = [session outgoingRatingForMessage:msg error:&error];
dirk@573
   589
    XCTAssertNotNil(numRating);
dirk@573
   590
    XCTAssertNil(error);
dirk@573
   591
    XCTAssertEqual(numRating.pEpRating, PEP_rating_reliable);
andreas@392
   592
dirk@524
   593
    PEPMessage *encMsg = [session encryptMessage:msg extraKeys:nil status:nil error:&error];
dirk@522
   594
    XCTAssertNotNil(encMsg);
dirk@522
   595
    XCTAssertNil(error);
andreas@392
   596
dirk@522
   597
    XCTAssertEqualObjects(encMsg.shortMessage, @"p≡p");
dirk@522
   598
    XCTAssertTrue([encMsg.longMessage containsString:@"p≡p"]);
andreas@392
   599
}
andreas@392
   600
andreas@392
   601
andreas@392
   602
- (void)testOutgoingBccColors
andreas@392
   603
{
andreas@392
   604
    PEPSession *session = [PEPSession new];
andreas@392
   605
andreas@392
   606
    // Our test user :
andreas@392
   607
    // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
andreas@392
   608
    // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
dirk@488
   609
    XCTAssertTrue([PEPTestUtils importBundledKey:@"6FF00E97_sec.asc" session:session]);
andreas@392
   610
andreas@392
   611
    PEPIdentity *identAlice = [[PEPIdentity alloc]
andreas@392
   612
                               initWithAddress:@"pep.test.alice@pep-project.org"
andreas@392
   613
                               userID:ownUserId
andreas@392
   614
                               userName:@"pEp Test Alice"
andreas@392
   615
                               isOwn:YES
andreas@392
   616
                               fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
andreas@392
   617
dirk@532
   618
    NSError *error = nil;
dirk@532
   619
    XCTAssertTrue([session mySelf:identAlice error:&error]);
dirk@532
   620
    XCTAssertNil(error);
andreas@392
   621
andreas@392
   622
    PEPMessage *msg = [PEPMessage new];
andreas@392
   623
    msg.from = identAlice;
andreas@392
   624
    msg.to = @[[[PEPIdentity alloc] initWithAddress:@"pep.test.bob@pep-project.org"
andreas@392
   625
                                             userID:@"42" userName:@"pEp Test Bob" isOwn:NO]];
andreas@392
   626
    msg.shortMessage = @"All Green Test";
andreas@392
   627
    msg.longMessage = @"This is a text content";
andreas@392
   628
    msg.direction = PEP_dir_outgoing;
andreas@392
   629
andreas@392
   630
    // Test with unknown Bob
dirk@528
   631
    PEP_rating rating;
dirk@573
   632
    NSNumber *numRating = [session outgoingRatingForMessage:msg error:&error];
dirk@573
   633
    XCTAssertNotNil(numRating);
dirk@573
   634
    XCTAssertNil(error);
dirk@573
   635
    XCTAssertEqual(numRating.pEpRating, PEP_rating_unencrypted);
andreas@392
   636
andreas@392
   637
    // Now let see with bob's pubkey already known
andreas@392
   638
    // pEp Test Bob (test key, don't use) <pep.test.bob@pep-project.org>
andreas@392
   639
    // BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39
dirk@488
   640
    XCTAssertTrue([PEPTestUtils importBundledKey:@"0xC9C2EE39.asc" session:session]);
andreas@392
   641
andreas@392
   642
    PEPIdentity *identBob = [[PEPIdentity alloc]
andreas@392
   643
                             initWithAddress:@"pep.test.bob@pep-project.org"
andreas@392
   644
                             userID:@"42" userName:@"pEp Test Bob"
andreas@392
   645
                             isOwn:NO
andreas@392
   646
                             fingerPrint:@"BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39"];
andreas@392
   647
dirk@532
   648
    XCTAssertTrue([session updateIdentity:identBob error:&error]);
dirk@532
   649
    XCTAssertNil(error);
andreas@392
   650
andreas@392
   651
    // Should be yellow, since no handshake happened.
dirk@573
   652
    numRating = [session outgoingRatingForMessage:msg error:&error];
dirk@573
   653
    XCTAssertNotNil(numRating);
dirk@573
   654
    XCTAssertNil(error);
dirk@573
   655
    XCTAssertEqual(numRating.pEpRating, PEP_rating_reliable);
andreas@392
   656
dirk@529
   657
    rating = [self ratingForIdentity:identBob session:session];
dirk@528
   658
    XCTAssertEqual(rating, PEP_rating_reliable);
andreas@392
   659
andreas@392
   660
    // Let' say we got that handshake, set PEP_ct_confirmed in Bob's identity
dirk@534
   661
    XCTAssertTrue([session trustPersonalKey:identBob error:&error]);
dirk@534
   662
    XCTAssertNil(error);
andreas@392
   663
andreas@392
   664
    // This time it should be green
dirk@573
   665
    numRating = [session outgoingRatingForMessage:msg error:&error];
dirk@573
   666
    XCTAssertNotNil(numRating);
dirk@573
   667
    XCTAssertNil(error);
dirk@573
   668
    XCTAssertEqual(numRating.pEpRating, PEP_rating_trusted);
andreas@392
   669
dirk@529
   670
    rating = [self ratingForIdentity:identBob session:session];
dirk@528
   671
    XCTAssertEqual(rating, PEP_rating_trusted);
andreas@392
   672
andreas@392
   673
    // Now let see if it turns back yellow if we add an unconfirmed folk.
andreas@392
   674
    // pEp Test John (test key, don't use) <pep.test.john@pep-project.org>
andreas@392
   675
    // AA2E4BEB93E5FE33DEFD8BE1135CD6D170DCF575
dirk@488
   676
    XCTAssertTrue([PEPTestUtils importBundledKey:@"0x70DCF575.asc" session:session]);
andreas@392
   677
andreas@392
   678
    PEPIdentity *identJohn = [[PEPIdentity alloc]
andreas@392
   679
                              initWithAddress:@"pep.test.john@pep-project.org"
andreas@392
   680
                              userID:@"101" userName:@"pEp Test John"
andreas@392
   681
                              isOwn:NO
andreas@392
   682
                              fingerPrint:@"AA2E4BEB93E5FE33DEFD8BE1135CD6D170DCF575"];
andreas@392
   683
dirk@532
   684
    XCTAssertTrue([session updateIdentity:identJohn error:&error]);
dirk@532
   685
    XCTAssertNil(error);
andreas@392
   686
andreas@392
   687
    msg.bcc = @[[[PEPIdentity alloc] initWithAddress:@"pep.test.john@pep-project.org"
andreas@392
   688
                                              userID:@"101" userName:@"pEp Test John" isOwn:NO]];
andreas@392
   689
andreas@392
   690
    // Yellow ?
dirk@573
   691
    numRating = [session outgoingRatingForMessage:msg error:&error];
dirk@573
   692
    XCTAssertNotNil(numRating);
dirk@573
   693
    XCTAssertNil(error);
dirk@573
   694
    XCTAssertEqual(numRating.pEpRating, PEP_rating_reliable);
andreas@392
   695
dirk@534
   696
    XCTAssertTrue([session trustPersonalKey:identJohn error:&error]);
dirk@534
   697
    XCTAssertNil(error);
andreas@392
   698
andreas@392
   699
    // This time it should be green
dirk@573
   700
    numRating = [session outgoingRatingForMessage:msg error:&error];
dirk@573
   701
    XCTAssertNotNil(numRating);
dirk@573
   702
    XCTAssertNil(error);
dirk@573
   703
    XCTAssertEqual(numRating.pEpRating, PEP_rating_trusted);
andreas@392
   704
dirk@529
   705
    rating = [self ratingForIdentity:identJohn session:session];
dirk@528
   706
    XCTAssertEqual(rating, PEP_rating_trusted);
andreas@392
   707
}
andreas@392
   708
andreas@392
   709
- (void)testDontEncryptForMistrusted
andreas@392
   710
{
andreas@392
   711
    PEPSession *session = [PEPSession new];
andreas@392
   712
andreas@392
   713
    // Our test user :
andreas@392
   714
    // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
andreas@392
   715
    // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
dirk@488
   716
    XCTAssertTrue([PEPTestUtils importBundledKey:@"6FF00E97_sec.asc" session:session]);
andreas@392
   717
andreas@392
   718
    PEPIdentity *identAlice = [[PEPIdentity alloc]
andreas@392
   719
                               initWithAddress:@"pep.test.alice@pep-project.org"
andreas@392
   720
                               userID:ownUserId
andreas@392
   721
                               userName:@"pEp Test Alice"
andreas@392
   722
                               isOwn:YES
andreas@392
   723
                               fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
andreas@392
   724
dirk@532
   725
    NSError *error = nil;
dirk@532
   726
    XCTAssertTrue([session mySelf:identAlice error:&error]);
dirk@532
   727
    XCTAssertNil(error);
andreas@392
   728
andreas@392
   729
    // pEp Test Bob (test key, don't use) <pep.test.bob@pep-project.org>
andreas@392
   730
    // BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39
dirk@488
   731
    XCTAssertTrue([PEPTestUtils importBundledKey:@"0xC9C2EE39.asc" session:session]);
andreas@392
   732
andreas@392
   733
    PEPIdentity *identBob = [[PEPIdentity alloc]
andreas@392
   734
                             initWithAddress:@"pep.test.bob@pep-project.org"
andreas@392
   735
                             userID:@"42" userName:@"pEp Test Bob"
andreas@392
   736
                             isOwn:NO
andreas@392
   737
                             fingerPrint:@"BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39"];
andreas@392
   738
dirk@532
   739
    XCTAssertTrue([session updateIdentity:identBob error:&error]);
dirk@532
   740
    XCTAssertNil(error);
andreas@392
   741
andreas@392
   742
    // mistrust Bob
dirk@536
   743
    XCTAssertTrue([session keyMistrusted:identBob error:&error]);
dirk@536
   744
    XCTAssertNil(error);
andreas@392
   745
andreas@392
   746
    PEPMessage *msg = [PEPMessage new];
andreas@392
   747
    msg.from = identAlice;
andreas@392
   748
    msg.to = @[[[PEPIdentity alloc] initWithAddress:@"pep.test.bob@pep-project.org" userID:@"42"
andreas@392
   749
                                           userName:@"pEp Test Bob" isOwn:NO]];
andreas@392
   750
    msg.shortMessage = @"All Green Test";
andreas@392
   751
    msg.longMessage = @"This is a text content";
andreas@392
   752
    msg.direction = PEP_dir_outgoing;
andreas@392
   753
andreas@392
   754
    // Gray == PEP_rating_unencrypted
dirk@573
   755
    NSNumber *numRating = [session outgoingRatingForMessage:msg error:&error];
dirk@573
   756
    XCTAssertNotNil(numRating);
dirk@573
   757
    XCTAssertNil(error);
dirk@573
   758
    XCTAssertEqual(numRating.pEpRating, PEP_rating_unencrypted);
andreas@392
   759
dirk@524
   760
    PEPMessage *encMsg = [session encryptMessage:msg extraKeys:nil status:nil error:&error];
dirk@522
   761
    XCTAssertNotNil(encMsg);
dirk@522
   762
    XCTAssertNil(error);
andreas@392
   763
dirk@522
   764
    XCTAssertNotEqualObjects(encMsg.attachments[0][@"mimeType"], @"application/pgp-encrypted");
andreas@392
   765
andreas@392
   766
    [self pEpCleanUp];
andreas@392
   767
}
andreas@392
   768
andreas@392
   769
- (void)testRevoke
andreas@392
   770
{
andreas@392
   771
    PEPSession *session = [PEPSession new];
andreas@392
   772
andreas@392
   773
    // Our test user :
andreas@392
   774
    // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
andreas@392
   775
    // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
dirk@488
   776
    XCTAssertTrue([PEPTestUtils importBundledKey:@"6FF00E97_sec.asc" session:session]);
andreas@392
   777
    NSString *fpr = @"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97";
andreas@392
   778
andreas@392
   779
    PEPIdentity *identAlice = [[PEPIdentity alloc]
andreas@392
   780
                               initWithAddress:@"pep.test.alice@pep-project.org"
andreas@392
   781
                               userID:ownUserId
andreas@392
   782
                               userName:@"pEp Test Alice"
andreas@392
   783
                               isOwn:YES
andreas@392
   784
                               fingerPrint:fpr];
andreas@392
   785
dirk@532
   786
    NSError *error = nil;
dirk@532
   787
    XCTAssertTrue([session mySelf:identAlice error:&error]);
dirk@532
   788
    XCTAssertNil(error);
andreas@392
   789
andreas@392
   790
    PEPIdentity *identAlice2 = [identAlice mutableCopy];
andreas@392
   791
andreas@392
   792
    // This will revoke key
dirk@536
   793
    XCTAssertTrue([session keyMistrusted:identAlice2 error:&error]);
dirk@536
   794
    XCTAssertNil(error);
andreas@416
   795
    identAlice2.fingerPrint = nil;
dirk@532
   796
dirk@532
   797
    XCTAssertTrue([session mySelf:identAlice error:&error]);
dirk@532
   798
    XCTAssertNil(error);
andreas@416
   799
andreas@392
   800
    // Check fingerprint is different
andreas@392
   801
    XCTAssertNotEqualObjects(identAlice2.fingerPrint, fpr);
andreas@392
   802
}
andreas@392
   803
andreas@392
   804
- (void)testMailToMyself
andreas@392
   805
{
andreas@392
   806
    PEPSession *session = [PEPSession new];
andreas@392
   807
andreas@392
   808
    // Our test user :
andreas@392
   809
    // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
andreas@392
   810
    // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
dirk@488
   811
    XCTAssertTrue([PEPTestUtils importBundledKey:@"6FF00E97_sec.asc" session:session]);
andreas@392
   812
andreas@392
   813
    PEPIdentity *identAlice = [[PEPIdentity alloc]
andreas@392
   814
                               initWithAddress:@"pep.test.alice@pep-project.org"
andreas@392
   815
                               userID:ownUserId
andreas@392
   816
                               userName:@"pEp Test Alice"
andreas@392
   817
                               isOwn:YES
andreas@392
   818
                               fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
andreas@392
   819
dirk@532
   820
    NSError *error = nil;
dirk@532
   821
    XCTAssertTrue([session mySelf:identAlice error:&error]);
dirk@532
   822
    XCTAssertNil(error);
andreas@392
   823
andreas@392
   824
    PEPMessage *msg = [PEPMessage new];
andreas@392
   825
    msg.from = identAlice;
andreas@392
   826
    msg.to = @[identAlice];
andreas@392
   827
    msg.shortMessage = @"Mail to Myself";
andreas@392
   828
    msg.longMessage = @"This is a text content";
andreas@392
   829
    msg.direction = PEP_dir_outgoing;
andreas@392
   830
dirk@573
   831
    NSNumber *numRating = [session outgoingRatingForMessage:msg error:&error];
dirk@573
   832
    XCTAssertNotNil(numRating);
dirk@573
   833
    XCTAssertNil(error);
dirk@573
   834
    XCTAssertEqual(numRating.pEpRating, PEP_rating_trusted_and_anonymized);
dirk@528
   835
dirk@524
   836
    PEPMessage *encMsg = [session encryptMessage:msg extraKeys:nil status:nil error:&error];
dirk@522
   837
    XCTAssertNotNil(encMsg);
dirk@522
   838
    XCTAssertNil(error);
andreas@392
   839
dirk@522
   840
    NSArray *keys;
andreas@392
   841
dirk@522
   842
    error = nil;
dirk@573
   843
    PEP_rating rating = PEP_rating_b0rken;
dirk@517
   844
    PEPMessage *decmsg = [session
dirk@522
   845
                          decryptMessage:encMsg
dirk@528
   846
                          rating:&rating
dirk@523
   847
                          extraKeys:&keys
dirk@524
   848
                          status:nil
dirk@517
   849
                          error:&error];
dirk@517
   850
    XCTAssertNotNil(decmsg);
dirk@514
   851
    XCTAssertNil(error);
dirk@528
   852
    XCTAssertEqual(rating, PEP_rating_trusted_and_anonymized);
andreas@392
   853
}
andreas@392
   854
andreas@392
   855
- (void)testEncryptedMailFromMutt
andreas@392
   856
{
andreas@392
   857
    PEPSession *session = [PEPSession new];
andreas@392
   858
andreas@392
   859
    // This is the public key for test001@peptest.ch
dirk@488
   860
    XCTAssertTrue([PEPTestUtils importBundledKey:@"A3FC7F0A.asc" session:session]);
andreas@392
   861
andreas@392
   862
    // This is the secret key for test001@peptest.ch
dirk@488
   863
    XCTAssertTrue([PEPTestUtils importBundledKey:@"A3FC7F0A_sec.asc" session:session]);
andreas@392
   864
andreas@392
   865
    // Mail from mutt, already processed into message dict by the app.
dirk@465
   866
    NSMutableDictionary *msgDict = [[PEPTestUtils
dirk@465
   867
                                     unarchiveDictionary:@"msg_to_A3FC7F0A_from_mutt.ser"]
dirk@465
   868
                                    mutableCopy];
andreas@392
   869
    [msgDict removeObjectForKey:kPepLongMessage];
andreas@392
   870
    [msgDict removeObjectForKey:kPepLongMessageFormatted];
andreas@392
   871
andreas@392
   872
    // Also extracted "live" from the app.
dirk@465
   873
    NSMutableDictionary *accountDict = [[PEPTestUtils
dirk@465
   874
                                         unarchiveDictionary:@"account_A3FC7F0A.ser"]
dirk@465
   875
                                        mutableCopy];
andreas@392
   876
    [accountDict removeObjectForKey:kPepCommType];
andreas@392
   877
    [accountDict removeObjectForKey:kPepFingerprint];
andreas@392
   878
    PEPIdentity *identMe = [[PEPIdentity alloc] initWithDictionary:accountDict];
andreas@392
   879
dirk@532
   880
    NSError *error = nil;
dirk@532
   881
    XCTAssertTrue([session mySelf:identMe error:&error]);
dirk@532
   882
    XCTAssertNil(error);
dirk@532
   883
andreas@392
   884
    XCTAssertNotNil(identMe.fingerPrint);
andreas@392
   885
andreas@392
   886
    NSArray* keys;
andreas@392
   887
    PEPMessage *msg = [PEPMessage new];
andreas@392
   888
    [msg setValuesForKeysWithDictionary:msgDict];
dirk@514
   889
dirk@514
   890
    // Technically, the mail is encrypted, but the signatures don't match
dirk@517
   891
    PEPMessage *pepDecryptedMail = [session
dirk@517
   892
                                    decryptMessage:msg
dirk@517
   893
                                    rating:nil
dirk@523
   894
                                    extraKeys:&keys
dirk@524
   895
                                    status:nil
dirk@517
   896
                                    error:&error];
dirk@517
   897
    XCTAssertNotNil(pepDecryptedMail);
dirk@514
   898
    XCTAssertNil(error);
dirk@514
   899
andreas@392
   900
    XCTAssertNotNil(pepDecryptedMail.longMessage);
andreas@392
   901
}
andreas@392
   902
andreas@392
   903
- (void)testOutgoingContactColor
andreas@392
   904
{
andreas@392
   905
    PEPSession *session = [PEPSession new];
andreas@392
   906
andreas@411
   907
    PEPIdentity *partner1Orig = [PEPTestUtils foreignPepIdentityWithAddress:@"partner1@dontcare.me"
andreas@411
   908
                                                                   userName:@"Partner 1"];
andreas@392
   909
    NSString *pubKeyPartner1 = [PEPTestUtils loadResourceByName:@"partner1_F2D281C2789DD7F6_pub.asc"];
andreas@392
   910
    XCTAssertNotNil(pubKeyPartner1);
dirk@551
   911
dirk@551
   912
    NSError *error = nil;
dirk@551
   913
    XCTAssertTrue([session importKey:pubKeyPartner1 error:&error]);
dirk@551
   914
    XCTAssertNil(error);
andreas@392
   915
dirk@529
   916
    PEP_rating color = [self ratingForIdentity:partner1Orig session:session];
andreas@392
   917
    XCTAssertEqual(color, PEP_rating_reliable);
andreas@392
   918
}
andreas@392
   919
andreas@392
   920
- (void)testGetTrustwords
andreas@392
   921
{
andreas@392
   922
    PEPSession *session = [PEPSession new];
andreas@392
   923
andreas@392
   924
    PEPIdentity *partner1Orig = [[PEPIdentity alloc]
andreas@392
   925
                                 initWithAddress:@"partner1@dontcare.me" userID:@"partner1"
andreas@392
   926
                                 userName:@"partner1"
andreas@392
   927
                                 isOwn:NO fingerPrint:@"F0CD3F7B422E5D587ABD885BF2D281C2789DD7F6"];
andreas@392
   928
andreas@392
   929
    PEPIdentity *meOrig = [[PEPIdentity alloc]
andreas@392
   930
                           initWithAddress:@"me@dontcare.me" userID:@"me"
andreas@392
   931
                           userName:@"me"
andreas@392
   932
                           isOwn:NO fingerPrint:@"CC1F73F6FB774BF08B197691E3BFBCA9248FC681"];
andreas@392
   933
andreas@392
   934
    NSString *pubKeyPartner1 = [PEPTestUtils loadResourceByName:@"partner1_F2D281C2789DD7F6_pub.asc"];
andreas@392
   935
    XCTAssertNotNil(pubKeyPartner1);
andreas@392
   936
    NSString *pubKeyMe = [PEPTestUtils loadResourceByName:@"meATdontcare_E3BFBCA9248FC681_pub.asc"];
andreas@392
   937
    XCTAssertNotNil(pubKeyMe);
andreas@392
   938
    NSString *secKeyMe = [PEPTestUtils loadResourceByName:@"meATdontcare_E3BFBCA9248FC681_sec.asc"];
andreas@392
   939
    XCTAssertNotNil(secKeyMe);
andreas@392
   940
dirk@558
   941
    NSError *error = nil;
andreas@392
   942
    NSString *trustwordsFull = [session getTrustwordsIdentity1:meOrig identity2:partner1Orig
dirk@558
   943
                                                      language:nil full:YES error:&error];
dirk@558
   944
    XCTAssertNil(error);
andreas@392
   945
    XCTAssertEqualObjects(trustwordsFull,
andreas@392
   946
                          @"EMERSON GASPER TOKENISM BOLUS COLLAGE DESPISE BEDDED ENCRYPTION IMAGINE BEDFORD");
andreas@392
   947
andreas@392
   948
    NSString *trustwordsFullEnglish = [session getTrustwordsIdentity1:meOrig identity2:partner1Orig
dirk@558
   949
                                                             language:@"en" full:YES error:&error];
dirk@558
   950
    XCTAssertNil(error);
andreas@392
   951
    XCTAssertEqualObjects(trustwordsFullEnglish, trustwordsFull);
andreas@392
   952
andreas@392
   953
    NSString *trustwordsUndefined = [session getTrustwordsIdentity1:meOrig identity2:partner1Orig
dirk@558
   954
                                                           language:@"ZZ" full:YES error:&error];
dirk@558
   955
    XCTAssertNotNil(error);
andreas@392
   956
    XCTAssertNil(trustwordsUndefined);
andreas@392
   957
}
andreas@392
   958
dirk@418
   959
- (void)testStringToRating
dirk@418
   960
{
dirk@418
   961
    PEPSession *session = [PEPSession new];
dirk@418
   962
    XCTAssertEqual([session ratingFromString:@"cannot_decrypt"], PEP_rating_cannot_decrypt);
dirk@418
   963
    XCTAssertEqual([session ratingFromString:@"have_no_key"], PEP_rating_have_no_key);
dirk@418
   964
    XCTAssertEqual([session ratingFromString:@"unencrypted"], PEP_rating_unencrypted);
dirk@418
   965
    XCTAssertEqual([session ratingFromString:@"unencrypted_for_some"],
dirk@418
   966
                   PEP_rating_unencrypted_for_some);
dirk@418
   967
    XCTAssertEqual([session ratingFromString:@"unreliable"], PEP_rating_unreliable);
dirk@418
   968
    XCTAssertEqual([session ratingFromString:@"reliable"], PEP_rating_reliable);
dirk@418
   969
    XCTAssertEqual([session ratingFromString:@"trusted"], PEP_rating_trusted);
dirk@418
   970
    XCTAssertEqual([session ratingFromString:@"trusted_and_anonymized"],
dirk@418
   971
                   PEP_rating_trusted_and_anonymized);
dirk@418
   972
    XCTAssertEqual([session ratingFromString:@"fully_anonymous"], PEP_rating_fully_anonymous);
dirk@418
   973
    XCTAssertEqual([session ratingFromString:@"mistrust"], PEP_rating_mistrust);
dirk@418
   974
    XCTAssertEqual([session ratingFromString:@"b0rken"], PEP_rating_b0rken);
dirk@418
   975
    XCTAssertEqual([session ratingFromString:@"under_attack"], PEP_rating_under_attack);
dirk@418
   976
    XCTAssertEqual([session ratingFromString:@"undefined"], PEP_rating_undefined);
dirk@418
   977
    XCTAssertEqual([session ratingFromString:@"does not exist111"], PEP_rating_undefined);
dirk@418
   978
}
dirk@418
   979
dirk@418
   980
- (void)testRatingToString
dirk@418
   981
{
dirk@418
   982
    PEPSession *session = [PEPSession new];
dirk@418
   983
    XCTAssertEqualObjects([session stringFromRating:PEP_rating_cannot_decrypt], @"cannot_decrypt");
dirk@418
   984
    XCTAssertEqualObjects([session stringFromRating:PEP_rating_have_no_key], @"have_no_key");
dirk@418
   985
    XCTAssertEqualObjects([session stringFromRating:PEP_rating_unencrypted], @"unencrypted");
dirk@418
   986
    XCTAssertEqualObjects([session stringFromRating:PEP_rating_unencrypted_for_some],
dirk@418
   987
                          @"unencrypted_for_some");
dirk@418
   988
    XCTAssertEqualObjects([session stringFromRating:PEP_rating_unreliable], @"unreliable");
dirk@418
   989
    XCTAssertEqualObjects([session stringFromRating:PEP_rating_reliable], @"reliable");
dirk@418
   990
    XCTAssertEqualObjects([session stringFromRating:PEP_rating_trusted], @"trusted");
dirk@418
   991
    XCTAssertEqualObjects([session stringFromRating:PEP_rating_trusted_and_anonymized],
dirk@418
   992
                          @"trusted_and_anonymized");
dirk@418
   993
    XCTAssertEqualObjects([session stringFromRating:PEP_rating_fully_anonymous],
dirk@418
   994
                          @"fully_anonymous");
dirk@418
   995
    XCTAssertEqualObjects([session stringFromRating:PEP_rating_mistrust], @"mistrust");
dirk@418
   996
    XCTAssertEqualObjects([session stringFromRating:PEP_rating_b0rken], @"b0rken");
dirk@418
   997
    XCTAssertEqualObjects([session stringFromRating:PEP_rating_under_attack], @"under_attack");
dirk@418
   998
    XCTAssertEqualObjects([session stringFromRating:PEP_rating_undefined], @"undefined");
dirk@418
   999
    XCTAssertEqualObjects([session stringFromRating:500], @"undefined");
dirk@418
  1000
}
dirk@418
  1001
dirk@431
  1002
- (void)testIsPEPUser
dirk@431
  1003
{
dirk@431
  1004
    PEPSession *session = [PEPSession new];
dirk@431
  1005
dirk@431
  1006
    PEPIdentity *identMe = [[PEPIdentity alloc]
dirk@431
  1007
                            initWithAddress:@"me-myself-and-i@pep-project.org"
dirk@431
  1008
                            userID:@"me-myself-and-i"
dirk@452
  1009
                            userName:@"pEp Me"
dirk@431
  1010
                            isOwn:YES];
dirk@532
  1011
    NSError *error = nil;
dirk@532
  1012
    XCTAssertTrue([session mySelf:identMe error:&error]);
dirk@532
  1013
    XCTAssertNil(error);
dirk@532
  1014
dirk@431
  1015
    XCTAssertNotNil(identMe.fingerPrint);
dirk@432
  1016
dirk@432
  1017
    // PEP_CANNOT_FIND_PERSON == 902
dirk@567
  1018
    XCTAssertTrue([session isPEPUser:identMe error:&error]);
dirk@431
  1019
}
dirk@431
  1020
dirk@449
  1021
- (void)testXEncStatusForOutgoingEncryptedMail
dirk@449
  1022
{
dirk@449
  1023
    [self helperXEncStatusForOutgoingEncryptdMailToSelf:NO expectedRating:PEP_rating_reliable];
dirk@449
  1024
}
dirk@449
  1025
dirk@449
  1026
- (void)testXEncStatusForOutgoingSelfEncryptedMail
dirk@449
  1027
{
dirk@449
  1028
    [self helperXEncStatusForOutgoingEncryptdMailToSelf:YES
dirk@449
  1029
                                         expectedRating:PEP_rating_trusted_and_anonymized];
dirk@449
  1030
}
dirk@449
  1031
dirk@456
  1032
- (void)testEncryptMessagesWithoutKeys
dirk@456
  1033
{
dirk@456
  1034
    PEPSession *session = [PEPSession new];
dirk@456
  1035
dirk@456
  1036
    PEPIdentity *identMe = [[PEPIdentity alloc]
dirk@456
  1037
                            initWithAddress:@"me-myself-and-i@pep-project.org"
dirk@456
  1038
                            userID:@"me-myself-and-i"
dirk@456
  1039
                            userName:@"pEp Me"
dirk@456
  1040
                            isOwn:YES];
dirk@532
  1041
    NSError *error = nil;
dirk@532
  1042
    XCTAssertTrue([session mySelf:identMe error:&error]);
dirk@532
  1043
    XCTAssertNil(error);
dirk@532
  1044
dirk@456
  1045
    XCTAssertNotNil(identMe.fingerPrint);
dirk@456
  1046
dirk@456
  1047
    PEPIdentity *identAlice = [[PEPIdentity alloc]
dirk@456
  1048
                               initWithAddress:@"alice@pep-project.org"
dirk@456
  1049
                               userID:@"alice"
dirk@456
  1050
                               userName:@"pEp Test Alice"
dirk@456
  1051
                               isOwn:NO];
dirk@456
  1052
dirk@456
  1053
    PEPMessage *msg = [PEPMessage new];
dirk@456
  1054
    msg.from = identMe;
dirk@456
  1055
    msg.to = @[identAlice];
dirk@456
  1056
    msg.shortMessage = @"Mail to Alice";
dirk@456
  1057
    msg.longMessage = @"Alice?";
dirk@456
  1058
    msg.direction = PEP_dir_outgoing;
dirk@456
  1059
dirk@573
  1060
    NSNumber *numRating = [session outgoingRatingForMessage:msg error:&error];
dirk@573
  1061
    XCTAssertNotNil(numRating);
dirk@573
  1062
    XCTAssertNil(error);
dirk@573
  1063
    XCTAssertEqual(numRating.pEpRating, PEP_rating_unencrypted);
dirk@528
  1064
dirk@524
  1065
    PEPMessage *encMsg = [session encryptMessage:msg extraKeys:nil status:nil error:&error];
dirk@522
  1066
    XCTAssertNotNil(encMsg);
dirk@522
  1067
    XCTAssertNil(error);
dirk@456
  1068
dirk@456
  1069
    XCTAssertNotNil(encMsg);
dirk@456
  1070
dirk@456
  1071
    PEPStringList *keys;
dirk@514
  1072
    PEP_rating pEpRating;
dirk@522
  1073
    error = nil;
dirk@517
  1074
    PEPMessage *decMsg = [session
dirk@517
  1075
                          decryptMessage:encMsg
dirk@517
  1076
                          rating:&pEpRating
dirk@523
  1077
                          extraKeys:&keys
dirk@524
  1078
                          status:nil
dirk@517
  1079
                          error:&error];
dirk@517
  1080
    XCTAssertNotNil(decMsg);
dirk@514
  1081
    XCTAssertNil(error);
dirk@514
  1082
dirk@456
  1083
    XCTAssertEqual(pEpRating, PEP_rating_unencrypted);
dirk@456
  1084
    XCTAssertNotNil(decMsg);
dirk@456
  1085
}
dirk@456
  1086
dirk@456
  1087
/**
dirk@456
  1088
 ENGINE-364. Tries to invoke trustPersonalKey on an identity without key,
dirk@456
  1089
 giving it a fake fingerprint.
dirk@456
  1090
 */
dirk@456
  1091
- (void)testTrustPersonalKey
dirk@456
  1092
{
dirk@456
  1093
    PEPSession *session = [PEPSession new];
dirk@456
  1094
dirk@456
  1095
    PEPIdentity *identMe = [[PEPIdentity alloc]
dirk@456
  1096
                            initWithAddress:@"me-myself-and-i@pep-project.org"
dirk@456
  1097
                            userID:@"me-myself-and-i"
dirk@456
  1098
                            userName:@"pEp Me"
dirk@456
  1099
                            isOwn:YES];
dirk@532
  1100
    NSError *error = nil;
dirk@532
  1101
    XCTAssertTrue([session mySelf:identMe error:&error]);
dirk@532
  1102
    XCTAssertNil(error);
dirk@532
  1103
dirk@456
  1104
    XCTAssertNotNil(identMe.fingerPrint);
dirk@456
  1105
dirk@456
  1106
    // The fingerprint is definitely wrong, we don't have a key
dirk@456
  1107
    PEPIdentity *identAlice = [[PEPIdentity alloc]
dirk@456
  1108
                               initWithAddress:@"alice@pep-project.org"
dirk@456
  1109
                               userID:@"alice"
dirk@456
  1110
                               userName:@"pEp Test Alice"
dirk@456
  1111
                               isOwn:NO
dirk@456
  1112
                               fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
dirk@456
  1113
dirk@534
  1114
    XCTAssertFalse([session trustPersonalKey:identAlice error:&error]);
dirk@534
  1115
    XCTAssertNotNil(error);
dirk@456
  1116
}
dirk@456
  1117
dirk@457
  1118
/**
dirk@457
  1119
 ENGINE-381
dirk@457
  1120
 */
dirk@457
  1121
- (void)testVolatileIdentityRating
dirk@457
  1122
{
dirk@457
  1123
    PEPSession *session = [PEPSession new];
dirk@457
  1124
dirk@457
  1125
    PEPIdentity *identMe = [[PEPIdentity alloc]
dirk@457
  1126
                            initWithAddress:@"me-myself-and-i@pep-project.org"
dirk@457
  1127
                            userID:@"me-myself-and-i"
dirk@457
  1128
                            userName:@"pEp Me"
dirk@457
  1129
                            isOwn:YES];
dirk@532
  1130
    NSError *error = nil;
dirk@532
  1131
    XCTAssertTrue([session mySelf:identMe error:&error]);
dirk@532
  1132
    XCTAssertNil(error);
dirk@532
  1133
dirk@457
  1134
    XCTAssertNotNil(identMe.fingerPrint);
dirk@457
  1135
dirk@457
  1136
    PEPIdentity *identAlice = [self
dirk@457
  1137
                               checkImportingKeyFilePath:@"6FF00E97_sec.asc"
dirk@457
  1138
                               address:@"pep.test.alice@pep-project.org"
dirk@457
  1139
                               userID:@"alice_user_id"
dirk@457
  1140
                               fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
dirk@457
  1141
                               session: session];
dirk@488
  1142
    XCTAssertNotNil(identAlice);
dirk@457
  1143
dirk@457
  1144
    dispatch_group_t identityRatingGroup = dispatch_group_create();
dirk@457
  1145
dirk@457
  1146
    void (^ratingBlock)(void) = ^{
dirk@457
  1147
        PEPSession *innerSession = [PEPSession new];
dirk@529
  1148
        PEP_rating rating = [self ratingForIdentity:identAlice session:innerSession];
dirk@457
  1149
        XCTAssertEqual(rating, PEP_rating_reliable);
dirk@457
  1150
    };
dirk@457
  1151
dirk@479
  1152
    for (int i = 0; i < 4; ++i) {
dirk@457
  1153
        dispatch_group_async(identityRatingGroup,
dirk@457
  1154
                             dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0),
dirk@457
  1155
                             ratingBlock);
dirk@457
  1156
    }
dirk@457
  1157
dirk@479
  1158
    for (int i = 0; i < 4; ++i) {
dirk@457
  1159
        ratingBlock();
dirk@457
  1160
    }
dirk@457
  1161
dirk@457
  1162
    dispatch_group_wait(identityRatingGroup, DISPATCH_TIME_FOREVER);
dirk@457
  1163
}
dirk@457
  1164
dirk@449
  1165
#pragma mark - configUnencryptedSubject
dirk@449
  1166
dirk@449
  1167
- (void)testConfigUnencryptedSubject
dirk@449
  1168
{
dirk@449
  1169
    // Setup Config to encrypt subject
dirk@449
  1170
    [PEPObjCAdapter setUnecryptedSubjectEnabled:NO];
dirk@449
  1171
dirk@449
  1172
    // Write mail to yourself ...
dirk@449
  1173
    PEPMessage *encMessage = [self mailWrittenToMySelf];
dirk@449
  1174
dirk@449
  1175
    // ... and assert subject is encrypted
dirk@449
  1176
    XCTAssertEqualObjects(encMessage.shortMessage, @"p≡p", @"Subject should be encrypted");
dirk@449
  1177
}
dirk@449
  1178
dirk@449
  1179
- (void)testConfigUnencryptedSubject_encryptedSubjectDisabled
dirk@449
  1180
{
dirk@449
  1181
    // Setup Config to not encrypt subject
dirk@449
  1182
    [PEPObjCAdapter setUnecryptedSubjectEnabled:YES];
dirk@449
  1183
dirk@449
  1184
    // Write mail to yourself ...
dirk@449
  1185
    PEPMessage *encMessage = [self mailWrittenToMySelf];
dirk@449
  1186
dirk@449
  1187
    // ... and assert the subject is not encrypted
dirk@449
  1188
    XCTAssertNotEqualObjects(encMessage.shortMessage, @"p≡p", @"Subject should not be encrypted");
dirk@449
  1189
}
dirk@449
  1190
andreas@392
  1191
#pragma mark - Helpers
andreas@392
  1192
dirk@529
  1193
/**
dirk@529
  1194
 Determines the rating for the given identity.
dirk@529
  1195
 @return PEP_rating_undefined on error
dirk@529
  1196
 */
dirk@529
  1197
- (PEP_rating)ratingForIdentity:(PEPIdentity *)identity session:(PEPSession *)session
dirk@529
  1198
{
dirk@529
  1199
    NSError *error;
dirk@529
  1200
    PEP_rating rating;
dirk@529
  1201
    XCTAssertTrue([session rating:&rating forIdentity:identity error:&error]);
dirk@529
  1202
    XCTAssertNil(error);
dirk@529
  1203
    return rating;
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