ParseExceptionsTest.java
Go to the documentation of this file.
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 package com.google.protobuf;
32 
33 import static org.junit.Assert.assertEquals;
34 import static org.junit.Assert.assertFalse;
35 import static org.junit.Assert.assertTrue;
36 import static org.junit.Assert.fail;
37 
39 import java.io.ByteArrayInputStream;
40 import java.io.ByteArrayOutputStream;
41 import java.io.FilterInputStream;
42 import java.io.IOException;
43 import java.io.InputStream;
44 import org.junit.Test;
45 import org.junit.runner.RunWith;
46 import org.junit.runners.JUnit4;
47 
57 @RunWith(JUnit4.class)
58 public class ParseExceptionsTest {
59 
60  private interface ParseTester {
61  DescriptorProto parse(InputStream in) throws IOException;
62  }
63 
64  private byte[] serializedProto;
65 
66  private void setup() {
67  serializedProto = DescriptorProto.getDescriptor().toProto().toByteArray();
68  }
69 
70  private void setupDelimited() {
71  ByteArrayOutputStream bos = new ByteArrayOutputStream();
72  try {
73  DescriptorProto.getDescriptor().toProto().writeDelimitedTo(bos);
74  } catch (IOException e) {
75  fail("Exception not expected: " + e);
76  }
77  serializedProto = bos.toByteArray();
78  }
79 
80  @Test
82  setup();
83  verifyExceptions(
84  new ParseTester() {
85  @Override
86  public DescriptorProto parse(InputStream in) throws IOException {
87  return DescriptorProto.parseFrom(in);
88  }
89  });
90  }
91 
92  @Test
94  setup();
95  verifyExceptions(
96  new ParseTester() {
97  @Override
98  public DescriptorProto parse(InputStream in) throws IOException {
99  return DescriptorProto.parseFrom(in, ExtensionRegistry.newInstance());
100  }
101  });
102  }
103 
104  @Test
106  setup();
107  verifyExceptions(
108  new ParseTester() {
109  @Override
110  public DescriptorProto parse(InputStream in) throws IOException {
111  return DescriptorProto.parseFrom(CodedInputStream.newInstance(in));
112  }
113  });
114  }
115 
116  @Test
118  setup();
119  verifyExceptions(
120  new ParseTester() {
121  @Override
122  public DescriptorProto parse(InputStream in) throws IOException {
123  return DescriptorProto.parseFrom(
125  }
126  });
127  }
128 
129  @Test
131  setupDelimited();
132  verifyExceptions(
133  new ParseTester() {
134  @Override
135  public DescriptorProto parse(InputStream in) throws IOException {
136  return DescriptorProto.parseDelimitedFrom(in);
137  }
138  });
139  }
140 
141  @Test
143  setupDelimited();
144  verifyExceptions(
145  new ParseTester() {
146  @Override
147  public DescriptorProto parse(InputStream in) throws IOException {
148  return DescriptorProto.parseDelimitedFrom(in, ExtensionRegistry.newInstance());
149  }
150  });
151  }
152 
153  @Test
155  setup();
156  verifyExceptions(
157  new ParseTester() {
158  @Override
159  public DescriptorProto parse(InputStream in) throws IOException {
160  return DescriptorProto.newBuilder().mergeFrom(in).build();
161  }
162  });
163  }
164 
165  @Test
167  setup();
168  verifyExceptions(
169  new ParseTester() {
170  @Override
171  public DescriptorProto parse(InputStream in) throws IOException {
172  return DescriptorProto.newBuilder()
173  .mergeFrom(in, ExtensionRegistry.newInstance())
174  .build();
175  }
176  });
177  }
178 
179  @Test
181  setup();
182  verifyExceptions(
183  new ParseTester() {
184  @Override
185  public DescriptorProto parse(InputStream in) throws IOException {
186  return DescriptorProto.newBuilder().mergeFrom(CodedInputStream.newInstance(in)).build();
187  }
188  });
189  }
190 
191  @Test
193  setup();
194  verifyExceptions(
195  new ParseTester() {
196  @Override
197  public DescriptorProto parse(InputStream in) throws IOException {
198  return DescriptorProto.newBuilder()
200  .build();
201  }
202  });
203  }
204 
205  @Test
207  setupDelimited();
208  verifyExceptions(
209  new ParseTester() {
210  @Override
211  public DescriptorProto parse(InputStream in) throws IOException {
212  DescriptorProto.Builder builder = DescriptorProto.newBuilder();
213  builder.mergeDelimitedFrom(in);
214  return builder.build();
215  }
216  });
217  }
218 
219  @Test
221  byte[] body = new byte[80];
223  cos.writeRawVarint32(90); // Greater than bytes in stream
224  cos.writeTag(DescriptorProto.ENUM_TYPE_FIELD_NUMBER, WireFormat.WIRETYPE_LENGTH_DELIMITED);
225  cos.writeRawVarint32(98); // Nested message with size larger than parent
227  cos.writeRawVarint32(100); // Unknown field with size larger than parent
228  ByteArrayInputStream bais = new ByteArrayInputStream(body);
229  try {
230  DescriptorProto.parseDelimitedFrom(bais);
231  fail();
232  } catch (InvalidProtocolBufferException expected) {
233  }
234  }
235 
236  @Test
238  setupDelimited();
239  verifyExceptions(
240  new ParseTester() {
241  @Override
242  public DescriptorProto parse(InputStream in) throws IOException {
243  DescriptorProto.Builder builder = DescriptorProto.newBuilder();
244  builder.mergeDelimitedFrom(in, ExtensionRegistry.newInstance());
245  return builder.build();
246  }
247  });
248  }
249 
250  private void verifyExceptions(ParseTester parseTester) {
251  // No exception
252  try {
253  assertEquals(
254  DescriptorProto.getDescriptor().toProto(),
255  parseTester.parse(new ByteArrayInputStream(serializedProto)));
256  } catch (IOException e) {
257  fail("No exception expected: " + e);
258  }
259 
260  // IOException
261  try {
262  // using a "broken" stream that will throw part-way through reading the message
263  parseTester.parse(broken(new ByteArrayInputStream(serializedProto)));
264  fail("IOException expected but not thrown");
265  } catch (IOException e) {
266  assertFalse(e instanceof InvalidProtocolBufferException);
267  }
268 
269  // InvalidProtocolBufferException
270  try {
271  // make the serialized proto invalid
272  for (int i = 0; i < 50; i++) {
273  serializedProto[i] = -1;
274  }
275  parseTester.parse(new ByteArrayInputStream(serializedProto));
276  fail("InvalidProtocolBufferException expected but not thrown");
277  } catch (IOException e) {
278  assertTrue(e instanceof InvalidProtocolBufferException);
279  }
280  }
281 
282  private InputStream broken(InputStream i) {
283  return new FilterInputStream(i) {
284  int count = 0;
285 
286  @Override
287  public int read() throws IOException {
288  if (count++ >= 50) {
289  throw new IOException("I'm broken!");
290  }
291  return super.read();
292  }
293 
294  @Override
295  public int read(byte[] b, int off, int len) throws IOException {
296  if ((count += len) >= 50) {
297  throw new IOException("I'm broken!");
298  }
299  return super.read(b, off, len);
300  }
301  };
302  }
303 }
setup
com.google.protobuf.CodedInputStream.newInstance
static CodedInputStream newInstance(final InputStream input)
Definition: CodedInputStream.java:79
parse
size_t parse(void *closure, const void *hd, const char *buf, size_t size, const upb_bufhandle *handle)
Definition: php/ext/google/protobuf/upb.c:11625
com.google.protobuf.ParseExceptionsTest.message_parseFrom_InputStream
void message_parseFrom_InputStream()
Definition: ParseExceptionsTest.java:81
com.google.protobuf.ParseExceptionsTest.setup
void setup()
Definition: ParseExceptionsTest.java:66
com.google.protobuf.ParseExceptionsTest.message_parseFrom_CodedInputStreamAndExtensionRegistry
void message_parseFrom_CodedInputStreamAndExtensionRegistry()
Definition: ParseExceptionsTest.java:117
com.google.protobuf.ParseExceptionsTest.ParseTester
Definition: ParseExceptionsTest.java:60
com.google.protobuf
Definition: ProtoCaliperBenchmark.java:2
com.google.protobuf.ParseExceptionsTest.serializedProto
byte[] serializedProto
Definition: ParseExceptionsTest.java:64
com.google.protobuf.ParseExceptionsTest
Definition: ParseExceptionsTest.java:58
com.google.protobuf::DescriptorProtos::DescriptorProto
com.google.protobuf.WireFormat
Definition: WireFormat.java:45
b
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:3228
com.google.protobuf.ParseExceptionsTest.messageBuilder_mergeFrom_InputStream
void messageBuilder_mergeFrom_InputStream()
Definition: ParseExceptionsTest.java:154
com.google.protobuf.ParseExceptionsTest.messageBuilder_mergeDelimitedFrom_InputStream
void messageBuilder_mergeDelimitedFrom_InputStream()
Definition: ParseExceptionsTest.java:206
com.google.protobuf.CodedInputStream
Definition: CodedInputStream.java:61
com.google.protobuf.ParseExceptionsTest.verifyExceptions
void verifyExceptions(ParseTester parseTester)
Definition: ParseExceptionsTest.java:250
com.google.protobuf.ParseExceptionsTest.message_parseDelimitedFrom_InputStreamAndExtensionRegistry
void message_parseDelimitedFrom_InputStreamAndExtensionRegistry()
Definition: ParseExceptionsTest.java:142
com.google.protobuf.ParseExceptionsTest.messageBuilder_mergeFrom_CodedInputStreamAndExtensionRegistry
void messageBuilder_mergeFrom_CodedInputStreamAndExtensionRegistry()
Definition: ParseExceptionsTest.java:192
com.google.protobuf.CodedOutputStream.writeTag
abstract void writeTag(int fieldNumber, int wireType)
com.google.protobuf.ParseExceptionsTest.setupDelimited
void setupDelimited()
Definition: ParseExceptionsTest.java:70
i
int i
Definition: gmock-matchers_test.cc:764
java
com.google.protobuf.WireFormat.WIRETYPE_LENGTH_DELIMITED
static final int WIRETYPE_LENGTH_DELIMITED
Definition: WireFormat.java:57
len
int len
Definition: php/ext/google/protobuf/map.c:206
com.google.protobuf.ParseExceptionsTest.messageBuilder_mergeFrom_InputStreamAndExtensionRegistry
void messageBuilder_mergeFrom_InputStreamAndExtensionRegistry()
Definition: ParseExceptionsTest.java:166
com.google.protobuf.ParseExceptionsTest.message_parseFrom_CodedInputStream
void message_parseFrom_CodedInputStream()
Definition: ParseExceptionsTest.java:105
com.google.protobuf.ExtensionRegistry.newInstance
static ExtensionRegistry newInstance()
Definition: ExtensionRegistry.java:93
com.google.protobuf.ParseExceptionsTest.message_parseFrom_InputStreamAndExtensionRegistry
void message_parseFrom_InputStreamAndExtensionRegistry()
Definition: ParseExceptionsTest.java:93
com.google.protobuf.CodedOutputStream.writeRawVarint32
final void writeRawVarint32(int value)
Definition: CodedOutputStream.java:1092
com.google
com
com.google.protobuf.CodedOutputStream.newInstance
static CodedOutputStream newInstance(final OutputStream output)
Definition: CodedOutputStream.java:92
com.google.protobuf.ExtensionRegistry
Definition: ExtensionRegistry.java:91
com.google.protobuf.ParseExceptionsTest.messageBuilder_mergeDelimitedFrom_InputStreamAndExtensionRegistry
void messageBuilder_mergeDelimitedFrom_InputStreamAndExtensionRegistry()
Definition: ParseExceptionsTest.java:237
com.google.protobuf.CodedOutputStream
Definition: CodedOutputStream.java:59
com.google.protobuf.InvalidProtocolBufferException
Definition: InvalidProtocolBufferException.java:41
com.google.protobuf.ParseExceptionsTest.broken
InputStream broken(InputStream i)
Definition: ParseExceptionsTest.java:282
com.google.protobuf.ParseExceptionsTest.messageBuilder_mergeFrom_CodedInputStream
void messageBuilder_mergeFrom_CodedInputStream()
Definition: ParseExceptionsTest.java:180
com.google.protobuf.ParseExceptionsTest.messageBuilder_mergeDelimitedFrom_InputStream_malformed
void messageBuilder_mergeDelimitedFrom_InputStream_malformed()
Definition: ParseExceptionsTest.java:220
com.google.protobuf::DescriptorProtos
com.google.protobuf.ParseExceptionsTest.ParseTester.parse
DescriptorProto parse(InputStream in)
count
GLint GLsizei count
Definition: glcorearb.h:2830
com.google.protobuf.ParseExceptionsTest.message_parseDelimitedFrom_InputStream
void message_parseDelimitedFrom_InputStream()
Definition: ParseExceptionsTest.java:130


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:57