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