Merge pull request #4068 from wsw2016/fix_4032

resolved issue 4032 and added a unit test
pull/4087/head
Adam Cozzette 7 years ago committed by GitHub
commit 1e418e4e54
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      src/google/protobuf/util/json_util.cc
  2. 24
      src/google/protobuf/util/json_util_test.cc

@ -50,8 +50,10 @@ namespace util {
namespace internal {
ZeroCopyStreamByteSink::~ZeroCopyStreamByteSink() {
if (buffer_size_ > 0) {
stream_->BackUp(buffer_size_);
}
}
void ZeroCopyStreamByteSink::Append(const char* bytes, size_t len) {
while (true) {

@ -40,6 +40,7 @@
#include <google/protobuf/util/json_format_proto3.pb.h>
#include <google/protobuf/util/type_resolver.h>
#include <google/protobuf/util/type_resolver_util.h>
#include <google/protobuf/io/zero_copy_stream_impl.h>
#include <gtest/gtest.h>
namespace google {
@ -457,6 +458,29 @@ TEST(ZeroCopyStreamByteSinkTest, TestAllInputOutputPatterns) {
}
}
TEST_F(JsonUtilTest, TestWrongJsonInput) {
using namespace google::protobuf;
const char json[] = "{\"unknown_field\":\"some_value\"}";
io::ArrayInputStream input_stream(json, strlen(json));
char proto_buffer[10000];
io::ArrayOutputStream output_stream(proto_buffer, sizeof(proto_buffer));
std::string message_type = "type.googleapis.com/proto3.TestMessage";
TypeResolver* resolver = NewTypeResolverForDescriptorPool(
"type.googleapis.com",
DescriptorPool::generated_pool());
util::Status result_status = util::JsonToBinaryStream(resolver,
message_type,
&input_stream,
&output_stream);
delete resolver;
EXPECT_FALSE(result_status.ok());
EXPECT_EQ(result_status.error_code(),
google::protobuf::util::error::INVALID_ARGUMENT);
}
} // namespace
} // namespace util
} // namespace protobuf

Loading…
Cancel
Save