blob: 6f3cbc9719cf680df8c3359f4b9fe1ef10418612 [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;
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)
James E. King, III377719c2017-02-15 14:33:20 -050050 --ca CA to validate server with.
Jim Kingf5f1b352015-06-24 13:47:24 -040051 --cert Certificate to use.
52 Required if using --ssl.
James E. King, III377719c2017-02-15 14:33:20 -050053 --ciphers Acceptable cipher list.
James E. King, III49f4dc02015-10-29 15:52:23 -040054 --domain-socket <file> Use a unix domain socket.
Jim Kingf5f1b352015-06-24 13:47:24 -040055 --help Show usage.
James E. King, III377719c2017-02-15 14:33:20 -050056 --key Certificate key.
57 Required if using --ssl.
Jim Kingf5f1b352015-06-24 13:47:24 -040058 --port <portnum> 9090 Port to use.
59 --protocol {binary} binary Protocol to use.
60 --ssl If present, use SSL.
61 --transport {buffered|framed} buffered Transport to use.
James E. King, III377719c2017-02-15 14:33:20 -050062
Jim Kingf5f1b352015-06-24 13:47:24 -040063EOF
Roger Meier41ad4342015-03-24 22:30:40 +010064}
Mark Slee3e3d7ad2007-05-16 02:35:58 +000065
Jim Kingf5f1b352015-06-24 13:47:24 -040066my %opts = (
67 'port' => 9090,
68 'protocol' => 'binary',
69 'transport' => 'buffered'
70);
Mark Slee3e3d7ad2007-05-16 02:35:58 +000071
Jim Kingf5f1b352015-06-24 13:47:24 -040072GetOptions(\%opts, qw (
James E. King, III377719c2017-02-15 14:33:20 -050073 ca=s
Jim Kingf5f1b352015-06-24 13:47:24 -040074 cert=s
James E. King, III377719c2017-02-15 14:33:20 -050075 ciphers=s
76 key=s
James E. King, III49f4dc02015-10-29 15:52:23 -040077 domain-socket=s
Jim Kingf5f1b352015-06-24 13:47:24 -040078 help
79 host=s
80 port=i
81 protocol=s
82 ssl
83 transport=s
84)) || exit 1;
85
86if ($opts{help}) {
87 usage();
88 exit 0;
89}
90
Jim Kingf5f1b352015-06-24 13:47:24 -040091my $socket = undef;
James E. King, III49f4dc02015-10-29 15:52:23 -040092if ($opts{"domain-socket"}) {
93 $socket = new Thrift::UnixSocket($opts{"domain-socket"});
94} elsif ($opts{ssl}) {
James E. King, III377719c2017-02-15 14:33:20 -050095 $socket = new Thrift::SSLSocket(\%opts);
Jim Kingf5f1b352015-06-24 13:47:24 -040096} else {
James E. King, III377719c2017-02-15 14:33:20 -050097 $socket = new Thrift::Socket($opts{host}, $opts{port});
Jim Kingf5f1b352015-06-24 13:47:24 -040098}
99
100my $transport;
101if ($opts{transport} eq 'buffered') {
102 $transport = new Thrift::BufferedTransport($socket, 1024, 1024);
103} elsif ($opts{transport} eq 'framed') {
104 $transport = new Thrift::FramedTransport($socket);
105} else {
106 usage();
107 exit 1;
108}
109
110my $protocol;
111if ($opts{protocol} eq 'binary') {
112 $protocol = new Thrift::BinaryProtocol($transport);
113} else {
114 usage();
115 exit 1;
116}
117
T Jake Luciani41687fc2008-12-23 03:45:43 +0000118my $testClient = new ThriftTest::ThriftTestClient($protocol);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000119
Jim Kingf5f1b352015-06-24 13:47:24 -0400120eval {
121 $transport->open();
James E. King, III377719c2017-02-15 14:33:20 -0500122};
Jim Kingf5f1b352015-06-24 13:47:24 -0400123if($@){
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000124 die(Dumper($@));
125}
126my $start = gettimeofday();
127
128#
129# VOID TEST
130#
131print("testVoid()");
132$testClient->testVoid();
133print(" = void\n");
134
135#
136# STRING TEST
137#
138print("testString(\"Test\")");
139my $s = $testClient->testString("Test");
140print(" = \"$s\"\n");
141
142#
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900143# BOOL TEST
144#
145print("testBool(1)");
Jens Geyerd629ea02015-09-23 21:16:50 +0200146my $t = $testClient->testBool(1);
147print(" = $t\n");
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900148print("testBool(0)");
Jens Geyerd629ea02015-09-23 21:16:50 +0200149my $f = $testClient->testBool(0);
150print(" = $f\n");
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900151
152
153#
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000154# BYTE TEST
155#
156print("testByte(1)");
157my $u8 = $testClient->testByte(1);
158print(" = $u8\n");
159
160#
161# I32 TEST
162#
163print("testI32(-1)");
164my $i32 = $testClient->testI32(-1);
165print(" = $i32\n");
166
167#
168#I64 TEST
169#
170print("testI64(-34359738368)");
171my $i64 = $testClient->testI64(-34359738368);
172print(" = $i64\n");
173
174#
175# DOUBLE TEST
176#
177print("testDouble(-852.234234234)");
178my $dub = $testClient->testDouble(-852.234234234);
179print(" = $dub\n");
180
181#
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100182# BINARY TEST --- TODO
183#
184
185
186#
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000187# STRUCT TEST
188#
189print("testStruct({\"Zero\", 1, -3, -5})");
Mark Slee82664432007-09-19 06:49:30 +0000190my $out = new ThriftTest::Xtruct();
191$out->string_thing("Zero");
192$out->byte_thing(1);
193$out->i32_thing(-3);
194$out->i64_thing(-5);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000195my $in = $testClient->testStruct($out);
Mark Slee82664432007-09-19 06:49:30 +0000196print(" = {\"".$in->string_thing."\", ".
197 $in->byte_thing.", ".
198 $in->i32_thing.", ".
199 $in->i64_thing."}\n");
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000200
201#
202# NESTED STRUCT TEST
203#
204print("testNest({1, {\"Zero\", 1, -3, -5}, 5}");
Mark Slee82664432007-09-19 06:49:30 +0000205my $out2 = new ThriftTest::Xtruct2();
206$out2->byte_thing(1);
207$out2->struct_thing($out);
208$out2->i32_thing(5);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000209my $in2 = $testClient->testNest($out2);
Mark Slee82664432007-09-19 06:49:30 +0000210$in = $in2->struct_thing;
211print(" = {".$in2->byte_thing.", {\"".
212 $in->string_thing."\", ".
213 $in->byte_thing.", ".
214 $in->i32_thing.", ".
215 $in->i64_thing."}, ".
216 $in2->i32_thing."}\n");
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000217
218#
219# MAP TEST
220#
221my $mapout = {};
222for (my $i = 0; $i < 5; ++$i) {
223 $mapout->{$i} = $i-10;
224}
225print("testMap({");
226my $first = 1;
227while( my($key,$val) = each %$mapout) {
228 if ($first) {
229 $first = 0;
230 } else {
231 print(", ");
232 }
233 print("$key => $val");
234}
235print("})");
236
237
238my $mapin = $testClient->testMap($mapout);
239print(" = {");
240
241$first = 1;
242while( my($key,$val) = each %$mapin){
243 if ($first) {
244 $first = 0;
245 } else {
246 print(", ");
247 }
248 print("$key => $val");
249}
250print("}\n");
251
252#
253# SET TEST
254#
255my $setout = [];
256for (my $i = -2; $i < 3; ++$i) {
257 push(@$setout, $i);
258}
259
260print("testSet({".join(",",@$setout)."})");
261
262my $setin = $testClient->testSet($setout);
263
264print(" = {".join(",",@$setout)."}\n");
265
266#
267# LIST TEST
268#
269my $listout = [];
270for (my $i = -2; $i < 3; ++$i) {
271 push(@$listout, $i);
272}
273
274print("testList({".join(",",@$listout)."})");
275
276my $listin = $testClient->testList($listout);
277
278print(" = {".join(",",@$listin)."}\n");
279
280#
281# ENUM TEST
282#
283print("testEnum(ONE)");
T Jake Luciani41687fc2008-12-23 03:45:43 +0000284my $ret = $testClient->testEnum(ThriftTest::Numberz::ONE);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000285print(" = $ret\n");
286
287print("testEnum(TWO)");
T Jake Luciani41687fc2008-12-23 03:45:43 +0000288$ret = $testClient->testEnum(ThriftTest::Numberz::TWO);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000289print(" = $ret\n");
290
291print("testEnum(THREE)");
T Jake Luciani41687fc2008-12-23 03:45:43 +0000292$ret = $testClient->testEnum(ThriftTest::Numberz::THREE);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000293print(" = $ret\n");
294
295print("testEnum(FIVE)");
T Jake Luciani41687fc2008-12-23 03:45:43 +0000296$ret = $testClient->testEnum(ThriftTest::Numberz::FIVE);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000297print(" = $ret\n");
298
299print("testEnum(EIGHT)");
T Jake Luciani41687fc2008-12-23 03:45:43 +0000300$ret = $testClient->testEnum(ThriftTest::Numberz::EIGHT);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000301print(" = $ret\n");
302
303#
304# TYPEDEF TEST
305#
306print("testTypedef(309858235082523)");
307my $uid = $testClient->testTypedef(309858235082523);
308print(" = $uid\n");
309
310#
311# NESTED MAP TEST
312#
313print("testMapMap(1)");
314my $mm = $testClient->testMapMap(1);
315print(" = {");
316while( my ($key,$val) = each %$mm) {
317 print("$key => {");
318 while( my($k2,$v2) = each %$val) {
319 print("$k2 => $v2, ");
320 }
321 print("}, ");
322}
323print("}\n");
324
325#
326# INSANITY TEST
327#
Mark Slee82664432007-09-19 06:49:30 +0000328my $insane = new ThriftTest::Insanity();
T Jake Luciani41687fc2008-12-23 03:45:43 +0000329$insane->{userMap}->{ThriftTest::Numberz::FIVE} = 5000;
Mark Slee82664432007-09-19 06:49:30 +0000330my $truck = new ThriftTest::Xtruct();
Jens Geyerd629ea02015-09-23 21:16:50 +0200331$truck->string_thing("Hello2");
332$truck->byte_thing(2);
333$truck->i32_thing(2);
334$truck->i64_thing(2);
335my $truck2 = new ThriftTest::Xtruct();
336$truck2->string_thing("Goodbye4");
337$truck2->byte_thing(4);
338$truck2->i32_thing(4);
339$truck2->i64_thing(4);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000340push(@{$insane->{xtructs}}, $truck);
Jens Geyerd629ea02015-09-23 21:16:50 +0200341push(@{$insane->{xtructs}}, $truck2);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000342
343print("testInsanity()");
344my $whoa = $testClient->testInsanity($insane);
345print(" = {");
346while( my ($key,$val) = each %$whoa) {
347 print("$key => {");
348 while( my($k2,$v2) = each %$val) {
349 print("$k2 => {");
350 my $userMap = $v2->{userMap};
351 print("{");
352 if (ref($userMap) eq "HASH") {
353 while( my($k3,$v3) = each %$userMap) {
354 print("$k3 => $v3, ");
355 }
356 }
357 print("}, ");
358
359 my $xtructs = $v2->{xtructs};
360 print("{");
361 if (ref($xtructs) eq "ARRAY") {
362 foreach my $x (@$xtructs) {
363 print("{\"".$x->{string_thing}."\", ".
364 $x->{byte_thing}.", ".$x->{i32_thing}.", ".$x->{i64_thing}."}, ");
365 }
366 }
367 print("}");
368
369 print("}, ");
370 }
371 print("}, ");
372}
373print("}\n");
374
375#
376# EXCEPTION TEST
377#
378print("testException('Xception')");
379eval {
380 $testClient->testException('Xception');
381 print(" void\nFAILURE\n");
Mark Slee82664432007-09-19 06:49:30 +0000382}; if($@ && $@->UNIVERSAL::isa('ThriftTest::Xception')) {
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000383 print(' caught xception '.$@->{errorCode}.': '.$@->{message}."\n");
384}
385
386
387#
388# Normal tests done.
389#
390my $stop = gettimeofday();
391my $elp = sprintf("%d",1000*($stop - $start), 0);
392print("Total time: $elp ms\n");
393
394#
395# Extraneous "I don't trust PHP to pack/unpack integer" tests
396#
397
398# Max I32
399my $num = 2**30 + 2**30 - 1;
400my $num2 = $testClient->testI32($num);
401if ($num != $num2) {
402 print "Missed max32 $num = $num2\n";
403}
404
405# Min I32
406$num = 0 - 2**31;
407$num2 = $testClient->testI32($num);
408if ($num != $num2) {
409 print "Missed min32 $num = $num2\n";
410}
411
412# Max Number I can get out of my perl
413$num = 2**40;
414$num2 = $testClient->testI64($num);
415if ($num != $num2) {
416 print "Missed max64 $num = $num2\n";
417}
418
419# Max Number I can get out of my perl
420$num = 0 - 2**40;
421$num2 = $testClient->testI64($num);
422if ($num != $num2) {
423 print "Missed min64 $num = $num2\n";
424}
425
426$transport->close();
427
428
429