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);
}
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);
}
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);
}
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);
}
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");
}
Aggregations