blob: 56c33acfb00f4aa4ce35e0a8664e5198ed42a1a0 [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// TCompactProtocolTests.swift
22// Thrift
23//
24// Created by Christopher Simpson on 8/19/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 TCompactProtocolTests: XCTestCase {
36 var transport: TMemoryBufferTransport = TMemoryBufferTransport(flushHandler: {
37 $0.reset(readBuffer: $1)
38 })
39 var proto: TCompactProtocol!
40
41 override func setUp() {
42 super.setUp()
43 proto = TCompactProtocol(on: transport)
44 transport.reset()
45 }
46
47 override func tearDown() {
48 super.tearDown()
49 transport.reset()
50 }
51
52 func testInt8WriteRead() {
53 let writeVal: UInt8 = 250
54 try? proto.write(writeVal)
55 try? transport.flush()
56 let readVal: UInt8 = (try? proto.read()) ?? 0
57 XCTAssertEqual(writeVal, readVal, "Error with UInt8, wrote \(writeVal) but read \(readVal)")
58 }
59
60 func testInt16WriteRead() {
61 let writeVal: Int16 = 12312
62 try? proto.write(writeVal)
63 try? transport.flush()
64
65 let readVal: Int16 = (try? proto.read()) ?? 0
66 XCTAssertEqual(writeVal, readVal, "Error with Int16, wrote \(writeVal) but read \(readVal)")
67 }
68
69 func testInt32WriteRead() {
70 let writeVal: Int32 = 2029234
71 try? proto.write(writeVal)
72 try? transport.flush()
73
74 let readVal: Int32 = (try? proto.read()) ?? 0
75 XCTAssertEqual(writeVal, readVal, "Error with Int32, wrote \(writeVal) but read \(readVal)")
76 }
77
78 func testInt64WriteRead() {
79 let writeVal: Int64 = 234234981374134
80 try? proto.write(writeVal)
81 try? transport.flush()
82
83 let readVal: Int64 = (try? proto.read()) ?? 0
84 XCTAssertEqual(writeVal, readVal, "Error with Int64, wrote \(writeVal) but read \(readVal)")
85 }
86
87 func testDoubleWriteRead() {
88 let writeVal: Double = 3.1415926
89 try? proto.write(writeVal)
90 try? transport.flush()
91
92 let readVal: Double = (try? proto.read()) ?? 0.0
93 XCTAssertEqual(writeVal, readVal, "Error with Double, wrote \(writeVal) but read \(readVal)")
94 }
95
96 func testBoolWriteRead() {
97 let writeVal: Bool = true
98 try? proto.write(writeVal)
99 try? transport.flush()
100
101 let readVal: Bool = (try? proto.read()) ?? false
102 XCTAssertEqual(writeVal, readVal, "Error with Bool, wrote \(writeVal) but read \(readVal)")
103 }
104
105 func testStringWriteRead() {
106 let writeVal: String = "Hello World"
107 try? proto.write(writeVal)
108 try? transport.flush()
109
110 let readVal: String!
111 do {
112 readVal = try proto.read()
113 } catch let error {
114 XCTAssertFalse(true, "Error reading \(error)")
115 return
116 }
117
118 XCTAssertEqual(writeVal, readVal, "Error with String, wrote \(writeVal) but read \(readVal)")
119 }
120
121 func testDataWriteRead() {
122 let writeVal: Data = "Data World".data(using: .utf8)!
123 try? proto.write(writeVal)
124 try? transport.flush()
125
126 let readVal: Data = (try? proto.read()) ?? "Goodbye World".data(using: .utf8)!
127 XCTAssertEqual(writeVal, readVal, "Error with Data, wrote \(writeVal) but read \(readVal)")
128 }
129
130 func testStructWriteRead() {
131 let msg = "Test Protocol Error"
132 let writeVal = TApplicationError(error: .protocolError, message: msg)
133 do {
134 try writeVal.write(to: proto)
135 try transport.flush()
136
137 } catch let error {
138 XCTAssertFalse(true, "Caught Error attempting to write \(error)")
139 }
140
141 do {
142 let readVal = try TApplicationError.read(from: proto)
143 XCTAssertEqual(readVal.error.thriftErrorCode, writeVal.error.thriftErrorCode, "Error case mismatch, expected \(readVal.error) got \(writeVal.error)")
144 XCTAssertEqual(readVal.message, writeVal.message, "Error message mismatch, expected \(readVal.message) got \(writeVal.message)")
145 } catch let error {
146 XCTAssertFalse(true, "Caught Error attempting to read \(error)")
147 }
148 }
149
Chris Simpson2566ecd2018-08-29 14:40:44 -0400150 func testInt32ZigZag() {
151 let zero: Int32 = 0
152 let one: Int32 = 1
153 let nOne: Int32 = -1
154 let two: Int32 = 2
155 let nTwo: Int32 = -2
156 let max = Int32.max
157 let min = Int32.min
158
159 XCTAssertEqual(proto.i32ToZigZag(zero), UInt32(0), "Error 32bit zigzag on \(zero)")
160 XCTAssertEqual(proto.zigZagToi32(0), zero, "Error 32bit zigzag on \(zero)")
161
162 XCTAssertEqual(proto.i32ToZigZag(nOne), UInt32(1), "Error 32bit zigzag on \(nOne)")
163 XCTAssertEqual(proto.zigZagToi32(1), nOne, "Error 32bit zigzag on \(nOne)")
164
165 XCTAssertEqual(proto.i32ToZigZag(one), UInt32(2), "Error 32bit zigzag on \(one)")
166 XCTAssertEqual(proto.zigZagToi32(2), one, "Error 32bit zigzag on \(one)")
167
168 XCTAssertEqual(proto.i32ToZigZag(nTwo), UInt32(3), "Error 32bit zigzag on \(nTwo)")
169 XCTAssertEqual(proto.zigZagToi32(3), nTwo, "Error 32bit zigzag on \(nTwo)")
170
171 XCTAssertEqual(proto.i32ToZigZag(two), UInt32(4), "Error 32bit zigzag on \(two)")
172 XCTAssertEqual(proto.zigZagToi32(4), two, "Error 32bit zigzag on \(two)")
173
174 let uMaxMinusOne: UInt32 = UInt32.max - 1
175 XCTAssertEqual(proto.i32ToZigZag(max), uMaxMinusOne, "Error 32bit zigzag on \(max)")
176 XCTAssertEqual(proto.zigZagToi32(uMaxMinusOne), max, "Error 32bit zigzag on \(max)")
177
178 XCTAssertEqual(proto.i32ToZigZag(min), UInt32.max, "Error 32bit zigzag on \(min)")
179 XCTAssertEqual(proto.zigZagToi32(UInt32.max), min, "Error 32bit zigzag on \(min)")
180 }
181
182 func testInt64ZigZag() {
183 let zero: Int64 = 0
184 let one: Int64 = 1
185 let nOne: Int64 = -1
186 let two: Int64 = 2
187 let nTwo: Int64 = -2
188 let max = Int64.max
189 let min = Int64.min
190
191 XCTAssertEqual(proto.i64ToZigZag(zero), UInt64(0), "Error 64bit zigzag on \(zero)")
192 XCTAssertEqual(proto.zigZagToi64(0), zero, "Error 64bit zigzag on \(zero)")
193
194 XCTAssertEqual(proto.i64ToZigZag(nOne), UInt64(1), "Error 64bit zigzag on \(nOne)")
195 XCTAssertEqual(proto.zigZagToi64(1), nOne, "Error 64bit zigzag on \(nOne)")
196
197 XCTAssertEqual(proto.i64ToZigZag(one), UInt64(2), "Error 64bit zigzag on \(one)")
198 XCTAssertEqual(proto.zigZagToi64(2), one, "Error 64bit zigzag on \(one)")
199
200 XCTAssertEqual(proto.i64ToZigZag(nTwo), UInt64(3), "Error 64bit zigzag on \(nTwo)")
201 XCTAssertEqual(proto.zigZagToi64(3), nTwo, "Error 64bit zigzag on \(nTwo)")
202
203 XCTAssertEqual(proto.i64ToZigZag(two), UInt64(4), "Error 64bit zigzag on \(two)")
204 XCTAssertEqual(proto.zigZagToi64(4), two, "Error 64bit zigzag on \(two)")
205
206 let uMaxMinusOne: UInt64 = UInt64.max - 1
207 XCTAssertEqual(proto.i64ToZigZag(max), uMaxMinusOne, "Error 64bit zigzag on \(max)")
208 XCTAssertEqual(proto.zigZagToi64(uMaxMinusOne), max, "Error 64bit zigzag on \(max)")
209
210 XCTAssertEqual(proto.i64ToZigZag(min), UInt64.max, "Error 64bit zigzag on \(min)")
211 XCTAssertEqual(proto.zigZagToi64(UInt64.max), min, "Error 64bit zigzag on \(min)")
212 }
213
Chris Simpsona9b6c702018-04-08 07:11:37 -0400214 static var allTests : [(String, (TCompactProtocolTests) -> () throws -> Void)] {
215 return [
216 ("testInt8WriteRead", testInt8WriteRead),
217 ("testInt16WriteRead", testInt16WriteRead),
218 ("testInt32WriteRead", testInt32WriteRead),
219 ("testInt64WriteRead", testInt64WriteRead),
220 ("testDoubleWriteRead", testDoubleWriteRead),
221 ("testBoolWriteRead", testBoolWriteRead),
222 ("testStringWriteRead", testStringWriteRead),
223 ("testDataWriteRead", testDataWriteRead),
Chris Simpson2566ecd2018-08-29 14:40:44 -0400224 ("testStructWriteRead", testStructWriteRead),
225 ("testInt32ZigZag", testInt32ZigZag),
226 ("testInt64ZigZag", testInt64ZigZag)
Chris Simpsona9b6c702018-04-08 07:11:37 -0400227 ]
228 }
229}