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