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