Internal change.

PiperOrigin-RevId: 586452605
pull/14865/head
Protobuf Team Bot 1 year ago committed by Mike Kruskal
parent 92cfb8a56d
commit 454a780602
  1. 1
      pkg/BUILD.bazel
  2. 24
      src/google/protobuf/BUILD.bazel
  3. 5
      src/google/protobuf/text_format.h
  4. 74
      src/google/protobuf/unredacted_debug_format_for_test.cc
  5. 33
      src/google/protobuf/unredacted_debug_format_for_test.h
  6. 88
      src/google/protobuf/unredacted_debug_format_for_test_test.cc

@ -220,6 +220,7 @@ cc_dist_library(
"//src/google/protobuf:lite_test_util",
"//src/google/protobuf:test_util",
"//src/google/protobuf:test_util2",
"//src/google/protobuf:unredacted_debug_format_for_test",
"//src/google/protobuf/compiler:annotation_test_util",
"//src/google/protobuf/compiler/cpp:unittest_lib",
"//src/google/protobuf/io:test_zero_copy_stream",

@ -1570,6 +1570,30 @@ cc_test(
],
)
cc_library(
name = "unredacted_debug_format_for_test",
testonly = True,
srcs = ["unredacted_debug_format_for_test.cc"],
hdrs = ["unredacted_debug_format_for_test.h"],
strip_include_prefix = "/src",
visibility = ["//visibility:public"],
deps = [
":protobuf",
],
)
cc_test(
name = "unredacted_debug_format_for_test_test",
srcs = ["unredacted_debug_format_for_test_test.cc"],
deps = [
":cc_lite_test_protos",
":cc_test_protos",
":protobuf",
":unredacted_debug_format_for_test",
"@com_google_googletest//:gtest_main",
],
)
################################################################################
# Helper targets for Kotlin tests
################################################################################

@ -66,7 +66,10 @@ PROTOBUF_EXPORT enum class FieldReporterLevel {
kUtf8Format = 8,
kDebugString = 12,
kShortDebugString = 13,
kUtf8DebugString = 14
kUtf8DebugString = 14,
kUnredactedDebugFormatForTest = 15,
kUnredactedShortDebugFormatForTest = 16,
kUnredactedUtf8DebugFormatForTest = 17
};
} // namespace internal

@ -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…
Cancel
Save