php_implementation_test.php 18.3 KB
Newer Older
1 2 3 4 5 6 7 8
<?php

require_once('test_base.php');
require_once('test_util.php');

use Foo\TestMessage;
use Foo\TestMessage_Sub;
use Foo\TestPackedMessage;
9
use Google\Protobuf\Internal\CodedInputStream;
10 11 12 13
use Google\Protobuf\Internal\FileDescriptorSet;
use Google\Protobuf\Internal\GPBLabel;
use Google\Protobuf\Internal\GPBType;
use Google\Protobuf\Internal\GPBWire;
14
use Google\Protobuf\Internal\CodedOutputStream;
15 16 17 18 19 20 21 22 23

class ImplementationTest extends TestBase
{

    public function testReadInt32()
    {
        $value = null;

        // Positive number.
24
        $input = new CodedInputStream(hex2bin("01"));
25 26 27 28
        GPBWire::readInt32($input, $value);
        $this->assertSame(1, $value);

        // Negative number.
29
        $input = new CodedInputStream(hex2bin("ffffffff0f"));
30 31 32 33
        GPBWire::readInt32($input, $value);
        $this->assertSame(-1, $value);

        // Discard overflow bits.
34
        $input = new CodedInputStream(hex2bin("ffffffff7f"));
35 36 37 38 39 40 41 42 43
        GPBWire::readInt32($input, $value);
        $this->assertSame(-1, $value);
    }

    public function testReadUint32()
    {
        $value = null;

        // Positive number.
44
        $input = new CodedInputStream(hex2bin("01"));
45 46 47 48
        GPBWire::readUint32($input, $value);
        $this->assertSame(1, $value);

        // Max uint32.
49
        $input = new CodedInputStream(hex2bin("ffffffff0f"));
50 51 52 53
        GPBWire::readUint32($input, $value);
        $this->assertSame(-1, $value);

        // Discard overflow bits.
54
        $input = new CodedInputStream(hex2bin("ffffffff7f"));
55 56 57 58 59 60 61 62 63
        GPBWire::readUint32($input, $value);
        $this->assertSame(-1, $value);
    }

    public function testReadInt64()
    {
        $value = null;

        // Positive number.
64
        $input = new CodedInputStream(hex2bin("01"));
65
        GPBWire::readInt64($input, $value);
66
        $this->assertEquals(1, $value);
67 68

        // Negative number.
69
        $input = new CodedInputStream(hex2bin("ffffffffffffffffff01"));
70
        GPBWire::readInt64($input, $value);
71
        $this->assertEquals(-1, $value);
72 73

        // Discard overflow bits.
74
        $input = new CodedInputStream(hex2bin("ffffffffffffffffff0f"));
75
        GPBWire::readInt64($input, $value);
76
        $this->assertEquals(-1, $value);
77 78 79 80 81 82 83
    }

    public function testReadUint64()
    {
        $value = null;

        // Positive number.
84
        $input = new CodedInputStream(hex2bin("01"));
85
        GPBWire::readUint64($input, $value);
86
        $this->assertEquals(1, $value);
87 88

        // Negative number.
89
        $input = new CodedInputStream(hex2bin("FFFFFFFFFFFFFFFFFF01"));
90
        GPBWire::readUint64($input, $value);
91
        $this->assertEquals(-1, $value);
92 93

        // Discard overflow bits.
94
        $input = new CodedInputStream(hex2bin("FFFFFFFFFFFFFFFFFF0F"));
95
        GPBWire::readUint64($input, $value);
96
        $this->assertEquals(-1, $value);
97 98 99 100 101 102
    }

    public function testReadSint32()
    {
        $value = null;

103
        $input = new CodedInputStream(hex2bin("00"));
104 105 106
        GPBWire::readSint32($input, $value);
        $this->assertSame(0, $value);

107
        $input = new CodedInputStream(hex2bin("01"));
108 109 110
        GPBWire::readSint32($input, $value);
        $this->assertSame(-1, $value);

111
        $input = new CodedInputStream(hex2bin("02"));
112 113 114 115 116 117 118 119
        GPBWire::readSint32($input, $value);
        $this->assertSame(1, $value);
    }

    public function testReadSint64()
    {
        $value = null;

120
        $input = new CodedInputStream(hex2bin("00"));
121
        GPBWire::readSint64($input, $value);
122
        $this->assertEquals(0, $value);
123

124
        $input = new CodedInputStream(hex2bin("01"));
125
        GPBWire::readSint64($input, $value);
126
        $this->assertEquals(-1, $value);
127

128
        $input = new CodedInputStream(hex2bin("02"));
129
        GPBWire::readSint64($input, $value);
130
        $this->assertEquals(1, $value);
131 132 133 134 135
    }

    public function testReadFixed32()
    {
        $value = null;
136
        $input = new CodedInputStream(hex2bin("12345678"));
137 138 139 140 141 142 143
        GPBWire::readFixed32($input, $value);
        $this->assertSame(0x78563412, $value);
    }

    public function testReadFixed64()
    {
        $value = null;
144
        $input = new CodedInputStream(hex2bin("1234567812345678"));
145
        GPBWire::readFixed64($input, $value);
146 147 148 149 150
        if (PHP_INT_SIZE == 4) {
            $this->assertSame("8671175386481439762", $value);
        } else {
            $this->assertSame(0x7856341278563412, $value);
        }
151 152 153 154 155
    }

    public function testReadSfixed32()
    {
        $value = null;
156
        $input = new CodedInputStream(hex2bin("12345678"));
157 158 159 160 161 162 163
        GPBWire::readSfixed32($input, $value);
        $this->assertSame(0x78563412, $value);
    }

    public function testReadFloat()
    {
        $value = null;
164
        $input = new CodedInputStream(hex2bin("0000803F"));
165 166 167 168 169 170 171 172
        GPBWire::readFloat($input, $value);
        $this->assertSame(1.0, $value);
    }

    public function testReadBool()
    {
        $value = null;

173
        $input = new CodedInputStream(hex2bin("00"));
174 175 176
        GPBWire::readBool($input, $value);
        $this->assertSame(false, $value);

177
        $input = new CodedInputStream(hex2bin("01"));
178 179 180 181 182 183 184
        GPBWire::readBool($input, $value);
        $this->assertSame(true, $value);
    }

    public function testReadDouble()
    {
        $value = null;
185
        $input = new CodedInputStream(hex2bin("000000000000F03F"));
186 187 188 189 190 191 192
        GPBWire::readDouble($input, $value);
        $this->assertSame(1.0, $value);
    }

    public function testReadSfixed64()
    {
        $value = null;
193
        $input = new CodedInputStream(hex2bin("1234567812345678"));
194
        GPBWire::readSfixed64($input, $value);
195 196 197 198 199
        if (PHP_INT_SIZE == 4) {
            $this->assertSame("8671175386481439762", $value);
        } else {
            $this->assertSame(0x7856341278563412, $value);
        }
200 201 202 203 204 205 206 207 208 209
    }

    public function testZigZagEncodeDecode()
    {
        $this->assertSame(0, GPBWire::zigZagEncode32(0));
        $this->assertSame(1, GPBWire::zigZagEncode32(-1));
        $this->assertSame(2, GPBWire::zigZagEncode32(1));
        $this->assertSame(3, GPBWire::zigZagEncode32(-2));
        $this->assertSame(0x7FFFFFFE, GPBWire::zigZagEncode32(0x3FFFFFFF));
        $this->assertSame(0x7FFFFFFF, GPBWire::zigZagEncode32(0xC0000000));
210
        $this->assertSame(0x7FFFFFFF, GPBWire::zigZagEncode32(-1073741824));
211 212 213 214 215 216 217 218

        $this->assertSame(0,  GPBWire::zigZagDecode32(0));
        $this->assertSame(-1, GPBWire::zigZagDecode32(1));
        $this->assertSame(1,  GPBWire::zigZagDecode32(2));
        $this->assertSame(-2, GPBWire::zigZagDecode32(3));
        $this->assertSame(0x3FFFFFFF,  GPBWire::zigZagDecode32(0x7FFFFFFE));
        $this->assertSame(-1073741824, GPBWire::zigZagDecode32(0x7FFFFFFF));
        $this->assertSame(0x7FFFFFFF,  GPBWire::zigZagDecode32(0xFFFFFFFE));
219 220 221
        $this->assertSame((int)-2147483648,GPBWire::zigZagDecode32(0xFFFFFFFF));

        if (PHP_INT_SIZE == 4) {
222 223
            $this->assertSame(-2, GPBWire::zigZagEncode32(0x7FFFFFFF));
            $this->assertSame(-1, GPBWire::zigZagEncode32(0x80000000));
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
            $this->assertSame('0', GPBWire::zigZagEncode64(0));
            $this->assertSame('1', GPBWire::zigZagEncode64(-1));
            $this->assertSame('2', GPBWire::zigZagEncode64(1));
            $this->assertSame('3', GPBWire::zigZagEncode64(-2));
            $this->assertSame(
                '2147483646',  // 0x7FFFFFE
                GPBWire::zigZagEncode64(0x3FFFFFFF));
            $this->assertSame(
                '2147483647',  // 0x7FFFFFF
                GPBWire::zigZagEncode64(-1073741824));  // 0xFFFFFFFFC0000000
            $this->assertSame(
                '4294967294',                           // 0xFFFFFFFE
                GPBWire::zigZagEncode64(2147483647));   // 0x7FFFFFFF
            $this->assertSame(
                '4294967295',                           // 0xFFFFFFFF
                GPBWire::zigZagEncode64(-2147483648));  // 0xFFFFFFFF80000000
            $this->assertSame(
                '18446744073709551614',  // 0xFFFFFFFFFFFFFFFE
                                         // 0x7FFFFFFFFFFFFFFF
                GPBWire::zigZagEncode64("9223372036854775807"));
            $this->assertSame(
                '18446744073709551615',  // 0xFFFFFFFFFFFFFFFF
                                         // 0x8000000000000000
                GPBWire::zigZagEncode64("-9223372036854775808"));

            $this->assertSame('0', GPBWire::zigZagDecode64(0));
            $this->assertSame('-1', GPBWire::zigZagDecode64(1));
            $this->assertSame('1', GPBWire::zigZagDecode64(2));
            $this->assertSame('-2', GPBWire::zigZagDecode64(3));
        } else {
254 255
            $this->assertSame(4294967294, GPBWire::zigZagEncode32(0x7FFFFFFF));
            $this->assertSame(4294967295, GPBWire::zigZagEncode32(0x80000000));
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
            $this->assertSame(0, GPBWire::zigZagEncode64(0));
            $this->assertSame(1, GPBWire::zigZagEncode64(-1));
            $this->assertSame(2, GPBWire::zigZagEncode64(1));
            $this->assertSame(3, GPBWire::zigZagEncode64(-2));
            $this->assertSame(0x7FFFFFFE, GPBWire::zigZagEncode64(0x3FFFFFFF));
            $this->assertSame(
                0x7FFFFFFF,
                GPBWire::zigZagEncode64(0xFFFFFFFFC0000000));
            $this->assertSame(
                0xFFFFFFFE,
                GPBWire::zigZagEncode64(0x7FFFFFFF));
            $this->assertSame(
                0xFFFFFFFF,
                GPBWire::zigZagEncode64(0xFFFFFFFF80000000));
            $this->assertSame(
                -2,  // 0xFFFFFFFFFFFFFFFE
                GPBWire::zigZagEncode64(0x7FFFFFFFFFFFFFFF));
            $this->assertSame(
                -1,  // 0xFFFFFFFFFFFFFFFF
                GPBWire::zigZagEncode64(0x8000000000000000));

            $this->assertSame(0, GPBWire::zigZagDecode64(0));
            $this->assertSame(-1, GPBWire::zigZagDecode64(1));
            $this->assertSame(1, GPBWire::zigZagDecode64(2));
            $this->assertSame(-2, GPBWire::zigZagDecode64(3));
        }
282 283 284 285 286 287 288 289 290 291 292 293 294 295

        // Round trip
        $this->assertSame(0, GPBWire::zigZagDecode32(GPBWire::zigZagEncode32(0)));
        $this->assertSame(1, GPBWire::zigZagDecode32(GPBWire::zigZagEncode32(1)));
        $this->assertSame(-1, GPBWire::zigZagDecode32(GPBWire::zigZagEncode32(-1)));
        $this->assertSame(14927,
                      GPBWire::zigZagDecode32(GPBWire::zigZagEncode32(14927)));
        $this->assertSame(-3612,
                      GPBWire::zigZagDecode32(GPBWire::zigZagEncode32(-3612)));
    }

    public function testDecode()
    {
        $m = new TestMessage();
296
        $m->mergeFromString(TestUtil::getGoldenTestMessage());
297 298 299 300 301 302
        TestUtil::assertTestMessage($m);
    }

    public function testDescriptorDecode()
    {
        $file_desc_set = new FileDescriptorSet();
303
        $file_desc_set->mergeFromString(hex2bin(
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
            "0a3b0a12746573745f696e636c7564652e70726f746f120362617222180a" .
            "0b54657374496e636c75646512090a0161180120012805620670726f746f33"));

        $this->assertSame(1, sizeof($file_desc_set->getFile()));

        $file_desc = $file_desc_set->getFile()[0];
        $this->assertSame("test_include.proto", $file_desc->getName());
        $this->assertSame("bar", $file_desc->getPackage());
        $this->assertSame(0, sizeof($file_desc->getDependency()));
        $this->assertSame(1, sizeof($file_desc->getMessageType()));
        $this->assertSame(0, sizeof($file_desc->getEnumType()));
        $this->assertSame("proto3", $file_desc->getSyntax());

        $desc = $file_desc->getMessageType()[0];
        $this->assertSame("TestInclude", $desc->getName());
        $this->assertSame(1, sizeof($desc->getField()));
        $this->assertSame(0, sizeof($desc->getNestedType()));
        $this->assertSame(0, sizeof($desc->getEnumType()));
        $this->assertSame(0, sizeof($desc->getOneofDecl()));

        $field = $desc->getField()[0];
        $this->assertSame("a", $field->getName());
        $this->assertSame(1, $field->getNumber());
        $this->assertSame(GPBLabel::OPTIONAL, $field->getLabel());
        $this->assertSame(GPBType::INT32, $field->getType());
    }

    public function testReadVarint64()
    {
        $var = 0;

        // Empty buffer.
336
        $input = new CodedInputStream(hex2bin(''));
337 338 339
        $this->assertFalse($input->readVarint64($var));

        // The largest varint is 10 bytes long.
340
        $input = new CodedInputStream(hex2bin('8080808080808080808001'));
341 342 343
        $this->assertFalse($input->readVarint64($var));

        // Corrupted varint.
344
        $input = new CodedInputStream(hex2bin('808080'));
345 346 347
        $this->assertFalse($input->readVarint64($var));

        // Normal case.
348
        $input = new CodedInputStream(hex2bin('808001'));
349
        $this->assertTrue($input->readVarint64($var));
350 351 352 353 354
        if (PHP_INT_SIZE == 4) {
            $this->assertSame('16384', $var);
        } else {
            $this->assertSame(16384, $var);
        }
355 356 357
        $this->assertFalse($input->readVarint64($var));

        // Read two varint.
358
        $input = new CodedInputStream(hex2bin('808001808002'));
359
        $this->assertTrue($input->readVarint64($var));
360 361 362 363 364
        if (PHP_INT_SIZE == 4) {
            $this->assertSame('16384', $var);
        } else {
            $this->assertSame(16384, $var);
        }
365
        $this->assertTrue($input->readVarint64($var));
366 367 368 369 370
        if (PHP_INT_SIZE == 4) {
            $this->assertSame('32768', $var);
        } else {
            $this->assertSame(32768, $var);
        }
371
        $this->assertFalse($input->readVarint64($var));
Sufir's avatar
Sufir committed
372 373

        // Read 64 testing
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
        $testVals = array(
            '10'                 => '0a000000000000000000',
            '100'                => '64000000000000000000',
            '800'                => 'a0060000000000000000',
            '6400'               => '80320000000000000000',
            '70400'              => '80a60400000000000000',
            '774400'             => '80a22f00000000000000',
            '9292800'            => '8098b704000000000000',
            '74342400'           => '80c0b923000000000000',
            '743424000'          => '8080bfe2020000000000',
            '8177664000'         => '8080b5bb1e0000000000',
            '65421312000'        => '8080a8dbf30100000000',
            '785055744000'       => '8080e0c7ec1600000000',
            '9420668928000'      => '808080dd969202000000',
            '103627358208000'    => '808080fff9c717000000',
            '1139900940288000'   => '808080f5bd9783020000',
            '13678811283456000'  => '808080fce699a6180000',
            '109430490267648000' => '808080e0b7ceb1c20100',
            '984874412408832000' => '808080e0f5c1bed50d00',
        );

        foreach ($testVals as $original => $encoded) {
396
            $input = new CodedInputStream(hex2bin($encoded));
397 398
            $this->assertTrue($input->readVarint64($var));
            $this->assertEquals($original, $var);
Sufir's avatar
Sufir committed
399
        }
400 401 402 403 404 405 406
    }

    public function testReadVarint32()
    {
        $var = 0;

        // Empty buffer.
407
        $input = new CodedInputStream(hex2bin(''));
408 409 410
        $this->assertFalse($input->readVarint32($var));

        // The largest varint is 10 bytes long.
411
        $input = new CodedInputStream(hex2bin('8080808080808080808001'));
412 413 414
        $this->assertFalse($input->readVarint32($var));

        // Corrupted varint.
415
        $input = new CodedInputStream(hex2bin('808080'));
416 417 418
        $this->assertFalse($input->readVarint32($var));

        // Normal case.
419
        $input = new CodedInputStream(hex2bin('808001'));
420 421 422 423 424
        $this->assertTrue($input->readVarint32($var));
        $this->assertSame(16384, $var);
        $this->assertFalse($input->readVarint32($var));

        // Read two varint.
425
        $input = new CodedInputStream(hex2bin('808001808002'));
426 427 428 429 430 431 432
        $this->assertTrue($input->readVarint32($var));
        $this->assertSame(16384, $var);
        $this->assertTrue($input->readVarint32($var));
        $this->assertSame(32768, $var);
        $this->assertFalse($input->readVarint32($var));

        // Read a 64-bit integer. High-order bits should be discarded.
433
        $input = new CodedInputStream(hex2bin('808081808001'));
434 435 436 437 438 439 440
        $this->assertTrue($input->readVarint32($var));
        $this->assertSame(16384, $var);
        $this->assertFalse($input->readVarint32($var));
    }

    public function testReadTag()
    {
441
        $input = new CodedInputStream(hex2bin('808001'));
442 443 444 445 446 447 448 449
        $tag = $input->readTag();
        $this->assertSame(16384, $tag);
        $tag = $input->readTag();
        $this->assertSame(0, $tag);
    }

    public function testPushPopLimit()
    {
450
        $input = new CodedInputStream(hex2bin('808001'));
451 452 453 454 455 456 457 458 459 460
        $old_limit = $input->pushLimit(0);
        $tag = $input->readTag();
        $this->assertSame(0, $tag);
        $input->popLimit($old_limit);
        $tag = $input->readTag();
        $this->assertSame(16384, $tag);
    }

    public function testReadRaw()
    {
461
        $input = new CodedInputStream(hex2bin('808001'));
462 463 464 465 466 467 468 469 470 471
        $buffer = null;

        $this->assertTrue($input->readRaw(3, $buffer));
        $this->assertSame(hex2bin('808001'), $buffer);

        $this->assertFalse($input->readRaw(1, $buffer));
    }

    public function testWriteVarint32()
    {
472 473
        $output = new CodedOutputStream(3);
        $output->writeVarint32(16384, true);
474
        $this->assertSame(hex2bin('808001'), $output->getData());
475 476

        // Negative numbers are padded to be compatible with int64.
477 478
        $output = new CodedOutputStream(10);
        $output->writeVarint32(-43, false);
479
        $this->assertSame(hex2bin('D5FFFFFFFFFFFFFFFF01'), $output->getData());
480 481 482 483
    }

    public function testWriteVarint64()
    {
484
        $output = new CodedOutputStream(10);
485 486 487 488 489 490
        $output->writeVarint64(-43);
        $this->assertSame(hex2bin('D5FFFFFFFFFFFFFFFF01'), $output->getData());
    }

    public function testWriteLittleEndian32()
    {
491
        $output = new CodedOutputStream(4);
492 493 494 495 496 497
        $output->writeLittleEndian32(46);
        $this->assertSame(hex2bin('2E000000'), $output->getData());
    }

    public function testWriteLittleEndian64()
    {
498
        $output = new CodedOutputStream(8);
499 500 501 502 503 504 505 506
        $output->writeLittleEndian64(47);
        $this->assertSame(hex2bin('2F00000000000000'), $output->getData());
    }

    public function testByteSize()
    {
        $m = new TestMessage();
        TestUtil::setTestMessage($m);
507
        $this->assertSame(506, $m->byteSize());
508 509 510 511 512 513
    }

    public function testPackedByteSize()
    {
        $m = new TestPackedMessage();
        TestUtil::setTestPackedMessage($m);
514
        $this->assertSame(166, $m->byteSize());
515 516
    }
}