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