Skip to content

Commit

Permalink
Change the test of SecretDataWithCrcField by testing SerializeWithTag…
Browse files Browse the repository at this point in the history
…Into and GetSerializedSizeIncludingTag, instead of SerializeInto and GetSerializedSize.

We make GetSerializedSize and SerializeInto protected to ensure we have all instances. In upcoming CLs I will inline those.

PiperOrigin-RevId: 705415969
Change-Id: I3aa355a0cfb8713be42a547b1363e6f0c65ccbe0
  • Loading branch information
tholenst authored and copybara-github committed Dec 12, 2024
1 parent 39ea800 commit 87e0932
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 29 deletions.
7 changes: 4 additions & 3 deletions tink/internal/proto_parser_secret_data_with_crc_field.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ class SecretDataWithCrcField : public Field<Struct> {
return (values.*data_).size() > 0;
}

WireType GetWireType() const override { return WireType::kLengthDelimited; }
int GetFieldNumber() const override { return field_number_; }

protected:
absl::Status SerializeInto(SerializationState& serialization_state,
const Struct& values) const override {
if (!serialization_state.HasCrc()) {
Expand Down Expand Up @@ -125,9 +129,6 @@ class SecretDataWithCrcField : public Field<Struct> {
return VarintLength(size) + size;
}

WireType GetWireType() const override { return WireType::kLengthDelimited; }
int GetFieldNumber() const override { return field_number_; }

private:
SecretDataWithCrc Struct::*data_;
int field_number_;
Expand Down
55 changes: 29 additions & 26 deletions tink/internal/proto_parser_secret_data_with_crc_field_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -167,27 +167,27 @@ TEST(SecretDataWithCrcBytesField, ExistingCRCIsExtendedWhenParsing) {
"Existing", HexDecodeOrDie("0a"), "1234567890"))));
}

TEST(SecretDataWithCrcBytesField, SerializeEmpty) {
TEST(SecretDataWithCrcBytesField, SerializeEmptyAlwaysSerialize) {
SecretDataWithCrcField<ParsedStruct> field(1, &ParsedStruct::secret_with_crc);
ParsedStruct s;
s.secret_with_crc = SecretDataWithCrc::WithComputedCrc("");

std::string buffer = "BUFFERBUFFERBUFFER";
absl::crc32c_t crc{};
SerializationState state = SerializationState(absl::MakeSpan(buffer), &crc);
EXPECT_THAT(field.SerializeInto(state, s), IsOk());
EXPECT_THAT(buffer[0], Eq(0));
EXPECT_THAT(crc, Eq(absl::ComputeCrc32c(HexDecodeOrDie("00"))));
EXPECT_THAT(field.SerializeWithTagInto(state, s), IsOk());
EXPECT_THAT(&state.GetBuffer()[0], Eq(&buffer[0]));
EXPECT_THAT(crc, Eq(absl::crc32c_t{}));
}

TEST(SecretDataWithCrcBytesField, SerializeRequiresCrc) {
SecretDataWithCrcField<ParsedStruct> field(1, &ParsedStruct::secret_with_crc);
ParsedStruct s;
s.secret_with_crc = SecretDataWithCrc::WithComputedCrc("");
s.secret_with_crc = SecretDataWithCrc::WithComputedCrc("a");

std::string buffer = "BUFFERBUFFERBUFFER";
SerializationState state = SerializationState(absl::MakeSpan(buffer));
EXPECT_THAT(field.SerializeInto(state, s), Not(IsOk()));
EXPECT_THAT(field.SerializeWithTagInto(state, s), Not(IsOk()));
}

TEST(SecretDataWithCrcBytesField, SerializeNonEmpty) {
Expand All @@ -199,14 +199,15 @@ TEST(SecretDataWithCrcBytesField, SerializeNonEmpty) {
std::string buffer = "BUFFERBUFFERBUFFERBUFFER";
absl::crc32c_t crc{};
SerializationState state = SerializationState(absl::MakeSpan(buffer), &crc);
EXPECT_THAT(field.GetSerializedSize(s), Eq(18));
ASSERT_THAT(field.SerializeInto(state, s), IsOk());
EXPECT_THAT(field.GetSerializedSizeIncludingTag(s), Eq(19));
ASSERT_THAT(field.SerializeWithTagInto(state, s), IsOk());
EXPECT_THAT(state.GetBuffer().size(),
Eq(buffer.size() - field.GetSerializedSize(s)));
EXPECT_THAT(&(state.GetBuffer())[0], Eq(&buffer[field.GetSerializedSize(s)]));
EXPECT_THAT(buffer, Eq(absl::StrCat(HexDecodeOrDie("11"), text, "BUFFER")));
Eq(buffer.size() - field.GetSerializedSizeIncludingTag(s)));
EXPECT_THAT(&(state.GetBuffer())[0],
Eq(&buffer[field.GetSerializedSizeIncludingTag(s)]));
EXPECT_THAT(buffer, Eq(absl::StrCat(HexDecodeOrDie("0a11"), text, "UFFER")));
EXPECT_THAT(
crc, Eq(absl::ComputeCrc32c(absl::StrCat(HexDecodeOrDie("11"), text))));
crc, Eq(absl::ComputeCrc32c(absl::StrCat(HexDecodeOrDie("0a11"), text))));
}

// Tests that when serializing a SecretDataWithCrcField, the resulting CRC
Expand All @@ -223,13 +224,14 @@ TEST(SecretDataWithCrcBytesField, CrcIsComputedFromCrc) {
std::string buffer = "BUFFERBUFFERBUFFERBUFFER";
absl::crc32c_t crc{};
SerializationState state = SerializationState(absl::MakeSpan(buffer), &crc);
EXPECT_THAT(field.GetSerializedSize(s), Eq(18));
ASSERT_THAT(field.SerializeInto(state, s), IsOk());
EXPECT_THAT(field.GetSerializedSizeIncludingTag(s), Eq(19));
ASSERT_THAT(field.SerializeWithTagInto(state, s), IsOk());
EXPECT_THAT(state.GetBuffer().size(),
Eq(buffer.size() - field.GetSerializedSize(s)));
EXPECT_THAT(buffer, Eq(absl::StrCat(HexDecodeOrDie("11"), text1, "BUFFER")));
Eq(buffer.size() - field.GetSerializedSizeIncludingTag(s)));
EXPECT_THAT(buffer, Eq(absl::StrCat(HexDecodeOrDie("0a11"), text1, "UFFER")));
EXPECT_THAT(
crc, Eq(absl::ComputeCrc32c(absl::StrCat(HexDecodeOrDie("11"), text2))));
crc,
Eq(absl::ComputeCrc32c(absl::StrCat(HexDecodeOrDie("0a11"), text2))));
}

TEST(SecretDataWithCrcBytesField, SerializeTooSmallBuffer) {
Expand All @@ -241,7 +243,7 @@ TEST(SecretDataWithCrcBytesField, SerializeTooSmallBuffer) {
std::string buffer = "BUFFERBUFFERBUFFE";
absl::crc32c_t crc{};
SerializationState state = SerializationState(absl::MakeSpan(buffer), &crc);
EXPECT_THAT(field.SerializeInto(state, s), Not(IsOk()));
EXPECT_THAT(field.SerializeWithTagInto(state, s), Not(IsOk()));
}

// The buffer won't even hold the varint.
Expand All @@ -254,7 +256,7 @@ TEST(SecretDataWithCrcBytesField, SerializeMuchTooSmallBuffer) {
std::string buffer = "";
absl::crc32c_t crc{};
SerializationState state = SerializationState(absl::MakeSpan(buffer), &crc);
EXPECT_THAT(field.SerializeInto(state, s), Not(IsOk()));
EXPECT_THAT(field.SerializeWithTagInto(state, s), Not(IsOk()));
}

// Test that when serializing, the existing CRC in the state is extended by
Expand All @@ -268,14 +270,15 @@ TEST(SecretDataWithCrcBytesField, ExistingCrcIsExtended) {
std::string buffer = "BUFFERBUFFERBUFFERBUFFER";
absl::crc32c_t crc = absl::ComputeCrc32c("existing");
SerializationState state = SerializationState(absl::MakeSpan(buffer), &crc);
EXPECT_THAT(field.GetSerializedSize(s), Eq(18));
ASSERT_THAT(field.SerializeInto(state, s), IsOk());
EXPECT_THAT(field.GetSerializedSizeIncludingTag(s), Eq(19));
ASSERT_THAT(field.SerializeWithTagInto(state, s), IsOk());
EXPECT_THAT(state.GetBuffer().size(),
Eq(buffer.size() - field.GetSerializedSize(s)));
EXPECT_THAT(&(state.GetBuffer())[0], Eq(&buffer[field.GetSerializedSize(s)]));
EXPECT_THAT(buffer, Eq(absl::StrCat(HexDecodeOrDie("11"), text, "BUFFER")));
EXPECT_THAT(crc, Eq(absl::ComputeCrc32c(
absl::StrCat("existing", HexDecodeOrDie("11"), text))));
Eq(buffer.size() - field.GetSerializedSizeIncludingTag(s)));
EXPECT_THAT(&(state.GetBuffer())[0],
Eq(&buffer[field.GetSerializedSizeIncludingTag(s)]));
EXPECT_THAT(buffer, Eq(absl::StrCat(HexDecodeOrDie("0a11"), text, "UFFER")));
EXPECT_THAT(crc, Eq(absl::ComputeCrc32c(absl::StrCat(
"existing", HexDecodeOrDie("0a11"), text))));
}

TEST(SecretDataWithCrcBytesField, RequiresSerialization) {
Expand Down

0 comments on commit 87e0932

Please sign in to comment.