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