blob: ca54c8bf4c0a4c9c016ab8de4da9d5c2b964e211 [file] [log] [blame]
Roger Meier32f39822014-06-18 22:43:17 +02001#!/usr/bin/env ruby
Nobuaki Sukegawa9b35a7c2015-11-17 11:01:41 +09002# encoding: utf-8
Roger Meier32f39822014-06-18 22:43:17 +02003
David Reissea2cba82009-03-30 21:35:00 +00004#
5# Licensed to the Apache Software Foundation (ASF) under one
6# or more contributor license agreements. See the NOTICE file
7# distributed with this work for additional information
8# regarding copyright ownership. The ASF licenses this file
9# to you under the Apache License, Version 2.0 (the
10# "License"); you may not use this file except in compliance
11# with the License. You may obtain a copy of the License at
12#
13# http://www.apache.org/licenses/LICENSE-2.0
14#
15# Unless required by applicable law or agreed to in writing,
16# software distributed under the License is distributed on an
17# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18# KIND, either express or implied. See the License for the
19# specific language governing permissions and limitations
20# under the License.
21#
22
Roger Meierc95d5df2014-01-19 21:53:02 +010023$:.push File.dirname(__FILE__) + '/..'
Kevin Clark4bd89162008-07-08 00:47:49 +000024
Roger Meierc95d5df2014-01-19 21:53:02 +010025require 'test_helper'
Kevin Clark4bd89162008-07-08 00:47:49 +000026require 'thrift'
Roger Meierc95d5df2014-01-19 21:53:02 +010027require 'thrift_test'
Kevin Clark4bd89162008-07-08 00:47:49 +000028
James E. King III9aaf2952018-03-20 15:06:08 -040029$domain_socket = nil
Roger Meiera3570ac2014-06-10 22:16:14 +020030$host = "localhost"
31$port = 9090
James E. King III714c77c2018-03-20 19:58:38 -040032$protocolType = "binary"
33$ssl = false
Roger Meiera3570ac2014-06-10 22:16:14 +020034$transport = "buffered"
James E. King III9aaf2952018-03-20 15:06:08 -040035
Roger Meiera3570ac2014-06-10 22:16:14 +020036ARGV.each do|a|
37 if a == "--help"
38 puts "Allowed options:"
39 puts "\t -h [ --help ] \t produce help message"
James E. King III714c77c2018-03-20 19:58:38 -040040 puts "\t--domain-socket arg (=) \t Unix domain socket path"
41 puts "\t--host arg (=localhost) \t Host to connect \t not valid with domain-socket"
42 puts "\t--port arg (=9090) \t Port number to listen \t not valid with domain-socket"
Dmytro Shteflyuk67bfb292026-01-28 11:23:50 -050043 puts "\t--protocol arg (=binary) \t protocol: accel, binary, compact, json, header"
James E. King III714c77c2018-03-20 19:58:38 -040044 puts "\t--ssl \t use ssl \t not valid with domain-socket"
Dmytro Shteflyuk67bfb292026-01-28 11:23:50 -050045 puts "\t--transport arg (=buffered) transport: buffered, framed, header, http"
Roger Meiera3570ac2014-06-10 22:16:14 +020046 exit
James E. King III9aaf2952018-03-20 15:06:08 -040047 elsif a.start_with?("--domain-socket")
48 $domain_socket = a.split("=")[1]
Roger Meiera3570ac2014-06-10 22:16:14 +020049 elsif a.start_with?("--host")
50 $host = a.split("=")[1]
51 elsif a.start_with?("--protocol")
52 $protocolType = a.split("=")[1]
James E. King III714c77c2018-03-20 19:58:38 -040053 elsif a == "--ssl"
54 $ssl = true
Roger Meiera3570ac2014-06-10 22:16:14 +020055 elsif a.start_with?("--transport")
56 $transport = a.split("=")[1]
57 elsif a.start_with?("--port")
Randy Abernethy983bf7d2015-10-09 12:28:57 -070058 $port = a.split("=")[1].to_i
Roger Meiera3570ac2014-06-10 22:16:14 +020059 end
60end
Roger Meiera3570ac2014-06-10 22:16:14 +020061
Kevin Clark4bd89162008-07-08 00:47:49 +000062class SimpleClientTest < Test::Unit::TestCase
Randy Abernethy983bf7d2015-10-09 12:28:57 -070063 def setup
Kevin Clark4bd89162008-07-08 00:47:49 +000064 unless @socket
James E. King III9aaf2952018-03-20 15:06:08 -040065 if $domain_socket.to_s.strip.empty?
James E. King III714c77c2018-03-20 19:58:38 -040066 if $ssl
67 # the working directory for ruby crosstest is test/rb/gen-rb
68 keysDir = File.join(File.dirname(File.dirname(Dir.pwd)), "keys")
69 ctx = OpenSSL::SSL::SSLContext.new
70 ctx.ca_file = File.join(keysDir, "CA.pem")
71 ctx.cert = OpenSSL::X509::Certificate.new(File.open(File.join(keysDir, "client.crt")))
72 ctx.cert_store = OpenSSL::X509::Store.new
73 ctx.cert_store.add_file(File.join(keysDir, 'server.pem'))
74 ctx.key = OpenSSL::PKey::RSA.new(File.open(File.join(keysDir, "client.key")))
75 ctx.options = OpenSSL::SSL::OP_NO_SSLv2 | OpenSSL::SSL::OP_NO_SSLv3
76 ctx.ssl_version = :SSLv23
77 ctx.verify_mode = OpenSSL::SSL::VERIFY_PEER
78 @socket = Thrift::SSLSocket.new($host, $port, nil, ctx)
79 else
80 @socket = Thrift::Socket.new($host, $port)
81 end
James E. King III9aaf2952018-03-20 15:06:08 -040082 else
James E. King III714c77c2018-03-20 19:58:38 -040083 @socket = Thrift::UNIXSocket.new($domain_socket)
James E. King III9aaf2952018-03-20 15:06:08 -040084 end
85
Roger Meiera3570ac2014-06-10 22:16:14 +020086 if $transport == "buffered"
87 transportFactory = Thrift::BufferedTransport.new(@socket)
Roger Meiera3570ac2014-06-10 22:16:14 +020088 elsif $transport == "framed"
89 transportFactory = Thrift::FramedTransport.new(@socket)
Dmytro Shteflyuk67bfb292026-01-28 11:23:50 -050090 elsif $transport == "header"
91 transportFactory = Thrift::HeaderTransport.new(@socket)
Roger Meiera3570ac2014-06-10 22:16:14 +020092 else
93 raise 'Unknown transport type'
94 end
95
96 if $protocolType == "binary"
97 @protocol = Thrift::BinaryProtocol.new(transportFactory)
Roger Meiera3570ac2014-06-10 22:16:14 +020098 elsif $protocolType == "compact"
99 @protocol = Thrift::CompactProtocol.new(transportFactory)
100 elsif $protocolType == "json"
101 @protocol = Thrift::JsonProtocol.new(transportFactory)
102 elsif $protocolType == "accel"
103 @protocol = Thrift::BinaryProtocolAccelerated.new(transportFactory)
Dmytro Shteflyuk67bfb292026-01-28 11:23:50 -0500104 elsif $protocolType == "header"
105 # HeaderProtocol wraps its own transport, so pass the selected transport
106 @protocol = Thrift::HeaderProtocol.new(transportFactory)
Roger Meiera3570ac2014-06-10 22:16:14 +0200107 else
108 raise 'Unknown protocol type'
109 end
James E. King III714c77c2018-03-20 19:58:38 -0400110 @client = Thrift::Test::ThriftTest::Client.new(@protocol)
Kevin Clark4bd89162008-07-08 00:47:49 +0000111 @socket.open
112 end
113 end
Randy Abernethy983bf7d2015-10-09 12:28:57 -0700114
115 def teardown
116 @socket.close
117 end
118
Roger Meiera3570ac2014-06-10 22:16:14 +0200119 def test_void
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900120 p 'test_void'
Roger Meiera3570ac2014-06-10 22:16:14 +0200121 @client.testVoid()
122 end
123
Kevin Clark4bd89162008-07-08 00:47:49 +0000124 def test_string
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900125 p 'test_string'
Nobuaki Sukegawa8cd519f2015-10-10 01:52:13 +0900126 test_string =
127 'quote: \" backslash:' +
128 ' forwardslash-escaped: \/ ' +
129 ' backspace: \b formfeed: \f newline: \n return: \r tab: ' +
130 ' now-all-of-them-together: "\\\/\b\n\r\t' +
131 ' now-a-bunch-of-junk: !@#$%&()(&%$#{}{}<><><' +
132 ' char-to-test-json-parsing: ]] \"]] \\" }}}{ [[[ '
Nobuaki Sukegawa9b35a7c2015-11-17 11:01:41 +0900133 test_string = "Afrikaans, Alemannisch, Aragonés, العربية, مصرى, " +
134 "Asturianu, Aymar aru, Azərbaycan, Башҡорт, Boarisch, Žemaitėška, " +
135 "Беларуская, Беларуская (тарашкевіца), Български, Bamanankan, " +
136 "বাংলা, Brezhoneg, Bosanski, Català, Mìng-dĕ̤ng-ngṳ̄, Нохчийн, " +
137 "Cebuano, ᏣᎳᎩ, Česky, Словѣ́ньскъ / ⰔⰎⰑⰂⰡⰐⰠⰔⰍⰟ, Чӑвашла, Cymraeg, " +
138 "Dansk, Zazaki, ދިވެހިބަސް, Ελληνικά, Emiliàn e rumagnòl, English, " +
139 "Esperanto, Español, Eesti, Euskara, فارسی, Suomi, Võro, Føroyskt, " +
140 "Français, Arpetan, Furlan, Frysk, Gaeilge, 贛語, Gàidhlig, Galego, " +
141 "Avañe'ẽ, ગુજરાતી, Gaelg, עברית, हिन्दी, Fiji Hindi, Hrvatski, " +
142 "Kreyòl ayisyen, Magyar, Հայերեն, Interlingua, Bahasa Indonesia, " +
143 "Ilokano, Ido, Íslenska, Italiano, 日本語, Lojban, Basa Jawa, " +
144 "ქართული, Kongo, Kalaallisut, ಕನ್ನಡ, 한국어, Къарачай-Малкъар, " +
145 "Ripoarisch, Kurdî, Коми, Kernewek, Кыргызча, Latina, Ladino, " +
146 "Lëtzebuergesch, Limburgs, Lingála, ລາວ, Lietuvių, Latviešu, Basa " +
147 "Banyumasan, Malagasy, Македонски, മലയാളം, मराठी, مازِرونی, Bahasa " +
148 "Melayu, Nnapulitano, Nedersaksisch, नेपाल भाषा, Nederlands, ‪" +
149 "Norsk (nynorsk)‬, ‪Norsk (bokmål)‬, Nouormand, Diné bizaad, " +
150 "Occitan, Иронау, Papiamentu, Deitsch, Polski, پنجابی, پښتو, " +
151 "Norfuk / Pitkern, Português, Runa Simi, Rumantsch, Romani, Română, " +
152 "Русский, Саха тыла, Sardu, Sicilianu, Scots, Sámegiella, Simple " +
153 "English, Slovenčina, Slovenščina, Српски / Srpski, Seeltersk, " +
154 "Svenska, Kiswahili, தமிழ், తెలుగు, Тоҷикӣ, ไทย, Türkmençe, Tagalog, " +
155 "Türkçe, Татарча/Tatarça, Українська, اردو, Tiếng Việt, Volapük, " +
156 "Walon, Winaray, 吴语, isiXhosa, ייִדיש, Yorùbá, Zeêuws, 中文, " +
157 "Bân-lâm-gú, 粵語"
Nobuaki Sukegawa8cd519f2015-10-10 01:52:13 +0900158
159 result_string = @client.testString(test_string)
160 assert_equal(test_string, result_string.force_encoding(Encoding::UTF_8))
Kevin Clark4bd89162008-07-08 00:47:49 +0000161 end
162
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900163 def test_bool
164 p 'test_bool'
165 assert_equal(@client.testBool(true), true)
166 assert_equal(@client.testBool(false), false)
167 end
168
Kevin Clark4bd89162008-07-08 00:47:49 +0000169 def test_byte
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900170 p 'test_byte'
171 val = 120
Kevin Clark4bd89162008-07-08 00:47:49 +0000172 assert_equal(@client.testByte(val), val)
173 assert_equal(@client.testByte(-val), -val)
174 end
175
176 def test_i32
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900177 p 'test_i32'
178 val = 2000000032
Kevin Clark4bd89162008-07-08 00:47:49 +0000179 assert_equal(@client.testI32(val), val)
180 assert_equal(@client.testI32(-val), -val)
181 end
182
183 def test_i64
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900184 p 'test_i64'
185 val = 9000000000000000064
Kevin Clark4bd89162008-07-08 00:47:49 +0000186 assert_equal(@client.testI64(val), val)
187 assert_equal(@client.testI64(-val), -val)
188 end
189
190 def test_double
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900191 p 'test_double'
Nobuaki Sukegawa228b3282015-10-10 03:11:49 +0900192 val = 3.14159265358979323846
Kevin Clark4bd89162008-07-08 00:47:49 +0000193 assert_equal(@client.testDouble(val), val)
194 assert_equal(@client.testDouble(-val), -val)
195 assert_kind_of(Float, @client.testDouble(val))
196 end
197
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900198 def test_binary
199 p 'test_binary'
Jens Geyer123258b2015-10-02 00:38:17 +0200200 val = (0...256).reverse_each.to_a
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900201 ret = @client.testBinary(val.pack('C*'))
202 assert_equal(val, ret.bytes.to_a)
203 end
Randy Abernethy983bf7d2015-10-09 12:28:57 -0700204
Kevin Clark4bd89162008-07-08 00:47:49 +0000205 def test_map
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900206 p 'test_map'
Kevin Clark4bd89162008-07-08 00:47:49 +0000207 val = {1 => 1, 2 => 2, 3 => 3}
208 assert_equal(@client.testMap(val), val)
209 assert_kind_of(Hash, @client.testMap(val))
210 end
211
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900212 def test_string_map
213 p 'test_string_map'
214 val = {'a' => '2', 'b' => 'blah', 'some' => 'thing'}
215 ret = @client.testStringMap(val)
216 assert_equal(val, ret)
217 assert_kind_of(Hash, ret)
218 end
219
Kevin Clark4bd89162008-07-08 00:47:49 +0000220 def test_list
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900221 p 'test_list'
Kevin Clark4bd89162008-07-08 00:47:49 +0000222 val = [1,2,3,4,5]
223 assert_equal(@client.testList(val), val)
224 assert_kind_of(Array, @client.testList(val))
225 end
226
227 def test_enum
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900228 p 'test_enum'
Kevin Clark4bd89162008-07-08 00:47:49 +0000229 val = Thrift::Test::Numberz::SIX
230 ret = @client.testEnum(val)
231
232 assert_equal(ret, 6)
Dmytro Shteflyukf06db1b2025-11-20 18:09:21 -0500233 assert_kind_of(Integer, ret)
Kevin Clark4bd89162008-07-08 00:47:49 +0000234 end
235
236 def test_typedef
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900237 p 'test_typedef'
Kevin Clark4bd89162008-07-08 00:47:49 +0000238 #UserId testTypedef(1: UserId thing),
Roger Meiera3570ac2014-06-10 22:16:14 +0200239 assert_equal(@client.testTypedef(309858235082523), 309858235082523)
Dmytro Shteflyukf06db1b2025-11-20 18:09:21 -0500240 assert_kind_of(Integer, @client.testTypedef(309858235082523))
Kevin Clark4bd89162008-07-08 00:47:49 +0000241 true
242 end
243
244 def test_set
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900245 p 'test_set'
Kevin Clark4bd89162008-07-08 00:47:49 +0000246 val = Set.new([1,2,3])
247 assert_equal(@client.testSet(val), val)
248 assert_kind_of(Set, @client.testSet(val))
249 end
250
251 def get_struct
252 Thrift::Test::Xtruct.new({'string_thing' => 'hi!', 'i32_thing' => 4 })
253 end
254
Dmytro Shteflyuke9ac8e32025-11-19 23:33:23 -0500255 def test_uuid
256 p 'test_uuid'
257 val = '00112233-4455-6677-8899-aabbccddeeff'
258 ret = @client.testUuid(val)
259 assert_equal(ret, val)
260 assert_kind_of(String, ret)
261 end
262
Kevin Clark4bd89162008-07-08 00:47:49 +0000263 def test_struct
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900264 p 'test_struct'
Kevin Clark4bd89162008-07-08 00:47:49 +0000265 ret = @client.testStruct(get_struct)
266
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900267 # TODO: not sure what unspecified "default" requiredness values should be
268 assert(ret.byte_thing == nil || ret.byte_thing == 0)
269 assert(ret.i64_thing == nil || ret.i64_thing == 0)
270
Kevin Clark4bd89162008-07-08 00:47:49 +0000271 assert_equal(ret.string_thing, 'hi!')
272 assert_equal(ret.i32_thing, 4)
273 assert_kind_of(Thrift::Test::Xtruct, ret)
274 end
275
276 def test_nest
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900277 p 'test_nest'
Kevin Clark4bd89162008-07-08 00:47:49 +0000278 struct2 = Thrift::Test::Xtruct2.new({'struct_thing' => get_struct, 'i32_thing' => 10})
279
280 ret = @client.testNest(struct2)
281
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900282 # TODO: not sure what unspecified "default" requiredness values should be
283 assert(ret.struct_thing.byte_thing == nil || ret.struct_thing.byte_thing == 0)
284 assert(ret.struct_thing.i64_thing == nil || ret.struct_thing.i64_thing == 0)
285
Kevin Clark4bd89162008-07-08 00:47:49 +0000286 assert_equal(ret.struct_thing.string_thing, 'hi!')
287 assert_equal(ret.struct_thing.i32_thing, 4)
288 assert_equal(ret.i32_thing, 10)
289
290 assert_kind_of(Thrift::Test::Xtruct, ret.struct_thing)
291 assert_kind_of(Thrift::Test::Xtruct2, ret)
292 end
293
Roger Meiera3570ac2014-06-10 22:16:14 +0200294 def test_insanity
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900295 p 'test_insanity'
Kevin Clark4bd89162008-07-08 00:47:49 +0000296 insane = Thrift::Test::Insanity.new({
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900297 'userMap' => {
298 Thrift::Test::Numberz::FIVE => 5,
299 Thrift::Test::Numberz::EIGHT => 8,
300 },
301 'xtructs' => [
Kevin Clark4bd89162008-07-08 00:47:49 +0000302 Thrift::Test::Xtruct.new({
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900303 'string_thing' => 'Goodbye4',
304 'byte_thing' => 4,
305 'i32_thing' => 4,
306 'i64_thing' => 4,
307 }),
308 Thrift::Test::Xtruct.new({
309 'string_thing' => 'Hello2',
310 'byte_thing' => 2,
311 'i32_thing' => 2,
312 'i64_thing' => 2,
Kevin Clark4bd89162008-07-08 00:47:49 +0000313 })
314 ]
315 })
316
317 ret = @client.testInsanity(insane)
318
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900319 assert_equal(insane, ret[1][2])
320 assert_equal(insane, ret[1][3])
Kevin Clark4bd89162008-07-08 00:47:49 +0000321
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900322 assert(ret[2][6].userMap == nil || ret[2][6].userMap.length == 0)
323 assert(ret[2][6].xtructs == nil || ret[2][6].xtructs.length == 0)
Kevin Clark4bd89162008-07-08 00:47:49 +0000324 end
325
326 def test_map_map
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900327 p 'test_map_map'
Kevin Clark4bd89162008-07-08 00:47:49 +0000328 ret = @client.testMapMap(4)
329 assert_kind_of(Hash, ret)
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900330 expected = {
331 -4 => {
332 -4 => -4,
333 -3 => -3,
334 -2 => -2,
335 -1 => -1,
336 },
337 4 => {
338 4 => 4,
339 3 => 3,
340 2 => 2,
341 1 => 1,
342 }
343 }
344 assert_equal(expected, ret)
345 end
346
347 def test_multi
348 p 'test_multi'
349 ret = @client.testMulti(42, 4242, 424242, {1 => 'blah', 2 => 'thing'}, Thrift::Test::Numberz::EIGHT, 24)
350 expected = Thrift::Test::Xtruct.new({
351 :string_thing => 'Hello2',
352 :byte_thing => 42,
353 :i32_thing => 4242,
354 :i64_thing => 424242
355 })
356 assert_equal(expected, ret)
Kevin Clark4bd89162008-07-08 00:47:49 +0000357 end
358
359 def test_exception
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900360 p 'test_exception'
Kevin Clark4bd89162008-07-08 00:47:49 +0000361 assert_raise Thrift::Test::Xception do
Roger Meiera3570ac2014-06-10 22:16:14 +0200362 @client.testException('Xception')
Kevin Clark4bd89162008-07-08 00:47:49 +0000363 end
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900364 begin
365 @client.testException('TException')
366 rescue => e
367 assert e.class.ancestors.include?(Thrift::Exception)
368 end
369 assert_nothing_raised do
370 @client.testException('test')
371 end
Kevin Clark4bd89162008-07-08 00:47:49 +0000372 end
Roger Meiera3570ac2014-06-10 22:16:14 +0200373
374 def test_multi_exception
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900375 p 'test_multi_exception'
Roger Meiera3570ac2014-06-10 22:16:14 +0200376 assert_raise Thrift::Test::Xception do
377 @client.testMultiException("Xception", "test 1")
378 end
379 assert_raise Thrift::Test::Xception2 do
380 @client.testMultiException("Xception2", "test 2")
381 end
382 assert_equal( @client.testMultiException("Success", "test 3").string_thing, "test 3")
383 end
384
385 def test_oneway
Nobuaki Sukegawa68238292015-09-21 23:28:22 +0900386 p 'test_oneway'
Roger Meiera3570ac2014-06-10 22:16:14 +0200387 time1 = Time.now.to_f
Nobuaki Sukegawaa6ab1f52015-11-28 15:04:39 +0900388 @client.testOneway(1)
Roger Meiera3570ac2014-06-10 22:16:14 +0200389 time2 = Time.now.to_f
Nobuaki Sukegawaa6ab1f52015-11-28 15:04:39 +0900390 assert_operator (time2-time1), :<, 0.1
Roger Meiera3570ac2014-06-10 22:16:14 +0200391 end
392
Kevin Clark4bd89162008-07-08 00:47:49 +0000393end