php_implementation_test.php 20 KB

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