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

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, 6 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
« no previous file with comments | « components/os_crypt/os_crypt_mocker_linux.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 <string>
6
7 #include "base/macros.h"
8 #include "components/os_crypt/key_storage_libsecret.h"
9 #include "components/os_crypt/libsecret_util_linux.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11
12 namespace {
13
14 // Mock functions use MockSecretValue, where SecretValue would appear, and are
15 // cast to the correct signature. We can reduce SecretValue to an std::string,
16 // because we don't use anything else from it.
17 using MockSecretValue = std::string;
18
19 // Replaces some of LibsecretLoader's methods with mocked ones.
20 class MockLibsecretLoader : public LibsecretLoader {
21 public:
22 // Sets up the minimum mock implementation necessary for OSCrypt to work
23 // with Libsecret. Also resets the state to mock a clean database.
24 static bool ResetForOSCrypt();
25
26 // Sets OSCrypt's password in the libsecret mock to a specific value
27 static void SetOSCryptPassword(const char*);
28
29 // Releases memory and restores LibsecretLoader to an uninitialized state.
30 static void TearDown();
31
32 private:
33 // These methods are used to redirect calls through LibsecretLoader
34 static const gchar* mock_secret_value_get_text(MockSecretValue* value);
35
36 static gboolean mock_secret_password_store_sync(const SecretSchema* schema,
37 const gchar* collection,
38 const gchar* label,
39 const gchar* password,
40 GCancellable* cancellable,
41 GError** error,
42 ...);
43
44 static MockSecretValue* mock_secret_service_lookup_sync(
45 SecretService* service,
46 const SecretSchema* schema,
47 GHashTable* attributes,
48 GCancellable* cancellable,
49 GError** error);
50
51 static void mock_secret_value_unref(gpointer value);
52
53 static GList* mock_secret_service_search_sync(SecretService* service,
54 const SecretSchema* schema,
55 GHashTable* attributes,
56 SecretSearchFlags flags,
57 GCancellable* cancellable,
58 GError** error);
59
60 // MockLibsecretLoader owns this object.
61 static MockSecretValue* stored_password_mock_ptr_;
62 };
63
64 MockSecretValue* MockLibsecretLoader::stored_password_mock_ptr_ = nullptr;
65
66 const gchar* MockLibsecretLoader::mock_secret_value_get_text(
67 MockSecretValue* value) {
68 return value->c_str();
69 }
70
71 // static
72 gboolean MockLibsecretLoader::mock_secret_password_store_sync(
73 const SecretSchema* schema,
74 const gchar* collection,
75 const gchar* label,
76 const gchar* password,
77 GCancellable* cancellable,
78 GError** error,
79 ...) {
80 delete stored_password_mock_ptr_;
81 stored_password_mock_ptr_ = new MockSecretValue(password);
82 return true;
83 }
84
85 // static
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 // static
96 void MockLibsecretLoader::mock_secret_value_unref(gpointer value) {}
97
98 // static
99 GList* MockLibsecretLoader::mock_secret_service_search_sync(
100 SecretService* service,
101 const SecretSchema* schema,
102 GHashTable* attributes,
103 SecretSearchFlags flags,
104 GCancellable* cancellable,
105 GError** error) {
106 *error = nullptr;
107 return nullptr;
108 }
109
110 // static
111 bool MockLibsecretLoader::ResetForOSCrypt() {
112 // 4 methods used by KeyStorageLibsecret::GetKey()
113 secret_password_store_sync =
114 &MockLibsecretLoader::mock_secret_password_store_sync;
115 secret_value_get_text = (decltype(&::secret_value_get_text)) &
116 MockLibsecretLoader::mock_secret_value_get_text;
117 secret_value_unref = &MockLibsecretLoader::mock_secret_value_unref;
118 secret_service_lookup_sync =
119 (decltype(&::secret_service_lookup_sync)) &
120 MockLibsecretLoader::mock_secret_service_lookup_sync;
121 // 1 method used by LibsecretLoader::EnsureLibsecretLoaded()
122 secret_service_search_sync =
123 &MockLibsecretLoader::mock_secret_service_search_sync;
124
125 delete stored_password_mock_ptr_;
126 stored_password_mock_ptr_ = nullptr;
127 libsecret_loaded_ = true;
128
129 return true;
130 }
131
132 // static
133 void MockLibsecretLoader::SetOSCryptPassword(const char* value) {
134 delete stored_password_mock_ptr_;
135 stored_password_mock_ptr_ = new MockSecretValue(value);
136 }
137
138 // static
139 void MockLibsecretLoader::TearDown() {
140 delete stored_password_mock_ptr_;
141 stored_password_mock_ptr_ = nullptr;
142 libsecret_loaded_ =
143 false; // Function pointers will be restored when loading.
144 }
145
146 class LibsecretTest : public testing::Test {
147 public:
148 LibsecretTest() = default;
149 ~LibsecretTest() override = default;
150
151 void SetUp() override { MockLibsecretLoader::ResetForOSCrypt(); }
152
153 void TearDown() override { MockLibsecretLoader::TearDown(); }
154
155 private:
156 DISALLOW_COPY_AND_ASSIGN(LibsecretTest);
157 };
158
159 TEST_F(LibsecretTest, LibsecretRepeats) {
160 KeyStorageLibsecret libsecret;
161 MockLibsecretLoader::ResetForOSCrypt();
162 std::string password = libsecret.GetKey();
163 EXPECT_FALSE(password.empty());
164 std::string password_repeat = libsecret.GetKey();
165 EXPECT_EQ(password, password_repeat);
166 }
167
168 TEST_F(LibsecretTest, LibsecretCreatesRandomised) {
169 KeyStorageLibsecret libsecret;
170 MockLibsecretLoader::ResetForOSCrypt();
171 std::string password = libsecret.GetKey();
172 MockLibsecretLoader::ResetForOSCrypt();
173 std::string password_new = libsecret.GetKey();
174 EXPECT_NE(password, password_new);
175 }
176
177 } // namespace
OLDNEW
« no previous file with comments | « components/os_crypt/os_crypt_mocker_linux.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698