blob: dc8b132551da67f19a77ce926bb077077a45bcb6 [file] [log] [blame]
HojjatK 💾a2a1f532023-06-13 10:32:56 -07001/*
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*/
HojjatK 💾d67e5c22023-06-05 19:17:26 -070019
20import XCTest
21import Foundation
22@testable import Thrift
23
24class TJSONProtocolTests: XCTestCase {
25 var transport: TMemoryBufferTransport = TMemoryBufferTransport(flushHandler: {
26 $0.reset(readBuffer: $1)
27 })
28
29 var proto: TJSONProtocol!
30
31 override func setUp() {
32 super.setUp()
33 proto = TJSONProtocol(on: transport)
34 transport.reset()
35 }
36
37 override func tearDown() {
38 super.tearDown()
39 transport.reset()
40 }
41
42 func testUInt8WriteRead() {
43 let writeVal: UInt8 = 250
44 try? proto.write(writeVal)
45 try? transport.flush()
46
47 let readVal: UInt8 = (try? proto.read()) ?? 0
48 XCTAssertEqual(writeVal, readVal, "Error with UInt8, wrote \(writeVal) but read \(readVal)")
49 }
50
51 func testInt8WriteRead() {
52 let writeVal: Int8 = -120
53 try? proto.write(writeVal)
54 try? transport.flush()
55
56 let readVal: Int8 = (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 let readVal: Int16 = (try? proto.read()) ?? 0
65 XCTAssertEqual(writeVal, readVal, "Error with Int16, wrote \(writeVal) but read \(readVal)")
66 }
67
68 func testInt32WriteRead() {
69 let writeVal: Int32 = 2029234
70 try? proto.write(writeVal)
71 try? transport.flush()
72
73 let readVal: Int32 = (try? proto.read()) ?? 0
74 XCTAssertEqual(writeVal, readVal, "Error with Int32, wrote \(writeVal) but read \(readVal)")
75 }
76
77 func testInt64WriteRead() {
78 let writeVal: Int64 = 234234981374134
79 try? proto.write(writeVal)
80 try? transport.flush()
81
82 let readVal: Int64 = (try? proto.read()) ?? 0
83 XCTAssertEqual(writeVal, readVal, "Error with Int64, wrote \(writeVal) but read \(readVal)")
84 }
85
86 func testDoubleWriteRead() {
87 let writeVal: Double = 3.1415926
88 try? proto.write(writeVal)
89 try? transport.flush()
90
91 let readVal: Double = (try? proto.read()) ?? 0.0
92 XCTAssertEqual(writeVal, readVal, "Error with Double, wrote \(writeVal) but read \(readVal)")
93 }
94
95 func testBoolWriteRead() {
96 let writeVal: Bool = true
97 try? proto.write(writeVal)
98 try? transport.flush()
99
100 let readVal: Bool = (try? proto.read()) ?? false
101 XCTAssertEqual(writeVal, readVal, "Error with Bool, wrote \(writeVal) but read \(readVal)")
102 }
103
104 func testStringWriteRead() {
105 let writeVal: String = "Hello World"
106 try? proto.write(writeVal)
107 try? transport.flush()
108
109 let readVal: String
110 do {
111 readVal = try proto.read()
112 } catch let error {
113 XCTAssertFalse(true, "Error reading \(error)")
114 return
115 }
116
117 XCTAssertEqual(writeVal, readVal, "Error with String, wrote \(writeVal) but read \(readVal)")
118 }
119
120 func testStringWriteRead2() {
121 let writeVal: String = "你好世界 means hello world!"
122 try? proto.write(writeVal)
123 try? transport.flush()
124
125 let readVal: String
126 do {
127 print(writeVal)
128 readVal = try proto.read()
129 print(readVal)
130 } catch let error {
131 XCTAssertFalse(true, "Error reading \(error)")
132 return
133 }
134
135 XCTAssertEqual(writeVal, readVal, "Error with String, wrote \(writeVal) but read \(readVal)")
136 }
137
138 func testDataWriteRead() {
139 let writeVal: Data = "Data World".data(using: .utf8)!
140 try? proto.write(writeVal)
141 try? transport.flush()
142
143 let readVal: Data = (try? proto.read()) ?? "Goodbye World".data(using: .utf8)!
144 XCTAssertEqual(writeVal, readVal, "Error with Data, wrote \(writeVal) but read \(readVal)")
145 }
146
147 func testUUIDWriteRead() {
148 let writeVal: UUID = UUID()
149 try? proto.write(writeVal)
150 try? transport.flush()
151
152 let newUuid = UUID()
153 let readVal: UUID = (try? proto.read()) ?? newUuid
154 XCTAssertEqual(writeVal, readVal, "Error with Data, wrote \(writeVal) but read \(readVal)")
155 }
156
157 func testStructWriteRead() {
158 let msg = "Test Protocol Error"
159 let writeVal = TApplicationError(error: .protocolError, message: msg)
160 do {
161 try writeVal.write(to: proto)
162 try? transport.flush()
163 } catch let error {
164 XCTAssertFalse(true, "Caught Error attempting to write \(error)")
165 }
166
167 do {
168 let readVal: TApplicationError = try TApplicationError.read(from: proto)
169 XCTAssertEqual(readVal.error.thriftErrorCode, writeVal.error.thriftErrorCode, "Error case mismatch, expected \(readVal.error) got \(writeVal.error)")
170 let readValMessage = readVal.message ?? "", writeValMessage = writeVal.message ?? ""
171 XCTAssertEqual(readVal.message, writeVal.message, "Error message mismatch, expected \(readValMessage) got \(writeValMessage)")
172 } catch let error {
173 XCTAssertFalse(true, "Caught Error attempting to read \(error)")
174 }
175 }
176
177 func testBase64WriteRead() {
178 let writeText = "!testing base64 read and write ..."
179 let writeData = writeText.data(using: .utf8)!
180 let writeVal = [UInt8](writeData)
181 try? proto.writeJsonBase64(bytes: writeVal)
182 try? transport.flush()
183
184 var data = Data()
185 if let readVal = try? proto.readJsonBase64() {
186 data = Data(bytes: readVal, count: readVal.count)
187 }
188 let readText = String(decoding: data, as: UTF8.self)
189 XCTAssertEqual(readText, writeText, "Error message mismatch, expected \(readText) got \(writeText)")
190 }
191
192 func testBase64WriteRead2() {
193 let writeText = "你好世界 means hello world!"
194 let writeData = writeText.data(using: .utf8)!
195 let writeVal = [UInt8](writeData)
196 try? proto.writeJsonBase64(bytes: writeVal)
197 try? transport.flush()
198
199 var data = Data()
200 if let readVal = try? proto.readJsonBase64() {
201 data = Data(bytes: readVal, count: readVal.count)
202 }
203 let readText = String(decoding: data, as: UTF8.self)
204 XCTAssertEqual(readText, writeText, "Error message mismatch, expected \(readText) got \(writeText)")
205 }
206
207 static var allTests : [(String, (TJSONProtocolTests) -> () throws -> Void)] {
208 return [
209 ("testUInt8WriteRead", testUInt8WriteRead),
210 ("testInt8WriteRead", testInt8WriteRead),
211 ("testInt16WriteRead", testInt16WriteRead),
212 ("testInt32WriteRead", testInt32WriteRead),
213 ("testInt64WriteRead", testInt64WriteRead),
214 ("testDoubleWriteRead", testDoubleWriteRead),
215 ("testBoolWriteRead", testBoolWriteRead),
216 ("testStringWriteRead", testStringWriteRead),
217 ("testStringWriteRead2", testStringWriteRead2),
218 ("testDataWriteRead", testDataWriteRead),
219 ("testUUIDWriteRead", testUUIDWriteRead),
220 ("testStructWriteRead", testStructWriteRead),
221 ("testBase64WriteRead", testBase64WriteRead),
222 ("testBase64WriteRead2", testBase64WriteRead2)
223 ]
224 }
225}