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