php_implementation_test.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. <?php
  2. require_once('test.pb.php');
  3. require_once('test_base.php');
  4. require_once('test_util.php');
  5. use Foo\TestMessage;
  6. use Foo\TestMessage_Sub;
  7. use Foo\TestPackedMessage;
  8. use Google\Protobuf\Internal\InputStream;
  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\OutputStream;
  14. class ImplementationTest extends TestBase
  15. {
  16. public function testReadInt32()
  17. {
  18. $value = null;
  19. // Positive number.
  20. $input = new InputStream(hex2bin("01"));
  21. GPBWire::readInt32($input, $value);
  22. $this->assertSame(1, $value);
  23. // Negative number.
  24. $input = new InputStream(hex2bin("ffffffff0f"));
  25. GPBWire::readInt32($input, $value);
  26. $this->assertSame(-1, $value);
  27. // Discard overflow bits.
  28. $input = new InputStream(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 InputStream(hex2bin("01"));
  37. GPBWire::readUint32($input, $value);
  38. $this->assertSame(1, $value);
  39. // Max uint32.
  40. $input = new InputStream(hex2bin("ffffffff0f"));
  41. GPBWire::readUint32($input, $value);
  42. $this->assertSame(-1, $value);
  43. // Discard overflow bits.
  44. $input = new InputStream(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 InputStream(hex2bin("01"));
  53. GPBWire::readInt64($input, $value);
  54. $this->assertEquals(1, $value);
  55. // Negative number.
  56. $input = new InputStream(hex2bin("ffffffffffffffffff01"));
  57. GPBWire::readInt64($input, $value);
  58. $this->assertEquals(-1, $value);
  59. // Discard overflow bits.
  60. $input = new InputStream(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 InputStream(hex2bin("01"));
  69. GPBWire::readUint64($input, $value);
  70. $this->assertEquals(1, $value);
  71. // Negative number.
  72. $input = new InputStream(hex2bin("FFFFFFFFFFFFFFFFFF01"));
  73. GPBWire::readUint64($input, $value);
  74. $this->assertEquals(-1, $value);
  75. // Discard overflow bits.
  76. $input = new InputStream(hex2bin("FFFFFFFFFFFFFFFFFF0F"));
  77. GPBWire::readUint64($input, $value);
  78. $this->assertEquals(-1, $value);
  79. }
  80. public function testReadSint32()
  81. {
  82. $value = null;
  83. $input = new InputStream(hex2bin("00"));
  84. GPBWire::readSint32($input, $value);
  85. $this->assertSame(0, $value);
  86. $input = new InputStream(hex2bin("01"));
  87. GPBWire::readSint32($input, $value);
  88. $this->assertSame(-1, $value);
  89. $input = new InputStream(hex2bin("02"));
  90. GPBWire::readSint32($input, $value);
  91. $this->assertSame(1, $value);
  92. }
  93. public function testReadSint64()
  94. {
  95. $value = null;
  96. $input = new InputStream(hex2bin("00"));
  97. GPBWire::readSint64($input, $value);
  98. $this->assertEquals(0, $value);
  99. $input = new InputStream(hex2bin("01"));
  100. GPBWire::readSint64($input, $value);
  101. $this->assertEquals(-1, $value);
  102. $input = new InputStream(hex2bin("02"));
  103. GPBWire::readSint64($input, $value);
  104. $this->assertEquals(1, $value);
  105. }
  106. public function testReadFixed32()
  107. {
  108. $value = null;
  109. $input = new InputStream(hex2bin("12345678"));
  110. GPBWire::readFixed32($input, $value);
  111. $this->assertSame(0x78563412, $value);
  112. }
  113. public function testReadFixed64()
  114. {
  115. $value = null;
  116. $input = new InputStream(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 InputStream(hex2bin("12345678"));
  128. GPBWire::readSfixed32($input, $value);
  129. $this->assertSame(0x78563412, $value);
  130. }
  131. public function testReadFloat()
  132. {
  133. $value = null;
  134. $input = new InputStream(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 InputStream(hex2bin("00"));
  142. GPBWire::readBool($input, $value);
  143. $this->assertSame(false, $value);
  144. $input = new InputStream(hex2bin("01"));
  145. GPBWire::readBool($input, $value);
  146. $this->assertSame(true, $value);
  147. }
  148. public function testReadDouble()
  149. {
  150. $value = null;
  151. $input = new InputStream(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 InputStream(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(-2, GPBWire::zigZagEncode32(0x7FFFFFFF));
  175. $this->assertSame(-1, GPBWire::zigZagEncode32(0x80000000));
  176. $this->assertSame(0, GPBWire::zigZagDecode32(0));
  177. $this->assertSame(-1, GPBWire::zigZagDecode32(1));
  178. $this->assertSame(1, GPBWire::zigZagDecode32(2));
  179. $this->assertSame(-2, GPBWire::zigZagDecode32(3));
  180. $this->assertSame(0x3FFFFFFF, GPBWire::zigZagDecode32(0x7FFFFFFE));
  181. $this->assertSame(-1073741824, GPBWire::zigZagDecode32(0x7FFFFFFF));
  182. $this->assertSame(0x7FFFFFFF, GPBWire::zigZagDecode32(0xFFFFFFFE));
  183. $this->assertSame((int)-2147483648,GPBWire::zigZagDecode32(0xFFFFFFFF));
  184. if (PHP_INT_SIZE == 4) {
  185. $this->assertSame('0', GPBWire::zigZagEncode64(0));
  186. $this->assertSame('1', GPBWire::zigZagEncode64(-1));
  187. $this->assertSame('2', GPBWire::zigZagEncode64(1));
  188. $this->assertSame('3', GPBWire::zigZagEncode64(-2));
  189. $this->assertSame(
  190. '2147483646', // 0x7FFFFFE
  191. GPBWire::zigZagEncode64(0x3FFFFFFF));
  192. $this->assertSame(
  193. '2147483647', // 0x7FFFFFF
  194. GPBWire::zigZagEncode64(-1073741824)); // 0xFFFFFFFFC0000000
  195. $this->assertSame(
  196. '4294967294', // 0xFFFFFFFE
  197. GPBWire::zigZagEncode64(2147483647)); // 0x7FFFFFFF
  198. $this->assertSame(
  199. '4294967295', // 0xFFFFFFFF
  200. GPBWire::zigZagEncode64(-2147483648)); // 0xFFFFFFFF80000000
  201. $this->assertSame(
  202. '18446744073709551614', // 0xFFFFFFFFFFFFFFFE
  203. // 0x7FFFFFFFFFFFFFFF
  204. GPBWire::zigZagEncode64("9223372036854775807"));
  205. $this->assertSame(
  206. '18446744073709551615', // 0xFFFFFFFFFFFFFFFF
  207. // 0x8000000000000000
  208. GPBWire::zigZagEncode64("-9223372036854775808"));
  209. $this->assertSame('0', GPBWire::zigZagDecode64(0));
  210. $this->assertSame('-1', GPBWire::zigZagDecode64(1));
  211. $this->assertSame('1', GPBWire::zigZagDecode64(2));
  212. $this->assertSame('-2', GPBWire::zigZagDecode64(3));
  213. } else {
  214. $this->assertSame(0, GPBWire::zigZagEncode64(0));
  215. $this->assertSame(1, GPBWire::zigZagEncode64(-1));
  216. $this->assertSame(2, GPBWire::zigZagEncode64(1));
  217. $this->assertSame(3, GPBWire::zigZagEncode64(-2));
  218. $this->assertSame(0x7FFFFFFE, GPBWire::zigZagEncode64(0x3FFFFFFF));
  219. $this->assertSame(
  220. 0x7FFFFFFF,
  221. GPBWire::zigZagEncode64(0xFFFFFFFFC0000000));
  222. $this->assertSame(
  223. 0xFFFFFFFE,
  224. GPBWire::zigZagEncode64(0x7FFFFFFF));
  225. $this->assertSame(
  226. 0xFFFFFFFF,
  227. GPBWire::zigZagEncode64(0xFFFFFFFF80000000));
  228. $this->assertSame(
  229. -2, // 0xFFFFFFFFFFFFFFFE
  230. GPBWire::zigZagEncode64(0x7FFFFFFFFFFFFFFF));
  231. $this->assertSame(
  232. -1, // 0xFFFFFFFFFFFFFFFF
  233. GPBWire::zigZagEncode64(0x8000000000000000));
  234. $this->assertSame(0, GPBWire::zigZagDecode64(0));
  235. $this->assertSame(-1, GPBWire::zigZagDecode64(1));
  236. $this->assertSame(1, GPBWire::zigZagDecode64(2));
  237. $this->assertSame(-2, GPBWire::zigZagDecode64(3));
  238. }
  239. // Round trip
  240. $this->assertSame(0, GPBWire::zigZagDecode32(GPBWire::zigZagEncode32(0)));
  241. $this->assertSame(1, GPBWire::zigZagDecode32(GPBWire::zigZagEncode32(1)));
  242. $this->assertSame(-1, GPBWire::zigZagDecode32(GPBWire::zigZagEncode32(-1)));
  243. $this->assertSame(14927,
  244. GPBWire::zigZagDecode32(GPBWire::zigZagEncode32(14927)));
  245. $this->assertSame(-3612,
  246. GPBWire::zigZagDecode32(GPBWire::zigZagEncode32(-3612)));
  247. }
  248. public function testDecode()
  249. {
  250. $m = new TestMessage();
  251. $m->decode(TestUtil::getGoldenTestMessage());
  252. TestUtil::assertTestMessage($m);
  253. }
  254. public function testDescriptorDecode()
  255. {
  256. $file_desc_set = new FileDescriptorSet();
  257. $file_desc_set->decode(hex2bin(
  258. "0a3b0a12746573745f696e636c7564652e70726f746f120362617222180a" .
  259. "0b54657374496e636c75646512090a0161180120012805620670726f746f33"));
  260. $this->assertSame(1, sizeof($file_desc_set->getFile()));
  261. $file_desc = $file_desc_set->getFile()[0];
  262. $this->assertSame("test_include.proto", $file_desc->getName());
  263. $this->assertSame("bar", $file_desc->getPackage());
  264. $this->assertSame(0, sizeof($file_desc->getDependency()));
  265. $this->assertSame(1, sizeof($file_desc->getMessageType()));
  266. $this->assertSame(0, sizeof($file_desc->getEnumType()));
  267. $this->assertSame("proto3", $file_desc->getSyntax());
  268. $desc = $file_desc->getMessageType()[0];
  269. $this->assertSame("TestInclude", $desc->getName());
  270. $this->assertSame(1, sizeof($desc->getField()));
  271. $this->assertSame(0, sizeof($desc->getNestedType()));
  272. $this->assertSame(0, sizeof($desc->getEnumType()));
  273. $this->assertSame(0, sizeof($desc->getOneofDecl()));
  274. $field = $desc->getField()[0];
  275. $this->assertSame("a", $field->getName());
  276. $this->assertSame(1, $field->getNumber());
  277. $this->assertSame(GPBLabel::OPTIONAL, $field->getLabel());
  278. $this->assertSame(GPBType::INT32, $field->getType());
  279. }
  280. public function testReadVarint64()
  281. {
  282. $var = 0;
  283. // Empty buffer.
  284. $input = new InputStream(hex2bin(''));
  285. $this->assertFalse($input->readVarint64($var));
  286. // The largest varint is 10 bytes long.
  287. $input = new InputStream(hex2bin('8080808080808080808001'));
  288. $this->assertFalse($input->readVarint64($var));
  289. // Corrupted varint.
  290. $input = new InputStream(hex2bin('808080'));
  291. $this->assertFalse($input->readVarint64($var));
  292. // Normal case.
  293. $input = new InputStream(hex2bin('808001'));
  294. $this->assertTrue($input->readVarint64($var));
  295. if (PHP_INT_SIZE == 4) {
  296. $this->assertSame('16384', $var);
  297. } else {
  298. $this->assertSame(16384, $var);
  299. }
  300. $this->assertFalse($input->readVarint64($var));
  301. // Read two varint.
  302. $input = new InputStream(hex2bin('808001808002'));
  303. $this->assertTrue($input->readVarint64($var));
  304. if (PHP_INT_SIZE == 4) {
  305. $this->assertSame('16384', $var);
  306. } else {
  307. $this->assertSame(16384, $var);
  308. }
  309. $this->assertTrue($input->readVarint64($var));
  310. if (PHP_INT_SIZE == 4) {
  311. $this->assertSame('32768', $var);
  312. } else {
  313. $this->assertSame(32768, $var);
  314. }
  315. $this->assertFalse($input->readVarint64($var));
  316. }
  317. public function testReadVarint32()
  318. {
  319. $var = 0;
  320. // Empty buffer.
  321. $input = new InputStream(hex2bin(''));
  322. $this->assertFalse($input->readVarint32($var));
  323. // The largest varint is 10 bytes long.
  324. $input = new InputStream(hex2bin('8080808080808080808001'));
  325. $this->assertFalse($input->readVarint32($var));
  326. // Corrupted varint.
  327. $input = new InputStream(hex2bin('808080'));
  328. $this->assertFalse($input->readVarint32($var));
  329. // Normal case.
  330. $input = new InputStream(hex2bin('808001'));
  331. $this->assertTrue($input->readVarint32($var));
  332. $this->assertSame(16384, $var);
  333. $this->assertFalse($input->readVarint32($var));
  334. // Read two varint.
  335. $input = new InputStream(hex2bin('808001808002'));
  336. $this->assertTrue($input->readVarint32($var));
  337. $this->assertSame(16384, $var);
  338. $this->assertTrue($input->readVarint32($var));
  339. $this->assertSame(32768, $var);
  340. $this->assertFalse($input->readVarint32($var));
  341. // Read a 64-bit integer. High-order bits should be discarded.
  342. $input = new InputStream(hex2bin('808081808001'));
  343. $this->assertTrue($input->readVarint32($var));
  344. $this->assertSame(16384, $var);
  345. $this->assertFalse($input->readVarint32($var));
  346. }
  347. public function testReadTag()
  348. {
  349. $input = new InputStream(hex2bin('808001'));
  350. $tag = $input->readTag();
  351. $this->assertSame(16384, $tag);
  352. $tag = $input->readTag();
  353. $this->assertSame(0, $tag);
  354. }
  355. public function testPushPopLimit()
  356. {
  357. $input = new InputStream(hex2bin('808001'));
  358. $old_limit = $input->pushLimit(0);
  359. $tag = $input->readTag();
  360. $this->assertSame(0, $tag);
  361. $input->popLimit($old_limit);
  362. $tag = $input->readTag();
  363. $this->assertSame(16384, $tag);
  364. }
  365. public function testReadRaw()
  366. {
  367. $input = new InputStream(hex2bin('808001'));
  368. $buffer = null;
  369. $this->assertTrue($input->readRaw(3, $buffer));
  370. $this->assertSame(hex2bin('808001'), $buffer);
  371. $this->assertFalse($input->readRaw(1, $buffer));
  372. }
  373. public function testWriteVarint32()
  374. {
  375. $output = new OutputStream(3);
  376. $output->writeVarint32(16384);
  377. $this->assertSame(hex2bin('808001'), $output->getData());
  378. }
  379. public function testWriteVarint64()
  380. {
  381. $output = new OutputStream(10);
  382. $output->writeVarint64(-43);
  383. $this->assertSame(hex2bin('D5FFFFFFFFFFFFFFFF01'), $output->getData());
  384. }
  385. public function testWriteLittleEndian32()
  386. {
  387. $output = new OutputStream(4);
  388. $output->writeLittleEndian32(46);
  389. $this->assertSame(hex2bin('2E000000'), $output->getData());
  390. }
  391. public function testWriteLittleEndian64()
  392. {
  393. $output = new OutputStream(8);
  394. $output->writeLittleEndian64(47);
  395. $this->assertSame(hex2bin('2F00000000000000'), $output->getData());
  396. }
  397. public function testByteSize()
  398. {
  399. $m = new TestMessage();
  400. TestUtil::setTestMessage($m);
  401. $this->assertSame(447, $m->byteSize());
  402. }
  403. public function testPackedByteSize()
  404. {
  405. $m = new TestPackedMessage();
  406. TestUtil::setTestPackedMessage($m);
  407. $this->assertSame(156, $m->byteSize());
  408. }
  409. }