pEpForiOSTests/DecryptionTests.swift
author Xavier Algarra <xavier@pep-project.org>
Tue, 06 Jun 2017 07:49:23 +0200
changeset 2248 75013a586bfd
parent 2180 6d917e49cd3a
child 2260 fc44c2834df4
permissions -rw-r--r--
IOS-137 add multiple accounts option with the new login
     1 //
     2 //  DecryptionTests.swift
     3 //  pEpForiOS
     4 //
     5 //  Created by Dirk Zimmermann on 15.05.17.
     6 //  Copyright © 2017 p≡p Security S.A. All rights reserved.
     7 //
     8 
     9 import XCTest
    10 
    11 import pEpForiOS
    12 import MessageModel
    13 
    14 class DecryptionTests: XCTestCase {
    15     var cdOwnAccount: CdAccount!
    16     var pEpOwnIdentity: PEPIdentity!
    17     var cdSenderAccount: CdAccount!
    18     var pEpSenderIdentity: PEPIdentity!
    19     var cdInbox: CdFolder!
    20 
    21     var persistentSetup: PersistentSetup!
    22     var session = PEPSession()
    23     var backgroundQueue: OperationQueue!
    24 
    25     override func setUp() {
    26         super.setUp()
    27 
    28         persistentSetup = PersistentSetup()
    29 
    30         let cdMyAccount = TestData().createWorkingCdAccount(number: 0)
    31         cdMyAccount.identity?.isMySelf = true
    32         let cdSenderAccount = TestData().createWorkingCdAccount(number: 1)
    33         cdSenderAccount.identity?.isMySelf = true
    34 
    35         cdInbox = CdFolder.create()
    36         cdInbox.name = ImapSync.defaultImapInboxName
    37         cdInbox.uuid = MessageID.generate()
    38         cdInbox.account = cdMyAccount
    39 
    40         TestUtil.skipValidation()
    41         Record.saveAndWait()
    42 
    43         self.cdOwnAccount = cdMyAccount
    44         self.cdSenderAccount = cdSenderAccount
    45 
    46         self.pEpOwnIdentity = pEpIdentity(cdAccount: cdMyAccount)
    47         self.pEpSenderIdentity = pEpIdentity(cdAccount: cdSenderAccount)
    48 
    49         self.backgroundQueue = OperationQueue()
    50     }
    51 
    52     override func tearDown() {
    53         persistentSetup = nil
    54         backgroundQueue.cancelAllOperations()
    55         backgroundQueue = nil
    56         super.tearDown()
    57     }
    58 
    59     func pEpIdentity(cdAccount: CdAccount) -> PEPIdentity? {
    60         guard
    61             let identityDict = cdAccount.identity?.pEpIdentity().mutableDictionary() else {
    62                 XCTFail()
    63                 return nil
    64         }
    65         session.mySelf(identityDict)
    66         guard let pEpId = identityDict as? PEPIdentity  else {
    67             XCTFail()
    68             return nil
    69         }
    70         return pEpId
    71     }
    72 
    73     func testBasicDecryption(shouldEncrypt: Bool) {
    74         let msgLongMessage = "This is a message!"
    75         let msgShortMessage = "Subject1"
    76         var pEpMsg = PEPMessage()
    77         pEpMsg[kPepFrom] = pEpSenderIdentity as AnyObject
    78         pEpMsg[kPepTo] = [pEpOwnIdentity] as NSArray
    79         pEpMsg[kPepLongMessage] = "Subject: \(msgShortMessage)\n\(msgLongMessage)" as NSString
    80         pEpMsg[kPepOutgoing] = true as AnyObject
    81 
    82         var encryptedDict = PEPMessage()
    83 
    84         if shouldEncrypt {
    85             let (status, encryptedDictOpt) = session.encrypt(pEpMessageDict: pEpMsg)
    86             XCTAssertEqual(status, PEP_STATUS_OK)
    87 
    88             guard
    89                 let theEncryptedDict = encryptedDictOpt as? PEPMessage,
    90                 let theAttachments = theEncryptedDict[kPepAttachments] as? NSArray else {
    91                     XCTFail()
    92                     return
    93             }
    94             XCTAssertEqual(theAttachments.count, 2)
    95 
    96             encryptedDict = theEncryptedDict
    97         }
    98 
    99         guard let inboxName = cdInbox.name else {
   100             XCTFail()
   101             return
   102         }
   103 
   104         let pantMail = CWIMAPMessage(pEpMessage: encryptedDict, mailboxName: inboxName)
   105         guard
   106             let cdMsg = CdMessage.insertOrUpdate(
   107                 pantomimeMessage: pantMail, account: cdOwnAccount,
   108                 messageUpdate: CWMessageUpdate.newComplete()) else {
   109                     XCTFail()
   110                     return
   111         }
   112 
   113         cdMsg.parent = cdInbox
   114         cdMsg.bodyFetched = true
   115 
   116         XCTAssertTrue(cdMsg.bodyFetched)
   117         XCTAssertFalse(cdMsg.imap?.localFlags?.flagDeleted ?? true)
   118         XCTAssertEqual(cdMsg.pEpRating, PEPUtil.pEpRatingNone)
   119         if shouldEncrypt {
   120             XCTAssertTrue(cdMsg.isProbablyPGPMime())
   121         }
   122 
   123         Record.saveAndWait()
   124 
   125         let expectationDecryptHasRun = expectation(description: "expectationDecryptHasRun")
   126         let errorContainer = ErrorContainer()
   127         let decryptOp = DecryptMessagesOperation(parentName: #function,
   128                                                  errorContainer: errorContainer)
   129         decryptOp.completionBlock = {
   130             expectationDecryptHasRun.fulfill()
   131         }
   132         backgroundQueue.addOperation(decryptOp)
   133 
   134         waitForExpectations(timeout: TestUtil.waitTime, handler: { error in
   135             XCTAssertNil(error)
   136         })
   137 
   138         XCTAssertEqual(decryptOp.numberOfMessagesDecrypted, 1)
   139 
   140         Record.Context.default.refreshAllObjects()
   141         XCTAssertEqual(
   142             Int32(cdMsg.pEpRating),
   143             shouldEncrypt ? PEP_rating_trusted_and_anonymized.rawValue:
   144                 Int32(PEPUtil.pEpRatingNone))
   145         if shouldEncrypt {
   146             XCTAssertEqual(cdMsg.shortMessage, msgShortMessage)
   147             XCTAssertEqual(cdMsg.longMessage, msgLongMessage)
   148         }
   149 
   150         for header in [kXEncStatus, kXpEpVersion, kXKeylist] {
   151             let p = NSPredicate(format: "message = %@ and name = %@", cdMsg, header)
   152             let headerField = CdHeaderField.first(predicate: p)
   153             if shouldEncrypt {
   154                 XCTAssertNotNil(headerField)
   155             } else {
   156                 XCTAssertNil(headerField)
   157             }
   158         }
   159     }
   160 
   161     func testBasicDecryptionOfEncryptedMail() {
   162         testBasicDecryption(shouldEncrypt: true)
   163     }
   164 
   165     func testBasicDecryptionOfUnEncryptedMail() {
   166         testBasicDecryption(shouldEncrypt: false)
   167     }
   168 }