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