77
88package com .google .protobuf ;
99
10- import static com . google . common . truth . Truth . assertWithMessage ;
10+ import static org . junit . Assert . assertThrows ;
1111
1212import com .google .protobuf .ArrayDecoders .Registers ;
1313import java .io .IOException ;
@@ -33,134 +33,184 @@ public void setUp() {
3333
3434 @ Test
3535 public void testException_decodeString () {
36- try {
37- ArrayDecoders .decodeString (NEGATIVE_SIZE_0 .toByteArray (), 0 , registers );
38- assertWithMessage ("should throw exception" ).fail ();
39- } catch (InvalidProtocolBufferException expected ) {
40- }
36+ assertThrows (
37+ InvalidProtocolBufferException .class ,
38+ () -> ArrayDecoders .decodeString (NEGATIVE_SIZE_0 .toByteArray (), 0 , registers ));
4139 }
4240
4341 @ Test
4442 public void testException_decodeStringRequireUtf8 () {
45- try {
46- ArrayDecoders .decodeStringRequireUtf8 (NEGATIVE_SIZE_0 .toByteArray (), 0 , registers );
47- assertWithMessage ("should throw an exception" ).fail ();
48- } catch (InvalidProtocolBufferException expected ) {
49- }
43+ assertThrows (
44+ InvalidProtocolBufferException .class ,
45+ () -> ArrayDecoders .decodeStringRequireUtf8 (NEGATIVE_SIZE_0 .toByteArray (), 0 , registers ));
5046 }
5147
5248 @ Test
5349 public void testException_decodeBytes () {
54- try {
55- ArrayDecoders .decodeBytes (NEGATIVE_SIZE_0 .toByteArray (), 0 , registers );
56- assertWithMessage ("should throw an exception" ).fail ();
57- } catch (InvalidProtocolBufferException expected ) {
58- }
50+ assertThrows (
51+ InvalidProtocolBufferException .class ,
52+ () -> ArrayDecoders .decodeBytes (NEGATIVE_SIZE_0 .toByteArray (), 0 , registers ));
5953 }
6054
6155 @ Test
6256 public void testException_decodeStringList_first () {
63- try {
64- ArrayDecoders .decodeStringList (
65- TAG ,
66- NEGATIVE_SIZE_0 .toByteArray (),
67- 0 ,
68- NEGATIVE_SIZE_0 .size (),
69- new ProtobufArrayList <Object >(),
70- registers );
71- assertWithMessage ("should throw an exception" ).fail ();
72- } catch (InvalidProtocolBufferException expected ) {
73- }
57+ assertThrows (
58+ InvalidProtocolBufferException .class ,
59+ () ->
60+ ArrayDecoders .decodeStringList (
61+ TAG ,
62+ NEGATIVE_SIZE_0 .toByteArray (),
63+ 0 ,
64+ NEGATIVE_SIZE_0 .size (),
65+ new ProtobufArrayList <Object >(),
66+ registers ));
7467 }
7568
7669 @ Test
7770 public void testException_decodeStringList_second () {
78- try {
79- ArrayDecoders .decodeStringList (
80- TAG ,
81- NEGATIVE_SIZE_1 .toByteArray (),
82- 0 ,
83- NEGATIVE_SIZE_1 .size (),
84- new ProtobufArrayList <Object >(),
85- registers );
86- assertWithMessage ("should throw an exception" ).fail ();
87- } catch (InvalidProtocolBufferException expected ) {
88- }
71+ assertThrows (
72+ InvalidProtocolBufferException .class ,
73+ () ->
74+ ArrayDecoders .decodeStringList (
75+ TAG ,
76+ NEGATIVE_SIZE_1 .toByteArray (),
77+ 0 ,
78+ NEGATIVE_SIZE_1 .size (),
79+ new ProtobufArrayList <Object >(),
80+ registers ));
8981 }
9082
9183 @ Test
9284 public void testException_decodeStringListRequireUtf8_first () {
93- try {
94- ArrayDecoders .decodeStringListRequireUtf8 (
95- TAG ,
96- NEGATIVE_SIZE_0 .toByteArray (),
97- 0 ,
98- NEGATIVE_SIZE_0 .size (),
99- new ProtobufArrayList <Object >(),
100- registers );
101- assertWithMessage ("should throw an exception" ).fail ();
102- } catch (InvalidProtocolBufferException expected ) {
103- }
85+ assertThrows (
86+ InvalidProtocolBufferException .class ,
87+ () ->
88+ ArrayDecoders .decodeStringListRequireUtf8 (
89+ TAG ,
90+ NEGATIVE_SIZE_0 .toByteArray (),
91+ 0 ,
92+ NEGATIVE_SIZE_0 .size (),
93+ new ProtobufArrayList <Object >(),
94+ registers ));
10495 }
10596
10697 @ Test
10798 public void testException_decodeStringListRequireUtf8_second () {
108- try {
109- ArrayDecoders .decodeStringListRequireUtf8 (
110- TAG ,
111- NEGATIVE_SIZE_1 .toByteArray (),
112- 0 ,
113- NEGATIVE_SIZE_1 .size (),
114- new ProtobufArrayList <Object >(),
115- registers );
116- assertWithMessage ("should throw an exception" ).fail ();
117- } catch (InvalidProtocolBufferException expected ) {
118- }
99+ assertThrows (
100+ InvalidProtocolBufferException .class ,
101+ () ->
102+ ArrayDecoders .decodeStringListRequireUtf8 (
103+ TAG ,
104+ NEGATIVE_SIZE_1 .toByteArray (),
105+ 0 ,
106+ NEGATIVE_SIZE_1 .size (),
107+ new ProtobufArrayList <Object >(),
108+ registers ));
119109 }
120110
121111 @ Test
122112 public void testException_decodeBytesList_first () {
123- try {
124- ArrayDecoders .decodeBytesList (
125- TAG ,
126- NEGATIVE_SIZE_0 .toByteArray (),
127- 0 ,
128- NEGATIVE_SIZE_0 .size (),
129- new ProtobufArrayList <Object >(),
130- registers );
131- assertWithMessage ("should throw an exception" ).fail ();
132- } catch (InvalidProtocolBufferException expected ) {
133- }
113+ assertThrows (
114+ InvalidProtocolBufferException .class ,
115+ () ->
116+ ArrayDecoders .decodeBytesList (
117+ TAG ,
118+ NEGATIVE_SIZE_0 .toByteArray (),
119+ 0 ,
120+ NEGATIVE_SIZE_0 .size (),
121+ new ProtobufArrayList <Object >(),
122+ registers ));
134123 }
135124
136125 @ Test
137126 public void testException_decodeBytesList_second () {
138- try {
139- ArrayDecoders .decodeBytesList (
140- TAG ,
141- NEGATIVE_SIZE_1 .toByteArray (),
142- 0 ,
143- NEGATIVE_SIZE_1 .size (),
144- new ProtobufArrayList <Object >(),
145- registers );
146- assertWithMessage ("should throw an exception" ).fail ();
147- } catch (InvalidProtocolBufferException expected ) {
148- }
127+ assertThrows (
128+ InvalidProtocolBufferException .class ,
129+ () ->
130+ ArrayDecoders .decodeBytesList (
131+ TAG ,
132+ NEGATIVE_SIZE_1 .toByteArray (),
133+ 0 ,
134+ NEGATIVE_SIZE_1 .size (),
135+ new ProtobufArrayList <Object >(),
136+ registers ));
149137 }
150138
151139 @ Test
152140 public void testException_decodeUnknownField () {
153- try {
154- ArrayDecoders .decodeUnknownField (
155- TAG ,
156- NEGATIVE_SIZE_0 .toByteArray (),
157- 0 ,
158- NEGATIVE_SIZE_0 .size (),
159- UnknownFieldSetLite .newInstance (),
160- registers );
161- assertWithMessage ("should throw an exception" ).fail ();
162- } catch (InvalidProtocolBufferException expected ) {
163- }
141+ assertThrows (
142+ InvalidProtocolBufferException .class ,
143+ () ->
144+ ArrayDecoders .decodeUnknownField (
145+ TAG ,
146+ NEGATIVE_SIZE_0 .toByteArray (),
147+ 0 ,
148+ NEGATIVE_SIZE_0 .size (),
149+ UnknownFieldSetLite .newInstance (),
150+ registers ));
151+ }
152+
153+ @ Test
154+ public void testDecodePackedFixed32List_negativeSize () {
155+ assertThrows (
156+ InvalidProtocolBufferException .class ,
157+ () ->
158+ ArrayDecoders .decodePackedFixed32List (
159+ packedSizeBytesNoTag (-1 ), 0 , new IntArrayList (), registers ));
160+ }
161+
162+ @ Test
163+ public void testDecodePackedFixed64List_negativeSize () {
164+ assertThrows (
165+ InvalidProtocolBufferException .class ,
166+ () ->
167+ ArrayDecoders .decodePackedFixed64List (
168+ packedSizeBytesNoTag (-1 ), 0 , new LongArrayList (), registers ));
169+ }
170+
171+ @ Test
172+ public void testDecodePackedFloatList_negativeSize () {
173+ assertThrows (
174+ InvalidProtocolBufferException .class ,
175+ () ->
176+ ArrayDecoders .decodePackedFloatList (
177+ packedSizeBytesNoTag (-1 ), 0 , new FloatArrayList (), registers ));
178+ }
179+
180+ @ Test
181+ public void testDecodePackedDoubleList_negativeSize () {
182+ assertThrows (
183+ InvalidProtocolBufferException .class ,
184+ () ->
185+ ArrayDecoders .decodePackedDoubleList (
186+ packedSizeBytesNoTag (-1 ), 0 , new DoubleArrayList (), registers ));
187+ }
188+
189+ @ Test
190+ public void testDecodePackedBoolList_negativeSize () {
191+ assertThrows (
192+ InvalidProtocolBufferException .class ,
193+ () ->
194+ ArrayDecoders .decodePackedBoolList (
195+ packedSizeBytesNoTag (-1 ), 0 , new BooleanArrayList (), registers ));
196+ }
197+
198+ @ Test
199+ public void testDecodePackedSInt32List_negativeSize () {
200+ assertThrows (
201+ InvalidProtocolBufferException .class ,
202+ () ->
203+ ArrayDecoders .decodePackedSInt32List (
204+ packedSizeBytesNoTag (-1 ), 0 , new IntArrayList (), registers ));
205+ }
206+
207+ @ Test
208+ public void testDecodePackedSInt64List_negativeSize () {
209+ assertThrows (
210+ InvalidProtocolBufferException .class ,
211+ () ->
212+ ArrayDecoders .decodePackedSInt64List (
213+ packedSizeBytesNoTag (-1 ), 0 , new LongArrayList (), registers ));
164214 }
165215
166216 @ Test
@@ -169,18 +219,15 @@ public void testException_decodeHugeField() {
169219 new byte [] {
170220 (byte ) 0x80 , (byte ) 0xFF , (byte ) 0xFF , (byte ) 0xEF , 0x73 , 0x74 , 0x69 , 0x6E , 0x67
171221 };
172- try {
173- ArrayDecoders .decodeUnknownField (
174- TAG , badBytes , 0 , badBytes .length , UnknownFieldSetLite .newInstance (), registers );
175- assertWithMessage ("should throw an exception" ).fail ();
176- } catch (InvalidProtocolBufferException expected ) {
177- }
222+ assertThrows (
223+ InvalidProtocolBufferException .class ,
224+ () ->
225+ ArrayDecoders .decodeUnknownField (
226+ TAG , badBytes , 0 , badBytes .length , UnknownFieldSetLite .newInstance (), registers ));
178227
179- try {
180- ArrayDecoders .decodeBytes (badBytes , 0 , registers );
181- assertWithMessage ("should throw an exception" ).fail ();
182- } catch (InvalidProtocolBufferException expected ) {
183- }
228+ assertThrows (
229+ InvalidProtocolBufferException .class ,
230+ () -> ArrayDecoders .decodeBytes (badBytes , 0 , registers ));
184231
185232 byte [] badBytesList =
186233 new byte [] {
@@ -197,11 +244,25 @@ public void testException_decodeHugeField() {
197244 0x6E ,
198245 0x67
199246 };
247+ assertThrows (
248+ InvalidProtocolBufferException .class ,
249+ () ->
250+ ArrayDecoders .decodeBytesList (
251+ TAG , badBytesList , 0 , badBytes .length , new ProtobufArrayList <>(), registers ));
252+ }
253+
254+ // Encodes a single varint without a tag prefix.
255+ // For use when testing decoding of packed primitive lists.
256+ // e.g. size = -1 is not a proper byte size for a list.
257+ private static byte [] packedSizeBytesNoTag (int size ) {
200258 try {
201- ArrayDecoders .decodeBytesList (
202- TAG , badBytesList , 0 , badBytes .length , new ProtobufArrayList <>(), registers );
203- assertWithMessage ("should throw an exception" ).fail ();
204- } catch (InvalidProtocolBufferException expected ) {
259+ ByteString .Output byteStringOutput = ByteString .newOutput ();
260+ CodedOutputStream codedOutput = CodedOutputStream .newInstance (byteStringOutput );
261+ codedOutput .writeInt32NoTag (size );
262+ codedOutput .flush ();
263+ return byteStringOutput .toByteString ().toByteArray ();
264+ } catch (IOException e ) {
265+ throw new RuntimeException (e );
205266 }
206267 }
207268
0 commit comments