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