Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IPROTO-273 Elements in collections are not properly limited #195

Merged
merged 1 commit into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,9 @@ private static void writeContainer(ImmutableSerializationContext ctx, TagWriter
Iterator elements = ((IterableElementContainerAdapter) containerMarshaller).getElements(container);
for (int i = 0; i < containerSize; i++) {
Object e = elements.next();
writeMessage(ctx, out, e, true);
TagWriter elementWriter = out.subWriter(WRAPPED_MESSAGE, true).getWriter();
writeMessage(ctx, elementWriter, e, true);
elementWriter.close();
}
if (elements.hasNext()) {
throw new IllegalStateException("Container number of elements mismatch");
Expand All @@ -339,7 +341,9 @@ private static void writeContainer(ImmutableSerializationContext ctx, TagWriter
IndexedElementContainerAdapter adapter = (IndexedElementContainerAdapter) containerMarshaller;
for (int i = 0; i < containerSize; i++) {
Object e = adapter.getElement(container, i);
writeMessage(ctx, out, e, true);
TagWriter elementWriter = out.subWriter(WRAPPED_MESSAGE, true).getWriter();
writeMessage(ctx, elementWriter, e, true);
elementWriter.close();
}
} else {
throw new IllegalStateException("Unknown container adapter kind : " + containerMarshaller.getJavaClass().getName());
Expand Down Expand Up @@ -611,13 +615,23 @@ private static Object readContainer(ImmutableSerializationContext ctx, TagReader
if (containerMarshaller instanceof IterableElementContainerAdapter) {
IterableElementContainerAdapter adapter = (IterableElementContainerAdapter) containerMarshaller;
for (int i = 0; i < containerSize; i++) {
Object e = readMessage(ctx, in, true);
if ((tag = in.readTag()) != (WRAPPED_MESSAGE << WireType.TAG_TYPE_NUM_BITS | WireType.WIRETYPE_LENGTH_DELIMITED)) {
throw new IllegalStateException("Unexpected tag : " + tag + " (Field number : "
+ WireType.getTagFieldNumber(tag) + ", Wire type : " + WireType.getTagWireType(tag) + ")");
}
TagReader elementReader = in.subReaderFromArray().getReader();
Object e = readMessage(ctx, elementReader, true);
adapter.appendElement(container, e);
}
} else if (containerMarshaller instanceof IndexedElementContainerAdapter) {
IndexedElementContainerAdapter adapter = (IndexedElementContainerAdapter) containerMarshaller;
for (int i = 0; i < containerSize; i++) {
Object e = readMessage(ctx, in, true);
if ((tag = in.readTag()) != (WRAPPED_MESSAGE << WireType.TAG_TYPE_NUM_BITS | WireType.WIRETYPE_LENGTH_DELIMITED)) {
throw new IllegalStateException("Unexpected tag : " + tag + " (Field number : "
+ WireType.getTagFieldNumber(tag) + ", Wire type : " + WireType.getTagWireType(tag) + ")");
}
TagReader elementReader = in.subReaderFromArray().getReader();
Object e = readMessage(ctx, elementReader, true);
adapter.setElement(container, i, e);
}
} else {
Expand Down
31 changes: 31 additions & 0 deletions types/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,37 @@
<groupId>${project.groupId}</groupId>
<artifactId>protostream-processor</artifactId>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jul</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package org.infinispan.protostream.types.java;

import org.infinispan.protostream.annotations.ProtoFactory;
import org.infinispan.protostream.annotations.ProtoField;

import java.util.Objects;

public class Book implements Comparable<Book> {

@ProtoField(number = 1)
String title;

@ProtoField(number = 2)
String description;

@ProtoField(number = 3, defaultValue = "2023")
int publicationYear;

@ProtoFactory
public Book(String title, String description, int publicationYear) {
this.title = title;
this.description = description;
this.publicationYear = publicationYear;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Book book = (Book) o;
return publicationYear == book.publicationYear && Objects.equals(title, book.title) && Objects.equals(description, book.description);
}

@Override
public int hashCode() {
return Objects.hash(title, description, publicationYear);
}

@Override
public String toString() {
return "Book{" +
"title='" + title + '\'' +
", description='" + description + '\'' +
", publicationYear=" + publicationYear +
'}';
}

@Override
public int compareTo(Book o) {
int cmp = Integer.compare(this.publicationYear, o.publicationYear);
if (cmp != 0) {
return cmp;
}
cmp = title.compareTo(o.title);
if (cmp != 0) {
return cmp;
}
return description.compareTo(o.description);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.infinispan.protostream.types.java;

import org.infinispan.protostream.GeneratedSchema;
import org.infinispan.protostream.annotations.AutoProtoSchemaBuilder;

@AutoProtoSchemaBuilder(
includeClasses = {
Book.class
},
schemaFileName = "book.proto",
schemaFilePath = "proto/",
schemaPackageName = "library")
public interface BookSchema extends GeneratedSchema {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
package org.infinispan.protostream.types.java;

import org.infinispan.protostream.GeneratedSchema;
import org.infinispan.protostream.ImmutableSerializationContext;
import org.infinispan.protostream.ProtobufUtil;
import org.infinispan.protostream.SerializationContext;
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
import java.util.function.Supplier;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;

public class MarshallingTest {
karesti marked this conversation as resolved.
Show resolved Hide resolved

@Test
public void testStringArrayList() throws IOException {
doCollectionMarshallTest(stringCollection(ArrayList::new), useWrappedMessage());
}

@Test
public void testBookArrayList() throws IOException {
doCollectionMarshallTest(bookCollection(ArrayList::new), useWrappedMessage());
}

@Test
public void testStringHashSet() throws IOException {
doCollectionMarshallTest(stringCollection(HashSet::new), useWrappedMessage());
}

@Test
public void testBookHashSet() throws IOException {
doCollectionMarshallTest(bookCollection(HashSet::new), useWrappedMessage());
}

@Test
public void testStringLinkedHashSet() throws IOException {
doCollectionMarshallTest(stringCollection(LinkedHashSet::new), useWrappedMessage());
}

@Test
public void testBookLinkedHashSet() throws IOException {
doCollectionMarshallTest(bookCollection(LinkedHashSet::new), useWrappedMessage());
}

@Test
public void testStringLinkedList() throws IOException {
doCollectionMarshallTest(stringCollection(LinkedList::new), useWrappedMessage());
}

@Test
public void testBookLinkedList() throws IOException {
doCollectionMarshallTest(bookCollection(LinkedList::new), useWrappedMessage());
}

@Test
public void testStringTreeSet() throws IOException {
doCollectionMarshallTest(stringCollection(TreeSet::new), useWrappedMessage());
}

@Test
public void testBookTreeSet() throws IOException {
doCollectionMarshallTest(bookCollection(TreeSet::new), useWrappedMessage());
}

@Test
public void testStringArray() throws IOException {
doArrayMarshallTest(stringArray(), useWrappedMessage());
}

@Test
public void testBookArray() throws IOException {
doArrayMarshallTest(bookArray(), useWrappedMessage());
}

@Test
public void testUUIDUsingByteArray() throws IOException {
doUUIDMarshallTest(useByteArray());
}

@Test
public void testUUIDUsingInputStream() throws IOException {
doUUIDMarshallTest(useInputStream());
}

@Test
public void testUUIDUsingWrappedMessage() throws IOException {
doUUIDMarshallTest(useWrappedMessage());
}

@Test
public void testBigDecimalUsingByteArray() throws IOException {
doBigDecimalMarshallTest(useByteArray());
}

@Test
public void testBigDecimalUsingInputStream() throws IOException {
doBigDecimalMarshallTest(useInputStream());
}

@Test
public void testBigDecimalUsingWrappedMessage() throws IOException {
doBigDecimalMarshallTest(useWrappedMessage());
}

@Test
public void testBigIntegerUsingByteArray() throws IOException {
doBigIntegerMarshallTest(useByteArray());
}

@Test
public void testBigIntegerUsingInputStream() throws IOException {
doBigIntegerMarshallTest(useInputStream());
}

@Test
public void testBigIntegerUsingWrappedMessage() throws IOException {
doBigIntegerMarshallTest(useWrappedMessage());
}

private void doUUIDMarshallTest(EncoderMethod<UUID> encoderMethod) throws IOException {
UUID uuid = UUID.randomUUID();
UUID other = encoderMethod.encodeAndDecode(uuid, newContext());
assertEquals(uuid, other);
}

private void doBigDecimalMarshallTest(EncoderMethod<BigDecimal> encoderMethod) throws IOException {
BigDecimal expected = BigDecimal.valueOf(ThreadLocalRandom.current().nextDouble(-1000, 1000));
BigDecimal copy = encoderMethod.encodeAndDecode(expected, newContext());
assertEquals(expected, copy);
}

private void doBigIntegerMarshallTest(EncoderMethod<BigInteger> encoderMethod) throws IOException {
BigInteger expected = BigInteger.valueOf(ThreadLocalRandom.current().nextInt(-1000, 1000));
BigInteger copy = encoderMethod.encodeAndDecode(expected, newContext());
assertEquals(expected, copy);
}

private static Collection<String> stringCollection(Supplier<Collection<String>> supplier) {
Collection<String> collection = supplier.get();
collection.add("a");
collection.add("b");
collection.add("c");
return collection;
}

private static Collection<Book> bookCollection(Supplier<Collection<Book>> supplier) {
Collection<Book> collection = supplier.get();
collection.add(new Book("Book1", "Description1", 2020));
collection.add(new Book("Book2", "Description2", 2021));
collection.add(new Book("Book3", "Description3", 2022));
return collection;
}

private static String[] stringArray() {
return new String[] {"a", "b", "c"};
}

private static Object[] bookArray() {
// cannot use new Book[] because there is no marshaller for it.
return new Object[] {
new Book("Book1", "Description1", 2020),
new Book("Book2", "Description2", 2021),
new Book("Book3", "Description3", 2022)
};
}

private static <E> void doCollectionMarshallTest(Collection<E> collection, EncoderMethod<Collection<E>> encoderMethod) throws IOException {
Collection<E> copy = encoderMethod.encodeAndDecode(collection, newContext());
assertEquals(collection.getClass(), copy.getClass());
assertEquals(collection, copy);
}

private static <T> void doArrayMarshallTest(T[] array, EncoderMethod<T[]> encoderMethod) throws IOException {
T[] copy = encoderMethod.encodeAndDecode(array, newContext());
assertArrayEquals(array, copy);
}

public static SerializationContext newContext() {
SerializationContext context = ProtobufUtil.newSerializationContext();
register(new CommonTypesSchema(), context);
register(new CommonContainerTypesSchema(), context);
register(new BookSchemaImpl(), context);
return context;
}

private static void register(GeneratedSchema schema, SerializationContext context) {
schema.registerMarshallers(context);
schema.registerSchema(context);
}

public static <T> EncoderMethod<T> useByteArray() {
return (object, ctx) -> {
ByteArrayOutputStream baos = new ByteArrayOutputStream(512);
ProtobufUtil.writeTo(ctx, baos, object);
//noinspection unchecked
return ProtobufUtil.fromByteArray(ctx, baos.toByteArray(), (Class<T>) object.getClass());
};
}

public static <T> EncoderMethod<T> useInputStream() {
return (object, ctx) -> {
ByteArrayOutputStream baos = new ByteArrayOutputStream(512);
ProtobufUtil.writeTo(ctx, baos, object);
InputStream is = new ByteArrayInputStream(baos.toByteArray());
//noinspection unchecked
return ProtobufUtil.readFrom(ctx, is, (Class<T>) object.getClass());
};
}

public static <T> EncoderMethod<T> useWrappedMessage() {
return (object, ctx) -> {
byte[] data = ProtobufUtil.toWrappedByteArray(ctx, object, 512);
return ProtobufUtil.fromWrappedByteArray(ctx, data);
};
}

@FunctionalInterface
public interface EncoderMethod<T> {
T encodeAndDecode(T object, ImmutableSerializationContext ctx) throws IOException;
}

}
Loading
Loading