|
4 | 4 | import static org.junit.jupiter.api.Assertions.assertEquals; |
5 | 5 | import static org.junit.jupiter.api.Assertions.assertThrows; |
6 | 6 |
|
| 7 | +import com.hedera.pbj.runtime.Codec; |
7 | 8 | import com.hedera.pbj.runtime.ParseException; |
8 | 9 | import com.hedera.pbj.runtime.io.buffer.BufferedData; |
9 | 10 | import com.hedera.pbj.runtime.io.buffer.Bytes; |
| 11 | +import com.hedera.pbj.test.proto.pbj.Everything; |
| 12 | +import com.hedera.pbj.test.proto.pbj.InnerEverything; |
10 | 13 | import com.hedera.pbj.test.proto.pbj.MessageWithBytes; |
11 | 14 | import com.hedera.pbj.test.proto.pbj.MessageWithString; |
12 | 15 | import org.junit.jupiter.api.Test; |
@@ -73,4 +76,45 @@ void testCustomMaxSize() throws Exception { |
73 | 76 | assertThrows( |
74 | 77 | ParseException.class, () -> MessageWithString.PROTOBUF.parse(data, false, false, Integer.MAX_VALUE, 6)); |
75 | 78 | } |
| 79 | + |
| 80 | + @Test |
| 81 | + void testNestedMaxSize() throws Exception { |
| 82 | + // This message, an inner nested message within it, as well as a field in that inner message all exceed the |
| 83 | + // DEFAULT_MAX_SIZE: |
| 84 | + final Everything everything = Everything.newBuilder() |
| 85 | + .innerEverything( |
| 86 | + InnerEverything.newBuilder().bytesField(Bytes.wrap("1".repeat(Codec.DEFAULT_MAX_SIZE + 1)))) |
| 87 | + .build(); |
| 88 | + final Bytes bytes = Everything.PROTOBUF.toBytes(everything); |
| 89 | + |
| 90 | + // Try negative cases first: |
| 91 | + assertThrows(ParseException.class, () -> Everything.PROTOBUF.parse(bytes.toReadableSequentialData())); |
| 92 | + assertThrows( |
| 93 | + ParseException.class, |
| 94 | + () -> Everything.PROTOBUF.parse( |
| 95 | + bytes.toReadableSequentialData(), false, false, Codec.DEFAULT_MAX_DEPTH, 256)); |
| 96 | + assertThrows( |
| 97 | + ParseException.class, |
| 98 | + () -> Everything.PROTOBUF.parse( |
| 99 | + bytes.toReadableSequentialData(), |
| 100 | + false, |
| 101 | + false, |
| 102 | + Codec.DEFAULT_MAX_DEPTH, |
| 103 | + Codec.DEFAULT_MAX_SIZE)); |
| 104 | + // +1 still shouldn't work because the outer and the inner objects are still larger: |
| 105 | + assertThrows( |
| 106 | + ParseException.class, |
| 107 | + () -> Everything.PROTOBUF.parse( |
| 108 | + bytes.toReadableSequentialData(), |
| 109 | + false, |
| 110 | + false, |
| 111 | + Codec.DEFAULT_MAX_DEPTH, |
| 112 | + Codec.DEFAULT_MAX_SIZE + 1)); |
| 113 | + |
| 114 | + // Now try supplying a large enough maxSize to parse it: |
| 115 | + final Everything parsedEverything = Everything.PROTOBUF.parse( |
| 116 | + bytes.toReadableSequentialData(), false, false, Codec.DEFAULT_MAX_DEPTH, Codec.DEFAULT_MAX_SIZE * 2); |
| 117 | + |
| 118 | + assertEquals(everything, parsedEverything); |
| 119 | + } |
76 | 120 | } |
0 commit comments