blob: 990274c5eb2118ba54cc6d658356247e473cefed [file] [log] [blame]
T Jake Luciani41687fc2008-12-23 03:45:43 +00001#!/usr/bin/env perl
Mark Slee3e3d7ad2007-05-16 02:35:58 +00002
David Reissea2cba82009-03-30 21:35:00 +00003#
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
James E. King, III177c37c2017-03-30 17:09:04 -040022use 5.10.0;
Mark Slee3e3d7ad2007-05-16 02:35:58 +000023use strict;
24use warnings;
25use Data::Dumper;
Jim Kingf5f1b352015-06-24 13:47:24 -040026use Getopt::Long qw(GetOptions);
Mark Slee3e3d7ad2007-05-16 02:35:58 +000027use Time::HiRes qw(gettimeofday);
28
29use lib '../../lib/perl/lib';
T Jake Luciani41687fc2008-12-23 03:45:43 +000030use lib 'gen-perl';
Mark Slee3e3d7ad2007-05-16 02:35:58 +000031
32use Thrift;
33use Thrift::BinaryProtocol;
Mark Slee3e3d7ad2007-05-16 02:35:58 +000034use Thrift::BufferedTransport;
Jim Kingf5f1b352015-06-24 13:47:24 -040035use Thrift::FramedTransport;
James E. King, III20e16bc2017-11-18 22:37:54 -050036use Thrift::MultiplexedProtocol;
Jim Kingf5f1b352015-06-24 13:47:24 -040037use Thrift::SSLSocket;
38use Thrift::Socket;
James E. King, III49f4dc02015-10-29 15:52:23 -040039use Thrift::UnixSocket;
Mark Slee3e3d7ad2007-05-16 02:35:58 +000040
James E. King, III20e16bc2017-11-18 22:37:54 -050041use ThriftTest::SecondService;
T Jake Luciani41687fc2008-12-23 03:45:43 +000042use ThriftTest::ThriftTest;
Mark Slee27ed6ec2007-08-16 01:26:31 +000043use ThriftTest::Types;
Mark Slee3e3d7ad2007-05-16 02:35:58 +000044
45$|++;
46
Jim Kingf5f1b352015-06-24 13:47:24 -040047sub usage {
48 print <<EOF;
49Usage: $0 [OPTIONS]
Mark Slee3e3d7ad2007-05-16 02:35:58 +000050
Jim Kingf5f1b352015-06-24 13:47:24 -040051Options: (default)
James E. King, III377719c2017-02-15 14:33:20 -050052 --ca CA to validate server with.
Jim Kingf5f1b352015-06-24 13:47:24 -040053 --cert Certificate to use.
54 Required if using --ssl.
James E. King, III377719c2017-02-15 14:33:20 -050055 --ciphers Acceptable cipher list.
James E. King, III49f4dc02015-10-29 15:52:23 -040056 --domain-socket <file> Use a unix domain socket.
Jim Kingf5f1b352015-06-24 13:47:24 -040057 --help Show usage.
James E. King, III377719c2017-02-15 14:33:20 -050058 --key Certificate key.
59 Required if using --ssl.
Jim Kingf5f1b352015-06-24 13:47:24 -040060 --port <portnum> 9090 Port to use.
61 --protocol {binary} binary Protocol to use.
62 --ssl If present, use SSL.
63 --transport {buffered|framed} buffered Transport to use.
James E. King, III377719c2017-02-15 14:33:20 -050064
Jim Kingf5f1b352015-06-24 13:47:24 -040065EOF
Roger Meier41ad4342015-03-24 22:30:40 +010066}
Mark Slee3e3d7ad2007-05-16 02:35:58 +000067
Jim Kingf5f1b352015-06-24 13:47:24 -040068my %opts = (
69 'port' => 9090,
70 'protocol' => 'binary',
71 'transport' => 'buffered'
72);
Mark Slee3e3d7ad2007-05-16 02:35:58 +000073
Jim Kingf5f1b352015-06-24 13:47:24 -040074GetOptions(\%opts, qw (
James E. King, III377719c2017-02-15 14:33:20 -050075 ca=s
Jim Kingf5f1b352015-06-24 13:47:24 -040076 cert=s
James E. King, III377719c2017-02-15 14:33:20 -050077 ciphers=s
78 key=s
James E. King, III49f4dc02015-10-29 15:52:23 -040079 domain-socket=s
Jim Kingf5f1b352015-06-24 13:47:24 -040080 help
81 host=s
82 port=i
83 protocol=s
84 ssl
85 transport=s
86)) || exit 1;
87
88if ($opts{help}) {
89 usage();
90 exit 0;
91}
92
Jim Kingf5f1b352015-06-24 13:47:24 -040093my $socket = undef;
James E. King, III49f4dc02015-10-29 15:52:23 -040094if ($opts{"domain-socket"}) {
95 $socket = new Thrift::UnixSocket($opts{"domain-socket"});
96} elsif ($opts{ssl}) {
James E. King, III377719c2017-02-15 14:33:20 -050097 $socket = new Thrift::SSLSocket(\%opts);
Jim Kingf5f1b352015-06-24 13:47:24 -040098} else {
James E. King, III377719c2017-02-15 14:33:20 -050099 $socket = new Thrift::Socket($opts{host}, $opts{port});
Jim Kingf5f1b352015-06-24 13:47:24 -0400100}
101
102my $transport;
103if ($opts{transport} eq 'buffered') {
104 $transport = new Thrift::BufferedTransport($socket, 1024, 1024);
105} elsif ($opts{transport} eq 'framed') {
106 $transport = new Thrift::FramedTransport($socket);
107} else {
108 usage();
109 exit 1;
110}
111
112my $protocol;
James E. King, III20e16bc2017-11-18 22:37:54 -0500113my $protocol2;
114if ($opts{protocol} eq 'binary' || $opts{protocol} eq 'multi') {
Jim Kingf5f1b352015-06-24 13:47:24 -0400115 $protocol = new Thrift::BinaryProtocol($transport);
116} else {
117 usage();
118 exit 1;
119}
120
James E. King, III20e16bc2017-11-18 22:37:54 -0500121my $secondService = undef;
122if (index($opts{protocol}, 'multi') == 0) {
123 $protocol2 = new Thrift::MultiplexedProtocol($protocol, "SecondService");
124 $protocol = new Thrift::MultiplexedProtocol($protocol, "ThriftTest");
125 $secondService = new ThriftTest::SecondServiceClient($protocol2);
126}
127
T Jake Luciani41687fc2008-12-23 03:45:43 +0000128my $testClient = new ThriftTest::ThriftTestClient($protocol);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000129
Jim Kingf5f1b352015-06-24 13:47:24 -0400130eval {
131 $transport->open();
James E. King, III377719c2017-02-15 14:33:20 -0500132};
Jim Kingf5f1b352015-06-24 13:47:24 -0400133if($@){
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000134 die(Dumper($@));
135}
James E. King, III20e16bc2017-11-18 22:37:54 -0500136
137use constant ERR_BASETYPES => 1;
138use constant ERR_STRUCTS => 2;
139use constant ERR_CONTAINERS => 4;
140use constant ERR_EXCEPTIONS => 8;
141use constant ERR_PROTOCOL => 16;
142use constant ERR_UNKNOWN => 64;
143
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000144my $start = gettimeofday();
145
146#
147# VOID TEST
148#
149print("testVoid()");
150$testClient->testVoid();
151print(" = void\n");
152
153#
154# STRING TEST
155#
156print("testString(\"Test\")");
157my $s = $testClient->testString("Test");
158print(" = \"$s\"\n");
James E. King, III20e16bc2017-11-18 22:37:54 -0500159exit(ERR_BASETYPES) if ($s ne 'Test');
160
161#
162# MULTIPLEXED TEST
163#
164if (index($opts{protocol}, 'multi') == 0) {
165 print("secondtestString(\"Test2\")");
166 $s = $secondService->secondtestString("Test2");
167 print(" = \"$s\"\n");
168 exit(ERR_PROTOCOL) if ($s ne 'testString("Test2")');
169}
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000170
171#
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900172# BOOL TEST
173#
174print("testBool(1)");
Jens Geyerd629ea02015-09-23 21:16:50 +0200175my $t = $testClient->testBool(1);
176print(" = $t\n");
James E. King, III20e16bc2017-11-18 22:37:54 -0500177exit(ERR_BASETYPES) if ($t ne 1);
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900178print("testBool(0)");
Jens Geyerd629ea02015-09-23 21:16:50 +0200179my $f = $testClient->testBool(0);
180print(" = $f\n");
James E. King, III20e16bc2017-11-18 22:37:54 -0500181exit(ERR_BASETYPES) if ($f ne "");
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900182
183
184#
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000185# BYTE TEST
186#
187print("testByte(1)");
188my $u8 = $testClient->testByte(1);
189print(" = $u8\n");
190
191#
192# I32 TEST
193#
194print("testI32(-1)");
195my $i32 = $testClient->testI32(-1);
196print(" = $i32\n");
James E. King, III20e16bc2017-11-18 22:37:54 -0500197exit(ERR_BASETYPES) if ($i32 ne -1);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000198
199#
James E. King, III20e16bc2017-11-18 22:37:54 -0500200# I64 TEST
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000201#
202print("testI64(-34359738368)");
203my $i64 = $testClient->testI64(-34359738368);
204print(" = $i64\n");
James E. King, III20e16bc2017-11-18 22:37:54 -0500205exit(ERR_BASETYPES) if ($i64 ne -34359738368);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000206
207#
208# DOUBLE TEST
209#
210print("testDouble(-852.234234234)");
211my $dub = $testClient->testDouble(-852.234234234);
212print(" = $dub\n");
James E. King, III20e16bc2017-11-18 22:37:54 -0500213exit(ERR_BASETYPES) if ($dub ne -852.234234234);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000214
215#
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100216# BINARY TEST --- TODO
217#
218
219
220#
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000221# STRUCT TEST
222#
223print("testStruct({\"Zero\", 1, -3, -5})");
Mark Slee82664432007-09-19 06:49:30 +0000224my $out = new ThriftTest::Xtruct();
225$out->string_thing("Zero");
226$out->byte_thing(1);
227$out->i32_thing(-3);
228$out->i64_thing(-5);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000229my $in = $testClient->testStruct($out);
Mark Slee82664432007-09-19 06:49:30 +0000230print(" = {\"".$in->string_thing."\", ".
231 $in->byte_thing.", ".
232 $in->i32_thing.", ".
233 $in->i64_thing."}\n");
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000234
235#
236# NESTED STRUCT TEST
237#
238print("testNest({1, {\"Zero\", 1, -3, -5}, 5}");
Mark Slee82664432007-09-19 06:49:30 +0000239my $out2 = new ThriftTest::Xtruct2();
240$out2->byte_thing(1);
241$out2->struct_thing($out);
242$out2->i32_thing(5);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000243my $in2 = $testClient->testNest($out2);
Mark Slee82664432007-09-19 06:49:30 +0000244$in = $in2->struct_thing;
245print(" = {".$in2->byte_thing.", {\"".
246 $in->string_thing."\", ".
247 $in->byte_thing.", ".
248 $in->i32_thing.", ".
249 $in->i64_thing."}, ".
250 $in2->i32_thing."}\n");
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000251
252#
253# MAP TEST
254#
255my $mapout = {};
256for (my $i = 0; $i < 5; ++$i) {
257 $mapout->{$i} = $i-10;
258}
259print("testMap({");
260my $first = 1;
261while( my($key,$val) = each %$mapout) {
262 if ($first) {
263 $first = 0;
264 } else {
265 print(", ");
266 }
267 print("$key => $val");
268}
269print("})");
270
271
272my $mapin = $testClient->testMap($mapout);
273print(" = {");
274
275$first = 1;
276while( my($key,$val) = each %$mapin){
277 if ($first) {
278 $first = 0;
279 } else {
280 print(", ");
281 }
282 print("$key => $val");
283}
284print("}\n");
285
286#
287# SET TEST
288#
289my $setout = [];
290for (my $i = -2; $i < 3; ++$i) {
291 push(@$setout, $i);
292}
293
294print("testSet({".join(",",@$setout)."})");
295
296my $setin = $testClient->testSet($setout);
297
298print(" = {".join(",",@$setout)."}\n");
299
300#
301# LIST TEST
302#
303my $listout = [];
304for (my $i = -2; $i < 3; ++$i) {
305 push(@$listout, $i);
306}
307
308print("testList({".join(",",@$listout)."})");
309
310my $listin = $testClient->testList($listout);
311
312print(" = {".join(",",@$listin)."}\n");
313
314#
315# ENUM TEST
316#
317print("testEnum(ONE)");
T Jake Luciani41687fc2008-12-23 03:45:43 +0000318my $ret = $testClient->testEnum(ThriftTest::Numberz::ONE);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000319print(" = $ret\n");
320
321print("testEnum(TWO)");
T Jake Luciani41687fc2008-12-23 03:45:43 +0000322$ret = $testClient->testEnum(ThriftTest::Numberz::TWO);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000323print(" = $ret\n");
324
325print("testEnum(THREE)");
T Jake Luciani41687fc2008-12-23 03:45:43 +0000326$ret = $testClient->testEnum(ThriftTest::Numberz::THREE);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000327print(" = $ret\n");
328
329print("testEnum(FIVE)");
T Jake Luciani41687fc2008-12-23 03:45:43 +0000330$ret = $testClient->testEnum(ThriftTest::Numberz::FIVE);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000331print(" = $ret\n");
332
333print("testEnum(EIGHT)");
T Jake Luciani41687fc2008-12-23 03:45:43 +0000334$ret = $testClient->testEnum(ThriftTest::Numberz::EIGHT);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000335print(" = $ret\n");
336
337#
338# TYPEDEF TEST
339#
340print("testTypedef(309858235082523)");
341my $uid = $testClient->testTypedef(309858235082523);
342print(" = $uid\n");
343
344#
345# NESTED MAP TEST
346#
347print("testMapMap(1)");
348my $mm = $testClient->testMapMap(1);
349print(" = {");
350while( my ($key,$val) = each %$mm) {
351 print("$key => {");
352 while( my($k2,$v2) = each %$val) {
353 print("$k2 => $v2, ");
354 }
355 print("}, ");
356}
357print("}\n");
358
359#
360# INSANITY TEST
361#
Mark Slee82664432007-09-19 06:49:30 +0000362my $insane = new ThriftTest::Insanity();
T Jake Luciani41687fc2008-12-23 03:45:43 +0000363$insane->{userMap}->{ThriftTest::Numberz::FIVE} = 5000;
Mark Slee82664432007-09-19 06:49:30 +0000364my $truck = new ThriftTest::Xtruct();
Jens Geyerd629ea02015-09-23 21:16:50 +0200365$truck->string_thing("Hello2");
366$truck->byte_thing(2);
367$truck->i32_thing(2);
368$truck->i64_thing(2);
369my $truck2 = new ThriftTest::Xtruct();
370$truck2->string_thing("Goodbye4");
371$truck2->byte_thing(4);
372$truck2->i32_thing(4);
373$truck2->i64_thing(4);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000374push(@{$insane->{xtructs}}, $truck);
Jens Geyerd629ea02015-09-23 21:16:50 +0200375push(@{$insane->{xtructs}}, $truck2);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000376
377print("testInsanity()");
378my $whoa = $testClient->testInsanity($insane);
379print(" = {");
380while( my ($key,$val) = each %$whoa) {
381 print("$key => {");
382 while( my($k2,$v2) = each %$val) {
383 print("$k2 => {");
384 my $userMap = $v2->{userMap};
385 print("{");
386 if (ref($userMap) eq "HASH") {
387 while( my($k3,$v3) = each %$userMap) {
388 print("$k3 => $v3, ");
389 }
390 }
391 print("}, ");
392
393 my $xtructs = $v2->{xtructs};
394 print("{");
395 if (ref($xtructs) eq "ARRAY") {
396 foreach my $x (@$xtructs) {
397 print("{\"".$x->{string_thing}."\", ".
398 $x->{byte_thing}.", ".$x->{i32_thing}.", ".$x->{i64_thing}."}, ");
399 }
400 }
401 print("}");
402
403 print("}, ");
404 }
405 print("}, ");
406}
407print("}\n");
408
409#
410# EXCEPTION TEST
411#
412print("testException('Xception')");
413eval {
414 $testClient->testException('Xception');
415 print(" void\nFAILURE\n");
Mark Slee82664432007-09-19 06:49:30 +0000416}; if($@ && $@->UNIVERSAL::isa('ThriftTest::Xception')) {
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000417 print(' caught xception '.$@->{errorCode}.': '.$@->{message}."\n");
418}
419
420
421#
422# Normal tests done.
423#
424my $stop = gettimeofday();
425my $elp = sprintf("%d",1000*($stop - $start), 0);
426print("Total time: $elp ms\n");
427
428#
429# Extraneous "I don't trust PHP to pack/unpack integer" tests
430#
431
432# Max I32
433my $num = 2**30 + 2**30 - 1;
434my $num2 = $testClient->testI32($num);
435if ($num != $num2) {
436 print "Missed max32 $num = $num2\n";
437}
438
439# Min I32
440$num = 0 - 2**31;
441$num2 = $testClient->testI32($num);
442if ($num != $num2) {
443 print "Missed min32 $num = $num2\n";
444}
445
446# Max Number I can get out of my perl
447$num = 2**40;
448$num2 = $testClient->testI64($num);
449if ($num != $num2) {
450 print "Missed max64 $num = $num2\n";
451}
452
453# Max Number I can get out of my perl
454$num = 0 - 2**40;
455$num2 = $testClient->testI64($num);
456if ($num != $num2) {
457 print "Missed min64 $num = $num2\n";
458}
459
460$transport->close();
461
462
463