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