pEpForiOSTests/TCPConnectionTest.swift
author Xavier Algarra <xavier@pep-project.org>
Tue, 06 Jun 2017 07:49:23 +0200
changeset 2248 75013a586bfd
parent 790 c12fc22b38df
permissions -rw-r--r--
IOS-137 add multiple accounts option with the new login
dirk@254
     1
//
dirk@254
     2
//  TCPConnectionTest.swift
dirk@254
     3
//  pEpForiOS
dirk@254
     4
//
dirk@254
     5
//  Created by Dirk Zimmermann on 27/05/16.
dirk@254
     6
//  Copyright © 2016 p≡p Security S.A. All rights reserved.
dirk@254
     7
//
dirk@254
     8
dirk@254
     9
import XCTest
dirk@254
    10
dirk@254
    11
import pEpForiOS
dirk@254
    12
dirk@254
    13
class TCPConnectionTest: XCTestCase {
dirk@254
    14
    /**
dirk@254
    15
     Test for verifying there are no retention cycles in CWTCPConnection
dirk@254
    16
     */
dirk@254
    17
    func testBasicConnection() {
dirk@254
    18
        class ConnectionDelegate: CWConnectionDelegate {
dirk@254
    19
            weak var connection: CWTCPConnection?
dirk@255
    20
            var refCount: ReferenceCounter
dirk@254
    21
            var expConnected: XCTestExpectation?
dirk@254
    22
            var expReceivedEventRead: XCTestExpectation?
dirk@254
    23
            var expReceivedEventWrite: XCTestExpectation?
dirk@254
    24
            var expReceivedEventError: XCTestExpectation?
dirk@254
    25
dirk@255
    26
            init(connection: CWTCPConnection, refCount: ReferenceCounter) {
dirk@254
    27
                self.connection = connection
dirk@254
    28
                self.refCount = refCount
dirk@254
    29
                self.refCount.inc()
dirk@254
    30
            }
dirk@254
    31
dirk@784
    32
            @objc fileprivate func connectionEstablished() {
dirk@254
    33
                expConnected?.fulfill()
dirk@254
    34
            }
dirk@254
    35
dirk@784
    36
            @objc fileprivate func receivedEvent(
dirk@790
    37
                _ theData: UnsafeMutableRawPointer?, type theType: RunLoopEventType,
dirk@790
    38
                extra theExtra: UnsafeMutableRawPointer?, forMode theMode: String?) {
dirk@254
    39
                switch theType {
dirk@254
    40
                case .ET_RDESC:
dirk@254
    41
                    let length = 1024
dirk@784
    42
                    var buffer = [UInt8](repeating: 0, count: length)
dirk@254
    43
                    let count = connection?.read(&buffer, length: length)
dirk@790
    44
                    let s = NSString(bytes: buffer, length: count!,
dirk@790
    45
                                     encoding: String.Encoding.utf8.rawValue)
dirk@254
    46
                    print("read \(s)")
dirk@254
    47
                    expReceivedEventRead?.fulfill()
dirk@254
    48
                case .ET_WDESC:
dirk@254
    49
                    expReceivedEventWrite?.fulfill()
dirk@254
    50
                    print("can write")
dirk@254
    51
                case .ET_EDESC:
dirk@254
    52
                    expReceivedEventError?.fulfill()
dirk@254
    53
                }
dirk@254
    54
            }
dirk@254
    55
dirk@254
    56
            deinit {
dirk@254
    57
                refCount.dec()
dirk@254
    58
            }
dirk@254
    59
        }
dirk@255
    60
        let refCount = ReferenceCounter()
dirk@254
    61
        for _ in 1...1 {
dirk@254
    62
            let connectInfo = TestData.connectInfo
dirk@254
    63
            var connection: CWTCPConnection? = CWTCPConnection.init(
dirk@254
    64
                name: connectInfo.imapServerName, port: UInt32(connectInfo.imapServerPort),
dirk@254
    65
                transport: connectInfo.imapTransport, background: true)
dirk@254
    66
            let delegate = ConnectionDelegate.init(connection: connection!, refCount: refCount)
dirk@790
    67
            delegate.expConnected = expectation(description: "connected")
dirk@790
    68
            delegate.expReceivedEventRead = expectation(description: "read")
dirk@790
    69
            delegate.expReceivedEventWrite = expectation(description: "write")
dirk@254
    70
            connection?.delegate = delegate
dirk@254
    71
            connection?.connect()
dirk@784
    72
            waitForExpectations(timeout: TestUtil.waitTime, handler: { error in
dirk@254
    73
                XCTAssertNil(error)
dirk@254
    74
                connection = nil
dirk@254
    75
            })
dirk@254
    76
        }
dirk@255
    77
        XCTAssertEqual(refCount.refCount, 0)
dirk@254
    78
    }
dirk@784
    79
}