parent
92cfb8a56d
commit
454a780602
6 changed files with 224 additions and 1 deletions
@ -0,0 +1,74 @@ |
||||
#include "google/protobuf/unredacted_debug_format_for_test.h" |
||||
|
||||
#include <string> |
||||
|
||||
#include "google/protobuf/message.h" |
||||
#include "google/protobuf/message_lite.h" |
||||
#include "google/protobuf/text_format.h" |
||||
|
||||
namespace google { |
||||
namespace protobuf { |
||||
namespace util { |
||||
|
||||
std::string UnredactedDebugFormatForTest(const google::protobuf::Message& message) { |
||||
std::string debug_string; |
||||
|
||||
google::protobuf::TextFormat::Printer printer; |
||||
printer.SetExpandAny(true); |
||||
printer.SetReportSensitiveFields( |
||||
internal::FieldReporterLevel::kUnredactedDebugFormatForTest); |
||||
|
||||
printer.PrintToString(message, &debug_string); |
||||
|
||||
return debug_string; |
||||
} |
||||
|
||||
std::string UnredactedShortDebugFormatForTest(const google::protobuf::Message& message) { |
||||
std::string debug_string; |
||||
|
||||
google::protobuf::TextFormat::Printer printer; |
||||
printer.SetSingleLineMode(true); |
||||
printer.SetExpandAny(true); |
||||
printer.SetReportSensitiveFields( |
||||
internal::FieldReporterLevel::kUnredactedShortDebugFormatForTest); |
||||
|
||||
printer.PrintToString(message, &debug_string); |
||||
// Single line mode currently might have an extra space at the end.
|
||||
if (!debug_string.empty() && debug_string[debug_string.size() - 1] == ' ') { |
||||
debug_string.resize(debug_string.size() - 1); |
||||
} |
||||
|
||||
return debug_string; |
||||
} |
||||
|
||||
std::string UnredactedUtf8DebugFormatForTest(const google::protobuf::Message& message) { |
||||
std::string debug_string; |
||||
|
||||
google::protobuf::TextFormat::Printer printer; |
||||
printer.SetUseUtf8StringEscaping(true); |
||||
printer.SetExpandAny(true); |
||||
printer.SetReportSensitiveFields( |
||||
internal::FieldReporterLevel::kUnredactedUtf8DebugFormatForTest); |
||||
|
||||
printer.PrintToString(message, &debug_string); |
||||
|
||||
return debug_string; |
||||
} |
||||
|
||||
std::string UnredactedDebugFormatForTest(const google::protobuf::MessageLite& message) { |
||||
return message.DebugString(); |
||||
} |
||||
|
||||
std::string UnredactedShortDebugFormatForTest( |
||||
const google::protobuf::MessageLite& message) { |
||||
return message.ShortDebugString(); |
||||
} |
||||
|
||||
std::string UnredactedUtf8DebugFormatForTest( |
||||
const google::protobuf::MessageLite& message) { |
||||
return message.Utf8DebugString(); |
||||
} |
||||
|
||||
} // namespace util
|
||||
} // namespace protobuf
|
||||
} // namespace google
|
@ -0,0 +1,33 @@ |
||||
#ifndef GOOGLE_PROTOBUF_UNREDACTED_DEBUG_FORMAT_FOR_TEST_H__ |
||||
#define GOOGLE_PROTOBUF_UNREDACTED_DEBUG_FORMAT_FOR_TEST_H__ |
||||
|
||||
#include "google/protobuf/message.h" |
||||
#include "google/protobuf/message_lite.h" |
||||
|
||||
namespace google { |
||||
namespace protobuf { |
||||
namespace util { |
||||
|
||||
// Generates a human-readable form of this message for debugging purposes in
|
||||
// test-only code. This API does not redact any fields in the message.
|
||||
std::string UnredactedDebugFormatForTest(const google::protobuf::Message& message); |
||||
// Like UnredactedDebugFormatForTest(), but prints the message in a single line.
|
||||
std::string UnredactedShortDebugFormatForTest(const google::protobuf::Message& message); |
||||
// Like UnredactedDebugFormatForTest(), but does not escape UTF-8 byte
|
||||
// sequences.
|
||||
std::string UnredactedUtf8DebugFormatForTest(const google::protobuf::Message& message); |
||||
|
||||
// The following APIs are added just to work with code that interoperates with
|
||||
// `Message` and `MessageLite`.
|
||||
|
||||
std::string UnredactedDebugFormatForTest(const google::protobuf::MessageLite& message); |
||||
std::string UnredactedShortDebugFormatForTest( |
||||
const google::protobuf::MessageLite& message); |
||||
std::string UnredactedUtf8DebugFormatForTest( |
||||
const google::protobuf::MessageLite& message); |
||||
|
||||
} // namespace util
|
||||
} // namespace protobuf
|
||||
} // namespace google
|
||||
|
||||
#endif // GOOGLE_PROTOBUF_UNREDACTED_DEBUG_FORMAT_FOR_TEST_H__
|
@ -0,0 +1,88 @@ |
||||
#include "google/protobuf/unredacted_debug_format_for_test.h" |
||||
|
||||
#include <gmock/gmock.h> |
||||
#include <gtest/gtest.h> |
||||
#include "google/protobuf/unittest.pb.h" |
||||
#include "google/protobuf/unittest_lite.pb.h" |
||||
|
||||
namespace { |
||||
|
||||
using ::google::protobuf::util::UnredactedDebugFormatForTest; |
||||
using ::google::protobuf::util::UnredactedShortDebugFormatForTest; |
||||
using ::google::protobuf::util::UnredactedUtf8DebugFormatForTest; |
||||
using ::testing::StrEq; |
||||
|
||||
TEST(UnredactedDebugFormatAPITest, MessageUnredactedDebugFormat) { |
||||
protobuf_unittest::RedactedFields proto; |
||||
protobuf_unittest::TestNestedMessageRedaction redacted_nested_proto; |
||||
protobuf_unittest::TestNestedMessageRedaction unredacted_nested_proto; |
||||
redacted_nested_proto.set_optional_unredacted_nested_string( |
||||
"\350\260\267\346\255\214"); |
||||
unredacted_nested_proto.set_optional_unredacted_nested_string( |
||||
"\350\260\267\346\255\214"); |
||||
*proto.mutable_optional_redacted_message() = redacted_nested_proto; |
||||
*proto.mutable_optional_unredacted_message() = unredacted_nested_proto; |
||||
|
||||
EXPECT_THAT(UnredactedDebugFormatForTest(proto), |
||||
StrEq("optional_redacted_message {\n " |
||||
"optional_unredacted_nested_string: " |
||||
"\"\\350\\260\\267\\346\\255\\214\"\n}\n" |
||||
"optional_unredacted_message {\n " |
||||
"optional_unredacted_nested_string: " |
||||
"\"\\350\\260\\267\\346\\255\\214\"\n}\n")); |
||||
} |
||||
|
||||
TEST(UnredactedDebugFormatAPITest, MessageUnredactedShortDebugFormat) { |
||||
protobuf_unittest::RedactedFields proto; |
||||
protobuf_unittest::TestNestedMessageRedaction redacted_nested_proto; |
||||
protobuf_unittest::TestNestedMessageRedaction unredacted_nested_proto; |
||||
redacted_nested_proto.set_optional_unredacted_nested_string("hello"); |
||||
unredacted_nested_proto.set_optional_unredacted_nested_string("world"); |
||||
*proto.mutable_optional_redacted_message() = redacted_nested_proto; |
||||
*proto.mutable_optional_unredacted_message() = unredacted_nested_proto; |
||||
|
||||
EXPECT_THAT(UnredactedShortDebugFormatForTest(proto), |
||||
StrEq("optional_redacted_message { " |
||||
"optional_unredacted_nested_string: \"hello\" } " |
||||
"optional_unredacted_message { " |
||||
"optional_unredacted_nested_string: \"world\" }")); |
||||
} |
||||
|
||||
TEST(UnredactedDebugFormatAPITest, MessageUnredactedUtf8DebugFormat) { |
||||
protobuf_unittest::RedactedFields proto; |
||||
protobuf_unittest::TestNestedMessageRedaction redacted_nested_proto; |
||||
protobuf_unittest::TestNestedMessageRedaction unredacted_nested_proto; |
||||
redacted_nested_proto.set_optional_unredacted_nested_string( |
||||
"\350\260\267\346\255\214"); |
||||
unredacted_nested_proto.set_optional_unredacted_nested_string( |
||||
"\350\260\267\346\255\214"); |
||||
*proto.mutable_optional_redacted_message() = redacted_nested_proto; |
||||
*proto.mutable_optional_unredacted_message() = unredacted_nested_proto; |
||||
|
||||
EXPECT_THAT(UnredactedUtf8DebugFormatForTest(proto), |
||||
StrEq("optional_redacted_message {\n " |
||||
"optional_unredacted_nested_string: " |
||||
"\"\xE8\xB0\xB7\xE6\xAD\x8C\"\n}\n" |
||||
"optional_unredacted_message {\n " |
||||
"optional_unredacted_nested_string: " |
||||
"\"\xE8\xB0\xB7\xE6\xAD\x8C\"\n}\n")); |
||||
} |
||||
|
||||
TEST(UnredactedDebugFormatAPITest, LiteUnredactedDebugFormat) { |
||||
protobuf_unittest::TestAllTypesLite message; |
||||
EXPECT_EQ(UnredactedDebugFormatForTest(message), message.DebugString()); |
||||
} |
||||
|
||||
TEST(UnredactedDebugFormatAPITest, LiteUnredactedShortDebugFormat) { |
||||
protobuf_unittest::TestAllTypesLite message; |
||||
EXPECT_EQ(UnredactedShortDebugFormatForTest(message), |
||||
message.ShortDebugString()); |
||||
} |
||||
|
||||
TEST(UnredactedDebugFormatAPITest, LiteUnredactedUtf8DebugFormat) { |
||||
protobuf_unittest::TestAllTypesLite message; |
||||
EXPECT_EQ(UnredactedUtf8DebugFormatForTest(message), |
||||
message.Utf8DebugString()); |
||||
} |
||||
|
||||
} // namespace
|
Loading…
Reference in new issue