blob: 9d4aab2eb6889ab5c774378a504efbf7148bd338 [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;
Mark Slee3e3d7ad2007-05-16 02:35:58 +000038
T Jake Luciani41687fc2008-12-23 03:45:43 +000039use ThriftTest::ThriftTest;
Mark Slee27ed6ec2007-08-16 01:26:31 +000040use ThriftTest::Types;
Mark Slee3e3d7ad2007-05-16 02:35:58 +000041
42$|++;
43
Jim Kingf5f1b352015-06-24 13:47:24 -040044sub usage {
45 print <<EOF;
46Usage: $0 [OPTIONS]
Mark Slee3e3d7ad2007-05-16 02:35:58 +000047
Jim Kingf5f1b352015-06-24 13:47:24 -040048Options: (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
57EOF
Roger Meier41ad4342015-03-24 22:30:40 +010058}
Mark Slee3e3d7ad2007-05-16 02:35:58 +000059
Jim Kingf5f1b352015-06-24 13:47:24 -040060my %opts = (
61 'port' => 9090,
62 'protocol' => 'binary',
63 'transport' => 'buffered'
64);
Mark Slee3e3d7ad2007-05-16 02:35:58 +000065
Jim Kingf5f1b352015-06-24 13:47:24 -040066GetOptions(\%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
76if ($opts{help}) {
77 usage();
78 exit 0;
79}
80
81if ($opts{ssl} and not defined $opts{cert}) {
82 usage();
83 exit 1;
84}
85
86my $socket = undef;
87if ($opts{ssl}) {
88 $socket = new Thrift::SSLSocket($opts{host}, $opts{port});
89} else {
90 $socket = new Thrift::Socket($opts{host}, $opts{port});
91}
92
93my $transport;
94if ($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
103my $protocol;
104if ($opts{protocol} eq 'binary') {
105 $protocol = new Thrift::BinaryProtocol($transport);
106} else {
107 usage();
108 exit 1;
109}
110
T Jake Luciani41687fc2008-12-23 03:45:43 +0000111my $testClient = new ThriftTest::ThriftTestClient($protocol);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000112
Jim Kingf5f1b352015-06-24 13:47:24 -0400113eval {
114 $transport->open();
115};
116if($@){
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000117 die(Dumper($@));
118}
119my $start = gettimeofday();
120
121#
122# VOID TEST
123#
124print("testVoid()");
125$testClient->testVoid();
126print(" = void\n");
127
128#
129# STRING TEST
130#
131print("testString(\"Test\")");
132my $s = $testClient->testString("Test");
133print(" = \"$s\"\n");
134
135#
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900136# BOOL TEST
137#
138print("testBool(1)");
Jens Geyerd629ea02015-09-23 21:16:50 +0200139my $t = $testClient->testBool(1);
140print(" = $t\n");
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900141print("testBool(0)");
Jens Geyerd629ea02015-09-23 21:16:50 +0200142my $f = $testClient->testBool(0);
143print(" = $f\n");
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +0900144
145
146#
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000147# BYTE TEST
148#
149print("testByte(1)");
150my $u8 = $testClient->testByte(1);
151print(" = $u8\n");
152
153#
154# I32 TEST
155#
156print("testI32(-1)");
157my $i32 = $testClient->testI32(-1);
158print(" = $i32\n");
159
160#
161#I64 TEST
162#
163print("testI64(-34359738368)");
164my $i64 = $testClient->testI64(-34359738368);
165print(" = $i64\n");
166
167#
168# DOUBLE TEST
169#
170print("testDouble(-852.234234234)");
171my $dub = $testClient->testDouble(-852.234234234);
172print(" = $dub\n");
173
174#
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100175# BINARY TEST --- TODO
176#
177
178
179#
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000180# STRUCT TEST
181#
182print("testStruct({\"Zero\", 1, -3, -5})");
Mark Slee82664432007-09-19 06:49:30 +0000183my $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 Slee3e3d7ad2007-05-16 02:35:58 +0000188my $in = $testClient->testStruct($out);
Mark Slee82664432007-09-19 06:49:30 +0000189print(" = {\"".$in->string_thing."\", ".
190 $in->byte_thing.", ".
191 $in->i32_thing.", ".
192 $in->i64_thing."}\n");
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000193
194#
195# NESTED STRUCT TEST
196#
197print("testNest({1, {\"Zero\", 1, -3, -5}, 5}");
Mark Slee82664432007-09-19 06:49:30 +0000198my $out2 = new ThriftTest::Xtruct2();
199$out2->byte_thing(1);
200$out2->struct_thing($out);
201$out2->i32_thing(5);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000202my $in2 = $testClient->testNest($out2);
Mark Slee82664432007-09-19 06:49:30 +0000203$in = $in2->struct_thing;
204print(" = {".$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 Slee3e3d7ad2007-05-16 02:35:58 +0000210
211#
212# MAP TEST
213#
214my $mapout = {};
215for (my $i = 0; $i < 5; ++$i) {
216 $mapout->{$i} = $i-10;
217}
218print("testMap({");
219my $first = 1;
220while( my($key,$val) = each %$mapout) {
221 if ($first) {
222 $first = 0;
223 } else {
224 print(", ");
225 }
226 print("$key => $val");
227}
228print("})");
229
230
231my $mapin = $testClient->testMap($mapout);
232print(" = {");
233
234$first = 1;
235while( my($key,$val) = each %$mapin){
236 if ($first) {
237 $first = 0;
238 } else {
239 print(", ");
240 }
241 print("$key => $val");
242}
243print("}\n");
244
245#
246# SET TEST
247#
248my $setout = [];
249for (my $i = -2; $i < 3; ++$i) {
250 push(@$setout, $i);
251}
252
253print("testSet({".join(",",@$setout)."})");
254
255my $setin = $testClient->testSet($setout);
256
257print(" = {".join(",",@$setout)."}\n");
258
259#
260# LIST TEST
261#
262my $listout = [];
263for (my $i = -2; $i < 3; ++$i) {
264 push(@$listout, $i);
265}
266
267print("testList({".join(",",@$listout)."})");
268
269my $listin = $testClient->testList($listout);
270
271print(" = {".join(",",@$listin)."}\n");
272
273#
274# ENUM TEST
275#
276print("testEnum(ONE)");
T Jake Luciani41687fc2008-12-23 03:45:43 +0000277my $ret = $testClient->testEnum(ThriftTest::Numberz::ONE);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000278print(" = $ret\n");
279
280print("testEnum(TWO)");
T Jake Luciani41687fc2008-12-23 03:45:43 +0000281$ret = $testClient->testEnum(ThriftTest::Numberz::TWO);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000282print(" = $ret\n");
283
284print("testEnum(THREE)");
T Jake Luciani41687fc2008-12-23 03:45:43 +0000285$ret = $testClient->testEnum(ThriftTest::Numberz::THREE);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000286print(" = $ret\n");
287
288print("testEnum(FIVE)");
T Jake Luciani41687fc2008-12-23 03:45:43 +0000289$ret = $testClient->testEnum(ThriftTest::Numberz::FIVE);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000290print(" = $ret\n");
291
292print("testEnum(EIGHT)");
T Jake Luciani41687fc2008-12-23 03:45:43 +0000293$ret = $testClient->testEnum(ThriftTest::Numberz::EIGHT);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000294print(" = $ret\n");
295
296#
297# TYPEDEF TEST
298#
299print("testTypedef(309858235082523)");
300my $uid = $testClient->testTypedef(309858235082523);
301print(" = $uid\n");
302
303#
304# NESTED MAP TEST
305#
306print("testMapMap(1)");
307my $mm = $testClient->testMapMap(1);
308print(" = {");
309while( my ($key,$val) = each %$mm) {
310 print("$key => {");
311 while( my($k2,$v2) = each %$val) {
312 print("$k2 => $v2, ");
313 }
314 print("}, ");
315}
316print("}\n");
317
318#
319# INSANITY TEST
320#
Mark Slee82664432007-09-19 06:49:30 +0000321my $insane = new ThriftTest::Insanity();
T Jake Luciani41687fc2008-12-23 03:45:43 +0000322$insane->{userMap}->{ThriftTest::Numberz::FIVE} = 5000;
Mark Slee82664432007-09-19 06:49:30 +0000323my $truck = new ThriftTest::Xtruct();
Jens Geyerd629ea02015-09-23 21:16:50 +0200324$truck->string_thing("Hello2");
325$truck->byte_thing(2);
326$truck->i32_thing(2);
327$truck->i64_thing(2);
328my $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 Slee3e3d7ad2007-05-16 02:35:58 +0000333push(@{$insane->{xtructs}}, $truck);
Jens Geyerd629ea02015-09-23 21:16:50 +0200334push(@{$insane->{xtructs}}, $truck2);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000335
336print("testInsanity()");
337my $whoa = $testClient->testInsanity($insane);
338print(" = {");
339while( 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}
366print("}\n");
367
368#
369# EXCEPTION TEST
370#
371print("testException('Xception')");
372eval {
373 $testClient->testException('Xception');
374 print(" void\nFAILURE\n");
Mark Slee82664432007-09-19 06:49:30 +0000375}; if($@ && $@->UNIVERSAL::isa('ThriftTest::Xception')) {
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000376 print(' caught xception '.$@->{errorCode}.': '.$@->{message}."\n");
377}
378
379
380#
381# Normal tests done.
382#
383my $stop = gettimeofday();
384my $elp = sprintf("%d",1000*($stop - $start), 0);
385print("Total time: $elp ms\n");
386
387#
388# Extraneous "I don't trust PHP to pack/unpack integer" tests
389#
390
391# Max I32
392my $num = 2**30 + 2**30 - 1;
393my $num2 = $testClient->testI32($num);
394if ($num != $num2) {
395 print "Missed max32 $num = $num2\n";
396}
397
398# Min I32
399$num = 0 - 2**31;
400$num2 = $testClient->testI32($num);
401if ($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);
408if ($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);
415if ($num != $num2) {
416 print "Missed min64 $num = $num2\n";
417}
418
419$transport->close();
420
421
422