pEpForiOSTests/GrandOperatorVerifyTests.swift
author Xavier Algarra <xavier@pep-project.org>
Tue, 06 Jun 2017 07:49:23 +0200
changeset 2248 75013a586bfd
parent 819 1e2af6f3d05b
permissions -rw-r--r--
IOS-137 add multiple accounts option with the new login
dirk@244
     1
//
dirk@244
     2
//  GrandOperatorVerifyTests.swift
dirk@244
     3
//  pEpForiOS
dirk@244
     4
//
dirk@244
     5
//  Created by Dirk Zimmermann on 26/04/16.
dirk@244
     6
//  Copyright © 2016 p≡p Security S.A. All rights reserved.
dirk@244
     7
//
dirk@244
     8
dirk@244
     9
import XCTest
dirk@244
    10
import CoreData
dirk@244
    11
dirk@244
    12
import pEpForiOS
dirk@244
    13
dirk@244
    14
/**
dirk@244
    15
 A collection of connection verification tests, where you can rely on all connections being
dirk@244
    16
 closed in the end (due to the one-way nature of testing a connection).
dirk@244
    17
 */
dirk@244
    18
class GrandOperatorVerifyTests: XCTestCase {
dirk@244
    19
    let comp = "GrandOperatorVerifyTests"
dirk@244
    20
dirk@244
    21
    let correct = TestData.connectInfo
dirk@244
    22
    var persistentSetup: PersistentSetup!
dirk@244
    23
dirk@244
    24
    override func setUp() {
dirk@244
    25
        super.setUp()
dirk@244
    26
        persistentSetup = PersistentSetup.init()
dirk@262
    27
    }
dirk@262
    28
dirk@262
    29
    override func tearDown() {
dirk@262
    30
        persistentSetup = nil
dirk@262
    31
        super.tearDown()
dirk@262
    32
    }
dirk@262
    33
dirk@262
    34
    /**
dirk@262
    35
     Verifies that background operations get cleaned up after execution in a background queue.
dirk@262
    36
     */
dirk@262
    37
    func testSimpleBackgroundOperation() {
dirk@784
    38
        class BackgroundOp: Operation {
dirk@262
    39
            let expRun: XCTestExpectation
dirk@262
    40
            let expDeinit: XCTestExpectation
dirk@262
    41
dirk@262
    42
            init(expRun: XCTestExpectation, expDeinit: XCTestExpectation) {
dirk@262
    43
                self.expRun = expRun
dirk@262
    44
                self.expDeinit = expDeinit
dirk@262
    45
            }
dirk@262
    46
dirk@262
    47
            override func main() {
dirk@262
    48
                expRun.fulfill()
dirk@262
    49
            }
dirk@262
    50
dirk@262
    51
            deinit {
dirk@262
    52
                expDeinit.fulfill()
dirk@262
    53
            }
dirk@262
    54
        }
dirk@784
    55
        let expRun = expectation(description: "run")
dirk@784
    56
        let expDeinit = expectation(description: "deinit")
dirk@262
    57
dirk@784
    58
        let queue = OperationQueue.init()
dirk@262
    59
        queue.addOperation(BackgroundOp.init(expRun: expRun, expDeinit: expDeinit))
dirk@262
    60
dirk@784
    61
        waitForExpectations(timeout: TestUtil.waitTime, handler: { error in
dirk@262
    62
            XCTAssertNil(error)
dirk@262
    63
        })
dirk@244
    64
    }
dirk@244
    65
dirk@244
    66
    func testVerifyConnectionAllFailed() {
dirk@784
    67
        let exp = expectation(description: "verified")
dirk@819
    68
        let connectionInfo = ImapSmtpConnectInfo.init(
dirk@364
    69
            nameOfTheUser: "The User",
dirk@244
    70
            email: "none", imapPassword: "none",
dirk@784
    71
            imapServerName: "cant.connect", imapServerPort: 993, imapTransport: .plain,
dirk@244
    72
            smtpServerName: "cant.connect", smtpServerPort: 516, smtpTransport: .TLS)
dirk@244
    73
        persistentSetup.grandOperator.verifyConnection(connectionInfo, completionBlock: { error in
dirk@244
    74
            XCTAssertNotNil(error)
dirk@784
    75
            XCTAssertTrue(error?.code == Constants.NetworkError.timeout.rawValue)
dirk@244
    76
            exp.fulfill()
dirk@244
    77
        })
dirk@784
    78
        waitForExpectations(timeout: TestUtil.waitTime, handler: { error in
dirk@244
    79
            XCTAssertNil(error)
dirk@244
    80
        })
dirk@244
    81
    }
dirk@244
    82
dirk@244
    83
    func testVerifyConnectionImapAuthenticationFailed() {
dirk@784
    84
        let exp = expectation(description: "verified")
dirk@819
    85
        let connectionInfo = ImapSmtpConnectInfo.init(
dirk@364
    86
            nameOfTheUser: "The User",
dirk@244
    87
            email: correct.email, imapPassword: "notwork",
dirk@244
    88
            imapServerName: correct.imapServerName, imapServerPort: correct.imapServerPort,
dirk@244
    89
            imapTransport: correct.imapTransport,
dirk@244
    90
            smtpServerName: correct.smtpServerName, smtpServerPort: correct.smtpServerPort,
dirk@244
    91
            smtpTransport: correct.smtpTransport)
dirk@244
    92
        persistentSetup.grandOperator.verifyConnection(connectionInfo, completionBlock: { error in
dirk@244
    93
            XCTAssertNotNil(error)
dirk@784
    94
            XCTAssertEqual(error!.code, Constants.NetworkError.authenticationFailed.rawValue)
dirk@244
    95
            exp.fulfill()
dirk@244
    96
        })
dirk@784
    97
        waitForExpectations(timeout: TestUtil.waitTime, handler: { error in
dirk@244
    98
            XCTAssertNil(error)
dirk@244
    99
        })
dirk@244
   100
    }
dirk@244
   101
dirk@244
   102
    func testVerifyConnectionSmtpAuthenticationFailed() {
dirk@784
   103
        let exp = expectation(description: "verified")
dirk@244
   104
        var connectionInfo = correct
dirk@244
   105
        connectionInfo.smtpPassword = "WRONG!"
dirk@244
   106
        persistentSetup.grandOperator.verifyConnection(connectionInfo, completionBlock: { error in
dirk@244
   107
            XCTAssertNotNil(error)
dirk@244
   108
            exp.fulfill()
dirk@244
   109
        })
dirk@784
   110
        waitForExpectations(timeout: TestUtil.waitTime, handler: { error in
dirk@244
   111
            XCTAssertNil(error)
dirk@244
   112
        })
dirk@244
   113
    }
dirk@244
   114
dirk@244
   115
    func testVerifyConnectionImapConnectionFailed() {
dirk@784
   116
        let exp = expectation(description: "verifyIMAPFailed")
dirk@244
   117
        var connectionInfo = correct
dirk@244
   118
        connectionInfo.imapServerName = "noconnect"
dirk@244
   119
        persistentSetup.grandOperator.verifyConnection(connectionInfo, completionBlock: { error in
dirk@244
   120
            XCTAssertNotNil(error)
dirk@244
   121
            exp.fulfill()
dirk@244
   122
        })
dirk@784
   123
        waitForExpectations(timeout: TestUtil.waitTime, handler: { error in
dirk@244
   124
            XCTAssertNil(error)
dirk@244
   125
        })
dirk@244
   126
    }
dirk@244
   127
dirk@244
   128
    func testVerifyConnectionSmtpConnectionFailed() {
dirk@784
   129
        let exp = expectation(description: "verifySMTPFailed")
dirk@244
   130
        var connectionInfo = correct
dirk@244
   131
        connectionInfo.smtpServerName = "noconnect"
dirk@244
   132
        persistentSetup.grandOperator.verifyConnection(connectionInfo, completionBlock: { error in
dirk@244
   133
            XCTAssertNotNil(error)
dirk@244
   134
            exp.fulfill()
dirk@244
   135
        })
dirk@784
   136
        waitForExpectations(timeout: TestUtil.waitTime, handler: { error in
dirk@244
   137
            XCTAssertNil(error)
dirk@244
   138
        })
dirk@244
   139
    }
dirk@244
   140
dirk@244
   141
    func testVerifyConnectionOk() {
dirk@784
   142
        let exp = expectation(description: "verifyConnectionOK")
dirk@244
   143
        persistentSetup.grandOperator.verifyConnection(correct, completionBlock: { error in
dirk@244
   144
            XCTAssertNil(error)
dirk@244
   145
            exp.fulfill()
dirk@244
   146
        })
dirk@784
   147
        waitForExpectations(timeout: TestUtil.waitTime, handler: { error in
dirk@244
   148
            XCTAssertNil(error)
dirk@244
   149
        })
dirk@244
   150
    }
dirk@244
   151
}