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