T Jake Luciani | 41687fc | 2008-12-23 03:45:43 +0000 | [diff] [blame] | 1 | #!/usr/bin/env perl |
Mark Slee | 3e3d7ad | 2007-05-16 02:35:58 +0000 | [diff] [blame] | 2 | |
David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [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 | |
Mark Slee | 3e3d7ad | 2007-05-16 02:35:58 +0000 | [diff] [blame] | 22 | require 5.6.0; |
| 23 | use strict; |
| 24 | use warnings; |
| 25 | use Data::Dumper; |
Jim King | f5f1b35 | 2015-06-24 13:47:24 -0400 | [diff] [blame] | 26 | use Getopt::Long qw(GetOptions); |
Mark Slee | 3e3d7ad | 2007-05-16 02:35:58 +0000 | [diff] [blame] | 27 | use Time::HiRes qw(gettimeofday); |
| 28 | |
| 29 | use lib '../../lib/perl/lib'; |
T Jake Luciani | 41687fc | 2008-12-23 03:45:43 +0000 | [diff] [blame] | 30 | use lib 'gen-perl'; |
Mark Slee | 3e3d7ad | 2007-05-16 02:35:58 +0000 | [diff] [blame] | 31 | |
| 32 | use Thrift; |
| 33 | use Thrift::BinaryProtocol; |
Mark Slee | 3e3d7ad | 2007-05-16 02:35:58 +0000 | [diff] [blame] | 34 | use Thrift::BufferedTransport; |
Jim King | f5f1b35 | 2015-06-24 13:47:24 -0400 | [diff] [blame] | 35 | use Thrift::FramedTransport; |
| 36 | use Thrift::SSLSocket; |
| 37 | use Thrift::Socket; |
Mark Slee | 3e3d7ad | 2007-05-16 02:35:58 +0000 | [diff] [blame] | 38 | |
T Jake Luciani | 41687fc | 2008-12-23 03:45:43 +0000 | [diff] [blame] | 39 | use ThriftTest::ThriftTest; |
Mark Slee | 27ed6ec | 2007-08-16 01:26:31 +0000 | [diff] [blame] | 40 | use ThriftTest::Types; |
Mark Slee | 3e3d7ad | 2007-05-16 02:35:58 +0000 | [diff] [blame] | 41 | |
| 42 | $|++; |
| 43 | |
Jim King | f5f1b35 | 2015-06-24 13:47:24 -0400 | [diff] [blame] | 44 | sub usage { |
| 45 | print <<EOF; |
| 46 | Usage: $0 [OPTIONS] |
Mark Slee | 3e3d7ad | 2007-05-16 02:35:58 +0000 | [diff] [blame] | 47 | |
Jim King | f5f1b35 | 2015-06-24 13:47:24 -0400 | [diff] [blame] | 48 | Options: (default) |
| 49 | --cert Certificate to use. |
| 50 | Required if using --ssl. |
| 51 | --help Show usage. |
| 52 | --port <portnum> 9090 Port to use. |
| 53 | --protocol {binary} binary Protocol to use. |
| 54 | --ssl If present, use SSL. |
| 55 | --transport {buffered|framed} buffered Transport to use. |
| 56 | |
| 57 | EOF |
Roger Meier | 41ad434 | 2015-03-24 22:30:40 +0100 | [diff] [blame] | 58 | } |
Mark Slee | 3e3d7ad | 2007-05-16 02:35:58 +0000 | [diff] [blame] | 59 | |
Jim King | f5f1b35 | 2015-06-24 13:47:24 -0400 | [diff] [blame] | 60 | my %opts = ( |
| 61 | 'port' => 9090, |
| 62 | 'protocol' => 'binary', |
| 63 | 'transport' => 'buffered' |
| 64 | ); |
Mark Slee | 3e3d7ad | 2007-05-16 02:35:58 +0000 | [diff] [blame] | 65 | |
Jim King | f5f1b35 | 2015-06-24 13:47:24 -0400 | [diff] [blame] | 66 | GetOptions(\%opts, qw ( |
| 67 | cert=s |
| 68 | help |
| 69 | host=s |
| 70 | port=i |
| 71 | protocol=s |
| 72 | ssl |
| 73 | transport=s |
| 74 | )) || exit 1; |
| 75 | |
| 76 | if ($opts{help}) { |
| 77 | usage(); |
| 78 | exit 0; |
| 79 | } |
| 80 | |
| 81 | if ($opts{ssl} and not defined $opts{cert}) { |
| 82 | usage(); |
| 83 | exit 1; |
| 84 | } |
| 85 | |
| 86 | my $socket = undef; |
| 87 | if ($opts{ssl}) { |
| 88 | $socket = new Thrift::SSLSocket($opts{host}, $opts{port}); |
| 89 | } else { |
| 90 | $socket = new Thrift::Socket($opts{host}, $opts{port}); |
| 91 | } |
| 92 | |
| 93 | my $transport; |
| 94 | if ($opts{transport} eq 'buffered') { |
| 95 | $transport = new Thrift::BufferedTransport($socket, 1024, 1024); |
| 96 | } elsif ($opts{transport} eq 'framed') { |
| 97 | $transport = new Thrift::FramedTransport($socket); |
| 98 | } else { |
| 99 | usage(); |
| 100 | exit 1; |
| 101 | } |
| 102 | |
| 103 | my $protocol; |
| 104 | if ($opts{protocol} eq 'binary') { |
| 105 | $protocol = new Thrift::BinaryProtocol($transport); |
| 106 | } else { |
| 107 | usage(); |
| 108 | exit 1; |
| 109 | } |
| 110 | |
T Jake Luciani | 41687fc | 2008-12-23 03:45:43 +0000 | [diff] [blame] | 111 | my $testClient = new ThriftTest::ThriftTestClient($protocol); |
Mark Slee | 3e3d7ad | 2007-05-16 02:35:58 +0000 | [diff] [blame] | 112 | |
Jim King | f5f1b35 | 2015-06-24 13:47:24 -0400 | [diff] [blame] | 113 | eval { |
| 114 | $transport->open(); |
| 115 | }; |
| 116 | if($@){ |
Mark Slee | 3e3d7ad | 2007-05-16 02:35:58 +0000 | [diff] [blame] | 117 | die(Dumper($@)); |
| 118 | } |
| 119 | my $start = gettimeofday(); |
| 120 | |
| 121 | # |
| 122 | # VOID TEST |
| 123 | # |
| 124 | print("testVoid()"); |
| 125 | $testClient->testVoid(); |
| 126 | print(" = void\n"); |
| 127 | |
| 128 | # |
| 129 | # STRING TEST |
| 130 | # |
| 131 | print("testString(\"Test\")"); |
| 132 | my $s = $testClient->testString("Test"); |
| 133 | print(" = \"$s\"\n"); |
| 134 | |
| 135 | # |
Nobuaki Sukegawa | a649e74 | 2015-09-21 13:53:25 +0900 | [diff] [blame] | 136 | # BOOL TEST |
| 137 | # |
| 138 | print("testBool(1)"); |
Jens Geyer | d629ea0 | 2015-09-23 21:16:50 +0200 | [diff] [blame] | 139 | my $t = $testClient->testBool(1); |
| 140 | print(" = $t\n"); |
Nobuaki Sukegawa | a649e74 | 2015-09-21 13:53:25 +0900 | [diff] [blame] | 141 | print("testBool(0)"); |
Jens Geyer | d629ea0 | 2015-09-23 21:16:50 +0200 | [diff] [blame] | 142 | my $f = $testClient->testBool(0); |
| 143 | print(" = $f\n"); |
Nobuaki Sukegawa | a649e74 | 2015-09-21 13:53:25 +0900 | [diff] [blame] | 144 | |
| 145 | |
| 146 | # |
Mark Slee | 3e3d7ad | 2007-05-16 02:35:58 +0000 | [diff] [blame] | 147 | # BYTE TEST |
| 148 | # |
| 149 | print("testByte(1)"); |
| 150 | my $u8 = $testClient->testByte(1); |
| 151 | print(" = $u8\n"); |
| 152 | |
| 153 | # |
| 154 | # I32 TEST |
| 155 | # |
| 156 | print("testI32(-1)"); |
| 157 | my $i32 = $testClient->testI32(-1); |
| 158 | print(" = $i32\n"); |
| 159 | |
| 160 | # |
| 161 | #I64 TEST |
| 162 | # |
| 163 | print("testI64(-34359738368)"); |
| 164 | my $i64 = $testClient->testI64(-34359738368); |
| 165 | print(" = $i64\n"); |
| 166 | |
| 167 | # |
| 168 | # DOUBLE TEST |
| 169 | # |
| 170 | print("testDouble(-852.234234234)"); |
| 171 | my $dub = $testClient->testDouble(-852.234234234); |
| 172 | print(" = $dub\n"); |
| 173 | |
| 174 | # |
Jens Geyer | 8bcfdd9 | 2014-12-14 03:14:26 +0100 | [diff] [blame] | 175 | # BINARY TEST --- TODO |
| 176 | # |
| 177 | |
| 178 | |
| 179 | # |
Mark Slee | 3e3d7ad | 2007-05-16 02:35:58 +0000 | [diff] [blame] | 180 | # STRUCT TEST |
| 181 | # |
| 182 | print("testStruct({\"Zero\", 1, -3, -5})"); |
Mark Slee | 8266443 | 2007-09-19 06:49:30 +0000 | [diff] [blame] | 183 | my $out = new ThriftTest::Xtruct(); |
| 184 | $out->string_thing("Zero"); |
| 185 | $out->byte_thing(1); |
| 186 | $out->i32_thing(-3); |
| 187 | $out->i64_thing(-5); |
Mark Slee | 3e3d7ad | 2007-05-16 02:35:58 +0000 | [diff] [blame] | 188 | my $in = $testClient->testStruct($out); |
Mark Slee | 8266443 | 2007-09-19 06:49:30 +0000 | [diff] [blame] | 189 | print(" = {\"".$in->string_thing."\", ". |
| 190 | $in->byte_thing.", ". |
| 191 | $in->i32_thing.", ". |
| 192 | $in->i64_thing."}\n"); |
Mark Slee | 3e3d7ad | 2007-05-16 02:35:58 +0000 | [diff] [blame] | 193 | |
| 194 | # |
| 195 | # NESTED STRUCT TEST |
| 196 | # |
| 197 | print("testNest({1, {\"Zero\", 1, -3, -5}, 5}"); |
Mark Slee | 8266443 | 2007-09-19 06:49:30 +0000 | [diff] [blame] | 198 | my $out2 = new ThriftTest::Xtruct2(); |
| 199 | $out2->byte_thing(1); |
| 200 | $out2->struct_thing($out); |
| 201 | $out2->i32_thing(5); |
Mark Slee | 3e3d7ad | 2007-05-16 02:35:58 +0000 | [diff] [blame] | 202 | my $in2 = $testClient->testNest($out2); |
Mark Slee | 8266443 | 2007-09-19 06:49:30 +0000 | [diff] [blame] | 203 | $in = $in2->struct_thing; |
| 204 | print(" = {".$in2->byte_thing.", {\"". |
| 205 | $in->string_thing."\", ". |
| 206 | $in->byte_thing.", ". |
| 207 | $in->i32_thing.", ". |
| 208 | $in->i64_thing."}, ". |
| 209 | $in2->i32_thing."}\n"); |
Mark Slee | 3e3d7ad | 2007-05-16 02:35:58 +0000 | [diff] [blame] | 210 | |
| 211 | # |
| 212 | # MAP TEST |
| 213 | # |
| 214 | my $mapout = {}; |
| 215 | for (my $i = 0; $i < 5; ++$i) { |
| 216 | $mapout->{$i} = $i-10; |
| 217 | } |
| 218 | print("testMap({"); |
| 219 | my $first = 1; |
| 220 | while( my($key,$val) = each %$mapout) { |
| 221 | if ($first) { |
| 222 | $first = 0; |
| 223 | } else { |
| 224 | print(", "); |
| 225 | } |
| 226 | print("$key => $val"); |
| 227 | } |
| 228 | print("})"); |
| 229 | |
| 230 | |
| 231 | my $mapin = $testClient->testMap($mapout); |
| 232 | print(" = {"); |
| 233 | |
| 234 | $first = 1; |
| 235 | while( my($key,$val) = each %$mapin){ |
| 236 | if ($first) { |
| 237 | $first = 0; |
| 238 | } else { |
| 239 | print(", "); |
| 240 | } |
| 241 | print("$key => $val"); |
| 242 | } |
| 243 | print("}\n"); |
| 244 | |
| 245 | # |
| 246 | # SET TEST |
| 247 | # |
| 248 | my $setout = []; |
| 249 | for (my $i = -2; $i < 3; ++$i) { |
| 250 | push(@$setout, $i); |
| 251 | } |
| 252 | |
| 253 | print("testSet({".join(",",@$setout)."})"); |
| 254 | |
| 255 | my $setin = $testClient->testSet($setout); |
| 256 | |
| 257 | print(" = {".join(",",@$setout)."}\n"); |
| 258 | |
| 259 | # |
| 260 | # LIST TEST |
| 261 | # |
| 262 | my $listout = []; |
| 263 | for (my $i = -2; $i < 3; ++$i) { |
| 264 | push(@$listout, $i); |
| 265 | } |
| 266 | |
| 267 | print("testList({".join(",",@$listout)."})"); |
| 268 | |
| 269 | my $listin = $testClient->testList($listout); |
| 270 | |
| 271 | print(" = {".join(",",@$listin)."}\n"); |
| 272 | |
| 273 | # |
| 274 | # ENUM TEST |
| 275 | # |
| 276 | print("testEnum(ONE)"); |
T Jake Luciani | 41687fc | 2008-12-23 03:45:43 +0000 | [diff] [blame] | 277 | my $ret = $testClient->testEnum(ThriftTest::Numberz::ONE); |
Mark Slee | 3e3d7ad | 2007-05-16 02:35:58 +0000 | [diff] [blame] | 278 | print(" = $ret\n"); |
| 279 | |
| 280 | print("testEnum(TWO)"); |
T Jake Luciani | 41687fc | 2008-12-23 03:45:43 +0000 | [diff] [blame] | 281 | $ret = $testClient->testEnum(ThriftTest::Numberz::TWO); |
Mark Slee | 3e3d7ad | 2007-05-16 02:35:58 +0000 | [diff] [blame] | 282 | print(" = $ret\n"); |
| 283 | |
| 284 | print("testEnum(THREE)"); |
T Jake Luciani | 41687fc | 2008-12-23 03:45:43 +0000 | [diff] [blame] | 285 | $ret = $testClient->testEnum(ThriftTest::Numberz::THREE); |
Mark Slee | 3e3d7ad | 2007-05-16 02:35:58 +0000 | [diff] [blame] | 286 | print(" = $ret\n"); |
| 287 | |
| 288 | print("testEnum(FIVE)"); |
T Jake Luciani | 41687fc | 2008-12-23 03:45:43 +0000 | [diff] [blame] | 289 | $ret = $testClient->testEnum(ThriftTest::Numberz::FIVE); |
Mark Slee | 3e3d7ad | 2007-05-16 02:35:58 +0000 | [diff] [blame] | 290 | print(" = $ret\n"); |
| 291 | |
| 292 | print("testEnum(EIGHT)"); |
T Jake Luciani | 41687fc | 2008-12-23 03:45:43 +0000 | [diff] [blame] | 293 | $ret = $testClient->testEnum(ThriftTest::Numberz::EIGHT); |
Mark Slee | 3e3d7ad | 2007-05-16 02:35:58 +0000 | [diff] [blame] | 294 | print(" = $ret\n"); |
| 295 | |
| 296 | # |
| 297 | # TYPEDEF TEST |
| 298 | # |
| 299 | print("testTypedef(309858235082523)"); |
| 300 | my $uid = $testClient->testTypedef(309858235082523); |
| 301 | print(" = $uid\n"); |
| 302 | |
| 303 | # |
| 304 | # NESTED MAP TEST |
| 305 | # |
| 306 | print("testMapMap(1)"); |
| 307 | my $mm = $testClient->testMapMap(1); |
| 308 | print(" = {"); |
| 309 | while( my ($key,$val) = each %$mm) { |
| 310 | print("$key => {"); |
| 311 | while( my($k2,$v2) = each %$val) { |
| 312 | print("$k2 => $v2, "); |
| 313 | } |
| 314 | print("}, "); |
| 315 | } |
| 316 | print("}\n"); |
| 317 | |
| 318 | # |
| 319 | # INSANITY TEST |
| 320 | # |
Mark Slee | 8266443 | 2007-09-19 06:49:30 +0000 | [diff] [blame] | 321 | my $insane = new ThriftTest::Insanity(); |
T Jake Luciani | 41687fc | 2008-12-23 03:45:43 +0000 | [diff] [blame] | 322 | $insane->{userMap}->{ThriftTest::Numberz::FIVE} = 5000; |
Mark Slee | 8266443 | 2007-09-19 06:49:30 +0000 | [diff] [blame] | 323 | my $truck = new ThriftTest::Xtruct(); |
Jens Geyer | d629ea0 | 2015-09-23 21:16:50 +0200 | [diff] [blame] | 324 | $truck->string_thing("Hello2"); |
| 325 | $truck->byte_thing(2); |
| 326 | $truck->i32_thing(2); |
| 327 | $truck->i64_thing(2); |
| 328 | my $truck2 = new ThriftTest::Xtruct(); |
| 329 | $truck2->string_thing("Goodbye4"); |
| 330 | $truck2->byte_thing(4); |
| 331 | $truck2->i32_thing(4); |
| 332 | $truck2->i64_thing(4); |
Mark Slee | 3e3d7ad | 2007-05-16 02:35:58 +0000 | [diff] [blame] | 333 | push(@{$insane->{xtructs}}, $truck); |
Jens Geyer | d629ea0 | 2015-09-23 21:16:50 +0200 | [diff] [blame] | 334 | push(@{$insane->{xtructs}}, $truck2); |
Mark Slee | 3e3d7ad | 2007-05-16 02:35:58 +0000 | [diff] [blame] | 335 | |
| 336 | print("testInsanity()"); |
| 337 | my $whoa = $testClient->testInsanity($insane); |
| 338 | print(" = {"); |
| 339 | while( my ($key,$val) = each %$whoa) { |
| 340 | print("$key => {"); |
| 341 | while( my($k2,$v2) = each %$val) { |
| 342 | print("$k2 => {"); |
| 343 | my $userMap = $v2->{userMap}; |
| 344 | print("{"); |
| 345 | if (ref($userMap) eq "HASH") { |
| 346 | while( my($k3,$v3) = each %$userMap) { |
| 347 | print("$k3 => $v3, "); |
| 348 | } |
| 349 | } |
| 350 | print("}, "); |
| 351 | |
| 352 | my $xtructs = $v2->{xtructs}; |
| 353 | print("{"); |
| 354 | if (ref($xtructs) eq "ARRAY") { |
| 355 | foreach my $x (@$xtructs) { |
| 356 | print("{\"".$x->{string_thing}."\", ". |
| 357 | $x->{byte_thing}.", ".$x->{i32_thing}.", ".$x->{i64_thing}."}, "); |
| 358 | } |
| 359 | } |
| 360 | print("}"); |
| 361 | |
| 362 | print("}, "); |
| 363 | } |
| 364 | print("}, "); |
| 365 | } |
| 366 | print("}\n"); |
| 367 | |
| 368 | # |
| 369 | # EXCEPTION TEST |
| 370 | # |
| 371 | print("testException('Xception')"); |
| 372 | eval { |
| 373 | $testClient->testException('Xception'); |
| 374 | print(" void\nFAILURE\n"); |
Mark Slee | 8266443 | 2007-09-19 06:49:30 +0000 | [diff] [blame] | 375 | }; if($@ && $@->UNIVERSAL::isa('ThriftTest::Xception')) { |
Mark Slee | 3e3d7ad | 2007-05-16 02:35:58 +0000 | [diff] [blame] | 376 | print(' caught xception '.$@->{errorCode}.': '.$@->{message}."\n"); |
| 377 | } |
| 378 | |
| 379 | |
| 380 | # |
| 381 | # Normal tests done. |
| 382 | # |
| 383 | my $stop = gettimeofday(); |
| 384 | my $elp = sprintf("%d",1000*($stop - $start), 0); |
| 385 | print("Total time: $elp ms\n"); |
| 386 | |
| 387 | # |
| 388 | # Extraneous "I don't trust PHP to pack/unpack integer" tests |
| 389 | # |
| 390 | |
| 391 | # Max I32 |
| 392 | my $num = 2**30 + 2**30 - 1; |
| 393 | my $num2 = $testClient->testI32($num); |
| 394 | if ($num != $num2) { |
| 395 | print "Missed max32 $num = $num2\n"; |
| 396 | } |
| 397 | |
| 398 | # Min I32 |
| 399 | $num = 0 - 2**31; |
| 400 | $num2 = $testClient->testI32($num); |
| 401 | if ($num != $num2) { |
| 402 | print "Missed min32 $num = $num2\n"; |
| 403 | } |
| 404 | |
| 405 | # Max Number I can get out of my perl |
| 406 | $num = 2**40; |
| 407 | $num2 = $testClient->testI64($num); |
| 408 | if ($num != $num2) { |
| 409 | print "Missed max64 $num = $num2\n"; |
| 410 | } |
| 411 | |
| 412 | # Max Number I can get out of my perl |
| 413 | $num = 0 - 2**40; |
| 414 | $num2 = $testClient->testI64($num); |
| 415 | if ($num != $num2) { |
| 416 | print "Missed min64 $num = $num2\n"; |
| 417 | } |
| 418 | |
| 419 | $transport->close(); |
| 420 | |
| 421 | |
| 422 | |