Skip to content
Snippets Groups Projects
Commit ccd7b36c authored by Oliver Wiese's avatar Oliver Wiese
Browse files

add test cases update coredata

parent 4718edc4
No related branches found
No related tags found
No related merge requests found
Showing
with 447 additions and 169 deletions
......@@ -139,7 +139,7 @@ open class EnzevalosContact: NSManagedObject, Contact, Comparable {
get{
var pks = Set<PersistentKey>()
for adr in getMailAddresses(){
pks = pks.union(adr.keys)
pks = pks.union(adr.publicKeys)
}
return pks
}
......
......@@ -12,7 +12,7 @@ import Contacts
open class CNMailAddressExtension: MailAddress{
public var primaryKey: PersistentKey?
public var keys: Set<PersistentKey>
public var publicKeys: Set<PersistentKey>
open var mailAddress:String{
......@@ -50,7 +50,7 @@ open class CNMailAddressExtension: MailAddress{
init(addr: CNLabeledValue<NSString>){ //FIXME: NSString hier richtig?
self.label = addr
self.keys = Set<PersistentKey>()
self.publicKeys = Set<PersistentKey>()
}
convenience init(addr: NSString){ //FIXME: manuell: String -> NSString
......
......@@ -188,7 +188,7 @@ class DataHandler {
let fReq = NSFetchRequest<NSFetchRequestResult>(entityName: "PersistentMail")
var predicates = [NSPredicate]()
predicates.append(NSPredicate(format: "folder = %@", folder))
predicates.append(NSPredicate(format: "keyID != nil"))
predicates.append(NSPredicate(format: "keyID.length > 0"))
let andPredicates = NSCompoundPredicate(andPredicateWithSubpredicates: predicates)
fReq.predicate = andPredicates
......@@ -207,7 +207,17 @@ class DataHandler {
}
}
return keys
var mykeys = Set<String>()
if let set = folder.mails{
if let mails = set as? Set<PersistentMail>{
for mail in mails{
if let key = mail.keyID{
mykeys.insert(key)
}
}
}
}
return Array(mykeys)
}
......@@ -443,7 +453,7 @@ class DataHandler {
}
}
}
adr.primaryKeyID = pk.keyID
return pk
}
......@@ -682,11 +692,13 @@ class DataHandler {
func getContact(name: String, address: String, key: String, prefer_enc: Bool) -> EnzevalosContact {
let contact = getContactByAddress(address)
contact.displayname = name
contact.getAddress(address)?.key?.adding(key)
if let mykey = findKey(keyID: key){
contact.getAddress(address)?.addToKeys(mykey)
}
if address == "ullimuelle@web.de"{
print("New Key: \(key) for ulli!")
}
//TODO IOptimize: look for Mail_Address and than for contact!
//TODO Optimize: look for Mail_Address and than for contact!
return contact
}
......@@ -809,6 +821,7 @@ class DataHandler {
mail.isSigned = true
if let signedKey = findKey(keyID: decData.signKey!){
mail.signedKey = signedKey
mail.keyID = signedKey.keyID
}
else{
mail.signedKey = newPublicKey(keyID: decData.signKey!, cryptoType: decData.encType, adr: decData.signedAdrs.first!, autocrypt: false, firstMail: mail, newGenerated: false)
......@@ -832,18 +845,6 @@ class DataHandler {
}
myfolder.updateRecords(mail: mail)
save(during: "new mail")
if let mails = myfolder.mails{
for m in mails{
if let x = m as? PersistentMail{
// print("\(x.subject) from \(x.from.mailAddress) at \(x.date)")
if x.subject == mail.subject && x.date == mail.date{
print("Mail found in folder!")
}
}
}
}
return mail
}
......
......@@ -65,7 +65,8 @@ public class Folder: NSManagedObject {
let record = KeyRecord(contact: ec, folder: self)
if record.mails.count > 0{
records.append(record)
} }
}
}
}
return records.sorted()
//return records // TODO: Sorting makes function to slow!
......@@ -154,5 +155,18 @@ public class Folder: NSManagedObject {
storedRecords?.sort()
}
}
else{
if mail.folder == self{
storedRecords = [KeyRecord]()
if mail.isSecure && mail.keyID != nil{
let record = KeyRecord(keyID: mail.keyID!, folder: self)
storedRecords?.append(record)
}
else{
let record = KeyRecord(contact: mail.from.contact!, folder: self)
storedRecords?.append(record)
}
}
}
}
}
......@@ -22,6 +22,9 @@ extension Folder {
@NSManaged public var path: String
@NSManaged public var lastUpdate: Date?
@NSManaged public var pseudonym: String
@NSManaged public var icon: String
@NSManaged public var uivalidity: Int32
@NSManaged public var delimiter: String
public var flags: MCOIMAPFolderFlag{
......@@ -58,7 +61,6 @@ extension Folder {
if text == nil{
return 1
}
return text!
}
}
......
......@@ -71,6 +71,7 @@ open class KeyRecord: Record {
}
}
open var mails: [PersistentMail] {
get{
return mailsInFolder(folder: folder)
......
......@@ -22,7 +22,7 @@ public protocol Mail: Comparable {
}
public func == <T: Mail> (lhs: T, rhs: T) -> Bool {
return lhs.date == rhs.date && lhs.uid == rhs.uid
return lhs.date == rhs.date && lhs.uid == rhs.uid //TODO: update see: https://www.limilabs.com/blog/unique-id-in-imap-protocol
}
public func << T: Mail > (lhs: T, rhs: T) -> Bool {
......
......@@ -71,7 +71,7 @@ public protocol MailAddress {
var hasKey: Bool{get}
var primaryKey: PersistentKey?{get}
var keys: Set<PersistentKey>{get}
var publicKeys: Set<PersistentKey>{get}
var contact: EnzevalosContact?{get}
}
......@@ -503,10 +503,11 @@ class MailHandler {
let y = imapsession.folderStatusOperation(INBOX)
y?.start{(error, status) -> Void in
print("Folder status: \(status.debugDescription)")
}
let x = imapsession.folderStatusOperation(INBOX)
x?.start{(e,info) -> Void in
print("Folder infos: \(info.debugDescription)")
// TODO: UIDValality check here!
let uidValidity = status?.uidValidity
let nextId = status?.uidNext
let unseencount = status?.unseenCount
}
return imapsession
......@@ -635,6 +636,7 @@ class MailHandler {
private func loadMessagesFromServer(_ uids: MCOIndexSet, folderPath: String, maxLoad: Int = MailHandler.MAXMAILS,record: KeyRecord?, newMailCallback: @escaping ((_ mail: PersistentMail?) -> ()), completionCallback: @escaping ((_ error: Bool) -> ())) {
let requestKind = MCOIMAPMessagesRequestKind(rawValue: MCOIMAPMessagesRequestKind.headers.rawValue | MCOIMAPMessagesRequestKind.flags.rawValue)
let fetchOperation: MCOIMAPFetchMessagesOperation = self.IMAPSession.fetchMessagesOperation(withFolder: folderPath, requestKind: requestKind, uids: uids)
fetchOperation.extraHeaders = [AUTOCRYPTHEADER, SETUPMESSAGE]
if uids.count() == 0{
......@@ -648,6 +650,7 @@ class MailHandler {
completionCallback(true)
return
}
var calledMails = 0
if let msgs = msg {
let dispatchGroup = DispatchGroup()
......@@ -678,6 +681,12 @@ class MailHandler {
return
}
let a = MCOAttachment()
var rec: [MCOAddress] = []
var cc: [MCOAddress] = []
var autocrypt: AutocryptContact? = nil
......@@ -727,7 +736,6 @@ class MailHandler {
for a in (msgParser?.attachments())! {
let at = a as! MCOAttachment
print("Attachment! \n type: \(at.mimeType) string: \(at.decodedString()) \n contentdesc: \(at.contentDescription) \n content ID: \(at.contentID)")
if at.mimeType == "application/pgp-encrypted" {
isEnc = true
}
......@@ -819,7 +827,6 @@ class MailHandler {
private func parsePublicKeys(attachment: MCOAttachment) -> [String]{
var newKey = [String]()
if let content = attachment.decodedString(){
print("Content: ####### \n \(content) \n ######")
if content.contains("-----BEGIN PGP PUBLIC KEY BLOCK-----"){
if let start = content.range(of: "-----BEGIN PGP PUBLIC KEY BLOCK-----"){
if let end = content.range(of: "-----END PGP PUBLIC KEY BLOCK-----\n"){
......@@ -872,15 +879,12 @@ class MailHandler {
let pgp = SwiftPGP()
var keyIds = [String]()
if sender != nil, let adr = DataHandler.handler.findMailAddress(adr: sender!){
print(adr.address)
print(adr.key?.count)
if let keys = adr.key{
for k in keys{
//if let keys = adr.keys{
for k in adr.publicKeys{
let key = k as! PersistentKey
print(key.keyID)
keyIds.append(key.keyID)
}
}
//}
}
if let a = autocrypt{
let key = try! pgp.importKeys(key: a.key, pw: nil, isSecretKey: false, autocrypt: true)
......@@ -987,7 +991,6 @@ class MailHandler {
loadMailsSinceDate(folder: inbox, since: date, maxLoad: 100, newMailCallback: newMailCallback, completionCallback: completionCallback)
}
else{
print("No date for init inbox!")
initFolder(folder: inbox, newMailCallback: newMailCallback, completionCallback: completionCallback)
}
......
......@@ -13,15 +13,32 @@ import Contacts
@objc(Mail_Address)
open class Mail_Address: NSManagedObject, MailAddress {
public var primaryKey: PersistentKey?{
get{
if hasKey{
return self.key?.anyObject() as? PersistentKey
for key in publicKeys{
if key.keyID == primaryKeyID {
return key
}
}
}
return nil
}
}
open var publicKeys: Set<PersistentKey>{
get{
if let pks = keys{
if let publicKeys = pks as? Set<PersistentKey>{
return publicKeys
}
}
return Set<PersistentKey>()
}
}
open var mailAddress: String {
return address.lowercased()
......@@ -38,20 +55,10 @@ open class Mail_Address: NSManagedObject, MailAddress {
return CNLabeledValue.init(label: CNLabelOther, value: address as NSString)
}
open var keys: Set<PersistentKey>{
get{
if self.key != nil{
if let mykeys = self.key as? Set<PersistentKey>{
return mykeys
}
}
return Set<PersistentKey>()
}
}
open var hasKey: Bool {
if key != nil && (key?.count)! > 0{
if publicKeys.count > 0{
return true
}
return false
......
......@@ -22,8 +22,9 @@ extension Mail_Address {
@NSManaged public var cc: NSSet?
@NSManaged public var from: NSSet?
@NSManaged public var to: NSSet?
@NSManaged public var key: NSSet?
@NSManaged public var pseudonym: String
@NSManaged public var primaryKeyID: String
@NSManaged public var keys: NSSet?
}
......@@ -83,16 +84,16 @@ extension Mail_Address {
// MARK: Generated accessors for key
extension Mail_Address {
@objc(addKeyObject:)
@NSManaged public func addToKey(_ value: PersistentKey)
@objc(addKeysObject:)
@NSManaged public func addToKeys(_ value: PersistentKey)
@objc(removeKeyObject:)
@NSManaged public func removeFromKey(_ value: PersistentKey)
@objc(removeKeysObject:)
@NSManaged public func removeFromKeys(_ value: PersistentKey)
@objc(addKey:)
@NSManaged public func addToKey(_ values: NSSet)
@objc(addKeys:)
@NSManaged public func addToKeys(_ values: NSSet)
@objc(removeKey:)
@NSManaged public func removeFromKey(_ values: NSSet)
@objc(removeKeys:)
@NSManaged public func removeFromKeys(_ values: NSSet)
}
......@@ -54,13 +54,30 @@ extension PersistentMail {
@NSManaged public var signedKey: PersistentKey?
public var keyID: String?{
set {
self.willChangeValue(forKey: "keyID")
self.setPrimitiveValue(newValue, forKey: "keyID")
self.didChangeValue(forKey: "keyID")
}
get {
if let key = signedKey{
return key.keyID
var signKeyID: String?
if let k = self.signedKey{
signKeyID = k.keyID
}
self.willAccessValue(forKey: "keyID")
if let text = self.primitiveValue(forKey: "keyID"){
signKeyID = text as? String
}
return nil
else{
if let id = signKeyID{
self.setPrimitiveValue(id, forKey: "keyID")
}
}
self.didAccessValue(forKey: "keyID")
return signKeyID
}
}
public var trouble: Bool{
set {
......
......@@ -240,7 +240,7 @@ class SwiftPGP: Encryption{
return nil
}
private func exportKeyData(id: String, isSecretkey: Bool) -> Data?{
if var key = loadKey(id: id){
if let key = loadKey(id: id){
if key.isSecret && isSecretkey{
if let keyData = try? key.export(keyType: PGPKeyType.secret){
return keyData
......@@ -259,21 +259,16 @@ class SwiftPGP: Encryption{
let keyring = Keyring()
let signKey = loadKey(id: myId)
if signKey != nil{
print("signing id: \(myId)")
keyring.import(keys: [signKey!])
}
let signedAdr = vaildAddress(key: signKey)
for id in ids{
if let key = loadKey(id: id){
keyring.import(keys: [key])
print("ID to encrypt: \(id)")
}
}
if let data = plaintext.data(using: String.Encoding.utf8){
do{
for key in keyring.keys{
print("Key: \(key.keyID.longIdentifier) is secret?: \(key.isSecret)")
}
let chipher = try ObjectivePGP.encrypt(data, addSignature: true, using: keyring.keys, passphraseForKey: loadPassword)
let armorChipherString = Armor.armored(chipher, as: .message)
let armorChipherData = armorChipherString.data(using: .utf8)
......
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="13241" systemVersion="16G29" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier="">
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="13772" systemVersion="16G1212" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier="">
<entity name="Attachment" representedClassName="Attachment" syncable="YES" codeGenerationType="class">
<attribute name="data" optional="YES" attributeType="Binary" syncable="YES"/>
<attribute name="encryptionState" optional="YES" attributeType="Integer 16" defaultValueString="0" usesScalarValueType="YES" syncable="YES"/>
<attribute name="id" optional="YES" attributeType="Integer 64" defaultValueString="0" usesScalarValueType="YES" syncable="YES"/>
<attribute name="signatureState" optional="YES" attributeType="Integer 16" defaultValueString="0" usesScalarValueType="YES" syncable="YES"/>
<attribute name="type" optional="YES" attributeType="Integer 16" defaultValueString="0" usesScalarValueType="YES" syncable="YES"/>
<relationship name="mail" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="PersistentMail" syncable="YES"/>
</entity>
<entity name="EnzevalosContact" representedClassName="EnzevalosContact" syncable="YES">
<attribute name="cnidentifier" optional="YES" attributeType="String" syncable="YES"/>
<attribute name="color" optional="YES" attributeType="Transformable" customClassName="UIColor" syncable="YES"/>
......@@ -9,22 +17,25 @@
<entity name="Folder" representedClassName="Folder" syncable="YES">
<attribute name="delimiter" optional="YES" attributeType="String" syncable="YES"/>
<attribute name="flags" optional="YES" attributeType="Integer 16" defaultValueString="0" usesScalarValueType="YES" syncable="YES"/>
<attribute name="icon" optional="YES" attributeType="String" syncable="YES"/>
<attribute name="lastUpdate" optional="YES" attributeType="Date" usesScalarValueType="NO" syncable="YES"/>
<attribute name="maxID" optional="YES" attributeType="Decimal" defaultValueString="1" syncable="YES"/>
<attribute name="path" attributeType="String" syncable="YES"/>
<attribute name="pseudonym" attributeType="String" syncable="YES"/>
<attribute name="uidvalidity" optional="YES" attributeType="Integer 32" defaultValueString="0.0" usesScalarValueType="YES" syncable="YES"/>
<relationship name="mails" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="PersistentMail" inverseName="folder" inverseEntity="PersistentMail" syncable="YES"/>
<relationship name="parent" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="Folder" inverseName="subfolder" inverseEntity="Folder" syncable="YES"/>
<relationship name="subfolder" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="Folder" inverseName="parent" inverseEntity="Folder" syncable="YES"/>
</entity>
<entity name="Mail_Address" representedClassName="Mail_Address" syncable="YES">
<attribute name="address" attributeType="String" defaultValueString="&quot;&quot;" syncable="YES"/>
<attribute name="primaryKeyID" optional="YES" attributeType="String" syncable="YES"/>
<attribute name="pseudonym" attributeType="String" syncable="YES"/>
<relationship name="bcc" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="PersistentMail" inverseName="bcc" inverseEntity="PersistentMail" syncable="YES"/>
<relationship name="cc" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="PersistentMail" inverseName="cc" inverseEntity="PersistentMail" syncable="YES"/>
<relationship name="contact" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="EnzevalosContact" inverseName="addresses" inverseEntity="EnzevalosContact" syncable="YES"/>
<relationship name="from" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="PersistentMail" inverseName="from" inverseEntity="PersistentMail" syncable="YES"/>
<relationship name="key" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="PersistentKey" inverseName="mailaddress" inverseEntity="PersistentKey" syncable="YES"/>
<relationship name="keys" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="PersistentKey" inverseName="mailaddress" inverseEntity="PersistentKey" syncable="YES"/>
<relationship name="to" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="PersistentMail" inverseName="to" inverseEntity="PersistentMail" syncable="YES"/>
</entity>
<entity name="PersistentKey" representedClassName="PersistentKey" syncable="YES">
......@@ -38,7 +49,7 @@
<attribute name="sentOwnPublicKey" attributeType="Boolean" defaultValueString="NO" usesScalarValueType="YES" syncable="YES"/>
<attribute name="verifiedDate" optional="YES" attributeType="Date" usesScalarValueType="NO" syncable="YES"/>
<relationship name="firstMail" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="PersistentMail" inverseName="firstKey" inverseEntity="PersistentMail" syncable="YES"/>
<relationship name="mailaddress" toMany="YES" deletionRule="Nullify" destinationEntity="Mail_Address" inverseName="key" inverseEntity="Mail_Address" syncable="YES"/>
<relationship name="mailaddress" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="Mail_Address" inverseName="keys" inverseEntity="Mail_Address" syncable="YES"/>
<relationship name="signedMails" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="PersistentMail" inverseName="signedKey" inverseEntity="PersistentMail" syncable="YES"/>
</entity>
<entity name="PersistentMail" representedClassName="PersistentMail" syncable="YES">
......@@ -77,10 +88,11 @@
<fetchRequest name="getMailAddress" entity="Mail_Address" predicateString="address == &quot;$adr&quot;"/>
<elements>
<element name="EnzevalosContact" positionX="-209" positionY="198" width="128" height="105"/>
<element name="Folder" positionX="-297" positionY="-18" width="128" height="180"/>
<element name="Mail_Address" positionX="-297" positionY="-18" width="128" height="165"/>
<element name="Folder" positionX="-297" positionY="-18" width="128" height="210"/>
<element name="Mail_Address" positionX="-297" positionY="-18" width="128" height="180"/>
<element name="PersistentKey" positionX="-315" positionY="-36" width="128" height="225"/>
<element name="PersistentMail" positionX="-416" positionY="-189" width="128" height="360"/>
<element name="SecretKey" positionX="-306" positionY="-27" width="128" height="105"/>
<element name="Attachment" positionX="-315" positionY="-36" width="128" height="135"/>
</elements>
</model>
\ No newline at end of file
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment