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