From 61999e3a04b79beb9045b30d992176da02a6d4c3 Mon Sep 17 00:00:00 2001
From: Lauren Elden <B.Lauren1695@gmail.com>
Date: Tue, 31 Mar 2020 11:52:32 +0200
Subject: [PATCH] #137: Added some comments + re-adjusted the code a bit +
 moved test case testfindMailForSecretKey + help function testkey() from
 CoreDataTests.swift to CryptoTests.swift

---
 enzevalos_iphone/DataHandler.swift            | 14 +++---
 enzevalos_iphone/SwiftPGP.swift               | 13 +++--
 enzevalos_iphoneTests/CoreDataTests.swift     | 46 -----------------
 enzevalos_iphoneTests/CryptoTests.swift       | 49 ++++++++++++++++++-
 .../phishing/UrlStringExtensionTests.swift    | 15 ------
 5 files changed, 64 insertions(+), 73 deletions(-)

diff --git a/enzevalos_iphone/DataHandler.swift b/enzevalos_iphone/DataHandler.swift
index 402f99c0..c3f22f4c 100644
--- a/enzevalos_iphone/DataHandler.swift
+++ b/enzevalos_iphone/DataHandler.swift
@@ -1119,19 +1119,17 @@ class DataHandler {
         return []
     }
     
+    /**
+        Filters all Presistent Mails with encState == EncryptionState.UnableToDecrypt
+     */
     func getAllNotDecryptedPersistentMail() -> [PersistentMail] {
         let result = getAllPersistentMails().filter({ $0.encState == EncryptionState.UnableToDecrypt })
         return result
     }
     
-    // filter all presistent mails without public key
-    func getAllNoPublicKeyPersistentMail() -> [PersistentMail] {
-        let result = getAllPersistentMails().filter({ $0.sigState == SignatureState.NoPublicKey })
-        return result
-    }
-    
-    
-    
+    /**
+     Filters all Presistent Mails with sigState == SignatureState.NoPublicKey
+     */
     func getAllNotSignedPersistentMail() -> [PersistentMail] {
         let result = getAllPersistentMails().filter({ $0.sigState == SignatureState.NoPublicKey })
         return result
diff --git a/enzevalos_iphone/SwiftPGP.swift b/enzevalos_iphone/SwiftPGP.swift
index c240656c..8d566aa8 100644
--- a/enzevalos_iphone/SwiftPGP.swift
+++ b/enzevalos_iphone/SwiftPGP.swift
@@ -596,7 +596,10 @@ class SwiftPGP: Encryption {
         return CryptoObject(chiphertext: data, plaintext: plaintext, decryptedData: plaindata, sigState: sigState, encState: encState, signKey: sigKeyID, encType: CryptoScheme.PGP, signedAdrs: signedAdr)
     }
     
-    // A help function for findMailForSecrectKey and findNotSignedMailForPublicKey
+    /**
+     A help function for findMailForSecrectKey and findNotSignedMailForPublicKey
+     Receives a keyID as String and returns the corresponding key as a [Key] List.
+     */
     func keyAsKeyList(keyID: String) -> [Key] {
         var keyList = [Key]()
         if let key: Key = loadKey(id: keyID) {
@@ -605,7 +608,9 @@ class SwiftPGP: Encryption {
         return keyList
     }
     
-    // Finds undecrypted mails in persistent mails and and tries to decrypt with the incoming secret key
+    /**
+     Receives a keyID as String and searches for undecrypted mails in persistent mails and and tries to decrypt them with the incoming secret key.
+     */
     func findMailForSecretKey(keyID: String) {
         var encState = EncryptionState.UnableToDecrypt
         var plaindata: Data? = nil
@@ -655,7 +660,9 @@ class SwiftPGP: Encryption {
         return (nil, EncryptionState.NoEncryption)
     }
     
-    // Finds unsigned mails in persistent mails and and tries to verify them with the incoming public key
+    /**
+    Receives a keyID as String and searches for unsigned mails in persistent mails and and tries to verify them with the incoming public key.
+    */
     func findNotSignedMailForPublicKey(keyID: String) {
         var sigState = SignatureState.NoPublicKey
         let key: [Key] = keyAsKeyList(keyID: keyID)
diff --git a/enzevalos_iphoneTests/CoreDataTests.swift b/enzevalos_iphoneTests/CoreDataTests.swift
index 1386de55..9d041ae6 100644
--- a/enzevalos_iphoneTests/CoreDataTests.swift
+++ b/enzevalos_iphoneTests/CoreDataTests.swift
@@ -104,52 +104,6 @@ class CoraDataTests: XCTestCase {
         }
     }
     
-    // Generate a test secret key
-    func testkey() -> (String){
-        let testsender = createUser()
-        let testkeyID = pgp.generateKey(adr: testsender.mailbox, new: true)
-        return testkeyID
-        
-    }
-    
-    // Test decryption of all undecrypted mails with new secret key
-    func testfindMailForSecretKey() {
-        let testkeyID = testkey()
-        let swiftpgp = SwiftPGP()
-        
-        // E-Mail generieren
-        guard let from = MCOAddress(mailbox: "sender@example.com") else {
-            return
-        }
-        
-        // Create test Mail
-        guard let m1 = testMail(from: from, to: [user], cc: [], bcc: []) else {
-            XCTFail("No test mail")
-            return
-        }
-            
-        // Create Ciphertext
-        let body = "encrypted text"
-        let senderPGP = SwiftPGP()
-        let encryptedObject = senderPGP.encrypt(plaintext: body, ids: [testkeyID], myId: "")
-        XCTAssert(encryptedObject.encryptionState == .ValidedEncryptedWithCurrentKey && encryptedObject.signatureState == .NoSignature)
-
-        guard let cipher = encryptedObject.chiperString else {
-            XCTFail("No chipher data")
-            return
-        }
-        
-        // Change Mail
-        m1.unableToDecrypt = true
-        m1.body = cipher
-        
-        datahandler.save(during: "")
-        
-        swiftpgp.findMailForSecretKey(keyID: testkeyID)
-        XCTAssertEqual(m1.body, "encrypted text")
-        XCTAssertFalse(m1.unableToDecrypt)
-    }
-
     func createUser(adr: String = String.random().lowercased(), name: String = String.random()) -> MCOAddress {
         return MCOAddress.init(displayName: name, mailbox: adr.lowercased())
     }
diff --git a/enzevalos_iphoneTests/CryptoTests.swift b/enzevalos_iphoneTests/CryptoTests.swift
index 06cbf1c0..84e8b148 100644
--- a/enzevalos_iphoneTests/CryptoTests.swift
+++ b/enzevalos_iphoneTests/CryptoTests.swift
@@ -610,7 +610,54 @@ class CryptoTests: XCTestCase {
         return mail
     }
     
-    // Not finished yet
+    /**
+     Generate a test secret key
+     */
+    func testkey() -> (String){
+        let testsender = createUser()
+        let testkeyID = pgp.generateKey(adr: testsender.mailbox, new: true)
+        return testkeyID
+    }
+    
+    func testfindMailForSecretKey() {
+        let testkeyID = testkey()
+        
+        // E-Mail generieren
+        guard let from = MCOAddress(mailbox: "sender@example.com") else {
+            return
+        }
+        
+        // Create test Mail
+        guard let m1 = testMail(from: from, to: [user], cc: [], bcc: []) else {
+            XCTFail("No test mail")
+            return
+        }
+            
+        // Create Ciphertext
+        let body = "encrypted text"
+        let senderPGP = SwiftPGP()
+        let encryptedObject = senderPGP.encrypt(plaintext: body, ids: [testkeyID], myId: "")
+        XCTAssert(encryptedObject.encryptionState == .ValidedEncryptedWithCurrentKey && encryptedObject.signatureState == .NoSignature)
+
+        guard let cipher = encryptedObject.chiperString else {
+            XCTFail("No chipher data")
+            return
+        }
+        
+        // Change Mail
+        m1.unableToDecrypt = true
+        m1.body = cipher
+        
+        datahandler.save(during: "")
+        
+        pgp.findMailForSecretKey(keyID: testkeyID)
+        XCTAssertEqual(m1.body, "encrypted text")
+        XCTAssertFalse(m1.unableToDecrypt)
+    }
+    
+    /*
+     !!!!! Not finished yet !!!!!
+     */
     func testfindNotSignedMailForPublicKey() {
         // E-Mail generieren
         guard let from = MCOAddress(mailbox: "alice@example.com") else {
diff --git a/enzevalos_iphoneTests/phishing/UrlStringExtensionTests.swift b/enzevalos_iphoneTests/phishing/UrlStringExtensionTests.swift
index 742e6387..2ea8a380 100644
--- a/enzevalos_iphoneTests/phishing/UrlStringExtensionTests.swift
+++ b/enzevalos_iphoneTests/phishing/UrlStringExtensionTests.swift
@@ -109,21 +109,6 @@ class UrlStringExtensionTests: XCTestCase {
         XCTAssertEqual(sld[2], finalSLD[2])
         XCTAssertEqual(sld[3], finalSLD[3])
     }
-        
-    func testIsValidRD(){
-        let pattern = testMailText.isValidRD()
-        XCTAssertNotNil(pattern)
-        XCTAssertEqual(pattern[0], vaildRD[0])
-        XCTAssertEqual(pattern[1], vaildRD[1])
-        XCTAssertEqual(pattern[2], vaildRD[2])
-        XCTAssertEqual(pattern[3], vaildRD[3])
-        XCTAssertEqual(pattern[4], vaildRD[4])
-        XCTAssertEqual(pattern[5], vaildRD[5])
-        XCTAssertEqual(pattern[6], vaildRD[6])
-        XCTAssertEqual(pattern[7], vaildRD[7])
-        XCTAssertEqual(pattern[8], vaildRD[8])
-        XCTAssertEqual(pattern[9], vaildRD[9])
-    }
     
     func testIsAllowedDistance() {
         let str = "google"
-- 
GitLab