diff --git a/enzevalos_iphone.xcodeproj/project.pbxproj b/enzevalos_iphone.xcodeproj/project.pbxproj index 2887ac1fd3a4d5aed723a1e9576e454b23ecc377..07d4b24ca65ac5685d65dedf638457a4dbe54394 100644 --- a/enzevalos_iphone.xcodeproj/project.pbxproj +++ b/enzevalos_iphone.xcodeproj/project.pbxproj @@ -151,6 +151,7 @@ 8428A8711F436A1E007649A5 /* GamificationStatusViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8428A86D1F436A1E007649A5 /* GamificationStatusViewController.swift */; }; 8428A8831F436AC9007649A5 /* GamificationDataUnitTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8428A8561F4369EA007649A5 /* GamificationDataUnitTest.swift */; }; 8428A8841F436ACC007649A5 /* GamificationElements.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 8428A8541F4369CF007649A5 /* GamificationElements.xcassets */; }; + 988C9C5D240D507A006213F0 /* PhishingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 988C9C5C240D507A006213F0 /* PhishingTests.swift */; }; A102AA8A1EDDB4F40024B457 /* videoOnboarding2.m4v in Resources */ = {isa = PBXBuildFile; fileRef = A102AA891EDDB4E80024B457 /* videoOnboarding2.m4v */; }; A1083A541E8BFEA6003666B7 /* Onboarding.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1083A531E8BFEA6003666B7 /* Onboarding.swift */; }; A10DAA5721F37600005D8BBB /* IntroInfoButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = A10DAA5621F37600005D8BBB /* IntroInfoButton.swift */; }; @@ -423,6 +424,7 @@ 8B87EFB6CEAA31452F744015 /* Pods-enzevalos_iphoneUITests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-enzevalos_iphoneUITests.release.xcconfig"; path = "../enzevalos_iphone_workspace/Pods/Target Support Files/Pods-enzevalos_iphoneUITests/Pods-enzevalos_iphoneUITests.release.xcconfig"; sourceTree = "<group>"; }; 91B6C9020C660BEA78FAEF28 /* Pods-enzevalos_iphone.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-enzevalos_iphone.debug.xcconfig"; path = "../enzevalos_iphone_workspace/Pods/Target Support Files/Pods-enzevalos_iphone/Pods-enzevalos_iphone.debug.xcconfig"; sourceTree = "<group>"; }; 94EE54279AB591E0CAB8EFD8 /* Pods_enzevalos_iphone.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_enzevalos_iphone.framework; sourceTree = BUILT_PRODUCTS_DIR; }; + 988C9C5C240D507A006213F0 /* PhishingTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PhishingTests.swift; sourceTree = "<group>"; }; 9A132EDE8BCA06ACDB505C22 /* Pods-enzevalos_iphoneUITests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-enzevalos_iphoneUITests.debug.xcconfig"; path = "../enzevalos_iphone_workspace/Pods/Target Support Files/Pods-enzevalos_iphoneUITests/Pods-enzevalos_iphoneUITests.debug.xcconfig"; sourceTree = "<group>"; }; 9B3D62838C729BAC6832270A /* Pods-enzevalos_iphone-AdHoc.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-enzevalos_iphone-AdHoc.debug.xcconfig"; path = "../enzevalos_iphone_workspace/Pods/Target Support Files/Pods-enzevalos_iphone-AdHoc/Pods-enzevalos_iphone-AdHoc.debug.xcconfig"; sourceTree = "<group>"; }; A102AA891EDDB4E80024B457 /* videoOnboarding2.m4v */ = {isa = PBXFileReference; lastKnownFileType = file; path = videoOnboarding2.m4v; sourceTree = "<group>"; }; @@ -959,6 +961,7 @@ A135268F1D955BE000D3BFE1 /* enzevalos_iphoneTests */ = { isa = PBXGroup; children = ( + 988C9C5C240D507A006213F0 /* PhishingTests.swift */, 47F0376C22A7278A0005C9DE /* private */, 470709202189C24800DF71A3 /* testKeys */, 470709112189BB4A00DF71A3 /* testMails */, @@ -1679,6 +1682,7 @@ 474054982244D7A9007CF83B /* MailServerConfigurationTest.swift in Sources */, 479B5977206914BE00B3944D /* CryptoTests.swift in Sources */, A15D215F223BE6E4003E0CE0 /* MailTest.swift in Sources */, + 988C9C5D240D507A006213F0 /* PhishingTests.swift in Sources */, 4715F637202A0248001BFFD0 /* CoreDataTests.swift in Sources */, 47C22281218AFD6300BD2C2B /* AutocryptTest.swift in Sources */, 478154AE2200641900A931EC /* StudyTest.swift in Sources */, diff --git a/enzevalos_iphone/StringExtension.swift b/enzevalos_iphone/StringExtension.swift index 06ef570ba354dea3c9be355f59090a06f482ea35..4b6f0dbe49224fccfc31548bf59880a42b70ce7f 100644 --- a/enzevalos_iphone/StringExtension.swift +++ b/enzevalos_iphone/StringExtension.swift @@ -61,3 +61,110 @@ extension String { } } +extension String { + func splitAddress() -> [String]{ + if self == ""{ + return [] + } + let mailAddressArr = self.split(separator: "@").map(String.init) + let mailLocalIdentity = mailAddressArr[0] + let subDomainArr = mailAddressArr[1].split(separator: ".").map(String.init) + let splittetMailArr = [mailLocalIdentity] + subDomainArr + return splittetMailArr + } + + func getLocalMailIdentity() -> String{ + guard let localIdentity = self.splitAddress().first + else { return ""} + return localIdentity + } + + func getSubdomains() -> [String]{ + if self == ""{ + return [] + } + var subDomains = self.splitAddress() + subDomains.removeFirst() + subDomains.removeLast() + return subDomains + } + + + func getDomain() -> String{ + guard let domain = self.splitAddress().last + else {return ""} + return domain + } +} + +extension String { + func findMailAddress() -> [String]{ + let splitString = self.split(separator: " ").map(String.init) + var results:[String] = [] + for val in splitString { + if val.contains("@"){ + results.append(val) + } + } + return(results) + } + + //for text only, not for hyperlink detection + func findURL() -> [String?]{ + var urls : [String?] = [] + do { + let detector = try NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue) + detector.enumerateMatches(in: self, options: [], range: NSMakeRange(0, self.count), using: { (result, _, _) in + if let match = result{ + if let match2 = match.url?.absoluteString, !match2.contains("@") { + urls.append(match2) + } + } + }) + } catch let error as NSError { + print(error.localizedDescription) + } + return urls + } + + // to find HTML tags and hyperlinks + func findHtmlTags() -> [String]{ + let htmlTagPattern = "<(.*?)a.*?href=.*?>(.*?)<\\/.*?a.*?>" + let range = NSRange(location: 0, length: self.utf16.count) + let regex = try! NSRegularExpression(pattern: htmlTagPattern) + let check = regex.matches(in: self, options: [], range: range) + var foundLinks:[String] = [] + for match in check{ + guard let range = Range(match.range, in: self) else {continue} + foundLinks.append(String(self[range])) + } + return (foundLinks) + } + + func htmlTagURL() -> [String]{ + let htmlTagUrlPattern = "(?<=href=')(.*?)(?='>)|(?<=href=\")(.*?)(?=\">)" + let range = NSRange(location: 0, length: self.utf16.count) + let regex = try! NSRegularExpression(pattern: htmlTagUrlPattern) + let check = regex.matches(in: self, options: [], range: range) + var foundLinks:[String] = [] + for match in check{ + guard let range = Range(match.range, in: self) else {continue} + foundLinks.append(String(self[range])) + } + return (foundLinks) + + } + + func htmlTagLinkName() -> [String]{ + let htmlTagLinkNamePattern = "(?<='>)(.*?)(?=<\\/a>)|(?<=\">)(.*?)(?=<)" + let range = NSRange(location: 0, length: self.utf16.count) + let regex = try! NSRegularExpression(pattern: htmlTagLinkNamePattern) + let check = regex.matches(in: self, options: [], range: range) + var foundLinks:[String] = [] + for match in check{ + guard let range = Range(match.range, in: self) else {continue} + foundLinks.append(String(self[range])) + } + return (foundLinks) + } +} diff --git a/enzevalos_iphoneTests/PhishingTests.swift b/enzevalos_iphoneTests/PhishingTests.swift new file mode 100644 index 0000000000000000000000000000000000000000..1927629ab39f5730855c63ec88dd606ea83e3870 --- /dev/null +++ b/enzevalos_iphoneTests/PhishingTests.swift @@ -0,0 +1,122 @@ +// +// PhishingTests.swift +// enzevalos_iphoneTests +// +// Created by Viktoria Sorgalla on 02.03.20. +// Copyright © 2020 fu-berlin. All rights reserved. +// + +import XCTest +@testable import enzevalos_iphone + +class PhishingTests: XCTestCase { + +// setup for mail address splitting + var testEMail = "alice.bob@moep.blubb.de" + + var finalTestEMailSplitAddress = ["alice.bob", "moep", "blubb", "de"] + + var finalTestEMailIdentity = "alice.bob" + + var finalTestEMailSubDomains = ["moep", "blubb"] + + var finalTestEMailDomain = "de" + + var TestText = "blabla moep jkjlkj lkjkljknmbjks llhil k. jhkhkih. huhuhj! fsdf bob.alice@zedat.fu-berlin.de dfsf. jhjknjknjkh https://git.imp.fu-berlin.de/enzevalos/enzevalos_iphone/issues/240 hjkhjkhkhn www.google.de kljhl@hjkghgkhj.com nljbjkjk.de url tag html mail <a href='https://www.w3schools.com'>Visit W3Schools</a> hjhkhiuhziu kjhkl <a href=\"https://www.w2schools.com\">Visit W2Schools</a>. lknljnlk. kmm /n lmölmpöl < a href=\"https://www.w8schools.com\">Visit W8Schools</a> gfg fghfghnhg <a href=\"https://www.w33schools.com\">Visit W33Schools</a> nkjhjkhkjn,mn jnmnklmj j <a href=\"https://www.w22schools.com\">Visit W22Schools</ a> hghjcfgh hfgchnvhj vgjcgj cjghcj <a href=\"https://www.w99schools.com\">Visit W99Schools</a >" + +// setup for find mail address in text + var finalMailAddresses = ["bob.alice@zedat.fu-berlin.de", "kljhl@hjkghgkhj.com"] + +// setup for find URLs in text + var finalURLs = ["https://git.imp.fu-berlin.de/enzevalos/enzevalos_iphone/issues/240", "http://www.google.de", "http://nljbjkjk.de", "https://www.w3schools.com", "https://www.w2schools.com", "https://www.w8schools.com", "https://www.w33schools.com", "https://www.w22schools.com", "https://www.w99schools.com"] + +// setup for tag tests + var finalTestTextFindHtmlTags = ["<a href=\'https://www.w3schools.com\'>Visit W3Schools</a>", "<a href=\"https://www.w2schools.com\">Visit W2Schools</a>", "< a href=\"https://www.w8schools.com\">Visit W8Schools</a>", "<a href=\"https://www.w33schools.com\">Visit W33Schools</a>", "<a href=\"https://www.w22schools.com\">Visit W22Schools</ a>", "<a href=\"https://www.w99schools.com\">Visit W99Schools</a >"] + + var finalTestTextHtmlTagURL = ["https://www.w3schools.com", "https://www.w2schools.com", "https://www.w8schools.com", "https://www.w33schools.com", "https://www.w22schools.com", "https://www.w99schools.com"] + + var finalTestTexthttpTagLinkName = ["Visit W3Schools", "Visit W2Schools", "Visit W8Schools", "Visit W33Schools", "Visit W22Schools", "Visit W99Schools"] + + override func setUp() { + super.setUp() + // Put setup code here. This method is called before the invocation of each test method in the class. + } + + override func tearDown() { + super.tearDown() + // Put teardown code here. This method is called after the invocation of each test method in the class. + } + + func testSplitMailAddress(){ + let arr = testEMail.splitAddress() + XCTAssertNotNil(arr) + XCTAssertEqual(arr[0], finalTestEMailSplitAddress[0]) + XCTAssertEqual(arr[1], finalTestEMailSplitAddress[1]) + XCTAssertEqual(arr[2], finalTestEMailSplitAddress[2]) + XCTAssertEqual(arr[3], finalTestEMailSplitAddress[3]) + } + + func testGetMailIdentity(){ + XCTAssertEqual(testEMail.getLocalMailIdentity(), finalTestEMailIdentity) + } + + func testGetSubdomains(){ + let arr = testEMail.getSubdomains() + XCTAssertNotNil(arr) + XCTAssertEqual(arr[0], finalTestEMailSubDomains[0]) + XCTAssertEqual(arr[1], finalTestEMailSubDomains[1]) + } + + func testGetDomain(){ + XCTAssertEqual(testEMail.getDomain(), finalTestEMailDomain) + } + + func testTextFindMailAddress(){ + let arr = TestText.findMailAddress() + XCTAssertNotNil(arr) + XCTAssertEqual(arr[0], finalMailAddresses[0]) + XCTAssertEqual(arr[1], finalMailAddresses[1]) + } + + func testTextFindURL(){ + let arr = TestText.findURL() + XCTAssertNotNil(arr) + XCTAssertEqual(arr[0], finalURLs[0]) + XCTAssertEqual(arr[1], finalURLs[1]) + XCTAssertEqual(arr[2], finalURLs[2]) + XCTAssertEqual(arr[3], finalURLs[3]) + } + + func testTextFindHlmlTags(){ + let arr = TestText.findHtmlTags() + XCTAssertNotNil(arr) + XCTAssertEqual(arr[0], finalTestTextFindHtmlTags[0]) + XCTAssertEqual(arr[1], finalTestTextFindHtmlTags[1]) + XCTAssertEqual(arr[2], finalTestTextFindHtmlTags[2]) + XCTAssertEqual(arr[3], finalTestTextFindHtmlTags[3]) + XCTAssertEqual(arr[4], finalTestTextFindHtmlTags[4]) + XCTAssertEqual(arr[5], finalTestTextFindHtmlTags[5]) + } + + func testTextHtmlTagURL(){ + let arr = TestText.htmlTagURL() + XCTAssertNotNil(arr) + XCTAssertEqual(arr[0], finalTestTextHtmlTagURL[0]) + XCTAssertEqual(arr[1], finalTestTextHtmlTagURL[1]) + XCTAssertEqual(arr[2], finalTestTextHtmlTagURL[2]) + XCTAssertEqual(arr[3], finalTestTextHtmlTagURL[3]) + XCTAssertEqual(arr[4], finalTestTextHtmlTagURL[4]) + XCTAssertEqual(arr[5], finalTestTextHtmlTagURL[5]) + } + + func testTextHttpTagLinkName(){ + let arr = TestText.htmlTagLinkName() + XCTAssertNotNil(arr) + XCTAssertEqual(arr[0], finalTestTexthttpTagLinkName[0]) + XCTAssertEqual(arr[1], finalTestTexthttpTagLinkName[1]) + XCTAssertEqual(arr[2], finalTestTexthttpTagLinkName[2]) + XCTAssertEqual(arr[3], finalTestTexthttpTagLinkName[3]) + XCTAssertEqual(arr[4], finalTestTexthttpTagLinkName[4]) + XCTAssertEqual(arr[5], finalTestTexthttpTagLinkName[5]) + } +}