| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 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 |
*/ |
| 22 |
|
| 23 |
namespace Test\Thrift\Unit\Lib\Protocol; |
| 24 |
|
| 25 |
use PHPUnit\Framework\TestCase; |
| 26 |
use Thrift\Exception\TProtocolException; |
| 27 |
use Thrift\Protocol\TBinaryProtocol; |
| 28 |
use Thrift\Transport\TTransport; |
| 29 |
use Thrift\Type\TType; |
| 30 |
|
| 31 |
class TBinaryProtocolTest extends TestCase |
| 32 |
{ |
| 33 |
private const VERSION_MASK = 0xffff0000; |
| 34 |
private const VERSION_1 = 0x80010000; |
| 35 |
|
| 36 |
/** |
| 37 |
* @dataProvider writeMessageBeginDataProvider |
| 38 |
*/ |
| 39 |
public function testWriteMessageBegin( |
| 40 |
$strictWrite, |
| 41 |
$name, |
| 42 |
$type, |
| 43 |
$seqid, |
| 44 |
$writeCallsParams, |
| 45 |
$writeCallsResults, |
| 46 |
$expectedResult |
| 47 |
) { |
| 48 |
$transport = $this->createMock(TTransport::class); |
| 49 |
$protocol = new TBinaryProtocol($transport, false, $strictWrite); |
| 50 |
|
| 51 |
$transport->expects($this->exactly(count($writeCallsParams))) |
| 52 |
->method('write') |
| 53 |
->withConsecutive(...$writeCallsParams) |
| 54 |
->willReturnOnConsecutiveCalls(...$writeCallsResults); |
| 55 |
|
| 56 |
$result = $protocol->writeMessageBegin($name, $type, $seqid); |
| 57 |
$this->assertEquals($expectedResult, $result); |
| 58 |
} |
| 59 |
|
| 60 |
public function writeMessageBeginDataProvider() |
| 61 |
{ |
| 62 |
$type = TType::STRING; |
| 63 |
$seqid = 555; |
| 64 |
|
| 65 |
yield 'strictWrite=true' => [ |
| 66 |
'strictWrite' => true, |
| 67 |
'name' => 'testName', |
| 68 |
'type' => $type, |
| 69 |
'seqid' => $seqid, |
| 70 |
'writeCallsParams' => [ |
| 71 |
[pack('N', self::VERSION_1 | $type), 4], #writeI32 |
| 72 |
[pack('N', strlen('testName')), 4], #writeStringLen |
| 73 |
['testName', 8], #writeString |
| 74 |
[pack('N', $seqid), 4], #writeI32 |
| 75 |
], |
| 76 |
'writeCallsResults' => [ |
| 77 |
4, |
| 78 |
4, |
| 79 |
8, |
| 80 |
4, |
| 81 |
], |
| 82 |
'expectedResult' => 20, |
| 83 |
]; |
| 84 |
|
| 85 |
yield 'strictWrite=false' => [ |
| 86 |
'strictWrite' => false, |
| 87 |
'name' => 'testName', |
| 88 |
'type' => $type, |
| 89 |
'seqid' => $seqid, |
| 90 |
'writeCallsParams' => [ |
| 91 |
[pack('N', strlen('testName')), 4], #writeStringLen |
| 92 |
['testName', 8], #writeString |
| 93 |
[pack('c', $type), 1], #writeByte |
| 94 |
[pack('N', $seqid), 4], #writeI32 |
| 95 |
], |
| 96 |
'writeCallsResults' => [ |
| 97 |
4, |
| 98 |
8, |
| 99 |
1, |
| 100 |
4, |
| 101 |
], |
| 102 |
'expectedResult' => 17, |
| 103 |
]; |
| 104 |
} |
| 105 |
|
| 106 |
public function testWriteMessageEnd() |
| 107 |
{ |
| 108 |
$transport = $this->createMock(TTransport::class); |
| 109 |
$protocol = new TBinaryProtocol($transport, false, false); |
| 110 |
|
| 111 |
$this->assertEquals(0, $protocol->writeMessageEnd()); |
| 112 |
} |
| 113 |
|
| 114 |
public function testWriteStructBegin() |
| 115 |
{ |
| 116 |
$transport = $this->createMock(TTransport::class); |
| 117 |
$protocol = new TBinaryProtocol($transport, false, false); |
| 118 |
|
| 119 |
$this->assertEquals(0, $protocol->writeStructBegin('testName')); |
| 120 |
} |
| 121 |
|
| 122 |
public function testWriteStructEnd() |
| 123 |
{ |
| 124 |
$transport = $this->createMock(TTransport::class); |
| 125 |
$protocol = new TBinaryProtocol($transport, false, false); |
| 126 |
|
| 127 |
$this->assertEquals(0, $protocol->writeStructEnd()); |
| 128 |
} |
| 129 |
|
| 130 |
public function testWriteFieldBegin() |
| 131 |
{ |
| 132 |
$fieldName = 'testName'; |
| 133 |
$fieldType = TType::STRING; |
| 134 |
$fieldId = 555; |
| 135 |
|
| 136 |
$transport = $this->createMock(TTransport::class); |
| 137 |
$protocol = new TBinaryProtocol($transport, false, false); |
| 138 |
|
| 139 |
$transport |
| 140 |
->expects($this->exactly(2)) |
| 141 |
->method('write') |
| 142 |
->withConsecutive( |
| 143 |
...[ |
| 144 |
[pack('c', $fieldType), 1], #writeByte |
| 145 |
[pack('n', $fieldId), 2], #writeI16 |
| 146 |
] |
| 147 |
)->willReturnOnConsecutiveCalls([1, 2]); |
| 148 |
|
| 149 |
$this->assertEquals(3, $protocol->writeFieldBegin($fieldName, $fieldType, $fieldId)); |
| 150 |
} |
| 151 |
|
| 152 |
public function testWriteFieldEnd() |
| 153 |
{ |
| 154 |
$transport = $this->createMock(TTransport::class); |
| 155 |
$protocol = new TBinaryProtocol($transport, false, false); |
| 156 |
|
| 157 |
$this->assertEquals(0, $protocol->writeFieldEnd()); |
| 158 |
} |
| 159 |
|
| 160 |
public function testWriteFieldStop() |
| 161 |
{ |
| 162 |
$transport = $this->createMock(TTransport::class); |
| 163 |
$protocol = new TBinaryProtocol($transport, false, false); |
| 164 |
|
| 165 |
$transport |
| 166 |
->expects($this->once()) |
| 167 |
->method('write') |
| 168 |
->with(pack('c', TType::STOP), 1) #writeByte |
| 169 |
->willReturn(1); |
| 170 |
|
| 171 |
$this->assertEquals(1, $protocol->writeFieldStop()); |
| 172 |
} |
| 173 |
|
| 174 |
public function testWriteMapBegin() |
| 175 |
{ |
| 176 |
$keyType = TType::I32; |
| 177 |
$valType = TType::STRING; |
| 178 |
$size = 99; |
| 179 |
|
| 180 |
$transport = $this->createMock(TTransport::class); |
| 181 |
$protocol = new TBinaryProtocol($transport, false, false); |
| 182 |
|
| 183 |
$transport |
| 184 |
->expects($this->exactly(3)) |
| 185 |
->method('write') |
| 186 |
->withConsecutive( |
| 187 |
...[ |
| 188 |
[pack('c', $keyType), 1], #writeByte |
| 189 |
[pack('c', $valType), 1], #writeByte |
| 190 |
[pack('N', $size), 4], #writeI32 |
| 191 |
] |
| 192 |
)->willReturnOnConsecutiveCalls([1, 1, 4]); |
| 193 |
|
| 194 |
$this->assertEquals(6, $protocol->writeMapBegin($keyType, $valType, $size)); |
| 195 |
} |
| 196 |
|
| 197 |
public function testWriteMapEnd() |
| 198 |
{ |
| 199 |
$transport = $this->createMock(TTransport::class); |
| 200 |
$protocol = new TBinaryProtocol($transport, false, false); |
| 201 |
|
| 202 |
$this->assertEquals(0, $protocol->writeMapEnd()); |
| 203 |
} |
| 204 |
|
| 205 |
public function testWriteListBegin() |
| 206 |
{ |
| 207 |
$elemType = TType::I32; |
| 208 |
$size = 99; |
| 209 |
|
| 210 |
$transport = $this->createMock(TTransport::class); |
| 211 |
$protocol = new TBinaryProtocol($transport, false, false); |
| 212 |
|
| 213 |
$transport |
| 214 |
->expects($this->exactly(2)) |
| 215 |
->method('write') |
| 216 |
->withConsecutive( |
| 217 |
...[ |
| 218 |
[pack('c', $elemType), 1], #writeByte |
| 219 |
[pack('N', $size), 4], #writeI32 |
| 220 |
] |
| 221 |
)->willReturnOnConsecutiveCalls([1, 4]); |
| 222 |
|
| 223 |
$this->assertEquals(5, $protocol->writeListBegin($elemType, $size)); |
| 224 |
} |
| 225 |
|
| 226 |
public function testWriteListEnd() |
| 227 |
{ |
| 228 |
$transport = $this->createMock(TTransport::class); |
| 229 |
$protocol = new TBinaryProtocol($transport, false, false); |
| 230 |
|
| 231 |
$this->assertEquals(0, $protocol->writeListEnd()); |
| 232 |
} |
| 233 |
|
| 234 |
public function testWriteSetBegin() |
| 235 |
{ |
| 236 |
$elemType = TType::I32; |
| 237 |
$size = 99; |
| 238 |
|
| 239 |
$transport = $this->createMock(TTransport::class); |
| 240 |
$protocol = new TBinaryProtocol($transport, false, false); |
| 241 |
|
| 242 |
$transport |
| 243 |
->expects($this->exactly(2)) |
| 244 |
->method('write') |
| 245 |
->withConsecutive( |
| 246 |
...[ |
| 247 |
[pack('c', $elemType), 1], #writeByte |
| 248 |
[pack('N', $size), 4], #writeI32 |
| 249 |
] |
| 250 |
)->willReturnOnConsecutiveCalls([1, 4]); |
| 251 |
|
| 252 |
$this->assertEquals(5, $protocol->writeSetBegin($elemType, $size)); |
| 253 |
} |
| 254 |
|
| 255 |
public function testWriteSetEnd() |
| 256 |
{ |
| 257 |
$transport = $this->createMock(TTransport::class); |
| 258 |
$protocol = new TBinaryProtocol($transport, false, false); |
| 259 |
|
| 260 |
$this->assertEquals(0, $protocol->writeSetEnd()); |
| 261 |
} |
| 262 |
|
| 263 |
public function testWriteBool() |
| 264 |
{ |
| 265 |
$value = true; |
| 266 |
$transport = $this->createMock(TTransport::class); |
| 267 |
$protocol = new TBinaryProtocol($transport, false, false); |
| 268 |
|
| 269 |
$transport |
| 270 |
->expects($this->once()) |
| 271 |
->method('write') |
| 272 |
->with(pack('c', (int)$value), 1) #writeByte |
| 273 |
->willReturn(1); |
| 274 |
|
| 275 |
$this->assertEquals(1, $protocol->writeBool($value)); |
| 276 |
} |
| 277 |
|
| 278 |
public function testWriteByte() |
| 279 |
{ |
| 280 |
$value = 1; |
| 281 |
$transport = $this->createMock(TTransport::class); |
| 282 |
$protocol = new TBinaryProtocol($transport, false, false); |
| 283 |
|
| 284 |
$transport |
| 285 |
->expects($this->once()) |
| 286 |
->method('write') |
| 287 |
->with(pack('c', $value), 1) #writeByte |
| 288 |
->willReturn(1); |
| 289 |
|
| 290 |
$this->assertEquals(1, $protocol->writeByte($value)); |
| 291 |
} |
| 292 |
|
| 293 |
public function testWriteI16() |
| 294 |
{ |
| 295 |
$value = 1; |
| 296 |
$transport = $this->createMock(TTransport::class); |
| 297 |
$protocol = new TBinaryProtocol($transport, false, false); |
| 298 |
|
| 299 |
$transport |
| 300 |
->expects($this->once()) |
| 301 |
->method('write') |
| 302 |
->with(pack('n', $value), 2) #writeI16 |
| 303 |
->willReturn(2); |
| 304 |
|
| 305 |
$this->assertEquals(2, $protocol->writeI16($value)); |
| 306 |
} |
| 307 |
|
| 308 |
public function testWriteI32() |
| 309 |
{ |
| 310 |
$value = 1; |
| 311 |
$transport = $this->createMock(TTransport::class); |
| 312 |
$protocol = new TBinaryProtocol($transport, false, false); |
| 313 |
|
| 314 |
$transport |
| 315 |
->expects($this->once()) |
| 316 |
->method('write') |
| 317 |
->with(pack('N', $value), 4) #writeI32 |
| 318 |
->willReturn(4); |
| 319 |
|
| 320 |
$this->assertEquals(4, $protocol->writeI32($value)); |
| 321 |
} |
| 322 |
|
| 323 |
public function testWriteI64For32BitArchitecture() |
| 324 |
{ |
| 325 |
if (PHP_INT_SIZE !== 4) { |
| 326 |
$this->markTestSkipped('Test is only for 32 bit architecture'); |
| 327 |
} |
| 328 |
$value = 1; |
| 329 |
$transport = $this->createMock(TTransport::class); |
| 330 |
$protocol = new TBinaryProtocol($transport, false, false); |
| 331 |
|
| 332 |
$neg = $value < 0; |
| 333 |
|
| 334 |
if ($neg) { |
| 335 |
$value *= -1; |
| 336 |
} |
| 337 |
|
| 338 |
$hi = (int)($value / 4294967296); |
| 339 |
$lo = (int)$value; |
| 340 |
|
| 341 |
if ($neg) { |
| 342 |
$hi = ~$hi; |
| 343 |
$lo = ~$lo; |
| 344 |
if (($lo & (int)0xffffffff) == (int)0xffffffff) { |
| 345 |
$lo = 0; |
| 346 |
$hi++; |
| 347 |
} else { |
| 348 |
$lo++; |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
$transport |
| 353 |
->expects($this->once()) |
| 354 |
->method('write') |
| 355 |
->with(pack('N2', $hi, $lo), 8) #writeI64 |
| 356 |
->willReturn(4); |
| 357 |
|
| 358 |
$this->assertEquals(8, $protocol->writeI64($value)); |
| 359 |
} |
| 360 |
|
| 361 |
public function testWriteI64For64BitArchitecture() |
| 362 |
{ |
| 363 |
if (PHP_INT_SIZE == 4) { |
| 364 |
$this->markTestSkipped('Test is only for 64 bit architecture'); |
| 365 |
} |
| 366 |
$value = 1; |
| 367 |
$transport = $this->createMock(TTransport::class); |
| 368 |
$protocol = new TBinaryProtocol($transport, false, false); |
| 369 |
|
| 370 |
$hi = $value >> 32; |
| 371 |
$lo = $value & 0xFFFFFFFF; |
| 372 |
|
| 373 |
$transport |
| 374 |
->expects($this->once()) |
| 375 |
->method('write') |
| 376 |
->with(pack('N2', $hi, $lo), 8) #writeI64 |
| 377 |
->willReturn(8); |
| 378 |
|
| 379 |
$this->assertEquals(8, $protocol->writeI64($value)); |
| 380 |
} |
| 381 |
|
| 382 |
public function testWriteDouble() |
| 383 |
{ |
| 384 |
$value = 1; |
| 385 |
$transport = $this->createMock(TTransport::class); |
| 386 |
$protocol = new TBinaryProtocol($transport, false, false); |
| 387 |
|
| 388 |
$transport |
| 389 |
->expects($this->once()) |
| 390 |
->method('write') |
| 391 |
->with(strrev(pack('d', $value)), 8) #writeDouble |
| 392 |
->willReturn(8); |
| 393 |
|
| 394 |
$this->assertEquals(8, $protocol->writeDouble($value)); |
| 395 |
} |
| 396 |
|
| 397 |
public function testWriteString() |
| 398 |
{ |
| 399 |
$value = 'string'; |
| 400 |
$transport = $this->createMock(TTransport::class); |
| 401 |
$protocol = new TBinaryProtocol($transport, false, false); |
| 402 |
|
| 403 |
$transport |
| 404 |
->expects($this->exactly(2)) |
| 405 |
->method('write') |
| 406 |
->withConsecutive( |
| 407 |
...[ |
| 408 |
[pack('N', strlen($value))], #writeI32, |
| 409 |
[$value, strlen($value)], #write, |
| 410 |
] |
| 411 |
)->willReturnOnConsecutiveCalls([4, 6]); |
| 412 |
|
| 413 |
$this->assertEquals(10, $protocol->writeString($value)); |
| 414 |
} |
| 415 |
|
| 416 |
public function testWriteUuid() |
| 417 |
{ |
| 418 |
$uuid = '01234567-89ab-cdef-0123-456789abcdef'; |
| 419 |
$transport = $this->createMock(TTransport::class); |
| 420 |
$protocol = new TBinaryProtocol($transport, false, false); |
| 421 |
|
| 422 |
$transport |
| 423 |
->expects($this->once()) |
| 424 |
->method('write') |
| 425 |
->with(hex2bin('0123456789abcdef0123456789abcdef'), 16) |
| 426 |
->willReturn(16); |
| 427 |
|
| 428 |
$this->assertEquals(16, $protocol->writeUuid($uuid)); |
| 429 |
} |
| 430 |
|
| 431 |
public function testReadUuid() |
| 432 |
{ |
| 433 |
$transport = $this->createMock(TTransport::class); |
| 434 |
$protocol = new TBinaryProtocol($transport, false, false); |
| 435 |
|
| 436 |
$transport |
| 437 |
->expects($this->once()) |
| 438 |
->method('readAll') |
| 439 |
->with(16) |
| 440 |
->willReturn(hex2bin('0123456789abcdef0123456789abcdef')); |
| 441 |
|
| 442 |
$this->assertEquals(16, $protocol->readUuid($value)); |
| 443 |
$this->assertEquals('01234567-89ab-cdef-0123-456789abcdef', $value); |
| 444 |
} |
| 445 |
|
| 446 |
/** |
| 447 |
* @dataProvider readMessageBeginDataProvider |
| 448 |
*/ |
| 449 |
public function testReadMessageBegin( |
| 450 |
$strictRead, |
| 451 |
$readCallsParams, |
| 452 |
$readCallsResults, |
| 453 |
$expectedReadLengthResult, |
| 454 |
$expectedName, |
| 455 |
$expectedType, |
| 456 |
$expectedSeqid, |
| 457 |
$expectedException = null, |
| 458 |
$expectedExceptionMessage = null, |
| 459 |
$expectedExceptionCode = null |
| 460 |
) { |
| 461 |
if ($expectedException) { |
| 462 |
$this->expectException($expectedException); |
| 463 |
$this->expectExceptionMessage($expectedExceptionMessage); |
| 464 |
$this->expectExceptionCode($expectedExceptionCode); |
| 465 |
} |
| 466 |
|
| 467 |
$transport = $this->createMock(TTransport::class); |
| 468 |
$protocol = new TBinaryProtocol($transport, $strictRead, true); |
| 469 |
|
| 470 |
$transport->expects($this->exactly(count($readCallsParams))) |
| 471 |
->method('readAll') |
| 472 |
->withConsecutive(...$readCallsParams) |
| 473 |
->willReturnOnConsecutiveCalls(...$readCallsResults); |
| 474 |
|
| 475 |
$result = $protocol->readMessageBegin($name, $type, $seqid); |
| 476 |
$this->assertEquals($expectedReadLengthResult, $result); |
| 477 |
$this->assertEquals($expectedName, $name); |
| 478 |
$this->assertEquals($expectedType, $type); |
| 479 |
$this->assertEquals($expectedSeqid, $seqid); |
| 480 |
} |
| 481 |
|
| 482 |
public function readMessageBeginDataProvider() |
| 483 |
{ |
| 484 |
yield 'strictRead=true' => [ |
| 485 |
'strictRead' => true, |
| 486 |
'readCallsParams' => [ |
| 487 |
[4], #readI32 |
| 488 |
[4], #readStringLen |
| 489 |
[8], #readString |
| 490 |
[4], #readI32 |
| 491 |
], |
| 492 |
'readCallsResults' => [ |
| 493 |
pack('N', 0x80010000 | TType::STRING), #version |
| 494 |
pack('N', strlen('testName')), |
| 495 |
'testName', |
| 496 |
pack('N', 555), |
| 497 |
], |
| 498 |
'expectedReadLengthResult' => 20, |
| 499 |
'expectedName' => 'testName', |
| 500 |
'expectedType' => TType::STRING, |
| 501 |
'expectedSeqid' => 555, |
| 502 |
]; |
| 503 |
|
| 504 |
yield 'incorrect version' => [ |
| 505 |
'strictRead' => true, |
| 506 |
'readCallsParams' => [ |
| 507 |
[4], #readI32 |
| 508 |
], |
| 509 |
'readCallsResults' => [ |
| 510 |
pack('N', 0x80000000 | TType::STRING), #version |
| 511 |
], |
| 512 |
'expectedReadLengthResult' => 4, |
| 513 |
'expectedName' => '', |
| 514 |
'expectedType' => 0, |
| 515 |
'expectedSeqid' => 0, |
| 516 |
'expectedException' => TProtocolException::class, |
| 517 |
'expectedExceptionMessage' => 'Bad version identifier: -2147483637', |
| 518 |
'expectedExceptionCode' => TProtocolException::BAD_VERSION, |
| 519 |
]; |
| 520 |
|
| 521 |
yield 'strictRead=false' => [ |
| 522 |
'strictRead' => false, |
| 523 |
'readCallsParams' => [ |
| 524 |
[4], #readStringLen |
| 525 |
[8], #readString |
| 526 |
[1], #readByte |
| 527 |
[4], #readI32 |
| 528 |
], |
| 529 |
'readCallsResults' => [ |
| 530 |
pack('N', strlen('testName')), |
| 531 |
'testName', |
| 532 |
pack('c', TType::STRING), |
| 533 |
pack('N', 555), |
| 534 |
], |
| 535 |
'expectedReadLengthResult' => 17, |
| 536 |
'expectedName' => 'testName', |
| 537 |
'expectedType' => TType::STRING, |
| 538 |
'expectedSeqid' => 555, |
| 539 |
]; |
| 540 |
|
| 541 |
yield 'strictRead=true without version' => [ |
| 542 |
'strictRead' => true, |
| 543 |
'readCallsParams' => [ |
| 544 |
[4], #readStringLen |
| 545 |
], |
| 546 |
'readCallsResults' => [ |
| 547 |
pack('N', strlen('testName')), |
| 548 |
], |
| 549 |
'expectedReadLengthResult' => 17, |
| 550 |
'expectedName' => 'testName', |
| 551 |
'expectedType' => TType::STRING, |
| 552 |
'expectedSeqid' => 555, |
| 553 |
'expectedException' => TProtocolException::class, |
| 554 |
'expectedExceptionMessage' => 'No version identifier, old protocol client?', |
| 555 |
'expectedExceptionCode' => TProtocolException::BAD_VERSION, |
| 556 |
]; |
| 557 |
} |
| 558 |
|
| 559 |
public function testReadMessageEnd() |
| 560 |
{ |
| 561 |
$transport = $this->createMock(TTransport::class); |
| 562 |
$protocol = new TBinaryProtocol($transport, false, false); |
| 563 |
|
| 564 |
$this->assertEquals(0, $protocol->readMessageEnd()); |
| 565 |
} |
| 566 |
|
| 567 |
public function testReadStructBegin() |
| 568 |
{ |
| 569 |
$transport = $this->createMock(TTransport::class); |
| 570 |
$protocol = new TBinaryProtocol($transport, false, false); |
| 571 |
|
| 572 |
$this->assertEquals(0, $protocol->readStructBegin($name)); |
| 573 |
$this->assertEquals('', $name); |
| 574 |
} |
| 575 |
|
| 576 |
public function testReadStructEnd() |
| 577 |
{ |
| 578 |
$transport = $this->createMock(TTransport::class); |
| 579 |
$protocol = new TBinaryProtocol($transport, false, false); |
| 580 |
|
| 581 |
$this->assertEquals(0, $protocol->readStructEnd()); |
| 582 |
} |
| 583 |
|
| 584 |
/** |
| 585 |
* @dataProvider readFieldBeginDataProvider |
| 586 |
*/ |
| 587 |
public function testReadFieldBegin( |
| 588 |
$storedFieldType, |
| 589 |
$readCallsParams, |
| 590 |
$readCallsResults, |
| 591 |
$expectedResult, |
| 592 |
$expectedName, |
| 593 |
$expectedFieldType, |
| 594 |
$expectedFieldId |
| 595 |
) { |
| 596 |
$transport = $this->createMock(TTransport::class); |
| 597 |
$protocol = new TBinaryProtocol($transport, false, false); |
| 598 |
|
| 599 |
$transport |
| 600 |
->expects($this->exactly(count($readCallsParams))) |
| 601 |
->method('readAll') |
| 602 |
->withConsecutive(...$readCallsParams) |
| 603 |
->willReturnOnConsecutiveCalls(...$readCallsResults); |
| 604 |
|
| 605 |
$this->assertEquals($expectedResult, $protocol->readFieldBegin($name, $fieldType, $fieldId)); |
| 606 |
$this->assertEquals($expectedName, $name); |
| 607 |
$this->assertEquals($expectedFieldType, $fieldType); |
| 608 |
$this->assertEquals($expectedFieldId, $fieldId); |
| 609 |
} |
| 610 |
|
| 611 |
public function readFieldBeginDataProvider() |
| 612 |
{ |
| 613 |
yield 'default' => [ |
| 614 |
'storedFieldType' => TType::STRING, |
| 615 |
'readCallsParams' => [ |
| 616 |
[1], #readByte |
| 617 |
[2], #readI16 |
| 618 |
], |
| 619 |
'readCallsResults' => [ |
| 620 |
pack('c', TType::STRING), |
| 621 |
pack('n', 555), |
| 622 |
], |
| 623 |
'expectedResult' => 3, |
| 624 |
'exprectedName' => '', |
| 625 |
'expectedFieldType' => TType::STRING, |
| 626 |
'expectedFieldId' => 555, |
| 627 |
]; |
| 628 |
|
| 629 |
yield 'stop field' => [ |
| 630 |
'storedFieldType' => TType::STOP, |
| 631 |
'readCallsParams' => [ |
| 632 |
[1], #readByte |
| 633 |
], |
| 634 |
'readCallsResults' => [ |
| 635 |
pack('c', TType::STOP), |
| 636 |
], |
| 637 |
'expectedResult' => 1, |
| 638 |
'exprectedName' => '', |
| 639 |
'expectedFieldType' => 0, |
| 640 |
'expectedFieldId' => 0, |
| 641 |
]; |
| 642 |
} |
| 643 |
|
| 644 |
public function testReadFieldEnd() |
| 645 |
{ |
| 646 |
$transport = $this->createMock(TTransport::class); |
| 647 |
$protocol = new TBinaryProtocol($transport, false, false); |
| 648 |
|
| 649 |
$this->assertEquals(0, $protocol->readFieldEnd()); |
| 650 |
} |
| 651 |
|
| 652 |
public function testReadMapBegin() |
| 653 |
{ |
| 654 |
$transport = $this->createMock(TTransport::class); |
| 655 |
$protocol = new TBinaryProtocol($transport, false, false); |
| 656 |
|
| 657 |
$transport |
| 658 |
->expects($this->exactly(3)) |
| 659 |
->method('readAll') |
| 660 |
->withConsecutive( |
| 661 |
...[ |
| 662 |
[1], #readByte |
| 663 |
[1], #readByte |
| 664 |
[4], #readI32 |
| 665 |
] |
| 666 |
)->willReturnOnConsecutiveCalls( |
| 667 |
pack('c', TType::I32), |
| 668 |
pack('c', TType::STRING), |
| 669 |
pack('N', 555) |
| 670 |
); |
| 671 |
|
| 672 |
$this->assertEquals(6, $protocol->readMapBegin($keyType, $valType, $size)); |
| 673 |
$this->assertEquals(TType::I32, $keyType); |
| 674 |
$this->assertEquals(TType::STRING, $valType); |
| 675 |
$this->assertEquals(555, $size); |
| 676 |
} |
| 677 |
|
| 678 |
public function testReadMapEnd() |
| 679 |
{ |
| 680 |
$transport = $this->createMock(TTransport::class); |
| 681 |
$protocol = new TBinaryProtocol($transport, false, false); |
| 682 |
|
| 683 |
$this->assertEquals(0, $protocol->readMapEnd()); |
| 684 |
} |
| 685 |
|
| 686 |
public function testReadListBegin() |
| 687 |
{ |
| 688 |
$transport = $this->createMock(TTransport::class); |
| 689 |
$protocol = new TBinaryProtocol($transport, false, false); |
| 690 |
|
| 691 |
$transport |
| 692 |
->expects($this->exactly(2)) |
| 693 |
->method('readAll') |
| 694 |
->withConsecutive( |
| 695 |
...[ |
| 696 |
[1], #readByte |
| 697 |
[4], #readI32 |
| 698 |
] |
| 699 |
)->willReturnOnConsecutiveCalls( |
| 700 |
pack('c', TType::I32), |
| 701 |
pack('N', 555) |
| 702 |
); |
| 703 |
|
| 704 |
$this->assertEquals(5, $protocol->readListBegin($elemType, $size)); |
| 705 |
$this->assertEquals(TType::I32, $elemType); |
| 706 |
$this->assertEquals(555, $size); |
| 707 |
} |
| 708 |
|
| 709 |
public function testReadListEnd() |
| 710 |
{ |
| 711 |
$transport = $this->createMock(TTransport::class); |
| 712 |
$protocol = new TBinaryProtocol($transport, false, false); |
| 713 |
|
| 714 |
$this->assertEquals(0, $protocol->readListEnd()); |
| 715 |
} |
| 716 |
|
| 717 |
public function testReadSetBegin() |
| 718 |
{ |
| 719 |
$transport = $this->createMock(TTransport::class); |
| 720 |
$protocol = new TBinaryProtocol($transport, false, false); |
| 721 |
|
| 722 |
$transport |
| 723 |
->expects($this->exactly(2)) |
| 724 |
->method('readAll') |
| 725 |
->withConsecutive( |
| 726 |
...[ |
| 727 |
[1], #readByte |
| 728 |
[4], #readI32 |
| 729 |
] |
| 730 |
)->willReturnOnConsecutiveCalls( |
| 731 |
pack('c', TType::I32), |
| 732 |
pack('N', 555) |
| 733 |
); |
| 734 |
|
| 735 |
$this->assertEquals(5, $protocol->readSetBegin($elemType, $size)); |
| 736 |
$this->assertEquals(TType::I32, $elemType); |
| 737 |
$this->assertEquals(555, $size); |
| 738 |
} |
| 739 |
|
| 740 |
public function testReadSetEnd() |
| 741 |
{ |
| 742 |
$transport = $this->createMock(TTransport::class); |
| 743 |
$protocol = new TBinaryProtocol($transport, false, false); |
| 744 |
|
| 745 |
$this->assertEquals(0, $protocol->readSetEnd()); |
| 746 |
} |
| 747 |
|
| 748 |
public function testReadBool() |
| 749 |
{ |
| 750 |
$transport = $this->createMock(TTransport::class); |
| 751 |
$protocol = new TBinaryProtocol($transport, false, false); |
| 752 |
|
| 753 |
$transport |
| 754 |
->expects($this->once()) |
| 755 |
->method('readAll') |
| 756 |
->with(1) #readByte |
| 757 |
->willReturn(pack('c', 1)); |
| 758 |
|
| 759 |
$this->assertEquals(1, $protocol->readBool($value)); |
| 760 |
$this->assertTrue($value); |
| 761 |
} |
| 762 |
|
| 763 |
public function testReadByte() |
| 764 |
{ |
| 765 |
$transport = $this->createMock(TTransport::class); |
| 766 |
$protocol = new TBinaryProtocol($transport, false, false); |
| 767 |
|
| 768 |
$transport |
| 769 |
->expects($this->once()) |
| 770 |
->method('readAll') |
| 771 |
->with(1) #readByte |
| 772 |
->willReturn(pack('c', 1)); |
| 773 |
|
| 774 |
$this->assertEquals(1, $protocol->readByte($value)); |
| 775 |
$this->assertEquals(1, $value); |
| 776 |
} |
| 777 |
|
| 778 |
/** |
| 779 |
* @dataProvider readI16DataProvider |
| 780 |
*/ |
| 781 |
public function testReadI16( |
| 782 |
$storedValue, |
| 783 |
$expectedValue |
| 784 |
) { |
| 785 |
$transport = $this->createMock(TTransport::class); |
| 786 |
$protocol = new TBinaryProtocol($transport, false, false); |
| 787 |
|
| 788 |
$transport |
| 789 |
->expects($this->once()) |
| 790 |
->method('readAll') |
| 791 |
->with(2) #readI16 |
| 792 |
->willReturn(pack('n', $storedValue)); |
| 793 |
|
| 794 |
$this->assertEquals(2, $protocol->readI16($value)); |
| 795 |
$this->assertEquals($expectedValue, $value); |
| 796 |
} |
| 797 |
|
| 798 |
public function readI16DataProvider() |
| 799 |
{ |
| 800 |
yield 'positive' => [1, 1]; |
| 801 |
yield 'negative' => [-1, -1]; |
| 802 |
} |
| 803 |
|
| 804 |
/** |
| 805 |
* @dataProvider readI16DataProvider |
| 806 |
*/ |
| 807 |
public function testReadI32( |
| 808 |
$storedValue, |
| 809 |
$expectedValue |
| 810 |
) { |
| 811 |
$transport = $this->createMock(TTransport::class); |
| 812 |
$protocol = new TBinaryProtocol($transport, false, false); |
| 813 |
|
| 814 |
$transport |
| 815 |
->expects($this->once()) |
| 816 |
->method('readAll') |
| 817 |
->with(4) #readI32 |
| 818 |
->willReturn(pack('N', $storedValue)); |
| 819 |
|
| 820 |
$this->assertEquals(4, $protocol->readI32($value)); |
| 821 |
$this->assertEquals($expectedValue, $value); |
| 822 |
} |
| 823 |
|
| 824 |
public function readI32DataProvider() |
| 825 |
{ |
| 826 |
yield 'positive' => [1, 1]; |
| 827 |
yield 'negative' => [-1, -1]; |
| 828 |
} |
| 829 |
|
| 830 |
/** |
| 831 |
* @dataProvider readI64For32BitArchitectureDataProvider |
| 832 |
*/ |
| 833 |
public function testReadI64For32BitArchitecture( |
| 834 |
$storedValue, |
| 835 |
$expectedValue |
| 836 |
) { |
| 837 |
if (PHP_INT_SIZE !== 4) { |
| 838 |
$this->markTestSkipped('Test is only for 32 bit architecture'); |
| 839 |
} |
| 840 |
|
| 841 |
$transport = $this->createMock(TTransport::class); |
| 842 |
$protocol = new TBinaryProtocol($transport, false, false); |
| 843 |
|
| 844 |
$neg = $storedValue < 0; |
| 845 |
|
| 846 |
if ($neg) { |
| 847 |
$storedValue *= -1; |
| 848 |
} |
| 849 |
|
| 850 |
$hi = (int)($storedValue / 4294967296); |
| 851 |
$lo = (int)$storedValue; |
| 852 |
|
| 853 |
if ($neg) { |
| 854 |
$hi = ~$hi; |
| 855 |
$lo = ~$lo; |
| 856 |
if (($lo & (int)0xffffffff) == (int)0xffffffff) { |
| 857 |
$lo = 0; |
| 858 |
$hi++; |
| 859 |
} else { |
| 860 |
$lo++; |
| 861 |
} |
| 862 |
} |
| 863 |
|
| 864 |
$transport |
| 865 |
->expects($this->once()) |
| 866 |
->method('write') |
| 867 |
->with(pack('N2', $hi, $lo), 8) #writeI64 |
| 868 |
->willReturn(4); |
| 869 |
|
| 870 |
$this->assertEquals(8, $protocol->readI64($value)); |
| 871 |
$this->assertEquals($expectedValue, $value); |
| 872 |
} |
| 873 |
|
| 874 |
public function readI64For32BitArchitectureDataProvider() |
| 875 |
{ |
| 876 |
$storedValueRepresent = function ($value) { |
| 877 |
$neg = $value < 0; |
| 878 |
|
| 879 |
if ($neg) { |
| 880 |
$value *= -1; |
| 881 |
} |
| 882 |
|
| 883 |
$hi = (int)($value / 4294967296); |
| 884 |
$lo = (int)$value; |
| 885 |
|
| 886 |
if ($neg) { |
| 887 |
$hi = ~$hi; |
| 888 |
$lo = ~$lo; |
| 889 |
if (($lo & (int)0xffffffff) == (int)0xffffffff) { |
| 890 |
$lo = 0; |
| 891 |
$hi++; |
| 892 |
} else { |
| 893 |
$lo++; |
| 894 |
} |
| 895 |
} |
| 896 |
|
| 897 |
return pack('N2', $hi, $lo); |
| 898 |
}; |
| 899 |
|
| 900 |
yield 'positive' => [ |
| 901 |
'storedValue' => $storedValueRepresent(1), |
| 902 |
'expectedValue' => 1, |
| 903 |
]; |
| 904 |
|
| 905 |
yield 'max' => [ |
| 906 |
'storedValue' => $storedValueRepresent(PHP_INT_MAX), |
| 907 |
'expectedValue' => PHP_INT_MAX, |
| 908 |
]; |
| 909 |
|
| 910 |
yield 'min' => [ |
| 911 |
'storedValue' => $storedValueRepresent(PHP_INT_MIN), |
| 912 |
'expectedValue' => PHP_INT_MIN, |
| 913 |
]; |
| 914 |
|
| 915 |
yield 'negative' => [ |
| 916 |
'storedValue' => $storedValueRepresent(-1), |
| 917 |
'expectedValue' => -1, |
| 918 |
]; |
| 919 |
} |
| 920 |
|
| 921 |
/** |
| 922 |
* @dataProvider readI64For64BitArchitectureDataProvider |
| 923 |
*/ |
| 924 |
public function testReadI64For64BitArchitecture( |
| 925 |
$storedValue, |
| 926 |
$expectedValue |
| 927 |
) { |
| 928 |
if (PHP_INT_SIZE == 4) { |
| 929 |
$this->markTestSkipped('Test is only for 64 bit architecture'); |
| 930 |
} |
| 931 |
$transport = $this->createMock(TTransport::class); |
| 932 |
$protocol = new TBinaryProtocol($transport, false, false); |
| 933 |
|
| 934 |
$transport |
| 935 |
->expects($this->once()) |
| 936 |
->method('readAll') |
| 937 |
->with(8) #readI64 |
| 938 |
->willReturn($storedValue); |
| 939 |
|
| 940 |
$this->assertEquals(8, $protocol->readI64($value)); |
| 941 |
$this->assertEquals($expectedValue, $value); |
| 942 |
} |
| 943 |
|
| 944 |
public function readI64For64BitArchitectureDataProvider() |
| 945 |
{ |
| 946 |
$storedValueRepresent = function ($value) { |
| 947 |
$hi = $value >> 32; |
| 948 |
$lo = $value & 0xFFFFFFFF; |
| 949 |
|
| 950 |
return pack('N2', $hi, $lo); |
| 951 |
}; |
| 952 |
|
| 953 |
yield 'positive' => [ |
| 954 |
'storedValue' => $storedValueRepresent(1), |
| 955 |
'expectedValue' => 1, |
| 956 |
]; |
| 957 |
|
| 958 |
yield 'max' => [ |
| 959 |
'storedValue' => $storedValueRepresent(PHP_INT_MAX), |
| 960 |
'expectedValue' => PHP_INT_MAX, |
| 961 |
]; |
| 962 |
|
| 963 |
yield 'min' => [ |
| 964 |
'storedValue' => $storedValueRepresent(PHP_INT_MIN), |
| 965 |
'expectedValue' => PHP_INT_MIN, |
| 966 |
]; |
| 967 |
|
| 968 |
yield 'negative' => [ |
| 969 |
'storedValue' => $storedValueRepresent(-1), |
| 970 |
'expectedValue' => -1, |
| 971 |
]; |
| 972 |
} |
| 973 |
|
| 974 |
public function testReadDouble() |
| 975 |
{ |
| 976 |
$transport = $this->createMock(TTransport::class); |
| 977 |
$protocol = new TBinaryProtocol($transport, false, false); |
| 978 |
|
| 979 |
$transport |
| 980 |
->expects($this->once()) |
| 981 |
->method('readAll') |
| 982 |
->with(8) #readDouble |
| 983 |
->willReturn(strrev(pack('d', 789))); |
| 984 |
|
| 985 |
$this->assertEquals(8, $protocol->readDouble($value)); |
| 986 |
$this->assertEquals(789, $value); |
| 987 |
} |
| 988 |
|
| 989 |
/** |
| 990 |
* @dataProvider readStringDataProvider |
| 991 |
*/ |
| 992 |
public function testReadString( |
| 993 |
$readCallsParams, |
| 994 |
$readCallsResults, |
| 995 |
$expectedLength, |
| 996 |
$expectedValue |
| 997 |
) { |
| 998 |
$transport = $this->createMock(TTransport::class); |
| 999 |
$protocol = new TBinaryProtocol($transport, false, false); |
| 1000 |
|
| 1001 |
$transport |
| 1002 |
->expects($this->exactly(count($readCallsParams))) |
| 1003 |
->method('readAll') |
| 1004 |
->withConsecutive(...$readCallsParams) |
| 1005 |
->willReturnOnConsecutiveCalls(...$readCallsResults); |
| 1006 |
|
| 1007 |
$this->assertEquals($expectedLength, $protocol->readString($value)); |
| 1008 |
$this->assertEquals($expectedValue, $value); |
| 1009 |
} |
| 1010 |
|
| 1011 |
public function readStringDataProvider() |
| 1012 |
{ |
| 1013 |
$storedValue = ''; |
| 1014 |
yield 'empty' => [ |
| 1015 |
'readCallsParams' => [ |
| 1016 |
[4] |
| 1017 |
], |
| 1018 |
'readCallsResults' => [ |
| 1019 |
pack('N', strlen($storedValue)) |
| 1020 |
], |
| 1021 |
'expectedLength' => 4, |
| 1022 |
'expectedValue' => '', |
| 1023 |
]; |
| 1024 |
|
| 1025 |
$storedValue = 'string'; |
| 1026 |
yield 'non-empty' => [ |
| 1027 |
'readCallsParams' => [ |
| 1028 |
[4], |
| 1029 |
[strlen($storedValue)] |
| 1030 |
], |
| 1031 |
'readCallsResults' => [ |
| 1032 |
pack('N', strlen($storedValue)), |
| 1033 |
$storedValue |
| 1034 |
], |
| 1035 |
'expectedLength' => 10, |
| 1036 |
'expectedValue' => 'string', |
| 1037 |
]; |
| 1038 |
} |
| 1039 |
} |