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