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

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