diff options
author | Yilun Chong <chongyilun250@sina.com> | 2017-07-24 14:50:52 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-07-24 14:50:52 -0700 |
commit | a713b73076f1d90bdc39ea8805b50421a59d7986 (patch) | |
tree | cea38fdfe5c1d93b6291e96332961b2c3bf5439f /conformance/conformance_cpp.cc | |
parent | f15185d370a5789c65105f77ba806c9faa563325 (diff) | |
parent | bceb830c396e3370d4cc15e0d4f96ed1819dcca9 (diff) | |
download | protobuf-a713b73076f1d90bdc39ea8805b50421a59d7986.tar.gz protobuf-a713b73076f1d90bdc39ea8805b50421a59d7986.tar.bz2 protobuf-a713b73076f1d90bdc39ea8805b50421a59d7986.zip |
Merge pull request #3281 from BSBandme/ConformanceTestYilunChong
Proto2 test message support to conformance test
Diffstat (limited to 'conformance/conformance_cpp.cc')
-rw-r--r-- | conformance/conformance_cpp.cc | 31 |
1 files changed, 21 insertions, 10 deletions
diff --git a/conformance/conformance_cpp.cc b/conformance/conformance_cpp.cc index b865cd93..bf70309a 100644 --- a/conformance/conformance_cpp.cc +++ b/conformance/conformance_cpp.cc @@ -34,6 +34,8 @@ #include "conformance.pb.h" #include <google/protobuf/test_messages_proto3.pb.h> +#include <google/protobuf/test_messages_proto2.pb.h> +#include <google/protobuf/message.h> #include <google/protobuf/util/json_util.h> #include <google/protobuf/util/type_resolver_util.h> @@ -41,13 +43,15 @@ using conformance::ConformanceRequest; using conformance::ConformanceResponse; using google::protobuf::Descriptor; using google::protobuf::DescriptorPool; +using google::protobuf::Message; +using google::protobuf::MessageFactory; 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 protobuf_test_messages::proto3::TestAllTypes; +using protobuf_test_messages::proto3::TestAllTypesProto3; using std::string; static const char kTypeUrlPrefix[] = "type.googleapis.com"; @@ -87,17 +91,24 @@ void CheckedWrite(int fd, const void *buf, size_t len) { } void DoTest(const ConformanceRequest& request, ConformanceResponse* response) { - TestAllTypes test_message; + Message *test_message; + const Descriptor *descriptor = DescriptorPool::generated_pool()->FindMessageTypeByName( + request.message_type()); + if (!descriptor) { + GOOGLE_LOG(FATAL) << "No such message type: " << request.message_type(); + } + test_message = MessageFactory::generated_factory()->GetPrototype(descriptor)->New(); switch (request.payload_case()) { - case ConformanceRequest::kProtobufPayload: - if (!test_message.ParseFromString(request.protobuf_payload())) { + case ConformanceRequest::kProtobufPayload: { + if (!test_message->ParseFromString(request.protobuf_payload())) { // Getting parse details would involve something like: // http://stackoverflow.com/questions/22121922/how-can-i-get-more-details-about-errors-generated-during-protobuf-parsing-c response->set_parse_error("Parse error (no more details available)."); return; } break; + } case ConformanceRequest::kJsonPayload: { string proto_binary; @@ -109,7 +120,7 @@ void DoTest(const ConformanceRequest& request, ConformanceResponse* response) { return; } - if (!test_message.ParseFromString(proto_binary)) { + if (!test_message->ParseFromString(proto_binary)) { response->set_runtime_error( "Parsing JSON generates invalid proto output."); return; @@ -127,14 +138,14 @@ void DoTest(const ConformanceRequest& request, ConformanceResponse* response) { GOOGLE_LOG(FATAL) << "Unspecified output format"; break; - case conformance::PROTOBUF: - GOOGLE_CHECK( - test_message.SerializeToString(response->mutable_protobuf_payload())); + case conformance::PROTOBUF: { + GOOGLE_CHECK(test_message->SerializeToString(response->mutable_protobuf_payload())); break; + } case conformance::JSON: { string proto_binary; - GOOGLE_CHECK(test_message.SerializeToString(&proto_binary)); + GOOGLE_CHECK(test_message->SerializeToString(&proto_binary)); Status status = BinaryToJsonString(type_resolver, *type_url, proto_binary, response->mutable_json_payload()); if (!status.ok()) { @@ -197,7 +208,7 @@ bool DoTestIo() { int main() { type_resolver = NewTypeResolverForDescriptorPool( kTypeUrlPrefix, DescriptorPool::generated_pool()); - type_url = new string(GetTypeUrl(TestAllTypes::descriptor())); + type_url = new string(GetTypeUrl(TestAllTypesProto3::descriptor())); while (1) { if (!DoTestIo()) { fprintf(stderr, "conformance-cpp: received EOF from test runner " |