diff options
author | Feng Xiao <xfxyjwf@gmail.com> | 2017-03-09 14:42:45 -0800 |
---|---|---|
committer | Feng Xiao <xfxyjwf@gmail.com> | 2017-03-09 14:42:45 -0800 |
commit | 075475f8341393e78d8cd664f8500f6397ee9941 (patch) | |
tree | 8b2182a9d84d43cdacec0613efc1feb5f92cfba9 /java/util/src/test | |
parent | 8e465dcf467beb92549e6d987e5dc3683b718f58 (diff) | |
download | protobuf-075475f8341393e78d8cd664f8500f6397ee9941.tar.gz protobuf-075475f8341393e78d8cd664f8500f6397ee9941.tar.bz2 protobuf-075475f8341393e78d8cd664f8500f6397ee9941.zip |
Don't expose gson exceptions in JsonFormat.
Diffstat (limited to 'java/util/src/test')
-rw-r--r-- | java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java b/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java index dafd9bb5..ef1e4160 100644 --- a/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java +++ b/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java @@ -62,6 +62,10 @@ import com.google.protobuf.util.JsonTestProto.TestStruct; import com.google.protobuf.util.JsonTestProto.TestTimestamp; import com.google.protobuf.util.JsonTestProto.TestWrappers; import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; +import java.io.StringReader; import java.math.BigDecimal; import java.math.BigInteger; import java.util.HashMap; @@ -1411,4 +1415,34 @@ public class JsonFormatTest extends TestCase { // Expected. } } + + // Test that we are not leaking out JSON exceptions. + public void testJsonException() throws Exception { + InputStream throwingInputStream = new InputStream() { + public int read() throws IOException { + throw new IOException("12345"); + } + }; + InputStreamReader throwingReader = new InputStreamReader(throwingInputStream); + // When the underlying reader throws IOException, JsonFormat should forward + // through this IOException. + try { + TestAllTypes.Builder builder = TestAllTypes.newBuilder(); + JsonFormat.parser().merge(throwingReader, builder); + fail("Exception is expected."); + } catch (IOException e) { + assertEquals("12345", e.getMessage()); + } + + Reader invalidJsonReader = new StringReader("{ xxx - yyy }"); + // When the JSON parser throws parser exceptions, JsonFormat should turn + // that into InvalidProtocolBufferException. + try { + TestAllTypes.Builder builder = TestAllTypes.newBuilder(); + JsonFormat.parser().merge(invalidJsonReader, builder); + fail("Exception is expected."); + } catch (InvalidProtocolBufferException e) { + // Expected. + } + } } |