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