php_implementation_test.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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\GPBUtil;
  11. use Google\Protobuf\Internal\Int64;
  12. use Google\Protobuf\Internal\Uint64;
  13. use Google\Protobuf\Internal\GPBLabel;
  14. use Google\Protobuf\Internal\GPBType;
  15. use Google\Protobuf\Internal\GPBWire;
  16. use Google\Protobuf\Internal\OutputStream;
  17. use Google\Protobuf\Internal\RepeatedField;
  18. class ImplementationTest extends TestBase
  19. {
  20. public function testReadInt32()
  21. {
  22. $value = null;
  23. // Positive number.
  24. $input = new InputStream(hex2bin("01"));
  25. GPBWire::readInt32($input, $value);
  26. $this->assertSame(1, $value);
  27. // Negative number.
  28. $input = new InputStream(hex2bin("ffffffff0f"));
  29. GPBWire::readInt32($input, $value);
  30. $this->assertSame(-1, $value);
  31. // Discard overflow bits.
  32. $input = new InputStream(hex2bin("ffffffff7f"));
  33. GPBWire::readInt32($input, $value);
  34. $this->assertSame(-1, $value);
  35. }
  36. public function testReadUint32()
  37. {
  38. $value = null;
  39. // Positive number.
  40. $input = new InputStream(hex2bin("01"));
  41. GPBWire::readUint32($input, $value);
  42. $this->assertSame(1, $value);
  43. // Max uint32.
  44. $input = new InputStream(hex2bin("ffffffff0f"));
  45. GPBWire::readUint32($input, $value);
  46. $this->assertSame(-1, $value);
  47. // Discard overflow bits.
  48. $input = new InputStream(hex2bin("ffffffff7f"));
  49. GPBWire::readUint32($input, $value);
  50. $this->assertSame(-1, $value);
  51. }
  52. public function testReadInt64()
  53. {
  54. $value = null;
  55. // Positive number.
  56. $input = new InputStream(hex2bin("01"));
  57. GPBWire::readInt64($input, $value);
  58. $this->assertSame(1, $value->toInteger());
  59. // Negative number.
  60. $input = new InputStream(hex2bin("ffffffffffffffffff01"));
  61. GPBWire::readInt64($input, $value);
  62. $this->assertSame(-1, $value->toInteger());
  63. // Discard overflow bits.
  64. $input = new InputStream(hex2bin("ffffffffffffffffff0f"));
  65. GPBWire::readInt64($input, $value);
  66. $this->assertSame(-1, $value->toInteger());
  67. }
  68. public function testReadUint64()
  69. {
  70. $value = null;
  71. // Positive number.
  72. $input = new InputStream(hex2bin("01"));
  73. GPBWire::readUint64($input, $value);
  74. $this->assertSame(1, $value->toInteger());
  75. // Negative number.
  76. $input = new InputStream(hex2bin("FFFFFFFFFFFFFFFFFF01"));
  77. GPBWire::readUint64($input, $value);
  78. $this->assertSame(-1, $value->toInteger());
  79. // Discard overflow bits.
  80. $input = new InputStream(hex2bin("FFFFFFFFFFFFFFFFFF0F"));
  81. GPBWire::readUint64($input, $value);
  82. $this->assertSame(-1, $value->toInteger());
  83. }
  84. public function testReadSint32()
  85. {
  86. $value = null;
  87. $input = new InputStream(hex2bin("00"));
  88. GPBWire::readSint32($input, $value);
  89. $this->assertSame(0, $value);
  90. $input = new InputStream(hex2bin("01"));
  91. GPBWire::readSint32($input, $value);
  92. $this->assertSame(-1, $value);
  93. $input = new InputStream(hex2bin("02"));
  94. GPBWire::readSint32($input, $value);
  95. $this->assertSame(1, $value);
  96. }
  97. public function testReadSint64()
  98. {
  99. $value = null;
  100. $input = new InputStream(hex2bin("00"));
  101. GPBWire::readSint64($input, $value);
  102. $this->assertEquals(GPBUtil::Int64(0), $value);
  103. $input = new InputStream(hex2bin("01"));
  104. GPBWire::readSint64($input, $value);
  105. $this->assertEquals(GPBUtil::Int64(-1), $value);
  106. $input = new InputStream(hex2bin("02"));
  107. GPBWire::readSint64($input, $value);
  108. $this->assertEquals(GPBUtil::Int64(1), $value);
  109. }
  110. public function testReadFixed32()
  111. {
  112. $value = null;
  113. $input = new InputStream(hex2bin("12345678"));
  114. GPBWire::readFixed32($input, $value);
  115. $this->assertSame(0x78563412, $value);
  116. }
  117. public function testReadFixed64()
  118. {
  119. $value = null;
  120. $input = new InputStream(hex2bin("1234567812345678"));
  121. GPBWire::readFixed64($input, $value);
  122. $this->assertEquals(Uint64::newValue(0x78563412, 0x78563412), $value);
  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. $this->assertEquals(Int64::newValue(0x78563412, 0x78563412), $value);
  161. }
  162. public function testZigZagEncodeDecode()
  163. {
  164. $this->assertSame(0, GPBWire::zigZagEncode32(0));
  165. $this->assertSame(1, GPBWire::zigZagEncode32(-1));
  166. $this->assertSame(2, GPBWire::zigZagEncode32(1));
  167. $this->assertSame(3, GPBWire::zigZagEncode32(-2));
  168. $this->assertSame(0x7FFFFFFE, GPBWire::zigZagEncode32(0x3FFFFFFF));
  169. $this->assertSame(0x7FFFFFFF, GPBWire::zigZagEncode32(0xC0000000));
  170. $this->assertSame(-2, GPBWire::zigZagEncode32(0x7FFFFFFF));
  171. $this->assertSame(-1, GPBWire::zigZagEncode32(0x80000000));
  172. $this->assertSame(0, GPBWire::zigZagDecode32(0));
  173. $this->assertSame(-1, GPBWire::zigZagDecode32(1));
  174. $this->assertSame(1, GPBWire::zigZagDecode32(2));
  175. $this->assertSame(-2, GPBWire::zigZagDecode32(3));
  176. $this->assertSame(0x3FFFFFFF, GPBWire::zigZagDecode32(0x7FFFFFFE));
  177. $this->assertSame(-1073741824, GPBWire::zigZagDecode32(0x7FFFFFFF));
  178. $this->assertSame(0x7FFFFFFF, GPBWire::zigZagDecode32(0xFFFFFFFE));
  179. $this->assertSame(-2147483648, GPBWire::zigZagDecode32(0xFFFFFFFF));
  180. $this->assertEquals(GPBUtil::Uint64(0),
  181. GPBWire::zigZagEncode64(GPBUtil::Int64(0)));
  182. $this->assertEquals(GPBUtil::Uint64(1),
  183. GPBWire::zigZagEncode64(GPBUtil::Int64(-1)));
  184. $this->assertEquals(GPBUtil::Uint64(2),
  185. GPBWire::zigZagEncode64(GPBUtil::Int64(1)));
  186. $this->assertEquals(GPBUtil::Uint64(3),
  187. GPBWire::zigZagEncode64(GPBUtil::Int64(-2)));
  188. $this->assertEquals(
  189. GPBUtil::Uint64(0x000000007FFFFFFE),
  190. GPBWire::zigZagEncode64(GPBUtil::Int64(0x000000003FFFFFFF)));
  191. $this->assertEquals(
  192. GPBUtil::Uint64(0x000000007FFFFFFF),
  193. GPBWire::zigZagEncode64(GPBUtil::Int64(0xFFFFFFFFC0000000)));
  194. $this->assertEquals(
  195. GPBUtil::Uint64(0x00000000FFFFFFFE),
  196. GPBWire::zigZagEncode64(GPBUtil::Int64(0x000000007FFFFFFF)));
  197. $this->assertEquals(
  198. GPBUtil::Uint64(0x00000000FFFFFFFF),
  199. GPBWire::zigZagEncode64(GPBUtil::Int64(0xFFFFFFFF80000000)));
  200. $this->assertEquals(
  201. Uint64::newValue(4294967295, 4294967294),
  202. GPBWire::zigZagEncode64(GPBUtil::Int64(0x7FFFFFFFFFFFFFFF)));
  203. $this->assertEquals(
  204. Uint64::newValue(4294967295, 4294967295),
  205. GPBWire::zigZagEncode64(GPBUtil::Int64(0x8000000000000000)));
  206. $this->assertEquals(GPBUtil::Int64(0),
  207. GPBWire::zigZagDecode64(GPBUtil::Uint64(0)));
  208. $this->assertEquals(GPBUtil::Int64(-1),
  209. GPBWire::zigZagDecode64(GPBUtil::Uint64(1)));
  210. $this->assertEquals(GPBUtil::Int64(1),
  211. GPBWire::zigZagDecode64(GPBUtil::Uint64(2)));
  212. $this->assertEquals(GPBUtil::Int64(-2),
  213. GPBWire::zigZagDecode64(GPBUtil::Uint64(3)));
  214. // Round trip
  215. $this->assertSame(0, GPBWire::zigZagDecode32(GPBWire::zigZagEncode32(0)));
  216. $this->assertSame(1, GPBWire::zigZagDecode32(GPBWire::zigZagEncode32(1)));
  217. $this->assertSame(-1, GPBWire::zigZagDecode32(GPBWire::zigZagEncode32(-1)));
  218. $this->assertSame(14927,
  219. GPBWire::zigZagDecode32(GPBWire::zigZagEncode32(14927)));
  220. $this->assertSame(-3612,
  221. GPBWire::zigZagDecode32(GPBWire::zigZagEncode32(-3612)));
  222. }
  223. public function testDecode()
  224. {
  225. $m = new TestMessage();
  226. $m->decode(TestUtil::getGoldenTestMessage());
  227. TestUtil::assertTestMessage($m);
  228. }
  229. public function testDescriptorDecode()
  230. {
  231. $file_desc_set = new FileDescriptorSet();
  232. $file_desc_set->decode(hex2bin(
  233. "0a3b0a12746573745f696e636c7564652e70726f746f120362617222180a" .
  234. "0b54657374496e636c75646512090a0161180120012805620670726f746f33"));
  235. $this->assertSame(1, sizeof($file_desc_set->getFile()));
  236. $file_desc = $file_desc_set->getFile()[0];
  237. $this->assertSame("test_include.proto", $file_desc->getName());
  238. $this->assertSame("bar", $file_desc->getPackage());
  239. $this->assertSame(0, sizeof($file_desc->getDependency()));
  240. $this->assertSame(1, sizeof($file_desc->getMessageType()));
  241. $this->assertSame(0, sizeof($file_desc->getEnumType()));
  242. $this->assertSame("proto3", $file_desc->getSyntax());
  243. $desc = $file_desc->getMessageType()[0];
  244. $this->assertSame("TestInclude", $desc->getName());
  245. $this->assertSame(1, sizeof($desc->getField()));
  246. $this->assertSame(0, sizeof($desc->getNestedType()));
  247. $this->assertSame(0, sizeof($desc->getEnumType()));
  248. $this->assertSame(0, sizeof($desc->getOneofDecl()));
  249. $field = $desc->getField()[0];
  250. $this->assertSame("a", $field->getName());
  251. $this->assertSame(1, $field->getNumber());
  252. $this->assertSame(GPBLabel::OPTIONAL, $field->getLabel());
  253. $this->assertSame(GPBType::INT32, $field->getType());
  254. }
  255. public function testReadVarint64()
  256. {
  257. $var = 0;
  258. // Empty buffer.
  259. $input = new InputStream(hex2bin(''));
  260. $this->assertFalse($input->readVarint64($var));
  261. // The largest varint is 10 bytes long.
  262. $input = new InputStream(hex2bin('8080808080808080808001'));
  263. $this->assertFalse($input->readVarint64($var));
  264. // Corrupted varint.
  265. $input = new InputStream(hex2bin('808080'));
  266. $this->assertFalse($input->readVarint64($var));
  267. // Normal case.
  268. $input = new InputStream(hex2bin('808001'));
  269. $this->assertTrue($input->readVarint64($var));
  270. $this->assertSame(16384, $var->toInteger());
  271. $this->assertFalse($input->readVarint64($var));
  272. // Read two varint.
  273. $input = new InputStream(hex2bin('808001808002'));
  274. $this->assertTrue($input->readVarint64($var));
  275. $this->assertSame(16384, $var->toInteger());
  276. $this->assertTrue($input->readVarint64($var));
  277. $this->assertSame(32768, $var->toInteger());
  278. $this->assertFalse($input->readVarint64($var));
  279. }
  280. public function testReadVarint32()
  281. {
  282. $var = 0;
  283. // Empty buffer.
  284. $input = new InputStream(hex2bin(''));
  285. $this->assertFalse($input->readVarint32($var));
  286. // The largest varint is 10 bytes long.
  287. $input = new InputStream(hex2bin('8080808080808080808001'));
  288. $this->assertFalse($input->readVarint32($var));
  289. // Corrupted varint.
  290. $input = new InputStream(hex2bin('808080'));
  291. $this->assertFalse($input->readVarint32($var));
  292. // Normal case.
  293. $input = new InputStream(hex2bin('808001'));
  294. $this->assertTrue($input->readVarint32($var));
  295. $this->assertSame(16384, $var);
  296. $this->assertFalse($input->readVarint32($var));
  297. // Read two varint.
  298. $input = new InputStream(hex2bin('808001808002'));
  299. $this->assertTrue($input->readVarint32($var));
  300. $this->assertSame(16384, $var);
  301. $this->assertTrue($input->readVarint32($var));
  302. $this->assertSame(32768, $var);
  303. $this->assertFalse($input->readVarint32($var));
  304. // Read a 64-bit integer. High-order bits should be discarded.
  305. $input = new InputStream(hex2bin('808081808001'));
  306. $this->assertTrue($input->readVarint32($var));
  307. $this->assertSame(16384, $var);
  308. $this->assertFalse($input->readVarint32($var));
  309. }
  310. public function testReadTag()
  311. {
  312. $input = new InputStream(hex2bin('808001'));
  313. $tag = $input->readTag();
  314. $this->assertSame(16384, $tag);
  315. $tag = $input->readTag();
  316. $this->assertSame(0, $tag);
  317. }
  318. public function testPushPopLimit()
  319. {
  320. $input = new InputStream(hex2bin('808001'));
  321. $old_limit = $input->pushLimit(0);
  322. $tag = $input->readTag();
  323. $this->assertSame(0, $tag);
  324. $input->popLimit($old_limit);
  325. $tag = $input->readTag();
  326. $this->assertSame(16384, $tag);
  327. }
  328. public function testReadRaw()
  329. {
  330. $input = new InputStream(hex2bin('808001'));
  331. $buffer = null;
  332. $this->assertTrue($input->readRaw(3, $buffer));
  333. $this->assertSame(hex2bin('808001'), $buffer);
  334. $this->assertFalse($input->readRaw(1, $buffer));
  335. }
  336. public function testWriteVarint32()
  337. {
  338. $output = new OutputStream(3);
  339. $output->writeVarint32(16384);
  340. $this->assertSame(hex2bin('808001'), $output->getData());
  341. }
  342. public function testWriteVarint64()
  343. {
  344. $output = new OutputStream(10);
  345. $output->writeVarint64(-43);
  346. $this->assertSame(hex2bin('D5FFFFFFFFFFFFFFFF01'), $output->getData());
  347. }
  348. public function testWriteLittleEndian32()
  349. {
  350. $output = new OutputStream(4);
  351. $output->writeLittleEndian32(46);
  352. $this->assertSame(hex2bin('2E000000'), $output->getData());
  353. }
  354. public function testWriteLittleEndian64()
  355. {
  356. $output = new OutputStream(8);
  357. $output->writeLittleEndian64(47);
  358. $this->assertSame(hex2bin('2F00000000000000'), $output->getData());
  359. }
  360. public function testByteSize()
  361. {
  362. $m = new TestMessage();
  363. TestUtil::setTestMessage($m);
  364. $this->assertSame(447, $m->byteSize());
  365. }
  366. public function testPackedByteSize()
  367. {
  368. $m = new TestPackedMessage();
  369. TestUtil::setTestPackedMessage($m);
  370. $this->assertSame(156, $m->byteSize());
  371. }
  372. }