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