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