Skip to content

Commit

Permalink
Return const& from SerializedKeyProto() to improve internal usability.
Browse files Browse the repository at this point in the history
This should enable secrets to be assigned to string views.

PiperOrigin-RevId: 595692793
Change-Id: Ibad0cc957023e894df5e6c3df35d53f144346483
  • Loading branch information
willinois authored and copybara-github committed Jan 4, 2024
1 parent ae4830d commit 6907937
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions tink/internal/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,7 @@ cc_test(
"//tink/util:statusor",
"//tink/util:test_matchers",
"@com_google_absl//absl/status",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/types:optional",
"@com_google_googletest//:gtest_main",
],
Expand Down
1 change: 1 addition & 0 deletions tink/internal/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,7 @@ tink_cc_test(
tink::internal::proto_key_serialization
gmock
absl::status
absl::strings
absl::optional
tink::core::insecure_secret_key_access
tink::core::restricted_data
Expand Down
2 changes: 1 addition & 1 deletion tink/internal/proto_key_serialization.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class ProtoKeySerialization : public Serialization {
}

// Returned value is only valid for the lifetime of this object.
RestrictedData SerializedKeyProto() const { return serialized_key_; }
const RestrictedData& SerializedKeyProto() const { return serialized_key_; }

google::crypto::tink::KeyData::KeyMaterialType KeyMaterialType() const {
return key_material_type_;
Expand Down
19 changes: 19 additions & 0 deletions tink/internal/proto_key_serialization_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/status/status.h"
#include "absl/strings/string_view.h"
#include "absl/types/optional.h"
#include "tink/insecure_secret_key_access.h"
#include "tink/restricted_data.h"
Expand Down Expand Up @@ -208,6 +209,24 @@ TEST_F(ProtoKeySerializationTest, IdRequirementNotEqual) {
EXPECT_THAT(Equals(*serialization, *other_serialization), IsFalse());
}

TEST_F(ProtoKeySerializationTest, AssignSecretToStringView) {
RestrictedData serialized_key =
RestrictedData("secret", InsecureSecretKeyAccess::Get());
util::StatusOr<ProtoKeySerialization> serialization =
ProtoKeySerialization::Create("type_url", serialized_key,
KeyData::SYMMETRIC, OutputPrefixType::TINK,
/*id_requirement=*/12345);
ASSERT_THAT(serialization.status(), IsOk());
ASSERT_THAT(serialization->SerializedKeyProto(), Eq(serialized_key));
ASSERT_THAT(serialization->SerializedKeyProto().GetSecret(
InsecureSecretKeyAccess::Get()),
Eq("secret"));

absl::string_view secret = serialization->SerializedKeyProto().GetSecret(
InsecureSecretKeyAccess::Get());
EXPECT_THAT(secret, Eq("secret"));
}

} // namespace internal
} // namespace tink
} // namespace crypto

0 comments on commit 6907937

Please sign in to comment.