• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <google/protobuf/text_format.h>
22 #include <google/protobuf/util/json_util.h>
23 
24 #include <grpc/grpc.h>
25 #include <grpc/support/log.h>
26 #include <gtest/gtest.h>
27 
28 #include "src/proto/grpc/channelz/channelz.pb.h"
29 
30 namespace grpc {
31 namespace testing {
32 
33 namespace {
34 
35 // Generic helper that takes in a json string, converts it to a proto, and
36 // then back to json. This ensures that the json string was correctly formatted
37 // according to https://developers.google.com/protocol-buffers/docs/proto3#json
38 template <typename Message>
VaidateProtoJsonTranslation(char * json_c_str)39 void VaidateProtoJsonTranslation(char* json_c_str) {
40   std::string json_str(json_c_str);
41   Message msg;
42   google::protobuf::util::JsonParseOptions parse_options;
43   // If the following line is failing, then uncomment the last line of the
44   // comment, and uncomment the lines that print the two strings. You can
45   // then compare the output, and determine what fields are missing.
46   //
47   // parse_options.ignore_unknown_fields = true;
48   EXPECT_EQ(google::protobuf::util::JsonStringToMessage(json_str, &msg,
49                                                         parse_options),
50             google::protobuf::util::Status::OK);
51   std::string proto_json_str;
52   google::protobuf::util::JsonPrintOptions print_options;
53   // We usually do not want this to be true, however it can be helpful to
54   // uncomment and see the output produced then all fields are printed.
55   // print_options.always_print_primitive_fields = true;
56   EXPECT_EQ(google::protobuf::util::MessageToJsonString(msg, &proto_json_str,
57                                                         print_options),
58             google::protobuf::util::Status::OK);
59   // uncomment these to compare the the json strings.
60   // gpr_log(GPR_ERROR, "tracer json: %s", json_str.c_str());
61   // gpr_log(GPR_ERROR, "proto  json: %s", proto_json_str.c_str());
62   EXPECT_EQ(json_str, proto_json_str);
63 }
64 
65 }  // namespace
66 
ValidateChannelTraceProtoJsonTranslation(char * json_c_str)67 void ValidateChannelTraceProtoJsonTranslation(char* json_c_str) {
68   VaidateProtoJsonTranslation<grpc::channelz::v1::ChannelTrace>(json_c_str);
69 }
70 
ValidateChannelProtoJsonTranslation(char * json_c_str)71 void ValidateChannelProtoJsonTranslation(char* json_c_str) {
72   VaidateProtoJsonTranslation<grpc::channelz::v1::Channel>(json_c_str);
73 }
74 
ValidateGetTopChannelsResponseProtoJsonTranslation(char * json_c_str)75 void ValidateGetTopChannelsResponseProtoJsonTranslation(char* json_c_str) {
76   VaidateProtoJsonTranslation<grpc::channelz::v1::GetTopChannelsResponse>(
77       json_c_str);
78 }
79 
ValidateGetChannelResponseProtoJsonTranslation(char * json_c_str)80 void ValidateGetChannelResponseProtoJsonTranslation(char* json_c_str) {
81   VaidateProtoJsonTranslation<grpc::channelz::v1::GetChannelResponse>(
82       json_c_str);
83 }
84 
ValidateSubchannelProtoJsonTranslation(char * json_c_str)85 void ValidateSubchannelProtoJsonTranslation(char* json_c_str) {
86   VaidateProtoJsonTranslation<grpc::channelz::v1::Subchannel>(json_c_str);
87 }
88 
ValidateServerProtoJsonTranslation(char * json_c_str)89 void ValidateServerProtoJsonTranslation(char* json_c_str) {
90   VaidateProtoJsonTranslation<grpc::channelz::v1::Server>(json_c_str);
91 }
92 
ValidateGetServersResponseProtoJsonTranslation(char * json_c_str)93 void ValidateGetServersResponseProtoJsonTranslation(char* json_c_str) {
94   VaidateProtoJsonTranslation<grpc::channelz::v1::GetServersResponse>(
95       json_c_str);
96 }
97 
98 }  // namespace testing
99 }  // namespace grpc
100