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" dirk@577: #import "PEPAttachment.h" andreas@392: #import "PEPTestUtils.h" dirk@706: #import "PEPSync.h" dirk@732: #import "PEPSendMessageDelegate.h" dirk@706: dirk@711: #import "PEPSessionTestNotifyHandshakeDelegate.h" dirk@711: #import "PEPSessionTestSendMessageDelegate.h" andreas@392: andreas@392: @interface PEPSessionTest : XCTestCase dirk@706: dirk@706: @property (nonatomic) PEPSync *sync; dirk@711: @property (nonatomic) PEPSessionTestSendMessageDelegate *sendMessageDelegate; dirk@711: @property (nonatomic) PEPSessionTestNotifyHandshakeDelegate *notifyHandshakeDelegate; dirk@706: andreas@392: @end andreas@392: andreas@392: @implementation PEPSessionTest andreas@392: andreas@392: - (void)setUp andreas@392: { andreas@392: [super setUp]; dirk@706: dirk@739: [self pEpCleanUp]; dirk@739: dirk@763: [self startSync]; dirk@706: dirk@601: [PEPObjCAdapter setUnEncryptedSubjectEnabled:NO]; andreas@392: } andreas@392: dirk@525: - (void)tearDown dirk@525: { dirk@706: [self.sync shutdown]; dirk@706: andreas@392: [self pEpCleanUp]; andreas@392: [super tearDown]; andreas@392: } andreas@392: andreas@392: - (void)testTrustWords andreas@392: { andreas@392: PEPSession *session = [PEPSession new]; andreas@392: dirk@531: NSError *error = nil; dirk@531: NSArray *trustwords = [session dirk@531: trustwordsForFingerprint:@"DB47DB47DB47DB47DB47DB47DB47DB47DB47DB47" dirk@531: languageID:@"en" dirk@531: shortened:false dirk@531: error:&error]; dirk@531: XCTAssertNil(error); 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: dirk@532: NSError *error = nil; dirk@532: XCTAssertTrue([session mySelf:identMe error:&error]); dirk@532: XCTAssertNil(error); andreas@392: andreas@392: XCTAssertNotNil(identMe.fingerPrint); andreas@392: XCTAssertNotEqual(identMe.commType, PEP_ct_unknown); andreas@392: dirk@567: XCTAssertTrue([identMe isPEPUser:session error:&error]); 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" dirk@533: userID:@"pep.test.iosgenkey@pep-project.org_userID" andreas@392: userName:@"pEp Test iOS GenKey" andreas@392: isOwn:YES]; andreas@392: dirk@532: NSError *error = nil; dirk@532: XCTAssertTrue([session mySelf:identMe error:&error]); dirk@532: XCTAssertNil(error); andreas@392: andreas@392: XCTAssertNotNil(identMe.fingerPrint); andreas@392: XCTAssertNotEqual(identMe.commType, PEP_ct_unknown); andreas@392: dirk@567: XCTAssertTrue([identMe isPEPUser:session error:&error]); andreas@392: andreas@392: dispatch_queue_t queue = dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0); andreas@392: dispatch_sync(queue, ^{ dirk@532: NSError *innerError = nil; 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]; dirk@532: dirk@532: XCTAssertTrue([session2 mySelf:identMe2 error:&innerError]); dirk@532: XCTAssertNil(innerError); dirk@532: andreas@392: XCTAssertNotNil(identMe2.fingerPrint); dirk@567: XCTAssertTrue([identMe2 isPEPUser:session error:&innerError]); 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]; dirk@532: XCTAssertTrue([session2 mySelf:identMe3 error:&innerError]); dirk@532: XCTAssertNil(innerError); dirk@532: andreas@392: XCTAssertNotNil(identMe3.fingerPrint); dirk@567: XCTAssertTrue([identMe3 isPEPUser:session error:&innerError]); 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@532: NSError *error = nil; dirk@532: XCTAssertTrue([session updateIdentity:identRandom error:&error]); dirk@532: XCTAssertNil(error); 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@529: XCTAssertEqual([self ratingForIdentity:me session:session], 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@529: XCTAssertEqual([self ratingForIdentity:alice session:session], PEP_rating_reliable); dirk@439: } dirk@439: dirk@546: /** ENGINE-409 */ dirk@546: - (void)testIdentityRatingMistrustReset dirk@546: { dirk@546: PEPSession *session = [PEPSession new]; dirk@546: dirk@546: PEPIdentity *me = [[PEPIdentity alloc] dirk@546: initWithAddress:@"me@example.org" dirk@546: userID:@"me_myself" dirk@546: userName:@"Me Me" dirk@546: isOwn:YES]; dirk@546: dirk@546: NSError *error = nil; dirk@546: XCTAssertTrue([session mySelf:me error:&error]); dirk@546: XCTAssertNil(error); dirk@546: dirk@546: XCTAssertNotNil(me.fingerPrint); dirk@546: XCTAssertEqual([self ratingForIdentity:me session:session], PEP_rating_trusted_and_anonymized); dirk@546: dirk@546: PEPIdentity *alice = [self dirk@546: checkImportingKeyFilePath:@"6FF00E97_sec.asc" dirk@546: address:@"pep.test.alice@pep-project.org" dirk@546: userID:@"This Is Alice" dirk@546: fingerPrint:@"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97" dirk@546: session: session]; dirk@546: XCTAssertNotNil(alice); dirk@546: XCTAssertEqual([self ratingForIdentity:alice session:session], PEP_rating_reliable); dirk@546: dirk@546: XCTAssertTrue([session keyMistrusted:alice error:&error]); dirk@546: XCTAssertNil(error); dirk@546: XCTAssertEqual([self ratingForIdentity:alice session:session], PEP_rating_have_no_key); dirk@546: } dirk@546: 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@532: dirk@532: NSError *error = nil; dirk@532: XCTAssertTrue([session mySelf:me error:&error]); dirk@532: XCTAssertNil(error); dirk@532: dirk@460: XCTAssertNotNil(me.fingerPrint); dirk@529: XCTAssertEqual([self ratingForIdentity:me session:session], 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@529: XCTAssertEqual([self ratingForIdentity:alice session:session], PEP_rating_reliable); dirk@445: dirk@534: XCTAssertTrue([session trustPersonalKey:alice error:&error]); dirk@534: XCTAssertNil(error); dirk@529: XCTAssertEqual([self ratingForIdentity:alice session:session], PEP_rating_trusted); dirk@446: dirk@545: XCTAssertTrue([session keyResetTrust:alice error:&error]); dirk@545: XCTAssertNil(error); dirk@529: XCTAssertEqual([self ratingForIdentity:alice session:session], PEP_rating_reliable); dirk@446: dirk@536: XCTAssertTrue([session keyMistrusted:alice error:&error]); dirk@536: XCTAssertNil(error); dirk@529: XCTAssertEqual([self ratingForIdentity:alice session:session], 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@532: NSError *error = nil; dirk@532: XCTAssertTrue([session mySelf:me error:&error]); dirk@532: XCTAssertNil(error); dirk@532: dirk@472: XCTAssertNotNil(me.fingerPrint); dirk@529: XCTAssertEqual([self ratingForIdentity:me session:session], 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@529: XCTAssertEqual([self ratingForIdentity:alice session:session], PEP_rating_reliable); dirk@472: dirk@534: XCTAssertTrue([session trustPersonalKey:alice error:&error]); dirk@534: XCTAssertNil(error); dirk@529: XCTAssertEqual([self ratingForIdentity:alice session:session], PEP_rating_trusted); dirk@472: dirk@545: XCTAssertTrue([session keyResetTrust:alice error:&error]); dirk@545: XCTAssertNil(error); dirk@529: XCTAssertEqual([self ratingForIdentity:alice session:session], PEP_rating_reliable); dirk@472: dirk@536: XCTAssertTrue([session keyMistrusted:alice error:&error]); dirk@536: XCTAssertNil(error); dirk@529: XCTAssertEqual([self ratingForIdentity:alice session:session], PEP_rating_have_no_key); 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@532: NSError *error = nil; dirk@532: XCTAssertTrue([session mySelf:me error:&error]); dirk@532: XCTAssertNil(error); dirk@532: dirk@459: XCTAssertNotNil(me.fingerPrint); dirk@529: XCTAssertEqual([self ratingForIdentity:me session:session], 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@529: XCTAssertEqual([self ratingForIdentity:alice session:session], 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@526: dirk@526: PEP_STATUS status; dirk@526: NSError *error = nil; dirk@526: PEPMessage *encMsg = [innerSession dirk@526: encryptMessage:msg dirk@557: forSelf:me dirk@556: extraKeys:nil dirk@526: status:&status error:&error]; dirk@458: XCTAssertEqual(status, PEP_STATUS_OK); dirk@526: XCTAssertNotNil(encMsg); 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@534: XCTAssertTrue([session trustPersonalKey:alice error:&error]); dirk@534: XCTAssertNil(error); dirk@529: XCTAssertEqual([self ratingForIdentity:alice session:session], PEP_rating_trusted); dirk@458: dirk@545: XCTAssertTrue([session keyResetTrust:alice error:&error]); dirk@545: XCTAssertNil(error); dirk@529: XCTAssertEqual([self ratingForIdentity:alice session:session], PEP_rating_reliable); dirk@458: dirk@536: XCTAssertTrue([session keyMistrusted:alice error:&error]); dirk@536: XCTAssertNil(error); dirk@529: XCTAssertEqual([self ratingForIdentity:alice session:session], PEP_rating_have_no_key); 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@528: NSError *error = nil; dirk@528: dirk@441: // Test with unknown Bob dirk@653: NSNumber *numRating = [self dirk@653: testOutgoingRatingForMessage:msgGray dirk@653: session:session dirk@653: error:&error]; dirk@573: XCTAssertNotNil(numRating); dirk@573: XCTAssertNil(error); dirk@573: XCTAssertEqual(numRating.pEpRating, 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@528: NSError *error = nil; dirk@528: dirk@441: // Should be yellow, since no handshake happened. dirk@653: NSNumber *numRating = [self testOutgoingRatingForMessage:msg session:session error:&error]; dirk@573: XCTAssertNotNil(numRating); dirk@528: XCTAssertNil(error); dirk@573: XCTAssertEqual(numRating.pEpRating, PEP_rating_reliable); andreas@392: dirk@573: PEP_rating rating = [self ratingForIdentity:identBob session:session]; dirk@528: XCTAssertEqual(rating, PEP_rating_reliable); andreas@392: andreas@392: // Let' say we got that handshake, set PEP_ct_confirmed in Bob's identity dirk@534: XCTAssertTrue([session trustPersonalKey:identBob error:&error]); dirk@534: XCTAssertNil(error); andreas@392: andreas@392: // This time it should be green dirk@653: numRating = [self testOutgoingRatingForMessage:msg session:session error:&error]; dirk@573: XCTAssertNotNil(numRating); dirk@573: XCTAssertNil(error); dirk@573: XCTAssertEqual(numRating.pEpRating, PEP_rating_trusted); andreas@392: dirk@529: rating = [self ratingForIdentity:identBob session:session]; dirk@528: XCTAssertEqual(rating, PEP_rating_trusted); andreas@392: andreas@392: // Let' say we undo handshake dirk@545: XCTAssertTrue([session keyResetTrust:identBob error:&error]); dirk@545: XCTAssertNil(error); andreas@392: andreas@392: // Yellow ? dirk@653: numRating = [self testOutgoingRatingForMessage:msg session:session error:&error]; dirk@573: XCTAssertNotNil(numRating); dirk@573: XCTAssertNil(error); dirk@573: XCTAssertEqual(numRating.pEpRating, PEP_rating_reliable); andreas@392: andreas@392: // mistrust Bob dirk@536: XCTAssertTrue([session keyMistrusted:identBob error:&error]); dirk@536: XCTAssertNil(error); andreas@392: dirk@444: identBob.fingerPrint = nil; dirk@532: XCTAssertTrue([session updateIdentity:identBob error:&error]); dirk@532: XCTAssertNil(error); dirk@441: XCTAssertNil(identBob.fingerPrint); dirk@441: andreas@392: // Gray == PEP_rating_unencrypted dirk@653: numRating = [self testOutgoingRatingForMessage:msg session:session error:&error]; dirk@573: XCTAssertNotNil(numRating); dirk@573: XCTAssertNil(error); dirk@573: XCTAssertEqual(numRating.pEpRating, PEP_rating_unencrypted); 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: dirk@532: NSError *error = nil; dirk@532: XCTAssertTrue([session mySelf:identAlice error:&error]); dirk@532: XCTAssertNil(error); 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 dirk@528: PEP_rating rating; dirk@653: NSNumber *numRating = [self testOutgoingRatingForMessage:msg session:session error:&error]; dirk@573: XCTAssertNotNil(numRating); dirk@573: XCTAssertNil(error); dirk@573: XCTAssertEqual(numRating.pEpRating, 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: dirk@532: XCTAssertTrue([session updateIdentity:identBob error:&error]); dirk@532: XCTAssertNil(error); andreas@392: andreas@392: // Should be yellow, since no handshake happened. dirk@653: numRating = [self testOutgoingRatingForMessage:msg session:session error:&error]; dirk@573: XCTAssertNotNil(numRating); dirk@573: XCTAssertNil(error); dirk@573: XCTAssertEqual(numRating.pEpRating, PEP_rating_reliable); andreas@392: dirk@529: rating = [self ratingForIdentity:identBob session:session]; dirk@528: XCTAssertEqual(rating, PEP_rating_reliable); andreas@392: andreas@392: // Let' say we got that handshake, set PEP_ct_confirmed in Bob's identity dirk@534: XCTAssertTrue([session trustPersonalKey:identBob error:&error]); dirk@534: XCTAssertNil(error); andreas@392: andreas@392: // This time it should be green dirk@653: numRating = [self testOutgoingRatingForMessage:msg session:session error:&error]; dirk@573: XCTAssertNotNil(numRating); dirk@573: XCTAssertNil(error); dirk@573: XCTAssertEqual(numRating.pEpRating, PEP_rating_trusted); andreas@392: dirk@529: rating = [self ratingForIdentity:identBob session:session]; dirk@528: XCTAssertEqual(rating, 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: dirk@532: XCTAssertTrue([session updateIdentity:identJohn error:&error]); dirk@532: XCTAssertNil(error); 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 ? dirk@653: numRating = [self testOutgoingRatingForMessage:msg session:session error:&error]; dirk@573: XCTAssertNotNil(numRating); dirk@573: XCTAssertNil(error); dirk@573: XCTAssertEqual(numRating.pEpRating, PEP_rating_reliable); andreas@392: dirk@534: XCTAssertTrue([session trustPersonalKey:identJohn error:&error]); dirk@534: XCTAssertNil(error); andreas@392: andreas@392: // This time it should be green dirk@653: numRating = [self testOutgoingRatingForMessage:msg session:session error:&error]; dirk@573: XCTAssertNotNil(numRating); dirk@573: XCTAssertNil(error); dirk@573: XCTAssertEqual(numRating.pEpRating, PEP_rating_trusted); andreas@392: dirk@529: rating = [self ratingForIdentity:identJohn session:session]; dirk@528: XCTAssertEqual(rating, 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: dirk@532: NSError *error = nil; dirk@532: XCTAssertTrue([session mySelf:identAlice error:&error]); dirk@532: XCTAssertNil(error); 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: dirk@532: XCTAssertTrue([session updateIdentity:identBob error:&error]); dirk@532: XCTAssertNil(error); andreas@392: andreas@392: // mistrust Bob dirk@536: XCTAssertTrue([session keyMistrusted:identBob error:&error]); dirk@536: XCTAssertNil(error); 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 dirk@653: NSNumber *numRating = [self testOutgoingRatingForMessage:msg session:session error:&error]; dirk@573: XCTAssertNotNil(numRating); dirk@573: XCTAssertNil(error); dirk@573: XCTAssertEqual(numRating.pEpRating, PEP_rating_unencrypted); andreas@392: dirk@524: PEPMessage *encMsg = [session encryptMessage:msg extraKeys:nil status:nil error:&error]; dirk@522: XCTAssertNotNil(encMsg); dirk@522: XCTAssertNil(error); andreas@392: dirk@577: 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: dirk@532: NSError *error = nil; dirk@532: XCTAssertTrue([session mySelf:identAlice error:&error]); dirk@532: XCTAssertNil(error); andreas@392: andreas@392: PEPIdentity *identAlice2 = [identAlice mutableCopy]; andreas@392: andreas@392: // This will revoke key dirk@536: XCTAssertTrue([session keyMistrusted:identAlice2 error:&error]); dirk@536: XCTAssertNil(error); andreas@416: identAlice2.fingerPrint = nil; dirk@532: dirk@532: XCTAssertTrue([session mySelf:identAlice error:&error]); dirk@532: XCTAssertNil(error); 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: dirk@532: NSError *error = nil; dirk@532: XCTAssertTrue([session mySelf:identAlice error:&error]); dirk@532: XCTAssertNil(error); 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: dirk@653: NSNumber *numRating = [self testOutgoingRatingForMessage:msg session:session error:&error]; dirk@573: XCTAssertNotNil(numRating); dirk@573: XCTAssertNil(error); dirk@573: XCTAssertEqual(numRating.pEpRating, PEP_rating_trusted_and_anonymized); dirk@528: dirk@524: PEPMessage *encMsg = [session encryptMessage:msg extraKeys:nil status:nil error:&error]; dirk@522: XCTAssertNotNil(encMsg); dirk@522: XCTAssertNil(error); andreas@392: dirk@522: NSArray *keys; andreas@392: dirk@522: error = nil; dirk@600: PEP_rating rating = PEP_rating_undefined; dirk@517: PEPMessage *decmsg = [session dirk@522: decryptMessage:encMsg dirk@583: flags:nil dirk@528: rating:&rating dirk@523: extraKeys:&keys dirk@524: status:nil dirk@517: error:&error]; dirk@517: XCTAssertNotNil(decmsg); dirk@514: XCTAssertNil(error); dirk@528: XCTAssertEqual(rating, PEP_rating_trusted_and_anonymized); andreas@392: } andreas@392: dirk@632: - (void)testEncryptedMailFromMuttWithReencryption 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]; dirk@579: [PEPTestUtils migrateUnarchivedMessageDictionary:msgDict]; 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: dirk@532: NSError *error = nil; dirk@532: XCTAssertTrue([session mySelf:identMe error:&error]); dirk@532: XCTAssertNil(error); dirk@532: andreas@392: XCTAssertNotNil(identMe.fingerPrint); andreas@392: andreas@392: NSArray* keys; andreas@392: PEPMessage *msg = [PEPMessage new]; andreas@392: [msg setValuesForKeysWithDictionary:msgDict]; dirk@590: PEPMessage *msgOriginal = [PEPMessage new]; dirk@590: [msgOriginal setValuesForKeysWithDictionary:msgDict]; dirk@514: dirk@590: XCTAssertEqualObjects(msg, msgOriginal); dirk@590: dirk@600: PEP_rating rating = PEP_rating_undefined; dirk@590: PEP_decrypt_flags flags = PEP_decrypt_flag_untrusted_server; dirk@590: dirk@517: PEPMessage *pepDecryptedMail = [session dirk@517: decryptMessage:msg dirk@590: flags:&flags dirk@590: rating:&rating dirk@523: extraKeys:&keys dirk@524: status:nil dirk@517: error:&error]; dirk@517: XCTAssertNotNil(pepDecryptedMail); dirk@514: XCTAssertNil(error); dirk@514: dirk@590: // Technically, the mail is encrypted, but the signatures don't match dirk@590: XCTAssertEqual(rating, PEP_rating_unreliable); dirk@590: dirk@632: // Since we're requesting re-encryption, src should have been changed dirk@632: XCTAssertNotEqualObjects(msg, msgOriginal); dirk@590: 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); dirk@551: dirk@551: NSError *error = nil; dirk@551: XCTAssertTrue([session importKey:pubKeyPartner1 error:&error]); dirk@551: XCTAssertNil(error); andreas@392: dirk@529: PEP_rating color = [self ratingForIdentity:partner1Orig session:session]; 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: dirk@558: NSError *error = nil; andreas@392: NSString *trustwordsFull = [session getTrustwordsIdentity1:meOrig identity2:partner1Orig dirk@558: language:nil full:YES error:&error]; dirk@558: XCTAssertNil(error); 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 dirk@558: language:@"en" full:YES error:&error]; dirk@558: XCTAssertNil(error); andreas@392: XCTAssertEqualObjects(trustwordsFullEnglish, trustwordsFull); andreas@392: andreas@392: NSString *trustwordsUndefined = [session getTrustwordsIdentity1:meOrig identity2:partner1Orig dirk@558: language:@"ZZ" full:YES error:&error]; dirk@558: XCTAssertNotNil(error); 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@532: NSError *error = nil; dirk@532: XCTAssertTrue([session mySelf:identMe error:&error]); dirk@532: XCTAssertNil(error); dirk@532: dirk@431: XCTAssertNotNil(identMe.fingerPrint); dirk@432: dirk@432: // PEP_CANNOT_FIND_PERSON == 902 dirk@567: XCTAssertTrue([session isPEPUser:identMe error:&error]); 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@532: NSError *error = nil; dirk@532: XCTAssertTrue([session mySelf:identMe error:&error]); dirk@532: XCTAssertNil(error); dirk@532: 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@653: NSNumber *numRating = [self testOutgoingRatingForMessage:msg session:session error:&error]; dirk@573: XCTAssertNotNil(numRating); dirk@573: XCTAssertNil(error); dirk@573: XCTAssertEqual(numRating.pEpRating, PEP_rating_unencrypted); dirk@528: dirk@524: PEPMessage *encMsg = [session encryptMessage:msg extraKeys:nil status: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@583: flags:nil dirk@517: rating:&pEpRating dirk@523: extraKeys:&keys dirk@524: status:nil 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@532: NSError *error = nil; dirk@532: XCTAssertTrue([session mySelf:identMe error:&error]); dirk@532: XCTAssertNil(error); dirk@532: 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@534: XCTAssertFalse([session trustPersonalKey:identAlice error:&error]); dirk@534: XCTAssertNotNil(error); 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@532: NSError *error = nil; dirk@532: XCTAssertTrue([session mySelf:identMe error:&error]); dirk@532: XCTAssertNil(error); dirk@532: 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@529: PEP_rating rating = [self ratingForIdentity:identAlice session:innerSession]; 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@612: /** dirk@615: IOSAD-93, testing for easy error case. dirk@612: */ dirk@618: - (void)testEncryptAndAttachPrivateKeyIllegalValue dirk@612: { dirk@612: PEPSession *session = [PEPSession new]; dirk@612: dirk@612: PEPIdentity *identMe = [[PEPIdentity alloc] dirk@612: initWithAddress:@"me-myself-and-i@pep-project.org" dirk@612: userID:@"me-myself-and-i" dirk@612: userName:@"pEp Me" dirk@612: isOwn:YES]; dirk@612: NSError *error = nil; dirk@612: XCTAssertTrue([session mySelf:identMe error:&error]); dirk@612: XCTAssertNil(error); dirk@612: dirk@612: XCTAssertNotNil(identMe.fingerPrint); dirk@612: dirk@612: NSString *fprAlice = @"4ABE3AAF59AC32CFE4F86500A9411D176FF00E97"; dirk@612: PEPIdentity *identAlice = [self dirk@612: checkImportingKeyFilePath:@"6FF00E97_sec.asc" dirk@612: address:@"pep.test.alice@pep-project.org" dirk@612: userID:@"alice_user_id" dirk@612: fingerPrint:fprAlice dirk@612: session: session]; dirk@612: XCTAssertNotNil(identAlice); dirk@612: dirk@612: NSString *shortMessage = @"whatever it may be"; dirk@612: NSString *longMessage = [NSString stringWithFormat:@"%@ %@", shortMessage, shortMessage]; dirk@612: PEPMessage *message = [PEPMessage new]; dirk@612: message.from = identMe; dirk@612: message.to = @[identAlice]; dirk@612: message.shortMessage = shortMessage; dirk@612: message.longMessage = longMessage; dirk@612: dirk@612: PEP_STATUS status = PEP_KEY_NOT_FOUND; dirk@612: error = nil; dirk@612: PEPMessage *encrypted = [session dirk@612: encryptMessage:message dirk@612: toFpr:fprAlice dirk@612: encFormat:PEP_enc_PEP dirk@612: flags:0 dirk@612: status:&status error:&error]; dirk@613: XCTAssertEqual(status, PEP_ILLEGAL_VALUE); dirk@613: XCTAssertNotNil(error); dirk@612: XCTAssertNil(encrypted); dirk@612: } dirk@612: dirk@449: #pragma mark - configUnencryptedSubject dirk@449: dirk@449: - (void)testConfigUnencryptedSubject dirk@449: { dirk@449: // Setup Config to encrypt subject dirk@601: [PEPObjCAdapter setUnEncryptedSubjectEnabled: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@602: - (void)testConfigUnencryptedSubjectEncryptedSubjectDisabled dirk@449: { dirk@449: // Setup Config to not encrypt subject dirk@601: [PEPObjCAdapter setUnEncryptedSubjectEnabled:YES]; dirk@449: dirk@449: // Write mail to yourself ... dirk@449: PEPMessage *encMessage = [self mailWrittenToMySelf]; dirk@449: dirk@602: // pEp to pEp uses message 2.0, which always encrypts subjects (ENGINE-429) dirk@602: XCTAssertEqualObjects(encMessage.shortMessage, @"p≡p", @"Subject should be encrypted"); dirk@449: } dirk@449: dirk@628: #pragma mark - Passive mode dirk@628: dirk@628: - (void)testPassiveMode dirk@628: { dirk@628: [self testPassiveModeEnabled:NO]; dirk@628: [self testPassiveModeEnabled:YES]; dirk@628: } dirk@628: dirk@646: #pragma mark - Decryption dirk@646: dirk@646: - (void)testDecryptionOfUnencryptedMessageWithOdtAttachmentContainingSpace dirk@646: { dirk@646: PEPSession *session = [PEPSession new]; dirk@646: dirk@646: PEPIdentity *me = [PEPTestUtils ownPepIdentityWithAddress:@"me@peptest.ch" dirk@646: userName:@"userName"]; dirk@646: NSError *error = nil; dirk@646: XCTAssertTrue([session mySelf:me error:&error]); dirk@646: XCTAssertNil(error); dirk@646: dirk@646: const char *dataString = "blahblah"; dirk@646: const size_t dataSize = strlen(dataString); dirk@646: char *rawData = strndup(dataString, dataSize); dirk@646: dirk@646: PEPAttachment *attachment = [[PEPAttachment alloc] dirk@646: initWithData:[NSData dirk@646: dataWithBytesNoCopy:rawData length:dataSize]]; dirk@646: attachment.filename = @"Someone andTextIncludingTheSpace.odt"; dirk@646: attachment.mimeType = @"application/vnd.oasis.opendocument.text"; dirk@646: dirk@646: NSString *shortMessage = @"Subject"; dirk@646: NSString *longMessage = @"Oh, this is a long body text!"; dirk@646: PEPMessage *mail = [PEPTestUtils mailFrom:me dirk@646: toIdent:me dirk@646: shortMessage:shortMessage dirk@646: longMessage:longMessage dirk@646: outgoing:YES]; dirk@646: dirk@646: mail.attachments = @[attachment]; dirk@646: dirk@646: error = nil; dirk@646: PEPStringList *keys; dirk@646: PEP_rating rating = PEP_rating_undefined; dirk@646: PEPMessage *decmsg = [session dirk@646: decryptMessage:mail dirk@646: flags:nil dirk@646: rating:&rating dirk@646: extraKeys:&keys dirk@646: status:nil dirk@646: error:&error]; dirk@646: XCTAssertNotNil(decmsg); dirk@646: XCTAssertNil(error); dirk@646: XCTAssertEqual(rating, PEP_rating_unencrypted); dirk@646: dirk@646: PEPAttachment *decryptedAttachment = [decmsg.attachments objectAtIndex:0]; dirk@646: XCTAssertEqualObjects(decryptedAttachment.mimeType, attachment.mimeType); dirk@646: XCTAssertEqualObjects(decryptedAttachment.filename, attachment.filename); dirk@646: } dirk@646: dirk@749: #pragma mark - Send Message dirk@749: dirk@749: /** dirk@749: Prove that mySelf triggers a message to be sent. dirk@749: */ dirk@749: - (void)testBasicSendMessage dirk@749: { dirk@749: PEPSession *session = [PEPSession new]; dirk@749: dirk@749: XCTAssertEqual(self.sendMessageDelegate.messages.count, 0); dirk@760: XCTAssertNil(self.sendMessageDelegate.lastMessage); dirk@749: dirk@749: XCTKVOExpectation *expHaveMessage = [[XCTKVOExpectation alloc] dirk@749: initWithKeyPath:@"lastMessage" dirk@749: object:self.sendMessageDelegate]; dirk@749: dirk@749: PEPIdentity *identMe = [[PEPIdentity alloc] dirk@749: initWithAddress:@"me-myself-and-i@pep-project.org" dirk@749: userID:@"me-myself-and-i" dirk@749: userName:@"pEp Me" dirk@749: isOwn:YES]; dirk@749: NSError *error = nil; dirk@749: XCTAssertTrue([session mySelf:identMe error:&error]); dirk@749: XCTAssertNil(error); dirk@749: dirk@749: XCTAssertNotNil(identMe.fingerPrint); dirk@749: dirk@762: [self waitForExpectations:@[expHaveMessage] timeout:PEPTestInternalSyncTimeout]; dirk@760: XCTAssertNotNil(self.sendMessageDelegate.lastMessage); dirk@749: dirk@749: XCTAssertEqual(self.sendMessageDelegate.messages.count, 1); dirk@749: } dirk@749: dirk@719: #pragma mark - Key Reset dirk@719: dirk@741: - (void)testBasicKeyResetTrust dirk@719: { dirk@719: PEPSession *session = [PEPSession new]; dirk@719: dirk@742: XCTAssertEqual(self.sendMessageDelegate.messages.count, 0); dirk@742: dirk@754: XCTKVOExpectation *expHaveMessage = [[XCTKVOExpectation alloc] dirk@754: initWithKeyPath:@"lastMessage" dirk@754: object:self.sendMessageDelegate]; dirk@754: dirk@719: PEPIdentity *identMe = [[PEPIdentity alloc] dirk@719: initWithAddress:@"me-myself-and-i@pep-project.org" dirk@719: userID:@"me-myself-and-i" dirk@719: userName:@"pEp Me" dirk@719: isOwn:YES]; dirk@719: NSError *error = nil; dirk@719: XCTAssertTrue([session mySelf:identMe error:&error]); dirk@719: XCTAssertNil(error); dirk@719: dirk@763: [self.sync shutdown]; dirk@763: dirk@719: XCTAssertNotNil(identMe.fingerPrint); dirk@719: NSString *fpr1 = identMe.fingerPrint; dirk@719: dirk@719: XCTAssertTrue([session keyResetTrust:identMe error:&error]); dirk@719: XCTAssertNil(error); dirk@719: dirk@719: identMe.fingerPrint = nil; dirk@719: XCTAssertTrue([session updateIdentity:identMe error:&error]); dirk@719: XCTAssertNotNil(identMe.fingerPrint); dirk@719: XCTAssertNotEqual(identMe.fingerPrint, fpr1); dirk@742: dirk@754: [self waitForExpectations:@[expHaveMessage] timeout:PEPTestInternalSyncTimeout]; dirk@745: dirk@742: XCTAssertEqual(self.sendMessageDelegate.messages.count, 1); dirk@719: } dirk@719: andreas@392: #pragma mark - Helpers andreas@392: dirk@763: - (void)reStartSync dirk@763: { dirk@763: [self.sync shutdown]; dirk@763: [self startSync]; dirk@763: } dirk@763: dirk@763: - (void)startSync dirk@763: { dirk@763: self.sendMessageDelegate = [PEPSessionTestSendMessageDelegate new]; dirk@763: self.notifyHandshakeDelegate = [PEPSessionTestNotifyHandshakeDelegate new]; dirk@763: dirk@763: self.sync = [[PEPSync alloc] dirk@763: initWithSendMessageDelegate:self.sendMessageDelegate dirk@763: notifyHandshakeDelegate:self.notifyHandshakeDelegate]; dirk@763: [self.sync startup]; dirk@763: } dirk@763: dirk@653: - (NSNumber * _Nullable)testOutgoingRatingForMessage:(PEPMessage * _Nonnull)theMessage dirk@653: session:(PEPSession *)session dirk@653: error:(NSError * _Nullable * _Nullable)error dirk@653: { dirk@653: NSNumber *ratingOriginal = [session outgoingRatingForMessage:theMessage error:error]; dirk@653: NSNumber *ratingPreview = [session outgoingRatingPreviewForMessage:theMessage error:nil]; dirk@653: XCTAssertEqual(ratingOriginal, ratingPreview); dirk@653: return ratingOriginal; dirk@653: } dirk@653: dirk@628: - (void)testPassiveModeEnabled:(BOOL)passiveModeEnabled dirk@628: { dirk@628: [PEPObjCAdapter setPassiveModeEnabled:passiveModeEnabled]; dirk@628: PEPSession *session = [PEPSession new]; dirk@628: dirk@628: PEPIdentity *identMe = [[PEPIdentity alloc] dirk@628: initWithAddress:@"me-myself-and-i@pep-project.org" dirk@628: userID:@"me-myself-and-i" dirk@628: userName:@"pEp Me" dirk@628: isOwn:YES]; dirk@628: NSError *error = nil; dirk@628: XCTAssertTrue([session mySelf:identMe error:&error]); dirk@628: XCTAssertNil(error); dirk@628: dirk@628: XCTAssertNotNil(identMe.fingerPrint); dirk@628: dirk@628: PEPIdentity *identAlice = [[PEPIdentity alloc] dirk@628: initWithAddress:@"pep.test.alice@pep-project.org" dirk@628: userID:@"alice_user_id" dirk@628: userName:@"Alice" dirk@628: isOwn:NO]; dirk@628: dirk@628: NSString *shortMessage = @"whatever it may be"; dirk@628: NSString *longMessage = [NSString stringWithFormat:@"%@ %@", shortMessage, shortMessage]; dirk@628: PEPMessage *message = [PEPMessage new]; dirk@628: message.direction = PEP_dir_outgoing; dirk@628: message.from = identMe; dirk@628: message.to = @[identAlice]; dirk@628: message.shortMessage = shortMessage; dirk@628: message.longMessage = longMessage; dirk@628: dirk@628: PEP_STATUS status = PEP_KEY_NOT_FOUND; dirk@628: PEPMessage *encryptedMessage = [session encryptMessage:message dirk@628: extraKeys:@[] dirk@628: status:&status dirk@628: error:&error]; dirk@628: XCTAssertNil(error); dirk@628: XCTAssertEqual(status, PEP_UNENCRYPTED); dirk@628: dirk@628: if (passiveModeEnabled) { dirk@628: XCTAssertNil(encryptedMessage.attachments); dirk@628: } else { dirk@628: XCTAssertEqual(encryptedMessage.attachments.count, 1); dirk@628: } dirk@628: } dirk@628: dirk@529: /** dirk@529: Determines the rating for the given identity. dirk@529: @return PEP_rating_undefined on error dirk@529: */ dirk@529: - (PEP_rating)ratingForIdentity:(PEPIdentity *)identity session:(PEPSession *)session dirk@529: { dirk@529: NSError *error; dirk@574: NSNumber *numRating = [session ratingForIdentity:identity error:&error]; dirk@529: XCTAssertNil(error); dirk@574: return numRating.pEpRating; dirk@529: } dirk@529: 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@532: NSError *error = nil; dirk@532: XCTAssertTrue([session updateIdentity:identTest error:&error]); dirk@532: XCTAssertNil(error); 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@532: NSError *error = nil; dirk@532: dirk@433: XCTAssertNotNil(partnerIdentity.fingerPrint); dirk@532: XCTAssertTrue([session updateIdentity:partnerIdentity error:&error]); dirk@532: XCTAssertNil(error); dirk@433: XCTAssertNotNil(partnerIdentity.fingerPrint); dirk@433: NSString *fingerprint = partnerIdentity.fingerPrint; dirk@433: partnerIdentity.fingerPrint = nil; dirk@532: XCTAssertTrue([session updateIdentity:partnerIdentity error:&error]); dirk@532: XCTAssertNil(error); 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"]; dirk@532: NSError *error = nil; dirk@532: XCTAssertTrue([session mySelf:me error:&error]); dirk@532: XCTAssertNil(error); 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]; dirk@526: PEP_STATUS status = PEP_UNKNOWN_ERROR; dirk@556: PEPMessage *encMessage = [session dirk@556: encryptMessage:mail dirk@557: forSelf:me dirk@556: extraKeys:nil dirk@556: status:&status dirk@556: error:&error]; dirk@526: XCTAssertNil(error); 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"]; dirk@532: NSError *error = nil; dirk@532: XCTAssertTrue([session mySelf:me error:&error]); dirk@532: XCTAssertNil(error); dirk@532: 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: dirk@526: PEP_STATUS status; dirk@556: PEPMessage *encMessage = [session dirk@556: encryptMessage:mail dirk@557: forSelf:me dirk@556: extraKeys:nil dirk@556: status:&status dirk@556: error:&error]; andreas@392: XCTAssertEqual(status, 0); andreas@392: XCTAssertEqualObjects(encMessage.shortMessage, @"p≡p"); andreas@392: dirk@514: PEP_rating rating; dirk@526: error = nil; dirk@517: PEPMessage *unencDict = [session dirk@517: decryptMessage:encMessage dirk@583: flags:nil dirk@517: rating:&rating dirk@523: extraKeys:keys dirk@524: status:nil 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@617: XCTAssertTrue([PEPTestUtils importBundledKey:@"6FF00E97.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@532: NSError *error = nil; dirk@532: XCTAssertTrue([session mySelf:identMe error:&error]); dirk@532: XCTAssertNil(error); dirk@532: 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@653: NSNumber *numRating = [self testOutgoingRatingForMessage:msg session:session error:&error]; dirk@573: XCTAssertNotNil(numRating); dirk@573: XCTAssertNil(error); dirk@573: XCTAssertEqual(numRating.pEpRating, PEP_rating_reliable); dirk@422: dirk@423: PEPMessage *encMsg; dirk@424: dirk@424: PEP_STATUS statusEnc = PEP_VERSION_MISMATCH; dirk@424: if (toSelf) { dirk@556: encMsg = [session dirk@556: encryptMessage:msg dirk@557: forSelf:identMe dirk@556: extraKeys:nil dirk@556: status:&statusEnc dirk@556: error:&error]; dirk@522: XCTAssertEqual(statusEnc, PEP_STATUS_OK); dirk@424: } else { dirk@524: encMsg = [session encryptMessage:msg extraKeys:nil status: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@526: error = nil; dirk@517: PEPMessage *decMsg = [session dirk@517: decryptMessage:encMsg dirk@583: flags:nil dirk@517: rating:&pEpRating dirk@523: extraKeys:&keys dirk@524: status:nil 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