Bryan Duxbury | a971fb0 | 2011-03-04 00:49:40 +0000 | [diff] [blame] | 1 | <?php |
Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 2 | |
| 3 | namespace test\php; |
| 4 | |
Robert Lu | 68707d9 | 2018-01-17 19:40:39 +0800 | [diff] [blame] | 5 | /** @var \Composer\Autoload\ClassLoader $loader */ |
| 6 | $loader = require __DIR__ . '/../../vendor/autoload.php'; |
Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 7 | |
| 8 | use Thrift\ClassLoader\ThriftClassLoader; |
| 9 | |
| 10 | if (!isset($GEN_DIR)) { |
| 11 | $GEN_DIR = 'gen-php'; |
| 12 | } |
| 13 | if (!isset($MODE)) { |
| 14 | $MODE = 'normal'; |
| 15 | } |
| 16 | |
Robert Lu | 68707d9 | 2018-01-17 19:40:39 +0800 | [diff] [blame] | 17 | |
| 18 | if ($GEN_DIR == 'gen-php') { |
| 19 | $loader->addPsr4('', $GEN_DIR); |
Andreas Scheja | d1ceba4 | 2016-05-15 21:49:04 +0200 | [diff] [blame] | 20 | } else { |
Robert Lu | 68707d9 | 2018-01-17 19:40:39 +0800 | [diff] [blame] | 21 | $loader = new ThriftClassLoader(); |
Andreas Scheja | d1ceba4 | 2016-05-15 21:49:04 +0200 | [diff] [blame] | 22 | $loader->registerDefinition('ThriftTest', $GEN_DIR); |
Robert Lu | 68707d9 | 2018-01-17 19:40:39 +0800 | [diff] [blame] | 23 | $loader->register(); |
Andreas Scheja | d1ceba4 | 2016-05-15 21:49:04 +0200 | [diff] [blame] | 24 | } |
Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 25 | |
David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 26 | /* |
| 27 | * Licensed to the Apache Software Foundation (ASF) under one |
| 28 | * or more contributor license agreements. See the NOTICE file |
| 29 | * distributed with this work for additional information |
| 30 | * regarding copyright ownership. The ASF licenses this file |
| 31 | * to you under the Apache License, Version 2.0 (the |
| 32 | * "License"); you may not use this file except in compliance |
| 33 | * with the License. You may obtain a copy of the License at |
| 34 | * |
| 35 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 36 | * |
| 37 | * Unless required by applicable law or agreed to in writing, |
| 38 | * software distributed under the License is distributed on an |
| 39 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 40 | * KIND, either express or implied. See the License for the |
| 41 | * specific language governing permissions and limitations |
| 42 | * under the License. |
| 43 | */ |
| 44 | |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 45 | /** Include the Thrift base */ |
Jim King | 5903d67 | 2015-06-29 18:12:48 -0400 | [diff] [blame] | 46 | /** Include the protocols */ |
Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 47 | use Thrift\Protocol\TBinaryProtocol; |
Håkon Hitland | f39d4c8 | 2016-11-17 16:18:03 +0100 | [diff] [blame] | 48 | use Thrift\Protocol\TBinaryProtocolAccelerated; |
Jim King | 5903d67 | 2015-06-29 18:12:48 -0400 | [diff] [blame] | 49 | use Thrift\Protocol\TCompactProtocol; |
| 50 | use Thrift\Protocol\TJSONProtocol; |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 51 | |
| 52 | /** Include the socket layer */ |
Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 53 | use Thrift\Transport\TSocket; |
| 54 | use Thrift\Transport\TSocketPool; |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 55 | |
| 56 | /** Include the socket layer */ |
Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 57 | use Thrift\Transport\TFramedTransport; |
| 58 | use Thrift\Transport\TBufferedTransport; |
Mark Slee | 5b74307 | 2007-11-13 04:00:29 +0000 | [diff] [blame] | 59 | |
Jim King | 5903d67 | 2015-06-29 18:12:48 -0400 | [diff] [blame] | 60 | function makeProtocol($transport, $PROTO) |
| 61 | { |
| 62 | if ($PROTO == 'binary') { |
| 63 | return new TBinaryProtocol($transport); |
| 64 | } else if ($PROTO == 'compact') { |
| 65 | return new TCompactProtocol($transport); |
| 66 | } else if ($PROTO == 'json') { |
| 67 | return new TJSONProtocol($transport); |
Håkon Hitland | f39d4c8 | 2016-11-17 16:18:03 +0100 | [diff] [blame] | 68 | } else if ($PROTO == 'accel') { |
| 69 | if (!function_exists('thrift_protocol_write_binary')) { |
| 70 | echo "Acceleration extension is not loaded\n"; |
| 71 | exit(1); |
| 72 | } |
| 73 | return new TBinaryProtocolAccelerated($transport); |
Jim King | 5903d67 | 2015-06-29 18:12:48 -0400 | [diff] [blame] | 74 | } |
| 75 | |
Håkon Hitland | f39d4c8 | 2016-11-17 16:18:03 +0100 | [diff] [blame] | 76 | echo "--protocol must be one of {binary|compact|json|accel}\n"; |
| 77 | exit(1); |
Jim King | 5903d67 | 2015-06-29 18:12:48 -0400 | [diff] [blame] | 78 | } |
| 79 | |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 80 | $host = 'localhost'; |
| 81 | $port = 9090; |
| 82 | |
| 83 | if ($argc > 1) { |
| 84 | $host = $argv[0]; |
| 85 | } |
| 86 | |
| 87 | if ($argc > 2) { |
| 88 | $host = $argv[1]; |
| 89 | } |
| 90 | |
Roger Meier | 41ad434 | 2015-03-24 22:30:40 +0100 | [diff] [blame] | 91 | foreach ($argv as $arg) { |
| 92 | if (substr($arg, 0, 7) == '--port=') { |
| 93 | $port = substr($arg, 7); |
Jim King | 5903d67 | 2015-06-29 18:12:48 -0400 | [diff] [blame] | 94 | } else if (substr($arg, 0, 12) == '--transport=') { |
| 95 | $MODE = substr($arg, 12); |
| 96 | } else if (substr($arg, 0, 11) == '--protocol=') { |
| 97 | $PROTO = substr($arg, 11); |
Robert Lu | 68707d9 | 2018-01-17 19:40:39 +0800 | [diff] [blame] | 98 | } |
Roger Meier | 41ad434 | 2015-03-24 22:30:40 +0100 | [diff] [blame] | 99 | } |
| 100 | |
Mark Slee | 1dd819c | 2006-10-26 04:56:18 +0000 | [diff] [blame] | 101 | $hosts = array('localhost'); |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 102 | |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 103 | $socket = new TSocket($host, $port); |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 104 | $socket = new TSocketPool($hosts, $port); |
| 105 | $socket->setDebug(TRUE); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 106 | |
Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 107 | if ($MODE == 'inline') { |
| 108 | $transport = $socket; |
Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 109 | $testClient = new \ThriftTest\ThriftTestClient($transport); |
Bryan Duxbury | a971fb0 | 2011-03-04 00:49:40 +0000 | [diff] [blame] | 110 | } else if ($MODE == 'framed') { |
| 111 | $framedSocket = new TFramedTransport($socket); |
| 112 | $transport = $framedSocket; |
Jim King | 5903d67 | 2015-06-29 18:12:48 -0400 | [diff] [blame] | 113 | $protocol = makeProtocol($transport, $PROTO); |
Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 114 | $testClient = new \ThriftTest\ThriftTestClient($protocol); |
Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 115 | } else { |
| 116 | $bufferedSocket = new TBufferedTransport($socket, 1024, 1024); |
| 117 | $transport = $bufferedSocket; |
Jim King | 5903d67 | 2015-06-29 18:12:48 -0400 | [diff] [blame] | 118 | $protocol = makeProtocol($transport, $PROTO); |
Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 119 | $testClient = new \ThriftTest\ThriftTestClient($protocol); |
Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | $transport->open(); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 123 | |
| 124 | $start = microtime(true); |
| 125 | |
Håkon Hitland | f39d4c8 | 2016-11-17 16:18:03 +0100 | [diff] [blame] | 126 | define('ERR_BASETYPES', 1); |
| 127 | define('ERR_STRUCTS', 2); |
| 128 | define('ERR_CONTAINERS', 4); |
| 129 | define('ERR_EXCEPTIONS', 8); |
| 130 | define('ERR_UNKNOWN', 64); |
Nobuaki Sukegawa | e4ba164 | 2016-07-22 18:09:32 +0900 | [diff] [blame] | 131 | $exitcode = 0; |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 132 | /** |
| 133 | * VOID TEST |
| 134 | */ |
| 135 | print_r("testVoid()"); |
| 136 | $testClient->testVoid(); |
| 137 | print_r(" = void\n"); |
| 138 | |
Nobuaki Sukegawa | e4ba164 | 2016-07-22 18:09:32 +0900 | [diff] [blame] | 139 | function roundtrip($testClient, $method, $value) { |
| 140 | global $exitcode; |
| 141 | print_r("$method($value)"); |
| 142 | $ret = $testClient->$method($value); |
| 143 | print_r(" = \"$ret\"\n"); |
Håkon Hitland | f39d4c8 | 2016-11-17 16:18:03 +0100 | [diff] [blame] | 144 | if ($value !== $ret) { |
Nobuaki Sukegawa | e4ba164 | 2016-07-22 18:09:32 +0900 | [diff] [blame] | 145 | print_r("*** FAILED ***\n"); |
| 146 | $exitcode |= ERR_BASETYPES; |
| 147 | } |
| 148 | } |
| 149 | |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 150 | /** |
| 151 | * STRING TEST |
| 152 | */ |
Nobuaki Sukegawa | e4ba164 | 2016-07-22 18:09:32 +0900 | [diff] [blame] | 153 | roundtrip($testClient, 'testString', "Test"); |
Mark Slee | 5b74307 | 2007-11-13 04:00:29 +0000 | [diff] [blame] | 154 | |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 155 | /** |
Håkon Hitland | f39d4c8 | 2016-11-17 16:18:03 +0100 | [diff] [blame] | 156 | * BOOL TEST |
| 157 | */ |
| 158 | roundtrip($testClient, 'testBool', true); |
| 159 | roundtrip($testClient, 'testBool', false); |
| 160 | |
| 161 | /** |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 162 | * BYTE TEST |
| 163 | */ |
Nobuaki Sukegawa | e4ba164 | 2016-07-22 18:09:32 +0900 | [diff] [blame] | 164 | roundtrip($testClient, 'testByte', 1); |
Håkon Hitland | f39d4c8 | 2016-11-17 16:18:03 +0100 | [diff] [blame] | 165 | roundtrip($testClient, 'testByte', -1); |
| 166 | roundtrip($testClient, 'testByte', 127); |
| 167 | roundtrip($testClient, 'testByte', -128); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 168 | |
| 169 | /** |
| 170 | * I32 TEST |
| 171 | */ |
Nobuaki Sukegawa | e4ba164 | 2016-07-22 18:09:32 +0900 | [diff] [blame] | 172 | roundtrip($testClient, 'testI32', -1); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 173 | |
| 174 | /** |
| 175 | * I64 TEST |
| 176 | */ |
Håkon Hitland | f39d4c8 | 2016-11-17 16:18:03 +0100 | [diff] [blame] | 177 | roundtrip($testClient, 'testI64', 0); |
| 178 | roundtrip($testClient, 'testI64', 1); |
| 179 | roundtrip($testClient, 'testI64', -1); |
Nobuaki Sukegawa | e4ba164 | 2016-07-22 18:09:32 +0900 | [diff] [blame] | 180 | roundtrip($testClient, 'testI64', -34359738368); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 181 | |
| 182 | /** |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 183 | * DOUBLE TEST |
| 184 | */ |
Nobuaki Sukegawa | e4ba164 | 2016-07-22 18:09:32 +0900 | [diff] [blame] | 185 | roundtrip($testClient, 'testDouble', -852.234234234); |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 186 | |
| 187 | /** |
Jens Geyer | 8bcfdd9 | 2014-12-14 03:14:26 +0100 | [diff] [blame] | 188 | * BINARY TEST -- TODO |
| 189 | */ |
| 190 | |
| 191 | /** |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 192 | * STRUCT TEST |
| 193 | */ |
| 194 | print_r("testStruct({\"Zero\", 1, -3, -5})"); |
Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 195 | $out = new \ThriftTest\Xtruct(); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 196 | $out->string_thing = "Zero"; |
| 197 | $out->byte_thing = 1; |
| 198 | $out->i32_thing = -3; |
| 199 | $out->i64_thing = -5; |
| 200 | $in = $testClient->testStruct($out); |
| 201 | print_r(" = {\"".$in->string_thing."\", ". |
| 202 | $in->byte_thing.", ". |
| 203 | $in->i32_thing.", ". |
| 204 | $in->i64_thing."}\n"); |
| 205 | |
Håkon Hitland | f39d4c8 | 2016-11-17 16:18:03 +0100 | [diff] [blame] | 206 | if ($in != $out) { |
| 207 | echo "**FAILED**\n"; |
| 208 | $exitcode |= ERR_STRUCTS; |
| 209 | } |
| 210 | |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 211 | /** |
| 212 | * NESTED STRUCT TEST |
| 213 | */ |
| 214 | print_r("testNest({1, {\"Zero\", 1, -3, -5}), 5}"); |
Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 215 | $out2 = new \ThriftTest\Xtruct2(); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 216 | $out2->byte_thing = 1; |
| 217 | $out2->struct_thing = $out; |
| 218 | $out2->i32_thing = 5; |
| 219 | $in2 = $testClient->testNest($out2); |
| 220 | $in = $in2->struct_thing; |
| 221 | print_r(" = {".$in2->byte_thing.", {\"". |
| 222 | $in->string_thing."\", ". |
| 223 | $in->byte_thing.", ". |
| 224 | $in->i32_thing.", ". |
| 225 | $in->i64_thing."}, ". |
| 226 | $in2->i32_thing."}\n"); |
| 227 | |
Håkon Hitland | f39d4c8 | 2016-11-17 16:18:03 +0100 | [diff] [blame] | 228 | if ($in2 != $out2) { |
| 229 | echo "**FAILED**\n"; |
| 230 | $exitcode |= ERR_STRUCTS; |
| 231 | } |
| 232 | |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 233 | /** |
| 234 | * MAP TEST |
| 235 | */ |
| 236 | $mapout = array(); |
| 237 | for ($i = 0; $i < 5; ++$i) { |
| 238 | $mapout[$i] = $i-10; |
| 239 | } |
| 240 | print_r("testMap({"); |
| 241 | $first = true; |
| 242 | foreach ($mapout as $key => $val) { |
| 243 | if ($first) { |
| 244 | $first = false; |
| 245 | } else { |
| 246 | print_r(", "); |
| 247 | } |
| 248 | print_r("$key => $val"); |
| 249 | } |
| 250 | print_r("})"); |
| 251 | |
| 252 | $mapin = $testClient->testMap($mapout); |
| 253 | print_r(" = {"); |
| 254 | $first = true; |
| 255 | foreach ($mapin as $key => $val) { |
| 256 | if ($first) { |
| 257 | $first = false; |
| 258 | } else { |
| 259 | print_r(", "); |
| 260 | } |
| 261 | print_r("$key => $val"); |
| 262 | } |
| 263 | print_r("}\n"); |
| 264 | |
Håkon Hitland | f39d4c8 | 2016-11-17 16:18:03 +0100 | [diff] [blame] | 265 | if ($mapin != $mapout) { |
| 266 | echo "**FAILED**\n"; |
| 267 | $exitcode |= ERR_CONTAINERS; |
| 268 | } |
| 269 | |
Håkon Hitland | e66b8fc | 2016-12-05 18:42:41 +0100 | [diff] [blame] | 270 | $mapout = array(); |
| 271 | for ($i = 0; $i < 10; $i++) { |
| 272 | $mapout["key$i"] = "val$i"; |
| 273 | } |
| 274 | print_r('testStringMap({'); |
| 275 | $first = true; |
| 276 | foreach ($mapout as $key => $val) { |
| 277 | if ($first) { |
| 278 | $first = false; |
| 279 | } else { |
| 280 | print_r(", "); |
| 281 | } |
| 282 | print_r("\"$key\" => \"$val\""); |
| 283 | } |
| 284 | print_r("})"); |
| 285 | $mapin = $testClient->testStringMap($mapout); |
| 286 | print_r(" = {"); |
| 287 | $first = true; |
| 288 | foreach ($mapin as $key => $val) { |
| 289 | if ($first) { |
| 290 | $first = false; |
| 291 | } else { |
| 292 | print_r(", "); |
| 293 | } |
| 294 | print_r("\"$key\" => \"$val\""); |
| 295 | } |
| 296 | print_r("}\n"); |
| 297 | ksort($mapin); |
| 298 | if ($mapin != $mapout) { |
| 299 | echo "**FAILED**\n"; |
| 300 | $exitcode |= ERR_CONTAINERS; |
| 301 | } |
| 302 | |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 303 | /** |
| 304 | * SET TEST |
| 305 | */ |
| 306 | $setout = array();; |
| 307 | for ($i = -2; $i < 3; ++$i) { |
Håkon Hitland | f39d4c8 | 2016-11-17 16:18:03 +0100 | [diff] [blame] | 308 | $setout[$i]= true; |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 309 | } |
| 310 | print_r("testSet({"); |
Håkon Hitland | f39d4c8 | 2016-11-17 16:18:03 +0100 | [diff] [blame] | 311 | echo implode(',', array_keys($setout)); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 312 | print_r("})"); |
| 313 | $setin = $testClient->testSet($setout); |
| 314 | print_r(" = {"); |
Håkon Hitland | f39d4c8 | 2016-11-17 16:18:03 +0100 | [diff] [blame] | 315 | echo implode(', ', array_keys($setin)); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 316 | print_r("}\n"); |
Håkon Hitland | f39d4c8 | 2016-11-17 16:18:03 +0100 | [diff] [blame] | 317 | // Order of keys in set does not matter |
| 318 | ksort($setin); |
| 319 | if ($setout !== $setin) { |
| 320 | echo "**FAILED**\n"; |
| 321 | $exitcode |= ERR_CONTAINERS; |
| 322 | } |
| 323 | // Regression test for corrupted array |
| 324 | if ($setin[2] !== $setout[2] || is_int($setin[2])) { |
| 325 | echo "**FAILED**\n"; |
| 326 | $exitcode |= ERR_CONTAINERS; |
| 327 | } |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 328 | |
| 329 | /** |
| 330 | * LIST TEST |
| 331 | */ |
| 332 | $listout = array(); |
| 333 | for ($i = -2; $i < 3; ++$i) { |
| 334 | $listout []= $i; |
| 335 | } |
| 336 | print_r("testList({"); |
| 337 | $first = true; |
| 338 | foreach ($listout as $val) { |
| 339 | if ($first) { |
| 340 | $first = false; |
| 341 | } else { |
| 342 | print_r(", "); |
| 343 | } |
| 344 | print_r($val); |
| 345 | } |
| 346 | print_r("})"); |
| 347 | $listin = $testClient->testList($listout); |
| 348 | print_r(" = {"); |
| 349 | $first = true; |
| 350 | foreach ($listin as $val) { |
| 351 | if ($first) { |
| 352 | $first = false; |
| 353 | } else { |
| 354 | print_r(", "); |
| 355 | } |
| 356 | print_r($val); |
| 357 | } |
| 358 | print_r("}\n"); |
Håkon Hitland | f39d4c8 | 2016-11-17 16:18:03 +0100 | [diff] [blame] | 359 | if ($listin !== $listout) { |
| 360 | echo "**FAILED**\n"; |
| 361 | $exitcode |= ERR_CONTAINERS; |
| 362 | } |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 363 | |
| 364 | /** |
| 365 | * ENUM TEST |
| 366 | */ |
| 367 | print_r("testEnum(ONE)"); |
Roger Meier | 87afaac | 2013-01-06 20:10:42 +0100 | [diff] [blame] | 368 | $ret = $testClient->testEnum(\ThriftTest\Numberz::ONE); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 369 | print_r(" = $ret\n"); |
Håkon Hitland | f39d4c8 | 2016-11-17 16:18:03 +0100 | [diff] [blame] | 370 | if ($ret != \ThriftTest\Numberz::ONE) { |
| 371 | echo "**FAILED**\n"; |
| 372 | $exitcode |= ERR_STRUCTS; |
| 373 | } |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 374 | |
| 375 | print_r("testEnum(TWO)"); |
Roger Meier | 87afaac | 2013-01-06 20:10:42 +0100 | [diff] [blame] | 376 | $ret = $testClient->testEnum(\ThriftTest\Numberz::TWO); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 377 | print_r(" = $ret\n"); |
Håkon Hitland | f39d4c8 | 2016-11-17 16:18:03 +0100 | [diff] [blame] | 378 | if ($ret != \ThriftTest\Numberz::TWO) { |
| 379 | echo "**FAILED**\n"; |
| 380 | $exitcode |= ERR_STRUCTS; |
| 381 | } |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 382 | |
| 383 | print_r("testEnum(THREE)"); |
Roger Meier | 87afaac | 2013-01-06 20:10:42 +0100 | [diff] [blame] | 384 | $ret = $testClient->testEnum(\ThriftTest\Numberz::THREE); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 385 | print_r(" = $ret\n"); |
Håkon Hitland | f39d4c8 | 2016-11-17 16:18:03 +0100 | [diff] [blame] | 386 | if ($ret != \ThriftTest\Numberz::THREE) { |
| 387 | echo "**FAILED**\n"; |
| 388 | $exitcode |= ERR_STRUCTS; |
| 389 | } |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 390 | |
| 391 | print_r("testEnum(FIVE)"); |
Roger Meier | 87afaac | 2013-01-06 20:10:42 +0100 | [diff] [blame] | 392 | $ret = $testClient->testEnum(\ThriftTest\Numberz::FIVE); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 393 | print_r(" = $ret\n"); |
Håkon Hitland | f39d4c8 | 2016-11-17 16:18:03 +0100 | [diff] [blame] | 394 | if ($ret != \ThriftTest\Numberz::FIVE) { |
| 395 | echo "**FAILED**\n"; |
| 396 | $exitcode |= ERR_STRUCTS; |
| 397 | } |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 398 | |
| 399 | print_r("testEnum(EIGHT)"); |
Roger Meier | 87afaac | 2013-01-06 20:10:42 +0100 | [diff] [blame] | 400 | $ret = $testClient->testEnum(\ThriftTest\Numberz::EIGHT); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 401 | print_r(" = $ret\n"); |
Håkon Hitland | f39d4c8 | 2016-11-17 16:18:03 +0100 | [diff] [blame] | 402 | if ($ret != \ThriftTest\Numberz::EIGHT) { |
| 403 | echo "**FAILED**\n"; |
| 404 | $exitcode |= ERR_STRUCTS; |
| 405 | } |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 406 | |
| 407 | /** |
| 408 | * TYPEDEF TEST |
| 409 | */ |
| 410 | print_r("testTypedef(309858235082523)"); |
| 411 | $uid = $testClient->testTypedef(309858235082523); |
| 412 | print_r(" = $uid\n"); |
Håkon Hitland | f39d4c8 | 2016-11-17 16:18:03 +0100 | [diff] [blame] | 413 | if ($uid !== 309858235082523) { |
| 414 | echo "**FAILED**\n"; |
| 415 | $exitcode |= ERR_STRUCTS; |
| 416 | } |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 417 | |
| 418 | /** |
| 419 | * NESTED MAP TEST |
| 420 | */ |
| 421 | print_r("testMapMap(1)"); |
| 422 | $mm = $testClient->testMapMap(1); |
| 423 | print_r(" = {"); |
| 424 | foreach ($mm as $key => $val) { |
| 425 | print_r("$key => {"); |
| 426 | foreach ($val as $k2 => $v2) { |
| 427 | print_r("$k2 => $v2, "); |
| 428 | } |
| 429 | print_r("}, "); |
| 430 | } |
| 431 | print_r("}\n"); |
Håkon Hitland | f39d4c8 | 2016-11-17 16:18:03 +0100 | [diff] [blame] | 432 | $expected_mm = [ |
| 433 | -4 => [-4 => -4, -3 => -3, -2 => -2, -1 => -1], |
| 434 | 4 => [4 => 4, 3 => 3, 2 => 2, 1 => 1], |
| 435 | ]; |
| 436 | if ($mm != $expected_mm) { |
| 437 | echo "**FAILED**\n"; |
| 438 | $exitcode |= ERR_CONTAINERS; |
| 439 | } |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 440 | |
| 441 | /** |
| 442 | * INSANITY TEST |
| 443 | */ |
Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 444 | $insane = new \ThriftTest\Insanity(); |
Roger Meier | 87afaac | 2013-01-06 20:10:42 +0100 | [diff] [blame] | 445 | $insane->userMap[\ThriftTest\Numberz::FIVE] = 5000; |
Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 446 | $truck = new \ThriftTest\Xtruct(); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 447 | $truck->string_thing = "Truck"; |
| 448 | $truck->byte_thing = 8; |
| 449 | $truck->i32_thing = 8; |
| 450 | $truck->i64_thing = 8; |
| 451 | $insane->xtructs []= $truck; |
| 452 | print_r("testInsanity()"); |
| 453 | $whoa = $testClient->testInsanity($insane); |
| 454 | print_r(" = {"); |
| 455 | foreach ($whoa as $key => $val) { |
| 456 | print_r("$key => {"); |
| 457 | foreach ($val as $k2 => $v2) { |
| 458 | print_r("$k2 => {"); |
| 459 | $userMap = $v2->userMap; |
| 460 | print_r("{"); |
Bryan Duxbury | a971fb0 | 2011-03-04 00:49:40 +0000 | [diff] [blame] | 461 | if (is_array($userMap)) { |
Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 462 | foreach ($userMap as $k3 => $v3) { |
| 463 | print_r("$k3 => $v3, "); |
| 464 | } |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 465 | } |
| 466 | print_r("}, "); |
Mark Slee | 5b74307 | 2007-11-13 04:00:29 +0000 | [diff] [blame] | 467 | |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 468 | $xtructs = $v2->xtructs; |
| 469 | print_r("{"); |
Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 470 | if (is_array($xtructs)) { |
| 471 | foreach ($xtructs as $x) { |
| 472 | print_r("{\"".$x->string_thing."\", ". |
| 473 | $x->byte_thing.", ".$x->i32_thing.", ".$x->i64_thing."}, "); |
| 474 | } |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 475 | } |
| 476 | print_r("}"); |
Mark Slee | 5b74307 | 2007-11-13 04:00:29 +0000 | [diff] [blame] | 477 | |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 478 | print_r("}, "); |
| 479 | } |
| 480 | print_r("}, "); |
| 481 | } |
| 482 | print_r("}\n"); |
| 483 | |
Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 484 | /** |
| 485 | * EXCEPTION TEST |
| 486 | */ |
| 487 | print_r("testException('Xception')"); |
| 488 | try { |
| 489 | $testClient->testException('Xception'); |
| 490 | print_r(" void\nFAILURE\n"); |
Håkon Hitland | f39d4c8 | 2016-11-17 16:18:03 +0100 | [diff] [blame] | 491 | $exitcode |= ERR_EXCEPTIONS; |
Roger Meier | 87afaac | 2013-01-06 20:10:42 +0100 | [diff] [blame] | 492 | } catch (\ThriftTest\Xception $x) { |
Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 493 | print_r(' caught xception '.$x->errorCode.': '.$x->message."\n"); |
| 494 | } |
| 495 | |
Roy Sindre Norangshol | ec64f23 | 2017-07-26 20:49:38 +0200 | [diff] [blame] | 496 | // Regression test for THRIFT-4263 |
| 497 | print_r("testBinarySerializer_Deserialize('foo')"); |
| 498 | try { |
| 499 | \Thrift\Serializer\TBinarySerializer::deserialize(base64_decode('foo'), \ThriftTest\Xtruct2::class); |
| 500 | echo "**FAILED**\n"; |
| 501 | $exitcode |= ERR_STRUCTS; |
| 502 | } catch (\Thrift\Exception\TTransportException $happy_exception) { |
| 503 | // We expected this due to binary data of base64_decode('foo') is less then 4 |
| 504 | // bytes and it tries to find thrift version number in the transport by |
| 505 | // reading i32() at the beginning. Casting to string validates that |
| 506 | // exception is still accessible in memory and not corrupted. Without patch, |
| 507 | // PHP will error log that the exception doesn't have any tostring method, |
| 508 | // which is a lie due to corrupted memory. |
| 509 | for($i=99; $i > 0; $i--) { |
| 510 | (string)$happy_exception; |
| 511 | } |
| 512 | print_r(" SUCCESS\n"); |
| 513 | } |
| 514 | |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 515 | /** |
| 516 | * Normal tests done. |
| 517 | */ |
| 518 | |
| 519 | $stop = microtime(true); |
| 520 | $elp = round(1000*($stop - $start), 0); |
| 521 | print_r("Total time: $elp ms\n"); |
| 522 | |
| 523 | /** |
| 524 | * Extraneous "I don't trust PHP to pack/unpack integer" tests |
| 525 | */ |
| 526 | |
Håkon Hitland | e66b8fc | 2016-12-05 18:42:41 +0100 | [diff] [blame] | 527 | if ($protocol instanceof TBinaryProtocolAccelerated) { |
| 528 | // Regression check: check that method name is not double-freed |
| 529 | // Method name should not be an interned string. |
| 530 | $method_name = "Void"; |
| 531 | $method_name = "test$method_name"; |
| 532 | |
| 533 | $seqid = 0; |
| 534 | $args = new \ThriftTest\ThriftTest_testVoid_args(); |
| 535 | thrift_protocol_write_binary($protocol, $method_name, \Thrift\Type\TMessageType::CALL, $args, $seqid, $protocol->isStrictWrite()); |
| 536 | $testClient->recv_testVoid(); |
| 537 | |
| 538 | } |
| 539 | |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 540 | // Max I32 |
| 541 | $num = pow(2, 30) + (pow(2, 30) - 1); |
Håkon Hitland | f39d4c8 | 2016-11-17 16:18:03 +0100 | [diff] [blame] | 542 | roundtrip($testClient, 'testI32', $num); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 543 | |
| 544 | // Min I32 |
| 545 | $num = 0 - pow(2, 31); |
Håkon Hitland | f39d4c8 | 2016-11-17 16:18:03 +0100 | [diff] [blame] | 546 | roundtrip($testClient, 'testI32', $num); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 547 | |
| 548 | // Max I64 |
| 549 | $num = pow(2, 62) + (pow(2, 62) - 1); |
Håkon Hitland | f39d4c8 | 2016-11-17 16:18:03 +0100 | [diff] [blame] | 550 | roundtrip($testClient, 'testI64', $num); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 551 | |
| 552 | // Min I64 |
Nobuaki Sukegawa | e4ba164 | 2016-07-22 18:09:32 +0900 | [diff] [blame] | 553 | $num = 0 - pow(2, 62) - pow(2, 62); |
Håkon Hitland | f39d4c8 | 2016-11-17 16:18:03 +0100 | [diff] [blame] | 554 | roundtrip($testClient, 'testI64', $num); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 555 | |
Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 556 | $transport->close(); |
Nobuaki Sukegawa | e4ba164 | 2016-07-22 18:09:32 +0900 | [diff] [blame] | 557 | exit($exitcode); |