blob: 35ac3f880cb10a59f5d1ea4a75f48485b362fd45 [file] [log] [blame]
Jens Geyer72a714e2025-08-26 22:12:07 +02001/*
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
Chris Simpsona9b6c702018-04-08 07:11:37 -040020//
21// TBinaryProtocolTests.swift
22// Thrift
23//
24// Created by Christopher Simpson on 8/18/16.
25//
26//
27
28import XCTest
29import Foundation
30@testable import Thrift
31
32
33/// Testing Binary protocol read/write against itself
34/// Uses separate read/write transport/protocols
35class TBinaryProtocolTests: XCTestCase {
36 var transport: TMemoryBufferTransport = TMemoryBufferTransport(flushHandler: {
37 $0.reset(readBuffer: $1)
38 })
39
40 var proto: TBinaryProtocol!
41
42 override func setUp() {
43 super.setUp()
44 proto = TBinaryProtocol(on: transport)
45 transport.reset()
46 }
47
48 override func tearDown() {
49 super.tearDown()
50 transport.reset()
51 }
52
53 func testInt8WriteRead() {
54 let writeVal: UInt8 = 250
55 try? proto.write(writeVal)
56 try? transport.flush()
57
58 let readVal: UInt8 = (try? proto.read()) ?? 0
59 XCTAssertEqual(writeVal, readVal, "Error with UInt8, wrote \(writeVal) but read \(readVal)")
60 }
61
62 func testInt16WriteRead() {
63
64 let writeVal: Int16 = 12312
65 try? proto.write(writeVal)
66 try? transport.flush()
67 let readVal: Int16 = (try? proto.read()) ?? 0
68 XCTAssertEqual(writeVal, readVal, "Error with Int16, wrote \(writeVal) but read \(readVal)")
69 }
70
71 func testInt32WriteRead() {
72 let writeVal: Int32 = 2029234
73 try? proto.write(writeVal)
74 try? transport.flush()
75
76 let readVal: Int32 = (try? proto.read()) ?? 0
77 XCTAssertEqual(writeVal, readVal, "Error with Int32, wrote \(writeVal) but read \(readVal)")
78 }
79
80 func testInt64WriteRead() {
81 let writeVal: Int64 = 234234981374134
82 try? proto.write(writeVal)
83 try? transport.flush()
84
85 let readVal: Int64 = (try? proto.read()) ?? 0
86 XCTAssertEqual(writeVal, readVal, "Error with Int64, wrote \(writeVal) but read \(readVal)")
87 }
88
89 func testDoubleWriteRead() {
90 let writeVal: Double = 3.1415926
91 try? proto.write(writeVal)
92 try? transport.flush()
93
94 let readVal: Double = (try? proto.read()) ?? 0.0
95 XCTAssertEqual(writeVal, readVal, "Error with Double, wrote \(writeVal) but read \(readVal)")
96 }
97
98 func testBoolWriteRead() {
99 let writeVal: Bool = true
100 try? proto.write(writeVal)
101 try? transport.flush()
102
103 let readVal: Bool = (try? proto.read()) ?? false
104 XCTAssertEqual(writeVal, readVal, "Error with Bool, wrote \(writeVal) but read \(readVal)")
105 }
106
107 func testStringWriteRead() {
108 let writeVal: String = "Hello World"
109 try? proto.write(writeVal)
110 try? transport.flush()
111
112 let readVal: String!
113 do {
114 readVal = try proto.read()
115 } catch let error {
116 XCTAssertFalse(true, "Error reading \(error)")
117 return
118 }
119
120 XCTAssertEqual(writeVal, readVal, "Error with String, wrote \(writeVal) but read \(readVal)")
121 }
122
123 func testDataWriteRead() {
124 let writeVal: Data = "Data World".data(using: .utf8)!
125 try? proto.write(writeVal)
126 try? transport.flush()
127
128 let readVal: Data = (try? proto.read()) ?? "Goodbye World".data(using: .utf8)!
129 XCTAssertEqual(writeVal, readVal, "Error with Data, wrote \(writeVal) but read \(readVal)")
130 }
131
132 func testStructWriteRead() {
133 let msg = "Test Protocol Error"
134 let writeVal = TApplicationError(error: .protocolError, message: msg)
135 do {
136 try writeVal.write(to: proto)
137 try? transport.flush()
Chris Simpsona9b6c702018-04-08 07:11:37 -0400138 } catch let error {
139 XCTAssertFalse(true, "Caught Error attempting to write \(error)")
140 }
141
142 do {
143 let readVal = try TApplicationError.read(from: proto)
144 XCTAssertEqual(readVal.error.thriftErrorCode, writeVal.error.thriftErrorCode, "Error case mismatch, expected \(readVal.error) got \(writeVal.error)")
145 XCTAssertEqual(readVal.message, writeVal.message, "Error message mismatch, expected \(readVal.message) got \(writeVal.message)")
146 } catch let error {
147 XCTAssertFalse(true, "Caught Error attempting to read \(error)")
148 }
149 }
Jano Svitoka0825922020-03-06 08:44:10 +0100150
Chris Simpson2566ecd2018-08-29 14:40:44 -0400151 func testUnsafeBitcastUpdate() {
152 let value: Double = 3.14159
153 let val: Int64 = 31415926
154 let uval: UInt64 = 31415926
155
156 let i64 = Int64(bitPattern: value.bitPattern)
157 let ubc = unsafeBitCast(value, to: Int64.self)
158
159 XCTAssertEqual(i64, ubc, "Bitcast Double-> i64 Values don't match")
160
161 let dbl = Double(bitPattern: UInt64(val))
162 let ubdb = unsafeBitCast(val, to: Double.self)
163
164 XCTAssertEqual(dbl, ubdb, "Bitcast i64 -> Double Values don't match")
165
166 let db2 = Double(bitPattern: uval)
167 let usbc2 = unsafeBitCast(uval, to: Double.self)
168
169 XCTAssertEqual(db2, usbc2, "Bitcast u64 -> Double Values don't match")
170
171
172 }
Chris Simpsona9b6c702018-04-08 07:11:37 -0400173
174 static var allTests : [(String, (TBinaryProtocolTests) -> () throws -> Void)] {
175 return [
176 ("testInt8WriteRead", testInt8WriteRead),
177 ("testInt16WriteRead", testInt16WriteRead),
178 ("testInt32WriteRead", testInt32WriteRead),
179 ("testInt64WriteRead", testInt64WriteRead),
180 ("testDoubleWriteRead", testDoubleWriteRead),
181 ("testBoolWriteRead", testBoolWriteRead),
182 ("testStringWriteRead", testStringWriteRead),
183 ("testDataWriteRead", testDataWriteRead),
Jano Svitoka0825922020-03-06 08:44:10 +0100184 ("testStructWriteRead", testStructWriteRead),
185 ("testUnsafeBitcastUpdate", testUnsafeBitcastUpdate)
Chris Simpsona9b6c702018-04-08 07:11:37 -0400186 ]
187 }
188}