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