diff --git a/enzevalos_iphone/MailComparison.swift b/enzevalos_iphone/MailComparison.swift index 586dff84e694df4b74250b66f898b4503cda22bd..3917644b86408ed597cfc145769ed4b24309f397 100644 --- a/enzevalos_iphone/MailComparison.swift +++ b/enzevalos_iphone/MailComparison.swift @@ -10,8 +10,13 @@ import Foundation import CoreData private let datahandler = DataHandler.handler + extension String { - func countMatches(_ mailAddr: String, _ inboxMails: [PersistentMail]) -> Int { + + /** + compare the given mail address to the persistant mails and return 2 if this address was found twice + ***/ + private func countMatches(_ mailAddr: String, _ inboxMails: [PersistentMail]) -> Int { var senders: [String] = [] for sender in inboxMails{ let sender: String = sender.from.mailAddress @@ -29,6 +34,35 @@ extension String { return numberOfFoundMatches } + /** + returns a mail address, wich has the same identity as the given one but a different domain + ***/ + private func getIdentityWithDifferentDomain(_ mailAdd: String, inboxMails: [PersistentMail]) -> String{ + let senderIdentity = mailAdd.getLocalMailIdentity() + var senderDomain = mailAdd.splitAddress() + senderDomain.removeFirst() + + var senders: [String] = [] + for sender in inboxMails{ + let sender: String = sender.from.mailAddress + senders.append(sender) + } + var foundMatch = "" + for addr in senders{ + let foundIdentity = addr.getLocalMailIdentity() + var foundDomain = addr.splitAddress() + foundDomain.removeFirst() + if senderIdentity == foundIdentity && senderDomain != foundDomain{ + foundMatch = addr + return foundMatch + } + } + return foundMatch + } + + /** + looks if the mail address of the sender is already known + ***/ public func compareSenderToContacts() -> String { // Case 1: Is the address of the sender in the address book? let enzContacts = datahandler.getContacts() @@ -45,11 +79,14 @@ extension String { } // Case 2: Is the address of the sender known through previous mails? let inboxMails = datahandler.getAllPersistentMails() - if countMatches(self, inboxMails) > 1{ + if countMatches(self, inboxMails) == 2{ return "has alrady send an email" } // Case 3: The identity of an mail address is known, but not the (sub-)domain - + let foundIdentity = getIdentityWithDifferentDomain(self, inboxMails: inboxMails) + if foundIdentity != ""{ + return foundIdentity + } return "not known" } } diff --git a/enzevalos_iphoneTests/GeneratedMocks.swift b/enzevalos_iphoneTests/GeneratedMocks.swift index 27ef4fd2fec5bcb1517f79e4a706dac9f46aa9cf..f31c5c5e524949f69b2ecb2ce5837b0b8591285c 100644 --- a/enzevalos_iphoneTests/GeneratedMocks.swift +++ b/enzevalos_iphoneTests/GeneratedMocks.swift @@ -1,4 +1,4 @@ -// MARK: - Mocks generated from file: enzevalos_iphone/AuthenticationModel.swift at 2020-03-30 10:11:05 +0000 +// MARK: - Mocks generated from file: enzevalos_iphone/AuthenticationModel.swift at 2020-03-30 11:54:02 +0000 // // AuthenticationModel.swift @@ -653,7 +653,7 @@ import Foundation } -// MARK: - Mocks generated from file: enzevalos_iphone/AuthenticationViewModel.swift at 2020-03-30 10:11:05 +0000 +// MARK: - Mocks generated from file: enzevalos_iphone/AuthenticationViewModel.swift at 2020-03-30 11:54:02 +0000 // // AuthenticationViewModel.swift diff --git a/enzevalos_iphoneTests/phishing/MailComparisonTests.swift b/enzevalos_iphoneTests/phishing/MailComparisonTests.swift index b41e536cf731fdb840eba0822ed3bcafffc94e62..7e256ea6e930b993a29be8ced8f942e2f283bf10 100644 --- a/enzevalos_iphoneTests/phishing/MailComparisonTests.swift +++ b/enzevalos_iphoneTests/phishing/MailComparisonTests.swift @@ -20,7 +20,12 @@ class MailComparisonTests: XCTestCase { let userAdr_2 = "carl@test.com" let userName_2 = "carl" var user_2: MCOAddress = MCOAddress.init(displayName: "Carl", mailbox: "carl@test.com") - + let userAddr_3_1 = "david@123.com" + let userAddr_3_2 = "david@1234.com" + let userName_3 = "david" + var user_3_1: MCOAddress = MCOAddress.init(displayName: "david", mailbox: "david@123.com") + var user_3_2: MCOAddress = MCOAddress.init(displayName: "david", mailbox: "david@1234.com") + override func setUp() { super.setUp() datahandler.reset() @@ -78,11 +83,8 @@ class MailComparisonTests: XCTestCase { let mutableContact = selectedContact.mutableCopy() as! CNMutableContact request.delete(mutableContact) try store.execute(request) - } catch { } - } - } func testCompareSenderToContacts () { @@ -90,8 +92,10 @@ class MailComparisonTests: XCTestCase { let mail_2 = testMail(from: user_1, to: [], cc: [], bcc: []) let mail_3 = testMail(from: user_2, to: [], cc: [], bcc: []) let mail_4 = testMail(from: user_2, to: [], cc: [], bcc: []) + let mail_5 = testMail(from: user_3_1, to: [], cc: [], bcc: []) + let mail_6 = testMail(from: user_3_2, to: [], cc: [], bcc: []) - let mailsInInbox: [PersistentMail] = [mail_1!, mail_2!, mail_3!, mail_4!] + let mailsInInbox: [PersistentMail] = [mail_1!, mail_2!, mail_3!, mail_4!, mail_5!, mail_6!] createAddressBook([user]) @@ -100,6 +104,8 @@ class MailComparisonTests: XCTestCase { XCTAssertEqual("not known", mailsInInbox[1].from.mailAddress.compareSenderToContacts()) XCTAssertEqual("has alrady send an email", mailsInInbox[2].from.mailAddress.compareSenderToContacts()) + XCTAssertEqual(userAddr_3_2, mailsInInbox[4].from.mailAddress.compareSenderToContacts()) + // The created user has to be deleted after the test resetAddressBook([user])