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