public synchronized void connectionPreface() throws IOException { if (closed) throw new IOException("closed"); if (!client) return; // Nothing to write; servers don't send connection headers! if (logger.isLoggable(FINE)) { logger.fine(format(">> CONNECTION %s", CONNECTION_PREFACE.hex())); } sink.write(CONNECTION_PREFACE.toByteArray()); sink.flush(); }
private ByteString gzipHeaderWithFlags(byte flags) { byte[] result = gzipHeader.toByteArray(); result[3] = flags; return ByteString.of(result); }
@Test public void testDefaults() throws Exception { assertThat(AllTypes.DEFAULT_DEFAULT_BOOL).isEqualTo((Object) true); // original: "<c-cedilla>ok\a\b\f\n\r\t\v\1\01\001\17\017\176\x1\x01\x11\X1\X01\X11g<u umlaut>zel" assertThat(AllTypes.DEFAULT_DEFAULT_STRING).isEqualTo( "çok\u0007\b\f\n\r\t\u000b\u0001\u0001" + "\u0001\u000f\u000f~\u0001\u0001\u0011\u0001\u0001\u0011güzel"); assertThat(new String(AllTypes.DEFAULT_DEFAULT_BYTES.toByteArray(), "ISO-8859-1")).isEqualTo( "çok\u0007\b\f\n\r\t\u000b\u0001\u0001\u0001\u000f\u000f~\u0001\u0001\u0011\u0001\u0001" + "\u0011güzel"); }
@Test public void upToRecursionLimit() throws Exception { // tag 2: nested message (64 times) // tag 1: signed varint32 456 ByteString data = ByteString.decodeHex("127e127c127a12781276127412721270126e126c126a12681266126" + "412621260125e125c125a12581256125412521250124e124c124a12481246124412421240123e123c123a123" + "81236123412321230122e122c122a12281226122412221220121e121c121a12181216121412121210120e120" + "c120a1208120612041202120008c803"); Recursive recursive = Recursive.ADAPTER.decode(data.toByteArray()); assertThat(recursive.value.intValue()).isEqualTo(456); }
@Test public void unknownTypeThrowsIOException() throws Exception { // tag 1 / type 0: 456 // tag 2 / type 7: 789 ByteString data = ByteString.decodeHex("08c803179506"); try { OneField.ADAPTER.decode(data.toByteArray()); fail(); } catch (ProtocolException expected) { assertThat(expected).hasMessage("Unexpected field encoding: 7"); } }
@Test public void startsWithByteArray() throws Exception { ByteString byteString = factory.decodeHex("112233"); assertTrue(byteString.startsWith(ByteString.decodeHex("").toByteArray())); assertTrue(byteString.startsWith(ByteString.decodeHex("11").toByteArray())); assertTrue(byteString.startsWith(ByteString.decodeHex("1122").toByteArray())); assertTrue(byteString.startsWith(ByteString.decodeHex("112233").toByteArray())); assertFalse(byteString.startsWith(ByteString.decodeHex("2233").toByteArray())); assertFalse(byteString.startsWith(ByteString.decodeHex("11223344").toByteArray())); assertFalse(byteString.startsWith(ByteString.decodeHex("112244").toByteArray())); }
@Test public void endsWithByteArray() throws Exception { ByteString byteString = factory.decodeHex("112233"); assertTrue(byteString.endsWith(ByteString.decodeHex("").toByteArray())); assertTrue(byteString.endsWith(ByteString.decodeHex("33").toByteArray())); assertTrue(byteString.endsWith(ByteString.decodeHex("2233").toByteArray())); assertTrue(byteString.endsWith(ByteString.decodeHex("112233").toByteArray())); assertFalse(byteString.endsWith(ByteString.decodeHex("1122").toByteArray())); assertFalse(byteString.endsWith(ByteString.decodeHex("00112233").toByteArray())); assertFalse(byteString.endsWith(ByteString.decodeHex("002233").toByteArray())); }
@Test public void gunzip_withHCRC() throws Exception { CRC32 hcrc = new CRC32(); ByteString gzipHeader = gzipHeaderWithFlags((byte) 0x02); hcrc.update(gzipHeader.toByteArray()); Buffer gzipped = new Buffer(); gzipped.write(gzipHeader); gzipped.writeShort(TestUtil.reverseBytes((short) hcrc.getValue())); // little endian gzipped.write(deflated); gzipped.write(gzipTrailer); assertGzipped(gzipped); }
@Test public void lastIndexOfByteArray() throws Exception { ByteString byteString = factory.decodeHex("112233"); assertEquals(0, byteString.lastIndexOf(ByteString.decodeHex("112233").toByteArray())); assertEquals(1, byteString.lastIndexOf(ByteString.decodeHex("2233").toByteArray())); assertEquals(2, byteString.lastIndexOf(ByteString.decodeHex("33").toByteArray())); assertEquals(3, byteString.lastIndexOf(ByteString.decodeHex("").toByteArray())); }
@Test public void indexOfByteArray() throws Exception { ByteString byteString = factory.decodeHex("112233"); assertEquals(0, byteString.indexOf(ByteString.decodeHex("112233").toByteArray())); assertEquals(1, byteString.indexOf(ByteString.decodeHex("2233").toByteArray())); assertEquals(2, byteString.indexOf(ByteString.decodeHex("33").toByteArray())); assertEquals(-1, byteString.indexOf(ByteString.decodeHex("112244").toByteArray())); }
@Test public void encodeDecodeStringUtf16be() throws Exception { Charset utf16be = Charset.forName("UTF-16BE"); ByteString byteString = ByteString.encodeString(bronzeHorseman, utf16be); assertByteArraysEquals(byteString.toByteArray(), bronzeHorseman.getBytes(utf16be)); assertEquals(byteString, ByteString.decodeHex("041d043000200431043504400435043304430020043f0443" + "04410442044b043d043d044b044500200432043e043b043d")); assertEquals(bronzeHorseman, byteString.string(utf16be)); }
@Test public void encodeDecodeStringAsciiIsLossy() throws Exception { Charset ascii = Charset.forName("US-ASCII"); ByteString byteString = ByteString.encodeString(bronzeHorseman, ascii); assertByteArraysEquals(byteString.toByteArray(), bronzeHorseman.getBytes(ascii)); assertEquals(byteString, ByteString.decodeHex("3f3f203f3f3f3f3f3f203f3f3f3f3f3f3f3f3f203f3f3f3f")); assertEquals("?? ?????? ????????? ????", byteString.string(ascii)); }
@Test public void encodeDecodeStringUtf32be() throws Exception { Charset utf32be = Charset.forName("UTF-32BE"); ByteString byteString = ByteString.encodeString(bronzeHorseman, utf32be); assertByteArraysEquals(byteString.toByteArray(), bronzeHorseman.getBytes(utf32be)); assertEquals(byteString, ByteString.decodeHex("0000041d0000043000000020000004310000043500000440" + "000004350000043300000443000000200000043f0000044300000441000004420000044b0000043d0000043d" + "0000044b0000044500000020000004320000043e0000043b0000043d")); assertEquals(bronzeHorseman, byteString.string(utf32be)); }
@Test public void encodeDecodeStringUtf8() throws Exception { Charset utf8 = Charset.forName("UTF-8"); ByteString byteString = ByteString.encodeString(bronzeHorseman, utf8); assertByteArraysEquals(byteString.toByteArray(), bronzeHorseman.getBytes(utf8)); assertEquals(byteString, ByteString.decodeHex("d09dd0b020d0b1d0b5d180d0b5d0b3d18320d0bfd183d181" + "d182d18bd0bdd0bdd18bd18520d0b2d0bed0bbd0bd")); assertEquals(bronzeHorseman, byteString.string(utf8)); }
@Test public void lastValueWinsForRepeatedValueOfNonrepeatedField() throws Exception { // tag 1 / type 0: 456 // tag 1 / type 0: 789 ByteString data = ByteString.decodeHex("08c803089506"); OneField oneField = OneField.ADAPTER.decode(data.toByteArray()); assertThat(new OneField.Builder().opt_int32(789).build()).isEqualTo(oneField); }
@Test public void utf8() throws Exception { ByteString byteString = factory.encodeUtf8(bronzeHorseman); assertByteArraysEquals(byteString.toByteArray(), bronzeHorseman.getBytes(Charsets.UTF_8)); assertTrue(byteString.equals(ByteString.of(bronzeHorseman.getBytes(Charsets.UTF_8)))); assertEquals(byteString.utf8(), bronzeHorseman); }
@Test public void unknownTagIgnored() throws Exception { // tag 1 / type 0: 456 // tag 2 / type 0: 789 ByteString data = ByteString.decodeHex("08c803109506"); OneField oneField = OneField.ADAPTER.decode(data.toByteArray()); OneField expected = new OneField.Builder().opt_int32(456).build(); assertThat(oneField).isNotEqualTo(expected); assertThat(oneField.withoutUnknownFields()).isEqualTo(expected); }
@Ignore("TODO(jwilson)") @Test public void typeMismatchHonorsWireDeclaredType() throws Exception { // tag 1 / 3-byte length-delimited string: 0x109506 // (0x109506 is a well-formed proto message that sets tag 2 to 456). ByteString data = ByteString.decodeHex("0a03109506"); OneField oneField = OneField.ADAPTER.decode(data.toByteArray()); assertThat(oneField).isEqualTo(new OneField.Builder().opt_int32(3).build()); }
@Test public void gunzipWhenCRCIncorrect() { Buffer gzipped = new Buffer(); gzipped.write(gzipHeader); gzipped.write(deflated); gzipped.writeInt(TestUtil.reverseBytes(0x1234567)); // wrong CRC gzipped.write(gzipTrailer.toByteArray(), 3, 4); try { gunzip(gzipped); fail(); } catch (IOException e) { assertEquals("CRC: actual 0x37ad8f8d != expected 0x01234567", e.getMessage()); } }
@Test public void gunzipWhenLengthIncorrect() { Buffer gzipped = new Buffer(); gzipped.write(gzipHeader); gzipped.write(deflated); gzipped.write(gzipTrailer.toByteArray(), 0, 4); gzipped.writeInt(TestUtil.reverseBytes(0x123456)); // wrong length try { gunzip(gzipped); fail(); } catch (IOException e) { assertEquals("ISIZE: actual 0x00000020 != expected 0x00123456", e.getMessage()); } }