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