Chris Simpson | a9b6c70 | 2018-04-08 07:11:37 -0400 | [diff] [blame^] | 1 | // |
| 2 | // TBinaryProtocolTests.swift |
| 3 | // Thrift |
| 4 | // |
| 5 | // Created by Christopher Simpson on 8/18/16. |
| 6 | // |
| 7 | // |
| 8 | |
| 9 | import XCTest |
| 10 | import Foundation |
| 11 | @testable import Thrift |
| 12 | |
| 13 | |
| 14 | /// Testing Binary protocol read/write against itself |
| 15 | /// Uses separate read/write transport/protocols |
| 16 | class TBinaryProtocolTests: XCTestCase { |
| 17 | var transport: TMemoryBufferTransport = TMemoryBufferTransport(flushHandler: { |
| 18 | $0.reset(readBuffer: $1) |
| 19 | }) |
| 20 | |
| 21 | var proto: TBinaryProtocol! |
| 22 | |
| 23 | override func setUp() { |
| 24 | super.setUp() |
| 25 | proto = TBinaryProtocol(on: transport) |
| 26 | transport.reset() |
| 27 | } |
| 28 | |
| 29 | override func tearDown() { |
| 30 | super.tearDown() |
| 31 | transport.reset() |
| 32 | } |
| 33 | |
| 34 | func testInt8WriteRead() { |
| 35 | let writeVal: UInt8 = 250 |
| 36 | try? proto.write(writeVal) |
| 37 | try? transport.flush() |
| 38 | |
| 39 | let readVal: UInt8 = (try? proto.read()) ?? 0 |
| 40 | XCTAssertEqual(writeVal, readVal, "Error with UInt8, wrote \(writeVal) but read \(readVal)") |
| 41 | } |
| 42 | |
| 43 | func testInt16WriteRead() { |
| 44 | |
| 45 | let writeVal: Int16 = 12312 |
| 46 | try? proto.write(writeVal) |
| 47 | try? transport.flush() |
| 48 | let readVal: Int16 = (try? proto.read()) ?? 0 |
| 49 | XCTAssertEqual(writeVal, readVal, "Error with Int16, wrote \(writeVal) but read \(readVal)") |
| 50 | } |
| 51 | |
| 52 | func testInt32WriteRead() { |
| 53 | let writeVal: Int32 = 2029234 |
| 54 | try? proto.write(writeVal) |
| 55 | try? transport.flush() |
| 56 | |
| 57 | let readVal: Int32 = (try? proto.read()) ?? 0 |
| 58 | XCTAssertEqual(writeVal, readVal, "Error with Int32, wrote \(writeVal) but read \(readVal)") |
| 59 | } |
| 60 | |
| 61 | func testInt64WriteRead() { |
| 62 | let writeVal: Int64 = 234234981374134 |
| 63 | try? proto.write(writeVal) |
| 64 | try? transport.flush() |
| 65 | |
| 66 | let readVal: Int64 = (try? proto.read()) ?? 0 |
| 67 | XCTAssertEqual(writeVal, readVal, "Error with Int64, wrote \(writeVal) but read \(readVal)") |
| 68 | } |
| 69 | |
| 70 | func testDoubleWriteRead() { |
| 71 | let writeVal: Double = 3.1415926 |
| 72 | try? proto.write(writeVal) |
| 73 | try? transport.flush() |
| 74 | |
| 75 | let readVal: Double = (try? proto.read()) ?? 0.0 |
| 76 | XCTAssertEqual(writeVal, readVal, "Error with Double, wrote \(writeVal) but read \(readVal)") |
| 77 | } |
| 78 | |
| 79 | func testBoolWriteRead() { |
| 80 | let writeVal: Bool = true |
| 81 | try? proto.write(writeVal) |
| 82 | try? transport.flush() |
| 83 | |
| 84 | let readVal: Bool = (try? proto.read()) ?? false |
| 85 | XCTAssertEqual(writeVal, readVal, "Error with Bool, wrote \(writeVal) but read \(readVal)") |
| 86 | } |
| 87 | |
| 88 | func testStringWriteRead() { |
| 89 | let writeVal: String = "Hello World" |
| 90 | try? proto.write(writeVal) |
| 91 | try? transport.flush() |
| 92 | |
| 93 | let readVal: String! |
| 94 | do { |
| 95 | readVal = try proto.read() |
| 96 | } catch let error { |
| 97 | XCTAssertFalse(true, "Error reading \(error)") |
| 98 | return |
| 99 | } |
| 100 | |
| 101 | XCTAssertEqual(writeVal, readVal, "Error with String, wrote \(writeVal) but read \(readVal)") |
| 102 | } |
| 103 | |
| 104 | func testDataWriteRead() { |
| 105 | let writeVal: Data = "Data World".data(using: .utf8)! |
| 106 | try? proto.write(writeVal) |
| 107 | try? transport.flush() |
| 108 | |
| 109 | let readVal: Data = (try? proto.read()) ?? "Goodbye World".data(using: .utf8)! |
| 110 | XCTAssertEqual(writeVal, readVal, "Error with Data, wrote \(writeVal) but read \(readVal)") |
| 111 | } |
| 112 | |
| 113 | func testStructWriteRead() { |
| 114 | let msg = "Test Protocol Error" |
| 115 | let writeVal = TApplicationError(error: .protocolError, message: msg) |
| 116 | do { |
| 117 | try writeVal.write(to: proto) |
| 118 | try? transport.flush() |
| 119 | |
| 120 | } catch let error { |
| 121 | XCTAssertFalse(true, "Caught Error attempting to write \(error)") |
| 122 | } |
| 123 | |
| 124 | do { |
| 125 | let readVal = try TApplicationError.read(from: proto) |
| 126 | XCTAssertEqual(readVal.error.thriftErrorCode, writeVal.error.thriftErrorCode, "Error case mismatch, expected \(readVal.error) got \(writeVal.error)") |
| 127 | XCTAssertEqual(readVal.message, writeVal.message, "Error message mismatch, expected \(readVal.message) got \(writeVal.message)") |
| 128 | } catch let error { |
| 129 | XCTAssertFalse(true, "Caught Error attempting to read \(error)") |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | static var allTests : [(String, (TBinaryProtocolTests) -> () throws -> Void)] { |
| 134 | return [ |
| 135 | ("testInt8WriteRead", testInt8WriteRead), |
| 136 | ("testInt16WriteRead", testInt16WriteRead), |
| 137 | ("testInt32WriteRead", testInt32WriteRead), |
| 138 | ("testInt64WriteRead", testInt64WriteRead), |
| 139 | ("testDoubleWriteRead", testDoubleWriteRead), |
| 140 | ("testBoolWriteRead", testBoolWriteRead), |
| 141 | ("testStringWriteRead", testStringWriteRead), |
| 142 | ("testDataWriteRead", testDataWriteRead), |
| 143 | ("testStructWriteRead", testStructWriteRead) |
| 144 | ] |
| 145 | } |
| 146 | } |