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