OLD | NEW |
| (Empty) |
1 <?php | |
2 | |
3 require_once('test_base.php'); | |
4 require_once('test_util.php'); | |
5 | |
6 use Foo\TestMessage; | |
7 use Foo\TestMessage_Sub; | |
8 use Foo\TestPackedMessage; | |
9 use Google\Protobuf\Internal\InputStream; | |
10 use Google\Protobuf\Internal\FileDescriptorSet; | |
11 use Google\Protobuf\Internal\GPBLabel; | |
12 use Google\Protobuf\Internal\GPBType; | |
13 use Google\Protobuf\Internal\GPBWire; | |
14 use Google\Protobuf\Internal\OutputStream; | |
15 | |
16 class ImplementationTest extends TestBase | |
17 { | |
18 | |
19 public function testReadInt32() | |
20 { | |
21 $value = null; | |
22 | |
23 // Positive number. | |
24 $input = new InputStream(hex2bin("01")); | |
25 GPBWire::readInt32($input, $value); | |
26 $this->assertSame(1, $value); | |
27 | |
28 // Negative number. | |
29 $input = new InputStream(hex2bin("ffffffff0f")); | |
30 GPBWire::readInt32($input, $value); | |
31 $this->assertSame(-1, $value); | |
32 | |
33 // Discard overflow bits. | |
34 $input = new InputStream(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 InputStream(hex2bin("01")); | |
45 GPBWire::readUint32($input, $value); | |
46 $this->assertSame(1, $value); | |
47 | |
48 // Max uint32. | |
49 $input = new InputStream(hex2bin("ffffffff0f")); | |
50 GPBWire::readUint32($input, $value); | |
51 $this->assertSame(-1, $value); | |
52 | |
53 // Discard overflow bits. | |
54 $input = new InputStream(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 InputStream(hex2bin("01")); | |
65 GPBWire::readInt64($input, $value); | |
66 $this->assertEquals(1, $value); | |
67 | |
68 // Negative number. | |
69 $input = new InputStream(hex2bin("ffffffffffffffffff01")); | |
70 GPBWire::readInt64($input, $value); | |
71 $this->assertEquals(-1, $value); | |
72 | |
73 // Discard overflow bits. | |
74 $input = new InputStream(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 InputStream(hex2bin("01")); | |
85 GPBWire::readUint64($input, $value); | |
86 $this->assertEquals(1, $value); | |
87 | |
88 // Negative number. | |
89 $input = new InputStream(hex2bin("FFFFFFFFFFFFFFFFFF01")); | |
90 GPBWire::readUint64($input, $value); | |
91 $this->assertEquals(-1, $value); | |
92 | |
93 // Discard overflow bits. | |
94 $input = new InputStream(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 InputStream(hex2bin("00")); | |
104 GPBWire::readSint32($input, $value); | |
105 $this->assertSame(0, $value); | |
106 | |
107 $input = new InputStream(hex2bin("01")); | |
108 GPBWire::readSint32($input, $value); | |
109 $this->assertSame(-1, $value); | |
110 | |
111 $input = new InputStream(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 InputStream(hex2bin("00")); | |
121 GPBWire::readSint64($input, $value); | |
122 $this->assertEquals(0, $value); | |
123 | |
124 $input = new InputStream(hex2bin("01")); | |
125 GPBWire::readSint64($input, $value); | |
126 $this->assertEquals(-1, $value); | |
127 | |
128 $input = new InputStream(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 InputStream(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 InputStream(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 InputStream(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 InputStream(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 InputStream(hex2bin("00")); | |
174 GPBWire::readBool($input, $value); | |
175 $this->assertSame(false, $value); | |
176 | |
177 $input = new InputStream(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 InputStream(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 InputStream(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(-2, GPBWire::zigZagEncode32(0x7FFFFFFF)); | |
211 $this->assertSame(-1, GPBWire::zigZagEncode32(0x80000000)); | |
212 | |
213 $this->assertSame(0, GPBWire::zigZagDecode32(0)); | |
214 $this->assertSame(-1, GPBWire::zigZagDecode32(1)); | |
215 $this->assertSame(1, GPBWire::zigZagDecode32(2)); | |
216 $this->assertSame(-2, GPBWire::zigZagDecode32(3)); | |
217 $this->assertSame(0x3FFFFFFF, GPBWire::zigZagDecode32(0x7FFFFFFE)); | |
218 $this->assertSame(-1073741824, GPBWire::zigZagDecode32(0x7FFFFFFF)); | |
219 $this->assertSame(0x7FFFFFFF, GPBWire::zigZagDecode32(0xFFFFFFFE)); | |
220 $this->assertSame((int)-2147483648,GPBWire::zigZagDecode32(0xFFFFFFFF)); | |
221 | |
222 if (PHP_INT_SIZE == 4) { | |
223 $this->assertSame('0', GPBWire::zigZagEncode64(0)); | |
224 $this->assertSame('1', GPBWire::zigZagEncode64(-1)); | |
225 $this->assertSame('2', GPBWire::zigZagEncode64(1)); | |
226 $this->assertSame('3', GPBWire::zigZagEncode64(-2)); | |
227 $this->assertSame( | |
228 '2147483646', // 0x7FFFFFE | |
229 GPBWire::zigZagEncode64(0x3FFFFFFF)); | |
230 $this->assertSame( | |
231 '2147483647', // 0x7FFFFFF | |
232 GPBWire::zigZagEncode64(-1073741824)); // 0xFFFFFFFFC0000000 | |
233 $this->assertSame( | |
234 '4294967294', // 0xFFFFFFFE | |
235 GPBWire::zigZagEncode64(2147483647)); // 0x7FFFFFFF | |
236 $this->assertSame( | |
237 '4294967295', // 0xFFFFFFFF | |
238 GPBWire::zigZagEncode64(-2147483648)); // 0xFFFFFFFF80000000 | |
239 $this->assertSame( | |
240 '18446744073709551614', // 0xFFFFFFFFFFFFFFFE | |
241 // 0x7FFFFFFFFFFFFFFF | |
242 GPBWire::zigZagEncode64("9223372036854775807")); | |
243 $this->assertSame( | |
244 '18446744073709551615', // 0xFFFFFFFFFFFFFFFF | |
245 // 0x8000000000000000 | |
246 GPBWire::zigZagEncode64("-9223372036854775808")); | |
247 | |
248 $this->assertSame('0', GPBWire::zigZagDecode64(0)); | |
249 $this->assertSame('-1', GPBWire::zigZagDecode64(1)); | |
250 $this->assertSame('1', GPBWire::zigZagDecode64(2)); | |
251 $this->assertSame('-2', GPBWire::zigZagDecode64(3)); | |
252 } else { | |
253 $this->assertSame(0, GPBWire::zigZagEncode64(0)); | |
254 $this->assertSame(1, GPBWire::zigZagEncode64(-1)); | |
255 $this->assertSame(2, GPBWire::zigZagEncode64(1)); | |
256 $this->assertSame(3, GPBWire::zigZagEncode64(-2)); | |
257 $this->assertSame(0x7FFFFFFE, GPBWire::zigZagEncode64(0x3FFFFFFF)); | |
258 $this->assertSame( | |
259 0x7FFFFFFF, | |
260 GPBWire::zigZagEncode64(0xFFFFFFFFC0000000)); | |
261 $this->assertSame( | |
262 0xFFFFFFFE, | |
263 GPBWire::zigZagEncode64(0x7FFFFFFF)); | |
264 $this->assertSame( | |
265 0xFFFFFFFF, | |
266 GPBWire::zigZagEncode64(0xFFFFFFFF80000000)); | |
267 $this->assertSame( | |
268 -2, // 0xFFFFFFFFFFFFFFFE | |
269 GPBWire::zigZagEncode64(0x7FFFFFFFFFFFFFFF)); | |
270 $this->assertSame( | |
271 -1, // 0xFFFFFFFFFFFFFFFF | |
272 GPBWire::zigZagEncode64(0x8000000000000000)); | |
273 | |
274 $this->assertSame(0, GPBWire::zigZagDecode64(0)); | |
275 $this->assertSame(-1, GPBWire::zigZagDecode64(1)); | |
276 $this->assertSame(1, GPBWire::zigZagDecode64(2)); | |
277 $this->assertSame(-2, GPBWire::zigZagDecode64(3)); | |
278 } | |
279 | |
280 // Round trip | |
281 $this->assertSame(0, GPBWire::zigZagDecode32(GPBWire::zigZagEncode32(0))
); | |
282 $this->assertSame(1, GPBWire::zigZagDecode32(GPBWire::zigZagEncode32(1))
); | |
283 $this->assertSame(-1, GPBWire::zigZagDecode32(GPBWire::zigZagEncode32(-1
))); | |
284 $this->assertSame(14927, | |
285 GPBWire::zigZagDecode32(GPBWire::zigZagEncode32(14927))); | |
286 $this->assertSame(-3612, | |
287 GPBWire::zigZagDecode32(GPBWire::zigZagEncode32(-3612))); | |
288 } | |
289 | |
290 public function testDecode() | |
291 { | |
292 $m = new TestMessage(); | |
293 $m->decode(TestUtil::getGoldenTestMessage()); | |
294 TestUtil::assertTestMessage($m); | |
295 } | |
296 | |
297 public function testDescriptorDecode() | |
298 { | |
299 $file_desc_set = new FileDescriptorSet(); | |
300 $file_desc_set->decode(hex2bin( | |
301 "0a3b0a12746573745f696e636c7564652e70726f746f120362617222180a" . | |
302 "0b54657374496e636c75646512090a0161180120012805620670726f746f33")); | |
303 | |
304 $this->assertSame(1, sizeof($file_desc_set->getFile())); | |
305 | |
306 $file_desc = $file_desc_set->getFile()[0]; | |
307 $this->assertSame("test_include.proto", $file_desc->getName()); | |
308 $this->assertSame("bar", $file_desc->getPackage()); | |
309 $this->assertSame(0, sizeof($file_desc->getDependency())); | |
310 $this->assertSame(1, sizeof($file_desc->getMessageType())); | |
311 $this->assertSame(0, sizeof($file_desc->getEnumType())); | |
312 $this->assertSame("proto3", $file_desc->getSyntax()); | |
313 | |
314 $desc = $file_desc->getMessageType()[0]; | |
315 $this->assertSame("TestInclude", $desc->getName()); | |
316 $this->assertSame(1, sizeof($desc->getField())); | |
317 $this->assertSame(0, sizeof($desc->getNestedType())); | |
318 $this->assertSame(0, sizeof($desc->getEnumType())); | |
319 $this->assertSame(0, sizeof($desc->getOneofDecl())); | |
320 | |
321 $field = $desc->getField()[0]; | |
322 $this->assertSame("a", $field->getName()); | |
323 $this->assertSame(1, $field->getNumber()); | |
324 $this->assertSame(GPBLabel::OPTIONAL, $field->getLabel()); | |
325 $this->assertSame(GPBType::INT32, $field->getType()); | |
326 } | |
327 | |
328 public function testReadVarint64() | |
329 { | |
330 $var = 0; | |
331 | |
332 // Empty buffer. | |
333 $input = new InputStream(hex2bin('')); | |
334 $this->assertFalse($input->readVarint64($var)); | |
335 | |
336 // The largest varint is 10 bytes long. | |
337 $input = new InputStream(hex2bin('8080808080808080808001')); | |
338 $this->assertFalse($input->readVarint64($var)); | |
339 | |
340 // Corrupted varint. | |
341 $input = new InputStream(hex2bin('808080')); | |
342 $this->assertFalse($input->readVarint64($var)); | |
343 | |
344 // Normal case. | |
345 $input = new InputStream(hex2bin('808001')); | |
346 $this->assertTrue($input->readVarint64($var)); | |
347 if (PHP_INT_SIZE == 4) { | |
348 $this->assertSame('16384', $var); | |
349 } else { | |
350 $this->assertSame(16384, $var); | |
351 } | |
352 $this->assertFalse($input->readVarint64($var)); | |
353 | |
354 // Read two varint. | |
355 $input = new InputStream(hex2bin('808001808002')); | |
356 $this->assertTrue($input->readVarint64($var)); | |
357 if (PHP_INT_SIZE == 4) { | |
358 $this->assertSame('16384', $var); | |
359 } else { | |
360 $this->assertSame(16384, $var); | |
361 } | |
362 $this->assertTrue($input->readVarint64($var)); | |
363 if (PHP_INT_SIZE == 4) { | |
364 $this->assertSame('32768', $var); | |
365 } else { | |
366 $this->assertSame(32768, $var); | |
367 } | |
368 $this->assertFalse($input->readVarint64($var)); | |
369 } | |
370 | |
371 public function testReadVarint32() | |
372 { | |
373 $var = 0; | |
374 | |
375 // Empty buffer. | |
376 $input = new InputStream(hex2bin('')); | |
377 $this->assertFalse($input->readVarint32($var)); | |
378 | |
379 // The largest varint is 10 bytes long. | |
380 $input = new InputStream(hex2bin('8080808080808080808001')); | |
381 $this->assertFalse($input->readVarint32($var)); | |
382 | |
383 // Corrupted varint. | |
384 $input = new InputStream(hex2bin('808080')); | |
385 $this->assertFalse($input->readVarint32($var)); | |
386 | |
387 // Normal case. | |
388 $input = new InputStream(hex2bin('808001')); | |
389 $this->assertTrue($input->readVarint32($var)); | |
390 $this->assertSame(16384, $var); | |
391 $this->assertFalse($input->readVarint32($var)); | |
392 | |
393 // Read two varint. | |
394 $input = new InputStream(hex2bin('808001808002')); | |
395 $this->assertTrue($input->readVarint32($var)); | |
396 $this->assertSame(16384, $var); | |
397 $this->assertTrue($input->readVarint32($var)); | |
398 $this->assertSame(32768, $var); | |
399 $this->assertFalse($input->readVarint32($var)); | |
400 | |
401 // Read a 64-bit integer. High-order bits should be discarded. | |
402 $input = new InputStream(hex2bin('808081808001')); | |
403 $this->assertTrue($input->readVarint32($var)); | |
404 $this->assertSame(16384, $var); | |
405 $this->assertFalse($input->readVarint32($var)); | |
406 } | |
407 | |
408 public function testReadTag() | |
409 { | |
410 $input = new InputStream(hex2bin('808001')); | |
411 $tag = $input->readTag(); | |
412 $this->assertSame(16384, $tag); | |
413 $tag = $input->readTag(); | |
414 $this->assertSame(0, $tag); | |
415 } | |
416 | |
417 public function testPushPopLimit() | |
418 { | |
419 $input = new InputStream(hex2bin('808001')); | |
420 $old_limit = $input->pushLimit(0); | |
421 $tag = $input->readTag(); | |
422 $this->assertSame(0, $tag); | |
423 $input->popLimit($old_limit); | |
424 $tag = $input->readTag(); | |
425 $this->assertSame(16384, $tag); | |
426 } | |
427 | |
428 public function testReadRaw() | |
429 { | |
430 $input = new InputStream(hex2bin('808001')); | |
431 $buffer = null; | |
432 | |
433 $this->assertTrue($input->readRaw(3, $buffer)); | |
434 $this->assertSame(hex2bin('808001'), $buffer); | |
435 | |
436 $this->assertFalse($input->readRaw(1, $buffer)); | |
437 } | |
438 | |
439 public function testWriteVarint32() | |
440 { | |
441 $output = new OutputStream(3); | |
442 $output->writeVarint32(16384); | |
443 $this->assertSame(hex2bin('808001'), $output->getData()); | |
444 } | |
445 | |
446 public function testWriteVarint64() | |
447 { | |
448 $output = new OutputStream(10); | |
449 $output->writeVarint64(-43); | |
450 $this->assertSame(hex2bin('D5FFFFFFFFFFFFFFFF01'), $output->getData()); | |
451 } | |
452 | |
453 public function testWriteLittleEndian32() | |
454 { | |
455 $output = new OutputStream(4); | |
456 $output->writeLittleEndian32(46); | |
457 $this->assertSame(hex2bin('2E000000'), $output->getData()); | |
458 } | |
459 | |
460 public function testWriteLittleEndian64() | |
461 { | |
462 $output = new OutputStream(8); | |
463 $output->writeLittleEndian64(47); | |
464 $this->assertSame(hex2bin('2F00000000000000'), $output->getData()); | |
465 } | |
466 | |
467 public function testByteSize() | |
468 { | |
469 $m = new TestMessage(); | |
470 TestUtil::setTestMessage($m); | |
471 $this->assertSame(447, $m->byteSize()); | |
472 } | |
473 | |
474 public function testPackedByteSize() | |
475 { | |
476 $m = new TestPackedMessage(); | |
477 TestUtil::setTestPackedMessage($m); | |
478 $this->assertSame(156, $m->byteSize()); | |
479 } | |
480 } | |
OLD | NEW |