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