blob: 323a36ff091fb551a0689358da96d34d8f2d1be4 [file] [log] [blame]
Austin Schuh40c16522018-10-28 20:27:54 -07001<?php
2
3require_once('test_base.php');
4require_once('test_util.php');
5
6use Foo\TestEnum;
7use Foo\TestMessage;
8use Foo\TestMessage\Sub;
9use Foo\TestPackedMessage;
10use Google\Protobuf\Internal\CodedInputStream;
11use Google\Protobuf\Internal\FileDescriptorSet;
12use Google\Protobuf\Internal\GPBLabel;
13use Google\Protobuf\Internal\GPBType;
14use Google\Protobuf\Internal\GPBWire;
15use Google\Protobuf\Internal\CodedOutputStream;
16
17class ImplementationTest extends TestBase
18{
19 public function testReadInt32()
20 {
21 $value = null;
22
23 // Positive number.
24 $input = new CodedInputStream(hex2bin("01"));
25 GPBWire::readInt32($input, $value);
26 $this->assertSame(1, $value);
27
28 // Negative number.
29 $input = new CodedInputStream(hex2bin("ffffffff0f"));
30 GPBWire::readInt32($input, $value);
31 $this->assertSame(-1, $value);
32
33 // Discard overflow bits.
34 $input = new CodedInputStream(hex2bin("ffffffff7f"));
35 GPBWire::readInt32($input, $value);
36 $this->assertSame(-1, $value);
37 }
38
39 public function testReadUint32()
40 {
41 $value = null;
42
43 // Positive number.
44 $input = new CodedInputStream(hex2bin("01"));
45 GPBWire::readUint32($input, $value);
46 $this->assertSame(1, $value);
47
48 // Max uint32.
49 $input = new CodedInputStream(hex2bin("ffffffff0f"));
50 GPBWire::readUint32($input, $value);
51 $this->assertSame(-1, $value);
52
53 // Discard overflow bits.
54 $input = new CodedInputStream(hex2bin("ffffffff7f"));
55 GPBWire::readUint32($input, $value);
56 $this->assertSame(-1, $value);
57 }
58
59 public function testReadInt64()
60 {
61 $value = null;
62
63 // 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 public function testReadUint64()
80 {
81 $value = null;
82
83 // 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 public function testReadSint32()
100 {
101 $value = null;
102
103 $input = new CodedInputStream(hex2bin("00"));
104 GPBWire::readSint32($input, $value);
105 $this->assertSame(0, $value);
106
107 $input = new CodedInputStream(hex2bin("01"));
108 GPBWire::readSint32($input, $value);
109 $this->assertSame(-1, $value);
110
111 $input = new CodedInputStream(hex2bin("02"));
112 GPBWire::readSint32($input, $value);
113 $this->assertSame(1, $value);
114 }
115
116 public function testReadSint64()
117 {
118 $value = null;
119
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 public function testReadFixed32()
134 {
135 $value = null;
136 $input = new CodedInputStream(hex2bin("12345678"));
137 GPBWire::readFixed32($input, $value);
138 $this->assertSame(0x78563412, $value);
139 }
140
141 public function testReadFixed64()
142 {
143 $value = null;
144 $input = new CodedInputStream(hex2bin("1234567812345678"));
145 GPBWire::readFixed64($input, $value);
146 if (PHP_INT_SIZE == 4) {
147 $this->assertSame("8671175386481439762", $value);
148 } else {
149 $this->assertSame(0x7856341278563412, $value);
150 }
151 }
152
153 public function testReadSfixed32()
154 {
155 $value = null;
156 $input = new CodedInputStream(hex2bin("12345678"));
157 GPBWire::readSfixed32($input, $value);
158 $this->assertSame(0x78563412, $value);
159 }
160
161 public function testReadFloat()
162 {
163 $value = null;
164 $input = new CodedInputStream(hex2bin("0000803F"));
165 GPBWire::readFloat($input, $value);
166 $this->assertSame(1.0, $value);
167 }
168
169 public function testReadBool()
170 {
171 $value = null;
172
173 $input = new CodedInputStream(hex2bin("00"));
174 GPBWire::readBool($input, $value);
175 $this->assertSame(false, $value);
176
177 $input = new CodedInputStream(hex2bin("01"));
178 GPBWire::readBool($input, $value);
179 $this->assertSame(true, $value);
180 }
181
182 public function testReadDouble()
183 {
184 $value = null;
185 $input = new CodedInputStream(hex2bin("000000000000F03F"));
186 GPBWire::readDouble($input, $value);
187 $this->assertSame(1.0, $value);
188 }
189
190 public function testReadSfixed64()
191 {
192 $value = null;
193 $input = new CodedInputStream(hex2bin("1234567812345678"));
194 GPBWire::readSfixed64($input, $value);
195 if (PHP_INT_SIZE == 4) {
196 $this->assertSame("8671175386481439762", $value);
197 } else {
198 $this->assertSame(0x7856341278563412, $value);
199 }
200 }
201
202 public function testZigZagEncodeDecode()
203 {
204 $this->assertSame(0, GPBWire::zigZagEncode32(0));
205 $this->assertSame(1, GPBWire::zigZagEncode32(-1));
206 $this->assertSame(2, GPBWire::zigZagEncode32(1));
207 $this->assertSame(3, GPBWire::zigZagEncode32(-2));
208 $this->assertSame(0x7FFFFFFE, GPBWire::zigZagEncode32(0x3FFFFFFF));
209 $this->assertSame(0x7FFFFFFF, GPBWire::zigZagEncode32(0xC0000000));
210 $this->assertSame(0x7FFFFFFF, GPBWire::zigZagEncode32(-1073741824));
211
212 $this->assertSame(0, GPBWire::zigZagDecode32(0));
213 $this->assertSame(-1, GPBWire::zigZagDecode32(1));
214 $this->assertSame(1, GPBWire::zigZagDecode32(2));
215 $this->assertSame(-2, GPBWire::zigZagDecode32(3));
216 $this->assertSame(0x3FFFFFFF, GPBWire::zigZagDecode32(0x7FFFFFFE));
217 $this->assertSame(-1073741824, GPBWire::zigZagDecode32(0x7FFFFFFF));
218 $this->assertSame(0x7FFFFFFF, GPBWire::zigZagDecode32(0xFFFFFFFE));
219 $this->assertSame((int)-2147483648,GPBWire::zigZagDecode32(0xFFFFFFFF));
220
221 if (PHP_INT_SIZE == 4) {
222 $this->assertSame(-2, GPBWire::zigZagEncode32(0x7FFFFFFF));
223 $this->assertSame(-1, GPBWire::zigZagEncode32(0x80000000));
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(4294967294, GPBWire::zigZagEncode32(0x7FFFFFFF));
255 $this->assertSame(4294967295, GPBWire::zigZagEncode32(0x80000000));
256 $this->assertSame(0, GPBWire::zigZagEncode64(0));
257 $this->assertSame(1, GPBWire::zigZagEncode64(-1));
258 $this->assertSame(2, GPBWire::zigZagEncode64(1));
259 $this->assertSame(3, GPBWire::zigZagEncode64(-2));
260 $this->assertSame(0x7FFFFFFE, GPBWire::zigZagEncode64(0x3FFFFFFF));
261 $this->assertSame(
262 0x7FFFFFFF,
263 GPBWire::zigZagEncode64(0xFFFFFFFFC0000000));
264 $this->assertSame(
265 0xFFFFFFFE,
266 GPBWire::zigZagEncode64(0x7FFFFFFF));
267 $this->assertSame(
268 0xFFFFFFFF,
269 GPBWire::zigZagEncode64(0xFFFFFFFF80000000));
270 $this->assertSame(
271 -2, // 0xFFFFFFFFFFFFFFFE
272 GPBWire::zigZagEncode64(0x7FFFFFFFFFFFFFFF));
273 $this->assertSame(
274 -1, // 0xFFFFFFFFFFFFFFFF
275 GPBWire::zigZagEncode64(0x8000000000000000));
276
277 $this->assertSame(0, GPBWire::zigZagDecode64(0));
278 $this->assertSame(-1, GPBWire::zigZagDecode64(1));
279 $this->assertSame(1, GPBWire::zigZagDecode64(2));
280 $this->assertSame(-2, GPBWire::zigZagDecode64(3));
281 }
282
283 // Round trip
284 $this->assertSame(0, GPBWire::zigZagDecode32(GPBWire::zigZagEncode32(0)));
285 $this->assertSame(1, GPBWire::zigZagDecode32(GPBWire::zigZagEncode32(1)));
286 $this->assertSame(-1, GPBWire::zigZagDecode32(GPBWire::zigZagEncode32(-1)));
287 $this->assertSame(14927,
288 GPBWire::zigZagDecode32(GPBWire::zigZagEncode32(14927)));
289 $this->assertSame(-3612,
290 GPBWire::zigZagDecode32(GPBWire::zigZagEncode32(-3612)));
291 }
292
293 public function testDecode()
294 {
295 $m = new TestMessage();
296 $m->mergeFromString(TestUtil::getGoldenTestMessage());
297 TestUtil::assertTestMessage($m);
298 }
299
300 public function testDescriptorDecode()
301 {
302 $file_desc_set = new FileDescriptorSet();
303 $file_desc_set->mergeFromString(hex2bin(
304 "0a3b0a12746573745f696e636c7564652e70726f746f120362617222180a" .
305 "0b54657374496e636c75646512090a0161180120012805620670726f746f33"));
306
307 $this->assertSame(1, sizeof($file_desc_set->getFile()));
308
309 $file_desc = $file_desc_set->getFile()[0];
310 $this->assertSame("test_include.proto", $file_desc->getName());
311 $this->assertSame("bar", $file_desc->getPackage());
312 $this->assertSame(0, sizeof($file_desc->getDependency()));
313 $this->assertSame(1, sizeof($file_desc->getMessageType()));
314 $this->assertSame(0, sizeof($file_desc->getEnumType()));
315 $this->assertSame("proto3", $file_desc->getSyntax());
316
317 $desc = $file_desc->getMessageType()[0];
318 $this->assertSame("TestInclude", $desc->getName());
319 $this->assertSame(1, sizeof($desc->getField()));
320 $this->assertSame(0, sizeof($desc->getNestedType()));
321 $this->assertSame(0, sizeof($desc->getEnumType()));
322 $this->assertSame(0, sizeof($desc->getOneofDecl()));
323
324 $field = $desc->getField()[0];
325 $this->assertSame("a", $field->getName());
326 $this->assertSame(1, $field->getNumber());
327 $this->assertSame(GPBLabel::OPTIONAL, $field->getLabel());
328 $this->assertSame(GPBType::INT32, $field->getType());
329 }
330
331 public function testReadVarint64()
332 {
333 $var = 0;
334
335 // Empty buffer.
336 $input = new CodedInputStream(hex2bin(''));
337 $this->assertFalse($input->readVarint64($var));
338
339 // The largest varint is 10 bytes long.
340 $input = new CodedInputStream(hex2bin('8080808080808080808001'));
341 $this->assertFalse($input->readVarint64($var));
342
343 // Corrupted varint.
344 $input = new CodedInputStream(hex2bin('808080'));
345 $this->assertFalse($input->readVarint64($var));
346
347 // Normal case.
348 $input = new CodedInputStream(hex2bin('808001'));
349 $this->assertTrue($input->readVarint64($var));
350 if (PHP_INT_SIZE == 4) {
351 $this->assertSame('16384', $var);
352 } else {
353 $this->assertSame(16384, $var);
354 }
355 $this->assertFalse($input->readVarint64($var));
356
357 // Read two varint.
358 $input = new CodedInputStream(hex2bin('808001808002'));
359 $this->assertTrue($input->readVarint64($var));
360 if (PHP_INT_SIZE == 4) {
361 $this->assertSame('16384', $var);
362 } else {
363 $this->assertSame(16384, $var);
364 }
365 $this->assertTrue($input->readVarint64($var));
366 if (PHP_INT_SIZE == 4) {
367 $this->assertSame('32768', $var);
368 } else {
369 $this->assertSame(32768, $var);
370 }
371 $this->assertFalse($input->readVarint64($var));
372
373 // Read 64 testing
374 $testVals = array(
375 '10' => '0a000000000000000000',
376 '100' => '64000000000000000000',
377 '800' => 'a0060000000000000000',
378 '6400' => '80320000000000000000',
379 '70400' => '80a60400000000000000',
380 '774400' => '80a22f00000000000000',
381 '9292800' => '8098b704000000000000',
382 '74342400' => '80c0b923000000000000',
383 '743424000' => '8080bfe2020000000000',
384 '8177664000' => '8080b5bb1e0000000000',
385 '65421312000' => '8080a8dbf30100000000',
386 '785055744000' => '8080e0c7ec1600000000',
387 '9420668928000' => '808080dd969202000000',
388 '103627358208000' => '808080fff9c717000000',
389 '1139900940288000' => '808080f5bd9783020000',
390 '13678811283456000' => '808080fce699a6180000',
391 '109430490267648000' => '808080e0b7ceb1c20100',
392 '984874412408832000' => '808080e0f5c1bed50d00',
393 );
394
395 foreach ($testVals as $original => $encoded) {
396 $input = new CodedInputStream(hex2bin($encoded));
397 $this->assertTrue($input->readVarint64($var));
398 $this->assertEquals($original, $var);
399 }
400 }
401
402 public function testReadVarint32()
403 {
404 $var = 0;
405
406 // Empty buffer.
407 $input = new CodedInputStream(hex2bin(''));
408 $this->assertFalse($input->readVarint32($var));
409
410 // The largest varint is 10 bytes long.
411 $input = new CodedInputStream(hex2bin('8080808080808080808001'));
412 $this->assertFalse($input->readVarint32($var));
413
414 // Corrupted varint.
415 $input = new CodedInputStream(hex2bin('808080'));
416 $this->assertFalse($input->readVarint32($var));
417
418 // Normal case.
419 $input = new CodedInputStream(hex2bin('808001'));
420 $this->assertTrue($input->readVarint32($var));
421 $this->assertSame(16384, $var);
422 $this->assertFalse($input->readVarint32($var));
423
424 // Read two varint.
425 $input = new CodedInputStream(hex2bin('808001808002'));
426 $this->assertTrue($input->readVarint32($var));
427 $this->assertSame(16384, $var);
428 $this->assertTrue($input->readVarint32($var));
429 $this->assertSame(32768, $var);
430 $this->assertFalse($input->readVarint32($var));
431
432 // Read a 64-bit integer. High-order bits should be discarded.
433 $input = new CodedInputStream(hex2bin('808081808001'));
434 $this->assertTrue($input->readVarint32($var));
435 $this->assertSame(16384, $var);
436 $this->assertFalse($input->readVarint32($var));
437 }
438
439 public function testReadTag()
440 {
441 $input = new CodedInputStream(hex2bin('808001'));
442 $tag = $input->readTag();
443 $this->assertSame(16384, $tag);
444 $tag = $input->readTag();
445 $this->assertSame(0, $tag);
446 }
447
448 public function testPushPopLimit()
449 {
450 $input = new CodedInputStream(hex2bin('808001'));
451 $old_limit = $input->pushLimit(0);
452 $tag = $input->readTag();
453 $this->assertSame(0, $tag);
454 $input->popLimit($old_limit);
455 $tag = $input->readTag();
456 $this->assertSame(16384, $tag);
457 }
458
459 public function testReadRaw()
460 {
461 $input = new CodedInputStream(hex2bin('808001'));
462 $buffer = null;
463
464 $this->assertTrue($input->readRaw(3, $buffer));
465 $this->assertSame(hex2bin('808001'), $buffer);
466
467 $this->assertFalse($input->readRaw(1, $buffer));
468 }
469
470 public function testWriteVarint32()
471 {
472 $output = new CodedOutputStream(3);
473 $output->writeVarint32(16384, true);
474 $this->assertSame(hex2bin('808001'), $output->getData());
475
476 // Negative numbers are padded to be compatible with int64.
477 $output = new CodedOutputStream(10);
478 $output->writeVarint32(-43, false);
479 $this->assertSame(hex2bin('D5FFFFFFFFFFFFFFFF01'), $output->getData());
480 }
481
482 public function testWriteVarint64()
483 {
484 $output = new CodedOutputStream(10);
485 $output->writeVarint64(-43);
486 $this->assertSame(hex2bin('D5FFFFFFFFFFFFFFFF01'), $output->getData());
487 }
488
489 public function testWriteLittleEndian32()
490 {
491 $output = new CodedOutputStream(4);
492 $output->writeLittleEndian32(46);
493 $this->assertSame(hex2bin('2E000000'), $output->getData());
494 }
495
496 public function testWriteLittleEndian64()
497 {
498 $output = new CodedOutputStream(8);
499 $output->writeLittleEndian64(47);
500 $this->assertSame(hex2bin('2F00000000000000'), $output->getData());
501 }
502
503 public function testByteSize()
504 {
505 $m = new TestMessage();
506 TestUtil::setTestMessage($m);
507 $this->assertSame(506, $m->byteSize());
508 }
509
510 public function testPackedByteSize()
511 {
512 $m = new TestPackedMessage();
513 TestUtil::setTestPackedMessage($m);
514 $this->assertSame(166, $m->byteSize());
515 }
516
517 /**
518 * @expectedException UnexpectedValueException
519 * @expectedExceptionMessage Invalid message property: optionalInt32
520 */
521 public function testArrayConstructorJsonCaseThrowsException()
522 {
523 $m = new TestMessage([
524 'optionalInt32' => -42,
525 ]);
526 }
527
528 /**
529 * @expectedException Exception
530 * @expectedExceptionMessage Expect Foo\TestMessage_Sub.
531 */
532 public function testArraysForMessagesThrowsException()
533 {
534 $m = new TestMessage([
535 'optional_message' => [
536 'a' => 33
537 ]
538 ]);
539 }
540
541 public function testArrayConstructorWithNullValues()
542 {
543 $requestData = [
544 'optional_bool' => null,
545 'optional_string' => null,
546 'optional_bytes' => null,
547 'optional_message' => null,
548 ];
549
550 $m = new TestMessage($requestData);
551
552 $this->assertSame(false, $m->getOptionalBool());
553 $this->assertSame('', $m->getOptionalString());
554 $this->assertSame('', $m->getOptionalBytes());
555 $this->assertSame(null, $m->getOptionalMessage());
556 }
557
558 /**
559 * @dataProvider provideArrayConstructorWithNullValuesThrowsException
560 * @expectedException Exception
561 */
562 public function testArrayConstructorWithNullValuesThrowsException($requestData)
563 {
564 $m = new TestMessage($requestData);
565 }
566
567 public function provideArrayConstructorWithNullValuesThrowsException()
568 {
569 return [
570 [['optional_int32' => null]],
571 [['optional_int64' => null]],
572 [['optional_uint32' => null]],
573 [['optional_uint64' => null]],
574 [['optional_sint32' => null]],
575 [['optional_sint64' => null]],
576 [['optional_fixed32' => null]],
577 [['optional_fixed64' => null]],
578 [['optional_sfixed32' => null]],
579 [['optional_sfixed64' => null]],
580 [['optional_float' => null]],
581 [['optional_double' => null]],
582 [['optional_enum' => null]],
583 [['repeated_int32' => null]],
584 [['map_int32_int32' => null]],
585 ];
586 }
587}