pEpObjCTests/PEPSessionTest.m
author Dirk Zimmermann <dz@pep.security>
Tue, 03 Dec 2019 14:07:27 +0100
branchIOSAD-160
changeset 1375 693a6506fb50
parent 1358 8266b8194627
child 1376 8e4f3ff6694b
permissions -rw-r--r--
IOSAD-160 Test new myself version
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@1353
    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@1375
  1269
/// Test the disable of sync via myself.
dz@1375
  1270
- (void)testNoBeaconOnMyself
dz@1375
  1271
{
dz@1375
  1272
    PEPSession *session = [PEPSession new];
dz@1375
  1273
dz@1375
  1274
    XCTAssertEqual(self.sendMessageDelegate.messages.count, 0);
dz@1375
  1275
    XCTAssertNil(self.sendMessageDelegate.lastMessage);
dz@1375
  1276
dz@1375
  1277
    PEPIdentity *identMe = [[PEPIdentity alloc]
dz@1375
  1278
                            initWithAddress:@"me-myself-and-i@pep-project.org"
dz@1375
  1279
                            userID:@"me-myself-and-i"
dz@1375
  1280
                            userName:@"pEp Me"
dz@1375
  1281
                            isOwn:YES];
dz@1375
  1282
dz@1375
  1283
    NSError *error = nil;
dz@1375
  1284
    XCTAssertTrue([session mySelf:identMe pEpSyncEnabled:NO error:&error]);
dz@1375
  1285
    XCTAssertNil(error);
dz@1375
  1286
dz@1375
  1287
    [self startSync];
dz@1375
  1288
dz@1375
  1289
    [NSThread sleepForTimeInterval:2];
dz@1375
  1290
    XCTAssertNil(self.sendMessageDelegate.lastMessage);
dz@1375
  1291
dz@1375
  1292
    XCTAssertEqual(self.sendMessageDelegate.messages.count, 0);
dz@1375
  1293
    [self shutdownSync];
dz@1375
  1294
}
dz@1375
  1295
dz@932
  1296
#pragma mark - key_reset_identity
dz@932
  1297
dz@932
  1298
- (void)testKeyResetIdentity
dz@932
  1299
{
dz@932
  1300
    PEPSession *session = [PEPSession new];
dz@932
  1301
dz@932
  1302
    PEPIdentity *me = [PEPTestUtils ownPepIdentityWithAddress:@"me@peptest.ch"
dz@932
  1303
                                                     userName:@"userName"];
dz@932
  1304
dz@932
  1305
    NSError *error = nil;
dz@932
  1306
    XCTAssertTrue([session mySelf:me error:&error]);
dz@932
  1307
    XCTAssertNil(error);
dz@932
  1308
dz@932
  1309
    NSString *fprOriginal = me.fingerPrint;
dz@932
  1310
    XCTAssertNotNil(fprOriginal);
dz@932
  1311
dz@932
  1312
    XCTAssertTrue([session keyReset:me fingerprint:nil error:&error]);
dz@932
  1313
    XCTAssertNil(error);
dz@932
  1314
dz@932
  1315
    XCTAssertTrue([session mySelf:me error:&error]);
dz@932
  1316
    XCTAssertNil(error);
dz@932
  1317
dz@932
  1318
    NSString *fprAfterReset = me.fingerPrint;
dz@932
  1319
    XCTAssertNotNil(fprAfterReset);
dz@932
  1320
dz@932
  1321
    XCTAssertNotEqual(fprOriginal, fprAfterReset);
dz@932
  1322
}
dz@932
  1323
dz@1011
  1324
#pragma mark - leave_device_group
dz@1011
  1325
dz@1014
  1326
/** Leaving a device group is successful even though none exists. */
dz@1014
  1327
- (void)testSuccessfulLeaveDeviceGroup
dz@1011
  1328
{
dz@1014
  1329
    PEPSession *session = [PEPSession new];
dz@1014
  1330
dz@1014
  1331
    PEPIdentity *identMe = [[PEPIdentity alloc]
dz@1014
  1332
                            initWithAddress:@"me-myself-and-i@pep-project.org"
dz@1014
  1333
                            userID:@"me-myself-and-i"
dz@1014
  1334
                            userName:@"pEp Me"
dz@1014
  1335
                            isOwn:YES];
dz@1014
  1336
    NSError *error = nil;
dz@1014
  1337
    XCTAssertTrue([session mySelf:identMe error:&error]);
dz@1014
  1338
    XCTAssertNil(error);
dz@1014
  1339
dz@1014
  1340
    [self startSync];
dz@1014
  1341
dz@1014
  1342
    error = nil;
andreas@1330
  1343
    XCTAssertTrue([session leaveDeviceGroup:&error]);
dz@1014
  1344
    XCTAssertNil(error);
dz@1014
  1345
dz@1343
  1346
    // leaving a device group should disable sync
dz@1358
  1347
    XCTAssertTrue(self.notifyHandshakeDelegate.engineDidShutdownKeySync);
dz@1343
  1348
dz@1353
  1349
    [self shutdownSync];
dz@1011
  1350
}
dz@1011
  1351
dz@1182
  1352
#pragma mark - enable/disable sync
dz@1182
  1353
dz@1204
  1354
- (void)testEnableDisableFailForSyncOnPartnerIdentity
dz@1202
  1355
{
dz@1202
  1356
    PEPIdentity *notMe = [[PEPIdentity alloc]
dz@1205
  1357
                          initWithAddress:@"notme@pep-project.org"
dz@1205
  1358
                          userID:@"notme_ID"
dz@1205
  1359
                          userName:@"notme"
dz@1205
  1360
                          isOwn:NO];
dz@1202
  1361
dz@1202
  1362
    NSError *error = nil;
dz@1202
  1363
    XCTAssertFalse([notMe enableKeySync:&error]);
dz@1202
  1364
    XCTAssertNotNil(error);
dz@1202
  1365
dz@1202
  1366
    error = nil;
dz@1202
  1367
    XCTAssertFalse([notMe disableKeySync:&error]);
dz@1202
  1368
    XCTAssertNotNil(error);
dz@1202
  1369
dz@1202
  1370
    error = nil;
dz@1202
  1371
    XCTAssertNil([notMe queryKeySyncEnabled:&error]);
dz@1202
  1372
    XCTAssertNotNil(error);
dz@1202
  1373
}
dz@1202
  1374
dz@1191
  1375
- (void)testEnableDisableSyncOnOwnIdentityWithQuery
dz@1182
  1376
{
dz@1182
  1377
    PEPSession *session = [PEPSession new];
dz@1182
  1378
dz@1197
  1379
    PEPIdentity *identMe1 = [[PEPIdentity alloc]
dz@1197
  1380
                             initWithAddress:@"me-myself-and-i@pep-project.org"
dz@1197
  1381
                             userID:@"me-myself-and-i"
dz@1197
  1382
                             userName:@"pEp Me"
dz@1197
  1383
                             isOwn:YES];
dz@1182
  1384
    NSError *error = nil;
dz@1197
  1385
    XCTAssertTrue([session mySelf:identMe1 error:&error]);
dz@1182
  1386
    XCTAssertNil(error);
dz@1182
  1387
dz@1197
  1388
    error = nil;
dz@1197
  1389
    PEPIdentity *identMe2 = [[PEPIdentity alloc]
dz@1197
  1390
                             initWithAddress:@"me-myself-and-i2@pep-project.org"
dz@1197
  1391
                             userID:@"me-myself-and-i2"
dz@1197
  1392
                             userName:@"pEp Me2"
dz@1197
  1393
                             isOwn:YES];
dz@1197
  1394
    XCTAssertTrue([session mySelf:identMe2 error:&error]);
dz@1197
  1395
    XCTAssertNil(error);
dz@1197
  1396
dz@1197
  1397
    XCTAssertNotEqualObjects(identMe1.fingerPrint, identMe2.fingerPrint);
dz@1197
  1398
dz@1188
  1399
    for (int i = 0; i < 10; ++i) {
dz@1188
  1400
        error = nil;
dz@1192
  1401
        BOOL enable = i % 2 == 0; // enable keysync on even numbers (roughly)
dz@1192
  1402
        if (enable) {
dz@1197
  1403
            XCTAssertTrue([session enableSyncForIdentity:identMe1 error:&error]);
dz@1198
  1404
            XCTAssertTrue([identMe2 enableKeySync:&error]);
dz@1188
  1405
        } else {
dz@1197
  1406
            XCTAssertTrue([session disableSyncForIdentity:identMe1 error:&error]);
dz@1198
  1407
            XCTAssertTrue([identMe2 disableKeySync:&error]);
dz@1188
  1408
        }
dz@1188
  1409
        XCTAssertNil(error);
dz@1191
  1410
dz@1197
  1411
        NSNumber *keySyncState1 = [session queryKeySyncEnabledForIdentity:identMe1 error:&error];
dz@1198
  1412
        NSNumber *keySyncState2 = [identMe2 queryKeySyncEnabled:&error];
dz@1191
  1413
        XCTAssertNil(error);
dz@1197
  1414
        XCTAssertNotNil(keySyncState1);
dz@1197
  1415
        XCTAssertNotNil(keySyncState2);
dz@1192
  1416
        if (enable) {
dz@1197
  1417
            XCTAssertTrue([keySyncState1 boolValue]);
dz@1191
  1418
        } else {
dz@1197
  1419
            XCTAssertFalse([keySyncState1 boolValue]);
dz@1191
  1420
        }
dz@1197
  1421
        XCTAssertEqualObjects(keySyncState1, keySyncState2);
dz@1196
  1422
    }
dz@1196
  1423
}
dz@1196
  1424
dz@1203
  1425
/**
dz@1203
  1426
 ENGINE-604, just in case.
dz@1203
  1427
 */
dz@1199
  1428
- (void)testQueryKeySyncOnOwnIdentityInALoop
dz@1199
  1429
{
dz@1199
  1430
    PEPSession *session = [PEPSession new];
dz@1199
  1431
dz@1199
  1432
    PEPIdentity *identMe = [[PEPIdentity alloc]
dz@1199
  1433
                            initWithAddress:@"me-myself-and-i@pep-project.org"
dz@1199
  1434
                            userID:@"me-myself-and-i"
dz@1199
  1435
                            userName:@"pEp Me"
dz@1199
  1436
                            isOwn:YES];
dz@1199
  1437
    NSError *error = nil;
dz@1199
  1438
    XCTAssertTrue([session mySelf:identMe error:&error]);
dz@1199
  1439
    XCTAssertNil(error);
dz@1199
  1440
dz@1199
  1441
    for (NSNumber *numBool in @[@YES, @NO]) {
dz@1199
  1442
        error = nil;
dz@1199
  1443
        if ([numBool boolValue]) {
dz@1199
  1444
            XCTAssertTrue([session enableSyncForIdentity:identMe error:&error]);
dz@1199
  1445
        } else {
dz@1199
  1446
            XCTAssertTrue([session disableSyncForIdentity:identMe error:&error]);
dz@1199
  1447
        }
dz@1199
  1448
        XCTAssertNil(error);
dz@1199
  1449
dz@1199
  1450
        for (int i = 0; i < 10; ++i) {
dz@1199
  1451
            NSNumber *numQuery = [session queryKeySyncEnabledForIdentity:identMe error:&error];
dz@1199
  1452
            XCTAssertNotNil(numQuery);
dz@1199
  1453
            XCTAssertEqualObjects(numBool, numQuery);
dz@1199
  1454
            XCTAssertNil(error);
dz@1199
  1455
        }
dz@1199
  1456
    }
dz@1199
  1457
}
dz@1199
  1458
dz@1286
  1459
/**
dz@1286
  1460
 Tests [PEPSessionProtocol keyResetAllOwnKeysError:error].
dz@1286
  1461
dz@1286
  1462
 Does the following:
dz@1286
  1463
 * Do a mySelf.
dz@1286
  1464
 * Catch the sent out sync (beacon?) message.
dz@1286
  1465
 * Do a key reset on all own identities ([PEPSessionProtocol keyResetAllOwnKeysError:error]).
dz@1286
  1466
 * Catch the sent out sync message.
dz@1286
  1467
 * Decrypt the caught sync messages.
dz@1286
  1468
 */
dz@1292
  1469
- (void)testDecryptBeaconsAfterKeyReset
dz@1286
  1470
{
dz@1286
  1471
    PEPSession *session = [PEPSession new];
dz@1286
  1472
dz@1286
  1473
    XCTAssertEqual(self.sendMessageDelegate.messages.count, 0);
dz@1286
  1474
    XCTAssertNil(self.sendMessageDelegate.lastMessage);
dz@1286
  1475
dz@1286
  1476
    PEPIdentity *identMe = [[PEPIdentity alloc]
dz@1286
  1477
                            initWithAddress:@"me-myself-and-i@pep-project.org"
dz@1286
  1478
                            userID:@"me-myself-and-i"
dz@1286
  1479
                            userName:@"pEp Me"
dz@1286
  1480
                            isOwn:YES];
dz@1286
  1481
    NSError *error = nil;
dz@1286
  1482
    XCTAssertTrue([session mySelf:identMe error:&error]);
dz@1286
  1483
    XCTAssertNil(error);
dz@1286
  1484
    XCTAssertNotNil(identMe.fingerPrint);
dz@1286
  1485
dz@1286
  1486
    [self startSync];
dz@1286
  1487
dz@1286
  1488
    XCTKVOExpectation *expHaveMessage1 = [[XCTKVOExpectation alloc]
dz@1286
  1489
                                          initWithKeyPath:@"lastMessage"
dz@1286
  1490
                                          object:self.sendMessageDelegate];
dz@1286
  1491
    [self waitForExpectations:@[expHaveMessage1] timeout:PEPTestInternalSyncTimeout];
dz@1286
  1492
    XCTAssertNotNil(self.sendMessageDelegate.lastMessage);
dz@1286
  1493
    XCTAssertEqual(self.sendMessageDelegate.messages.count, 1);
dz@1286
  1494
dz@1286
  1495
    PEPMessage *oldBeacon = self.sendMessageDelegate.lastMessage;
dz@1286
  1496
dz@1286
  1497
    XCTAssertTrue([session keyResetAllOwnKeysError:&error]);
dz@1286
  1498
    XCTAssertNil(error);
dz@1286
  1499
dz@1286
  1500
    XCTKVOExpectation *expHaveMessage2 = [[XCTKVOExpectation alloc]
dz@1286
  1501
                                          initWithKeyPath:@"lastMessage"
dz@1286
  1502
                                          object:self.sendMessageDelegate];
dz@1286
  1503
    [self waitForExpectations:@[expHaveMessage2] timeout:PEPTestInternalSyncTimeout];
dz@1286
  1504
    XCTAssertNotNil(self.sendMessageDelegate.lastMessage);
dz@1286
  1505
    XCTAssertEqual(self.sendMessageDelegate.messages.count, 2);
dz@1286
  1506
dz@1286
  1507
    PEPMessage *newBeacon = self.sendMessageDelegate.lastMessage;
dz@1286
  1508
dz@1286
  1509
    XCTAssertNotEqual(oldBeacon, newBeacon);
dz@1286
  1510
dz@1286
  1511
    PEPRating rating;
dz@1286
  1512
    PEPStringList *extraKeys;
dz@1286
  1513
    PEPStatus status;
dz@1286
  1514
    PEPMessage *decryptedOldBeacon = [session decryptMessage:oldBeacon
dz@1286
  1515
                                                       flags:nil
dz@1286
  1516
                                                      rating:&rating
dz@1286
  1517
                                                   extraKeys:&extraKeys
dz@1286
  1518
                                                      status:&status
dz@1286
  1519
                                                       error:&error];
dz@1286
  1520
    XCTAssertNotNil(decryptedOldBeacon);
dz@1286
  1521
    XCTAssertNil(error);
dz@1286
  1522
dz@1286
  1523
    PEPMessage *decryptedNewBeacon = [session decryptMessage:newBeacon
dz@1286
  1524
                                                       flags:nil
dz@1286
  1525
                                                      rating:&rating
dz@1286
  1526
                                                   extraKeys:&extraKeys
dz@1286
  1527
                                                      status:&status
dz@1286
  1528
                                                       error:&error];
dz@1286
  1529
    XCTAssertNotNil(decryptedNewBeacon);
dz@1286
  1530
    XCTAssertNil(error);
dz@1286
  1531
dz@1353
  1532
    [self shutdownSync];
dz@1286
  1533
}
dz@1286
  1534
andreas@392
  1535
#pragma mark - Helpers
andreas@392
  1536
dirk@774
  1537
- (void)testSendMessageOnSession:(PEPSession *)session
dirk@774
  1538
{
dirk@749
  1539
    XCTAssertEqual(self.sendMessageDelegate.messages.count, 0);
dirk@760
  1540
    XCTAssertNil(self.sendMessageDelegate.lastMessage);
dirk@749
  1541
dirk@749
  1542
    PEPIdentity *identMe = [[PEPIdentity alloc]
dirk@749
  1543
                            initWithAddress:@"me-myself-and-i@pep-project.org"
dirk@749
  1544
                            userID:@"me-myself-and-i"
dirk@749
  1545
                            userName:@"pEp Me"
dirk@749
  1546
                            isOwn:YES];
dz@1343
  1547
dirk@749
  1548
    NSError *error = nil;
dirk@749
  1549
    XCTAssertTrue([session mySelf:identMe error:&error]);
dirk@749
  1550
    XCTAssertNil(error);
dirk@749
  1551
dz@993
  1552
    [self startSync];
dirk@764
  1553
dirk@764
  1554
    XCTKVOExpectation *expHaveMessage = [[XCTKVOExpectation alloc]
dirk@764
  1555
                                         initWithKeyPath:@"lastMessage"
dirk@764
  1556
                                         object:self.sendMessageDelegate];
dirk@764
  1557
dirk@749
  1558
    XCTAssertNotNil(identMe.fingerPrint);
dirk@749
  1559
dirk@762
  1560
    [self waitForExpectations:@[expHaveMessage] timeout:PEPTestInternalSyncTimeout];
dirk@760
  1561
    XCTAssertNotNil(self.sendMessageDelegate.lastMessage);
dirk@749
  1562
dirk@749
  1563
    XCTAssertEqual(self.sendMessageDelegate.messages.count, 1);
dz@1353
  1564
    [self shutdownSync];
dirk@749
  1565
}
dirk@749
  1566
dirk@763
  1567
- (void)startSync
dirk@763
  1568
{
dirk@763
  1569
    self.sendMessageDelegate = [PEPSessionTestSendMessageDelegate new];
dirk@763
  1570
    self.notifyHandshakeDelegate = [PEPSessionTestNotifyHandshakeDelegate new];
dirk@763
  1571
dirk@763
  1572
    self.sync = [[PEPSync alloc]
dirk@763
  1573
                 initWithSendMessageDelegate:self.sendMessageDelegate
dirk@763
  1574
                 notifyHandshakeDelegate:self.notifyHandshakeDelegate];
dirk@763
  1575
    [self.sync startup];
dirk@763
  1576
}
dirk@763
  1577
dz@1353
  1578
- (void)shutdownSync
dz@1013
  1579
{
dz@1013
  1580
    [self.sync shutdown];
dz@1013
  1581
}
dz@1013
  1582
dirk@653
  1583
- (NSNumber * _Nullable)testOutgoingRatingForMessage:(PEPMessage * _Nonnull)theMessage
dirk@653
  1584
                                             session:(PEPSession *)session
dirk@653
  1585
                                               error:(NSError * _Nullable * _Nullable)error
dirk@653
  1586
{
dirk@653
  1587
    NSNumber *ratingOriginal = [session outgoingRatingForMessage:theMessage error:error];
dirk@653
  1588
    NSNumber *ratingPreview = [session outgoingRatingPreviewForMessage:theMessage error:nil];
dirk@653
  1589
    XCTAssertEqual(ratingOriginal, ratingPreview);
dirk@653
  1590
    return ratingOriginal;
dirk@653
  1591
}
dirk@653
  1592
dirk@628
  1593
- (void)testPassiveModeEnabled:(BOOL)passiveModeEnabled
dirk@628
  1594
{
dirk@628
  1595
    [PEPObjCAdapter setPassiveModeEnabled:passiveModeEnabled];
dirk@628
  1596
    PEPSession *session = [PEPSession new];
dirk@628
  1597
dirk@628
  1598
    PEPIdentity *identMe = [[PEPIdentity alloc]
dirk@628
  1599
                            initWithAddress:@"me-myself-and-i@pep-project.org"
dirk@628
  1600
                            userID:@"me-myself-and-i"
dirk@628
  1601
                            userName:@"pEp Me"
dirk@628
  1602
                            isOwn:YES];
dirk@628
  1603
    NSError *error = nil;
dirk@628
  1604
    XCTAssertTrue([session mySelf:identMe error:&error]);
dirk@628
  1605
    XCTAssertNil(error);
dirk@628
  1606
dirk@628
  1607
    XCTAssertNotNil(identMe.fingerPrint);
dirk@628
  1608
dirk@628
  1609
    PEPIdentity *identAlice = [[PEPIdentity alloc]
dirk@628
  1610
                               initWithAddress:@"pep.test.alice@pep-project.org"
dirk@628
  1611
                               userID:@"alice_user_id"
dirk@628
  1612
                               userName:@"Alice"
dirk@628
  1613
                               isOwn:NO];
dirk@628
  1614
dirk@628
  1615
    NSString *shortMessage = @"whatever it may be";
dirk@628
  1616
    NSString *longMessage = [NSString stringWithFormat:@"%@ %@", shortMessage, shortMessage];
dirk@628
  1617
    PEPMessage *message = [PEPMessage new];
dz@964
  1618
    message.direction = PEPMsgDirectionOutgoing;
dirk@628
  1619
    message.from = identMe;
dirk@628
  1620
    message.to = @[identAlice];
dirk@628
  1621
    message.shortMessage = shortMessage;
dirk@628
  1622
    message.longMessage = longMessage;
dirk@628
  1623
dz@868
  1624
    PEPStatus status = PEPStatusKeyNotFound;
dirk@628
  1625
    PEPMessage *encryptedMessage =  [session encryptMessage:message
dirk@628
  1626
                                                  extraKeys:@[]
dirk@628
  1627
                                                     status:&status
dirk@628
  1628
                                                      error:&error];
dirk@628
  1629
    XCTAssertNil(error);
dz@961
  1630
    XCTAssertEqual(status, PEPStatusUnencrypted);
dirk@628
  1631
dirk@628
  1632
    if (passiveModeEnabled) {
dirk@628
  1633
        XCTAssertNil(encryptedMessage.attachments);
dirk@628
  1634
    } else {
dirk@628
  1635
        XCTAssertEqual(encryptedMessage.attachments.count, 1);
dirk@628
  1636
    }
dirk@628
  1637
}
dirk@628
  1638
dirk@529
  1639
/**
dirk@529
  1640
 Determines the rating for the given identity.
dz@868
  1641
 @return PEPRatingUndefined on error
dirk@529
  1642
 */
dz@868
  1643
- (PEPRating)ratingForIdentity:(PEPIdentity *)identity session:(PEPSession *)session
dirk@529
  1644
{
dirk@529
  1645
    NSError *error;
dirk@574
  1646
    NSNumber *numRating = [session ratingForIdentity:identity error:&error];
dirk@529
  1647
    XCTAssertNil(error);
dirk@574
  1648
    return numRating.pEpRating;
dirk@529
  1649
}
dirk@529
  1650
dirk@439
  1651
- (PEPIdentity *)checkImportingKeyFilePath:(NSString *)filePath address:(NSString *)address
dirk@439
  1652
                                    userID:(NSString *)userID
dirk@439
  1653
                               fingerPrint:(NSString *)fingerPrint
dirk@456
  1654
                                   session:(PEPSession *)session
dirk@437
  1655
{
dirk@456
  1656
    if (!session) {
dirk@456
  1657
        session = [PEPSession new];
dirk@456
  1658
    }
dirk@437
  1659
dirk@488
  1660
    BOOL success = [PEPTestUtils importBundledKey:filePath session:session];
dirk@488
  1661
    XCTAssertTrue(success);
dirk@437
  1662
dirk@488
  1663
    if (success) {
dirk@488
  1664
        // Our test user:
dirk@488
  1665
        PEPIdentity *identTest = [[PEPIdentity alloc]
dirk@488
  1666
                                  initWithAddress:address
dirk@488
  1667
                                  userID:userID
dirk@488
  1668
                                  userName:[NSString stringWithFormat:@"Some User Name %@", userID]
dirk@488
  1669
                                  isOwn:NO];
dirk@437
  1670
dirk@532
  1671
        NSError *error = nil;
dirk@532
  1672
        XCTAssertTrue([session updateIdentity:identTest error:&error]);
dirk@532
  1673
        XCTAssertNil(error);
dirk@488
  1674
        XCTAssertNotNil(identTest.fingerPrint);
dirk@488
  1675
        XCTAssertEqualObjects(identTest.fingerPrint, fingerPrint);
dirk@439
  1676
dirk@488
  1677
        return identTest;
dirk@488
  1678
    } else {
dirk@488
  1679
        return nil;
dirk@488
  1680
    }
dirk@439
  1681
}
dirk@439
  1682
andreas@1032
  1683
- (PEPIdentity *)checkMySelfImportingKeyFilePath:(NSString *)filePath
andreas@1032
  1684
                                         address:(NSString *)address
dirk@439
  1685
                                          userID:(NSString *)userID
dirk@439
  1686
                                     fingerPrint:(NSString *)fingerPrint
dirk@491
  1687
                                         session:(PEPSession *)session
dirk@439
  1688
{
dirk@494
  1689
    XCTAssertTrue([PEPTestUtils importBundledKey:filePath session:session]);
dirk@439
  1690
dirk@439
  1691
    // Our test user:
dirk@439
  1692
    PEPIdentity *identTest = [[PEPIdentity alloc]
dirk@439
  1693
                              initWithAddress:address
dirk@439
  1694
                              userID:userID
dirk@439
  1695
                              userName:[NSString stringWithFormat:@"Some User Name %@", userID]
dirk@439
  1696
                              isOwn:YES
dirk@439
  1697
                              fingerPrint: fingerPrint];
dirk@439
  1698
dirk@502
  1699
    NSError *error;
dirk@502
  1700
    XCTAssertTrue([session setOwnKey:identTest fingerprint:fingerPrint error:&error]);
dirk@502
  1701
    XCTAssertNil(error);
dirk@439
  1702
    XCTAssertNotNil(identTest.fingerPrint);
dirk@439
  1703
    XCTAssertEqualObjects(identTest.fingerPrint, fingerPrint);
dirk@439
  1704
dirk@439
  1705
    return identTest;
dirk@437
  1706
}
dirk@437
  1707
dirk@433
  1708
/**
dirk@433
  1709
 Verifies that a partner ID is really a correct Identity.
dirk@433
  1710
 Usually used on identities imported as keys, since the engine has problems with them.
dirk@433
  1711
 */
dirk@433
  1712
- (void)updateAndVerifyPartnerIdentity:(PEPIdentity *)partnerIdentity session:(PEPSession *)session
dirk@433
  1713
{
dirk@532
  1714
    NSError *error = nil;
dirk@532
  1715
dirk@433
  1716
    XCTAssertNotNil(partnerIdentity.fingerPrint);
dirk@532
  1717
    XCTAssertTrue([session updateIdentity:partnerIdentity error:&error]);
dirk@532
  1718
    XCTAssertNil(error);
dirk@433
  1719
    XCTAssertNotNil(partnerIdentity.fingerPrint);
dirk@433
  1720
    NSString *fingerprint = partnerIdentity.fingerPrint;
dirk@433
  1721
    partnerIdentity.fingerPrint = nil;
dirk@532
  1722
    XCTAssertTrue([session updateIdentity:partnerIdentity error:&error]);
dirk@532
  1723
    XCTAssertNil(error);
dirk@433
  1724
    XCTAssertNotNil(partnerIdentity.fingerPrint);
dirk@433
  1725
    XCTAssertEqualObjects(partnerIdentity.fingerPrint, fingerprint);
dirk@433
  1726
}
dirk@433
  1727
andreas@416
  1728
- (PEPMessage *)mailWrittenToMySelf
andreas@416
  1729
{
andreas@416
  1730
    PEPSession *session = [PEPSession new];
andreas@416
  1731
andreas@416
  1732
    // Write a e-mail to yourself ...
andreas@416
  1733
    PEPIdentity *me = [PEPTestUtils ownPepIdentityWithAddress:@"me@peptest.ch"
andreas@416
  1734
                                                     userName:@"userName"];
dirk@532
  1735
    NSError *error = nil;
dirk@532
  1736
    XCTAssertTrue([session mySelf:me error:&error]);
dirk@532
  1737
    XCTAssertNil(error);
andreas@416
  1738
andreas@416
  1739
    NSString *shortMessage = @"Subject";
andreas@416
  1740
    NSString *longMessage = @"Oh, this is a long body text!";
andreas@416
  1741
    PEPMessage *mail = [PEPTestUtils mailFrom:me
andreas@416
  1742
                                      toIdent:me
andreas@416
  1743
                                 shortMessage:shortMessage
andreas@416
  1744
                                  longMessage:longMessage
andreas@416
  1745
                                     outgoing:YES];
dz@926
  1746
    PEPStatus status = PEPStatusUnknownError;
dirk@556
  1747
    PEPMessage *encMessage = [session
dirk@556
  1748
                              encryptMessage:mail
dirk@557
  1749
                              forSelf:me
dirk@556
  1750
                              extraKeys:nil
dirk@556
  1751
                              status:&status
dirk@556
  1752
                              error:&error];
dirk@526
  1753
    XCTAssertNil(error);
andreas@416
  1754
andreas@416
  1755
    return encMessage;
andreas@416
  1756
}
andreas@416
  1757
andreas@392
  1758
- (PEPMessage *)internalEncryptToMySelfKeys:(PEPStringList **)keys
andreas@392
  1759
{
andreas@392
  1760
    PEPSession *session = [PEPSession new];
andreas@411
  1761
    
andreas@411
  1762
    PEPIdentity *me = [PEPTestUtils ownPepIdentityWithAddress:@"me@peptest.ch"
andreas@411
  1763
                                                     userName:@"userName"];
dirk@532
  1764
    NSError *error = nil;
dirk@532
  1765
    XCTAssertTrue([session mySelf:me error:&error]);
dirk@532
  1766
    XCTAssertNil(error);
dirk@532
  1767
andreas@392
  1768
    XCTAssertNotNil(me.fingerPrint);
andreas@392
  1769
andreas@392
  1770
    // Create draft
andreas@392
  1771
    NSString *shortMessage = @"Subject";
andreas@392
  1772
    NSString *longMessage = @"Oh, this is a long body text!";
andreas@392
  1773
    PEPMessage *mail = [PEPTestUtils mailFrom:me toIdent:me shortMessage:shortMessage longMessage:longMessage outgoing:YES];
andreas@392
  1774
dz@926
  1775
    PEPStatus status;
dirk@556
  1776
    PEPMessage *encMessage = [session
dirk@556
  1777
                              encryptMessage:mail
dirk@557
  1778
                              forSelf:me
dirk@556
  1779
                              extraKeys:nil
dirk@556
  1780
                              status:&status
dirk@556
  1781
                              error:&error];
andreas@392
  1782
    XCTAssertEqual(status, 0);
andreas@392
  1783
    XCTAssertEqualObjects(encMessage.shortMessage, @"p≡p");
andreas@392
  1784
dz@868
  1785
    PEPRating rating;
dirk@526
  1786
    error = nil;
dirk@517
  1787
    PEPMessage *unencDict = [session
dirk@517
  1788
                             decryptMessage:encMessage
dirk@583
  1789
                             flags:nil
dirk@517
  1790
                             rating:&rating
dirk@523
  1791
                             extraKeys:keys
dirk@524
  1792
                             status:nil
dirk@517
  1793
                             error:&error];
dirk@517
  1794
    XCTAssertNotNil(unencDict);
dirk@514
  1795
    XCTAssertNil(error);
dirk@514
  1796
dz@868
  1797
    XCTAssertGreaterThanOrEqual(rating, PEPRatingReliable);
andreas@392
  1798
andreas@392
  1799
    XCTAssertEqualObjects(unencDict.shortMessage, shortMessage);
andreas@392
  1800
    XCTAssertEqualObjects(unencDict.longMessage, longMessage);
andreas@392
  1801
andreas@392
  1802
    return unencDict;
andreas@392
  1803
}
andreas@392
  1804
andreas@392
  1805
- (void)pEpCleanUp
andreas@392
  1806
{
dirk@470
  1807
    [PEPTestUtils cleanUp];
andreas@392
  1808
}
andreas@392
  1809
dirk@424
  1810
- (void)helperXEncStatusForOutgoingEncryptdMailToSelf:(BOOL)toSelf
dz@868
  1811
                                       expectedRating:(PEPRating)expectedRating
dirk@422
  1812
{
dirk@422
  1813
    PEPSession *session = [PEPSession new];
dirk@422
  1814
dirk@422
  1815
    // Partner pubkey for the test:
dirk@422
  1816
    // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
dirk@422
  1817
    // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
dirk@617
  1818
    XCTAssertTrue([PEPTestUtils importBundledKey:@"6FF00E97.asc" session:session]);
dirk@422
  1819
dirk@422
  1820
    PEPIdentity *identAlice = [[PEPIdentity alloc]
dirk@422
  1821
                               initWithAddress:@"pep.test.alice@pep-project.org"
dirk@422
  1822
                               userID:ownUserId
dirk@422
  1823
                               userName:@"pEp Test Alice"
dirk@422
  1824
                               isOwn:NO
dirk@433
  1825
                               fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
dirk@433
  1826
    [self updateAndVerifyPartnerIdentity:identAlice session:session];
dirk@422
  1827
dirk@422
  1828
    PEPIdentity *identMe = [[PEPIdentity alloc]
dirk@422
  1829
                               initWithAddress:@"me-myself-and-i@pep-project.org"
dirk@422
  1830
                               userID:@"me-myself-and-i"
dirk@452
  1831
                               userName:@"pEp Me"
dirk@422
  1832
                               isOwn:YES];
dirk@532
  1833
    NSError *error = nil;
dirk@532
  1834
    XCTAssertTrue([session mySelf:identMe error:&error]);
dirk@532
  1835
    XCTAssertNil(error);
dirk@532
  1836
dirk@422
  1837
    XCTAssertNotNil(identMe.fingerPrint);
dirk@422
  1838
dirk@422
  1839
    PEPMessage *msg = [PEPMessage new];
dirk@422
  1840
    msg.from = identMe;
dirk@422
  1841
    msg.to = @[identAlice];
dirk@422
  1842
    msg.shortMessage = @"Mail to Alice";
dirk@422
  1843
    msg.longMessage = @"Alice?";
dz@964
  1844
    msg.direction = PEPMsgDirectionOutgoing;
dirk@422
  1845
dirk@653
  1846
    NSNumber *numRating = [self testOutgoingRatingForMessage:msg session:session error:&error];
dirk@573
  1847
    XCTAssertNotNil(numRating);
dirk@573
  1848
    XCTAssertNil(error);
dz@868
  1849
    XCTAssertEqual(numRating.pEpRating, PEPRatingReliable);
dirk@422
  1850
dirk@423
  1851
    PEPMessage *encMsg;
dirk@424
  1852
dz@926
  1853
    PEPStatus statusEnc = PEPStatusVersionMismatch;
dirk@424
  1854
    if (toSelf) {
dirk@556
  1855
        encMsg = [session
dirk@556
  1856
                  encryptMessage:msg
dirk@557
  1857
                  forSelf:identMe
dirk@556
  1858
                  extraKeys:nil
dirk@556
  1859
                  status:&statusEnc
dirk@556
  1860
                  error:&error];
dz@962
  1861
        XCTAssertEqual(statusEnc, PEPStatusOK);
dirk@424
  1862
    } else {
dirk@524
  1863
        encMsg = [session encryptMessage:msg extraKeys:nil status:nil error:&error];
dirk@522
  1864
        XCTAssertNotNil(encMsg);
dirk@522
  1865
        XCTAssertNil(error);
dirk@424
  1866
    }
dirk@423
  1867
    XCTAssertNotNil(encMsg);
dirk@423
  1868
dirk@423
  1869
    PEPStringList *keys;
dz@868
  1870
    PEPRating pEpRating;
dirk@526
  1871
    error = nil;
dirk@517
  1872
    PEPMessage *decMsg = [session
dirk@517
  1873
                          decryptMessage:encMsg
dirk@583
  1874
                          flags:nil
dirk@517
  1875
                          rating:&pEpRating
dirk@523
  1876
                          extraKeys:&keys
dirk@524
  1877
                          status:nil
dirk@517
  1878
                          error:&error];
dirk@514
  1879
    XCTAssertNil(error);
dirk@517
  1880
    XCTAssertNotNil(decMsg);
dirk@514
  1881
dirk@424
  1882
    XCTAssertEqual(pEpRating, expectedRating);
dirk@423
  1883
dirk@423
  1884
    NSArray * encStatusField = nil;
dirk@423
  1885
    for (NSArray *field in decMsg.optionalFields) {
dirk@423
  1886
        NSString *header = [field[0] lowercaseString];
dirk@423
  1887
        if ([header isEqualToString:@"x-encstatus"]) {
dirk@423
  1888
            encStatusField = field;
dirk@423
  1889
        }
dirk@423
  1890
    }
dirk@423
  1891
    XCTAssertNotNil(encStatusField);
dirk@423
  1892
    if (encStatusField) {
dz@868
  1893
        PEPRating outgoingRating = [session ratingFromString:encStatusField[1]];
dirk@424
  1894
        XCTAssertEqual(outgoingRating, expectedRating);
dirk@423
  1895
    }
dirk@422
  1896
}
dirk@422
  1897
andreas@392
  1898
@end