Skip to content

Commit

Permalink
Migrate from the constructor taking an optional which computes the CR…
Browse files Browse the repository at this point in the history
…C to WithComputedCrc.

Since the CRC will become optional, the other constructor will create an object without CRC.

PiperOrigin-RevId: 696894313
Change-Id: Iadd2910b046a4b179ec6e519d11d35c5c23e57b7
  • Loading branch information
tholenst authored and copybara-github committed Nov 15, 2024
1 parent d2c0f4e commit bbf2c95
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 21 deletions.
32 changes: 16 additions & 16 deletions tink/internal/proto_parser_secret_data_with_crc_field_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ struct ParsedStruct {
TEST(SecretDataWithCrcBytesField, ClearMemberWorks) {
SecretDataWithCrcField<ParsedStruct> field(1, &ParsedStruct::secret_with_crc);
ParsedStruct s;
s.secret_with_crc = SecretDataWithCrc("hello");
s.secret_with_crc = SecretDataWithCrc::WithComputedCrc("hello");
field.ClearMember(s);
EXPECT_THAT(s.secret_with_crc.UncheckedData(),
Eq(""));
Expand All @@ -62,7 +62,7 @@ TEST(SecretDataWithCrcBytesField, ClearMemberWorks) {
TEST(SecretDataWithCrcBytesField, ConsumeIntoMemberSuccessCases) {
SecretDataWithCrcField<ParsedStruct> field(1, &ParsedStruct::secret_with_crc);
ParsedStruct s;
s.secret_with_crc = SecretDataWithCrc("before");
s.secret_with_crc = SecretDataWithCrc::WithComputedCrc("before");

std::string bytes =
absl::StrCat(/* 10 bytes */ HexDecodeOrDie("0a"), "1234567890XYZ");
Expand All @@ -82,7 +82,7 @@ TEST(SecretDataWithCrcBytesField, ConsumeIntoMemberSuccessCases) {
TEST(SecretDataWithCrcBytesField, ConsumeIntoMemberRequiresStateWithCrc) {
SecretDataWithCrcField<ParsedStruct> field(1, &ParsedStruct::secret_with_crc);
ParsedStruct s;
s.secret_with_crc = SecretDataWithCrc("before");
s.secret_with_crc = SecretDataWithCrc::WithComputedCrc("before");

std::string bytes =
absl::StrCat(/* 10 bytes */ HexDecodeOrDie("0a"), "1234567890XYZ");
Expand All @@ -94,7 +94,7 @@ TEST(SecretDataWithCrcBytesField, ConsumeIntoMemberRequiresStateWithCrc) {
TEST(SecretDataWithCrcBytesField, ConsumeIntoMemberVarintSaysTooLong) {
SecretDataWithCrcField<ParsedStruct> field(1, &ParsedStruct::secret_with_crc);
ParsedStruct s;
s.secret_with_crc = SecretDataWithCrc("before");
s.secret_with_crc = SecretDataWithCrc::WithComputedCrc("before");

std::string bytes =
absl::StrCat(/* 10 bytes */ HexDecodeOrDie("0b"), "1234567890");
Expand All @@ -107,7 +107,7 @@ TEST(SecretDataWithCrcBytesField, ConsumeIntoMemberVarintSaysTooLong) {
TEST(SecretDataWithCrcBytesField, ConsumeIntoMemberEmptyString) {
SecretDataWithCrcField<ParsedStruct> field(1, &ParsedStruct::secret_with_crc);
ParsedStruct s;
s.secret_with_crc = SecretDataWithCrc("before");
s.secret_with_crc = SecretDataWithCrc::WithComputedCrc("before");

std::string bytes = absl::StrCat(/* 0 bytes */ HexDecodeOrDie("00"), "abcde");
absl::crc32c_t crc_to_maintain = absl::crc32c_t{};
Expand All @@ -124,7 +124,7 @@ TEST(SecretDataWithCrcBytesField, ConsumeIntoMemberEmptyString) {
TEST(SecretDataWithCrcBytesField, EmptyWithoutVarint) {
SecretDataWithCrcField<ParsedStruct> field(1, &ParsedStruct::secret_with_crc);
ParsedStruct s;
s.secret_with_crc = SecretDataWithCrc("before");
s.secret_with_crc = SecretDataWithCrc::WithComputedCrc("before");

std::string bytes = "";
absl::crc32c_t crc_to_maintain = absl::crc32c_t{};
Expand All @@ -136,7 +136,7 @@ TEST(SecretDataWithCrcBytesField, EmptyWithoutVarint) {
TEST(SecretDataWithCrcBytesField, InvalidVarint) {
SecretDataWithCrcField<ParsedStruct> field(1, &ParsedStruct::secret_with_crc);
ParsedStruct s;
s.secret_with_crc = SecretDataWithCrc("before");
s.secret_with_crc = SecretDataWithCrc::WithComputedCrc("before");

std::string bytes = absl::StrCat(HexDecodeOrDie("808080808000"), "abcde");
absl::crc32c_t crc_to_maintain = absl::crc32c_t{};
Expand All @@ -149,7 +149,7 @@ TEST(SecretDataWithCrcBytesField, InvalidVarint) {
TEST(SecretDataWithCrcBytesField, ExistingCRCIsExtendedWhenParsing) {
SecretDataWithCrcField<ParsedStruct> field(1, &ParsedStruct::secret_with_crc);
ParsedStruct s;
s.secret_with_crc = SecretDataWithCrc("before");
s.secret_with_crc = SecretDataWithCrc::WithComputedCrc("before");

std::string bytes =
absl::StrCat(/* 10 bytes */ HexDecodeOrDie("0a"), "1234567890XYZ");
Expand All @@ -170,7 +170,7 @@ TEST(SecretDataWithCrcBytesField, ExistingCRCIsExtendedWhenParsing) {
TEST(SecretDataWithCrcBytesField, SerializeEmpty) {
SecretDataWithCrcField<ParsedStruct> field(1, &ParsedStruct::secret_with_crc);
ParsedStruct s;
s.secret_with_crc = SecretDataWithCrc("");
s.secret_with_crc = SecretDataWithCrc::WithComputedCrc("");

std::string buffer = "BUFFERBUFFERBUFFER";
absl::crc32c_t crc{};
Expand All @@ -183,7 +183,7 @@ TEST(SecretDataWithCrcBytesField, SerializeEmpty) {
TEST(SecretDataWithCrcBytesField, SerializeRequiresCrc) {
SecretDataWithCrcField<ParsedStruct> field(1, &ParsedStruct::secret_with_crc);
ParsedStruct s;
s.secret_with_crc = SecretDataWithCrc("");
s.secret_with_crc = SecretDataWithCrc::WithComputedCrc("");

std::string buffer = "BUFFERBUFFERBUFFER";
SerializationState state = SerializationState(absl::MakeSpan(buffer));
Expand All @@ -194,7 +194,7 @@ TEST(SecretDataWithCrcBytesField, SerializeNonEmpty) {
SecretDataWithCrcField<ParsedStruct> field(1, &ParsedStruct::secret_with_crc);
ParsedStruct s;
std::string text = "this is some text";
s.secret_with_crc = SecretDataWithCrc(text);
s.secret_with_crc = SecretDataWithCrc::WithComputedCrc(text);

std::string buffer = "BUFFERBUFFERBUFFERBUFFER";
absl::crc32c_t crc{};
Expand Down Expand Up @@ -236,7 +236,7 @@ TEST(SecretDataWithCrcBytesField, SerializeTooSmallBuffer) {
SecretDataWithCrcField<ParsedStruct> field(1, &ParsedStruct::secret_with_crc);
ParsedStruct s;
std::string text = "this is some text";
s.secret_with_crc = SecretDataWithCrc(text);
s.secret_with_crc = SecretDataWithCrc::WithComputedCrc(text);

std::string buffer = "BUFFERBUFFERBUFFE";
absl::crc32c_t crc{};
Expand All @@ -249,7 +249,7 @@ TEST(SecretDataWithCrcBytesField, SerializeMuchTooSmallBuffer) {
SecretDataWithCrcField<ParsedStruct> field(1, &ParsedStruct::secret_with_crc);
ParsedStruct s;
std::string text = "this is some text";
s.secret_with_crc = SecretDataWithCrc(text);
s.secret_with_crc = SecretDataWithCrc::WithComputedCrc(text);

std::string buffer = "";
absl::crc32c_t crc{};
Expand All @@ -263,7 +263,7 @@ TEST(SecretDataWithCrcBytesField, ExistingCrcIsExtended) {
SecretDataWithCrcField<ParsedStruct> field(1, &ParsedStruct::secret_with_crc);
ParsedStruct s;
std::string text = "this is some text";
s.secret_with_crc = SecretDataWithCrc(text);
s.secret_with_crc = SecretDataWithCrc::WithComputedCrc(text);

std::string buffer = "BUFFERBUFFERBUFFERBUFFER";
absl::crc32c_t crc = absl::ComputeCrc32c("existing");
Expand All @@ -281,9 +281,9 @@ TEST(SecretDataWithCrcBytesField, ExistingCrcIsExtended) {
TEST(SecretDataWithCrcBytesField, RequiresSerialization) {
SecretDataWithCrcField<ParsedStruct> field(1, &ParsedStruct::secret_with_crc);
ParsedStruct s;
s.secret_with_crc = SecretDataWithCrc("");
s.secret_with_crc = SecretDataWithCrc::WithComputedCrc("");
EXPECT_THAT(field.RequiresSerialization(s), IsFalse());
s.secret_with_crc = SecretDataWithCrc("this is some text");
s.secret_with_crc = SecretDataWithCrc::WithComputedCrc("this is some text");
EXPECT_THAT(field.RequiresSerialization(s), IsTrue());
}

Expand Down
12 changes: 7 additions & 5 deletions tink/internal/proto_parser_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,8 @@ TEST(ProtoParserTest, SerializeSecredDataFieldAlwaysSerializeWorks) {

TEST(ProtoParserTest, SingleBytesFieldSecretDataWithCrcSerializingWorks) {
ParsedStruct parsed_struct;
parsed_struct.secret_data_with_crc_member_1 = SecretDataWithCrc("some text");
parsed_struct.secret_data_with_crc_member_1 =
SecretDataWithCrc::WithComputedCrc("some text");

absl::StatusOr<ProtoParser<ParsedStruct>> parser =
ProtoParserBuilder<ParsedStruct>()
Expand All @@ -941,9 +942,10 @@ TEST(ProtoParserTest, SingleBytesFieldSecretDataWithCrcSerializingWorks) {

TEST(ProtoParserTest, TwoBytesFieldSecretDataWithCrcSerializingWorks) {
ParsedStruct parsed_struct;
parsed_struct.secret_data_with_crc_member_1 = SecretDataWithCrc("some text");
parsed_struct.secret_data_with_crc_member_1 =
SecretDataWithCrc::WithComputedCrc("some text");
parsed_struct.secret_data_with_crc_member_2 =
SecretDataWithCrc("another text");
SecretDataWithCrc::WithComputedCrc("another text");

absl::StatusOr<ProtoParser<ParsedStruct>> parser =
ProtoParserBuilder<ParsedStruct>()
Expand Down Expand Up @@ -997,9 +999,9 @@ TEST(ProtoParserTest, CrcOfInnerFieldSerializationWorks) {
std::string text1 = "something";
std::string text2 = "anything, does not matter";
parsed_struct.inner_member_1.secret_data_with_crc_member_1 =
SecretDataWithCrc(text1);
SecretDataWithCrc::WithComputedCrc(text1);
parsed_struct.inner_member_2.secret_data_with_crc_member_1 =
SecretDataWithCrc(text2);
SecretDataWithCrc::WithComputedCrc(text2);

absl::StatusOr<ProtoParser<ParsedStruct>> parser =
ProtoParserBuilder<ParsedStruct>()
Expand Down

0 comments on commit bbf2c95

Please sign in to comment.