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 <cstdarg> // Needed to mock ellipsis | |
6 | |
Lei Zhang
2016/08/30 23:19:24
no blank line
cfroussios
2016/08/31 12:18:41
Done.
| |
7 #include <string> | |
8 | |
9 #include "base/macros.h" | |
10 #include "base/test/test_simple_task_runner.h" | |
11 #include "components/os_crypt/key_storage_keyring.h" | |
12 #include "components/os_crypt/keyring_util_linux.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | |
14 | |
15 namespace { | |
16 | |
17 #if defined(GOOGLE_CHROME_BUILD) | |
18 const char kApplicationName[] = "chrome"; | |
19 #else | |
20 const char kApplicationName[] = "chromium"; | |
21 #endif | |
22 | |
23 // Replaces some of GnomeKeyringLoader's methods with mocked ones. | |
24 class MockGnomeKeyringLoader : public GnomeKeyringLoader { | |
25 public: | |
26 static void ResetForOSCrypt() { | |
27 GnomeKeyringLoader::gnome_keyring_find_password_sync_ptr = | |
28 &mock_gnome_keyring_find_password_sync; | |
29 GnomeKeyringLoader::gnome_keyring_store_password_sync_ptr = | |
30 &mock_gnome_keyring_store_password_sync; | |
31 GnomeKeyringLoader::gnome_keyring_free_password_ptr = | |
32 &mock_gnome_keyring_free_password; | |
33 | |
34 delete s_password_ptr_; | |
35 s_password_ptr_ = nullptr; | |
36 | |
37 // GnomeKeyringLoader does not (re)load keyring is this is true. | |
38 GnomeKeyringLoader::keyring_loaded = true; | |
39 } | |
40 | |
41 static void SetOSCryptPassword(const char* password) { | |
42 delete s_password_ptr_; | |
43 s_password_ptr_ = new std::string(password); | |
44 } | |
45 | |
46 static void TearDown() { | |
47 delete s_password_ptr_; | |
48 s_password_ptr_ = nullptr; | |
49 // Function pointers will be reset the next time loading is requested. | |
50 GnomeKeyringLoader::keyring_loaded = false; | |
51 } | |
52 | |
53 private: | |
54 // These methods are used to redirect calls through GnomeKeyringLoader. | |
55 static GnomeKeyringResult mock_gnome_keyring_find_password_sync( | |
56 const GnomeKeyringPasswordSchema* schema, | |
57 gchar** password, | |
58 ...); | |
59 | |
60 static GnomeKeyringResult mock_gnome_keyring_store_password_sync( | |
61 const GnomeKeyringPasswordSchema* schema, | |
62 const gchar* keyring, | |
63 const gchar* display_name, | |
64 const gchar* password, | |
65 ...); | |
66 | |
67 static void mock_gnome_keyring_free_password(gchar* password); | |
68 | |
69 static std::string* s_password_ptr_; | |
70 }; | |
71 | |
72 std::string* MockGnomeKeyringLoader::s_password_ptr_ = nullptr; | |
73 | |
74 // static | |
75 GnomeKeyringResult | |
76 MockGnomeKeyringLoader::mock_gnome_keyring_find_password_sync( | |
77 const GnomeKeyringPasswordSchema* schema, | |
78 gchar** password, | |
79 ...) { | |
80 va_list attrs; | |
81 va_start(attrs, password); | |
82 EXPECT_STREQ("application", va_arg(attrs, const char*)); | |
83 EXPECT_STREQ(kApplicationName, va_arg(attrs, const char*)); | |
84 EXPECT_EQ(nullptr, va_arg(attrs, const char*)); | |
85 va_end(attrs); | |
86 | |
87 if (!s_password_ptr_) { | |
88 return GNOME_KEYRING_RESULT_NO_MATCH; | |
89 } else { | |
Lei Zhang
2016/08/30 23:19:24
no else after return
cfroussios
2016/08/31 12:18:41
Done.
| |
90 *password = strdup(s_password_ptr_->c_str()); | |
91 return GNOME_KEYRING_RESULT_OK; | |
92 } | |
93 } | |
94 | |
95 // static | |
96 GnomeKeyringResult | |
97 MockGnomeKeyringLoader::mock_gnome_keyring_store_password_sync( | |
98 const GnomeKeyringPasswordSchema* schema, | |
99 const gchar* keyring, | |
100 const gchar* display_name, | |
101 const gchar* password, | |
102 ...) { | |
103 va_list attrs; | |
104 va_start(attrs, password); | |
105 EXPECT_STREQ("application", va_arg(attrs, const char*)); | |
106 EXPECT_STREQ(kApplicationName, va_arg(attrs, const char*)); | |
107 EXPECT_EQ(nullptr, va_arg(attrs, const char*)); | |
108 va_end(attrs); | |
109 | |
110 delete s_password_ptr_; | |
111 s_password_ptr_ = new std::string(password); | |
112 return GNOME_KEYRING_RESULT_OK; | |
113 } | |
114 | |
115 // static | |
116 void MockGnomeKeyringLoader::mock_gnome_keyring_free_password(gchar* password) { | |
117 free(password); // We are mocking a C function. | |
118 } | |
119 | |
120 class GnomeKeyringTest : public testing::Test { | |
121 public: | |
122 GnomeKeyringTest(); | |
123 ~GnomeKeyringTest() override; | |
124 | |
125 void SetUp() override { MockGnomeKeyringLoader::ResetForOSCrypt(); } | |
126 | |
127 void TearDown() override { MockGnomeKeyringLoader::TearDown(); } | |
128 | |
129 protected: | |
130 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | |
131 KeyStorageKeyring keyring_; | |
132 | |
133 private: | |
134 DISALLOW_COPY_AND_ASSIGN(GnomeKeyringTest); | |
135 }; | |
136 | |
137 GnomeKeyringTest::GnomeKeyringTest() | |
138 : task_runner_(new base::TestSimpleTaskRunner()), keyring_(task_runner_) {} | |
139 | |
140 GnomeKeyringTest::~GnomeKeyringTest() {} | |
141 | |
142 TEST_F(GnomeKeyringTest, KeyringRepeats) { | |
143 MockGnomeKeyringLoader::ResetForOSCrypt(); | |
144 | |
145 std::string password = keyring_.GetKey(); | |
146 EXPECT_FALSE(password.empty()); | |
147 std::string password_repeat = keyring_.GetKey(); | |
148 EXPECT_EQ(password, password_repeat); | |
149 } | |
150 | |
151 TEST_F(GnomeKeyringTest, KeyringCreatesRandomised) { | |
152 MockGnomeKeyringLoader::ResetForOSCrypt(); | |
153 | |
154 std::string password = keyring_.GetKey(); | |
155 MockGnomeKeyringLoader::ResetForOSCrypt(); | |
156 std::string password_new = keyring_.GetKey(); | |
157 EXPECT_NE(password, password_new); | |
158 } | |
159 | |
160 } // namespace | |
OLD | NEW |