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