blob: 7ec32bf39e5bcf74a86e2cc17c384cb891116e98 [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
Mark Slee3e3d7ad2007-05-16 02:35:58 +000022require 5.6.0;
23use 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;
36use Thrift::SSLSocket;
37use Thrift::Socket;
James E. King, III49f4dc02015-10-29 15:52:23 -040038use Thrift::UnixSocket;
Mark Slee3e3d7ad2007-05-16 02:35:58 +000039
T Jake Luciani41687fc2008-12-23 03:45:43 +000040use ThriftTest::ThriftTest;
Mark Slee27ed6ec2007-08-16 01:26:31 +000041use ThriftTest::Types;
Mark Slee3e3d7ad2007-05-16 02:35:58 +000042
43$|++;
44
Jim Kingf5f1b352015-06-24 13:47:24 -040045sub usage {
46 print <<EOF;
47Usage: $0 [OPTIONS]
Mark Slee3e3d7ad2007-05-16 02:35:58 +000048
Jim Kingf5f1b352015-06-24 13:47:24 -040049Options: (default)
50 --cert Certificate to use.
51 Required if using --ssl.
James E. King, III49f4dc02015-10-29 15:52:23 -040052 --domain-socket <file> Use a unix domain socket.
Jim Kingf5f1b352015-06-24 13:47:24 -040053 --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
59EOF
Roger Meier41ad4342015-03-24 22:30:40 +010060}
Mark Slee3e3d7ad2007-05-16 02:35:58 +000061
Jim Kingf5f1b352015-06-24 13:47:24 -040062my %opts = (
63 'port' => 9090,
64 'protocol' => 'binary',
65 'transport' => 'buffered'
66);
Mark Slee3e3d7ad2007-05-16 02:35:58 +000067
Jim Kingf5f1b352015-06-24 13:47:24 -040068GetOptions(\%opts, qw (
69 cert=s
James E. King, III49f4dc02015-10-29 15:52:23 -040070 domain-socket=s
Jim Kingf5f1b352015-06-24 13:47:24 -040071 help
72 host=s
73 port=i
74 protocol=s
75 ssl
76 transport=s
77)) || exit 1;
78
79if ($opts{help}) {
80 usage();
81 exit 0;
82}
83
84if ($opts{ssl} and not defined $opts{cert}) {
85 usage();
86 exit 1;
87}
88
89my $socket = undef;
James E. King, III49f4dc02015-10-29 15:52:23 -040090if ($opts{"domain-socket"}) {
91 $socket = new Thrift::UnixSocket($opts{"domain-socket"});
92} elsif ($opts{ssl}) {
Jim Kingf5f1b352015-06-24 13:47:24 -040093 $socket = new Thrift::SSLSocket($opts{host}, $opts{port});
94} else {
95 $socket = new Thrift::Socket($opts{host}, $opts{port});
96}
97
98my $transport;
99if ($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
108my $protocol;
109if ($opts{protocol} eq 'binary') {
110 $protocol = new Thrift::BinaryProtocol($transport);
111} else {
112 usage();
113 exit 1;
114}
115
T Jake Luciani41687fc2008-12-23 03:45:43 +0000116my $testClient = new ThriftTest::ThriftTestClient($protocol);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000117
Jim Kingf5f1b352015-06-24 13:47:24 -0400118eval {
119 $transport->open();
120};
121if($@){
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000122 die(Dumper($@));
123}
124my $start = gettimeofday();
125
126#
127# VOID TEST
128#
129print("testVoid()");
130$testClient->testVoid();
131print(" = void\n");
132
133#
134# STRING TEST
135#
136print("testString(\"Test\")");
137my $s = $testClient->testString("Test");
138print(" = \"$s\"\n");
139
140#
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900141# BOOL TEST
142#
143print("testBool(1)");
Jens Geyerd629ea02015-09-23 21:16:50 +0200144my $t = $testClient->testBool(1);
145print(" = $t\n");
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900146print("testBool(0)");
Jens Geyerd629ea02015-09-23 21:16:50 +0200147my $f = $testClient->testBool(0);
148print(" = $f\n");
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900149
150
151#
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000152# BYTE TEST
153#
154print("testByte(1)");
155my $u8 = $testClient->testByte(1);
156print(" = $u8\n");
157
158#
159# I32 TEST
160#
161print("testI32(-1)");
162my $i32 = $testClient->testI32(-1);
163print(" = $i32\n");
164
165#
166#I64 TEST
167#
168print("testI64(-34359738368)");
169my $i64 = $testClient->testI64(-34359738368);
170print(" = $i64\n");
171
172#
173# DOUBLE TEST
174#
175print("testDouble(-852.234234234)");
176my $dub = $testClient->testDouble(-852.234234234);
177print(" = $dub\n");
178
179#
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100180# BINARY TEST --- TODO
181#
182
183
184#
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000185# STRUCT TEST
186#
187print("testStruct({\"Zero\", 1, -3, -5})");
Mark Slee82664432007-09-19 06:49:30 +0000188my $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 Slee3e3d7ad2007-05-16 02:35:58 +0000193my $in = $testClient->testStruct($out);
Mark Slee82664432007-09-19 06:49:30 +0000194print(" = {\"".$in->string_thing."\", ".
195 $in->byte_thing.", ".
196 $in->i32_thing.", ".
197 $in->i64_thing."}\n");
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000198
199#
200# NESTED STRUCT TEST
201#
202print("testNest({1, {\"Zero\", 1, -3, -5}, 5}");
Mark Slee82664432007-09-19 06:49:30 +0000203my $out2 = new ThriftTest::Xtruct2();
204$out2->byte_thing(1);
205$out2->struct_thing($out);
206$out2->i32_thing(5);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000207my $in2 = $testClient->testNest($out2);
Mark Slee82664432007-09-19 06:49:30 +0000208$in = $in2->struct_thing;
209print(" = {".$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 Slee3e3d7ad2007-05-16 02:35:58 +0000215
216#
217# MAP TEST
218#
219my $mapout = {};
220for (my $i = 0; $i < 5; ++$i) {
221 $mapout->{$i} = $i-10;
222}
223print("testMap({");
224my $first = 1;
225while( my($key,$val) = each %$mapout) {
226 if ($first) {
227 $first = 0;
228 } else {
229 print(", ");
230 }
231 print("$key => $val");
232}
233print("})");
234
235
236my $mapin = $testClient->testMap($mapout);
237print(" = {");
238
239$first = 1;
240while( my($key,$val) = each %$mapin){
241 if ($first) {
242 $first = 0;
243 } else {
244 print(", ");
245 }
246 print("$key => $val");
247}
248print("}\n");
249
250#
251# SET TEST
252#
253my $setout = [];
254for (my $i = -2; $i < 3; ++$i) {
255 push(@$setout, $i);
256}
257
258print("testSet({".join(",",@$setout)."})");
259
260my $setin = $testClient->testSet($setout);
261
262print(" = {".join(",",@$setout)."}\n");
263
264#
265# LIST TEST
266#
267my $listout = [];
268for (my $i = -2; $i < 3; ++$i) {
269 push(@$listout, $i);
270}
271
272print("testList({".join(",",@$listout)."})");
273
274my $listin = $testClient->testList($listout);
275
276print(" = {".join(",",@$listin)."}\n");
277
278#
279# ENUM TEST
280#
281print("testEnum(ONE)");
T Jake Luciani41687fc2008-12-23 03:45:43 +0000282my $ret = $testClient->testEnum(ThriftTest::Numberz::ONE);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000283print(" = $ret\n");
284
285print("testEnum(TWO)");
T Jake Luciani41687fc2008-12-23 03:45:43 +0000286$ret = $testClient->testEnum(ThriftTest::Numberz::TWO);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000287print(" = $ret\n");
288
289print("testEnum(THREE)");
T Jake Luciani41687fc2008-12-23 03:45:43 +0000290$ret = $testClient->testEnum(ThriftTest::Numberz::THREE);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000291print(" = $ret\n");
292
293print("testEnum(FIVE)");
T Jake Luciani41687fc2008-12-23 03:45:43 +0000294$ret = $testClient->testEnum(ThriftTest::Numberz::FIVE);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000295print(" = $ret\n");
296
297print("testEnum(EIGHT)");
T Jake Luciani41687fc2008-12-23 03:45:43 +0000298$ret = $testClient->testEnum(ThriftTest::Numberz::EIGHT);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000299print(" = $ret\n");
300
301#
302# TYPEDEF TEST
303#
304print("testTypedef(309858235082523)");
305my $uid = $testClient->testTypedef(309858235082523);
306print(" = $uid\n");
307
308#
309# NESTED MAP TEST
310#
311print("testMapMap(1)");
312my $mm = $testClient->testMapMap(1);
313print(" = {");
314while( my ($key,$val) = each %$mm) {
315 print("$key => {");
316 while( my($k2,$v2) = each %$val) {
317 print("$k2 => $v2, ");
318 }
319 print("}, ");
320}
321print("}\n");
322
323#
324# INSANITY TEST
325#
Mark Slee82664432007-09-19 06:49:30 +0000326my $insane = new ThriftTest::Insanity();
T Jake Luciani41687fc2008-12-23 03:45:43 +0000327$insane->{userMap}->{ThriftTest::Numberz::FIVE} = 5000;
Mark Slee82664432007-09-19 06:49:30 +0000328my $truck = new ThriftTest::Xtruct();
Jens Geyerd629ea02015-09-23 21:16:50 +0200329$truck->string_thing("Hello2");
330$truck->byte_thing(2);
331$truck->i32_thing(2);
332$truck->i64_thing(2);
333my $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 Slee3e3d7ad2007-05-16 02:35:58 +0000338push(@{$insane->{xtructs}}, $truck);
Jens Geyerd629ea02015-09-23 21:16:50 +0200339push(@{$insane->{xtructs}}, $truck2);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000340
341print("testInsanity()");
342my $whoa = $testClient->testInsanity($insane);
343print(" = {");
344while( 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}
371print("}\n");
372
373#
374# EXCEPTION TEST
375#
376print("testException('Xception')");
377eval {
378 $testClient->testException('Xception');
379 print(" void\nFAILURE\n");
Mark Slee82664432007-09-19 06:49:30 +0000380}; if($@ && $@->UNIVERSAL::isa('ThriftTest::Xception')) {
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000381 print(' caught xception '.$@->{errorCode}.': '.$@->{message}."\n");
382}
383
384
385#
386# Normal tests done.
387#
388my $stop = gettimeofday();
389my $elp = sprintf("%d",1000*($stop - $start), 0);
390print("Total time: $elp ms\n");
391
392#
393# Extraneous "I don't trust PHP to pack/unpack integer" tests
394#
395
396# Max I32
397my $num = 2**30 + 2**30 - 1;
398my $num2 = $testClient->testI32($num);
399if ($num != $num2) {
400 print "Missed max32 $num = $num2\n";
401}
402
403# Min I32
404$num = 0 - 2**31;
405$num2 = $testClient->testI32($num);
406if ($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);
413if ($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);
420if ($num != $num2) {
421 print "Missed min64 $num = $num2\n";
422}
423
424$transport->close();
425
426
427