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