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 <grpc/support/port_platform.h>
20
21 #include "test/cpp/util/channel_trace_proto_helper.h"
22
23 #include <grpc/grpc.h>
24 #include <grpc/support/log.h>
25 #include <grpcpp/impl/codegen/config.h>
26 #include <grpcpp/impl/codegen/config_protobuf.h>
27 #include <gtest/gtest.h>
28
29 #include "src/core/lib/iomgr/error.h"
30 #include "src/core/lib/json/json.h"
31 #include "src/proto/grpc/channelz/channelz.pb.h"
32
33 namespace grpc {
34
35 namespace {
36
37 // Generic helper that takes in a json string, converts it to a proto, and
38 // then back to json. This ensures that the json string was correctly formatted
39 // according to https://developers.google.com/protocol-buffers/docs/proto3#json
40 template <typename Message>
VaidateProtoJsonTranslation(const std::string & json_str)41 void VaidateProtoJsonTranslation(const std::string& json_str) {
42 Message msg;
43 grpc::protobuf::json::JsonParseOptions parse_options;
44 // If the following line is failing, then uncomment the last line of the
45 // comment, and uncomment the lines that print the two strings. You can
46 // then compare the output, and determine what fields are missing.
47 //
48 // parse_options.ignore_unknown_fields = true;
49 grpc::protobuf::util::Status s =
50 grpc::protobuf::json::JsonStringToMessage(json_str, &msg, parse_options);
51 EXPECT_TRUE(s.ok());
52 std::string proto_json_str;
53 grpc::protobuf::json::JsonPrintOptions print_options;
54 // We usually do not want this to be true, however it can be helpful to
55 // uncomment and see the output produced then all fields are printed.
56 // print_options.always_print_primitive_fields = true;
57 s = grpc::protobuf::json::MessageToJsonString(msg, &proto_json_str);
58 EXPECT_TRUE(s.ok());
59 // Parse JSON and re-dump to string, to make sure formatting is the
60 // same as what would be generated by our JSON library.
61 grpc_error* error = GRPC_ERROR_NONE;
62 grpc_core::Json parsed_json =
63 grpc_core::Json::Parse(proto_json_str.c_str(), &error);
64 ASSERT_EQ(error, GRPC_ERROR_NONE) << grpc_error_string(error);
65 ASSERT_EQ(parsed_json.type(), grpc_core::Json::Type::OBJECT);
66 proto_json_str = parsed_json.Dump();
67 // uncomment these to compare the json strings.
68 // gpr_log(GPR_ERROR, "tracer json: %s", json_str.c_str());
69 // gpr_log(GPR_ERROR, "proto json: %s", proto_json_str.c_str());
70 EXPECT_EQ(json_str, proto_json_str);
71 }
72
73 } // namespace
74
75 namespace testing {
76
ValidateChannelTraceProtoJsonTranslation(const char * json_c_str)77 void ValidateChannelTraceProtoJsonTranslation(const char* json_c_str) {
78 VaidateProtoJsonTranslation<grpc::channelz::v1::ChannelTrace>(json_c_str);
79 }
80
ValidateChannelProtoJsonTranslation(const char * json_c_str)81 void ValidateChannelProtoJsonTranslation(const char* json_c_str) {
82 VaidateProtoJsonTranslation<grpc::channelz::v1::Channel>(json_c_str);
83 }
84
ValidateGetTopChannelsResponseProtoJsonTranslation(const char * json_c_str)85 void ValidateGetTopChannelsResponseProtoJsonTranslation(
86 const char* json_c_str) {
87 VaidateProtoJsonTranslation<grpc::channelz::v1::GetTopChannelsResponse>(
88 json_c_str);
89 }
90
ValidateGetChannelResponseProtoJsonTranslation(const char * json_c_str)91 void ValidateGetChannelResponseProtoJsonTranslation(const char* json_c_str) {
92 VaidateProtoJsonTranslation<grpc::channelz::v1::GetChannelResponse>(
93 json_c_str);
94 }
95
ValidateGetServerResponseProtoJsonTranslation(const char * json_c_str)96 void ValidateGetServerResponseProtoJsonTranslation(const char* json_c_str) {
97 VaidateProtoJsonTranslation<grpc::channelz::v1::GetServerResponse>(
98 json_c_str);
99 }
100
ValidateSubchannelProtoJsonTranslation(const char * json_c_str)101 void ValidateSubchannelProtoJsonTranslation(const char* json_c_str) {
102 VaidateProtoJsonTranslation<grpc::channelz::v1::Subchannel>(json_c_str);
103 }
104
ValidateServerProtoJsonTranslation(const char * json_c_str)105 void ValidateServerProtoJsonTranslation(const char* json_c_str) {
106 VaidateProtoJsonTranslation<grpc::channelz::v1::Server>(json_c_str);
107 }
108
ValidateGetServersResponseProtoJsonTranslation(const char * json_c_str)109 void ValidateGetServersResponseProtoJsonTranslation(const char* json_c_str) {
110 VaidateProtoJsonTranslation<grpc::channelz::v1::GetServersResponse>(
111 json_c_str);
112 }
113
114 } // namespace testing
115 } // namespace grpc
116