Jano Svitok | a082592 | 2020-03-06 08:44:10 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | |
| 20 | import XCTest |
| 21 | import Foundation |
| 22 | @testable import Thrift |
| 23 | |
| 24 | /// Testig TFramedTransport |
| 25 | /// |
| 26 | class TFramedTransportTests: XCTestCase { |
| 27 | var underlyingTransport: TMemoryBufferTransport = TMemoryBufferTransport(flushHandler: { |
| 28 | $0.reset(readBuffer: $1) |
| 29 | }) |
| 30 | |
| 31 | var proto: TBinaryProtocol! |
| 32 | var transport: TFramedTransport! |
| 33 | |
| 34 | override func setUp() { |
| 35 | super.setUp() |
| 36 | transport = TFramedTransport(transport:underlyingTransport) |
| 37 | proto = TBinaryProtocol(on: transport) |
| 38 | underlyingTransport.reset() |
| 39 | } |
| 40 | |
| 41 | override func tearDown() { |
| 42 | super.tearDown() |
| 43 | underlyingTransport.reset() |
| 44 | } |
| 45 | func testInt8WriteRead() { |
| 46 | let writeVal: UInt8 = 250 |
| 47 | try? proto.write(writeVal) |
| 48 | try? transport.flush() |
| 49 | |
| 50 | let readVal: UInt8 = (try? proto.read()) ?? 0 |
| 51 | XCTAssertEqual(writeVal, readVal, "Error with UInt8, wrote \(writeVal) but read \(readVal)") |
| 52 | } |
| 53 | |
| 54 | func testInt16WriteRead() { |
| 55 | |
| 56 | let writeVal: Int16 = 12312 |
| 57 | try? proto.write(writeVal) |
| 58 | try? transport.flush() |
| 59 | |
| 60 | let readVal: Int16 = (try? proto.read()) ?? 0 |
| 61 | XCTAssertEqual(writeVal, readVal, "Error with Int16, wrote \(writeVal) but read \(readVal)") |
| 62 | } |
| 63 | |
| 64 | func testInt32WriteRead() { |
| 65 | let writeVal: Int32 = 2029234 |
| 66 | try? proto.write(writeVal) |
| 67 | try? transport.flush() |
| 68 | |
| 69 | let readVal: Int32 = (try? proto.read()) ?? 0 |
| 70 | XCTAssertEqual(writeVal, readVal, "Error with Int32, wrote \(writeVal) but read \(readVal)") |
| 71 | } |
| 72 | |
| 73 | func testInt64WriteRead() { |
| 74 | let writeVal: Int64 = 234234981374134 |
| 75 | try? proto.write(writeVal) |
| 76 | try? transport.flush() |
| 77 | |
| 78 | let readVal: Int64 = (try? proto.read()) ?? 0 |
| 79 | XCTAssertEqual(writeVal, readVal, "Error with Int64, wrote \(writeVal) but read \(readVal)") |
| 80 | } |
| 81 | |
| 82 | func testDoubleWriteRead() { |
| 83 | let writeVal: Double = 3.1415926 |
| 84 | try? proto.write(writeVal) |
| 85 | try? transport.flush() |
| 86 | |
| 87 | let readVal: Double = (try? proto.read()) ?? 0.0 |
| 88 | XCTAssertEqual(writeVal, readVal, "Error with Double, wrote \(writeVal) but read \(readVal)") |
| 89 | } |
| 90 | |
| 91 | func testBoolWriteRead() { |
| 92 | let writeVal: Bool = true |
| 93 | try? proto.write(writeVal) |
| 94 | try? transport.flush() |
| 95 | |
| 96 | let readVal: Bool = (try? proto.read()) ?? false |
| 97 | XCTAssertEqual(writeVal, readVal, "Error with Bool, wrote \(writeVal) but read \(readVal)") |
| 98 | } |
| 99 | |
| 100 | func testStringWriteRead() { |
| 101 | let writeVal: String = "Hello World" |
| 102 | try? proto.write(writeVal) |
| 103 | try? transport.flush() |
| 104 | |
| 105 | let readVal: String! |
| 106 | do { |
| 107 | readVal = try proto.read() |
| 108 | } catch let error { |
| 109 | XCTAssertFalse(true, "Error reading \(error)") |
| 110 | return |
| 111 | } |
| 112 | |
| 113 | XCTAssertEqual(writeVal, readVal, "Error with String, wrote \(writeVal) but read \(readVal)") |
| 114 | } |
| 115 | |
| 116 | func testDataWriteRead() { |
| 117 | let writeVal: Data = "Data World".data(using: .utf8)! |
| 118 | try? proto.write(writeVal) |
| 119 | try? transport.flush() |
| 120 | |
| 121 | let readVal: Data = (try? proto.read()) ?? "Goodbye World".data(using: .utf8)! |
| 122 | XCTAssertEqual(writeVal, readVal, "Error with Data, wrote \(writeVal) but read \(readVal)") |
| 123 | } |
| 124 | |
| 125 | func testStructWriteRead() { |
| 126 | let msg = "Test Protocol Error" |
| 127 | let writeVal = TApplicationError(error: .protocolError, message: msg) |
| 128 | do { |
| 129 | try writeVal.write(to: proto) |
| 130 | try? transport.flush() |
| 131 | } catch let error { |
| 132 | XCTAssertFalse(true, "Caught Error attempting to write \(error)") |
| 133 | } |
| 134 | |
| 135 | do { |
| 136 | let readVal = try TApplicationError.read(from: proto) |
| 137 | XCTAssertEqual(readVal.error.thriftErrorCode, writeVal.error.thriftErrorCode, "Error case mismatch, expected \(readVal.error) got \(writeVal.error)") |
| 138 | XCTAssertEqual(readVal.message, writeVal.message, "Error message mismatch, expected \(readVal.message) got \(writeVal.message)") |
| 139 | } catch let error { |
| 140 | XCTAssertFalse(true, "Caught Error attempting to read \(error)") |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | func testUnsafeBitcastUpdate() { |
| 145 | let value: Double = 3.14159 |
| 146 | let val: Int64 = 31415926 |
| 147 | let uval: UInt64 = 31415926 |
| 148 | |
| 149 | let i64 = Int64(bitPattern: value.bitPattern) |
| 150 | let ubc = unsafeBitCast(value, to: Int64.self) |
| 151 | |
| 152 | XCTAssertEqual(i64, ubc, "Bitcast Double-> i64 Values don't match") |
| 153 | |
| 154 | let dbl = Double(bitPattern: UInt64(val)) |
| 155 | let ubdb = unsafeBitCast(val, to: Double.self) |
| 156 | |
| 157 | XCTAssertEqual(dbl, ubdb, "Bitcast i64 -> Double Values don't match") |
| 158 | |
| 159 | let db2 = Double(bitPattern: uval) |
| 160 | let usbc2 = unsafeBitCast(uval, to: Double.self) |
| 161 | |
| 162 | XCTAssertEqual(db2, usbc2, "Bitcast u64 -> Double Values don't match") |
| 163 | } |
| 164 | |
| 165 | static var allTests : [(String, (TFramedTransportTests) -> () throws -> Void)] { |
| 166 | return [ |
| 167 | ("testInt8WriteRead", testInt8WriteRead), |
| 168 | ("testInt16WriteRead", testInt16WriteRead), |
| 169 | ("testInt32WriteRead", testInt32WriteRead), |
| 170 | ("testInt64WriteRead", testInt64WriteRead), |
| 171 | ("testDoubleWriteRead", testDoubleWriteRead), |
| 172 | ("testBoolWriteRead", testBoolWriteRead), |
| 173 | ("testStringWriteRead", testStringWriteRead), |
| 174 | ("testDataWriteRead", testDataWriteRead), |
| 175 | ("testStructWriteRead", testStructWriteRead), |
| 176 | ("testUnsafeBitcastUpdate", testUnsafeBitcastUpdate) |
| 177 | ] |
| 178 | } |
| 179 | } |