Skip to content

Commit

Permalink
Add "AsStringView" which does the same as UncheckedData.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 697595344
Change-Id: Iacd1df454a39cbafeb7affbfce8bd23019da81d1
  • Loading branch information
tholenst authored and copybara-github committed Nov 18, 2024
1 parent c681f61 commit e1c0ab1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
2 changes: 1 addition & 1 deletion tink/internal/secret_data_with_crc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ SecretDataWithCrc::SecretDataWithCrc(
absl::string_view data, absl::optional<SecretValue<absl::crc32c_t>> crc)
: SecretDataWithCrc(SecretDataFromStringView(data), std::move(crc)) {}

absl::string_view SecretDataWithCrc::UncheckedData() const {
absl::string_view SecretDataWithCrc::AsStringView() const {
return SecretDataAsStringView(data_);
}

Expand Down
8 changes: 7 additions & 1 deletion tink/internal/secret_data_with_crc.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,13 @@ class SecretDataWithCrc final {
absl::StatusOr<absl::string_view> data() const;

// Returns the data without verifying the CRC32C.
absl::string_view UncheckedData() const;
// Use AsStringView instead.
absl::string_view UncheckedData() const {
return AsStringView();
}

// Returns the data without verifying the CRC32C.
absl::string_view AsStringView() const;

// Returns the data without verifying the CRC32C. Leaves the object in an
// invalid state.
Expand Down
17 changes: 17 additions & 0 deletions tink/internal/secret_data_with_crc_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,23 @@ TEST(SecretDataWitCrcTest, UncheckedDataWithInvalidCrcSucceeds) {
EXPECT_EQ(data_3.UncheckedData(), data);
}

TEST(SecretDataWitCrcTest, AsStringViewWithInvalidCrcSucceeds) {
std::string data = Random::GetRandomBytes(256);
absl::crc32c_t crc =
absl::crc32c_t{static_cast<uint32_t>(absl::ComputeCrc32c(data)) + 1};

SecretDataWithCrc data_1(data, SecretValue<absl::crc32c_t>(crc));
EXPECT_EQ(data_1.AsStringView(), data);

SecretData secret_data = SecretDataFromStringView(data);
SecretDataWithCrc data_2(secret_data, SecretValue<absl::crc32c_t>(crc));
EXPECT_EQ(data_2.AsStringView(), data);

SecretDataWithCrc data_3(std::move(secret_data),
SecretValue<absl::crc32c_t>(crc));
EXPECT_EQ(data_3.AsStringView(), data);
}

TEST(SecretDataWitCrcTest, CopyConstructor) {
std::string data = Random::GetRandomBytes(256);
absl::crc32c_t crc = absl::ComputeCrc32c(data);
Expand Down

0 comments on commit e1c0ab1

Please sign in to comment.