aboutsummaryrefslogtreecommitdiffhomepage
path: root/conformance/conformance_cpp.cc
diff options
context:
space:
mode:
authorGravatar Josh Haberman <jhaberman@gmail.com>2015-07-10 16:36:59 -0700
committerGravatar Josh Haberman <jhaberman@gmail.com>2015-07-10 16:36:59 -0700
commitb0500b37b231b8f93a5b1b95e23d13830e11eb07 (patch)
treeda6aab20d86574dee821130e36741d02ccfd83c2 /conformance/conformance_cpp.cc
parentfe50044041e8b8bc6b8d8183e8d043ed6dba403e (diff)
Added support for Json and valid input to conformance tests.
This was enabled by the recent open-sourcing of JSON support and MessageDifferencer. MessageDifferencer allows the conformance suite to expand because it allows us to write tests for payloads that parse successfully. To verify the testee's output payload, we need to parse it back into a message and compare the message instances. Comparing output bytes vs. a golden message is *not* valid, because protobufs do not have a canonical encoding (especially in the presence of maps, which have no prescribed serialization order). We only add one small JSON test for now, but with the framework in place we now have the foundation to dramatically expand the coverage of the conformance test suite. Also added the ability for the testee to skip tests that exercise features that are unimplemented. This allows Java (which currently has no JSON support) to skip tests involving JSON. Change-Id: I697b4363da432b61ae3b638b4287c4cda1af4deb
Diffstat (limited to 'conformance/conformance_cpp.cc')
-rw-r--r--conformance/conformance_cpp.cc56
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 "