blob: 0f1ce657707eeb6f6bd660b0d5872e5cb1f2c02f [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#
136# BYTE TEST
137#
138print("testByte(1)");
139my $u8 = $testClient->testByte(1);
140print(" = $u8\n");
141
142#
143# I32 TEST
144#
145print("testI32(-1)");
146my $i32 = $testClient->testI32(-1);
147print(" = $i32\n");
148
149#
150#I64 TEST
151#
152print("testI64(-34359738368)");
153my $i64 = $testClient->testI64(-34359738368);
154print(" = $i64\n");
155
156#
157# DOUBLE TEST
158#
159print("testDouble(-852.234234234)");
160my $dub = $testClient->testDouble(-852.234234234);
161print(" = $dub\n");
162
163#
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100164# BINARY TEST --- TODO
165#
166
167
168#
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000169# STRUCT TEST
170#
171print("testStruct({\"Zero\", 1, -3, -5})");
Mark Slee82664432007-09-19 06:49:30 +0000172my $out = new ThriftTest::Xtruct();
173$out->string_thing("Zero");
174$out->byte_thing(1);
175$out->i32_thing(-3);
176$out->i64_thing(-5);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000177my $in = $testClient->testStruct($out);
Mark Slee82664432007-09-19 06:49:30 +0000178print(" = {\"".$in->string_thing."\", ".
179 $in->byte_thing.", ".
180 $in->i32_thing.", ".
181 $in->i64_thing."}\n");
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000182
183#
184# NESTED STRUCT TEST
185#
186print("testNest({1, {\"Zero\", 1, -3, -5}, 5}");
Mark Slee82664432007-09-19 06:49:30 +0000187my $out2 = new ThriftTest::Xtruct2();
188$out2->byte_thing(1);
189$out2->struct_thing($out);
190$out2->i32_thing(5);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000191my $in2 = $testClient->testNest($out2);
Mark Slee82664432007-09-19 06:49:30 +0000192$in = $in2->struct_thing;
193print(" = {".$in2->byte_thing.", {\"".
194 $in->string_thing."\", ".
195 $in->byte_thing.", ".
196 $in->i32_thing.", ".
197 $in->i64_thing."}, ".
198 $in2->i32_thing."}\n");
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000199
200#
201# MAP TEST
202#
203my $mapout = {};
204for (my $i = 0; $i < 5; ++$i) {
205 $mapout->{$i} = $i-10;
206}
207print("testMap({");
208my $first = 1;
209while( my($key,$val) = each %$mapout) {
210 if ($first) {
211 $first = 0;
212 } else {
213 print(", ");
214 }
215 print("$key => $val");
216}
217print("})");
218
219
220my $mapin = $testClient->testMap($mapout);
221print(" = {");
222
223$first = 1;
224while( my($key,$val) = each %$mapin){
225 if ($first) {
226 $first = 0;
227 } else {
228 print(", ");
229 }
230 print("$key => $val");
231}
232print("}\n");
233
234#
235# SET TEST
236#
237my $setout = [];
238for (my $i = -2; $i < 3; ++$i) {
239 push(@$setout, $i);
240}
241
242print("testSet({".join(",",@$setout)."})");
243
244my $setin = $testClient->testSet($setout);
245
246print(" = {".join(",",@$setout)."}\n");
247
248#
249# LIST TEST
250#
251my $listout = [];
252for (my $i = -2; $i < 3; ++$i) {
253 push(@$listout, $i);
254}
255
256print("testList({".join(",",@$listout)."})");
257
258my $listin = $testClient->testList($listout);
259
260print(" = {".join(",",@$listin)."}\n");
261
262#
263# ENUM TEST
264#
265print("testEnum(ONE)");
T Jake Luciani41687fc2008-12-23 03:45:43 +0000266my $ret = $testClient->testEnum(ThriftTest::Numberz::ONE);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000267print(" = $ret\n");
268
269print("testEnum(TWO)");
T Jake Luciani41687fc2008-12-23 03:45:43 +0000270$ret = $testClient->testEnum(ThriftTest::Numberz::TWO);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000271print(" = $ret\n");
272
273print("testEnum(THREE)");
T Jake Luciani41687fc2008-12-23 03:45:43 +0000274$ret = $testClient->testEnum(ThriftTest::Numberz::THREE);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000275print(" = $ret\n");
276
277print("testEnum(FIVE)");
T Jake Luciani41687fc2008-12-23 03:45:43 +0000278$ret = $testClient->testEnum(ThriftTest::Numberz::FIVE);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000279print(" = $ret\n");
280
281print("testEnum(EIGHT)");
T Jake Luciani41687fc2008-12-23 03:45:43 +0000282$ret = $testClient->testEnum(ThriftTest::Numberz::EIGHT);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000283print(" = $ret\n");
284
285#
286# TYPEDEF TEST
287#
288print("testTypedef(309858235082523)");
289my $uid = $testClient->testTypedef(309858235082523);
290print(" = $uid\n");
291
292#
293# NESTED MAP TEST
294#
295print("testMapMap(1)");
296my $mm = $testClient->testMapMap(1);
297print(" = {");
298while( my ($key,$val) = each %$mm) {
299 print("$key => {");
300 while( my($k2,$v2) = each %$val) {
301 print("$k2 => $v2, ");
302 }
303 print("}, ");
304}
305print("}\n");
306
307#
308# INSANITY TEST
309#
Mark Slee82664432007-09-19 06:49:30 +0000310my $insane = new ThriftTest::Insanity();
T Jake Luciani41687fc2008-12-23 03:45:43 +0000311$insane->{userMap}->{ThriftTest::Numberz::FIVE} = 5000;
Mark Slee82664432007-09-19 06:49:30 +0000312my $truck = new ThriftTest::Xtruct();
313$truck->string_thing("Truck");
314$truck->byte_thing(8);
315$truck->i32_thing(8);
316$truck->i64_thing(8);
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000317push(@{$insane->{xtructs}}, $truck);
318
319print("testInsanity()");
320my $whoa = $testClient->testInsanity($insane);
321print(" = {");
322while( my ($key,$val) = each %$whoa) {
323 print("$key => {");
324 while( my($k2,$v2) = each %$val) {
325 print("$k2 => {");
326 my $userMap = $v2->{userMap};
327 print("{");
328 if (ref($userMap) eq "HASH") {
329 while( my($k3,$v3) = each %$userMap) {
330 print("$k3 => $v3, ");
331 }
332 }
333 print("}, ");
334
335 my $xtructs = $v2->{xtructs};
336 print("{");
337 if (ref($xtructs) eq "ARRAY") {
338 foreach my $x (@$xtructs) {
339 print("{\"".$x->{string_thing}."\", ".
340 $x->{byte_thing}.", ".$x->{i32_thing}.", ".$x->{i64_thing}."}, ");
341 }
342 }
343 print("}");
344
345 print("}, ");
346 }
347 print("}, ");
348}
349print("}\n");
350
351#
352# EXCEPTION TEST
353#
354print("testException('Xception')");
355eval {
356 $testClient->testException('Xception');
357 print(" void\nFAILURE\n");
Mark Slee82664432007-09-19 06:49:30 +0000358}; if($@ && $@->UNIVERSAL::isa('ThriftTest::Xception')) {
Mark Slee3e3d7ad2007-05-16 02:35:58 +0000359 print(' caught xception '.$@->{errorCode}.': '.$@->{message}."\n");
360}
361
362
363#
364# Normal tests done.
365#
366my $stop = gettimeofday();
367my $elp = sprintf("%d",1000*($stop - $start), 0);
368print("Total time: $elp ms\n");
369
370#
371# Extraneous "I don't trust PHP to pack/unpack integer" tests
372#
373
374# Max I32
375my $num = 2**30 + 2**30 - 1;
376my $num2 = $testClient->testI32($num);
377if ($num != $num2) {
378 print "Missed max32 $num = $num2\n";
379}
380
381# Min I32
382$num = 0 - 2**31;
383$num2 = $testClient->testI32($num);
384if ($num != $num2) {
385 print "Missed min32 $num = $num2\n";
386}
387
388# Max Number I can get out of my perl
389$num = 2**40;
390$num2 = $testClient->testI64($num);
391if ($num != $num2) {
392 print "Missed max64 $num = $num2\n";
393}
394
395# Max Number I can get out of my perl
396$num = 0 - 2**40;
397$num2 = $testClient->testI64($num);
398if ($num != $num2) {
399 print "Missed min64 $num = $num2\n";
400}
401
402$transport->close();
403
404
405