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