andreas@392: // andreas@392: // PEPSessionTest.m andreas@392: // pEpObjCAdapterTests andreas@392: // andreas@392: // Created by Andreas Buff on 18.01.18. andreas@392: // Copyright © 2018 p≡p. All rights reserved. andreas@392: // andreas@392: andreas@392: #import andreas@392: andreas@392: #import "PEPObjCAdapter.h" andreas@392: #import "NSDictionary+Extension.h" andreas@392: #import "PEPIdentity.h" andreas@392: #import "PEPMessage.h" andreas@392: andreas@392: #import "PEPTestUtils.h" andreas@392: #import "PEPTestSyncDelegate.h" andreas@392: andreas@392: @interface PEPSessionTest : XCTestCase andreas@392: @end andreas@392: andreas@392: @implementation PEPSessionTest andreas@392: andreas@392: - (void)setUp andreas@392: { andreas@392: [super setUp]; andreas@416: [PEPObjCAdapter setUnecryptedSubjectEnabled:NO]; andreas@416: andreas@392: [self pEpCleanUp]; andreas@392: } andreas@392: andreas@392: - (void)tearDown { andreas@392: [self pEpCleanUp]; andreas@392: [super tearDown]; andreas@392: } andreas@392: andreas@392: - (void)testSyncSession andreas@392: { andreas@392: PEPSession *session = [PEPSession new]; dirk@474: dirk@474: // Dummy to set up the DB, since this is currenty only triggered by session use, dirk@474: // which PEPObjCAdapter.startSync does not trigger. dirk@474: [session getLog]; dirk@474: andreas@392: PEPTestSyncDelegate *syncDelegate = [[PEPTestSyncDelegate alloc] init]; andreas@392: andreas@392: // This should attach session just created andreas@392: [PEPObjCAdapter startSync:syncDelegate]; andreas@392: andreas@392: PEPIdentity *identMe = [[PEPIdentity alloc] andreas@392: initWithAddress:@"pep.test.iosgenkey@pep-project.org" andreas@392: userID:@"Me" andreas@392: userName:@"pEp Test iOS GenKey" andreas@392: isOwn:YES]; andreas@392: andreas@392: [session mySelf:identMe]; andreas@392: andreas@392: bool res = [syncDelegate waitUntilSent:1]; andreas@392: andreas@392: // Can't currently work, engine doesn't contain sync. andreas@392: XCTAssertFalse(res); andreas@392: andreas@392: // This should detach session just created andreas@392: [PEPObjCAdapter stopSync]; andreas@392: } andreas@392: andreas@392: - (void)testTrustWords andreas@392: { andreas@392: PEPSession *session = [PEPSession new]; andreas@392: andreas@392: NSArray *trustwords = [session trustwords:@"DB47DB47DB47DB47DB47DB47DB47DB47DB47DB47" andreas@392: forLanguage:@"en" shortened:false]; andreas@392: XCTAssertEqual([trustwords count], 10); andreas@392: andreas@392: for(id word in trustwords) andreas@392: XCTAssertEqualObjects(word, @"BAPTISMAL"); andreas@392: } andreas@392: andreas@392: - (void)testGenKey andreas@392: { andreas@392: PEPSession *session = [PEPSession new]; andreas@392: andreas@392: PEPIdentity *identMe = [[PEPIdentity alloc] andreas@392: initWithAddress:@"pep.test.iosgenkey@pep-project.org" andreas@392: userID:@"Me" andreas@392: userName:@"pEp Test iOS GenKey" andreas@392: isOwn:YES]; andreas@392: andreas@392: [session mySelf:identMe]; andreas@392: andreas@392: XCTAssertNotNil(identMe.fingerPrint); andreas@392: XCTAssertNotEqual(identMe.commType, PEP_ct_unknown); andreas@392: dirk@498: XCTAssertTrue([identMe isPEPUser:session]); andreas@392: } andreas@392: andreas@392: - (void)testMySelfCommType andreas@392: { andreas@392: PEPSession *session = [PEPSession new]; andreas@392: andreas@392: PEPIdentity *identMe = [[PEPIdentity alloc] andreas@392: initWithAddress:@"pep.test.iosgenkey@pep-project.org" andreas@392: userID:@"Me" andreas@392: userName:@"pEp Test iOS GenKey" andreas@392: isOwn:YES]; andreas@392: andreas@392: [session mySelf:identMe]; andreas@392: andreas@392: XCTAssertNotNil(identMe.fingerPrint); andreas@392: XCTAssertNotEqual(identMe.commType, PEP_ct_unknown); andreas@392: dirk@498: XCTAssertTrue([identMe isPEPUser:session]); andreas@392: andreas@392: dispatch_queue_t queue = dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0); andreas@392: dispatch_sync(queue, ^{ andreas@392: PEPSession *session2 = [PEPSession new]; andreas@392: andreas@392: // Now simulate an update from the app, which usually only caches andreas@392: // kPepUsername, kPepAddress and optionally kPepUserID. andreas@392: PEPIdentity *identMe2 = [[PEPIdentity alloc] andreas@392: initWithAddress:identMe.address andreas@392: userID:identMe.userID andreas@392: userName:identMe.userName andreas@392: isOwn:NO]; andreas@392: [session2 mySelf:identMe2]; andreas@392: XCTAssertNotNil(identMe2.fingerPrint); dirk@498: XCTAssertTrue([identMe2 isPEPUser:session]); andreas@392: XCTAssertEqualObjects(identMe2.fingerPrint, identMe.fingerPrint); andreas@392: andreas@392: // Now pretend the app only knows kPepUsername and kPepAddress andreas@411: PEPIdentity *identMe3 = [PEPTestUtils foreignPepIdentityWithAddress:identMe.address andreas@411: userName:identMe.userName]; andreas@392: [session2 mySelf:identMe3]; andreas@392: XCTAssertNotNil(identMe3.fingerPrint); dirk@498: XCTAssertFalse([identMe3 isPEPUser:session]); andreas@392: XCTAssertEqualObjects(identMe3.fingerPrint, identMe.fingerPrint); andreas@392: andreas@392: XCTAssertEqualObjects(identMe.address, identMe2.address); andreas@392: XCTAssertEqualObjects(identMe.address, identMe3.address); andreas@392: XCTAssertEqual(identMe.commType, identMe2.commType); andreas@392: XCTAssertEqual(identMe.commType, identMe3.commType); andreas@392: }); andreas@392: } andreas@392: dirk@435: - (void)testPartnerWithoutFingerPrint dirk@435: { dirk@435: PEPSession *session = [PEPSession new]; dirk@435: dirk@435: PEPIdentity *identRandom = [[PEPIdentity alloc] dirk@435: initWithAddress:@"does_not_exist@example.com" dirk@435: userID:@"got_out" dirk@435: userName:@"No Way Not Even Alice" dirk@435: isOwn:NO]; dirk@435: dirk@435: [session updateIdentity:identRandom]; dirk@435: XCTAssertNil(identRandom.fingerPrint); dirk@435: } dirk@435: dirk@437: - (void)testImportPartnerKeys dirk@436: { dirk@488: XCTAssertNotNil([self checkImportingKeyFilePath:@"6FF00E97_sec.asc" dirk@488: address:@"pep.test.alice@pep-project.org" dirk@488: userID:@"This Is Alice" dirk@488: fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97" dirk@488: session: nil]); dirk@436: dirk@488: XCTAssertNotNil([self checkImportingKeyFilePath:@"0xC9C2EE39.asc" dirk@488: address:@"pep.test.bob@pep-project.org" dirk@488: userID:@"This Is Bob" dirk@488: fingerPrint:@"BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39" dirk@488: session: nil]); dirk@436: } dirk@436: dirk@439: - (void)testIdentityRating dirk@439: { dirk@439: PEPSession *session = [PEPSession new]; dirk@439: dirk@439: PEPIdentity *me = [self dirk@439: checkMySelfImportingKeyFilePath:@"6FF00E97_sec.asc" dirk@439: address:@"pep.test.alice@pep-project.org" dirk@439: userID:@"Alice_User_ID" dirk@491: fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97" dirk@491: session:session]; dirk@439: XCTAssertEqual([session identityRating:me], PEP_rating_trusted_and_anonymized); dirk@439: dirk@439: PEPIdentity *alice = [self dirk@439: checkImportingKeyFilePath:@"6FF00E97_sec.asc" dirk@439: address:@"pep.test.alice@pep-project.org" dirk@439: userID:@"This Is Alice" dirk@456: fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97" dirk@456: session: session]; dirk@488: XCTAssertNotNil(alice); dirk@439: XCTAssertEqual([session identityRating:alice], PEP_rating_reliable); dirk@439: } dirk@439: dirk@446: - (void)testIdentityRatingTrustResetMistrustUndo dirk@445: { dirk@445: PEPSession *session = [PEPSession new]; dirk@445: dirk@460: PEPIdentity *me = [[PEPIdentity alloc] dirk@460: initWithAddress:@"me@example.org" dirk@460: userID:@"me_myself" dirk@460: userName:@"Me Me" dirk@460: isOwn:YES]; dirk@460: [session mySelf:me]; dirk@460: XCTAssertNotNil(me.fingerPrint); dirk@445: XCTAssertEqual([session identityRating:me], PEP_rating_trusted_and_anonymized); dirk@445: dirk@445: PEPIdentity *alice = [self dirk@445: checkImportingKeyFilePath:@"6FF00E97_sec.asc" dirk@445: address:@"pep.test.alice@pep-project.org" dirk@445: userID:@"This Is Alice" dirk@456: fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97" dirk@456: session: session]; dirk@488: XCTAssertNotNil(alice); dirk@445: XCTAssertEqual([session identityRating:alice], PEP_rating_reliable); dirk@445: dirk@446: [session trustPersonalKey:alice]; dirk@446: XCTAssertEqual([session identityRating:alice], PEP_rating_trusted); dirk@446: dirk@446: [session keyResetTrust:alice]; dirk@446: XCTAssertEqual([session identityRating:alice], PEP_rating_reliable); dirk@446: dirk@445: [session keyMistrusted:alice]; dirk@445: XCTAssertEqual([session identityRating:alice], PEP_rating_have_no_key); dirk@445: dirk@445: [session undoLastMistrust]; dirk@445: XCTAssertEqual([session identityRating:alice], PEP_rating_reliable); dirk@446: dirk@446: [session trustPersonalKey:alice]; dirk@446: XCTAssertEqual([session identityRating:alice], PEP_rating_trusted); dirk@446: dirk@446: [session keyResetTrust:alice]; dirk@450: XCTAssertEqual([session identityRating:alice], PEP_rating_have_no_key); dirk@445: } dirk@445: dirk@478: /** ENGINE-384 */ dirk@472: - (void)testIdentityRatingCrash dirk@472: { dirk@472: PEPSession *session = [PEPSession new]; dirk@472: dirk@472: PEPIdentity *me = [[PEPIdentity alloc] dirk@472: initWithAddress:@"me@example.org" dirk@472: userID:@"me_myself" dirk@472: userName:@"Me Me" dirk@472: isOwn:YES]; dirk@472: [session mySelf:me]; dirk@472: XCTAssertNotNil(me.fingerPrint); dirk@472: XCTAssertEqual([session identityRating:me], PEP_rating_trusted_and_anonymized); dirk@472: dirk@472: PEPIdentity *alice = [self dirk@472: checkImportingKeyFilePath:@"6FF00E97_sec.asc" dirk@472: address:@"pep.test.alice@pep-project.org" dirk@472: userID:@"This Is Alice" dirk@472: fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97" dirk@472: session: session]; dirk@488: XCTAssertNotNil(alice); dirk@472: XCTAssertEqual([session identityRating:alice], PEP_rating_reliable); dirk@472: dirk@472: [session trustPersonalKey:alice]; dirk@472: XCTAssertEqual([session identityRating:alice], PEP_rating_trusted); dirk@472: dirk@472: [session keyResetTrust:alice]; dirk@472: XCTAssertEqual([session identityRating:alice], PEP_rating_reliable); dirk@472: dirk@472: [session keyMistrusted:alice]; dirk@472: XCTAssertEqual([session identityRating:alice], PEP_rating_have_no_key); dirk@472: dirk@472: [session undoLastMistrust]; dirk@472: dirk@472: [session trustPersonalKey:alice]; dirk@472: XCTAssertEqual([session identityRating:alice], PEP_rating_trusted); dirk@472: dirk@472: [session keyResetTrust:alice]; dirk@472: XCTAssertEqual([session identityRating:alice], PEP_rating_have_no_key); dirk@472: dirk@478: // This line provoked the crash dirk@472: [session trustPersonalKey:alice]; dirk@472: } dirk@472: dirk@458: /** dirk@458: Try to provoke a SQLITE_BUSY (ENGINE-374) dirk@458: */ dirk@458: - (void)testIdentityRatingTrustResetMistrustUndoBusy dirk@458: { dirk@458: PEPSession *session = [PEPSession new]; dirk@458: dirk@459: PEPIdentity *me = [[PEPIdentity alloc] dirk@459: initWithAddress:@"me@example.org" dirk@459: userID:@"me_myself" dirk@459: userName:@"Me Me" dirk@459: isOwn:YES]; dirk@459: [session mySelf:me]; dirk@459: XCTAssertNotNil(me.fingerPrint); dirk@458: XCTAssertEqual([session identityRating:me], PEP_rating_trusted_and_anonymized); dirk@458: dirk@458: PEPIdentity *alice = [self dirk@458: checkImportingKeyFilePath:@"6FF00E97_sec.asc" dirk@458: address:@"pep.test.alice@pep-project.org" dirk@458: userID:@"This Is Alice" dirk@458: fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97" dirk@458: session: session]; dirk@488: XCTAssertNotNil(alice); dirk@458: XCTAssertEqual([session identityRating:alice], PEP_rating_reliable); dirk@458: dirk@458: void (^encryptingBlock)(void) = ^{ dirk@458: PEPSession *innerSession = [PEPSession new]; dirk@458: PEPMessage *msg = [PEPMessage new]; dirk@458: msg.from = me; dirk@458: msg.to = @[alice]; dirk@458: msg.shortMessage = @"The subject"; dirk@458: msg.longMessage = @"Lots and lots of text"; dirk@476: msg.direction = PEP_dir_outgoing; dirk@458: PEPMessage *encMsg; dirk@458: PEP_STATUS status = [innerSession encryptMessage:msg identity:me dest:&encMsg]; dirk@458: XCTAssertEqual(status, PEP_STATUS_OK); dirk@458: }; dirk@458: dirk@458: dispatch_group_t backgroundGroup = dispatch_group_create(); dirk@458: dispatch_group_async(backgroundGroup, dirk@458: dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), encryptingBlock); dirk@458: dirk@458: [session trustPersonalKey:alice]; dirk@458: XCTAssertEqual([session identityRating:alice], PEP_rating_trusted); dirk@458: dirk@458: [session keyResetTrust:alice]; dirk@458: XCTAssertEqual([session identityRating:alice], PEP_rating_reliable); dirk@458: dirk@458: [session keyMistrusted:alice]; dirk@458: XCTAssertEqual([session identityRating:alice], PEP_rating_have_no_key); dirk@458: dirk@458: [session undoLastMistrust]; dirk@458: XCTAssertEqual([session identityRating:alice], PEP_rating_reliable); dirk@458: dirk@458: [session trustPersonalKey:alice]; dirk@458: XCTAssertEqual([session identityRating:alice], PEP_rating_trusted); dirk@458: dirk@458: [session keyResetTrust:alice]; dirk@458: XCTAssertEqual([session identityRating:alice], PEP_rating_have_no_key); dirk@458: dirk@458: dispatch_group_wait(backgroundGroup, DISPATCH_TIME_FOREVER); dirk@458: } dirk@458: andreas@392: - (void)testOutgoingColors andreas@392: { andreas@392: PEPSession *session = [PEPSession new]; andreas@392: andreas@392: // Our test user : andreas@392: // pEp Test Alice (test key don't use) andreas@392: // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97 dirk@488: XCTAssertTrue([PEPTestUtils importBundledKey:@"6FF00E97_sec.asc" session:session]); andreas@392: andreas@392: // Our test user : dirk@440: PEPIdentity *identAlice = [self dirk@440: checkMySelfImportingKeyFilePath:@"6FF00E97_sec.asc" dirk@440: address:@"pep.test.alice@pep-project.org" dirk@440: userID:@"Alice_User_ID" dirk@491: fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97" dirk@491: session:session]; andreas@392: andreas@392: //Message andreas@392: dirk@441: { dirk@441: PEPIdentity *identUnknownBob = [[PEPIdentity alloc] dirk@441: initWithAddress:@"pep.test.unknown.bob@pep-project.org" dirk@441: userID:@"4242" userName:@"pEp Test Bob Unknown" dirk@441: isOwn:NO]; dirk@441: dirk@441: PEPMessage *msgGray = [PEPMessage new]; dirk@441: msgGray.from = identAlice; dirk@441: msgGray.to = @[identUnknownBob]; dirk@441: msgGray.shortMessage = @"All Gray Test"; dirk@441: msgGray.longMessage = @"This is a text content"; dirk@441: msgGray.direction = PEP_dir_outgoing; dirk@441: dirk@441: // Test with unknown Bob dirk@441: PEP_rating clr = [session outgoingColorForMessage:msgGray]; dirk@441: XCTAssertEqual(clr, PEP_rating_unencrypted); dirk@441: } dirk@441: dirk@441: PEPIdentity *identBob = [self dirk@441: checkImportingKeyFilePath:@"0xC9C2EE39.asc" dirk@441: address:@"pep.test.bob@pep-project.org" dirk@441: userID:@"42" dirk@456: fingerPrint:@"BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39" dirk@456: session: session]; dirk@488: XCTAssertNotNil(identBob); dirk@438: andreas@392: PEPMessage *msg = [PEPMessage new]; andreas@392: msg.from = identAlice; dirk@438: msg.to = @[identBob]; dirk@441: msg.shortMessage = @"All Gray Test"; andreas@392: msg.longMessage = @"This is a text content"; andreas@392: msg.direction = PEP_dir_outgoing; andreas@392: dirk@441: // Should be yellow, since no handshake happened. andreas@392: PEP_rating clr = [session outgoingColorForMessage:msg]; dirk@420: XCTAssertEqual(clr, PEP_rating_reliable); andreas@392: andreas@392: clr = [session identityRating:identBob]; dirk@420: XCTAssertEqual(clr, PEP_rating_reliable); andreas@392: andreas@392: // Let' say we got that handshake, set PEP_ct_confirmed in Bob's identity andreas@392: [session trustPersonalKey:identBob]; andreas@392: andreas@392: // This time it should be green andreas@392: clr = [session outgoingColorForMessage:msg]; dirk@420: XCTAssertEqual(clr, PEP_rating_trusted); andreas@392: andreas@392: clr = [session identityRating:identBob]; dirk@420: XCTAssertEqual(clr, PEP_rating_trusted); andreas@392: andreas@392: // Let' say we undo handshake andreas@392: [session keyResetTrust:identBob]; andreas@392: andreas@392: // Yellow ? andreas@392: clr = [session outgoingColorForMessage:msg]; dirk@420: XCTAssertEqual(clr, PEP_rating_reliable); andreas@392: andreas@392: // mistrust Bob andreas@392: [session keyMistrusted:identBob]; andreas@392: dirk@444: identBob.fingerPrint = nil; dirk@441: [session updateIdentity:identBob]; dirk@441: XCTAssertNil(identBob.fingerPrint); dirk@441: andreas@392: // Gray == PEP_rating_unencrypted andreas@392: clr = [session outgoingColorForMessage:msg]; dirk@420: XCTAssertEqual(clr, PEP_rating_unencrypted); andreas@392: dirk@444: // Undo dirk@443: [session undoLastMistrust]; dirk@444: [session updateIdentity:identBob]; dirk@444: XCTAssertNotNil(identBob.fingerPrint); andreas@392: andreas@392: // Back to yellow andreas@392: clr = [session outgoingColorForMessage:msg]; dirk@420: XCTAssertEqual(clr, PEP_rating_reliable); dirk@444: XCTAssertEqual([session identityRating:identBob], PEP_rating_reliable); andreas@392: andreas@392: // Trust again andreas@392: [session trustPersonalKey:identBob]; andreas@392: andreas@392: // Back to green andreas@392: clr = [session outgoingColorForMessage:msg]; dirk@420: XCTAssertEqual(clr, PEP_rating_trusted); andreas@392: andreas@392: // Now let see if it turns back yellow if we add an unconfirmed folk. andreas@392: // pEp Test John (test key, don't use) andreas@392: // AA2E4BEB93E5FE33DEFD8BE1135CD6D170DCF575 dirk@488: XCTAssertTrue([PEPTestUtils importBundledKey:@"0x70DCF575.asc" session:session]); andreas@392: andreas@392: PEPIdentity *identJohn = [[PEPIdentity alloc] andreas@392: initWithAddress:@"pep.test.john@pep-project.org" andreas@392: userID:@"101" userName:@"pEp Test John" andreas@392: isOwn:NO andreas@392: fingerPrint:@"AA2E4BEB93E5FE33DEFD8BE1135CD6D170DCF575"]; andreas@392: andreas@392: [session updateIdentity:identJohn]; andreas@392: andreas@411: msg.cc = @[[PEPTestUtils foreignPepIdentityWithAddress:@"pep.test.john@pep-project.org" andreas@411: userName:@"pEp Test John"]]; andreas@392: // Yellow ? andreas@392: clr = [session outgoingColorForMessage:msg]; dirk@420: XCTAssertEqual(clr, PEP_rating_reliable); andreas@392: dirk@522: NSError *error = nil; dirk@522: PEPMessage *encMsg = [session encryptMessage:msg extraKeys:nil error:&error]; dirk@522: XCTAssertNotNil(encMsg); dirk@522: XCTAssertNil(error); andreas@392: dirk@522: XCTAssertEqualObjects(encMsg.shortMessage, @"p≡p"); dirk@522: XCTAssertTrue([encMsg.longMessage containsString:@"p≡p"]); andreas@392: } andreas@392: andreas@392: andreas@392: - (void)testOutgoingBccColors andreas@392: { andreas@392: PEPSession *session = [PEPSession new]; andreas@392: andreas@392: // Our test user : andreas@392: // pEp Test Alice (test key don't use) andreas@392: // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97 dirk@488: XCTAssertTrue([PEPTestUtils importBundledKey:@"6FF00E97_sec.asc" session:session]); andreas@392: andreas@392: PEPIdentity *identAlice = [[PEPIdentity alloc] andreas@392: initWithAddress:@"pep.test.alice@pep-project.org" andreas@392: userID:ownUserId andreas@392: userName:@"pEp Test Alice" andreas@392: isOwn:YES andreas@392: fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"]; andreas@392: andreas@392: [session mySelf:identAlice]; andreas@392: andreas@392: PEPMessage *msg = [PEPMessage new]; andreas@392: msg.from = identAlice; andreas@392: msg.to = @[[[PEPIdentity alloc] initWithAddress:@"pep.test.bob@pep-project.org" andreas@392: userID:@"42" userName:@"pEp Test Bob" isOwn:NO]]; andreas@392: msg.shortMessage = @"All Green Test"; andreas@392: msg.longMessage = @"This is a text content"; andreas@392: msg.direction = PEP_dir_outgoing; andreas@392: andreas@392: // Test with unknown Bob andreas@392: PEP_rating clr = [session outgoingColorForMessage:msg]; dirk@420: XCTAssertEqual(clr, PEP_rating_unencrypted); andreas@392: andreas@392: // Now let see with bob's pubkey already known andreas@392: // pEp Test Bob (test key, don't use) andreas@392: // BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39 dirk@488: XCTAssertTrue([PEPTestUtils importBundledKey:@"0xC9C2EE39.asc" session:session]); andreas@392: andreas@392: PEPIdentity *identBob = [[PEPIdentity alloc] andreas@392: initWithAddress:@"pep.test.bob@pep-project.org" andreas@392: userID:@"42" userName:@"pEp Test Bob" andreas@392: isOwn:NO andreas@392: fingerPrint:@"BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39"]; andreas@392: andreas@392: [session updateIdentity:identBob]; andreas@392: andreas@392: // Should be yellow, since no handshake happened. andreas@392: clr = [session outgoingColorForMessage:msg]; dirk@420: XCTAssertEqual(clr, PEP_rating_reliable); andreas@392: andreas@392: clr = [session identityRating:identBob]; dirk@420: XCTAssertEqual(clr, PEP_rating_reliable); andreas@392: andreas@392: // Let' say we got that handshake, set PEP_ct_confirmed in Bob's identity andreas@392: [session trustPersonalKey:identBob]; andreas@392: andreas@392: // This time it should be green andreas@392: clr = [session outgoingColorForMessage:msg]; dirk@420: XCTAssertEqual(clr, PEP_rating_trusted); andreas@392: andreas@392: clr = [session identityRating:identBob]; dirk@420: XCTAssertEqual(clr, PEP_rating_trusted); andreas@392: andreas@392: // Now let see if it turns back yellow if we add an unconfirmed folk. andreas@392: // pEp Test John (test key, don't use) andreas@392: // AA2E4BEB93E5FE33DEFD8BE1135CD6D170DCF575 dirk@488: XCTAssertTrue([PEPTestUtils importBundledKey:@"0x70DCF575.asc" session:session]); andreas@392: andreas@392: PEPIdentity *identJohn = [[PEPIdentity alloc] andreas@392: initWithAddress:@"pep.test.john@pep-project.org" andreas@392: userID:@"101" userName:@"pEp Test John" andreas@392: isOwn:NO andreas@392: fingerPrint:@"AA2E4BEB93E5FE33DEFD8BE1135CD6D170DCF575"]; andreas@392: andreas@392: [session updateIdentity:identJohn]; andreas@392: andreas@392: msg.bcc = @[[[PEPIdentity alloc] initWithAddress:@"pep.test.john@pep-project.org" andreas@392: userID:@"101" userName:@"pEp Test John" isOwn:NO]]; andreas@392: andreas@392: // Yellow ? andreas@392: clr = [session outgoingColorForMessage:msg]; dirk@420: XCTAssertEqual(clr, PEP_rating_reliable); andreas@392: andreas@392: [session trustPersonalKey:identJohn]; andreas@392: andreas@392: // This time it should be green andreas@392: clr = [session outgoingColorForMessage:msg]; dirk@420: XCTAssertEqual(clr, PEP_rating_trusted); andreas@392: andreas@392: clr = [session identityRating:identJohn]; dirk@420: XCTAssertEqual(clr, PEP_rating_trusted); andreas@392: } andreas@392: andreas@392: - (void)testDontEncryptForMistrusted andreas@392: { andreas@392: PEPSession *session = [PEPSession new]; andreas@392: andreas@392: // Our test user : andreas@392: // pEp Test Alice (test key don't use) andreas@392: // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97 dirk@488: XCTAssertTrue([PEPTestUtils importBundledKey:@"6FF00E97_sec.asc" session:session]); andreas@392: andreas@392: PEPIdentity *identAlice = [[PEPIdentity alloc] andreas@392: initWithAddress:@"pep.test.alice@pep-project.org" andreas@392: userID:ownUserId andreas@392: userName:@"pEp Test Alice" andreas@392: isOwn:YES andreas@392: fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"]; andreas@392: andreas@392: [session mySelf:identAlice]; andreas@392: andreas@392: // pEp Test Bob (test key, don't use) andreas@392: // BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39 dirk@488: XCTAssertTrue([PEPTestUtils importBundledKey:@"0xC9C2EE39.asc" session:session]); andreas@392: andreas@392: PEPIdentity *identBob = [[PEPIdentity alloc] andreas@392: initWithAddress:@"pep.test.bob@pep-project.org" andreas@392: userID:@"42" userName:@"pEp Test Bob" andreas@392: isOwn:NO andreas@392: fingerPrint:@"BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39"]; andreas@392: andreas@392: [session updateIdentity:identBob]; andreas@392: andreas@392: // mistrust Bob andreas@392: [session keyMistrusted:identBob]; andreas@392: andreas@392: PEPMessage *msg = [PEPMessage new]; andreas@392: msg.from = identAlice; andreas@392: msg.to = @[[[PEPIdentity alloc] initWithAddress:@"pep.test.bob@pep-project.org" userID:@"42" andreas@392: userName:@"pEp Test Bob" isOwn:NO]]; andreas@392: msg.shortMessage = @"All Green Test"; andreas@392: msg.longMessage = @"This is a text content"; andreas@392: msg.direction = PEP_dir_outgoing; andreas@392: andreas@392: // Gray == PEP_rating_unencrypted andreas@392: PEP_rating clr = [session outgoingColorForMessage:msg]; dirk@420: XCTAssertEqual(clr, PEP_rating_unencrypted); andreas@392: dirk@522: NSError *error = nil; dirk@522: PEPMessage *encMsg = [session encryptMessage:msg extraKeys:nil error:&error]; dirk@522: XCTAssertNotNil(encMsg); dirk@522: XCTAssertNil(error); andreas@392: dirk@522: XCTAssertNotEqualObjects(encMsg.attachments[0][@"mimeType"], @"application/pgp-encrypted"); andreas@392: andreas@392: [self pEpCleanUp]; andreas@392: } andreas@392: andreas@392: - (void)testRevoke andreas@392: { andreas@392: PEPSession *session = [PEPSession new]; andreas@392: andreas@392: // Our test user : andreas@392: // pEp Test Alice (test key don't use) andreas@392: // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97 dirk@488: XCTAssertTrue([PEPTestUtils importBundledKey:@"6FF00E97_sec.asc" session:session]); andreas@392: NSString *fpr = @"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"; andreas@392: andreas@392: PEPIdentity *identAlice = [[PEPIdentity alloc] andreas@392: initWithAddress:@"pep.test.alice@pep-project.org" andreas@392: userID:ownUserId andreas@392: userName:@"pEp Test Alice" andreas@392: isOwn:YES andreas@392: fingerPrint:fpr]; andreas@392: andreas@392: [session mySelf:identAlice]; andreas@392: andreas@392: PEPIdentity *identAlice2 = [identAlice mutableCopy]; andreas@392: andreas@392: // This will revoke key andreas@392: [session keyMistrusted:identAlice2]; andreas@416: identAlice2.fingerPrint = nil; andreas@416: [session mySelf:identAlice]; andreas@416: andreas@392: // Check fingerprint is different andreas@392: XCTAssertNotEqualObjects(identAlice2.fingerPrint, fpr); andreas@392: } andreas@392: andreas@392: - (void)testMailToMyself andreas@392: { andreas@392: PEPSession *session = [PEPSession new]; andreas@392: andreas@392: // Our test user : andreas@392: // pEp Test Alice (test key don't use) andreas@392: // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97 dirk@488: XCTAssertTrue([PEPTestUtils importBundledKey:@"6FF00E97_sec.asc" session:session]); andreas@392: andreas@392: PEPIdentity *identAlice = [[PEPIdentity alloc] andreas@392: initWithAddress:@"pep.test.alice@pep-project.org" andreas@392: userID:ownUserId andreas@392: userName:@"pEp Test Alice" andreas@392: isOwn:YES andreas@392: fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"]; andreas@392: andreas@392: [session mySelf:identAlice]; andreas@392: andreas@392: PEPMessage *msg = [PEPMessage new]; andreas@392: msg.from = identAlice; andreas@392: msg.to = @[identAlice]; andreas@392: msg.shortMessage = @"Mail to Myself"; andreas@392: msg.longMessage = @"This is a text content"; andreas@392: msg.direction = PEP_dir_outgoing; andreas@392: andreas@392: PEP_rating clr = [session outgoingColorForMessage:msg]; dirk@420: XCTAssertEqual(clr, PEP_rating_trusted_and_anonymized); andreas@392: dirk@522: NSError *error = nil; dirk@522: PEPMessage *encMsg = [session encryptMessage:msg extraKeys:nil error:&error]; dirk@522: XCTAssertNotNil(encMsg); dirk@522: XCTAssertNil(error); andreas@392: dirk@522: NSArray *keys; andreas@392: dirk@522: error = nil; dirk@517: PEPMessage *decmsg = [session dirk@522: decryptMessage:encMsg dirk@517: rating:&clr dirk@517: keys:&keys dirk@517: error:&error]; dirk@517: XCTAssertNotNil(decmsg); dirk@514: XCTAssertNil(error); dirk@420: XCTAssertEqual(clr, PEP_rating_trusted_and_anonymized); andreas@392: } andreas@392: andreas@392: - (void)testEncryptedMailFromMutt andreas@392: { andreas@392: PEPSession *session = [PEPSession new]; andreas@392: andreas@392: // This is the public key for test001@peptest.ch dirk@488: XCTAssertTrue([PEPTestUtils importBundledKey:@"A3FC7F0A.asc" session:session]); andreas@392: andreas@392: // This is the secret key for test001@peptest.ch dirk@488: XCTAssertTrue([PEPTestUtils importBundledKey:@"A3FC7F0A_sec.asc" session:session]); andreas@392: andreas@392: // Mail from mutt, already processed into message dict by the app. dirk@465: NSMutableDictionary *msgDict = [[PEPTestUtils dirk@465: unarchiveDictionary:@"msg_to_A3FC7F0A_from_mutt.ser"] dirk@465: mutableCopy]; andreas@392: [msgDict removeObjectForKey:kPepLongMessage]; andreas@392: [msgDict removeObjectForKey:kPepLongMessageFormatted]; andreas@392: andreas@392: // Also extracted "live" from the app. dirk@465: NSMutableDictionary *accountDict = [[PEPTestUtils dirk@465: unarchiveDictionary:@"account_A3FC7F0A.ser"] dirk@465: mutableCopy]; andreas@392: [accountDict removeObjectForKey:kPepCommType]; andreas@392: [accountDict removeObjectForKey:kPepFingerprint]; andreas@392: PEPIdentity *identMe = [[PEPIdentity alloc] initWithDictionary:accountDict]; andreas@392: andreas@392: [session mySelf:identMe]; andreas@392: XCTAssertNotNil(identMe.fingerPrint); andreas@392: andreas@392: NSArray* keys; andreas@392: PEPMessage *msg = [PEPMessage new]; andreas@392: [msg setValuesForKeysWithDictionary:msgDict]; dirk@514: dirk@514: // Technically, the mail is encrypted, but the signatures don't match dirk@514: NSError *error; dirk@517: PEPMessage *pepDecryptedMail = [session dirk@517: decryptMessage:msg dirk@517: rating:nil dirk@517: keys:&keys dirk@517: error:&error]; dirk@517: XCTAssertNotNil(pepDecryptedMail); dirk@514: XCTAssertNil(error); dirk@514: andreas@392: XCTAssertNotNil(pepDecryptedMail.longMessage); andreas@392: } andreas@392: andreas@392: - (void)testOutgoingContactColor andreas@392: { andreas@392: PEPSession *session = [PEPSession new]; andreas@392: andreas@411: PEPIdentity *partner1Orig = [PEPTestUtils foreignPepIdentityWithAddress:@"partner1@dontcare.me" andreas@411: userName:@"Partner 1"]; andreas@392: NSString *pubKeyPartner1 = [PEPTestUtils loadResourceByName:@"partner1_F2D281C2789DD7F6_pub.asc"]; andreas@392: XCTAssertNotNil(pubKeyPartner1); andreas@392: [session importKey:pubKeyPartner1]; andreas@392: andreas@392: PEP_rating color = [session identityRating:partner1Orig]; andreas@392: XCTAssertEqual(color, PEP_rating_reliable); andreas@392: } andreas@392: andreas@392: - (void)testGetTrustwords andreas@392: { andreas@392: PEPSession *session = [PEPSession new]; andreas@392: andreas@392: PEPIdentity *partner1Orig = [[PEPIdentity alloc] andreas@392: initWithAddress:@"partner1@dontcare.me" userID:@"partner1" andreas@392: userName:@"partner1" andreas@392: isOwn:NO fingerPrint:@"F0CD3F7B422E5D587ABD885BF2D281C2789DD7F6"]; andreas@392: andreas@392: PEPIdentity *meOrig = [[PEPIdentity alloc] andreas@392: initWithAddress:@"me@dontcare.me" userID:@"me" andreas@392: userName:@"me" andreas@392: isOwn:NO fingerPrint:@"CC1F73F6FB774BF08B197691E3BFBCA9248FC681"]; andreas@392: andreas@392: NSString *pubKeyPartner1 = [PEPTestUtils loadResourceByName:@"partner1_F2D281C2789DD7F6_pub.asc"]; andreas@392: XCTAssertNotNil(pubKeyPartner1); andreas@392: NSString *pubKeyMe = [PEPTestUtils loadResourceByName:@"meATdontcare_E3BFBCA9248FC681_pub.asc"]; andreas@392: XCTAssertNotNil(pubKeyMe); andreas@392: NSString *secKeyMe = [PEPTestUtils loadResourceByName:@"meATdontcare_E3BFBCA9248FC681_sec.asc"]; andreas@392: XCTAssertNotNil(secKeyMe); andreas@392: andreas@392: NSString *trustwordsFull = [session getTrustwordsIdentity1:meOrig identity2:partner1Orig andreas@392: language:nil full:YES]; andreas@392: XCTAssertEqualObjects(trustwordsFull, andreas@392: @"EMERSON GASPER TOKENISM BOLUS COLLAGE DESPISE BEDDED ENCRYPTION IMAGINE BEDFORD"); andreas@392: andreas@392: NSString *trustwordsFullEnglish = [session getTrustwordsIdentity1:meOrig identity2:partner1Orig andreas@392: language:@"en" full:YES]; andreas@392: XCTAssertEqualObjects(trustwordsFullEnglish, trustwordsFull); andreas@392: andreas@392: NSString *trustwordsUndefined = [session getTrustwordsIdentity1:meOrig identity2:partner1Orig andreas@392: language:@"ZZ" full:YES]; andreas@392: XCTAssertNil(trustwordsUndefined); andreas@392: } andreas@392: dirk@418: - (void)testStringToRating dirk@418: { dirk@418: PEPSession *session = [PEPSession new]; dirk@418: XCTAssertEqual([session ratingFromString:@"cannot_decrypt"], PEP_rating_cannot_decrypt); dirk@418: XCTAssertEqual([session ratingFromString:@"have_no_key"], PEP_rating_have_no_key); dirk@418: XCTAssertEqual([session ratingFromString:@"unencrypted"], PEP_rating_unencrypted); dirk@418: XCTAssertEqual([session ratingFromString:@"unencrypted_for_some"], dirk@418: PEP_rating_unencrypted_for_some); dirk@418: XCTAssertEqual([session ratingFromString:@"unreliable"], PEP_rating_unreliable); dirk@418: XCTAssertEqual([session ratingFromString:@"reliable"], PEP_rating_reliable); dirk@418: XCTAssertEqual([session ratingFromString:@"trusted"], PEP_rating_trusted); dirk@418: XCTAssertEqual([session ratingFromString:@"trusted_and_anonymized"], dirk@418: PEP_rating_trusted_and_anonymized); dirk@418: XCTAssertEqual([session ratingFromString:@"fully_anonymous"], PEP_rating_fully_anonymous); dirk@418: XCTAssertEqual([session ratingFromString:@"mistrust"], PEP_rating_mistrust); dirk@418: XCTAssertEqual([session ratingFromString:@"b0rken"], PEP_rating_b0rken); dirk@418: XCTAssertEqual([session ratingFromString:@"under_attack"], PEP_rating_under_attack); dirk@418: XCTAssertEqual([session ratingFromString:@"undefined"], PEP_rating_undefined); dirk@418: XCTAssertEqual([session ratingFromString:@"does not exist111"], PEP_rating_undefined); dirk@418: } dirk@418: dirk@418: - (void)testRatingToString dirk@418: { dirk@418: PEPSession *session = [PEPSession new]; dirk@418: XCTAssertEqualObjects([session stringFromRating:PEP_rating_cannot_decrypt], @"cannot_decrypt"); dirk@418: XCTAssertEqualObjects([session stringFromRating:PEP_rating_have_no_key], @"have_no_key"); dirk@418: XCTAssertEqualObjects([session stringFromRating:PEP_rating_unencrypted], @"unencrypted"); dirk@418: XCTAssertEqualObjects([session stringFromRating:PEP_rating_unencrypted_for_some], dirk@418: @"unencrypted_for_some"); dirk@418: XCTAssertEqualObjects([session stringFromRating:PEP_rating_unreliable], @"unreliable"); dirk@418: XCTAssertEqualObjects([session stringFromRating:PEP_rating_reliable], @"reliable"); dirk@418: XCTAssertEqualObjects([session stringFromRating:PEP_rating_trusted], @"trusted"); dirk@418: XCTAssertEqualObjects([session stringFromRating:PEP_rating_trusted_and_anonymized], dirk@418: @"trusted_and_anonymized"); dirk@418: XCTAssertEqualObjects([session stringFromRating:PEP_rating_fully_anonymous], dirk@418: @"fully_anonymous"); dirk@418: XCTAssertEqualObjects([session stringFromRating:PEP_rating_mistrust], @"mistrust"); dirk@418: XCTAssertEqualObjects([session stringFromRating:PEP_rating_b0rken], @"b0rken"); dirk@418: XCTAssertEqualObjects([session stringFromRating:PEP_rating_under_attack], @"under_attack"); dirk@418: XCTAssertEqualObjects([session stringFromRating:PEP_rating_undefined], @"undefined"); dirk@418: XCTAssertEqualObjects([session stringFromRating:500], @"undefined"); dirk@418: } dirk@418: dirk@431: - (void)testIsPEPUser dirk@431: { dirk@431: PEPSession *session = [PEPSession new]; dirk@431: dirk@431: PEPIdentity *identMe = [[PEPIdentity alloc] dirk@431: initWithAddress:@"me-myself-and-i@pep-project.org" dirk@431: userID:@"me-myself-and-i" dirk@452: userName:@"pEp Me" dirk@431: isOwn:YES]; dirk@431: [session mySelf:identMe]; dirk@431: XCTAssertNotNil(identMe.fingerPrint); dirk@432: dirk@432: // PEP_CANNOT_FIND_PERSON == 902 dirk@453: XCTAssertTrue([session isPEPUser:identMe]); dirk@431: } dirk@431: dirk@449: - (void)testXEncStatusForOutgoingEncryptedMail dirk@449: { dirk@449: [self helperXEncStatusForOutgoingEncryptdMailToSelf:NO expectedRating:PEP_rating_reliable]; dirk@449: } dirk@449: dirk@449: - (void)testXEncStatusForOutgoingSelfEncryptedMail dirk@449: { dirk@449: [self helperXEncStatusForOutgoingEncryptdMailToSelf:YES dirk@449: expectedRating:PEP_rating_trusted_and_anonymized]; dirk@449: } dirk@449: dirk@456: - (void)testEncryptMessagesWithoutKeys dirk@456: { dirk@456: PEPSession *session = [PEPSession new]; dirk@456: dirk@456: PEPIdentity *identMe = [[PEPIdentity alloc] dirk@456: initWithAddress:@"me-myself-and-i@pep-project.org" dirk@456: userID:@"me-myself-and-i" dirk@456: userName:@"pEp Me" dirk@456: isOwn:YES]; dirk@456: [session mySelf:identMe]; dirk@456: XCTAssertNotNil(identMe.fingerPrint); dirk@456: dirk@456: PEPIdentity *identAlice = [[PEPIdentity alloc] dirk@456: initWithAddress:@"alice@pep-project.org" dirk@456: userID:@"alice" dirk@456: userName:@"pEp Test Alice" dirk@456: isOwn:NO]; dirk@456: dirk@456: PEPMessage *msg = [PEPMessage new]; dirk@456: msg.from = identMe; dirk@456: msg.to = @[identAlice]; dirk@456: msg.shortMessage = @"Mail to Alice"; dirk@456: msg.longMessage = @"Alice?"; dirk@456: msg.direction = PEP_dir_outgoing; dirk@456: dirk@456: PEP_rating clr = [session outgoingColorForMessage:msg]; dirk@456: XCTAssertEqual(clr, PEP_rating_unencrypted); dirk@456: dirk@522: NSError *error = nil; dirk@522: PEPMessage *encMsg = [session encryptMessage:msg extraKeys:nil error:&error]; dirk@522: XCTAssertNotNil(encMsg); dirk@522: XCTAssertNil(error); dirk@456: dirk@456: XCTAssertNotNil(encMsg); dirk@456: dirk@456: PEPStringList *keys; dirk@514: PEP_rating pEpRating; dirk@522: error = nil; dirk@517: PEPMessage *decMsg = [session dirk@517: decryptMessage:encMsg dirk@517: rating:&pEpRating dirk@517: keys:&keys dirk@517: error:&error]; dirk@517: XCTAssertNotNil(decMsg); dirk@514: XCTAssertNil(error); dirk@514: dirk@456: XCTAssertEqual(pEpRating, PEP_rating_unencrypted); dirk@456: XCTAssertNotNil(decMsg); dirk@456: } dirk@456: dirk@456: /** dirk@456: ENGINE-364. Tries to invoke trustPersonalKey on an identity without key, dirk@456: giving it a fake fingerprint. dirk@456: */ dirk@456: - (void)testTrustPersonalKey dirk@456: { dirk@456: PEPSession *session = [PEPSession new]; dirk@456: dirk@456: PEPIdentity *identMe = [[PEPIdentity alloc] dirk@456: initWithAddress:@"me-myself-and-i@pep-project.org" dirk@456: userID:@"me-myself-and-i" dirk@456: userName:@"pEp Me" dirk@456: isOwn:YES]; dirk@456: [session mySelf:identMe]; dirk@456: XCTAssertNotNil(identMe.fingerPrint); dirk@456: dirk@456: // The fingerprint is definitely wrong, we don't have a key dirk@456: PEPIdentity *identAlice = [[PEPIdentity alloc] dirk@456: initWithAddress:@"alice@pep-project.org" dirk@456: userID:@"alice" dirk@456: userName:@"pEp Test Alice" dirk@456: isOwn:NO dirk@456: fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"]; dirk@456: dirk@456: [session trustPersonalKey:identAlice]; dirk@456: } dirk@456: dirk@457: /** dirk@457: ENGINE-381 dirk@457: */ dirk@457: - (void)testVolatileIdentityRating dirk@457: { dirk@457: PEPSession *session = [PEPSession new]; dirk@457: dirk@457: PEPIdentity *identMe = [[PEPIdentity alloc] dirk@457: initWithAddress:@"me-myself-and-i@pep-project.org" dirk@457: userID:@"me-myself-and-i" dirk@457: userName:@"pEp Me" dirk@457: isOwn:YES]; dirk@457: [session mySelf:identMe]; dirk@457: XCTAssertNotNil(identMe.fingerPrint); dirk@457: dirk@457: PEPIdentity *identAlice = [self dirk@457: checkImportingKeyFilePath:@"6FF00E97_sec.asc" dirk@457: address:@"pep.test.alice@pep-project.org" dirk@457: userID:@"alice_user_id" dirk@457: fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97" dirk@457: session: session]; dirk@488: XCTAssertNotNil(identAlice); dirk@457: dirk@457: dispatch_group_t identityRatingGroup = dispatch_group_create(); dirk@457: dirk@457: void (^ratingBlock)(void) = ^{ dirk@457: PEPSession *innerSession = [PEPSession new]; dirk@457: PEP_rating rating = [innerSession identityRating:identAlice]; dirk@457: XCTAssertEqual(rating, PEP_rating_reliable); dirk@457: }; dirk@457: dirk@479: for (int i = 0; i < 4; ++i) { dirk@457: dispatch_group_async(identityRatingGroup, dirk@457: dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), dirk@457: ratingBlock); dirk@457: } dirk@457: dirk@479: for (int i = 0; i < 4; ++i) { dirk@457: ratingBlock(); dirk@457: } dirk@457: dirk@457: dispatch_group_wait(identityRatingGroup, DISPATCH_TIME_FOREVER); dirk@457: } dirk@457: dirk@449: #pragma mark - configUnencryptedSubject dirk@449: dirk@449: - (void)testConfigUnencryptedSubject dirk@449: { dirk@449: // Setup Config to encrypt subject dirk@449: [PEPObjCAdapter setUnecryptedSubjectEnabled:NO]; dirk@449: dirk@449: // Write mail to yourself ... dirk@449: PEPMessage *encMessage = [self mailWrittenToMySelf]; dirk@449: dirk@449: // ... and assert subject is encrypted dirk@449: XCTAssertEqualObjects(encMessage.shortMessage, @"p≡p", @"Subject should be encrypted"); dirk@449: } dirk@449: dirk@449: - (void)testConfigUnencryptedSubject_encryptedSubjectDisabled dirk@449: { dirk@449: // Setup Config to not encrypt subject dirk@449: [PEPObjCAdapter setUnecryptedSubjectEnabled:YES]; dirk@449: dirk@449: // Write mail to yourself ... dirk@449: PEPMessage *encMessage = [self mailWrittenToMySelf]; dirk@449: dirk@449: // ... and assert the subject is not encrypted dirk@449: XCTAssertNotEqualObjects(encMessage.shortMessage, @"p≡p", @"Subject should not be encrypted"); dirk@449: } dirk@449: andreas@392: #pragma mark - Helpers andreas@392: dirk@439: - (PEPIdentity *)checkImportingKeyFilePath:(NSString *)filePath address:(NSString *)address dirk@439: userID:(NSString *)userID dirk@439: fingerPrint:(NSString *)fingerPrint dirk@456: session:(PEPSession *)session dirk@437: { dirk@456: if (!session) { dirk@456: session = [PEPSession new]; dirk@456: } dirk@437: dirk@488: BOOL success = [PEPTestUtils importBundledKey:filePath session:session]; dirk@488: XCTAssertTrue(success); dirk@437: dirk@488: if (success) { dirk@488: // Our test user: dirk@488: PEPIdentity *identTest = [[PEPIdentity alloc] dirk@488: initWithAddress:address dirk@488: userID:userID dirk@488: userName:[NSString stringWithFormat:@"Some User Name %@", userID] dirk@488: isOwn:NO]; dirk@437: dirk@488: [session updateIdentity:identTest]; dirk@488: XCTAssertNotNil(identTest.fingerPrint); dirk@488: XCTAssertEqualObjects(identTest.fingerPrint, fingerPrint); dirk@439: dirk@488: return identTest; dirk@488: } else { dirk@488: return nil; dirk@488: } dirk@439: } dirk@439: dirk@439: - (PEPIdentity *)checkMySelfImportingKeyFilePath:(NSString *)filePath address:(NSString *)address dirk@439: userID:(NSString *)userID dirk@439: fingerPrint:(NSString *)fingerPrint dirk@491: session:(PEPSession *)session dirk@439: { dirk@494: XCTAssertTrue([PEPTestUtils importBundledKey:filePath session:session]); dirk@439: dirk@439: // Our test user: dirk@439: PEPIdentity *identTest = [[PEPIdentity alloc] dirk@439: initWithAddress:address dirk@439: userID:userID dirk@439: userName:[NSString stringWithFormat:@"Some User Name %@", userID] dirk@439: isOwn:YES dirk@439: fingerPrint: fingerPrint]; dirk@439: dirk@502: NSError *error; dirk@502: XCTAssertTrue([session setOwnKey:identTest fingerprint:fingerPrint error:&error]); dirk@502: XCTAssertNil(error); dirk@439: XCTAssertNotNil(identTest.fingerPrint); dirk@439: XCTAssertEqualObjects(identTest.fingerPrint, fingerPrint); dirk@439: dirk@439: return identTest; dirk@437: } dirk@437: dirk@433: /** dirk@433: Verifies that a partner ID is really a correct Identity. dirk@433: Usually used on identities imported as keys, since the engine has problems with them. dirk@433: */ dirk@433: - (void)updateAndVerifyPartnerIdentity:(PEPIdentity *)partnerIdentity session:(PEPSession *)session dirk@433: { dirk@433: XCTAssertNotNil(partnerIdentity.fingerPrint); dirk@433: [session updateIdentity:partnerIdentity]; dirk@433: XCTAssertNotNil(partnerIdentity.fingerPrint); dirk@433: NSString *fingerprint = partnerIdentity.fingerPrint; dirk@433: partnerIdentity.fingerPrint = nil; dirk@433: [session updateIdentity:partnerIdentity]; dirk@433: XCTAssertNotNil(partnerIdentity.fingerPrint); dirk@433: XCTAssertEqualObjects(partnerIdentity.fingerPrint, fingerprint); dirk@433: } dirk@433: andreas@416: - (PEPMessage *)mailWrittenToMySelf andreas@416: { andreas@416: PEPSession *session = [PEPSession new]; andreas@416: andreas@416: // Write a e-mail to yourself ... andreas@416: PEPIdentity *me = [PEPTestUtils ownPepIdentityWithAddress:@"me@peptest.ch" andreas@416: userName:@"userName"]; andreas@416: [session mySelf:me]; andreas@416: andreas@416: NSString *shortMessage = @"Subject"; andreas@416: NSString *longMessage = @"Oh, this is a long body text!"; andreas@416: PEPMessage *mail = [PEPTestUtils mailFrom:me andreas@416: toIdent:me andreas@416: shortMessage:shortMessage andreas@416: longMessage:longMessage andreas@416: outgoing:YES]; andreas@416: PEPMessage *encMessage; andreas@416: [session encryptMessage:mail identity:me dest:&encMessage]; andreas@416: andreas@416: return encMessage; andreas@416: } andreas@416: andreas@392: - (PEPMessage *)internalEncryptToMySelfKeys:(PEPStringList **)keys andreas@392: { andreas@392: PEPSession *session = [PEPSession new]; andreas@411: andreas@411: PEPIdentity *me = [PEPTestUtils ownPepIdentityWithAddress:@"me@peptest.ch" andreas@411: userName:@"userName"]; andreas@392: [session mySelf:me]; andreas@392: XCTAssertNotNil(me.fingerPrint); andreas@392: andreas@392: // Create draft andreas@392: NSString *shortMessage = @"Subject"; andreas@392: NSString *longMessage = @"Oh, this is a long body text!"; andreas@392: PEPMessage *mail = [PEPTestUtils mailFrom:me toIdent:me shortMessage:shortMessage longMessage:longMessage outgoing:YES]; andreas@392: andreas@392: PEPMessage *encMessage; andreas@392: PEP_STATUS status = [session encryptMessage:mail identity:me dest:&encMessage]; andreas@392: XCTAssertEqual(status, 0); andreas@392: XCTAssertEqualObjects(encMessage.shortMessage, @"p≡p"); andreas@392: dirk@514: PEP_rating rating; dirk@514: NSError *error; dirk@517: PEPMessage *unencDict = [session dirk@517: decryptMessage:encMessage dirk@517: rating:&rating dirk@517: keys:keys dirk@517: error:&error]; dirk@517: XCTAssertNotNil(unencDict); dirk@514: XCTAssertNil(error); dirk@514: andreas@392: XCTAssertGreaterThanOrEqual(rating, PEP_rating_reliable); andreas@392: andreas@392: XCTAssertEqualObjects(unencDict.shortMessage, shortMessage); andreas@392: XCTAssertEqualObjects(unencDict.longMessage, longMessage); andreas@392: andreas@392: return unencDict; andreas@392: } andreas@392: andreas@392: - (void)pEpCleanUp andreas@392: { dirk@470: [PEPTestUtils cleanUp]; andreas@392: } andreas@392: dirk@424: - (void)helperXEncStatusForOutgoingEncryptdMailToSelf:(BOOL)toSelf dirk@424: expectedRating:(PEP_rating)expectedRating dirk@422: { dirk@422: PEPSession *session = [PEPSession new]; dirk@422: dirk@422: // Partner pubkey for the test: dirk@422: // pEp Test Alice (test key don't use) dirk@422: // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97 dirk@494: XCTAssertTrue([PEPTestUtils importBundledKey:@"0x6FF00E97.asc" session:session]); dirk@422: dirk@422: PEPIdentity *identAlice = [[PEPIdentity alloc] dirk@422: initWithAddress:@"pep.test.alice@pep-project.org" dirk@422: userID:ownUserId dirk@422: userName:@"pEp Test Alice" dirk@422: isOwn:NO dirk@433: fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"]; dirk@433: [self updateAndVerifyPartnerIdentity:identAlice session:session]; dirk@422: dirk@422: PEPIdentity *identMe = [[PEPIdentity alloc] dirk@422: initWithAddress:@"me-myself-and-i@pep-project.org" dirk@422: userID:@"me-myself-and-i" dirk@452: userName:@"pEp Me" dirk@422: isOwn:YES]; dirk@422: [session mySelf:identMe]; dirk@422: XCTAssertNotNil(identMe.fingerPrint); dirk@422: dirk@422: PEPMessage *msg = [PEPMessage new]; dirk@422: msg.from = identMe; dirk@422: msg.to = @[identAlice]; dirk@422: msg.shortMessage = @"Mail to Alice"; dirk@422: msg.longMessage = @"Alice?"; dirk@422: msg.direction = PEP_dir_outgoing; dirk@422: dirk@422: PEP_rating clr = [session outgoingColorForMessage:msg]; dirk@424: XCTAssertEqual(clr, PEP_rating_reliable); dirk@422: dirk@423: PEPMessage *encMsg; dirk@424: dirk@424: PEP_STATUS statusEnc = PEP_VERSION_MISMATCH; dirk@424: if (toSelf) { dirk@424: statusEnc = [session encryptMessage:msg identity:identMe dest:&encMsg]; dirk@522: XCTAssertEqual(statusEnc, PEP_STATUS_OK); dirk@424: } else { dirk@522: NSError *error = nil; dirk@522: encMsg = [session encryptMessage:msg extraKeys:nil error:&error]; dirk@522: XCTAssertNotNil(encMsg); dirk@522: XCTAssertNil(error); dirk@424: } dirk@423: XCTAssertNotNil(encMsg); dirk@423: dirk@423: PEPStringList *keys; dirk@514: PEP_rating pEpRating; dirk@514: NSError *error; dirk@517: PEPMessage *decMsg = [session dirk@517: decryptMessage:encMsg dirk@517: rating:&pEpRating dirk@517: keys:&keys dirk@517: error:&error]; dirk@514: XCTAssertNil(error); dirk@517: XCTAssertNotNil(decMsg); dirk@514: dirk@424: XCTAssertEqual(pEpRating, expectedRating); dirk@423: dirk@423: NSArray * encStatusField = nil; dirk@423: for (NSArray *field in decMsg.optionalFields) { dirk@423: NSString *header = [field[0] lowercaseString]; dirk@423: if ([header isEqualToString:@"x-encstatus"]) { dirk@423: encStatusField = field; dirk@423: } dirk@423: } dirk@423: XCTAssertNotNil(encStatusField); dirk@423: if (encStatusField) { dirk@423: PEP_rating outgoingRating = [session ratingFromString:encStatusField[1]]; dirk@424: XCTAssertEqual(outgoingRating, expectedRating); dirk@423: } dirk@422: } dirk@422: andreas@392: @end