5 // Created by Andreas Buff on 18.01.18.
6 // Copyright © 2018 p≡p. All rights reserved.
9 #import <XCTest/XCTest.h>
11 #import "PEPObjCAdapter.h"
12 #import "NSDictionary+Extension.h"
13 #import "PEPIdentity.h"
14 #import "PEPMessage.h"
16 #import "PEPTestUtils.h"
17 #import "PEPTestSyncDelegate.h"
19 @interface PEPSessionTest : XCTestCase
22 @implementation PEPSessionTest
27 [PEPObjCAdapter setUnecryptedSubjectEnabled:NO];
37 - (void)testSyncSession
39 PEPSession *session = [PEPSession new];
41 // Dummy to set up the DB, since this is currenty only triggered by session use,
42 // which PEPObjCAdapter.startSync does not trigger.
45 PEPTestSyncDelegate *syncDelegate = [[PEPTestSyncDelegate alloc] init];
47 // This should attach session just created
48 [PEPObjCAdapter startSync:syncDelegate];
50 PEPIdentity *identMe = [[PEPIdentity alloc]
51 initWithAddress:@"pep.test.iosgenkey@pep-project.org"
53 userName:@"pEp Test iOS GenKey"
56 [session mySelf:identMe];
58 bool res = [syncDelegate waitUntilSent:1];
60 // Can't currently work, engine doesn't contain sync.
63 // This should detach session just created
64 [PEPObjCAdapter stopSync];
67 - (void)testTrustWords
69 PEPSession *session = [PEPSession new];
71 NSArray *trustwords = [session trustwords:@"DB47DB47DB47DB47DB47DB47DB47DB47DB47DB47"
72 forLanguage:@"en" shortened:false];
73 XCTAssertEqual([trustwords count], 10);
75 for(id word in trustwords)
76 XCTAssertEqualObjects(word, @"BAPTISMAL");
81 PEPSession *session = [PEPSession new];
83 PEPIdentity *identMe = [[PEPIdentity alloc]
84 initWithAddress:@"pep.test.iosgenkey@pep-project.org"
86 userName:@"pEp Test iOS GenKey"
89 [session mySelf:identMe];
91 XCTAssertNotNil(identMe.fingerPrint);
92 XCTAssertNotEqual(identMe.commType, PEP_ct_unknown);
94 XCTAssertTrue([identMe isPEPUser:session]);
97 - (void)testMySelfCommType
99 PEPSession *session = [PEPSession new];
101 PEPIdentity *identMe = [[PEPIdentity alloc]
102 initWithAddress:@"pep.test.iosgenkey@pep-project.org"
104 userName:@"pEp Test iOS GenKey"
107 [session mySelf:identMe];
109 XCTAssertNotNil(identMe.fingerPrint);
110 XCTAssertNotEqual(identMe.commType, PEP_ct_unknown);
112 XCTAssertTrue([identMe isPEPUser:session]);
114 dispatch_queue_t queue = dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0);
115 dispatch_sync(queue, ^{
116 PEPSession *session2 = [PEPSession new];
118 // Now simulate an update from the app, which usually only caches
119 // kPepUsername, kPepAddress and optionally kPepUserID.
120 PEPIdentity *identMe2 = [[PEPIdentity alloc]
121 initWithAddress:identMe.address
122 userID:identMe.userID
123 userName:identMe.userName
125 [session2 mySelf:identMe2];
126 XCTAssertNotNil(identMe2.fingerPrint);
127 XCTAssertTrue([identMe2 isPEPUser:session]);
128 XCTAssertEqualObjects(identMe2.fingerPrint, identMe.fingerPrint);
130 // Now pretend the app only knows kPepUsername and kPepAddress
131 PEPIdentity *identMe3 = [PEPTestUtils foreignPepIdentityWithAddress:identMe.address
132 userName:identMe.userName];
133 [session2 mySelf:identMe3];
134 XCTAssertNotNil(identMe3.fingerPrint);
135 XCTAssertFalse([identMe3 isPEPUser:session]);
136 XCTAssertEqualObjects(identMe3.fingerPrint, identMe.fingerPrint);
138 XCTAssertEqualObjects(identMe.address, identMe2.address);
139 XCTAssertEqualObjects(identMe.address, identMe3.address);
140 XCTAssertEqual(identMe.commType, identMe2.commType);
141 XCTAssertEqual(identMe.commType, identMe3.commType);
145 - (void)testPartnerWithoutFingerPrint
147 PEPSession *session = [PEPSession new];
149 PEPIdentity *identRandom = [[PEPIdentity alloc]
150 initWithAddress:@"does_not_exist@example.com"
152 userName:@"No Way Not Even Alice"
155 [session updateIdentity:identRandom];
156 XCTAssertNil(identRandom.fingerPrint);
159 - (void)testImportPartnerKeys
161 XCTAssertNotNil([self checkImportingKeyFilePath:@"6FF00E97_sec.asc"
162 address:@"pep.test.alice@pep-project.org"
163 userID:@"This Is Alice"
164 fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
167 XCTAssertNotNil([self checkImportingKeyFilePath:@"0xC9C2EE39.asc"
168 address:@"pep.test.bob@pep-project.org"
169 userID:@"This Is Bob"
170 fingerPrint:@"BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39"
174 - (void)testIdentityRating
176 PEPSession *session = [PEPSession new];
178 PEPIdentity *me = [self
179 checkMySelfImportingKeyFilePath:@"6FF00E97_sec.asc"
180 address:@"pep.test.alice@pep-project.org"
181 userID:@"Alice_User_ID"
182 fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
184 XCTAssertEqual([session identityRating:me], PEP_rating_trusted_and_anonymized);
186 PEPIdentity *alice = [self
187 checkImportingKeyFilePath:@"6FF00E97_sec.asc"
188 address:@"pep.test.alice@pep-project.org"
189 userID:@"This Is Alice"
190 fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
192 XCTAssertNotNil(alice);
193 XCTAssertEqual([session identityRating:alice], PEP_rating_reliable);
196 - (void)testIdentityRatingTrustResetMistrustUndo
198 PEPSession *session = [PEPSession new];
200 PEPIdentity *me = [[PEPIdentity alloc]
201 initWithAddress:@"me@example.org"
206 XCTAssertNotNil(me.fingerPrint);
207 XCTAssertEqual([session identityRating:me], PEP_rating_trusted_and_anonymized);
209 PEPIdentity *alice = [self
210 checkImportingKeyFilePath:@"6FF00E97_sec.asc"
211 address:@"pep.test.alice@pep-project.org"
212 userID:@"This Is Alice"
213 fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
215 XCTAssertNotNil(alice);
216 XCTAssertEqual([session identityRating:alice], PEP_rating_reliable);
218 [session trustPersonalKey:alice];
219 XCTAssertEqual([session identityRating:alice], PEP_rating_trusted);
221 [session keyResetTrust:alice];
222 XCTAssertEqual([session identityRating:alice], PEP_rating_reliable);
224 [session keyMistrusted:alice];
225 XCTAssertEqual([session identityRating:alice], PEP_rating_have_no_key);
227 [session undoLastMistrust];
228 XCTAssertEqual([session identityRating:alice], PEP_rating_reliable);
230 [session trustPersonalKey:alice];
231 XCTAssertEqual([session identityRating:alice], PEP_rating_trusted);
233 [session keyResetTrust:alice];
234 XCTAssertEqual([session identityRating:alice], PEP_rating_have_no_key);
238 - (void)testIdentityRatingCrash
240 PEPSession *session = [PEPSession new];
242 PEPIdentity *me = [[PEPIdentity alloc]
243 initWithAddress:@"me@example.org"
248 XCTAssertNotNil(me.fingerPrint);
249 XCTAssertEqual([session identityRating:me], PEP_rating_trusted_and_anonymized);
251 PEPIdentity *alice = [self
252 checkImportingKeyFilePath:@"6FF00E97_sec.asc"
253 address:@"pep.test.alice@pep-project.org"
254 userID:@"This Is Alice"
255 fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
257 XCTAssertNotNil(alice);
258 XCTAssertEqual([session identityRating:alice], PEP_rating_reliable);
260 [session trustPersonalKey:alice];
261 XCTAssertEqual([session identityRating:alice], PEP_rating_trusted);
263 [session keyResetTrust:alice];
264 XCTAssertEqual([session identityRating:alice], PEP_rating_reliable);
266 [session keyMistrusted:alice];
267 XCTAssertEqual([session identityRating:alice], PEP_rating_have_no_key);
269 [session undoLastMistrust];
271 [session trustPersonalKey:alice];
272 XCTAssertEqual([session identityRating:alice], PEP_rating_trusted);
274 [session keyResetTrust:alice];
275 XCTAssertEqual([session identityRating:alice], PEP_rating_have_no_key);
277 // This line provoked the crash
278 [session trustPersonalKey:alice];
282 Try to provoke a SQLITE_BUSY (ENGINE-374)
284 - (void)testIdentityRatingTrustResetMistrustUndoBusy
286 PEPSession *session = [PEPSession new];
288 PEPIdentity *me = [[PEPIdentity alloc]
289 initWithAddress:@"me@example.org"
294 XCTAssertNotNil(me.fingerPrint);
295 XCTAssertEqual([session identityRating:me], PEP_rating_trusted_and_anonymized);
297 PEPIdentity *alice = [self
298 checkImportingKeyFilePath:@"6FF00E97_sec.asc"
299 address:@"pep.test.alice@pep-project.org"
300 userID:@"This Is Alice"
301 fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
303 XCTAssertNotNil(alice);
304 XCTAssertEqual([session identityRating:alice], PEP_rating_reliable);
306 void (^encryptingBlock)(void) = ^{
307 PEPSession *innerSession = [PEPSession new];
308 PEPMessage *msg = [PEPMessage new];
311 msg.shortMessage = @"The subject";
312 msg.longMessage = @"Lots and lots of text";
313 msg.direction = PEP_dir_outgoing;
315 PEP_STATUS status = [innerSession encryptMessage:msg identity:me dest:&encMsg];
316 XCTAssertEqual(status, PEP_STATUS_OK);
319 dispatch_group_t backgroundGroup = dispatch_group_create();
320 dispatch_group_async(backgroundGroup,
321 dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), encryptingBlock);
323 [session trustPersonalKey:alice];
324 XCTAssertEqual([session identityRating:alice], PEP_rating_trusted);
326 [session keyResetTrust:alice];
327 XCTAssertEqual([session identityRating:alice], PEP_rating_reliable);
329 [session keyMistrusted:alice];
330 XCTAssertEqual([session identityRating:alice], PEP_rating_have_no_key);
332 [session undoLastMistrust];
333 XCTAssertEqual([session identityRating:alice], PEP_rating_reliable);
335 [session trustPersonalKey:alice];
336 XCTAssertEqual([session identityRating:alice], PEP_rating_trusted);
338 [session keyResetTrust:alice];
339 XCTAssertEqual([session identityRating:alice], PEP_rating_have_no_key);
341 dispatch_group_wait(backgroundGroup, DISPATCH_TIME_FOREVER);
344 - (void)testOutgoingColors
346 PEPSession *session = [PEPSession new];
349 // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
350 // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
351 XCTAssertTrue([PEPTestUtils importBundledKey:@"6FF00E97_sec.asc" session:session]);
354 PEPIdentity *identAlice = [self
355 checkMySelfImportingKeyFilePath:@"6FF00E97_sec.asc"
356 address:@"pep.test.alice@pep-project.org"
357 userID:@"Alice_User_ID"
358 fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
364 PEPIdentity *identUnknownBob = [[PEPIdentity alloc]
365 initWithAddress:@"pep.test.unknown.bob@pep-project.org"
366 userID:@"4242" userName:@"pEp Test Bob Unknown"
369 PEPMessage *msgGray = [PEPMessage new];
370 msgGray.from = identAlice;
371 msgGray.to = @[identUnknownBob];
372 msgGray.shortMessage = @"All Gray Test";
373 msgGray.longMessage = @"This is a text content";
374 msgGray.direction = PEP_dir_outgoing;
376 // Test with unknown Bob
377 PEP_rating clr = [session outgoingColorForMessage:msgGray];
378 XCTAssertEqual(clr, PEP_rating_unencrypted);
381 PEPIdentity *identBob = [self
382 checkImportingKeyFilePath:@"0xC9C2EE39.asc"
383 address:@"pep.test.bob@pep-project.org"
385 fingerPrint:@"BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39"
387 XCTAssertNotNil(identBob);
389 PEPMessage *msg = [PEPMessage new];
390 msg.from = identAlice;
391 msg.to = @[identBob];
392 msg.shortMessage = @"All Gray Test";
393 msg.longMessage = @"This is a text content";
394 msg.direction = PEP_dir_outgoing;
396 // Should be yellow, since no handshake happened.
397 PEP_rating clr = [session outgoingColorForMessage:msg];
398 XCTAssertEqual(clr, PEP_rating_reliable);
400 clr = [session identityRating:identBob];
401 XCTAssertEqual(clr, PEP_rating_reliable);
403 // Let' say we got that handshake, set PEP_ct_confirmed in Bob's identity
404 [session trustPersonalKey:identBob];
406 // This time it should be green
407 clr = [session outgoingColorForMessage:msg];
408 XCTAssertEqual(clr, PEP_rating_trusted);
410 clr = [session identityRating:identBob];
411 XCTAssertEqual(clr, PEP_rating_trusted);
413 // Let' say we undo handshake
414 [session keyResetTrust:identBob];
417 clr = [session outgoingColorForMessage:msg];
418 XCTAssertEqual(clr, PEP_rating_reliable);
421 [session keyMistrusted:identBob];
423 identBob.fingerPrint = nil;
424 [session updateIdentity:identBob];
425 XCTAssertNil(identBob.fingerPrint);
427 // Gray == PEP_rating_unencrypted
428 clr = [session outgoingColorForMessage:msg];
429 XCTAssertEqual(clr, PEP_rating_unencrypted);
432 [session undoLastMistrust];
433 [session updateIdentity:identBob];
434 XCTAssertNotNil(identBob.fingerPrint);
437 clr = [session outgoingColorForMessage:msg];
438 XCTAssertEqual(clr, PEP_rating_reliable);
439 XCTAssertEqual([session identityRating:identBob], PEP_rating_reliable);
442 [session trustPersonalKey:identBob];
445 clr = [session outgoingColorForMessage:msg];
446 XCTAssertEqual(clr, PEP_rating_trusted);
448 // Now let see if it turns back yellow if we add an unconfirmed folk.
449 // pEp Test John (test key, don't use) <pep.test.john@pep-project.org>
450 // AA2E4BEB93E5FE33DEFD8BE1135CD6D170DCF575
451 XCTAssertTrue([PEPTestUtils importBundledKey:@"0x70DCF575.asc" session:session]);
453 PEPIdentity *identJohn = [[PEPIdentity alloc]
454 initWithAddress:@"pep.test.john@pep-project.org"
455 userID:@"101" userName:@"pEp Test John"
457 fingerPrint:@"AA2E4BEB93E5FE33DEFD8BE1135CD6D170DCF575"];
459 [session updateIdentity:identJohn];
461 msg.cc = @[[PEPTestUtils foreignPepIdentityWithAddress:@"pep.test.john@pep-project.org"
462 userName:@"pEp Test John"]];
464 clr = [session outgoingColorForMessage:msg];
465 XCTAssertEqual(clr, PEP_rating_reliable);
467 NSError *error = nil;
468 PEPMessage *encMsg = [session encryptMessage:msg extraKeys:nil error:&error];
469 XCTAssertNotNil(encMsg);
472 XCTAssertEqualObjects(encMsg.shortMessage, @"p≡p");
473 XCTAssertTrue([encMsg.longMessage containsString:@"p≡p"]);
477 - (void)testOutgoingBccColors
479 PEPSession *session = [PEPSession new];
482 // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
483 // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
484 XCTAssertTrue([PEPTestUtils importBundledKey:@"6FF00E97_sec.asc" session:session]);
486 PEPIdentity *identAlice = [[PEPIdentity alloc]
487 initWithAddress:@"pep.test.alice@pep-project.org"
489 userName:@"pEp Test Alice"
491 fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
493 [session mySelf:identAlice];
495 PEPMessage *msg = [PEPMessage new];
496 msg.from = identAlice;
497 msg.to = @[[[PEPIdentity alloc] initWithAddress:@"pep.test.bob@pep-project.org"
498 userID:@"42" userName:@"pEp Test Bob" isOwn:NO]];
499 msg.shortMessage = @"All Green Test";
500 msg.longMessage = @"This is a text content";
501 msg.direction = PEP_dir_outgoing;
503 // Test with unknown Bob
504 PEP_rating clr = [session outgoingColorForMessage:msg];
505 XCTAssertEqual(clr, PEP_rating_unencrypted);
507 // Now let see with bob's pubkey already known
508 // pEp Test Bob (test key, don't use) <pep.test.bob@pep-project.org>
509 // BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39
510 XCTAssertTrue([PEPTestUtils importBundledKey:@"0xC9C2EE39.asc" session:session]);
512 PEPIdentity *identBob = [[PEPIdentity alloc]
513 initWithAddress:@"pep.test.bob@pep-project.org"
514 userID:@"42" userName:@"pEp Test Bob"
516 fingerPrint:@"BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39"];
518 [session updateIdentity:identBob];
520 // Should be yellow, since no handshake happened.
521 clr = [session outgoingColorForMessage:msg];
522 XCTAssertEqual(clr, PEP_rating_reliable);
524 clr = [session identityRating:identBob];
525 XCTAssertEqual(clr, PEP_rating_reliable);
527 // Let' say we got that handshake, set PEP_ct_confirmed in Bob's identity
528 [session trustPersonalKey:identBob];
530 // This time it should be green
531 clr = [session outgoingColorForMessage:msg];
532 XCTAssertEqual(clr, PEP_rating_trusted);
534 clr = [session identityRating:identBob];
535 XCTAssertEqual(clr, PEP_rating_trusted);
537 // Now let see if it turns back yellow if we add an unconfirmed folk.
538 // pEp Test John (test key, don't use) <pep.test.john@pep-project.org>
539 // AA2E4BEB93E5FE33DEFD8BE1135CD6D170DCF575
540 XCTAssertTrue([PEPTestUtils importBundledKey:@"0x70DCF575.asc" session:session]);
542 PEPIdentity *identJohn = [[PEPIdentity alloc]
543 initWithAddress:@"pep.test.john@pep-project.org"
544 userID:@"101" userName:@"pEp Test John"
546 fingerPrint:@"AA2E4BEB93E5FE33DEFD8BE1135CD6D170DCF575"];
548 [session updateIdentity:identJohn];
550 msg.bcc = @[[[PEPIdentity alloc] initWithAddress:@"pep.test.john@pep-project.org"
551 userID:@"101" userName:@"pEp Test John" isOwn:NO]];
554 clr = [session outgoingColorForMessage:msg];
555 XCTAssertEqual(clr, PEP_rating_reliable);
557 [session trustPersonalKey:identJohn];
559 // This time it should be green
560 clr = [session outgoingColorForMessage:msg];
561 XCTAssertEqual(clr, PEP_rating_trusted);
563 clr = [session identityRating:identJohn];
564 XCTAssertEqual(clr, PEP_rating_trusted);
567 - (void)testDontEncryptForMistrusted
569 PEPSession *session = [PEPSession new];
572 // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
573 // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
574 XCTAssertTrue([PEPTestUtils importBundledKey:@"6FF00E97_sec.asc" session:session]);
576 PEPIdentity *identAlice = [[PEPIdentity alloc]
577 initWithAddress:@"pep.test.alice@pep-project.org"
579 userName:@"pEp Test Alice"
581 fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
583 [session mySelf:identAlice];
585 // pEp Test Bob (test key, don't use) <pep.test.bob@pep-project.org>
586 // BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39
587 XCTAssertTrue([PEPTestUtils importBundledKey:@"0xC9C2EE39.asc" session:session]);
589 PEPIdentity *identBob = [[PEPIdentity alloc]
590 initWithAddress:@"pep.test.bob@pep-project.org"
591 userID:@"42" userName:@"pEp Test Bob"
593 fingerPrint:@"BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39"];
595 [session updateIdentity:identBob];
598 [session keyMistrusted:identBob];
600 PEPMessage *msg = [PEPMessage new];
601 msg.from = identAlice;
602 msg.to = @[[[PEPIdentity alloc] initWithAddress:@"pep.test.bob@pep-project.org" userID:@"42"
603 userName:@"pEp Test Bob" isOwn:NO]];
604 msg.shortMessage = @"All Green Test";
605 msg.longMessage = @"This is a text content";
606 msg.direction = PEP_dir_outgoing;
608 // Gray == PEP_rating_unencrypted
609 PEP_rating clr = [session outgoingColorForMessage:msg];
610 XCTAssertEqual(clr, PEP_rating_unencrypted);
612 NSError *error = nil;
613 PEPMessage *encMsg = [session encryptMessage:msg extraKeys:nil error:&error];
614 XCTAssertNotNil(encMsg);
617 XCTAssertNotEqualObjects(encMsg.attachments[0][@"mimeType"], @"application/pgp-encrypted");
624 PEPSession *session = [PEPSession new];
627 // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
628 // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
629 XCTAssertTrue([PEPTestUtils importBundledKey:@"6FF00E97_sec.asc" session:session]);
630 NSString *fpr = @"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97";
632 PEPIdentity *identAlice = [[PEPIdentity alloc]
633 initWithAddress:@"pep.test.alice@pep-project.org"
635 userName:@"pEp Test Alice"
639 [session mySelf:identAlice];
641 PEPIdentity *identAlice2 = [identAlice mutableCopy];
643 // This will revoke key
644 [session keyMistrusted:identAlice2];
645 identAlice2.fingerPrint = nil;
646 [session mySelf:identAlice];
648 // Check fingerprint is different
649 XCTAssertNotEqualObjects(identAlice2.fingerPrint, fpr);
652 - (void)testMailToMyself
654 PEPSession *session = [PEPSession new];
657 // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
658 // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
659 XCTAssertTrue([PEPTestUtils importBundledKey:@"6FF00E97_sec.asc" session:session]);
661 PEPIdentity *identAlice = [[PEPIdentity alloc]
662 initWithAddress:@"pep.test.alice@pep-project.org"
664 userName:@"pEp Test Alice"
666 fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
668 [session mySelf:identAlice];
670 PEPMessage *msg = [PEPMessage new];
671 msg.from = identAlice;
672 msg.to = @[identAlice];
673 msg.shortMessage = @"Mail to Myself";
674 msg.longMessage = @"This is a text content";
675 msg.direction = PEP_dir_outgoing;
677 PEP_rating clr = [session outgoingColorForMessage:msg];
678 XCTAssertEqual(clr, PEP_rating_trusted_and_anonymized);
680 NSError *error = nil;
681 PEPMessage *encMsg = [session encryptMessage:msg extraKeys:nil error:&error];
682 XCTAssertNotNil(encMsg);
688 PEPMessage *decmsg = [session
689 decryptMessage:encMsg
693 XCTAssertNotNil(decmsg);
695 XCTAssertEqual(clr, PEP_rating_trusted_and_anonymized);
698 - (void)testEncryptedMailFromMutt
700 PEPSession *session = [PEPSession new];
702 // This is the public key for test001@peptest.ch
703 XCTAssertTrue([PEPTestUtils importBundledKey:@"A3FC7F0A.asc" session:session]);
705 // This is the secret key for test001@peptest.ch
706 XCTAssertTrue([PEPTestUtils importBundledKey:@"A3FC7F0A_sec.asc" session:session]);
708 // Mail from mutt, already processed into message dict by the app.
709 NSMutableDictionary *msgDict = [[PEPTestUtils
710 unarchiveDictionary:@"msg_to_A3FC7F0A_from_mutt.ser"]
712 [msgDict removeObjectForKey:kPepLongMessage];
713 [msgDict removeObjectForKey:kPepLongMessageFormatted];
715 // Also extracted "live" from the app.
716 NSMutableDictionary *accountDict = [[PEPTestUtils
717 unarchiveDictionary:@"account_A3FC7F0A.ser"]
719 [accountDict removeObjectForKey:kPepCommType];
720 [accountDict removeObjectForKey:kPepFingerprint];
721 PEPIdentity *identMe = [[PEPIdentity alloc] initWithDictionary:accountDict];
723 [session mySelf:identMe];
724 XCTAssertNotNil(identMe.fingerPrint);
727 PEPMessage *msg = [PEPMessage new];
728 [msg setValuesForKeysWithDictionary:msgDict];
730 // Technically, the mail is encrypted, but the signatures don't match
732 PEPMessage *pepDecryptedMail = [session
737 XCTAssertNotNil(pepDecryptedMail);
740 XCTAssertNotNil(pepDecryptedMail.longMessage);
743 - (void)testOutgoingContactColor
745 PEPSession *session = [PEPSession new];
747 PEPIdentity *partner1Orig = [PEPTestUtils foreignPepIdentityWithAddress:@"partner1@dontcare.me"
748 userName:@"Partner 1"];
749 NSString *pubKeyPartner1 = [PEPTestUtils loadResourceByName:@"partner1_F2D281C2789DD7F6_pub.asc"];
750 XCTAssertNotNil(pubKeyPartner1);
751 [session importKey:pubKeyPartner1];
753 PEP_rating color = [session identityRating:partner1Orig];
754 XCTAssertEqual(color, PEP_rating_reliable);
757 - (void)testGetTrustwords
759 PEPSession *session = [PEPSession new];
761 PEPIdentity *partner1Orig = [[PEPIdentity alloc]
762 initWithAddress:@"partner1@dontcare.me" userID:@"partner1"
764 isOwn:NO fingerPrint:@"F0CD3F7B422E5D587ABD885BF2D281C2789DD7F6"];
766 PEPIdentity *meOrig = [[PEPIdentity alloc]
767 initWithAddress:@"me@dontcare.me" userID:@"me"
769 isOwn:NO fingerPrint:@"CC1F73F6FB774BF08B197691E3BFBCA9248FC681"];
771 NSString *pubKeyPartner1 = [PEPTestUtils loadResourceByName:@"partner1_F2D281C2789DD7F6_pub.asc"];
772 XCTAssertNotNil(pubKeyPartner1);
773 NSString *pubKeyMe = [PEPTestUtils loadResourceByName:@"meATdontcare_E3BFBCA9248FC681_pub.asc"];
774 XCTAssertNotNil(pubKeyMe);
775 NSString *secKeyMe = [PEPTestUtils loadResourceByName:@"meATdontcare_E3BFBCA9248FC681_sec.asc"];
776 XCTAssertNotNil(secKeyMe);
778 NSString *trustwordsFull = [session getTrustwordsIdentity1:meOrig identity2:partner1Orig
779 language:nil full:YES];
780 XCTAssertEqualObjects(trustwordsFull,
781 @"EMERSON GASPER TOKENISM BOLUS COLLAGE DESPISE BEDDED ENCRYPTION IMAGINE BEDFORD");
783 NSString *trustwordsFullEnglish = [session getTrustwordsIdentity1:meOrig identity2:partner1Orig
784 language:@"en" full:YES];
785 XCTAssertEqualObjects(trustwordsFullEnglish, trustwordsFull);
787 NSString *trustwordsUndefined = [session getTrustwordsIdentity1:meOrig identity2:partner1Orig
788 language:@"ZZ" full:YES];
789 XCTAssertNil(trustwordsUndefined);
792 - (void)testStringToRating
794 PEPSession *session = [PEPSession new];
795 XCTAssertEqual([session ratingFromString:@"cannot_decrypt"], PEP_rating_cannot_decrypt);
796 XCTAssertEqual([session ratingFromString:@"have_no_key"], PEP_rating_have_no_key);
797 XCTAssertEqual([session ratingFromString:@"unencrypted"], PEP_rating_unencrypted);
798 XCTAssertEqual([session ratingFromString:@"unencrypted_for_some"],
799 PEP_rating_unencrypted_for_some);
800 XCTAssertEqual([session ratingFromString:@"unreliable"], PEP_rating_unreliable);
801 XCTAssertEqual([session ratingFromString:@"reliable"], PEP_rating_reliable);
802 XCTAssertEqual([session ratingFromString:@"trusted"], PEP_rating_trusted);
803 XCTAssertEqual([session ratingFromString:@"trusted_and_anonymized"],
804 PEP_rating_trusted_and_anonymized);
805 XCTAssertEqual([session ratingFromString:@"fully_anonymous"], PEP_rating_fully_anonymous);
806 XCTAssertEqual([session ratingFromString:@"mistrust"], PEP_rating_mistrust);
807 XCTAssertEqual([session ratingFromString:@"b0rken"], PEP_rating_b0rken);
808 XCTAssertEqual([session ratingFromString:@"under_attack"], PEP_rating_under_attack);
809 XCTAssertEqual([session ratingFromString:@"undefined"], PEP_rating_undefined);
810 XCTAssertEqual([session ratingFromString:@"does not exist111"], PEP_rating_undefined);
813 - (void)testRatingToString
815 PEPSession *session = [PEPSession new];
816 XCTAssertEqualObjects([session stringFromRating:PEP_rating_cannot_decrypt], @"cannot_decrypt");
817 XCTAssertEqualObjects([session stringFromRating:PEP_rating_have_no_key], @"have_no_key");
818 XCTAssertEqualObjects([session stringFromRating:PEP_rating_unencrypted], @"unencrypted");
819 XCTAssertEqualObjects([session stringFromRating:PEP_rating_unencrypted_for_some],
820 @"unencrypted_for_some");
821 XCTAssertEqualObjects([session stringFromRating:PEP_rating_unreliable], @"unreliable");
822 XCTAssertEqualObjects([session stringFromRating:PEP_rating_reliable], @"reliable");
823 XCTAssertEqualObjects([session stringFromRating:PEP_rating_trusted], @"trusted");
824 XCTAssertEqualObjects([session stringFromRating:PEP_rating_trusted_and_anonymized],
825 @"trusted_and_anonymized");
826 XCTAssertEqualObjects([session stringFromRating:PEP_rating_fully_anonymous],
828 XCTAssertEqualObjects([session stringFromRating:PEP_rating_mistrust], @"mistrust");
829 XCTAssertEqualObjects([session stringFromRating:PEP_rating_b0rken], @"b0rken");
830 XCTAssertEqualObjects([session stringFromRating:PEP_rating_under_attack], @"under_attack");
831 XCTAssertEqualObjects([session stringFromRating:PEP_rating_undefined], @"undefined");
832 XCTAssertEqualObjects([session stringFromRating:500], @"undefined");
835 - (void)testIsPEPUser
837 PEPSession *session = [PEPSession new];
839 PEPIdentity *identMe = [[PEPIdentity alloc]
840 initWithAddress:@"me-myself-and-i@pep-project.org"
841 userID:@"me-myself-and-i"
844 [session mySelf:identMe];
845 XCTAssertNotNil(identMe.fingerPrint);
847 // PEP_CANNOT_FIND_PERSON == 902
848 XCTAssertTrue([session isPEPUser:identMe]);
851 - (void)testXEncStatusForOutgoingEncryptedMail
853 [self helperXEncStatusForOutgoingEncryptdMailToSelf:NO expectedRating:PEP_rating_reliable];
856 - (void)testXEncStatusForOutgoingSelfEncryptedMail
858 [self helperXEncStatusForOutgoingEncryptdMailToSelf:YES
859 expectedRating:PEP_rating_trusted_and_anonymized];
862 - (void)testEncryptMessagesWithoutKeys
864 PEPSession *session = [PEPSession new];
866 PEPIdentity *identMe = [[PEPIdentity alloc]
867 initWithAddress:@"me-myself-and-i@pep-project.org"
868 userID:@"me-myself-and-i"
871 [session mySelf:identMe];
872 XCTAssertNotNil(identMe.fingerPrint);
874 PEPIdentity *identAlice = [[PEPIdentity alloc]
875 initWithAddress:@"alice@pep-project.org"
877 userName:@"pEp Test Alice"
880 PEPMessage *msg = [PEPMessage new];
882 msg.to = @[identAlice];
883 msg.shortMessage = @"Mail to Alice";
884 msg.longMessage = @"Alice?";
885 msg.direction = PEP_dir_outgoing;
887 PEP_rating clr = [session outgoingColorForMessage:msg];
888 XCTAssertEqual(clr, PEP_rating_unencrypted);
890 NSError *error = nil;
891 PEPMessage *encMsg = [session encryptMessage:msg extraKeys:nil error:&error];
892 XCTAssertNotNil(encMsg);
895 XCTAssertNotNil(encMsg);
898 PEP_rating pEpRating;
900 PEPMessage *decMsg = [session
901 decryptMessage:encMsg
905 XCTAssertNotNil(decMsg);
908 XCTAssertEqual(pEpRating, PEP_rating_unencrypted);
909 XCTAssertNotNil(decMsg);
913 ENGINE-364. Tries to invoke trustPersonalKey on an identity without key,
914 giving it a fake fingerprint.
916 - (void)testTrustPersonalKey
918 PEPSession *session = [PEPSession new];
920 PEPIdentity *identMe = [[PEPIdentity alloc]
921 initWithAddress:@"me-myself-and-i@pep-project.org"
922 userID:@"me-myself-and-i"
925 [session mySelf:identMe];
926 XCTAssertNotNil(identMe.fingerPrint);
928 // The fingerprint is definitely wrong, we don't have a key
929 PEPIdentity *identAlice = [[PEPIdentity alloc]
930 initWithAddress:@"alice@pep-project.org"
932 userName:@"pEp Test Alice"
934 fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
936 [session trustPersonalKey:identAlice];
942 - (void)testVolatileIdentityRating
944 PEPSession *session = [PEPSession new];
946 PEPIdentity *identMe = [[PEPIdentity alloc]
947 initWithAddress:@"me-myself-and-i@pep-project.org"
948 userID:@"me-myself-and-i"
951 [session mySelf:identMe];
952 XCTAssertNotNil(identMe.fingerPrint);
954 PEPIdentity *identAlice = [self
955 checkImportingKeyFilePath:@"6FF00E97_sec.asc"
956 address:@"pep.test.alice@pep-project.org"
957 userID:@"alice_user_id"
958 fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"
960 XCTAssertNotNil(identAlice);
962 dispatch_group_t identityRatingGroup = dispatch_group_create();
964 void (^ratingBlock)(void) = ^{
965 PEPSession *innerSession = [PEPSession new];
966 PEP_rating rating = [innerSession identityRating:identAlice];
967 XCTAssertEqual(rating, PEP_rating_reliable);
970 for (int i = 0; i < 4; ++i) {
971 dispatch_group_async(identityRatingGroup,
972 dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0),
976 for (int i = 0; i < 4; ++i) {
980 dispatch_group_wait(identityRatingGroup, DISPATCH_TIME_FOREVER);
983 #pragma mark - configUnencryptedSubject
985 - (void)testConfigUnencryptedSubject
987 // Setup Config to encrypt subject
988 [PEPObjCAdapter setUnecryptedSubjectEnabled:NO];
990 // Write mail to yourself ...
991 PEPMessage *encMessage = [self mailWrittenToMySelf];
993 // ... and assert subject is encrypted
994 XCTAssertEqualObjects(encMessage.shortMessage, @"p≡p", @"Subject should be encrypted");
997 - (void)testConfigUnencryptedSubject_encryptedSubjectDisabled
999 // Setup Config to not encrypt subject
1000 [PEPObjCAdapter setUnecryptedSubjectEnabled:YES];
1002 // Write mail to yourself ...
1003 PEPMessage *encMessage = [self mailWrittenToMySelf];
1005 // ... and assert the subject is not encrypted
1006 XCTAssertNotEqualObjects(encMessage.shortMessage, @"p≡p", @"Subject should not be encrypted");
1009 #pragma mark - Helpers
1011 - (PEPIdentity *)checkImportingKeyFilePath:(NSString *)filePath address:(NSString *)address
1012 userID:(NSString *)userID
1013 fingerPrint:(NSString *)fingerPrint
1014 session:(PEPSession *)session
1017 session = [PEPSession new];
1020 BOOL success = [PEPTestUtils importBundledKey:filePath session:session];
1021 XCTAssertTrue(success);
1025 PEPIdentity *identTest = [[PEPIdentity alloc]
1026 initWithAddress:address
1028 userName:[NSString stringWithFormat:@"Some User Name %@", userID]
1031 [session updateIdentity:identTest];
1032 XCTAssertNotNil(identTest.fingerPrint);
1033 XCTAssertEqualObjects(identTest.fingerPrint, fingerPrint);
1041 - (PEPIdentity *)checkMySelfImportingKeyFilePath:(NSString *)filePath address:(NSString *)address
1042 userID:(NSString *)userID
1043 fingerPrint:(NSString *)fingerPrint
1044 session:(PEPSession *)session
1046 XCTAssertTrue([PEPTestUtils importBundledKey:filePath session:session]);
1049 PEPIdentity *identTest = [[PEPIdentity alloc]
1050 initWithAddress:address
1052 userName:[NSString stringWithFormat:@"Some User Name %@", userID]
1054 fingerPrint: fingerPrint];
1057 XCTAssertTrue([session setOwnKey:identTest fingerprint:fingerPrint error:&error]);
1058 XCTAssertNil(error);
1059 XCTAssertNotNil(identTest.fingerPrint);
1060 XCTAssertEqualObjects(identTest.fingerPrint, fingerPrint);
1066 Verifies that a partner ID is really a correct Identity.
1067 Usually used on identities imported as keys, since the engine has problems with them.
1069 - (void)updateAndVerifyPartnerIdentity:(PEPIdentity *)partnerIdentity session:(PEPSession *)session
1071 XCTAssertNotNil(partnerIdentity.fingerPrint);
1072 [session updateIdentity:partnerIdentity];
1073 XCTAssertNotNil(partnerIdentity.fingerPrint);
1074 NSString *fingerprint = partnerIdentity.fingerPrint;
1075 partnerIdentity.fingerPrint = nil;
1076 [session updateIdentity:partnerIdentity];
1077 XCTAssertNotNil(partnerIdentity.fingerPrint);
1078 XCTAssertEqualObjects(partnerIdentity.fingerPrint, fingerprint);
1081 - (PEPMessage *)mailWrittenToMySelf
1083 PEPSession *session = [PEPSession new];
1085 // Write a e-mail to yourself ...
1086 PEPIdentity *me = [PEPTestUtils ownPepIdentityWithAddress:@"me@peptest.ch"
1087 userName:@"userName"];
1088 [session mySelf:me];
1090 NSString *shortMessage = @"Subject";
1091 NSString *longMessage = @"Oh, this is a long body text!";
1092 PEPMessage *mail = [PEPTestUtils mailFrom:me
1094 shortMessage:shortMessage
1095 longMessage:longMessage
1097 PEPMessage *encMessage;
1098 [session encryptMessage:mail identity:me dest:&encMessage];
1103 - (PEPMessage *)internalEncryptToMySelfKeys:(PEPStringList **)keys
1105 PEPSession *session = [PEPSession new];
1107 PEPIdentity *me = [PEPTestUtils ownPepIdentityWithAddress:@"me@peptest.ch"
1108 userName:@"userName"];
1109 [session mySelf:me];
1110 XCTAssertNotNil(me.fingerPrint);
1113 NSString *shortMessage = @"Subject";
1114 NSString *longMessage = @"Oh, this is a long body text!";
1115 PEPMessage *mail = [PEPTestUtils mailFrom:me toIdent:me shortMessage:shortMessage longMessage:longMessage outgoing:YES];
1117 PEPMessage *encMessage;
1118 PEP_STATUS status = [session encryptMessage:mail identity:me dest:&encMessage];
1119 XCTAssertEqual(status, 0);
1120 XCTAssertEqualObjects(encMessage.shortMessage, @"p≡p");
1124 PEPMessage *unencDict = [session
1125 decryptMessage:encMessage
1129 XCTAssertNotNil(unencDict);
1130 XCTAssertNil(error);
1132 XCTAssertGreaterThanOrEqual(rating, PEP_rating_reliable);
1134 XCTAssertEqualObjects(unencDict.shortMessage, shortMessage);
1135 XCTAssertEqualObjects(unencDict.longMessage, longMessage);
1142 [PEPTestUtils cleanUp];
1145 - (void)helperXEncStatusForOutgoingEncryptdMailToSelf:(BOOL)toSelf
1146 expectedRating:(PEP_rating)expectedRating
1148 PEPSession *session = [PEPSession new];
1150 // Partner pubkey for the test:
1151 // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
1152 // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97
1153 XCTAssertTrue([PEPTestUtils importBundledKey:@"0x6FF00E97.asc" session:session]);
1155 PEPIdentity *identAlice = [[PEPIdentity alloc]
1156 initWithAddress:@"pep.test.alice@pep-project.org"
1158 userName:@"pEp Test Alice"
1160 fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"];
1161 [self updateAndVerifyPartnerIdentity:identAlice session:session];
1163 PEPIdentity *identMe = [[PEPIdentity alloc]
1164 initWithAddress:@"me-myself-and-i@pep-project.org"
1165 userID:@"me-myself-and-i"
1168 [session mySelf:identMe];
1169 XCTAssertNotNil(identMe.fingerPrint);
1171 PEPMessage *msg = [PEPMessage new];
1173 msg.to = @[identAlice];
1174 msg.shortMessage = @"Mail to Alice";
1175 msg.longMessage = @"Alice?";
1176 msg.direction = PEP_dir_outgoing;
1178 PEP_rating clr = [session outgoingColorForMessage:msg];
1179 XCTAssertEqual(clr, PEP_rating_reliable);
1183 PEP_STATUS statusEnc = PEP_VERSION_MISMATCH;
1185 statusEnc = [session encryptMessage:msg identity:identMe dest:&encMsg];
1186 XCTAssertEqual(statusEnc, PEP_STATUS_OK);
1188 NSError *error = nil;
1189 encMsg = [session encryptMessage:msg extraKeys:nil error:&error];
1190 XCTAssertNotNil(encMsg);
1191 XCTAssertNil(error);
1193 XCTAssertNotNil(encMsg);
1195 PEPStringList *keys;
1196 PEP_rating pEpRating;
1198 PEPMessage *decMsg = [session
1199 decryptMessage:encMsg
1203 XCTAssertNil(error);
1204 XCTAssertNotNil(decMsg);
1206 XCTAssertEqual(pEpRating, expectedRating);
1208 NSArray * encStatusField = nil;
1209 for (NSArray *field in decMsg.optionalFields) {
1210 NSString *header = [field[0] lowercaseString];
1211 if ([header isEqualToString:@"x-encstatus"]) {
1212 encStatusField = field;
1215 XCTAssertNotNil(encStatusField);
1216 if (encStatusField) {
1217 PEP_rating outgoingRating = [session ratingFromString:encStatusField[1]];
1218 XCTAssertEqual(outgoingRating, expectedRating);