IOS-1521 fixes: illegal attemp to set reference in between CdObjects from different contexts IOS-1521
authorbuff <andreas@pep-project.org>
Tue, 07 May 2019 12:33:04 +0200
branchIOS-1521
changeset 8509ebda9eac9dcc
parent 8508 0be70acaec88
child 8516 6d4a506443f2
IOS-1521 fixes: illegal attemp to set reference in between CdObjects from different contexts
pEpForiOSTests/SimpleOperationsTest.swift
     1.1 --- a/pEpForiOSTests/SimpleOperationsTest.swift	Tue May 07 12:32:04 2019 +0200
     1.2 +++ b/pEpForiOSTests/SimpleOperationsTest.swift	Tue May 07 12:33:04 2019 +0200
     1.3 @@ -1,86 +1,3 @@
     1.4 -//!!!: 'NSInvalidArgumentException', reason: 'Illegal attempt to establish a relationship 'message' between objects in different contexts
     1.5 -/*CoreData: warning:       'CdImapFlags' (0x60f000277120) from NSManagedObjectModel (0x6070007c65c0) claims 'CdImapFlags'.
     1.6 -2019-04-04 20:39:02.797769+0200 pEp[68212:2049767] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Illegal attempt to establish a relationship 'message' between objects in different contexts (source = <CdImapFields: 0x6070007cd080> (entity: CdImapFields; id: 0x60300083ff80 <x-coredata:///CdImapFields/t0D5C349D-D99B-4995-8345-B6EC2B45C9051475> ; data: {
     1.7 -contentType = nil;
     1.8 -localFlags = "0x6030007c2b20 <x-coredata:///CdImapFlags/t0D5C349D-D99B-4995-8345-B6EC2B45C9051476>";
     1.9 -message = nil;
    1.10 -messageNumber = 0;
    1.11 -mimeBoundary = nil;
    1.12 -serverFlags = "0x6030007d1220 <x-coredata:///CdImapFlags/t0D5C349D-D99B-4995-8345-B6EC2B45C9051477>";
    1.13 -}) , destination = <CdMessage: 0x60700062e090> (entity: CdMessage; id: 0x603000720610 <x-coredata://18D9E440-E589-4E84-82E9-C828D250E78F/CdMessage/p96> ; data: {
    1.14 -attachments =     (
    1.15 -"0x6030007bdf60 <x-coredata://18D9E440-E589-4E84-82E9-C828D250E78F/CdAttachment/p100>"
    1.16 -);
    1.17 -bcc =     (
    1.18 -);
    1.19 -cc =     (
    1.20 -);
    1.21 -comments = nil;
    1.22 -from = "0x6030005cfb30 <x-coredata://18D9E440-E589-4E84-82E9-C828D250E78F/CdIdentity/p97>";
    1.23 -imap = nil;
    1.24 -keysFromDecryption = "<relationship fault: 0x606000cff2c0 'keysFromDecryption'>";
    1.25 -keywords = "<relationship fault: 0x603000712e70 'keywords'>";
    1.26 -longMessage = "Long message 2";
    1.27 -longMessageFormatted = "<h1>Long HTML 2</h1>";
    1.28 -optionalFields =     (
    1.29 -);
    1.30 -pEpProtected = 1;
    1.31 -pEpRating = "-32768";
    1.32 -parent = "0x6030007eb470 <x-coredata://18D9E440-E589-4E84-82E9-C828D250E78F/CdFolder/p12>";
    1.33 -received = nil;
    1.34 -receivedBy = nil;
    1.35 -references =     (
    1.36 -);
    1.37 -replyTo =     (
    1.38 -);
    1.39 -sent = "2019-04-04 18:39:00 +0000";
    1.40 -shortMessage = "Some subject 2";
    1.41 -targetFolder = nil;
    1.42 -to =     (
    1.43 -"0x603000732c10 <x-coredata://18D9E440-E589-4E84-82E9-C828D250E78F/CdIdentity/p98>"
    1.44 -);
    1.45 -uid = 0;
    1.46 -underAttack = 0;
    1.47 -uuid = "1F5BC02D.B7CA.43F6.9ABD.1E2D98F0BE33@pretty.Easy.privacy";
    1.48 -}))'
    1.49 -*** First throw call stack:
    1.50 -(
    1.51 -0   CoreFoundation                      0x000000010f8126fb __exceptionPreprocess + 331
    1.52 -1   libobjc.A.dylib                     0x000000010e222ac5 objc_exception_throw + 48
    1.53 -2   CoreData                            0x000000010f1d63a1 _PFManagedObject_coerceValueForKeyWithDescription + 2481
    1.54 -3   CoreData                            0x000000010f1b2e15 _sharedIMPL_setvfk_core + 229
    1.55 -4   MessageModel                        0x000000010ba75cf3 $s12MessageModel02CdA0C10imapFields7contextAA0c4ImapE0CSo22NSManagedObjectContextCSg_tF + 2755
    1.56 -5   MessageModel                        0x000000010ba7a509 $s12MessageModel02CdA0C22assureImapFieldsNotNil7contextySo22NSManagedObjectContextC_tF + 1641
    1.57 -6   MessageModel                        0x000000010b96c3a3 $s12MessageModel0A0C8cdObject7contextAcA02CdA0C_So09NSManagedD7ContextCtcfc + 467
    1.58 -7   MessageModel                        0x000000010b96c16f $s12MessageModel0A0C8cdObject7contextAcA02CdA0C_So09NSManagedD7ContextCtcfC + 79
    1.59 -8   MessageModel                        0x000000010bad57e0 $s12MessageModel0aB11ObjectUtilsC03getA006fromCdA07contextAA0A0CAA0gA0C_So09NSManagedC7ContextCSgtF + 1248
    1.60 -9   MessageModel                        0x000000010ba7b07b $s12MessageModel02CdA0C7messageAA0A0CSgyF + 411
    1.61 -10  MessageModel                        0x000000010b94ee3b $s12MessageModel23EncryptAndSendOperationC23setOriginalRatingHeader33_DDE5BEED9A081413EDDB7FA0C8D7E8E9LL02toA9WithObjId9inContextySo17NSManagedObjectIDC_So0z6ObjectY0CtF + 1227
    1.62 -11  MessageModel                        0x000000010b94db8e $s12MessageModel23EncryptAndSendOperationC010handleNextA8Internal7contextySo22NSManagedObjectContextC_tF + 3902
    1.63 -12  MessageModel                        0x000000010b94bfdd $s12MessageModel23EncryptAndSendOperationC010handleNextA0yyFyycfU_ + 973
    1.64 -13  MessageModel                        0x000000010b9560c1 $s12MessageModel23EncryptAndSendOperationC010handleNextA0yyFyycfU_TA + 17
    1.65 -14  MessageModel                        0x000000010b8bb01e $sIeg_IeyB_TR + 142
    1.66 -15  CoreData                            0x000000010f1cc84a developerSubmittedBlockToNSManagedObjectContextPerform + 170
    1.67 -16  libclang_rt.asan_iossim_dynamic.dylib 0x000000010a7f37b4 asan_dispatch_call_block_and_release + 260
    1.68 -17  libdispatch.dylib                   0x000000011273cd02 _dispatch_client_callout + 8
    1.69 -18  libdispatch.dylib                   0x0000000112743720 _dispatch_lane_serial_drain + 705
    1.70 -19  libdispatch.dylib                   0x0000000112744261 _dispatch_lane_invoke + 398
    1.71 -20  libdispatch.dylib                   0x000000011274cfcb _dispatch_workloop_worker_thread + 645
    1.72 -21  libsystem_pthread.dylib             0x0000000112b1e611 _pthread_wqthread + 421
    1.73 -22  libsystem_pthread.dylib             0x0000000112b1e3fd start_wqthread + 13
    1.74 -*/
    1.75 -
    1.76 -/////
    1.77 -////  SimpleOperationsTest.swift
    1.78 -////  pEpForiOS
    1.79 -////
    1.80 -////  Created by Dirk Zimmermann on 03/05/16.
    1.81 -////  Copyright © 2016 p≡p Security S.A. All rights reserved.
    1.82 -////
    1.83 -//
    1.84 -//import XCTest
    1.85 -//
    1.86 -
    1.87  import XCTest
    1.88  
    1.89  import CoreData
    1.90 @@ -266,7 +183,7 @@
    1.91  
    1.92          let opLogin = LoginImapOperation(parentName: #function, imapSyncData: imapSyncData)
    1.93          guard let op = SyncFoldersFromServerOperation(parentName: #function,
    1.94 -                                                                 imapSyncData: imapSyncData)
    1.95 +                                                      imapSyncData: imapSyncData)
    1.96              else {
    1.97                  XCTFail()
    1.98                  return
    1.99 @@ -352,7 +269,7 @@
   1.100                  address: "somemail\(i)@test.com"))
   1.101              message.setSubject("Subject \(i)")
   1.102              message.setRecipients([CWInternetAddress(personal: "thisIsMe",
   1.103 -                                                          address: "myaddress@test.com", type: .toRecipient)])
   1.104 +                                                     address: "myaddress@test.com", type: .toRecipient)])
   1.105              message.setFolder(folder)
   1.106              message.setUID(UInt(i))
   1.107              message.setMessageID("\(i)@whatever.test")
   1.108 @@ -491,55 +408,54 @@
   1.109  
   1.110      // MARK: - EncryptAndSendOperation
   1.111  
   1.112 -    //!!!: crash : (reason: 'Illegal attempt to establish a relationship 'message' between objects in different contexts (source = <CdImapFields: 0x607000ba49d0> (entity: CdImapFields; )
   1.113 -//    func testEncryptAndSendOperation() {
   1.114 -//        // Create mails to send ...
   1.115 -//        let sentUUIDs = try! TestUtil.createOutgoingMails(cdAccount: cdAccount,
   1.116 -//                                                          testCase: self,
   1.117 -//                                                          numberOfMails: 3,
   1.118 -//                                                          context: moc).map { $0.uuid! }
   1.119 -//        // ... Login ...
   1.120 -//        let smtpSendData = SmtpSendData(connectInfo: smtpConnectInfo)
   1.121 -//        let errorContainer = ErrorContainer()
   1.122 -//        let smtpLogin = LoginSmtpOperation(parentName: #function,
   1.123 -//                                           smtpSendData: smtpSendData,
   1.124 -//                                           errorContainer: errorContainer)
   1.125 -//        smtpLogin.completionBlock = {
   1.126 -//            smtpLogin.completionBlock = nil
   1.127 -//            XCTAssertNotNil(smtpSendData.smtp)
   1.128 -//        }
   1.129 -//        // ... and send them.
   1.130 -//        let expMailsSent = expectation(description: "expMailsSent")
   1.131 -//        let sendOp = EncryptAndSendOperation(
   1.132 -//            parentName: #function,
   1.133 -//            smtpSendData: smtpSendData, errorContainer: errorContainer)
   1.134 -//        XCTAssertNotNil(EncryptAndSendOperation.retrieveNextMessage(context: moc,
   1.135 -//                                                                    cdAccount: cdAccount))
   1.136 -//        sendOp.addDependency(smtpLogin)
   1.137 -//        sendOp.completionBlock = {
   1.138 -//            sendOp.completionBlock = nil
   1.139 -//            expMailsSent.fulfill()
   1.140 -//        }
   1.141 -//        let queue = OperationQueue()
   1.142 -//        queue.addOperation(smtpLogin)
   1.143 -//        queue.addOperation(sendOp)
   1.144 -//        waitForExpectations(timeout: TestUtil.waitTime, handler: { error in
   1.145 -//            XCTAssertNil(error)
   1.146 -//            XCTAssertFalse(sendOp.hasErrors())
   1.147 -//        })
   1.148 -//        // Check sent status of all sent mails
   1.149 -//        for sentUuid in sentUUIDs {
   1.150 -//            let msgs = CdMessage.search(byUUID: sentUuid, includeFakeMessages: false)
   1.151 -//            XCTAssertEqual(msgs.count, 1)
   1.152 -//            guard let msg = msgs.first else {
   1.153 -//                XCTFail("Missing sent message")
   1.154 -//                return
   1.155 -//            }
   1.156 -//            // Have been moved from outbox to sent
   1.157 -//             XCTAssertEqual(msg.parent?.folderType, FolderType.sent)
   1.158 -//        }
   1.159 -//        smtpSendData.smtp?.close()
   1.160 -//    }
   1.161 +    func testEncryptAndSendOperation() {
   1.162 +        // Create mails to send ...
   1.163 +        let sentUUIDs = try! TestUtil.createOutgoingMails(cdAccount: cdAccount,
   1.164 +                                                          testCase: self,
   1.165 +                                                          numberOfMails: 3,
   1.166 +                                                          context: moc).map { $0.uuid! }
   1.167 +        // ... Login ...
   1.168 +        let smtpSendData = SmtpSendData(connectInfo: smtpConnectInfo)
   1.169 +        let errorContainer = ErrorContainer()
   1.170 +        let smtpLogin = LoginSmtpOperation(parentName: #function,
   1.171 +                                           smtpSendData: smtpSendData,
   1.172 +                                           errorContainer: errorContainer)
   1.173 +        smtpLogin.completionBlock = {
   1.174 +            smtpLogin.completionBlock = nil
   1.175 +            XCTAssertNotNil(smtpSendData.smtp)
   1.176 +        }
   1.177 +        // ... and send them.
   1.178 +        let expMailsSent = expectation(description: "expMailsSent")
   1.179 +        let sendOp = EncryptAndSendOperation(
   1.180 +            parentName: #function,
   1.181 +            smtpSendData: smtpSendData, errorContainer: errorContainer)
   1.182 +        XCTAssertNotNil(EncryptAndSendOperation.retrieveNextMessage(context: moc,
   1.183 +                                                                    cdAccount: cdAccount))
   1.184 +        sendOp.addDependency(smtpLogin)
   1.185 +        sendOp.completionBlock = {
   1.186 +            sendOp.completionBlock = nil
   1.187 +            expMailsSent.fulfill()
   1.188 +        }
   1.189 +        let queue = OperationQueue()
   1.190 +        queue.addOperation(smtpLogin)
   1.191 +        queue.addOperation(sendOp)
   1.192 +        waitForExpectations(timeout: TestUtil.waitTime, handler: { error in
   1.193 +            XCTAssertNil(error)
   1.194 +            XCTAssertFalse(sendOp.hasErrors())
   1.195 +        })
   1.196 +        // Check sent status of all sent mails
   1.197 +        for sentUuid in sentUUIDs {
   1.198 +            let msgs = CdMessage.search(byUUID: sentUuid, includeFakeMessages: false)
   1.199 +            XCTAssertEqual(msgs.count, 1)
   1.200 +            guard let msg = msgs.first else {
   1.201 +                XCTFail("Missing sent message")
   1.202 +                return
   1.203 +            }
   1.204 +            // Have been moved from outbox to sent
   1.205 +            XCTAssertEqual(msg.parent?.folderType, FolderType.sent)
   1.206 +        }
   1.207 +        smtpSendData.smtp?.close()
   1.208 +    }
   1.209  
   1.210      func testAppendSentMailsOperation() {
   1.211          let imapSyncData = ImapSyncData(connectInfo: imapConnectInfo)
   1.212 @@ -789,20 +705,22 @@
   1.213  
   1.214      //fails on first run when the an account was setup on
   1.215      func testFixAttachmentsOperation() {
   1.216 -          let moc = Stack.shared.newPrivateConcurrentContext
   1.217 +        let moc = Stack.shared.newPrivateConcurrentContext
   1.218          let cdFolder = CdFolder(context: moc)
   1.219          cdFolder.name = "AttachmentTestFolder"
   1.220          cdFolder.folderType = FolderType.inbox
   1.221 -        cdFolder.account = cdAccount
   1.222 +        cdFolder.account = (moc.object(with: cdAccount.objectID) as! CdAccount)
   1.223  
   1.224 -        let cdMsg = CdMessage.create(messageID: "2", uid: 1, parent: cdFolder)
   1.225 +        let cdMsg = CdMessage(context: moc)
   1.226 +        cdMsg.uuid = "2"
   1.227 +        cdMsg.parent = cdFolder
   1.228  
   1.229          let cdAttachWithoutSize = CdAttachment(context: moc)
   1.230          cdAttachWithoutSize.data = "Some bytes for an attachment".data(using: .utf8)
   1.231          cdAttachWithoutSize.message = cdMsg
   1.232 -        
   1.233 +
   1.234          moc.saveAndLogErrors()
   1.235 -        
   1.236 +
   1.237          let expAttachmentsFixed = expectation(description: "expAttachmentsFixed")
   1.238          let fixAttachmentsOp = FixAttachmentsOperation(parentName: #function)
   1.239          fixAttachmentsOp.completionBlock = {
   1.240 @@ -816,7 +734,7 @@
   1.241              XCTAssertNil(error)
   1.242              XCTAssertFalse(fixAttachmentsOp.hasErrors())
   1.243          })
   1.244 -        
   1.245 +
   1.246          moc.refreshAllObjects()
   1.247          
   1.248          guard let allAttachments = CdAttachment.all() as? [CdAttachment] else {