Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <dlfcn.h> | |
| 6 #include <memory> | |
| 7 #include <string> | |
| 8 #include <utility> | |
| 9 | |
| 10 #include "base/logging.h" | |
| 11 #include "base/macros.h" | |
| 12 #include "components/os_crypt/key_storage_linux.h" | |
| 13 #include "components/os_crypt/libsecret_util_posix.h" | |
| 14 #include "components/os_crypt/os_crypt.h" | |
| 15 #include "testing/gtest/include/gtest/gtest.h" | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 struct MockSecretValue { | |
| 20 public: | |
| 21 std::unique_ptr<std::string> password; | |
| 22 explicit MockSecretValue(const gchar* password); | |
|
vabr (Chromium)
2016/05/13 15:10:19
nit: Please separate the methods from each other a
cfroussios
2016/05/13 17:09:13
Done.
| |
| 23 ~MockSecretValue(); | |
| 24 }; | |
| 25 | |
| 26 // Replaces some of LibsecretLoader's methods with mocked ones. | |
| 27 class MockLibsecretLoader : public LibsecretLoader { | |
| 28 public: | |
| 29 // Sets up the minimum mock implementation necessary for |Libsecret| to work. | |
|
vabr (Chromium)
2016/05/13 15:10:19
nit: Please separate methods with blank lines.
cfroussios
2016/05/13 17:09:13
Done.
| |
| 30 // Also resets the state to mock a clean database. | |
| 31 static bool ResetForOSCrypt(); | |
| 32 // Shorthand for setting OSCrypt's password in the libsecret mock to a | |
| 33 // specific value | |
| 34 static void SetOSCryptPassword(const char*); | |
| 35 }; | |
| 36 | |
| 37 MockSecretValue::MockSecretValue(const gchar* password) | |
| 38 : password(new std::string(password)) {} | |
| 39 MockSecretValue::~MockSecretValue() = default; | |
|
vabr (Chromium)
2016/05/13 15:10:19
nit: Please add a blank line above this one.
cfroussios
2016/05/13 17:09:13
Done.
| |
| 40 | |
| 41 std::unique_ptr<MockSecretValue> stored_password(nullptr); | |
|
vabr (Chromium)
2016/05/13 15:10:19
nit: No need to pass nullptr to a unique_ptr, just
vabr (Chromium)
2016/05/13 15:10:19
No non-POD data types as static variables (see my
vabr (Chromium)
2016/05/13 15:10:19
nit: Please use g_ as a prefix for global variable
cfroussios
2016/05/13 17:09:13
Acknowledged.
| |
| 42 | |
| 43 const gchar* mock_secret_value_get_text(MockSecretValue* value) { | |
| 44 return value->password->c_str(); | |
| 45 } | |
| 46 | |
| 47 gboolean mock_secret_password_store_sync(const SecretSchema* schema, | |
| 48 const gchar* collection, | |
| 49 const gchar* label, | |
| 50 const gchar* password, | |
| 51 GCancellable* cancellable, | |
| 52 GError** error, | |
| 53 ...) { | |
| 54 stored_password.reset(new MockSecretValue(password)); | |
| 55 return true; | |
| 56 } | |
| 57 | |
| 58 MockSecretValue* mock_secret_service_lookup_sync(SecretService* service, | |
| 59 const SecretSchema* schema, | |
| 60 GHashTable* attributes, | |
| 61 GCancellable* cancellable, | |
| 62 GError** error) { | |
| 63 return stored_password.get(); | |
| 64 } | |
| 65 | |
| 66 void mock_secret_value_unref(gpointer value) {} | |
| 67 | |
| 68 GList* mock_secret_service_search_sync(SecretService* service, | |
| 69 const SecretSchema* schema, | |
| 70 GHashTable* attributes, | |
| 71 SecretSearchFlags flags, | |
| 72 GCancellable* cancellable, | |
| 73 GError** error) { | |
| 74 *error = nullptr; | |
| 75 return nullptr; | |
| 76 } | |
| 77 | |
| 78 // static | |
| 79 bool MockLibsecretLoader::ResetForOSCrypt() { | |
| 80 // 4 methods used by Libsecret.GetPassword(); | |
| 81 secret_password_store_sync = &mock_secret_password_store_sync; | |
| 82 secret_value_get_text = | |
| 83 (decltype(&::secret_value_get_text)) & mock_secret_value_get_text; | |
| 84 secret_value_unref = &mock_secret_value_unref; | |
| 85 secret_service_lookup_sync = (decltype(&::secret_service_lookup_sync)) & | |
| 86 mock_secret_service_lookup_sync; | |
| 87 // 1 method used by LibsecretLoader::EnsureLibsecretLoaded | |
| 88 secret_service_search_sync = &mock_secret_service_search_sync; | |
| 89 | |
| 90 stored_password.reset(nullptr); | |
| 91 libsecret_loaded_ = true; | |
| 92 | |
| 93 return true; | |
| 94 } | |
| 95 | |
| 96 void MockLibsecretLoader::SetOSCryptPassword(const char* value) { | |
| 97 stored_password.reset(new MockSecretValue(value)); | |
| 98 } | |
| 99 | |
| 100 TEST(LibsecretTest, VerifyPosixMigrationCoexistance) { | |
| 101 const std::string originaltext = "hello"; | |
| 102 std::string ciphertext; | |
| 103 std::string decipheredtext; | |
| 104 std::string decipheredtextV11; | |
| 105 std::string decipheredtextV10; | |
| 106 | |
| 107 // Verify that there are 3 versions currently supported | |
| 108 | |
| 109 KeyStorageMock* key_storage = OSCrypt::UseMockKeyStorage(true); | |
| 110 | |
| 111 ASSERT_TRUE(OSCrypt::EncryptString(originaltext, &ciphertext)); | |
|
vabr (Chromium)
2016/05/13 15:10:19
The three blocks do not seem to depend on each oth
cfroussios
2016/05/13 17:09:12
Done.
| |
| 112 ASSERT_EQ(ciphertext.substr(0, 3), "v11"); | |
| 113 ASSERT_TRUE(OSCrypt::DecryptString(ciphertext, &decipheredtextV11)); | |
| 114 ASSERT_EQ(originaltext, decipheredtextV11); | |
| 115 | |
| 116 key_storage->ResetTo(std::string("peanuts")); | |
| 117 ASSERT_TRUE(OSCrypt::EncryptString(originaltext, &ciphertext)); | |
| 118 key_storage->ResetTo(std::string("not_peanuts")); | |
| 119 ciphertext = ciphertext.substr(3).insert(0, "v10"); | |
| 120 ASSERT_TRUE(OSCrypt::DecryptString(ciphertext, &decipheredtextV10)); | |
| 121 ASSERT_EQ(originaltext, decipheredtextV10); | |
| 122 | |
| 123 ciphertext = originaltext; // No encryption | |
| 124 ASSERT_TRUE(OSCrypt::DecryptString(ciphertext, &decipheredtext)); | |
| 125 ASSERT_EQ(originaltext, decipheredtext); | |
| 126 | |
| 127 // Don't force a static configuration on other tests | |
| 128 OSCrypt::UseMockKeyStorage(false); | |
|
vabr (Chromium)
2016/05/13 15:10:19
It might be safer to define a test fixture (based
cfroussios
2016/05/13 17:09:12
Done.
| |
| 129 } | |
| 130 | |
| 131 TEST(LibsecretTest, LibsecretRepeats) { | |
| 132 KeyStorageLibsecret libsecret; | |
| 133 MockLibsecretLoader::ResetForOSCrypt(); | |
| 134 std::string password = libsecret.GetKey(); | |
| 135 EXPECT_NE(password, ""); | |
|
vabr (Chromium)
2016/05/13 15:10:19
EXPECT_FALSE(password.empty());
cfroussios
2016/05/13 17:09:12
Done.
| |
| 136 std::string password_repeat = libsecret.GetKey(); | |
| 137 EXPECT_EQ(password, password_repeat); | |
| 138 } | |
| 139 | |
| 140 TEST(LibsecretTest, LibsecretCreatesRandomised) { | |
| 141 KeyStorageLibsecret libsecret; | |
| 142 MockLibsecretLoader::ResetForOSCrypt(); | |
| 143 std::string password = libsecret.GetKey(); | |
| 144 MockLibsecretLoader::ResetForOSCrypt(); | |
| 145 std::string password_new = libsecret.GetKey(); | |
| 146 EXPECT_NE(password, password_new); | |
| 147 } | |
| 148 | |
| 149 } // namespace | |
| OLD | NEW |