pEpObjCTests/PEPSessionTest.m
author buff <andreas@pep-project.org>
Mon, 29 Jan 2018 10:46:40 +0100
changeset 411 ae155bbb8fb4
parent 393 6f8fa4c7b897
child 416 5a37145e3eca
permissions -rw-r--r--
fixes: test uses identities without user_id
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@392
    27
    [self pEpCleanUp];
andreas@392
    28
}
andreas@392
    29
andreas@392
    30
- (void)tearDown {
andreas@392
    31
    [self pEpCleanUp];
andreas@392
    32
    [super tearDown];
andreas@392
    33
}
andreas@392
    34
andreas@392
    35
- (void)testSyncSession
andreas@392
    36
{
andreas@392
    37
    PEPSession *session = [PEPSession new];
andreas@392
    38
    PEPTestSyncDelegate *syncDelegate = [[PEPTestSyncDelegate alloc] init];
andreas@392
    39
andreas@392
    40
    // This should attach session just created
andreas@392
    41
    [PEPObjCAdapter startSync:syncDelegate];
andreas@392
    42
andreas@392
    43
    PEPIdentity *identMe = [[PEPIdentity alloc]
andreas@392
    44
                            initWithAddress:@"pep.test.iosgenkey@pep-project.org"
andreas@392
    45
                            userID:@"Me"
andreas@392
    46
                            userName:@"pEp Test iOS GenKey"
andreas@392
    47
                            isOwn:YES];
andreas@392
    48
andreas@392
    49
    [session mySelf:identMe];
andreas@392
    50
andreas@392
    51
    bool res = [syncDelegate waitUntilSent:1];
andreas@392
    52
andreas@392
    53
    // Can't currently work, engine doesn't contain sync.
andreas@392
    54
    XCTAssertFalse(res);
andreas@392
    55
andreas@392
    56
    // This should detach session just created
andreas@392
    57
    [PEPObjCAdapter stopSync];
andreas@392
    58
}
andreas@392
    59
andreas@392
    60
- (void)testTrustWords
andreas@392
    61
{
andreas@392
    62
    PEPSession *session = [PEPSession new];
andreas@392
    63
andreas@392
    64
    NSArray *trustwords = [session trustwords:@"DB47DB47DB47DB47DB47DB47DB47DB47DB47DB47"
andreas@392
    65
                                  forLanguage:@"en" shortened:false];
andreas@392
    66
    XCTAssertEqual([trustwords count], 10);
andreas@392
    67
andreas@392
    68
    for(id word in trustwords)
andreas@392
    69
        XCTAssertEqualObjects(word, @"BAPTISMAL");
andreas@392
    70
}
andreas@392
    71
andreas@392
    72
- (void)testGenKey
andreas@392
    73
{
andreas@392
    74
    PEPSession *session = [PEPSession new];
andreas@392
    75
andreas@392
    76
    PEPIdentity *identMe = [[PEPIdentity alloc]
andreas@392
    77
                            initWithAddress:@"pep.test.iosgenkey@pep-project.org"
andreas@392
    78
                            userID:@"Me"
andreas@392
    79
                            userName:@"pEp Test iOS GenKey"
andreas@392
    80
                            isOwn:YES];
andreas@392
    81
andreas@392
    82
    [session mySelf:identMe];
andreas@392
    83
andreas@392
    84
    XCTAssertNotNil(identMe.fingerPrint);
andreas@392
    85
    XCTAssertNotEqual(identMe.commType, PEP_ct_unknown);
andreas@392
    86
andreas@392
    87
    // check that the comm type is not a PGP one
andreas@392
    88
    XCTAssertFalse([identMe containsPGPCommType]);
andreas@392
    89
}
andreas@392
    90
andreas@392
    91
- (void)testMySelfCommType
andreas@392
    92
{
andreas@392
    93
    PEPSession *session = [PEPSession new];
andreas@392
    94
andreas@392
    95
    PEPIdentity *identMe = [[PEPIdentity alloc]
andreas@392
    96
                            initWithAddress:@"pep.test.iosgenkey@pep-project.org"
andreas@392
    97
                            userID:@"Me"
andreas@392
    98
                            userName:@"pEp Test iOS GenKey"
andreas@392
    99
                            isOwn:YES];
andreas@392
   100
andreas@392
   101
    [session mySelf:identMe];
andreas@392
   102
andreas@392
   103
    XCTAssertNotNil(identMe.fingerPrint);
andreas@392
   104
    XCTAssertNotEqual(identMe.commType, PEP_ct_unknown);
andreas@392
   105
andreas@392
   106
    // check that the comm type is not a PGP one
andreas@392
   107
    XCTAssertFalse([identMe containsPGPCommType]);
andreas@392
   108
andreas@392
   109
    dispatch_queue_t queue = dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0);
andreas@392
   110
    dispatch_sync(queue, ^{
andreas@392
   111
        PEPSession *session2 = [PEPSession new];
andreas@392
   112
andreas@392
   113
        // Now simulate an update from the app, which usually only caches
andreas@392
   114
        // kPepUsername, kPepAddress and optionally kPepUserID.
andreas@392
   115
        PEPIdentity *identMe2 = [[PEPIdentity alloc]
andreas@392
   116
                                 initWithAddress:identMe.address
andreas@392
   117
                                 userID:identMe.userID
andreas@392
   118
                                 userName:identMe.userName
andreas@392
   119
                                 isOwn:NO];
andreas@392
   120
        [session2 mySelf:identMe2];
andreas@392
   121
        XCTAssertNotNil(identMe2.fingerPrint);
andreas@392
   122
        XCTAssertFalse([identMe2 containsPGPCommType]);
andreas@392
   123
        XCTAssertEqualObjects(identMe2.fingerPrint, identMe.fingerPrint);
andreas@392
   124
andreas@392
   125
        // Now pretend the app only knows kPepUsername and kPepAddress
andreas@411
   126
        PEPIdentity *identMe3 = [PEPTestUtils foreignPepIdentityWithAddress:identMe.address
andreas@411
   127
                                                                   userName:identMe.userName];
andreas@392
   128
        [session2 mySelf:identMe3];
andreas@392
   129
        XCTAssertNotNil(identMe3.fingerPrint);
andreas@392
   130
        XCTAssertFalse([identMe3 containsPGPCommType]);
andreas@392
   131
        XCTAssertEqualObjects(identMe3.fingerPrint, identMe.fingerPrint);
andreas@392
   132
andreas@392
   133
        XCTAssertEqualObjects(identMe.address, identMe2.address);
andreas@392
   134
        XCTAssertEqualObjects(identMe.address, identMe3.address);
andreas@392
   135
        XCTAssertEqual(identMe.commType, identMe2.commType);
andreas@392
   136
        XCTAssertEqual(identMe.commType, identMe3.commType);
andreas@392
   137
    });
andreas@392
   138
}
andreas@392
   139
andreas@392
   140
- (void)testOutgoingColors
andreas@392
   141
{
andreas@392
   142
    PEPSession *session = [PEPSession new];
andreas@392
   143
andreas@392
   144
    // Our test user :
andreas@392
   145
    // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
andreas@392
   146
    // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
andreas@392
   147
    [PEPTestUtils importBundledKey:@"6FF00E97_sec.asc"];
andreas@392
   148
andreas@392
   149
    // Our test user :
andreas@392
   150
    PEPIdentity *identAlice = [[PEPIdentity alloc]
andreas@392
   151
                               initWithAddress:@"pep.test.alice@pep-project.org"
andreas@392
   152
                               userID:ownUserId
andreas@392
   153
                               userName:@"pEp Test Alice"
andreas@392
   154
                               isOwn:YES
andreas@392
   155
                               fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
andreas@392
   156
andreas@392
   157
    [session mySelf:identAlice];
andreas@392
   158
andreas@392
   159
    //Message
andreas@392
   160
andreas@392
   161
    PEPMessage *msg = [PEPMessage new];
andreas@392
   162
    msg.from = identAlice;
andreas@392
   163
    msg.to = @[[[PEPIdentity alloc] initWithAddress:@"pep.test.bob@pep-project.org"
andreas@392
   164
                                             userID: @"42"
andreas@392
   165
                                           userName:@"pEp Test Bob"
andreas@392
   166
                                              isOwn:NO]];
andreas@392
   167
    msg.shortMessage = @"All Green Test";
andreas@392
   168
    msg.longMessage = @"This is a text content";
andreas@392
   169
    msg.direction = PEP_dir_outgoing;
andreas@392
   170
andreas@392
   171
    // Test with unknown Bob
andreas@392
   172
    PEP_rating clr = [session outgoingColorForMessage:msg];
andreas@392
   173
    XCTAssert( clr == PEP_rating_unencrypted);
andreas@392
   174
andreas@392
   175
    // Now let see with bob's pubkey already known
andreas@392
   176
    // pEp Test Bob (test key, don't use) <pep.test.bob@pep-project.org>
andreas@392
   177
    // BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39
andreas@392
   178
    [PEPTestUtils importBundledKey:@"0xC9C2EE39.asc"];
andreas@392
   179
andreas@392
   180
    PEPIdentity *identBob = [[PEPIdentity alloc]
andreas@392
   181
                             initWithAddress:@"pep.test.bob@pep-project.org"
andreas@392
   182
                             userID:@"42" userName:@"pEp Test Bob"
andreas@392
   183
                             isOwn:NO
andreas@392
   184
                             fingerPrint:@"BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39"];
andreas@392
   185
andreas@392
   186
    [session updateIdentity:identBob];
andreas@392
   187
andreas@392
   188
    // Should be yellow, since no handshake happened.
andreas@392
   189
    clr = [session outgoingColorForMessage:msg];
andreas@392
   190
    XCTAssert( clr == PEP_rating_reliable);
andreas@392
   191
andreas@392
   192
    clr = [session identityRating:identBob];
andreas@392
   193
    XCTAssert( clr == PEP_rating_reliable);
andreas@392
   194
andreas@392
   195
    // Let' say we got that handshake, set PEP_ct_confirmed in Bob's identity
andreas@392
   196
    [session trustPersonalKey:identBob];
andreas@392
   197
andreas@392
   198
    // This time it should be green
andreas@392
   199
    clr = [session outgoingColorForMessage:msg];
andreas@392
   200
    XCTAssert( clr == PEP_rating_trusted);
andreas@392
   201
andreas@392
   202
    clr = [session identityRating:identBob];
andreas@392
   203
    XCTAssert( clr == PEP_rating_trusted);
andreas@392
   204
andreas@392
   205
    // Let' say we undo handshake
andreas@392
   206
    [session keyResetTrust:identBob];
andreas@392
   207
andreas@392
   208
    // Yellow ?
andreas@392
   209
    clr = [session outgoingColorForMessage:msg];
andreas@392
   210
    XCTAssert( clr == PEP_rating_reliable);
andreas@392
   211
andreas@392
   212
    // mistrust Bob
andreas@392
   213
    [session keyMistrusted:identBob];
andreas@392
   214
andreas@392
   215
    // Gray == PEP_rating_unencrypted
andreas@392
   216
    clr = [session outgoingColorForMessage:msg];
andreas@392
   217
    XCTAssert( clr == PEP_rating_unencrypted);
andreas@392
   218
andreas@392
   219
    // Forget
andreas@392
   220
    [session keyResetTrust:identBob];
andreas@392
   221
andreas@392
   222
    // Back to yellow
andreas@392
   223
    clr = [session outgoingColorForMessage:msg];
andreas@392
   224
    XCTAssert( clr == PEP_rating_reliable);
andreas@392
   225
andreas@392
   226
    // Trust again
andreas@392
   227
    [session trustPersonalKey:identBob];
andreas@392
   228
andreas@392
   229
    // Back to green
andreas@392
   230
    clr = [session outgoingColorForMessage:msg];
andreas@392
   231
    XCTAssert( clr == PEP_rating_trusted);
andreas@392
   232
andreas@392
   233
    // Now let see if it turns back yellow if we add an unconfirmed folk.
andreas@392
   234
    // pEp Test John (test key, don't use) <pep.test.john@pep-project.org>
andreas@392
   235
    // AA2E4BEB93E5FE33DEFD8BE1135CD6D170DCF575
andreas@392
   236
    [PEPTestUtils importBundledKey:@"0x70DCF575.asc"];
andreas@392
   237
andreas@392
   238
    PEPIdentity *identJohn = [[PEPIdentity alloc]
andreas@392
   239
                              initWithAddress:@"pep.test.john@pep-project.org"
andreas@392
   240
                              userID:@"101" userName:@"pEp Test John"
andreas@392
   241
                              isOwn:NO
andreas@392
   242
                              fingerPrint:@"AA2E4BEB93E5FE33DEFD8BE1135CD6D170DCF575"];
andreas@392
   243
andreas@392
   244
    [session updateIdentity:identJohn];
andreas@392
   245
andreas@411
   246
    msg.cc = @[[PEPTestUtils foreignPepIdentityWithAddress:@"pep.test.john@pep-project.org"
andreas@411
   247
                                                  userName:@"pEp Test John"]];
andreas@392
   248
    // Yellow ?
andreas@392
   249
    clr = [session outgoingColorForMessage:msg];
andreas@392
   250
    XCTAssert( clr == PEP_rating_reliable);
andreas@392
   251
andreas@392
   252
    PEPMessage *encmsg;
andreas@392
   253
    PEP_STATUS status = [session encryptMessage:msg extra:@[] dest:&encmsg];
andreas@392
   254
andreas@392
   255
    XCTAssertNotNil(encmsg);
andreas@392
   256
    XCTAssertEqualObjects(encmsg.shortMessage, @"p≡p");
andreas@392
   257
    XCTAssertTrue([encmsg.longMessage containsString:@"p≡p"]);
andreas@392
   258
andreas@392
   259
    XCTAssert(status == PEP_STATUS_OK);
andreas@392
   260
}
andreas@392
   261
andreas@392
   262
andreas@392
   263
- (void)testOutgoingBccColors
andreas@392
   264
{
andreas@392
   265
    PEPSession *session = [PEPSession new];
andreas@392
   266
andreas@392
   267
    // Our test user :
andreas@392
   268
    // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
andreas@392
   269
    // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
andreas@392
   270
    [PEPTestUtils importBundledKey:@"6FF00E97_sec.asc"];
andreas@392
   271
andreas@392
   272
    PEPIdentity *identAlice = [[PEPIdentity alloc]
andreas@392
   273
                               initWithAddress:@"pep.test.alice@pep-project.org"
andreas@392
   274
                               userID:ownUserId
andreas@392
   275
                               userName:@"pEp Test Alice"
andreas@392
   276
                               isOwn:YES
andreas@392
   277
                               fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
andreas@392
   278
andreas@392
   279
    [session mySelf:identAlice];
andreas@392
   280
andreas@392
   281
    PEPMessage *msg = [PEPMessage new];
andreas@392
   282
    msg.from = identAlice;
andreas@392
   283
    msg.to = @[[[PEPIdentity alloc] initWithAddress:@"pep.test.bob@pep-project.org"
andreas@392
   284
                                             userID:@"42" userName:@"pEp Test Bob" isOwn:NO]];
andreas@392
   285
    msg.shortMessage = @"All Green Test";
andreas@392
   286
    msg.longMessage = @"This is a text content";
andreas@392
   287
    msg.direction = PEP_dir_outgoing;
andreas@392
   288
andreas@392
   289
    // Test with unknown Bob
andreas@392
   290
    PEP_rating clr = [session outgoingColorForMessage:msg];
andreas@392
   291
    XCTAssert( clr == PEP_rating_unencrypted);
andreas@392
   292
andreas@392
   293
    // Now let see with bob's pubkey already known
andreas@392
   294
    // pEp Test Bob (test key, don't use) <pep.test.bob@pep-project.org>
andreas@392
   295
    // BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39
andreas@392
   296
    [PEPTestUtils importBundledKey:@"0xC9C2EE39.asc"];
andreas@392
   297
andreas@392
   298
    PEPIdentity *identBob = [[PEPIdentity alloc]
andreas@392
   299
                             initWithAddress:@"pep.test.bob@pep-project.org"
andreas@392
   300
                             userID:@"42" userName:@"pEp Test Bob"
andreas@392
   301
                             isOwn:NO
andreas@392
   302
                             fingerPrint:@"BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39"];
andreas@392
   303
andreas@392
   304
    [session updateIdentity:identBob];
andreas@392
   305
andreas@392
   306
    // Should be yellow, since no handshake happened.
andreas@392
   307
    clr = [session outgoingColorForMessage:msg];
andreas@392
   308
    XCTAssert( clr == PEP_rating_reliable);
andreas@392
   309
andreas@392
   310
    clr = [session identityRating:identBob];
andreas@392
   311
    XCTAssert( clr == PEP_rating_reliable);
andreas@392
   312
andreas@392
   313
    // Let' say we got that handshake, set PEP_ct_confirmed in Bob's identity
andreas@392
   314
    [session trustPersonalKey:identBob];
andreas@392
   315
andreas@392
   316
    // This time it should be green
andreas@392
   317
    clr = [session outgoingColorForMessage:msg];
andreas@392
   318
    XCTAssert( clr == PEP_rating_trusted);
andreas@392
   319
andreas@392
   320
    clr = [session identityRating:identBob];
andreas@392
   321
    XCTAssert( clr == PEP_rating_trusted);
andreas@392
   322
andreas@392
   323
    // Now let see if it turns back yellow if we add an unconfirmed folk.
andreas@392
   324
    // pEp Test John (test key, don't use) <pep.test.john@pep-project.org>
andreas@392
   325
    // AA2E4BEB93E5FE33DEFD8BE1135CD6D170DCF575
andreas@392
   326
    [PEPTestUtils importBundledKey:@"0x70DCF575.asc"];
andreas@392
   327
andreas@392
   328
    PEPIdentity *identJohn = [[PEPIdentity alloc]
andreas@392
   329
                              initWithAddress:@"pep.test.john@pep-project.org"
andreas@392
   330
                              userID:@"101" userName:@"pEp Test John"
andreas@392
   331
                              isOwn:NO
andreas@392
   332
                              fingerPrint:@"AA2E4BEB93E5FE33DEFD8BE1135CD6D170DCF575"];
andreas@392
   333
andreas@392
   334
    [session updateIdentity:identJohn];
andreas@392
   335
andreas@392
   336
    msg.bcc = @[[[PEPIdentity alloc] initWithAddress:@"pep.test.john@pep-project.org"
andreas@392
   337
                                              userID:@"101" userName:@"pEp Test John" isOwn:NO]];
andreas@392
   338
andreas@392
   339
    // Yellow ?
andreas@392
   340
    clr = [session outgoingColorForMessage:msg];
andreas@392
   341
    XCTAssert( clr == PEP_rating_reliable);
andreas@392
   342
andreas@392
   343
    [session trustPersonalKey:identJohn];
andreas@392
   344
andreas@392
   345
    // This time it should be green
andreas@392
   346
    clr = [session outgoingColorForMessage:msg];
andreas@392
   347
    XCTAssert( clr == PEP_rating_trusted);
andreas@392
   348
andreas@392
   349
    clr = [session identityRating:identJohn];
andreas@392
   350
    XCTAssert( clr == PEP_rating_trusted);
andreas@392
   351
}
andreas@392
   352
andreas@392
   353
- (void)testDontEncryptForMistrusted
andreas@392
   354
{
andreas@392
   355
    PEPSession *session = [PEPSession new];
andreas@392
   356
andreas@392
   357
    // Our test user :
andreas@392
   358
    // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
andreas@392
   359
    // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
andreas@392
   360
    [PEPTestUtils importBundledKey:@"6FF00E97_sec.asc"];
andreas@392
   361
andreas@392
   362
    PEPIdentity *identAlice = [[PEPIdentity alloc]
andreas@392
   363
                               initWithAddress:@"pep.test.alice@pep-project.org"
andreas@392
   364
                               userID:ownUserId
andreas@392
   365
                               userName:@"pEp Test Alice"
andreas@392
   366
                               isOwn:YES
andreas@392
   367
                               fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
andreas@392
   368
andreas@392
   369
    [session mySelf:identAlice];
andreas@392
   370
andreas@392
   371
    // pEp Test Bob (test key, don't use) <pep.test.bob@pep-project.org>
andreas@392
   372
    // BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39
andreas@392
   373
    [PEPTestUtils importBundledKey:@"0xC9C2EE39.asc"];
andreas@392
   374
andreas@392
   375
    PEPIdentity *identBob = [[PEPIdentity alloc]
andreas@392
   376
                             initWithAddress:@"pep.test.bob@pep-project.org"
andreas@392
   377
                             userID:@"42" userName:@"pEp Test Bob"
andreas@392
   378
                             isOwn:NO
andreas@392
   379
                             fingerPrint:@"BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39"];
andreas@392
   380
andreas@392
   381
    [session updateIdentity:identBob];
andreas@392
   382
andreas@392
   383
    // mistrust Bob
andreas@392
   384
    [session keyMistrusted:identBob];
andreas@392
   385
andreas@392
   386
    PEPMessage *msg = [PEPMessage new];
andreas@392
   387
    msg.from = identAlice;
andreas@392
   388
    msg.to = @[[[PEPIdentity alloc] initWithAddress:@"pep.test.bob@pep-project.org" userID:@"42"
andreas@392
   389
                                           userName:@"pEp Test Bob" isOwn:NO]];
andreas@392
   390
    msg.shortMessage = @"All Green Test";
andreas@392
   391
    msg.longMessage = @"This is a text content";
andreas@392
   392
    msg.direction = PEP_dir_outgoing;
andreas@392
   393
andreas@392
   394
    // Gray == PEP_rating_unencrypted
andreas@392
   395
    PEP_rating clr = [session outgoingColorForMessage:msg];
andreas@392
   396
    XCTAssert( clr == PEP_rating_unencrypted);
andreas@392
   397
andreas@392
   398
    PEPMessage *encmsg;
andreas@392
   399
    PEP_STATUS status = [session encryptMessage:msg extra:@[] dest:&encmsg];
andreas@392
   400
andreas@392
   401
    XCTAssert(status == PEP_UNENCRYPTED);
andreas@392
   402
andreas@392
   403
    XCTAssertNotEqualObjects(encmsg.attachments[0][@"mimeType"], @"application/pgp-encrypted");
andreas@392
   404
andreas@392
   405
    [self pEpCleanUp];
andreas@392
   406
}
andreas@392
   407
andreas@392
   408
- (void)testRenewExpired
andreas@392
   409
{
andreas@392
   410
    PEPSession *session = [PEPSession new];
andreas@392
   411
andreas@392
   412
    // Our expired test user :
andreas@392
   413
    // pEp Test Hector (old test key don't use) <pep.test.hector@pep-project.org>
andreas@392
   414
    [PEPTestUtils importBundledKey:@"5CB2C182_sec.asc"];
andreas@392
   415
andreas@392
   416
    PEPIdentity *identHector = [[PEPIdentity alloc]
andreas@392
   417
                                initWithAddress:@"pep.test.hector@pep-project.org"
andreas@392
   418
                                userID:@"fc2d33" userName:@"pEp Test Hector"
andreas@392
   419
                                isOwn:NO
andreas@392
   420
                                fingerPrint:@"EEA655839E347EC9E10A5DE2E80CB3FD5CB2C182"];
andreas@392
   421
andreas@392
   422
    // Check that this key is indeed expired
andreas@392
   423
    [session updateIdentity:identHector];
andreas@392
   424
    XCTAssertEqual(PEP_ct_key_expired, identHector.commType);
andreas@392
   425
andreas@392
   426
    PEPIdentity *identHectorOwn = [[PEPIdentity alloc]
andreas@392
   427
                                   initWithAddress:@"pep.test.hector@pep-project.org"
andreas@392
   428
                                   userID:ownUserId userName:@"pEp Test Hector"
andreas@392
   429
                                   isOwn:YES
andreas@392
   430
                                   fingerPrint:@"EEA655839E347EC9E10A5DE2E80CB3FD5CB2C182"];
andreas@392
   431
andreas@392
   432
    // Myself automatically renew expired key.
andreas@392
   433
    [session mySelf:identHectorOwn];
andreas@392
   434
    XCTAssertEqual(PEP_ct_pEp, identHectorOwn.commType);
andreas@392
   435
andreas@392
   436
    [self pEpCleanUpRestoringBackupNamed:@"Bob"];
andreas@392
   437
andreas@392
   438
andreas@392
   439
    [self pEpSetUp:@"Bob"];
andreas@392
   440
andreas@392
   441
    PEPIdentity *_identHector = [[PEPIdentity alloc]
andreas@392
   442
                                 initWithAddress:@"pep.test.hector@pep-project.org"
andreas@392
   443
                                 userID:@"khkhkh" userName:@"pEp Test Hector"
andreas@392
   444
                                 isOwn:NO
andreas@392
   445
                                 fingerPrint:@"EEA655839E347EC9E10A5DE2E80CB3FD5CB2C182"];
andreas@392
   446
andreas@392
   447
    [session updateIdentity:_identHector];
andreas@392
   448
    XCTAssertEqual(PEP_ct_OpenPGP_unconfirmed, _identHector.commType);
andreas@392
   449
}
andreas@392
   450
andreas@392
   451
- (void)testRevoke
andreas@392
   452
{
andreas@392
   453
    PEPSession *session = [PEPSession new];
andreas@392
   454
andreas@392
   455
    // Our test user :
andreas@392
   456
    // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
andreas@392
   457
    // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
andreas@392
   458
    [PEPTestUtils importBundledKey:@"6FF00E97_sec.asc"];
andreas@392
   459
    NSString *fpr = @"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97";
andreas@392
   460
andreas@392
   461
    PEPIdentity *identAlice = [[PEPIdentity alloc]
andreas@392
   462
                               initWithAddress:@"pep.test.alice@pep-project.org"
andreas@392
   463
                               userID:ownUserId
andreas@392
   464
                               userName:@"pEp Test Alice"
andreas@392
   465
                               isOwn:YES
andreas@392
   466
                               fingerPrint:fpr];
andreas@392
   467
andreas@392
   468
    [session mySelf:identAlice];
andreas@392
   469
andreas@392
   470
    PEPIdentity *identAlice2 = [identAlice mutableCopy];
andreas@392
   471
andreas@392
   472
    // This will revoke key
andreas@392
   473
    [session keyMistrusted:identAlice2];
andreas@392
   474
andreas@392
   475
    // Check fingerprint is different
andreas@392
   476
    XCTAssertNotEqualObjects(identAlice2.fingerPrint, fpr);
andreas@392
   477
}
andreas@392
   478
andreas@392
   479
- (void)testMailToMyself
andreas@392
   480
{
andreas@392
   481
    PEPSession *session = [PEPSession new];
andreas@392
   482
andreas@392
   483
    // Our test user :
andreas@392
   484
    // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
andreas@392
   485
    // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
andreas@392
   486
    [PEPTestUtils importBundledKey:@"6FF00E97_sec.asc"];
andreas@392
   487
andreas@392
   488
    PEPIdentity *identAlice = [[PEPIdentity alloc]
andreas@392
   489
                               initWithAddress:@"pep.test.alice@pep-project.org"
andreas@392
   490
                               userID:ownUserId
andreas@392
   491
                               userName:@"pEp Test Alice"
andreas@392
   492
                               isOwn:YES
andreas@392
   493
                               fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
andreas@392
   494
andreas@392
   495
    [session mySelf:identAlice];
andreas@392
   496
andreas@392
   497
    PEPMessage *msg = [PEPMessage new];
andreas@392
   498
    msg.from = identAlice;
andreas@392
   499
    msg.to = @[identAlice];
andreas@392
   500
    msg.shortMessage = @"Mail to Myself";
andreas@392
   501
    msg.longMessage = @"This is a text content";
andreas@392
   502
    msg.direction = PEP_dir_outgoing;
andreas@392
   503
andreas@392
   504
    // Test with unknown Bob
andreas@392
   505
    PEP_rating clr = [session outgoingColorForMessage:msg];
andreas@392
   506
    XCTAssert( clr == PEP_rating_trusted_and_anonymized);
andreas@392
   507
andreas@392
   508
    PEPMessage *encmsg;
andreas@392
   509
    PEP_STATUS status = [session encryptMessage:msg extra:@[] dest:&encmsg];
andreas@392
   510
andreas@392
   511
    XCTAssert(status == PEP_STATUS_OK);
andreas@392
   512
andreas@392
   513
    NSArray* keys;
andreas@392
   514
    PEPMessage *decmsg;
andreas@392
   515
andreas@392
   516
    clr = [session decryptMessage:encmsg dest:&decmsg keys:&keys];
andreas@392
   517
    XCTAssert( clr == PEP_rating_trusted_and_anonymized);
andreas@392
   518
}
andreas@392
   519
andreas@392
   520
- (void)testEncryptedMailFromMutt
andreas@392
   521
{
andreas@392
   522
    PEPSession *session = [PEPSession new];
andreas@392
   523
andreas@392
   524
    // This is the public key for test001@peptest.ch
andreas@392
   525
    [PEPTestUtils importBundledKey:@"A3FC7F0A.asc"];
andreas@392
   526
andreas@392
   527
    // This is the secret key for test001@peptest.ch
andreas@392
   528
    [PEPTestUtils importBundledKey:@"A3FC7F0A_sec.asc"];
andreas@392
   529
andreas@392
   530
    // Mail from mutt, already processed into message dict by the app.
andreas@392
   531
    NSMutableDictionary *msgDict = [PEPTestUtils unarchiveDictionary:@"msg_to_A3FC7F0A_from_mutt.ser"].mutableCopy;
andreas@392
   532
    [msgDict removeObjectForKey:kPepLongMessage];
andreas@392
   533
    [msgDict removeObjectForKey:kPepLongMessageFormatted];
andreas@392
   534
andreas@392
   535
    // Also extracted "live" from the app.
andreas@392
   536
    NSMutableDictionary *accountDict = [PEPTestUtils unarchiveDictionary:@"account_A3FC7F0A.ser"].mutableCopy;
andreas@392
   537
    [accountDict removeObjectForKey:kPepCommType];
andreas@392
   538
    [accountDict removeObjectForKey:kPepFingerprint];
andreas@392
   539
    PEPIdentity *identMe = [[PEPIdentity alloc] initWithDictionary:accountDict];
andreas@392
   540
andreas@392
   541
    [session mySelf:identMe];
andreas@392
   542
    XCTAssertNotNil(identMe.fingerPrint);
andreas@392
   543
andreas@392
   544
    NSArray* keys;
andreas@392
   545
    PEPMessage *msg = [PEPMessage new];
andreas@392
   546
    [msg setValuesForKeysWithDictionary:msgDict];
andreas@392
   547
    PEPMessage *pepDecryptedMail;
andreas@392
   548
    [session decryptMessage:msg dest:&pepDecryptedMail keys:&keys];
andreas@392
   549
    XCTAssertNotNil(pepDecryptedMail.longMessage);
andreas@392
   550
}
andreas@392
   551
andreas@392
   552
- (void)testOutgoingContactColor
andreas@392
   553
{
andreas@392
   554
    PEPSession *session = [PEPSession new];
andreas@392
   555
andreas@411
   556
    PEPIdentity *partner1Orig = [PEPTestUtils foreignPepIdentityWithAddress:@"partner1@dontcare.me"
andreas@411
   557
                                                                   userName:@"Partner 1"];
andreas@392
   558
    NSString *pubKeyPartner1 = [PEPTestUtils loadResourceByName:@"partner1_F2D281C2789DD7F6_pub.asc"];
andreas@392
   559
    XCTAssertNotNil(pubKeyPartner1);
andreas@392
   560
    [session importKey:pubKeyPartner1];
andreas@392
   561
andreas@392
   562
    PEP_rating color = [session identityRating:partner1Orig];
andreas@392
   563
    XCTAssertEqual(color, PEP_rating_reliable);
andreas@392
   564
}
andreas@392
   565
andreas@392
   566
- (void)testMessageTrustwordsWithMySelf
andreas@392
   567
{
andreas@392
   568
    PEPSession *session = [PEPSession new];
andreas@392
   569
andreas@392
   570
    PEPStringList *keys = nil;
andreas@392
   571
    PEPMessage *decryptedDict = [self internalEncryptToMySelfKeys:&keys];
andreas@392
   572
    XCTAssertNotNil(keys);
andreas@392
   573
    XCTAssert(keys.count > 0);
andreas@392
   574
andreas@392
   575
    PEPIdentity *receiver = decryptedDict.to[0];
andreas@392
   576
    [session updateIdentity:receiver];
andreas@392
   577
    XCTAssertNotNil(receiver);
andreas@392
   578
    PEP_STATUS trustwordsStatus;
andreas@392
   579
andreas@392
   580
    NSString *trustwords = [session getTrustwordsForMessage:decryptedDict
andreas@392
   581
                                                   receiver:receiver
andreas@392
   582
                                                  keysArray:keys language:@"en"
andreas@392
   583
                                                       full:YES
andreas@392
   584
                                            resultingStatus: &trustwordsStatus];
andreas@392
   585
    // No trustwords with yourself
andreas@392
   586
    XCTAssertEqual(trustwordsStatus, PEP_TRUSTWORDS_DUPLICATE_FPR);
andreas@392
   587
    XCTAssertNil(trustwords);
andreas@392
   588
}
andreas@392
   589
andreas@392
   590
- (void)testGetTrustwords
andreas@392
   591
{
andreas@392
   592
    PEPSession *session = [PEPSession new];
andreas@392
   593
andreas@392
   594
    PEPIdentity *partner1Orig = [[PEPIdentity alloc]
andreas@392
   595
                                 initWithAddress:@"partner1@dontcare.me" userID:@"partner1"
andreas@392
   596
                                 userName:@"partner1"
andreas@392
   597
                                 isOwn:NO fingerPrint:@"F0CD3F7B422E5D587ABD885BF2D281C2789DD7F6"];
andreas@392
   598
andreas@392
   599
    PEPIdentity *meOrig = [[PEPIdentity alloc]
andreas@392
   600
                           initWithAddress:@"me@dontcare.me" userID:@"me"
andreas@392
   601
                           userName:@"me"
andreas@392
   602
                           isOwn:NO fingerPrint:@"CC1F73F6FB774BF08B197691E3BFBCA9248FC681"];
andreas@392
   603
andreas@392
   604
    NSString *pubKeyPartner1 = [PEPTestUtils loadResourceByName:@"partner1_F2D281C2789DD7F6_pub.asc"];
andreas@392
   605
    XCTAssertNotNil(pubKeyPartner1);
andreas@392
   606
    NSString *pubKeyMe = [PEPTestUtils loadResourceByName:@"meATdontcare_E3BFBCA9248FC681_pub.asc"];
andreas@392
   607
    XCTAssertNotNil(pubKeyMe);
andreas@392
   608
    NSString *secKeyMe = [PEPTestUtils loadResourceByName:@"meATdontcare_E3BFBCA9248FC681_sec.asc"];
andreas@392
   609
    XCTAssertNotNil(secKeyMe);
andreas@392
   610
andreas@392
   611
    NSString *trustwordsFull = [session getTrustwordsIdentity1:meOrig identity2:partner1Orig
andreas@392
   612
                                                      language:nil full:YES];
andreas@392
   613
    XCTAssertEqualObjects(trustwordsFull,
andreas@392
   614
                          @"EMERSON GASPER TOKENISM BOLUS COLLAGE DESPISE BEDDED ENCRYPTION IMAGINE BEDFORD");
andreas@392
   615
andreas@392
   616
    NSString *trustwordsFullEnglish = [session getTrustwordsIdentity1:meOrig identity2:partner1Orig
andreas@392
   617
                                                             language:@"en" full:YES];
andreas@392
   618
    XCTAssertEqualObjects(trustwordsFullEnglish, trustwordsFull);
andreas@392
   619
andreas@392
   620
    NSString *trustwordsUndefined = [session getTrustwordsIdentity1:meOrig identity2:partner1Orig
andreas@392
   621
                                                           language:@"ZZ" full:YES];
andreas@392
   622
    XCTAssertNil(trustwordsUndefined);
andreas@392
   623
}
andreas@392
   624
andreas@392
   625
#pragma mark - Helpers
andreas@392
   626
andreas@392
   627
- (PEPMessage *)internalEncryptToMySelfKeys:(PEPStringList **)keys
andreas@392
   628
{
andreas@392
   629
    PEPSession *session = [PEPSession new];
andreas@411
   630
    
andreas@411
   631
    PEPIdentity *me = [PEPTestUtils ownPepIdentityWithAddress:@"me@peptest.ch"
andreas@411
   632
                                                     userName:@"userName"];
andreas@392
   633
    [session mySelf:me];
andreas@392
   634
    XCTAssertNotNil(me.fingerPrint);
andreas@392
   635
andreas@392
   636
    // Create draft
andreas@392
   637
    NSString *shortMessage = @"Subject";
andreas@392
   638
    NSString *longMessage = @"Oh, this is a long body text!";
andreas@392
   639
    PEPMessage *mail = [PEPTestUtils mailFrom:me toIdent:me shortMessage:shortMessage longMessage:longMessage outgoing:YES];
andreas@392
   640
andreas@392
   641
    PEPMessage *encMessage;
andreas@392
   642
    PEP_STATUS status = [session encryptMessage:mail identity:me dest:&encMessage];
andreas@392
   643
    XCTAssertEqual(status, 0);
andreas@392
   644
    XCTAssertEqualObjects(encMessage.shortMessage, @"p≡p");
andreas@392
   645
andreas@392
   646
    PEPMessage *unencDict;
andreas@392
   647
    PEP_rating rating = [session decryptMessage:encMessage dest:&unencDict keys:keys];
andreas@392
   648
    XCTAssertGreaterThanOrEqual(rating, PEP_rating_reliable);
andreas@392
   649
andreas@392
   650
    XCTAssertEqualObjects(unencDict.shortMessage, shortMessage);
andreas@392
   651
    XCTAssertEqualObjects(unencDict.longMessage, longMessage);
andreas@392
   652
andreas@392
   653
    return unencDict;
andreas@392
   654
}
andreas@392
   655
andreas@392
   656
- (void)pEpCleanUpRestoringBackupNamed:(NSString *)backup {
andreas@392
   657
    [PEPTestUtils deleteWorkFilesAfterBackingUpWithBackupName:backup];
andreas@392
   658
}
andreas@392
   659
andreas@392
   660
- (void)pEpCleanUp
andreas@392
   661
{
andreas@392
   662
    [PEPSession cleanup];
andreas@392
   663
    [self pEpCleanUpRestoringBackupNamed:NULL];
andreas@392
   664
}
andreas@392
   665
andreas@392
   666
- (void)pEpSetUp:(NSString *)restore
andreas@392
   667
{
andreas@392
   668
    // Must be the first thing you do before using anything pEp-related
andreas@392
   669
    // ... but this is now done in session, with a "dispatch_once"
andreas@392
   670
    // [PEPObjCAdapter setupTrustWordsDB:[NSBundle bundleForClass:[self class]]];
andreas@392
   671
andreas@392
   672
    [PEPTestUtils deleteWorkFilesAfterBackingUpWithBackupName:nil];
andreas@392
   673
    [PEPTestUtils restoreWorkFilesFromBackupNamed:restore];
andreas@392
   674
}
andreas@392
   675
andreas@392
   676
- (void)pEpSetUp
andreas@392
   677
{
andreas@392
   678
    [self pEpSetUp:NULL];
andreas@392
   679
}
andreas@392
   680
andreas@392
   681
@end