OLD | NEW |
(Empty) | |
| 1 <?php |
| 2 |
| 3 require_once('test.pb.php'); |
| 4 require_once('test_base.php'); |
| 5 require_once('test_util.php'); |
| 6 |
| 7 use Foo\TestMessage; |
| 8 use Foo\TestMessage_Sub; |
| 9 use Foo\TestPackedMessage; |
| 10 use Google\Protobuf\Internal\InputStream; |
| 11 use Google\Protobuf\Internal\FileDescriptorSet; |
| 12 use Google\Protobuf\Internal\GPBLabel; |
| 13 use Google\Protobuf\Internal\GPBType; |
| 14 use Google\Protobuf\Internal\GPBWire; |
| 15 use Google\Protobuf\Internal\OutputStream; |
| 16 |
| 17 class ImplementationTest extends TestBase |
| 18 { |
| 19 |
| 20 public function testReadInt32() |
| 21 { |
| 22 $value = null; |
| 23 |
| 24 // Positive number. |
| 25 $input = new InputStream(hex2bin("01")); |
| 26 GPBWire::readInt32($input, $value); |
| 27 $this->assertSame(1, $value); |
| 28 |
| 29 // Negative number. |
| 30 $input = new InputStream(hex2bin("ffffffff0f")); |
| 31 GPBWire::readInt32($input, $value); |
| 32 $this->assertSame(-1, $value); |
| 33 |
| 34 // Discard overflow bits. |
| 35 $input = new InputStream(hex2bin("ffffffff7f")); |
| 36 GPBWire::readInt32($input, $value); |
| 37 $this->assertSame(-1, $value); |
| 38 } |
| 39 |
| 40 public function testReadUint32() |
| 41 { |
| 42 $value = null; |
| 43 |
| 44 // Positive number. |
| 45 $input = new InputStream(hex2bin("01")); |
| 46 GPBWire::readUint32($input, $value); |
| 47 $this->assertSame(1, $value); |
| 48 |
| 49 // Max uint32. |
| 50 $input = new InputStream(hex2bin("ffffffff0f")); |
| 51 GPBWire::readUint32($input, $value); |
| 52 $this->assertSame(-1, $value); |
| 53 |
| 54 // Discard overflow bits. |
| 55 $input = new InputStream(hex2bin("ffffffff7f")); |
| 56 GPBWire::readUint32($input, $value); |
| 57 $this->assertSame(-1, $value); |
| 58 } |
| 59 |
| 60 public function testReadInt64() |
| 61 { |
| 62 $value = null; |
| 63 |
| 64 // Positive number. |
| 65 $input = new InputStream(hex2bin("01")); |
| 66 GPBWire::readInt64($input, $value); |
| 67 $this->assertEquals(1, $value); |
| 68 |
| 69 // Negative number. |
| 70 $input = new InputStream(hex2bin("ffffffffffffffffff01")); |
| 71 GPBWire::readInt64($input, $value); |
| 72 $this->assertEquals(-1, $value); |
| 73 |
| 74 // Discard overflow bits. |
| 75 $input = new InputStream(hex2bin("ffffffffffffffffff0f")); |
| 76 GPBWire::readInt64($input, $value); |
| 77 $this->assertEquals(-1, $value); |
| 78 } |
| 79 |
| 80 public function testReadUint64() |
| 81 { |
| 82 $value = null; |
| 83 |
| 84 // Positive number. |
| 85 $input = new InputStream(hex2bin("01")); |
| 86 GPBWire::readUint64($input, $value); |
| 87 $this->assertEquals(1, $value); |
| 88 |
| 89 // Negative number. |
| 90 $input = new InputStream(hex2bin("FFFFFFFFFFFFFFFFFF01")); |
| 91 GPBWire::readUint64($input, $value); |
| 92 $this->assertEquals(-1, $value); |
| 93 |
| 94 // Discard overflow bits. |
| 95 $input = new InputStream(hex2bin("FFFFFFFFFFFFFFFFFF0F")); |
| 96 GPBWire::readUint64($input, $value); |
| 97 $this->assertEquals(-1, $value); |
| 98 } |
| 99 |
| 100 public function testReadSint32() |
| 101 { |
| 102 $value = null; |
| 103 |
| 104 $input = new InputStream(hex2bin("00")); |
| 105 GPBWire::readSint32($input, $value); |
| 106 $this->assertSame(0, $value); |
| 107 |
| 108 $input = new InputStream(hex2bin("01")); |
| 109 GPBWire::readSint32($input, $value); |
| 110 $this->assertSame(-1, $value); |
| 111 |
| 112 $input = new InputStream(hex2bin("02")); |
| 113 GPBWire::readSint32($input, $value); |
| 114 $this->assertSame(1, $value); |
| 115 } |
| 116 |
| 117 public function testReadSint64() |
| 118 { |
| 119 $value = null; |
| 120 |
| 121 $input = new InputStream(hex2bin("00")); |
| 122 GPBWire::readSint64($input, $value); |
| 123 $this->assertEquals(0, $value); |
| 124 |
| 125 $input = new InputStream(hex2bin("01")); |
| 126 GPBWire::readSint64($input, $value); |
| 127 $this->assertEquals(-1, $value); |
| 128 |
| 129 $input = new InputStream(hex2bin("02")); |
| 130 GPBWire::readSint64($input, $value); |
| 131 $this->assertEquals(1, $value); |
| 132 } |
| 133 |
| 134 public function testReadFixed32() |
| 135 { |
| 136 $value = null; |
| 137 $input = new InputStream(hex2bin("12345678")); |
| 138 GPBWire::readFixed32($input, $value); |
| 139 $this->assertSame(0x78563412, $value); |
| 140 } |
| 141 |
| 142 public function testReadFixed64() |
| 143 { |
| 144 $value = null; |
| 145 $input = new InputStream(hex2bin("1234567812345678")); |
| 146 GPBWire::readFixed64($input, $value); |
| 147 if (PHP_INT_SIZE == 4) { |
| 148 $this->assertSame("8671175386481439762", $value); |
| 149 } else { |
| 150 $this->assertSame(0x7856341278563412, $value); |
| 151 } |
| 152 } |
| 153 |
| 154 public function testReadSfixed32() |
| 155 { |
| 156 $value = null; |
| 157 $input = new InputStream(hex2bin("12345678")); |
| 158 GPBWire::readSfixed32($input, $value); |
| 159 $this->assertSame(0x78563412, $value); |
| 160 } |
| 161 |
| 162 public function testReadFloat() |
| 163 { |
| 164 $value = null; |
| 165 $input = new InputStream(hex2bin("0000803F")); |
| 166 GPBWire::readFloat($input, $value); |
| 167 $this->assertSame(1.0, $value); |
| 168 } |
| 169 |
| 170 public function testReadBool() |
| 171 { |
| 172 $value = null; |
| 173 |
| 174 $input = new InputStream(hex2bin("00")); |
| 175 GPBWire::readBool($input, $value); |
| 176 $this->assertSame(false, $value); |
| 177 |
| 178 $input = new InputStream(hex2bin("01")); |
| 179 GPBWire::readBool($input, $value); |
| 180 $this->assertSame(true, $value); |
| 181 } |
| 182 |
| 183 public function testReadDouble() |
| 184 { |
| 185 $value = null; |
| 186 $input = new InputStream(hex2bin("000000000000F03F")); |
| 187 GPBWire::readDouble($input, $value); |
| 188 $this->assertSame(1.0, $value); |
| 189 } |
| 190 |
| 191 public function testReadSfixed64() |
| 192 { |
| 193 $value = null; |
| 194 $input = new InputStream(hex2bin("1234567812345678")); |
| 195 GPBWire::readSfixed64($input, $value); |
| 196 if (PHP_INT_SIZE == 4) { |
| 197 $this->assertSame("8671175386481439762", $value); |
| 198 } else { |
| 199 $this->assertSame(0x7856341278563412, $value); |
| 200 } |
| 201 } |
| 202 |
| 203 public function testZigZagEncodeDecode() |
| 204 { |
| 205 $this->assertSame(0, GPBWire::zigZagEncode32(0)); |
| 206 $this->assertSame(1, GPBWire::zigZagEncode32(-1)); |
| 207 $this->assertSame(2, GPBWire::zigZagEncode32(1)); |
| 208 $this->assertSame(3, GPBWire::zigZagEncode32(-2)); |
| 209 $this->assertSame(0x7FFFFFFE, GPBWire::zigZagEncode32(0x3FFFFFFF)); |
| 210 $this->assertSame(0x7FFFFFFF, GPBWire::zigZagEncode32(0xC0000000)); |
| 211 $this->assertSame(-2, GPBWire::zigZagEncode32(0x7FFFFFFF)); |
| 212 $this->assertSame(-1, GPBWire::zigZagEncode32(0x80000000)); |
| 213 |
| 214 $this->assertSame(0, GPBWire::zigZagDecode32(0)); |
| 215 $this->assertSame(-1, GPBWire::zigZagDecode32(1)); |
| 216 $this->assertSame(1, GPBWire::zigZagDecode32(2)); |
| 217 $this->assertSame(-2, GPBWire::zigZagDecode32(3)); |
| 218 $this->assertSame(0x3FFFFFFF, GPBWire::zigZagDecode32(0x7FFFFFFE)); |
| 219 $this->assertSame(-1073741824, GPBWire::zigZagDecode32(0x7FFFFFFF)); |
| 220 $this->assertSame(0x7FFFFFFF, GPBWire::zigZagDecode32(0xFFFFFFFE)); |
| 221 $this->assertSame((int)-2147483648,GPBWire::zigZagDecode32(0xFFFFFFFF)); |
| 222 |
| 223 if (PHP_INT_SIZE == 4) { |
| 224 $this->assertSame('0', GPBWire::zigZagEncode64(0)); |
| 225 $this->assertSame('1', GPBWire::zigZagEncode64(-1)); |
| 226 $this->assertSame('2', GPBWire::zigZagEncode64(1)); |
| 227 $this->assertSame('3', GPBWire::zigZagEncode64(-2)); |
| 228 $this->assertSame( |
| 229 '2147483646', // 0x7FFFFFE |
| 230 GPBWire::zigZagEncode64(0x3FFFFFFF)); |
| 231 $this->assertSame( |
| 232 '2147483647', // 0x7FFFFFF |
| 233 GPBWire::zigZagEncode64(-1073741824)); // 0xFFFFFFFFC0000000 |
| 234 $this->assertSame( |
| 235 '4294967294', // 0xFFFFFFFE |
| 236 GPBWire::zigZagEncode64(2147483647)); // 0x7FFFFFFF |
| 237 $this->assertSame( |
| 238 '4294967295', // 0xFFFFFFFF |
| 239 GPBWire::zigZagEncode64(-2147483648)); // 0xFFFFFFFF80000000 |
| 240 $this->assertSame( |
| 241 '18446744073709551614', // 0xFFFFFFFFFFFFFFFE |
| 242 // 0x7FFFFFFFFFFFFFFF |
| 243 GPBWire::zigZagEncode64("9223372036854775807")); |
| 244 $this->assertSame( |
| 245 '18446744073709551615', // 0xFFFFFFFFFFFFFFFF |
| 246 // 0x8000000000000000 |
| 247 GPBWire::zigZagEncode64("-9223372036854775808")); |
| 248 |
| 249 $this->assertSame('0', GPBWire::zigZagDecode64(0)); |
| 250 $this->assertSame('-1', GPBWire::zigZagDecode64(1)); |
| 251 $this->assertSame('1', GPBWire::zigZagDecode64(2)); |
| 252 $this->assertSame('-2', GPBWire::zigZagDecode64(3)); |
| 253 } else { |
| 254 $this->assertSame(0, GPBWire::zigZagEncode64(0)); |
| 255 $this->assertSame(1, GPBWire::zigZagEncode64(-1)); |
| 256 $this->assertSame(2, GPBWire::zigZagEncode64(1)); |
| 257 $this->assertSame(3, GPBWire::zigZagEncode64(-2)); |
| 258 $this->assertSame(0x7FFFFFFE, GPBWire::zigZagEncode64(0x3FFFFFFF)); |
| 259 $this->assertSame( |
| 260 0x7FFFFFFF, |
| 261 GPBWire::zigZagEncode64(0xFFFFFFFFC0000000)); |
| 262 $this->assertSame( |
| 263 0xFFFFFFFE, |
| 264 GPBWire::zigZagEncode64(0x7FFFFFFF)); |
| 265 $this->assertSame( |
| 266 0xFFFFFFFF, |
| 267 GPBWire::zigZagEncode64(0xFFFFFFFF80000000)); |
| 268 $this->assertSame( |
| 269 -2, // 0xFFFFFFFFFFFFFFFE |
| 270 GPBWire::zigZagEncode64(0x7FFFFFFFFFFFFFFF)); |
| 271 $this->assertSame( |
| 272 -1, // 0xFFFFFFFFFFFFFFFF |
| 273 GPBWire::zigZagEncode64(0x8000000000000000)); |
| 274 |
| 275 $this->assertSame(0, GPBWire::zigZagDecode64(0)); |
| 276 $this->assertSame(-1, GPBWire::zigZagDecode64(1)); |
| 277 $this->assertSame(1, GPBWire::zigZagDecode64(2)); |
| 278 $this->assertSame(-2, GPBWire::zigZagDecode64(3)); |
| 279 } |
| 280 |
| 281 // Round trip |
| 282 $this->assertSame(0, GPBWire::zigZagDecode32(GPBWire::zigZagEncode32(0))
); |
| 283 $this->assertSame(1, GPBWire::zigZagDecode32(GPBWire::zigZagEncode32(1))
); |
| 284 $this->assertSame(-1, GPBWire::zigZagDecode32(GPBWire::zigZagEncode32(-1
))); |
| 285 $this->assertSame(14927, |
| 286 GPBWire::zigZagDecode32(GPBWire::zigZagEncode32(14927))); |
| 287 $this->assertSame(-3612, |
| 288 GPBWire::zigZagDecode32(GPBWire::zigZagEncode32(-3612))); |
| 289 } |
| 290 |
| 291 public function testDecode() |
| 292 { |
| 293 $m = new TestMessage(); |
| 294 $m->decode(TestUtil::getGoldenTestMessage()); |
| 295 TestUtil::assertTestMessage($m); |
| 296 } |
| 297 |
| 298 public function testDescriptorDecode() |
| 299 { |
| 300 $file_desc_set = new FileDescriptorSet(); |
| 301 $file_desc_set->decode(hex2bin( |
| 302 "0a3b0a12746573745f696e636c7564652e70726f746f120362617222180a" . |
| 303 "0b54657374496e636c75646512090a0161180120012805620670726f746f33")); |
| 304 |
| 305 $this->assertSame(1, sizeof($file_desc_set->getFile())); |
| 306 |
| 307 $file_desc = $file_desc_set->getFile()[0]; |
| 308 $this->assertSame("test_include.proto", $file_desc->getName()); |
| 309 $this->assertSame("bar", $file_desc->getPackage()); |
| 310 $this->assertSame(0, sizeof($file_desc->getDependency())); |
| 311 $this->assertSame(1, sizeof($file_desc->getMessageType())); |
| 312 $this->assertSame(0, sizeof($file_desc->getEnumType())); |
| 313 $this->assertSame("proto3", $file_desc->getSyntax()); |
| 314 |
| 315 $desc = $file_desc->getMessageType()[0]; |
| 316 $this->assertSame("TestInclude", $desc->getName()); |
| 317 $this->assertSame(1, sizeof($desc->getField())); |
| 318 $this->assertSame(0, sizeof($desc->getNestedType())); |
| 319 $this->assertSame(0, sizeof($desc->getEnumType())); |
| 320 $this->assertSame(0, sizeof($desc->getOneofDecl())); |
| 321 |
| 322 $field = $desc->getField()[0]; |
| 323 $this->assertSame("a", $field->getName()); |
| 324 $this->assertSame(1, $field->getNumber()); |
| 325 $this->assertSame(GPBLabel::OPTIONAL, $field->getLabel()); |
| 326 $this->assertSame(GPBType::INT32, $field->getType()); |
| 327 } |
| 328 |
| 329 public function testReadVarint64() |
| 330 { |
| 331 $var = 0; |
| 332 |
| 333 // Empty buffer. |
| 334 $input = new InputStream(hex2bin('')); |
| 335 $this->assertFalse($input->readVarint64($var)); |
| 336 |
| 337 // The largest varint is 10 bytes long. |
| 338 $input = new InputStream(hex2bin('8080808080808080808001')); |
| 339 $this->assertFalse($input->readVarint64($var)); |
| 340 |
| 341 // Corrupted varint. |
| 342 $input = new InputStream(hex2bin('808080')); |
| 343 $this->assertFalse($input->readVarint64($var)); |
| 344 |
| 345 // Normal case. |
| 346 $input = new InputStream(hex2bin('808001')); |
| 347 $this->assertTrue($input->readVarint64($var)); |
| 348 if (PHP_INT_SIZE == 4) { |
| 349 $this->assertSame('16384', $var); |
| 350 } else { |
| 351 $this->assertSame(16384, $var); |
| 352 } |
| 353 $this->assertFalse($input->readVarint64($var)); |
| 354 |
| 355 // Read two varint. |
| 356 $input = new InputStream(hex2bin('808001808002')); |
| 357 $this->assertTrue($input->readVarint64($var)); |
| 358 if (PHP_INT_SIZE == 4) { |
| 359 $this->assertSame('16384', $var); |
| 360 } else { |
| 361 $this->assertSame(16384, $var); |
| 362 } |
| 363 $this->assertTrue($input->readVarint64($var)); |
| 364 if (PHP_INT_SIZE == 4) { |
| 365 $this->assertSame('32768', $var); |
| 366 } else { |
| 367 $this->assertSame(32768, $var); |
| 368 } |
| 369 $this->assertFalse($input->readVarint64($var)); |
| 370 } |
| 371 |
| 372 public function testReadVarint32() |
| 373 { |
| 374 $var = 0; |
| 375 |
| 376 // Empty buffer. |
| 377 $input = new InputStream(hex2bin('')); |
| 378 $this->assertFalse($input->readVarint32($var)); |
| 379 |
| 380 // The largest varint is 10 bytes long. |
| 381 $input = new InputStream(hex2bin('8080808080808080808001')); |
| 382 $this->assertFalse($input->readVarint32($var)); |
| 383 |
| 384 // Corrupted varint. |
| 385 $input = new InputStream(hex2bin('808080')); |
| 386 $this->assertFalse($input->readVarint32($var)); |
| 387 |
| 388 // Normal case. |
| 389 $input = new InputStream(hex2bin('808001')); |
| 390 $this->assertTrue($input->readVarint32($var)); |
| 391 $this->assertSame(16384, $var); |
| 392 $this->assertFalse($input->readVarint32($var)); |
| 393 |
| 394 // Read two varint. |
| 395 $input = new InputStream(hex2bin('808001808002')); |
| 396 $this->assertTrue($input->readVarint32($var)); |
| 397 $this->assertSame(16384, $var); |
| 398 $this->assertTrue($input->readVarint32($var)); |
| 399 $this->assertSame(32768, $var); |
| 400 $this->assertFalse($input->readVarint32($var)); |
| 401 |
| 402 // Read a 64-bit integer. High-order bits should be discarded. |
| 403 $input = new InputStream(hex2bin('808081808001')); |
| 404 $this->assertTrue($input->readVarint32($var)); |
| 405 $this->assertSame(16384, $var); |
| 406 $this->assertFalse($input->readVarint32($var)); |
| 407 } |
| 408 |
| 409 public function testReadTag() |
| 410 { |
| 411 $input = new InputStream(hex2bin('808001')); |
| 412 $tag = $input->readTag(); |
| 413 $this->assertSame(16384, $tag); |
| 414 $tag = $input->readTag(); |
| 415 $this->assertSame(0, $tag); |
| 416 } |
| 417 |
| 418 public function testPushPopLimit() |
| 419 { |
| 420 $input = new InputStream(hex2bin('808001')); |
| 421 $old_limit = $input->pushLimit(0); |
| 422 $tag = $input->readTag(); |
| 423 $this->assertSame(0, $tag); |
| 424 $input->popLimit($old_limit); |
| 425 $tag = $input->readTag(); |
| 426 $this->assertSame(16384, $tag); |
| 427 } |
| 428 |
| 429 public function testReadRaw() |
| 430 { |
| 431 $input = new InputStream(hex2bin('808001')); |
| 432 $buffer = null; |
| 433 |
| 434 $this->assertTrue($input->readRaw(3, $buffer)); |
| 435 $this->assertSame(hex2bin('808001'), $buffer); |
| 436 |
| 437 $this->assertFalse($input->readRaw(1, $buffer)); |
| 438 } |
| 439 |
| 440 public function testWriteVarint32() |
| 441 { |
| 442 $output = new OutputStream(3); |
| 443 $output->writeVarint32(16384); |
| 444 $this->assertSame(hex2bin('808001'), $output->getData()); |
| 445 } |
| 446 |
| 447 public function testWriteVarint64() |
| 448 { |
| 449 $output = new OutputStream(10); |
| 450 $output->writeVarint64(-43); |
| 451 $this->assertSame(hex2bin('D5FFFFFFFFFFFFFFFF01'), $output->getData()); |
| 452 } |
| 453 |
| 454 public function testWriteLittleEndian32() |
| 455 { |
| 456 $output = new OutputStream(4); |
| 457 $output->writeLittleEndian32(46); |
| 458 $this->assertSame(hex2bin('2E000000'), $output->getData()); |
| 459 } |
| 460 |
| 461 public function testWriteLittleEndian64() |
| 462 { |
| 463 $output = new OutputStream(8); |
| 464 $output->writeLittleEndian64(47); |
| 465 $this->assertSame(hex2bin('2F00000000000000'), $output->getData()); |
| 466 } |
| 467 |
| 468 public function testByteSize() |
| 469 { |
| 470 $m = new TestMessage(); |
| 471 TestUtil::setTestMessage($m); |
| 472 $this->assertSame(447, $m->byteSize()); |
| 473 } |
| 474 |
| 475 public function testPackedByteSize() |
| 476 { |
| 477 $m = new TestPackedMessage(); |
| 478 TestUtil::setTestPackedMessage($m); |
| 479 $this->assertSame(156, $m->byteSize()); |
| 480 } |
| 481 } |
OLD | NEW |