|
| 1 | +/** |
| 2 | + * Copyright IBM Corporation 2018 |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + **/ |
| 16 | + |
| 17 | +import XCTest |
| 18 | +@testable import SwiftSMTP |
| 19 | + |
| 20 | +class TestTLSMode: XCTestCase { |
| 21 | + static var allTests = [ |
| 22 | + ("testNormal", testNormal), |
| 23 | + ("testIgnoreTLS", testIgnoreTLS), |
| 24 | + ("testRequireTLS", testRequireTLS), |
| 25 | + ("testRequireSTARTTLS", testRequireSTARTTLS) |
| 26 | + ] |
| 27 | + |
| 28 | + func testNormal() { |
| 29 | + let expectation = self.expectation(description: #function) |
| 30 | + defer { waitForExpectations(timeout: testDuration) } |
| 31 | + |
| 32 | + do { |
| 33 | + _ = try SMTPSocket( |
| 34 | + hostname: hostname, |
| 35 | + email: email, |
| 36 | + password: password, |
| 37 | + port: port, |
| 38 | + tlsMode: .normal, |
| 39 | + tlsConfiguration: nil, |
| 40 | + authMethods: authMethods, |
| 41 | + domainName: domainName, |
| 42 | + timeout: timeout |
| 43 | + ) |
| 44 | + expectation.fulfill() |
| 45 | + } catch { |
| 46 | + XCTFail(String(describing: error)) |
| 47 | + expectation.fulfill() |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + func testIgnoreTLS() { |
| 52 | + let expectation = self.expectation(description: #function) |
| 53 | + defer { waitForExpectations(timeout: testDuration) } |
| 54 | + |
| 55 | + do { |
| 56 | + _ = try SMTPSocket( |
| 57 | + hostname: hostname, |
| 58 | + email: email, |
| 59 | + password: password, |
| 60 | + port: port, |
| 61 | + tlsMode: .ignoreTLS, |
| 62 | + tlsConfiguration: nil, |
| 63 | + authMethods: authMethods, |
| 64 | + domainName: domainName, |
| 65 | + timeout: timeout |
| 66 | + ) |
| 67 | + XCTFail() |
| 68 | + expectation.fulfill() |
| 69 | + } catch { |
| 70 | + if case SMTPError.noAuthMethodsOrRequiresTLS = error { |
| 71 | + expectation.fulfill() |
| 72 | + } else { |
| 73 | + XCTFail(String(describing: error)) |
| 74 | + expectation.fulfill() |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + func testRequireTLS() { |
| 80 | + let expectation = self.expectation(description: #function) |
| 81 | + defer { waitForExpectations(timeout: testDuration) } |
| 82 | + |
| 83 | + do { |
| 84 | + _ = try SMTPSocket( |
| 85 | + hostname: hostname, |
| 86 | + email: email, |
| 87 | + password: password, |
| 88 | + port: 465, |
| 89 | + tlsMode: .requireTLS, |
| 90 | + tlsConfiguration: nil, |
| 91 | + authMethods: authMethods, |
| 92 | + domainName: domainName, |
| 93 | + timeout: timeout |
| 94 | + ) |
| 95 | + expectation.fulfill() |
| 96 | + } catch { |
| 97 | + XCTFail(String(describing: error)) |
| 98 | + expectation.fulfill() |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + func testRequireSTARTTLS() { |
| 103 | + let expectation = self.expectation(description: #function) |
| 104 | + defer { waitForExpectations(timeout: testDuration) } |
| 105 | + |
| 106 | + do { |
| 107 | + _ = try SMTPSocket( |
| 108 | + hostname: hostname, |
| 109 | + email: email, |
| 110 | + password: password, |
| 111 | + port: port, |
| 112 | + tlsMode: .requireSTARTTLS, |
| 113 | + tlsConfiguration: nil, |
| 114 | + authMethods: authMethods, |
| 115 | + domainName: domainName, |
| 116 | + timeout: timeout |
| 117 | + ) |
| 118 | + expectation.fulfill() |
| 119 | + } catch { |
| 120 | + XCTFail(String(describing: error)) |
| 121 | + expectation.fulfill() |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments