diff options
author | Joshua Haberman <jhaberman@gmail.com> | 2015-07-13 09:50:01 -0700 |
---|---|---|
committer | Joshua Haberman <jhaberman@gmail.com> | 2015-07-13 09:50:01 -0700 |
commit | a3ca1fa4bf4d3e4b0958f96034c9be9d82c7a1d2 (patch) | |
tree | da6aab20d86574dee821130e36741d02ccfd83c2 /conformance/conformance_cpp.cc | |
parent | fe50044041e8b8bc6b8d8183e8d043ed6dba403e (diff) | |
parent | b0500b37b231b8f93a5b1b95e23d13830e11eb07 (diff) | |
download | protobuf-a3ca1fa4bf4d3e4b0958f96034c9be9d82c7a1d2.tar.gz protobuf-a3ca1fa4bf4d3e4b0958f96034c9be9d82c7a1d2.tar.bz2 protobuf-a3ca1fa4bf4d3e4b0958f96034c9be9d82c7a1d2.zip |
Merge pull request #588 from haberman/conformance-json
Added support for JSON and valid input to conformance tests.
Diffstat (limited to 'conformance/conformance_cpp.cc')
-rw-r--r-- | conformance/conformance_cpp.cc | 56 |
1 files changed, 47 insertions, 9 deletions
diff --git a/conformance/conformance_cpp.cc b/conformance/conformance_cpp.cc index ff47fbbf..863b6a5b 100644 --- a/conformance/conformance_cpp.cc +++ b/conformance/conformance_cpp.cc @@ -33,14 +33,33 @@ #include <unistd.h> #include "conformance.pb.h" +#include <google/protobuf/util/json_util.h> +#include <google/protobuf/util/type_resolver_util.h> -using std::string; using conformance::ConformanceRequest; using conformance::ConformanceResponse; using conformance::TestAllTypes; +using google::protobuf::Descriptor; +using google::protobuf::DescriptorPool; +using google::protobuf::internal::scoped_ptr; +using google::protobuf::util::BinaryToJsonString; +using google::protobuf::util::JsonToBinaryString; +using google::protobuf::util::NewTypeResolverForDescriptorPool; +using google::protobuf::util::Status; +using google::protobuf::util::TypeResolver; +using std::string; + +static const char kTypeUrlPrefix[] = "type.googleapis.com"; + +static string GetTypeUrl(const Descriptor* message) { + return string(kTypeUrlPrefix) + "/" + message->full_name(); +} int test_count = 0; bool verbose = false; +TypeResolver* type_resolver; +string* type_url; + bool CheckedRead(int fd, void *buf, size_t len) { size_t ofs = 0; @@ -79,27 +98,43 @@ void DoTest(const ConformanceRequest& request, ConformanceResponse* response) { } break; - case ConformanceRequest::kJsonPayload: - response->set_runtime_error("JSON input is not yet supported."); + case ConformanceRequest::kJsonPayload: { + string proto_binary; + Status status = JsonToBinaryString(type_resolver, *type_url, + request.json_payload(), &proto_binary); + if (!status.ok()) { + response->set_parse_error(string("Parse error: ") + + status.error_message().as_string()); + return; + } + + GOOGLE_CHECK(test_message.ParseFromString(proto_binary)); break; + } case ConformanceRequest::PAYLOAD_NOT_SET: GOOGLE_LOG(FATAL) << "Request didn't have payload."; break; } - switch (request.requested_output()) { - case ConformanceRequest::UNSPECIFIED: + switch (request.requested_output_format()) { + case conformance::UNSPECIFIED: GOOGLE_LOG(FATAL) << "Unspecified output format"; break; - case ConformanceRequest::PROTOBUF: - test_message.SerializeToString(response->mutable_protobuf_payload()); + case conformance::PROTOBUF: + GOOGLE_CHECK( + test_message.SerializeToString(response->mutable_protobuf_payload())); break; - case ConformanceRequest::JSON: - response->set_runtime_error("JSON output is not yet supported."); + case conformance::JSON: { + string proto_binary; + GOOGLE_CHECK(test_message.SerializeToString(&proto_binary)); + Status status = BinaryToJsonString(type_resolver, *type_url, proto_binary, + response->mutable_json_payload()); + GOOGLE_CHECK(status.ok()); break; + } } } @@ -146,6 +181,9 @@ bool DoTestIo() { } int main() { + type_resolver = NewTypeResolverForDescriptorPool( + kTypeUrlPrefix, DescriptorPool::generated_pool()); + type_url = new string(GetTypeUrl(TestAllTypes::descriptor())); while (1) { if (!DoTestIo()) { fprintf(stderr, "conformance-cpp: received EOF from test runner " |