brpc_proto_unittest.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Licensed to the Apache Software Foundation (ASF) under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The ASF licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. // Date: 2015/03/31 14:50:20
  18. #include <gtest/gtest.h>
  19. #include <google/protobuf/descriptor.h>
  20. #include <google/protobuf/descriptor.pb.h>
  21. #include <google/protobuf/dynamic_message.h>
  22. #include "brpc/policy/baidu_rpc_meta.pb.h"
  23. #include "echo.pb.h"
  24. namespace {
  25. using namespace google::protobuf;
  26. using namespace brpc;
  27. void BuildDependency(const FileDescriptor *file_desc, DescriptorPool *pool) {
  28. for (int i = 0; i < file_desc->dependency_count(); ++i) {
  29. const FileDescriptor *fd = file_desc->dependency(i);
  30. BuildDependency(fd, pool);
  31. FileDescriptorProto proto;
  32. fd->CopyTo(&proto);
  33. ASSERT_TRUE(pool->BuildFile(proto) != NULL);
  34. }
  35. FileDescriptorProto proto;
  36. file_desc->CopyTo(&proto);
  37. ASSERT_TRUE(pool->BuildFile(proto) != NULL);
  38. }
  39. TEST(ProtoTest, proto) {
  40. policy::RpcMeta meta;
  41. const Descriptor *desc = meta.GetDescriptor();
  42. const FileDescriptor *file_desc = desc->file();
  43. DescriptorPool pool;
  44. DynamicMessageFactory factory(&pool);
  45. BuildDependency(file_desc, &pool);
  46. FileDescriptorProto file_desc_proto;
  47. file_desc->CopyTo(&file_desc_proto);
  48. const FileDescriptor *new_file_desc = pool.BuildFile(file_desc_proto);
  49. ASSERT_TRUE(new_file_desc != NULL);
  50. const Descriptor *new_desc = new_file_desc->FindMessageTypeByName(desc->name());
  51. ASSERT_TRUE(new_desc != NULL);
  52. meta.set_correlation_id(123);
  53. std::string data;
  54. ASSERT_TRUE(meta.SerializeToString(&data));
  55. Message *msg = factory.GetPrototype(new_desc)->New();
  56. ASSERT_TRUE(msg != NULL);
  57. ASSERT_TRUE(msg->ParseFromString(data));
  58. ASSERT_TRUE(msg->SerializeToString(&data));
  59. policy::RpcMeta new_meta;
  60. ASSERT_TRUE(new_meta.ParseFromString(data));
  61. ASSERT_EQ(123, new_meta.correlation_id());
  62. }
  63. TEST(ProtoTest, required_enum) {
  64. test::Message1 msg1;
  65. msg1.set_stat(test::STATE0_NUM_1);
  66. std::string buf;
  67. ASSERT_TRUE(msg1.SerializeToString(&buf));
  68. test::Message2 msg2;
  69. ASSERT_TRUE(msg2.ParseFromString(buf));
  70. ASSERT_EQ((int)msg1.stat(), (int)msg2.stat());
  71. msg1.set_stat(test::STATE0_NUM_2);
  72. ASSERT_TRUE(msg1.SerializeToString(&buf));
  73. ASSERT_FALSE(msg2.ParseFromString(buf));
  74. }
  75. } //namespace