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