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