Roger Meier | 32f3982 | 2014-06-18 22:43:17 +0200 | [diff] [blame] | 1 | #!/usr/bin/env ruby |
| 2 | |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 3 | # |
| 4 | # Licensed to the Apache Software Foundation (ASF) under one |
| 5 | # or more contributor license agreements. See the NOTICE file |
| 6 | # distributed with this work for additional information |
| 7 | # regarding copyright ownership. The ASF licenses this file |
| 8 | # to you under the Apache License, Version 2.0 (the |
| 9 | # "License"); you may not use this file except in compliance |
| 10 | # with the License. You may obtain a copy of the License at |
| 11 | # |
| 12 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | # |
| 14 | # Unless required by applicable law or agreed to in writing, |
| 15 | # software distributed under the License is distributed on an |
| 16 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 17 | # KIND, either express or implied. See the License for the |
| 18 | # specific language governing permissions and limitations |
| 19 | # under the License. |
| 20 | # |
| 21 | |
| 22 | $:.push File.dirname(__FILE__) + '/..' |
| 23 | |
| 24 | require 'test_helper' |
| 25 | require 'thrift' |
| 26 | require 'thrift_test' |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 27 | require 'thrift_test_types' |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 28 | |
| 29 | class SimpleHandler |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 30 | [:testVoid, :testString, :testBool, :testByte, :testI32, :testI64, :testDouble, :testBinary, |
| 31 | :testStruct, :testMap, :testStringMap, :testSet, :testList, :testNest, :testEnum, :testTypedef, |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 32 | :testEnum, :testTypedef, :testMultiException].each do |meth| |
| 33 | |
| 34 | define_method(meth) do |thing| |
Jens Geyer | 123258b | 2015-10-02 00:38:17 +0200 | [diff] [blame] | 35 | p meth |
| 36 | p thing |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 37 | thing |
| 38 | end |
| 39 | |
| 40 | end |
| 41 | |
| 42 | def testVoid() |
| 43 | end |
| 44 | |
| 45 | def testInsanity(thing) |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 46 | return { |
| 47 | 1 => { |
| 48 | 2 => thing, |
| 49 | 3 => thing |
| 50 | }, |
| 51 | 2 => { |
| 52 | 6 => Thrift::Test::Insanity::new() |
| 53 | } |
| 54 | } |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 55 | end |
| 56 | |
| 57 | def testMapMap(thing) |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 58 | return { |
| 59 | -4 => { |
| 60 | -4 => -4, |
| 61 | -3 => -3, |
| 62 | -2 => -2, |
| 63 | -1 => -1, |
| 64 | }, |
| 65 | 4 => { |
| 66 | 4 => 4, |
| 67 | 3 => 3, |
| 68 | 2 => 2, |
| 69 | 1 => 1, |
| 70 | } |
| 71 | } |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 72 | end |
| 73 | |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 74 | def testMulti(arg0, arg1, arg2, arg3, arg4, arg5) |
| 75 | return Thrift::Test::Xtruct.new({ |
| 76 | 'string_thing' => 'Hello2', |
| 77 | 'byte_thing' => arg0, |
| 78 | 'i32_thing' => arg1, |
| 79 | 'i64_thing' => arg2, |
| 80 | }) |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 81 | end |
| 82 | |
| 83 | def testException(thing) |
| 84 | if thing == "Xception" |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 85 | raise Thrift::Test::Xception, :errorCode => 1001, :message => thing |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 86 | elsif thing == "TException" |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 87 | raise Thrift::Exception, :message => thing |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 88 | else |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 89 | # no-op |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 90 | end |
| 91 | end |
| 92 | |
| 93 | def testMultiException(arg0, arg1) |
| 94 | if arg0 == "Xception2" |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 95 | raise Thrift::Test::Xception2, :errorCode => 2002, :struct_thing => ::Thrift::Test::Xtruct.new({ :string_thing => 'This is an Xception2' }) |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 96 | elsif arg0 == "Xception" |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 97 | raise Thrift::Test::Xception, :errorCode => 1001, :message => 'This is an Xception' |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 98 | else |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 99 | return ::Thrift::Test::Xtruct.new({'string_thing' => arg1}) |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 100 | end |
| 101 | end |
| 102 | |
Nobuaki Sukegawa | 6823829 | 2015-09-21 23:28:22 +0900 | [diff] [blame] | 103 | def testOneway(arg0) |
| 104 | sleep(arg0) |
| 105 | end |
| 106 | |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 107 | end |
| 108 | |
James E. King III | 9aaf295 | 2018-03-20 15:06:08 -0400 | [diff] [blame] | 109 | domain_socket = nil |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 110 | port = 9090 |
James E. King III | 9aaf295 | 2018-03-20 15:06:08 -0400 | [diff] [blame] | 111 | protocol = "binary" |
| 112 | @protocolFactory = nil |
James E. King III | 714c77c | 2018-03-20 19:58:38 -0400 | [diff] [blame] | 113 | ssl = false |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 114 | transport = "buffered" |
James E. King III | 9aaf295 | 2018-03-20 15:06:08 -0400 | [diff] [blame] | 115 | @transportFactory = nil |
| 116 | |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 117 | ARGV.each do|a| |
| 118 | if a == "--help" |
| 119 | puts "Allowed options:" |
| 120 | puts "\t -h [ --help ] \t produce help message" |
James E. King III | 714c77c | 2018-03-20 19:58:38 -0400 | [diff] [blame] | 121 | puts "\t--domain-socket arg (=) \t Unix domain socket path" |
| 122 | puts "\t--port arg (=9090) \t Port number to listen \t not valid with domain-socket" |
James E. King III | 9aaf295 | 2018-03-20 15:06:08 -0400 | [diff] [blame] | 123 | puts "\t--protocol arg (=binary) \t protocol: accel, binary, compact, json" |
James E. King III | 714c77c | 2018-03-20 19:58:38 -0400 | [diff] [blame] | 124 | puts "\t--ssl \t use ssl \t not valid with domain-socket" |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 125 | puts "\t--transport arg (=buffered) transport: buffered, framed, http" |
| 126 | exit |
James E. King III | 9aaf295 | 2018-03-20 15:06:08 -0400 | [diff] [blame] | 127 | elsif a.start_with?("--domain-socket") |
| 128 | domain_socket = a.split("=")[1] |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 129 | elsif a.start_with?("--protocol") |
| 130 | protocol = a.split("=")[1] |
James E. King III | 714c77c | 2018-03-20 19:58:38 -0400 | [diff] [blame] | 131 | elsif a == "--ssl" |
| 132 | ssl = true |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 133 | elsif a.start_with?("--transport") |
| 134 | transport = a.split("=")[1] |
| 135 | elsif a.start_with?("--port") |
| 136 | port = a.split("=")[1].to_i |
| 137 | end |
| 138 | end |
| 139 | |
James E. King III | 9aaf295 | 2018-03-20 15:06:08 -0400 | [diff] [blame] | 140 | if protocol == "binary" || protocol.to_s.strip.empty? |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 141 | @protocolFactory = Thrift::BinaryProtocolFactory.new |
| 142 | elsif protocol == "compact" |
| 143 | @protocolFactory = Thrift::CompactProtocolFactory.new |
| 144 | elsif protocol == "json" |
| 145 | @protocolFactory = Thrift::JsonProtocolFactory.new |
| 146 | elsif protocol == "accel" |
| 147 | @protocolFactory = Thrift::BinaryProtocolAcceleratedFactory.new |
| 148 | else |
| 149 | raise 'Unknown protocol type' |
| 150 | end |
| 151 | |
James E. King III | 9aaf295 | 2018-03-20 15:06:08 -0400 | [diff] [blame] | 152 | if transport == "buffered" || transport.to_s.strip.empty? |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 153 | @transportFactory = Thrift::BufferedTransportFactory.new |
| 154 | elsif transport == "framed" |
| 155 | @transportFactory = Thrift::FramedTransportFactory.new |
| 156 | else |
| 157 | raise 'Unknown transport type' |
| 158 | end |
| 159 | |
James E. King III | 9aaf295 | 2018-03-20 15:06:08 -0400 | [diff] [blame] | 160 | @handler = SimpleHandler.new |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 161 | @processor = Thrift::Test::ThriftTest::Processor.new(@handler) |
James E. King III | 9aaf295 | 2018-03-20 15:06:08 -0400 | [diff] [blame] | 162 | @transport = nil |
| 163 | if domain_socket.to_s.strip.empty? |
James E. King III | 714c77c | 2018-03-20 19:58:38 -0400 | [diff] [blame] | 164 | if ssl |
| 165 | # the working directory for ruby crosstest is test/rb/gen-rb |
| 166 | keysDir = File.join(File.dirname(File.dirname(Dir.pwd)), "keys") |
| 167 | ctx = OpenSSL::SSL::SSLContext.new |
| 168 | ctx.ca_file = File.join(keysDir, "CA.pem") |
| 169 | ctx.cert = OpenSSL::X509::Certificate.new(File.open(File.join(keysDir, "server.crt"))) |
| 170 | ctx.cert_store = OpenSSL::X509::Store.new |
| 171 | ctx.cert_store.add_file(File.join(keysDir, 'client.pem')) |
| 172 | ctx.key = OpenSSL::PKey::RSA.new(File.open(File.join(keysDir, "server.key"))) |
| 173 | ctx.options = OpenSSL::SSL::OP_NO_SSLv2 | OpenSSL::SSL::OP_NO_SSLv3 |
| 174 | ctx.ssl_version = :SSLv23 |
| 175 | ctx.verify_mode = OpenSSL::SSL::VERIFY_PEER |
| 176 | @transport = Thrift::SSLServerSocket.new(nil, port, ctx) |
| 177 | else |
| 178 | @transport = Thrift::ServerSocket.new(port) |
| 179 | end |
James E. King III | 9aaf295 | 2018-03-20 15:06:08 -0400 | [diff] [blame] | 180 | else |
| 181 | @transport = Thrift::UNIXServerSocket.new(domain_socket) |
| 182 | end |
| 183 | |
| 184 | @server = Thrift::ThreadedServer.new(@processor, @transport, @transportFactory, @protocolFactory) |
| 185 | |
| 186 | puts "Starting TestServer #{@server.to_s}" |
Roger Meier | a3570ac | 2014-06-10 22:16:14 +0200 | [diff] [blame] | 187 | @server.serve |
James E. King III | 9aaf295 | 2018-03-20 15:06:08 -0400 | [diff] [blame] | 188 | puts "done." |