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 <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 static MockSecretValue* stored_password_mock_ptr_; | |
vabr (Chromium)
2016/05/30 15:05:37
nit: This needs to be declared after all the metho
vabr (Chromium)
2016/05/30 15:05:38
nit: Please add a comment saying that this pointer
cfroussios
2016/05/30 16:19:41
Done.
cfroussios
2016/05/30 16:19:41
Done.
| |
31 | |
32 // These methods are used to redirect calls through LibsecretLoader | |
33 static const gchar* mock_secret_value_get_text(MockSecretValue* value); | |
34 | |
35 static gboolean mock_secret_password_store_sync(const SecretSchema* schema, | |
36 const gchar* collection, | |
37 const gchar* label, | |
38 const gchar* password, | |
39 GCancellable* cancellable, | |
40 GError** error, | |
41 ...); | |
42 | |
43 static MockSecretValue* mock_secret_service_lookup_sync( | |
44 SecretService* service, | |
45 const SecretSchema* schema, | |
46 GHashTable* attributes, | |
47 GCancellable* cancellable, | |
48 GError** error); | |
49 | |
50 static void mock_secret_value_unref(gpointer value); | |
51 | |
52 static GList* mock_secret_service_search_sync(SecretService* service, | |
53 const SecretSchema* schema, | |
54 GHashTable* attributes, | |
55 SecretSearchFlags flags, | |
56 GCancellable* cancellable, | |
57 GError** error); | |
58 }; | |
59 | |
60 MockSecretValue* MockLibsecretLoader::stored_password_mock_ptr_ = nullptr; | |
61 | |
62 const gchar* MockLibsecretLoader::mock_secret_value_get_text( | |
63 MockSecretValue* value) { | |
64 return value->c_str(); | |
65 } | |
66 | |
67 gboolean MockLibsecretLoader::mock_secret_password_store_sync( | |
vabr (Chromium)
2016/05/30 15:05:38
nit: // static
| |
68 const SecretSchema* schema, | |
69 const gchar* collection, | |
70 const gchar* label, | |
71 const gchar* password, | |
72 GCancellable* cancellable, | |
73 GError** error, | |
74 ...) { | |
75 delete stored_password_mock_ptr_; | |
76 stored_password_mock_ptr_ = new MockSecretValue(password); | |
77 return true; | |
78 } | |
79 | |
80 MockSecretValue* MockLibsecretLoader::mock_secret_service_lookup_sync( | |
vabr (Chromium)
2016/05/30 15:05:38
nit:
// static
| |
81 SecretService* service, | |
82 const SecretSchema* schema, | |
83 GHashTable* attributes, | |
84 GCancellable* cancellable, | |
85 GError** error) { | |
86 return stored_password_mock_ptr_; | |
87 } | |
88 | |
89 void MockLibsecretLoader::mock_secret_value_unref(gpointer value) {} | |
90 | |
91 GList* MockLibsecretLoader::mock_secret_service_search_sync( | |
92 SecretService* service, | |
93 const SecretSchema* schema, | |
94 GHashTable* attributes, | |
95 SecretSearchFlags flags, | |
96 GCancellable* cancellable, | |
97 GError** error) { | |
98 *error = nullptr; | |
99 return nullptr; | |
100 } | |
101 | |
102 // static | |
103 bool MockLibsecretLoader::ResetForOSCrypt() { | |
104 // 4 methods used by KeyStorageLibsecret::GetKey() | |
105 secret_password_store_sync = | |
106 &MockLibsecretLoader::mock_secret_password_store_sync; | |
107 secret_value_get_text = (decltype(&::secret_value_get_text)) & | |
108 MockLibsecretLoader::mock_secret_value_get_text; | |
109 secret_value_unref = &MockLibsecretLoader::mock_secret_value_unref; | |
110 secret_service_lookup_sync = | |
111 (decltype(&::secret_service_lookup_sync)) & | |
112 MockLibsecretLoader::mock_secret_service_lookup_sync; | |
113 // 1 method used by LibsecretLoader::EnsureLibsecretLoaded() | |
114 secret_service_search_sync = | |
115 &MockLibsecretLoader::mock_secret_service_search_sync; | |
116 | |
117 delete stored_password_mock_ptr_; | |
118 stored_password_mock_ptr_ = nullptr; | |
119 libsecret_loaded_ = true; | |
120 | |
121 return true; | |
122 } | |
123 | |
124 void MockLibsecretLoader::SetOSCryptPassword(const char* value) { | |
125 delete stored_password_mock_ptr_; | |
126 stored_password_mock_ptr_ = new MockSecretValue(value); | |
127 } | |
128 | |
129 void MockLibsecretLoader::TearDown() { | |
130 delete stored_password_mock_ptr_; | |
131 stored_password_mock_ptr_ = nullptr; | |
132 libsecret_loaded_ = | |
133 false; // Function pointers will be restored when loading. | |
134 } | |
135 | |
136 class LibsecretTest : public testing::Test { | |
137 public: | |
138 LibsecretTest() = default; | |
139 ~LibsecretTest() override = default; | |
140 | |
141 void SetUp() override { MockLibsecretLoader::ResetForOSCrypt(); } | |
142 | |
143 void TearDown() override { MockLibsecretLoader::TearDown(); } | |
144 | |
145 private: | |
146 DISALLOW_COPY_AND_ASSIGN(LibsecretTest); | |
147 }; | |
148 | |
149 TEST_F(LibsecretTest, LibsecretRepeats) { | |
150 KeyStorageLibsecret libsecret; | |
151 MockLibsecretLoader::ResetForOSCrypt(); | |
152 std::string password = libsecret.GetKey(); | |
153 EXPECT_FALSE(password.empty()); | |
154 std::string password_repeat = libsecret.GetKey(); | |
155 EXPECT_EQ(password, password_repeat); | |
156 } | |
157 | |
158 TEST_F(LibsecretTest, LibsecretCreatesRandomised) { | |
159 KeyStorageLibsecret libsecret; | |
160 MockLibsecretLoader::ResetForOSCrypt(); | |
161 std::string password = libsecret.GetKey(); | |
162 MockLibsecretLoader::ResetForOSCrypt(); | |
163 std::string password_new = libsecret.GetKey(); | |
164 EXPECT_NE(password, password_new); | |
165 } | |
166 | |
167 } // namespace | |
OLD | NEW |