File: key_storage_libsecret_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (271 lines) | stat: -rw-r--r-- 9,136 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/390223051): Remove C-library calls to fix the errors.
#pragma allow_unsafe_libc_calls
#endif

#include <string>
#include <unordered_map>

#include "base/lazy_instance.h"
#include "components/os_crypt/sync/key_storage_libsecret.h"
#include "components/os_crypt/sync/libsecret_util_linux.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/glib/scoped_gobject.h"

namespace {

const SecretSchema kKeystoreSchemaV2 = {
    "chrome_libsecret_os_crypt_password_v2",
    SECRET_SCHEMA_DONT_MATCH_NAME,
    {
        {"application", SECRET_SCHEMA_ATTRIBUTE_STRING},
        {nullptr, SECRET_SCHEMA_ATTRIBUTE_STRING},
    }};

// This test mocks C-functions used by Libsecret. In order to present a
// consistent world view, we need a single backing instance that contains all
// the relevant data.
class MockPasswordStore {
  // The functions that interact with the password store expect to return
  // gobjects. These C-style objects are hard to work with. Rather than finagle
  // with the type system, we always use objects with type G_TYPE_OBJECT. We
  // then keep a local map from G_TYPE_OBJECT to std::string, and all relevant
  // translation just look up entries in this map.
 public:
  void Reset() {
    mapping_.clear();
    ClearPassword();
    for (GObject* o : objects_returned_to_caller_) {
      ASSERT_EQ(o->ref_count, 1u);
    }
    objects_returned_to_caller_.clear();
  }

  void ClearPassword() {
    if (password_) {
      ASSERT_EQ((*password_).ref_count, 1u);
      password_ = ScopedGObject<GObject>();  // Reset
    }
  }

  void SetPassword(const std::string& password) {
    ASSERT_FALSE(password_);
    password_ = TakeGObject(
        static_cast<GObject*>(g_object_new(G_TYPE_OBJECT, nullptr)));
    mapping_[password_.get()] = password;
  }

  // The returned object has a ref count of 2. This way, after the client
  // deletes the object, it isn't destroyed, and we can check that all these
  // objects have ref count of 1 at the end of the test.
  GObject* MakeTempObject(const std::string& value) {
    ScopedGObject temp = WrapGObject(
        static_cast<GObject*>(g_object_new(G_TYPE_OBJECT, nullptr)));
    objects_returned_to_caller_.push_back(temp);
    mapping_[temp.get()] = value;
    return temp;
  }

  const gchar* GetString(void* opaque_id) {
    return mapping_[static_cast<GObject*>(opaque_id)].c_str();
  }

  GObject* password() { return password_; }

  // The keys in `mapping_` refer to objects in `objects_returned_to_caller_` or
  // `password_`, which manage their lifetime.
  std::unordered_map<GObject*, std::string> mapping_;
  std::vector<ScopedGObject<GObject>> objects_returned_to_caller_;
  ScopedGObject<GObject> password_;
};
base::LazyInstance<MockPasswordStore>::Leaky g_password_store =
    LAZY_INSTANCE_INITIALIZER;

// Replaces some of LibsecretLoader's methods with mocked ones.
class MockLibsecretLoader : public LibsecretLoader {
 public:
  // Sets up the minimum mock implementation necessary for OSCrypt to work
  // with Libsecret. Also resets the state to mock a clean database.
  static bool ResetForOSCrypt();

  // Sets OSCrypt's password in the libsecret mock to a specific value
  static void SetOSCryptPassword(const char*);

  // Releases memory and restores LibsecretLoader to an uninitialized state.
  static void TearDown();

 private:
  // These methods are used to redirect calls through LibsecretLoader
  static const gchar* mock_secret_value_get_text(SecretValue* value);

  static gboolean mock_secret_password_store_sync(const SecretSchema* schema,
                                                  const gchar* collection,
                                                  const gchar* label,
                                                  const gchar* password,
                                                  GCancellable* cancellable,
                                                  GError** error,
                                                  ...);

  static void mock_secret_value_unref(gpointer value);

  static GList* mock_secret_service_search_sync(SecretService* service,
                                                const SecretSchema* schema,
                                                GHashTable* attributes,
                                                SecretSearchFlags flags,
                                                GCancellable* cancellable,
                                                GError** error);

  static SecretValue* mock_secret_item_get_secret(SecretItem* item);

  static guint64 mock_secret_item_get_created(SecretItem* item);

  static guint64 mock_secret_item_get_modified(SecretItem* item);
};

const gchar* MockLibsecretLoader::mock_secret_value_get_text(
    SecretValue* value) {
  return g_password_store.Pointer()->GetString(value);
}

// static
gboolean MockLibsecretLoader::mock_secret_password_store_sync(
    const SecretSchema* schema,
    const gchar* collection,
    const gchar* label,
    const gchar* password,
    GCancellable* cancellable,
    GError** error,
    ...) {
  // TODO(crbug.com/40490926) We don't read the dummy we store to unlock
  // keyring.
  if (strcmp("_chrome_dummy_schema_for_unlocking", schema->name) == 0) {
    return true;
  }

  EXPECT_STREQ(kKeystoreSchemaV2.name, schema->name);
  g_password_store.Pointer()->SetPassword(password);
  return true;
}

// static
void MockLibsecretLoader::mock_secret_value_unref(gpointer value) {
  g_object_unref(value);
}

// static
GList* MockLibsecretLoader::mock_secret_service_search_sync(
    SecretService* service,
    const SecretSchema* schema,
    GHashTable* attributes,
    SecretSearchFlags flags,
    GCancellable* cancellable,
    GError** error) {
  EXPECT_STREQ(kKeystoreSchemaV2.name, schema->name);

  EXPECT_TRUE(flags & SECRET_SEARCH_UNLOCK);
  EXPECT_TRUE(flags & SECRET_SEARCH_LOAD_SECRETS);

  GObject* item = nullptr;
  MockPasswordStore* store = g_password_store.Pointer();
  GObject* password = store->password();
  if (password) {
    item = store->MakeTempObject(store->GetString(password));
  }

  if (!item) {
    return nullptr;
  }

  GList* result = nullptr;
  result = g_list_append(result, item);
  g_clear_error(error);
  return result;
}

// static
SecretValue* MockLibsecretLoader::mock_secret_item_get_secret(
    SecretItem* item) {
  // Add a ref to make sure that the caller unrefs with secret_value_unref.
  g_object_ref(item);
  return reinterpret_cast<SecretValue*>(item);
}

// static
guint64 MockLibsecretLoader::mock_secret_item_get_created(SecretItem* item) {
  return 0;
}

// static
guint64 MockLibsecretLoader::mock_secret_item_get_modified(SecretItem* item) {
  return 0;
}

// static
bool MockLibsecretLoader::ResetForOSCrypt() {
  // Methods used by KeyStorageLibsecret
  secret_password_store_sync =
      &MockLibsecretLoader::mock_secret_password_store_sync;
  secret_value_get_text = &MockLibsecretLoader::mock_secret_value_get_text;
  secret_value_unref = &MockLibsecretLoader::mock_secret_value_unref;
  secret_service_search_sync =
      &MockLibsecretLoader::mock_secret_service_search_sync;
  secret_item_get_secret = &MockLibsecretLoader::mock_secret_item_get_secret;
  secret_item_get_created = &MockLibsecretLoader::mock_secret_item_get_created;
  secret_item_get_modified =
      &MockLibsecretLoader::mock_secret_item_get_modified;

  g_password_store.Pointer()->Reset();
  libsecret_loaded_ = true;

  return true;
}

// static
void MockLibsecretLoader::TearDown() {
  g_password_store.Pointer()->Reset();
  libsecret_loaded_ =
      false;  // Function pointers will be restored when loading.
}

class LibsecretTest : public testing::Test {
 public:
  LibsecretTest() = default;

  LibsecretTest(const LibsecretTest&) = delete;
  LibsecretTest& operator=(const LibsecretTest&) = delete;

  ~LibsecretTest() override = default;

  void SetUp() override { MockLibsecretLoader::ResetForOSCrypt(); }

  void TearDown() override { MockLibsecretLoader::TearDown(); }
};

TEST_F(LibsecretTest, LibsecretRepeats) {
  KeyStorageLibsecret libsecret("chromium");
  MockLibsecretLoader::ResetForOSCrypt();
  g_password_store.Pointer()->SetPassword("initial password");
  std::optional<std::string> password = libsecret.GetKey();
  EXPECT_TRUE(password.has_value());
  EXPECT_FALSE(password.value().empty());
  std::optional<std::string> password_repeat = libsecret.GetKey();
  EXPECT_TRUE(password_repeat.has_value());
  EXPECT_EQ(password.value(), password_repeat.value());
}

TEST_F(LibsecretTest, LibsecretCreatesRandomised) {
  KeyStorageLibsecret libsecret("chromium");
  MockLibsecretLoader::ResetForOSCrypt();
  std::optional<std::string> password = libsecret.GetKey();
  MockLibsecretLoader::ResetForOSCrypt();
  std::optional<std::string> password_new = libsecret.GetKey();
  EXPECT_TRUE(password.has_value());
  EXPECT_TRUE(password_new.has_value());
  EXPECT_NE(password.value(), password_new.value());
}

}  // namespace