Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(535)

Side by Side Diff: components/os_crypt/os_crypt_util_linux_unittest.cc

Issue 1973483002: OSCrypt for POSIX uses libsecret to store a randomised encryption key. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Recommendations Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 <memory>
6 #include <string>
7 #include <utility>
8
9 #include "base/logging.h"
10 #include "base/macros.h"
11 #include "components/os_crypt/key_storage_libsecret.h"
12 #include "components/os_crypt/key_storage_linux.h"
13 #include "components/os_crypt/libsecret_util_linux.h"
14 #include "components/os_crypt/os_crypt.h"
15 #include "components/os_crypt/os_crypt_mocker_linux.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17
18 namespace {
19
20 using MockSecretValue = std::string;
vabr (Chromium) 2016/05/23 12:38:33 Why are we renaming std::string? Unless there is a
cfroussios 2016/05/30 11:50:17 The MockSecretValue maintains symmetry between lib
vabr (Chromium) 2016/05/30 15:05:37 I'm afraid I don't understand what you mean by "ma
cfroussios 2016/05/30 16:19:41 By symmetry I mean calling out the type being mock
vabr (Chromium) 2016/05/30 18:00:47 Thanks, this makes great sense to me now. I agree
cfroussios 2016/05/31 10:56:11 Done.
21
22 // Replaces some of LibsecretLoader's methods with mocked ones.
23 class MockLibsecretLoader : public LibsecretLoader {
24 public:
25 // Sets up the minimum mock implementation necessary for OSCrypt to work
26 // with Libsecret. Also resets the state to mock a clean database.
27 static bool ResetForOSCrypt();
28
29 // Sets OSCrypt's password in the libsecret mock to a specific value
30 static void SetOSCryptPassword(const char*);
31
32 // Releases memory and restores LibsecretLoader to an uninitialized state.
33 static void TearDown();
34
35 private:
36 static MockSecretValue* stored_password_mock_ptr_;
vabr (Chromium) 2016/05/23 12:38:33 Why does this need to be static? It is only reset
cfroussios 2016/05/30 11:50:17 mock_secret_password_store_sync needs to be able t
vabr (Chromium) 2016/05/30 15:05:37 Acknowledged.
37
38 // These methods are used to redirect calls through LibsecretLoader
39 static const gchar* mock_secret_value_get_text(MockSecretValue* value);
40
41 static gboolean mock_secret_password_store_sync(const SecretSchema* schema,
42 const gchar* collection,
43 const gchar* label,
44 const gchar* password,
45 GCancellable* cancellable,
46 GError** error,
47 ...);
48
49 static MockSecretValue* mock_secret_service_lookup_sync(
50 SecretService* service,
51 const SecretSchema* schema,
52 GHashTable* attributes,
53 GCancellable* cancellable,
54 GError** error);
55
56 static void mock_secret_value_unref(gpointer value);
57
58 static GList* mock_secret_service_search_sync(SecretService* service,
59 const SecretSchema* schema,
60 GHashTable* attributes,
61 SecretSearchFlags flags,
62 GCancellable* cancellable,
63 GError** error);
64 };
65
66 MockSecretValue* MockLibsecretLoader::stored_password_mock_ptr_ = nullptr;
67
68 const gchar* MockLibsecretLoader::mock_secret_value_get_text(
69 MockSecretValue* value) {
70 return value->c_str();
71 }
72
73 gboolean MockLibsecretLoader::mock_secret_password_store_sync(
74 const SecretSchema* schema,
75 const gchar* collection,
76 const gchar* label,
77 const gchar* password,
78 GCancellable* cancellable,
79 GError** error,
80 ...) {
81 delete stored_password_mock_ptr_;
82 stored_password_mock_ptr_ = new MockSecretValue(password);
83 return true;
84 }
85
86 MockSecretValue* MockLibsecretLoader::mock_secret_service_lookup_sync(
87 SecretService* service,
88 const SecretSchema* schema,
89 GHashTable* attributes,
90 GCancellable* cancellable,
91 GError** error) {
92 return stored_password_mock_ptr_;
93 }
94
95 void MockLibsecretLoader::mock_secret_value_unref(gpointer value) {}
96
97 GList* MockLibsecretLoader::mock_secret_service_search_sync(
98 SecretService* service,
99 const SecretSchema* schema,
100 GHashTable* attributes,
101 SecretSearchFlags flags,
102 GCancellable* cancellable,
103 GError** error) {
104 *error = nullptr;
105 return nullptr;
106 }
107
108 // static
109 bool MockLibsecretLoader::ResetForOSCrypt() {
110 // 4 methods used by KeyStorageLibsecret::GetKey()
111 secret_password_store_sync =
112 &MockLibsecretLoader::mock_secret_password_store_sync;
113 secret_value_get_text = (decltype(&::secret_value_get_text)) &
114 MockLibsecretLoader::mock_secret_value_get_text;
115 secret_value_unref = &MockLibsecretLoader::mock_secret_value_unref;
116 secret_service_lookup_sync =
117 (decltype(&::secret_service_lookup_sync)) &
118 MockLibsecretLoader::mock_secret_service_lookup_sync;
119 // 1 method used by LibsecretLoader::EnsureLibsecretLoaded()
120 secret_service_search_sync =
121 &MockLibsecretLoader::mock_secret_service_search_sync;
122
123 delete stored_password_mock_ptr_;
124 stored_password_mock_ptr_ = nullptr;
125 libsecret_loaded_ = true;
126
127 return true;
128 }
129
130 void MockLibsecretLoader::SetOSCryptPassword(const char* value) {
131 delete stored_password_mock_ptr_;
132 stored_password_mock_ptr_ = new MockSecretValue(value);
133 }
134
135 void MockLibsecretLoader::TearDown() {
136 delete stored_password_mock_ptr_;
137 stored_password_mock_ptr_ = nullptr;
138 libsecret_loaded_ = false; // function pointers will be restored when loading
vabr (Chromium) 2016/05/23 12:38:33 nit: Start a sentence with a capital letter and en
cfroussios 2016/05/30 11:50:17 Done.
139 }
140
141 class OSCryptLinuxTest : public testing::Test {
vabr (Chromium) 2016/05/23 12:38:33 Could all the OSCryptLinuxTests be in a separate f
cfroussios 2016/05/30 11:50:17 Done.
142 public:
143 OSCryptLinuxTest() = default;
144 ~OSCryptLinuxTest() override = default;
145
146 void SetUp() override {
147 OSCryptMockerLinux::SetUpWithSingleton();
148 key_storage_ = OSCryptMockerLinux::GetInstance();
149 }
150
151 void TearDown() override { OSCryptMockerLinux::TearDown(); }
152
153 protected:
154 OSCryptMockerLinux* key_storage_ = nullptr;
155
156 private:
157 DISALLOW_COPY_AND_ASSIGN(OSCryptLinuxTest);
158 };
159
160 TEST_F(OSCryptLinuxTest, VerifyV0) {
161 const std::string originaltext = "hello";
162 std::string ciphertext;
163 std::string decipheredtext;
164
165 key_storage_->ResetTo("");
166 ciphertext = originaltext; // No encryption
167 ASSERT_TRUE(OSCrypt::DecryptString(ciphertext, &decipheredtext));
168 ASSERT_EQ(originaltext, decipheredtext);
169 }
170
171 TEST_F(OSCryptLinuxTest, VerifyV10) {
172 const std::string originaltext = "hello";
173 std::string ciphertext;
174 std::string decipheredtext;
175
176 key_storage_->ResetTo("peanuts");
177 ASSERT_TRUE(OSCrypt::EncryptString(originaltext, &ciphertext));
178 key_storage_->ResetTo("not_peanuts");
179 ciphertext = ciphertext.substr(3).insert(0, "v10");
180 ASSERT_TRUE(OSCrypt::DecryptString(ciphertext, &decipheredtext));
181 ASSERT_EQ(originaltext, decipheredtext);
182 }
183
184 TEST_F(OSCryptLinuxTest, VerifyV11) {
185 const std::string originaltext = "hello";
186 std::string ciphertext;
187 std::string decipheredtext;
188
189 key_storage_->ResetTo("");
190 ASSERT_TRUE(OSCrypt::EncryptString(originaltext, &ciphertext));
191 ASSERT_EQ(ciphertext.substr(0, 3), "v11");
192 ASSERT_TRUE(OSCrypt::DecryptString(ciphertext, &decipheredtext));
193 ASSERT_EQ(originaltext, decipheredtext);
194 }
195
196 class LibsecretTest : public testing::Test {
197 public:
198 LibsecretTest() = default;
199 ~LibsecretTest() override = default;
200
201 void SetUp() override { MockLibsecretLoader::ResetForOSCrypt(); }
202
203 void TearDown() override { MockLibsecretLoader::TearDown(); }
204
205 private:
206 DISALLOW_COPY_AND_ASSIGN(LibsecretTest);
207 };
208
209 TEST_F(LibsecretTest, LibsecretRepeats) {
210 KeyStorageLibsecret libsecret;
211 MockLibsecretLoader::ResetForOSCrypt();
212 std::string password = libsecret.GetKey();
213 EXPECT_FALSE(password.empty());
214 std::string password_repeat = libsecret.GetKey();
215 EXPECT_EQ(password, password_repeat);
216 }
217
218 TEST_F(LibsecretTest, LibsecretCreatesRandomised) {
219 KeyStorageLibsecret libsecret;
220 MockLibsecretLoader::ResetForOSCrypt();
221 std::string password = libsecret.GetKey();
222 MockLibsecretLoader::ResetForOSCrypt();
223 std::string password_new = libsecret.GetKey();
224 EXPECT_NE(password, password_new);
225 }
226
227 } // namespace
OLDNEW
« components/os_crypt/os_crypt_mocker_linux.cc ('K') | « components/os_crypt/os_crypt_unittest.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698