Cleanup OSCrypt after initialisation
OSCrypt only needs to read from the backend during its initialisation.
Instances created for this initialisation can be cleaned up afterwards.
To support this change, the mocking of OSCrypt can no longer be based
on a singleton. Tests using the mocking mechanism are updated.
Bug: 709096
Change-Id: Iba5b37ad45ada11b9a217dc2c388313f2371f647
Reviewed-on: https://chromium-review.googlesource.com/571221
Reviewed-by: Roger Tawa <[email protected]>
Reviewed-by: Mathieu Perreault <[email protected]>
Reviewed-by: Martin Šrámek <[email protected]>
Reviewed-by: Pavel Yatsuk <[email protected]>
Reviewed-by: Scott Violet <[email protected]>
Reviewed-by: Vasilii Sukhanov <[email protected]>
Commit-Queue: Christos Froussios <[email protected]>
Cr-Commit-Position: refs/heads/master@{#488675}
diff --git a/components/os_crypt/os_crypt_linux_unittest.cc b/components/os_crypt/os_crypt_linux_unittest.cc
index dce4c4a2..58d6da38 100644
--- a/components/os_crypt/os_crypt_linux_unittest.cc
+++ b/components/os_crypt/os_crypt_linux_unittest.cc
@@ -12,35 +12,45 @@
namespace {
-KeyStorageLinux* GetNullKeyStorage() {
+std::unique_ptr<KeyStorageLinux> GetNullKeyStorage() {
return nullptr;
}
class OSCryptLinuxTest : public testing::Test {
public:
- OSCryptLinuxTest() = default;
- ~OSCryptLinuxTest() override = default;
+ OSCryptLinuxTest() : key_("something") { key_ptr_ = &key_; }
+
+ ~OSCryptLinuxTest() override { key_ptr_ = nullptr; };
void SetUp() override {
- OSCryptMockerLinux::SetUpWithSingleton();
- key_storage_ = OSCryptMockerLinux::GetInstance();
+ OSCryptMockerLinux::SetUp();
+ UseMockKeyStorageForTesting(nullptr, OSCryptLinuxTest::GetKey);
}
void TearDown() override { OSCryptMockerLinux::TearDown(); }
protected:
- OSCryptMockerLinux* key_storage_ = nullptr;
+ void SetEncryptionKey(const std::string& key) { key_ = key; }
+
+ // Get the key of the currently running test.
+ static std::string* GetKey() { return key_ptr_; }
private:
+ std::string key_;
+ // Points to the |key_| of the currently running test.
+ static std::string* key_ptr_;
+
DISALLOW_COPY_AND_ASSIGN(OSCryptLinuxTest);
};
+std::string* OSCryptLinuxTest::key_ptr_;
+
TEST_F(OSCryptLinuxTest, VerifyV0) {
const std::string originaltext = "hello";
std::string ciphertext;
std::string decipheredtext;
- key_storage_->ResetTo("");
+ SetEncryptionKey(std::string());
ciphertext = originaltext; // No encryption
ASSERT_TRUE(OSCrypt::DecryptString(ciphertext, &decipheredtext));
ASSERT_EQ(originaltext, decipheredtext);
@@ -51,9 +61,9 @@
std::string ciphertext;
std::string decipheredtext;
- key_storage_->ResetTo("peanuts");
+ SetEncryptionKey("peanuts");
ASSERT_TRUE(OSCrypt::EncryptString(originaltext, &ciphertext));
- key_storage_->ResetTo("not_peanuts");
+ SetEncryptionKey("not_peanuts");
ciphertext = ciphertext.substr(3).insert(0, "v10");
ASSERT_TRUE(OSCrypt::DecryptString(ciphertext, &decipheredtext));
ASSERT_EQ(originaltext, decipheredtext);
@@ -64,7 +74,7 @@
std::string ciphertext;
std::string decipheredtext;
- key_storage_->ResetTo("");
+ SetEncryptionKey(std::string());
ASSERT_TRUE(OSCrypt::EncryptString(originaltext, &ciphertext));
ASSERT_EQ(ciphertext.substr(0, 3), "v11");
ASSERT_TRUE(OSCrypt::DecryptString(ciphertext, &decipheredtext));