Search in sources :

Example 6 with Person

use of com.squareup.wire.protos.person.Person 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 7 with Person

use of com.squareup.wire.protos.person.Person 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)

Aggregations

Person (com.squareup.wire.protos.person.Person)7 Test (org.junit.Test)7 PhoneNumber (com.squareup.wire.protos.person.Person.PhoneNumber)2 ByteString (okio.ByteString)2 Buffer (okio.Buffer)1