Search in sources :

Example 41 with ByteString

use of okio.ByteString in project wire by square.

the class WireTest method testBadEnum.

@Test
public void testBadEnum() throws IOException {
    Person person = new Person.Builder().id(1).name("Joe Schmoe").phone(Arrays.asList(new PhoneNumber.Builder().number("555-1212").type(PhoneType.WORK).build())).build();
    assertThat(person.phone.get(0).type).isEqualTo(PhoneType.WORK);
    ProtoAdapter<Person> adapter = Person.ADAPTER;
    byte[] data = adapter.encode(person);
    assertThat(data[27]).isEqualTo((byte) PhoneType.WORK.getValue());
    // Corrupt the PhoneNumber type field with an undefined value
    data[27] = 17;
    // Parsed PhoneNumber has no value set
    Person result = adapter.decode(data);
    assertThat(result.phone.get(0).type).isNull();
    // The value 17 will be stored as an unknown varint with tag number 2
    ByteString unknownFields = result.phone.get(0).unknownFields();
    ProtoReader reader = new ProtoReader(new Buffer().write(unknownFields));
    long token = reader.beginMessage();
    assertThat(reader.nextTag()).isEqualTo(2);
    assertThat(reader.peekFieldEncoding()).isEqualTo(FieldEncoding.VARINT);
    assertThat(FieldEncoding.VARINT.rawProtoAdapter().decode(reader)).isEqualTo(17L);
    assertThat(reader.nextTag()).isEqualTo(-1);
    reader.endMessage(token);
    // Serialize again, value is preserved
    byte[] newData = adapter.encode(result);
    assertThat(data).isEqualTo(newData);
}
Also used : Buffer(okio.Buffer) ByteString(okio.ByteString) Person(com.squareup.wire.protos.person.Person) Test(org.junit.Test)

Example 42 with ByteString

use of okio.ByteString in project wire by square.

the class ParseTest method lastValueWinsForRepeatedValueOfNonrepeatedField.

@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);
}
Also used : ByteString(okio.ByteString) OneField(com.squareup.wire.protos.edgecases.OneField) Test(org.junit.Test)

Example 43 with ByteString

use of okio.ByteString in project wire by square.

the class ProtoAdapterTest method getFromClass.

@Test
public void getFromClass() throws Exception {
    Person person = new Person.Builder().id(99).name("Omar Little").build();
    ByteString encoded = ByteString.decodeHex("0a0b4f6d6172204c6974746c651063");
    ProtoAdapter<Person> personAdapter = ProtoAdapter.get(Person.class);
    assertThat(ByteString.of(personAdapter.encode(person))).isEqualTo(encoded);
    assertThat(personAdapter.decode(encoded)).isEqualTo(person);
}
Also used : ByteString(okio.ByteString) Person(com.squareup.wire.protos.person.Person) Test(org.junit.Test)

Example 44 with ByteString

use of okio.ByteString in project wire by square.

the class ProtoReaderTest method packedExposedAsRepeated.

@Test
public void packedExposedAsRepeated() throws IOException {
    ByteString packedEncoded = ByteString.decodeHex("d20504d904bd05");
    ProtoReader reader = new ProtoReader(new Buffer().write(packedEncoded));
    long token = reader.beginMessage();
    assertThat(reader.nextTag()).isEqualTo(90);
    assertThat(ProtoAdapter.INT32.decode(reader)).isEqualTo(601);
    assertThat(reader.nextTag()).isEqualTo(90);
    assertThat(ProtoAdapter.INT32.decode(reader)).isEqualTo(701);
    assertThat(reader.nextTag()).isEqualTo(-1);
    reader.endMessage(token);
}
Also used : Buffer(okio.Buffer) ByteString(okio.ByteString) Test(org.junit.Test)

Example 45 with ByteString

use of okio.ByteString in project okhttp by square.

the class AutobahnTester method runTest.

private void runTest(final long number, final long count) {
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicLong startNanos = new AtomicLong();
    newWebSocket("/runCase?case=" + number + "&agent=okhttp", new WebSocketListener() {

        @Override
        public void onOpen(WebSocket webSocket, Response response) {
            System.out.println("Executing test case " + number + "/" + count);
            startNanos.set(System.nanoTime());
        }

        @Override
        public void onMessage(final WebSocket webSocket, final ByteString bytes) {
            webSocket.send(bytes);
        }

        @Override
        public void onMessage(final WebSocket webSocket, final String text) {
            webSocket.send(text);
        }

        @Override
        public void onClosing(WebSocket webSocket, int code, String reason) {
            webSocket.close(1000, null);
            latch.countDown();
        }

        @Override
        public void onFailure(WebSocket webSocket, Throwable t, Response response) {
            t.printStackTrace(System.out);
            latch.countDown();
        }
    });
    try {
        if (!latch.await(30, TimeUnit.SECONDS)) {
            throw new IllegalStateException("Timed out waiting for test " + number + " to finish.");
        }
    } catch (InterruptedException e) {
        throw new AssertionError();
    }
    long endNanos = System.nanoTime();
    long tookMs = TimeUnit.NANOSECONDS.toMillis(endNanos - startNanos.get());
    System.out.println("Took " + tookMs + "ms");
}
Also used : ByteString(okio.ByteString) ByteString(okio.ByteString) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong)

Aggregations

ByteString (okio.ByteString)59 Test (org.junit.Test)37 Buffer (okio.Buffer)26 MockResponse (okhttp3.mockwebserver.MockResponse)11 ProtocolException (java.net.ProtocolException)5 IOException (java.io.IOException)4 OneField (com.squareup.wire.protos.edgecases.OneField)3 EOFException (java.io.EOFException)3 BufferedSink (okio.BufferedSink)3 BufferedSource (okio.BufferedSource)3 AllTypes (com.squareup.wire.protos.alltypes.AllTypes)2 Person (com.squareup.wire.protos.person.Person)2 ArrayList (java.util.ArrayList)2 Headers (okhttp3.Headers)2 Request (okhttp3.Request)2 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)2 GzipSink (okio.GzipSink)2 Ignore (org.junit.Ignore)2 Phone (retrofit2.converter.protobuf.PhoneProtos.Phone)2 GsonBuilder (com.google.gson.GsonBuilder)1