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