xref: /aosp_15_r20/external/grpc-grpc/test/cpp/util/channel_trace_proto_helper.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 //
2 //
3 // Copyright 2018 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // 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, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18 
19 #include "test/cpp/util/channel_trace_proto_helper.h"
20 
21 #include <gtest/gtest.h>
22 
23 #include <grpc/grpc.h>
24 #include <grpc/support/log.h>
25 #include <grpc/support/port_platform.h>
26 #include <grpcpp/impl/codegen/config_protobuf.h>
27 #include <grpcpp/support/config.h>
28 
29 #include "src/core/lib/gprpp/crash.h"
30 #include "src/core/lib/iomgr/error.h"
31 #include "src/core/lib/json/json.h"
32 #include "src/core/lib/json/json_reader.h"
33 #include "src/core/lib/json/json_writer.h"
34 #include "src/proto/grpc/channelz/channelz.pb.h"
35 
36 namespace grpc {
37 
38 namespace {
39 
40 // Generic helper that takes in a json string, converts it to a proto, and
41 // then back to json. This ensures that the json string was correctly formatted
42 // according to https://developers.google.com/protocol-buffers/docs/proto3#json
43 template <typename Message>
VaidateProtoJsonTranslation(const std::string & json_str)44 void VaidateProtoJsonTranslation(const std::string& json_str) {
45   Message msg;
46   grpc::protobuf::json::JsonParseOptions parse_options;
47   // If the following line is failing, then uncomment the last line of the
48   // comment, and uncomment the lines that print the two strings. You can
49   // then compare the output, and determine what fields are missing.
50   //
51   // parse_options.ignore_unknown_fields = true;
52   grpc::protobuf::util::Status s =
53       grpc::protobuf::json::JsonStringToMessage(json_str, &msg, parse_options);
54   EXPECT_TRUE(s.ok());
55   std::string proto_json_str;
56   grpc::protobuf::json::JsonPrintOptions print_options;
57   // We usually do not want this to be true, however it can be helpful to
58   // uncomment and see the output produced then all fields are printed.
59   // print_options.always_print_primitive_fields = true;
60   s = grpc::protobuf::json::MessageToJsonString(msg, &proto_json_str,
61                                                 print_options);
62   EXPECT_TRUE(s.ok());
63   // Parse JSON and re-dump to string, to make sure formatting is the
64   // same as what would be generated by our JSON library.
65   auto parsed_json = grpc_core::JsonParse(proto_json_str.c_str());
66   ASSERT_TRUE(parsed_json.ok()) << parsed_json.status();
67   ASSERT_EQ(parsed_json->type(), grpc_core::Json::Type::kObject);
68   proto_json_str = grpc_core::JsonDump(*parsed_json);
69   EXPECT_EQ(json_str, proto_json_str);
70 }
71 
72 }  // namespace
73 
74 namespace testing {
75 
ValidateChannelTraceProtoJsonTranslation(const char * json_c_str)76 void ValidateChannelTraceProtoJsonTranslation(const char* json_c_str) {
77   VaidateProtoJsonTranslation<grpc::channelz::v1::ChannelTrace>(json_c_str);
78 }
79 
ValidateChannelProtoJsonTranslation(const char * json_c_str)80 void ValidateChannelProtoJsonTranslation(const char* json_c_str) {
81   VaidateProtoJsonTranslation<grpc::channelz::v1::Channel>(json_c_str);
82 }
83 
ValidateGetTopChannelsResponseProtoJsonTranslation(const char * json_c_str)84 void ValidateGetTopChannelsResponseProtoJsonTranslation(
85     const char* json_c_str) {
86   VaidateProtoJsonTranslation<grpc::channelz::v1::GetTopChannelsResponse>(
87       json_c_str);
88 }
89 
ValidateGetChannelResponseProtoJsonTranslation(const char * json_c_str)90 void ValidateGetChannelResponseProtoJsonTranslation(const char* json_c_str) {
91   VaidateProtoJsonTranslation<grpc::channelz::v1::GetChannelResponse>(
92       json_c_str);
93 }
94 
ValidateGetServerResponseProtoJsonTranslation(const char * json_c_str)95 void ValidateGetServerResponseProtoJsonTranslation(const char* json_c_str) {
96   VaidateProtoJsonTranslation<grpc::channelz::v1::GetServerResponse>(
97       json_c_str);
98 }
99 
ValidateSubchannelProtoJsonTranslation(const char * json_c_str)100 void ValidateSubchannelProtoJsonTranslation(const char* json_c_str) {
101   VaidateProtoJsonTranslation<grpc::channelz::v1::Subchannel>(json_c_str);
102 }
103 
ValidateServerProtoJsonTranslation(const char * json_c_str)104 void ValidateServerProtoJsonTranslation(const char* json_c_str) {
105   VaidateProtoJsonTranslation<grpc::channelz::v1::Server>(json_c_str);
106 }
107 
ValidateGetServersResponseProtoJsonTranslation(const char * json_c_str)108 void ValidateGetServersResponseProtoJsonTranslation(const char* json_c_str) {
109   VaidateProtoJsonTranslation<grpc::channelz::v1::GetServersResponse>(
110       json_c_str);
111 }
112 
113 }  // namespace testing
114 }  // namespace grpc
115