Lutz Justen | b7ef580 | 2019-05-14 14:03:20 +0200 | [diff] [blame] | 1 | // Copyright 2019 The Chromium OS 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 "kerberos/account_manager.h" |
| 6 | |
| 7 | #include <map> |
| 8 | #include <memory> |
| 9 | #include <utility> |
| 10 | |
| 11 | #include <base/bind.h> |
Lutz Justen | e39cbd4 | 2019-05-14 14:52:24 +0200 | [diff] [blame] | 12 | #include <base/files/file_path.h> |
Lutz Justen | b7ef580 | 2019-05-14 14:03:20 +0200 | [diff] [blame] | 13 | #include <base/files/file_util.h> |
| 14 | #include <base/files/scoped_temp_dir.h> |
Lutz Justen | d925d71 | 2019-06-27 17:34:00 +0200 | [diff] [blame] | 15 | #include <base/memory/ref_counted.h> |
| 16 | #include <base/test/test_mock_time_task_runner.h> |
Lutz Justen | 2d857e1 | 2019-07-16 15:58:09 +0200 | [diff] [blame^] | 17 | #include <gmock/gmock.h> |
Lutz Justen | b7ef580 | 2019-05-14 14:03:20 +0200 | [diff] [blame] | 18 | #include <gtest/gtest.h> |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 19 | #include <libpasswordprovider/fake_password_provider.h> |
| 20 | #include <libpasswordprovider/password_provider_test_utils.h> |
Lutz Justen | b7ef580 | 2019-05-14 14:03:20 +0200 | [diff] [blame] | 21 | |
Lutz Justen | e39cbd4 | 2019-05-14 14:52:24 +0200 | [diff] [blame] | 22 | #include "kerberos/fake_krb5_interface.h" |
Lutz Justen | 2d857e1 | 2019-07-16 15:58:09 +0200 | [diff] [blame^] | 23 | #include "kerberos/kerberos_metrics.h" |
Lutz Justen | 3360180 | 2019-07-03 22:40:57 +0200 | [diff] [blame] | 24 | #include "kerberos/krb5_jail_wrapper.h" |
Lutz Justen | e39cbd4 | 2019-05-14 14:52:24 +0200 | [diff] [blame] | 25 | |
Lutz Justen | 2d857e1 | 2019-07-16 15:58:09 +0200 | [diff] [blame^] | 26 | using testing::_; |
| 27 | using testing::Mock; |
| 28 | using testing::NiceMock; |
| 29 | using testing::Return; |
| 30 | |
Lutz Justen | d925d71 | 2019-06-27 17:34:00 +0200 | [diff] [blame] | 31 | namespace kerberos { |
Lutz Justen | b7ef580 | 2019-05-14 14:03:20 +0200 | [diff] [blame] | 32 | namespace { |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 33 | |
Lutz Justen | b7ef580 | 2019-05-14 14:03:20 +0200 | [diff] [blame] | 34 | constexpr char kUser[] = "user@REALM.COM"; |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 35 | constexpr char kUser2[] = "user2@REALM2.COM"; |
Lutz Justen | 781825e | 2019-07-13 07:49:44 +0200 | [diff] [blame] | 36 | constexpr char kUser3[] = "user3@REALM3.COM"; |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 37 | constexpr char kPassword[] = "i<3k3R8e5Oz"; |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 38 | constexpr char kPassword2[] = "ih4zf00d"; |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 39 | constexpr char kKrb5Conf[] = R"( |
| 40 | [libdefaults] |
| 41 | default_realm = REALM.COM)"; |
| 42 | |
Lutz Justen | d925d71 | 2019-06-27 17:34:00 +0200 | [diff] [blame] | 43 | constexpr Krb5Interface::TgtStatus kValidTgt(3600, 3600); |
| 44 | constexpr Krb5Interface::TgtStatus kExpiredTgt(0, 0); |
| 45 | |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 46 | // Convenience defines to make code more readable |
| 47 | constexpr bool kManaged = true; |
| 48 | constexpr bool kUnmanaged = false; |
| 49 | |
| 50 | constexpr bool kRememberPassword = true; |
| 51 | constexpr bool kDontRememberPassword = false; |
| 52 | |
| 53 | constexpr bool kUseLoginPassword = true; |
| 54 | constexpr bool kDontUseLoginPassword = false; |
| 55 | |
| 56 | constexpr char kEmptyPassword[] = ""; |
| 57 | |
Lutz Justen | 2d857e1 | 2019-07-16 15:58:09 +0200 | [diff] [blame^] | 58 | class MockMetrics : public KerberosMetrics { |
| 59 | public: |
| 60 | explicit MockMetrics(const base::FilePath& storage_dir) |
| 61 | : KerberosMetrics(storage_dir) {} |
| 62 | ~MockMetrics() override = default; |
| 63 | |
| 64 | MOCK_METHOD0(ShouldReportDailyUsageStats, bool()); |
| 65 | MOCK_METHOD5(ReportDailyUsageStats, void(int, int, int, int, int)); |
| 66 | |
| 67 | private: |
| 68 | DISALLOW_COPY_AND_ASSIGN(MockMetrics); |
| 69 | }; |
| 70 | |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 71 | } // namespace |
Lutz Justen | b7ef580 | 2019-05-14 14:03:20 +0200 | [diff] [blame] | 72 | |
Lutz Justen | b7ef580 | 2019-05-14 14:03:20 +0200 | [diff] [blame] | 73 | class AccountManagerTest : public ::testing::Test { |
| 74 | public: |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 75 | AccountManagerTest() |
| 76 | : kerberos_files_changed_( |
| 77 | base::BindRepeating(&AccountManagerTest::OnKerberosFilesChanged, |
Lutz Justen | 8eeb4a2 | 2019-06-19 17:00:32 +0200 | [diff] [blame] | 78 | base::Unretained(this))), |
| 79 | kerberos_ticket_expiring_( |
| 80 | base::BindRepeating(&AccountManagerTest::OnKerberosTicketExpiring, |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 81 | base::Unretained(this))) {} |
Lutz Justen | b7ef580 | 2019-05-14 14:03:20 +0200 | [diff] [blame] | 82 | ~AccountManagerTest() override = default; |
| 83 | |
| 84 | void SetUp() override { |
| 85 | ::testing::Test::SetUp(); |
| 86 | |
| 87 | // Create temp directory for files written during tests. |
| 88 | CHECK(storage_dir_.CreateUniqueTempDir()); |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 89 | accounts_path_ = storage_dir_.GetPath().Append("accounts"); |
Lutz Justen | 3360180 | 2019-07-03 22:40:57 +0200 | [diff] [blame] | 90 | account_dir_ = storage_dir_.GetPath().Append( |
Lutz Justen | c569050 | 2019-05-24 17:00:54 +0200 | [diff] [blame] | 91 | AccountManager::GetSafeFilenameForTesting(kUser)); |
Lutz Justen | 3360180 | 2019-07-03 22:40:57 +0200 | [diff] [blame] | 92 | krb5cc_path_ = account_dir_.Append("krb5cc"); |
| 93 | krb5conf_path_ = account_dir_.Append("krb5.conf"); |
| 94 | password_path_ = account_dir_.Append("password"); |
Lutz Justen | b7ef580 | 2019-05-14 14:03:20 +0200 | [diff] [blame] | 95 | |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 96 | // Create the manager with a fake krb5 interface. |
| 97 | auto krb5 = std::make_unique<FakeKrb5Interface>(); |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 98 | auto password_provider = |
| 99 | std::make_unique<password_provider::FakePasswordProvider>(); |
Lutz Justen | 2d857e1 | 2019-07-16 15:58:09 +0200 | [diff] [blame^] | 100 | metrics_ = std::make_unique<NiceMock<MockMetrics>>(storage_dir_.GetPath()); |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 101 | krb5_ = krb5.get(); |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 102 | password_provider_ = password_provider.get(); |
Lutz Justen | b7ef580 | 2019-05-14 14:03:20 +0200 | [diff] [blame] | 103 | manager_ = std::make_unique<AccountManager>( |
Lutz Justen | 8eeb4a2 | 2019-06-19 17:00:32 +0200 | [diff] [blame] | 104 | storage_dir_.GetPath(), kerberos_files_changed_, |
| 105 | kerberos_ticket_expiring_, std::move(krb5), |
Lutz Justen | 2d857e1 | 2019-07-16 15:58:09 +0200 | [diff] [blame^] | 106 | std::move(password_provider), metrics_.get()); |
Lutz Justen | b7ef580 | 2019-05-14 14:03:20 +0200 | [diff] [blame] | 107 | } |
| 108 | |
Lutz Justen | 82d07ce | 2019-05-16 10:46:14 +0200 | [diff] [blame] | 109 | void TearDown() override { |
| 110 | // Make sure the file stored on disk contains the same accounts as the |
| 111 | // manager instance. This catches cases where AccountManager forgets to save |
| 112 | // accounts on some change. |
| 113 | if (base::PathExists(accounts_path_)) { |
| 114 | std::vector<Account> accounts; |
| 115 | EXPECT_EQ(ERROR_NONE, manager_->ListAccounts(&accounts)); |
| 116 | |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 117 | AccountManager other_manager( |
| 118 | storage_dir_.GetPath(), kerberos_files_changed_, |
Lutz Justen | 8eeb4a2 | 2019-06-19 17:00:32 +0200 | [diff] [blame] | 119 | kerberos_ticket_expiring_, std::make_unique<FakeKrb5Interface>(), |
Lutz Justen | 2d857e1 | 2019-07-16 15:58:09 +0200 | [diff] [blame^] | 120 | std::make_unique<password_provider::FakePasswordProvider>(), |
| 121 | metrics_.get()); |
Lutz Justen | 82d07ce | 2019-05-16 10:46:14 +0200 | [diff] [blame] | 122 | other_manager.LoadAccounts(); |
| 123 | std::vector<Account> other_accounts; |
| 124 | EXPECT_EQ(ERROR_NONE, other_manager.ListAccounts(&other_accounts)); |
| 125 | |
| 126 | ASSERT_NO_FATAL_FAILURE(ExpectAccountsEqual(accounts, other_accounts)); |
| 127 | } |
| 128 | |
| 129 | ::testing::Test::TearDown(); |
| 130 | } |
| 131 | |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 132 | // Add account with default settings. |
| 133 | ErrorType AddAccount() { return manager_->AddAccount(kUser, kUnmanaged); } |
| 134 | |
| 135 | // Sets some default Kerberos configuration. |
| 136 | ErrorType SetConfig() { return manager_->SetConfig(kUser, kKrb5Conf); } |
| 137 | |
| 138 | // Acquire Kerberos ticket with default credentials and settings. |
| 139 | ErrorType AcquireTgt() { |
| 140 | return manager_->AcquireTgt(kUser, kPassword, kDontRememberPassword, |
| 141 | kDontUseLoginPassword); |
| 142 | } |
| 143 | |
| 144 | void SaveLoginPassword(const char* password) { |
| 145 | auto password_ptr = password_provider::test::CreatePassword(password); |
| 146 | password_provider_->SavePassword(*password_ptr); |
| 147 | } |
| 148 | |
Lutz Justen | d925d71 | 2019-06-27 17:34:00 +0200 | [diff] [blame] | 149 | // Fast forwards to the next scheduled task (assumed to be the renewal task) |
| 150 | // and verifies expectation that |krb5_->RenewTgt() was called|. |
| 151 | void RunScheduledRenewalTask() { |
| 152 | int initial_count = krb5_->renew_tgt_call_count(); |
| 153 | EXPECT_EQ(1, task_runner_->GetPendingTaskCount()); |
| 154 | task_runner_->FastForwardBy(task_runner_->NextPendingTaskDelay()); |
| 155 | EXPECT_EQ(initial_count + 1, krb5_->renew_tgt_call_count()); |
| 156 | } |
| 157 | |
Lutz Justen | b7ef580 | 2019-05-14 14:03:20 +0200 | [diff] [blame] | 158 | protected: |
| 159 | void OnKerberosFilesChanged(const std::string& principal_name) { |
| 160 | kerberos_files_changed_count_[principal_name]++; |
| 161 | } |
| 162 | |
Lutz Justen | 8eeb4a2 | 2019-06-19 17:00:32 +0200 | [diff] [blame] | 163 | void OnKerberosTicketExpiring(const std::string& principal_name) { |
| 164 | kerberos_ticket_expiring_count_[principal_name]++; |
| 165 | } |
| 166 | |
Lutz Justen | 82d07ce | 2019-05-16 10:46:14 +0200 | [diff] [blame] | 167 | void ExpectAccountsEqual(const std::vector<Account>& account_list_1, |
| 168 | const std::vector<Account>& account_list_2) { |
| 169 | ASSERT_EQ(account_list_1.size(), account_list_2.size()); |
| 170 | for (size_t n = 0; n < account_list_1.size(); ++n) { |
| 171 | const Account& account1 = account_list_1[n]; |
| 172 | const Account& account2 = account_list_2[n]; |
| 173 | |
| 174 | EXPECT_EQ(account1.principal_name(), account2.principal_name()); |
| 175 | EXPECT_EQ(account1.is_managed(), account2.is_managed()); |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 176 | EXPECT_EQ(account1.use_login_password(), account2.use_login_password()); |
Lutz Justen | 82d07ce | 2019-05-16 10:46:14 +0200 | [diff] [blame] | 177 | // TODO(https://crbug.com/952239): Check additional properties. |
| 178 | } |
| 179 | } |
| 180 | |
Lutz Justen | b7ef580 | 2019-05-14 14:03:20 +0200 | [diff] [blame] | 181 | std::unique_ptr<AccountManager> manager_; |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 182 | |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 183 | // Fake Kerberos interface used by |manager_|. Not owned. |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 184 | FakeKrb5Interface* krb5_; |
| 185 | |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 186 | // Fake password provider to get the login password. Not owned. |
| 187 | password_provider::FakePasswordProvider* password_provider_; |
| 188 | |
Lutz Justen | 2d857e1 | 2019-07-16 15:58:09 +0200 | [diff] [blame^] | 189 | // Mock metrics for testing UMA stat recording. |
| 190 | std::unique_ptr<NiceMock<MockMetrics>> metrics_; |
| 191 | |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 192 | // Paths of files stored by |manager_|. |
Lutz Justen | b7ef580 | 2019-05-14 14:03:20 +0200 | [diff] [blame] | 193 | base::ScopedTempDir storage_dir_; |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 194 | base::FilePath accounts_path_; |
Lutz Justen | 3360180 | 2019-07-03 22:40:57 +0200 | [diff] [blame] | 195 | base::FilePath account_dir_; |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 196 | base::FilePath krb5conf_path_; |
| 197 | base::FilePath krb5cc_path_; |
Lutz Justen | c569050 | 2019-05-24 17:00:54 +0200 | [diff] [blame] | 198 | base::FilePath password_path_; |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 199 | |
| 200 | AccountManager::KerberosFilesChangedCallback kerberos_files_changed_; |
Lutz Justen | 8eeb4a2 | 2019-06-19 17:00:32 +0200 | [diff] [blame] | 201 | AccountManager::KerberosTicketExpiringCallback kerberos_ticket_expiring_; |
| 202 | |
Lutz Justen | b7ef580 | 2019-05-14 14:03:20 +0200 | [diff] [blame] | 203 | std::map<std::string, int> kerberos_files_changed_count_; |
Lutz Justen | 8eeb4a2 | 2019-06-19 17:00:32 +0200 | [diff] [blame] | 204 | std::map<std::string, int> kerberos_ticket_expiring_count_; |
Lutz Justen | b7ef580 | 2019-05-14 14:03:20 +0200 | [diff] [blame] | 205 | |
Lutz Justen | d925d71 | 2019-06-27 17:34:00 +0200 | [diff] [blame] | 206 | scoped_refptr<base::TestMockTimeTaskRunner> task_runner_{ |
| 207 | new base::TestMockTimeTaskRunner()}; |
| 208 | base::TestMockTimeTaskRunner::ScopedContext scoped_context_{task_runner_}; |
| 209 | |
Lutz Justen | b7ef580 | 2019-05-14 14:03:20 +0200 | [diff] [blame] | 210 | private: |
| 211 | DISALLOW_COPY_AND_ASSIGN(AccountManagerTest); |
| 212 | }; |
| 213 | |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 214 | // Adding an account succeeds and serializes the file on disk. |
Lutz Justen | b7ef580 | 2019-05-14 14:03:20 +0200 | [diff] [blame] | 215 | TEST_F(AccountManagerTest, AddAccountSuccess) { |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 216 | EXPECT_FALSE(base::PathExists(accounts_path_)); |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 217 | EXPECT_EQ(ERROR_NONE, AddAccount()); |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 218 | EXPECT_TRUE(base::PathExists(accounts_path_)); |
Lutz Justen | b7ef580 | 2019-05-14 14:03:20 +0200 | [diff] [blame] | 219 | } |
| 220 | |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 221 | // AddAccount() fails if the same account is added twice. |
Lutz Justen | b7ef580 | 2019-05-14 14:03:20 +0200 | [diff] [blame] | 222 | TEST_F(AccountManagerTest, AddDuplicateAccountFail) { |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 223 | ignore_result(AddAccount()); |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 224 | |
| 225 | EXPECT_TRUE(base::DeleteFile(accounts_path_, false /* recursive */)); |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 226 | EXPECT_EQ(ERROR_DUPLICATE_PRINCIPAL_NAME, AddAccount()); |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 227 | EXPECT_FALSE(base::PathExists(accounts_path_)); |
Lutz Justen | b7ef580 | 2019-05-14 14:03:20 +0200 | [diff] [blame] | 228 | } |
| 229 | |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 230 | // Adding a managed account overwrites an existing unmanaged account. |
| 231 | TEST_F(AccountManagerTest, ManagedOverridesUnmanaged) { |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 232 | ignore_result(manager_->AddAccount(kUser, kUnmanaged)); |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 233 | |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 234 | EXPECT_EQ(ERROR_NONE, AcquireTgt()); |
Lutz Justen | 82d07ce | 2019-05-16 10:46:14 +0200 | [diff] [blame] | 235 | EXPECT_TRUE(base::PathExists(krb5cc_path_)); |
| 236 | |
| 237 | // Overwriting with a managed account should wipe existing files and make the |
| 238 | // account managed. |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 239 | EXPECT_EQ(ERROR_DUPLICATE_PRINCIPAL_NAME, |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 240 | manager_->AddAccount(kUser, kManaged)); |
Lutz Justen | 82d07ce | 2019-05-16 10:46:14 +0200 | [diff] [blame] | 241 | EXPECT_FALSE(base::PathExists(krb5cc_path_)); |
| 242 | |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 243 | std::vector<Account> accounts; |
| 244 | EXPECT_EQ(ERROR_NONE, manager_->ListAccounts(&accounts)); |
| 245 | ASSERT_EQ(1u, accounts.size()); |
| 246 | EXPECT_TRUE(accounts[0].is_managed()); |
| 247 | } |
| 248 | |
| 249 | // Adding an unmanaged account does not overwrite an existing managed account. |
| 250 | TEST_F(AccountManagerTest, UnmanagedDoesNotOverrideManaged) { |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 251 | ignore_result(manager_->AddAccount(kUser, kManaged)); |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 252 | |
| 253 | EXPECT_EQ(ERROR_DUPLICATE_PRINCIPAL_NAME, |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 254 | manager_->AddAccount(kUser, kUnmanaged)); |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 255 | std::vector<Account> accounts; |
| 256 | EXPECT_EQ(ERROR_NONE, manager_->ListAccounts(&accounts)); |
| 257 | ASSERT_EQ(1u, accounts.size()); |
| 258 | EXPECT_TRUE(accounts[0].is_managed()); |
| 259 | } |
| 260 | |
| 261 | // RemoveAccount() succeeds if the account exists and serializes the file on |
| 262 | // disk. |
Lutz Justen | b7ef580 | 2019-05-14 14:03:20 +0200 | [diff] [blame] | 263 | TEST_F(AccountManagerTest, RemoveAccountSuccess) { |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 264 | ignore_result(AddAccount()); |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 265 | |
| 266 | EXPECT_TRUE(base::DeleteFile(accounts_path_, false /* recursive */)); |
Lutz Justen | b7ef580 | 2019-05-14 14:03:20 +0200 | [diff] [blame] | 267 | EXPECT_EQ(ERROR_NONE, manager_->RemoveAccount(kUser)); |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 268 | EXPECT_TRUE(base::PathExists(accounts_path_)); |
Lutz Justen | b7ef580 | 2019-05-14 14:03:20 +0200 | [diff] [blame] | 269 | } |
| 270 | |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 271 | // RemoveAccount() fails if the account does not exist. |
Lutz Justen | b7ef580 | 2019-05-14 14:03:20 +0200 | [diff] [blame] | 272 | TEST_F(AccountManagerTest, RemoveUnknownAccountFail) { |
| 273 | EXPECT_EQ(ERROR_UNKNOWN_PRINCIPAL_NAME, manager_->RemoveAccount(kUser)); |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 274 | EXPECT_FALSE(base::PathExists(accounts_path_)); |
| 275 | } |
| 276 | |
| 277 | // RemoveAccount() does not trigger KerberosFilesChanged if the credential cache |
| 278 | // does not exists. |
| 279 | TEST_F(AccountManagerTest, RemoveAccountTriggersKFCIfCCExists) { |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 280 | ignore_result(AddAccount()); |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 281 | |
| 282 | EXPECT_EQ(ERROR_NONE, manager_->RemoveAccount(kUser)); |
| 283 | EXPECT_EQ(0, kerberos_files_changed_count_[kUser]); |
| 284 | } |
| 285 | |
| 286 | // RemoveAccount() triggers KerberosFilesChanged if the credential cache exists. |
| 287 | TEST_F(AccountManagerTest, RemoveAccountDoesNotTriggerKFCIfCCDoesNotExist) { |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 288 | ignore_result(AddAccount()); |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 289 | |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 290 | EXPECT_EQ(ERROR_NONE, AcquireTgt()); |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 291 | EXPECT_EQ(1, kerberos_files_changed_count_[kUser]); |
| 292 | EXPECT_EQ(ERROR_NONE, manager_->RemoveAccount(kUser)); |
| 293 | EXPECT_EQ(2, kerberos_files_changed_count_[kUser]); |
Lutz Justen | b7ef580 | 2019-05-14 14:03:20 +0200 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | // Repeatedly calling AddAccount() and RemoveAccount() succeeds. |
| 297 | TEST_F(AccountManagerTest, RepeatedAddRemoveSuccess) { |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 298 | ignore_result(AddAccount()); |
Lutz Justen | b7ef580 | 2019-05-14 14:03:20 +0200 | [diff] [blame] | 299 | ignore_result(manager_->RemoveAccount(kUser)); |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 300 | |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 301 | EXPECT_EQ(ERROR_NONE, AddAccount()); |
Lutz Justen | b7ef580 | 2019-05-14 14:03:20 +0200 | [diff] [blame] | 302 | EXPECT_EQ(ERROR_NONE, manager_->RemoveAccount(kUser)); |
| 303 | } |
| 304 | |
Lutz Justen | 2d42087 | 2019-06-25 14:17:21 +0200 | [diff] [blame] | 305 | // ClearAccounts(CLEAR_ALL) clears all accounts. |
| 306 | TEST_F(AccountManagerTest, ClearAccountsSuccess) { |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 307 | ignore_result(manager_->AddAccount(kUser, kUnmanaged)); |
| 308 | ignore_result(manager_->AddAccount(kUser2, kManaged)); |
Lutz Justen | 82d07ce | 2019-05-16 10:46:14 +0200 | [diff] [blame] | 309 | |
Lutz Justen | 781825e | 2019-07-13 07:49:44 +0200 | [diff] [blame] | 310 | EXPECT_EQ(ERROR_NONE, manager_->ClearAccounts(CLEAR_ALL, {})); |
Lutz Justen | 82d07ce | 2019-05-16 10:46:14 +0200 | [diff] [blame] | 311 | std::vector<Account> accounts; |
| 312 | EXPECT_EQ(ERROR_NONE, manager_->ListAccounts(&accounts)); |
| 313 | EXPECT_EQ(0u, accounts.size()); |
| 314 | } |
| 315 | |
Lutz Justen | 2d42087 | 2019-06-25 14:17:21 +0200 | [diff] [blame] | 316 | // ClearAccounts(CLEAR_ALL) wipes Kerberos configuration and credential cache. |
| 317 | TEST_F(AccountManagerTest, ClearAccountsRemovesKerberosFiles) { |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 318 | ignore_result(AddAccount()); |
Lutz Justen | 82d07ce | 2019-05-16 10:46:14 +0200 | [diff] [blame] | 319 | |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 320 | EXPECT_EQ(ERROR_NONE, SetConfig()); |
| 321 | EXPECT_EQ(ERROR_NONE, AcquireTgt()); |
Lutz Justen | 82d07ce | 2019-05-16 10:46:14 +0200 | [diff] [blame] | 322 | EXPECT_TRUE(base::PathExists(krb5conf_path_)); |
| 323 | EXPECT_TRUE(base::PathExists(krb5cc_path_)); |
Lutz Justen | 781825e | 2019-07-13 07:49:44 +0200 | [diff] [blame] | 324 | EXPECT_EQ(ERROR_NONE, manager_->ClearAccounts(CLEAR_ALL, {})); |
Lutz Justen | 82d07ce | 2019-05-16 10:46:14 +0200 | [diff] [blame] | 325 | EXPECT_FALSE(base::PathExists(krb5conf_path_)); |
| 326 | EXPECT_FALSE(base::PathExists(krb5cc_path_)); |
| 327 | } |
| 328 | |
Lutz Justen | 2d42087 | 2019-06-25 14:17:21 +0200 | [diff] [blame] | 329 | // ClearAccounts(CLEAR_ALL) triggers KerberosFilesChanged if the credential |
| 330 | // cache exists. |
| 331 | TEST_F(AccountManagerTest, ClearAccountsTriggersKFCIfCCExists) { |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 332 | ignore_result(AddAccount()); |
Lutz Justen | 82d07ce | 2019-05-16 10:46:14 +0200 | [diff] [blame] | 333 | |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 334 | EXPECT_EQ(ERROR_NONE, AcquireTgt()); |
Lutz Justen | 82d07ce | 2019-05-16 10:46:14 +0200 | [diff] [blame] | 335 | EXPECT_EQ(1, kerberos_files_changed_count_[kUser]); |
Lutz Justen | 781825e | 2019-07-13 07:49:44 +0200 | [diff] [blame] | 336 | EXPECT_EQ(ERROR_NONE, manager_->ClearAccounts(CLEAR_ALL, {})); |
Lutz Justen | 82d07ce | 2019-05-16 10:46:14 +0200 | [diff] [blame] | 337 | EXPECT_EQ(2, kerberos_files_changed_count_[kUser]); |
| 338 | } |
| 339 | |
Lutz Justen | 2d42087 | 2019-06-25 14:17:21 +0200 | [diff] [blame] | 340 | // ClearAccounts(CLEAR_ALL) does not trigger KerberosFilesChanged if the |
| 341 | // credential cache does not exist. |
| 342 | TEST_F(AccountManagerTest, ClearAccountsDoesNotTriggerKFCIfDoesNotCCExist) { |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 343 | ignore_result(AddAccount()); |
Lutz Justen | 82d07ce | 2019-05-16 10:46:14 +0200 | [diff] [blame] | 344 | |
Lutz Justen | 781825e | 2019-07-13 07:49:44 +0200 | [diff] [blame] | 345 | EXPECT_EQ(ERROR_NONE, manager_->ClearAccounts(CLEAR_ALL, {})); |
Lutz Justen | 82d07ce | 2019-05-16 10:46:14 +0200 | [diff] [blame] | 346 | EXPECT_EQ(0, kerberos_files_changed_count_[kUser]); |
| 347 | } |
| 348 | |
Lutz Justen | 2d42087 | 2019-06-25 14:17:21 +0200 | [diff] [blame] | 349 | // ClearAccounts(CLEAR_ONLY_UNMANAGED_ACCOUNTS) clears only unmanaged accounts. |
| 350 | TEST_F(AccountManagerTest, ClearUnmanagedAccountsSuccess) { |
| 351 | ignore_result(manager_->AddAccount(kUser, kUnmanaged)); |
| 352 | ignore_result(manager_->AddAccount(kUser2, kManaged)); |
| 353 | |
Lutz Justen | 781825e | 2019-07-13 07:49:44 +0200 | [diff] [blame] | 354 | EXPECT_EQ(ERROR_NONE, |
| 355 | manager_->ClearAccounts(CLEAR_ONLY_UNMANAGED_ACCOUNTS, {})); |
Lutz Justen | 2d42087 | 2019-06-25 14:17:21 +0200 | [diff] [blame] | 356 | std::vector<Account> accounts; |
| 357 | EXPECT_EQ(ERROR_NONE, manager_->ListAccounts(&accounts)); |
| 358 | ASSERT_EQ(1u, accounts.size()); |
| 359 | EXPECT_EQ(kUser2, accounts[0].principal_name()); |
| 360 | } |
| 361 | |
| 362 | // ClearAccounts(CLEAR_ONLY_UNMANAGED_REMEMBERED_PASSWORDS) clears only |
| 363 | // passwords of unmanaged accounts. |
| 364 | TEST_F(AccountManagerTest, ClearUnmanagedPasswordsSuccess) { |
| 365 | // kUser is unmanaged, kUser2 is managed. |
| 366 | ignore_result(manager_->AddAccount(kUser, kUnmanaged)); |
| 367 | ignore_result(manager_->AddAccount(kUser2, kManaged)); |
| 368 | ignore_result(manager_->AcquireTgt(kUser, kPassword, kRememberPassword, |
| 369 | kDontUseLoginPassword)); |
| 370 | ignore_result(manager_->AcquireTgt(kUser2, kPassword, kRememberPassword, |
| 371 | kDontUseLoginPassword)); |
| 372 | |
| 373 | base::FilePath password_path_2 = |
| 374 | storage_dir_.GetPath() |
| 375 | .Append(AccountManager::GetSafeFilenameForTesting(kUser2)) |
| 376 | .Append("password"); |
| 377 | EXPECT_TRUE(base::PathExists(password_path_)); |
| 378 | EXPECT_TRUE(base::PathExists(password_path_2)); |
| 379 | |
Lutz Justen | 781825e | 2019-07-13 07:49:44 +0200 | [diff] [blame] | 380 | EXPECT_EQ(ERROR_NONE, manager_->ClearAccounts( |
| 381 | CLEAR_ONLY_UNMANAGED_REMEMBERED_PASSWORDS, {})); |
Lutz Justen | 2d42087 | 2019-06-25 14:17:21 +0200 | [diff] [blame] | 382 | std::vector<Account> accounts; |
| 383 | EXPECT_EQ(ERROR_NONE, manager_->ListAccounts(&accounts)); |
| 384 | ASSERT_EQ(2u, accounts.size()); |
| 385 | EXPECT_FALSE(base::PathExists(password_path_)); |
| 386 | EXPECT_TRUE(base::PathExists(password_path_2)); |
| 387 | } |
| 388 | |
Lutz Justen | 781825e | 2019-07-13 07:49:44 +0200 | [diff] [blame] | 389 | // ClearAccounts(CLEAR_ONLY_MANAGED_ACCOUNTS) clears only managed accounts that |
| 390 | // are not on the keep list. |
| 391 | TEST_F(AccountManagerTest, ClearManagedPasswordsWithKeepListSuccess) { |
| 392 | ignore_result(manager_->AddAccount(kUser, kManaged)); |
| 393 | ignore_result(manager_->AddAccount(kUser2, kManaged)); |
| 394 | ignore_result(manager_->AddAccount(kUser3, kUnmanaged)); |
| 395 | |
| 396 | // Keep the managed kUser-account. |
| 397 | EXPECT_EQ(ERROR_NONE, |
| 398 | manager_->ClearAccounts(CLEAR_ONLY_MANAGED_ACCOUNTS, {kUser})); |
| 399 | std::vector<Account> accounts; |
| 400 | EXPECT_EQ(ERROR_NONE, manager_->ListAccounts(&accounts)); |
| 401 | ASSERT_EQ(2u, accounts.size()); |
| 402 | EXPECT_EQ(kUser, accounts[0].principal_name()); |
| 403 | EXPECT_EQ(kUser3, accounts[1].principal_name()); |
| 404 | } |
| 405 | |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 406 | // SetConfig() succeeds and writes the config to |krb5conf_path_|. |
| 407 | TEST_F(AccountManagerTest, SetConfigSuccess) { |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 408 | ignore_result(AddAccount()); |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 409 | |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 410 | EXPECT_EQ(ERROR_NONE, SetConfig()); |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 411 | std::string krb5_conf; |
| 412 | EXPECT_TRUE(base::ReadFileToString(krb5conf_path_, &krb5_conf)); |
| 413 | EXPECT_EQ(krb5_conf, kKrb5Conf); |
| 414 | } |
| 415 | |
Lutz Justen | 9028140 | 2019-07-05 15:14:37 +0200 | [diff] [blame] | 416 | // SetConfig() calls ValidateConfig on the Kerberos interface. |
| 417 | TEST_F(AccountManagerTest, SetConfigValidatesConfig) { |
| 418 | ignore_result(AddAccount()); |
| 419 | |
| 420 | krb5_->set_validate_config_error(ERROR_BAD_CONFIG); |
| 421 | EXPECT_EQ(ERROR_BAD_CONFIG, SetConfig()); |
| 422 | } |
| 423 | |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 424 | // SetConfig() triggers KerberosFilesChanged if the credential cache exists. |
| 425 | TEST_F(AccountManagerTest, SetConfigTriggersKFCIfCCExists) { |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 426 | ignore_result(AddAccount()); |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 427 | |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 428 | EXPECT_EQ(ERROR_NONE, AcquireTgt()); |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 429 | EXPECT_EQ(1, kerberos_files_changed_count_[kUser]); |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 430 | EXPECT_EQ(ERROR_NONE, SetConfig()); |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 431 | EXPECT_EQ(2, kerberos_files_changed_count_[kUser]); |
| 432 | } |
| 433 | |
Lutz Justen | 82d07ce | 2019-05-16 10:46:14 +0200 | [diff] [blame] | 434 | // SetConfig() does not trigger KerberosFilesChanged if the credential cache |
| 435 | // does not exist. |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 436 | TEST_F(AccountManagerTest, SetConfigDoesNotTriggerKFCIfDoesNotCCExist) { |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 437 | ignore_result(AddAccount()); |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 438 | |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 439 | EXPECT_EQ(ERROR_NONE, SetConfig()); |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 440 | EXPECT_EQ(0, kerberos_files_changed_count_[kUser]); |
| 441 | } |
| 442 | |
| 443 | // RemoveAccount() removes the config file. |
| 444 | TEST_F(AccountManagerTest, RemoveAccountRemovesConfig) { |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 445 | ignore_result(AddAccount()); |
| 446 | ignore_result(SetConfig()); |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 447 | |
| 448 | EXPECT_TRUE(base::PathExists(krb5conf_path_)); |
| 449 | EXPECT_EQ(ERROR_NONE, manager_->RemoveAccount(kUser)); |
| 450 | EXPECT_FALSE(base::PathExists(krb5conf_path_)); |
| 451 | } |
| 452 | |
Lutz Justen | 9028140 | 2019-07-05 15:14:37 +0200 | [diff] [blame] | 453 | // ValidateConfig() validates a good config successfully. |
| 454 | TEST_F(AccountManagerTest, ValidateConfigSuccess) { |
| 455 | constexpr char kValidKrb5Conf[] = ""; |
| 456 | ConfigErrorInfo error_info; |
| 457 | EXPECT_EQ(ERROR_NONE, manager_->ValidateConfig(kValidKrb5Conf, &error_info)); |
| 458 | EXPECT_EQ(CONFIG_ERROR_NONE, error_info.code()); |
| 459 | } |
| 460 | |
| 461 | // ValidateConfig() returns the correct error for a bad config. |
| 462 | TEST_F(AccountManagerTest, ValidateConfigFailure) { |
| 463 | ConfigErrorInfo expected_error_info; |
| 464 | expected_error_info.set_code(CONFIG_ERROR_SECTION_SYNTAX); |
| 465 | krb5_->set_config_error_info(expected_error_info); |
| 466 | krb5_->set_validate_config_error(ERROR_BAD_CONFIG); |
| 467 | |
| 468 | constexpr char kBadKrb5Conf[] = |
| 469 | "[libdefaults]'); DROP TABLE KerberosTickets;--"; |
| 470 | ConfigErrorInfo error_info; |
| 471 | EXPECT_EQ(ERROR_BAD_CONFIG, |
| 472 | manager_->ValidateConfig(kBadKrb5Conf, &error_info)); |
| 473 | EXPECT_EQ(expected_error_info.SerializeAsString(), |
| 474 | error_info.SerializeAsString()); |
| 475 | } |
| 476 | |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 477 | // AcquireTgt() succeeds and writes a credential cache file. |
| 478 | TEST_F(AccountManagerTest, AcquireTgtSuccess) { |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 479 | ignore_result(AddAccount()); |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 480 | |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 481 | EXPECT_EQ(ERROR_NONE, AcquireTgt()); |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 482 | EXPECT_TRUE(base::PathExists(krb5cc_path_)); |
| 483 | } |
| 484 | |
| 485 | // AcquireTgt() triggers KerberosFilesChanged on success. |
| 486 | TEST_F(AccountManagerTest, AcquireTgtTriggersKFCOnSuccess) { |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 487 | ignore_result(AddAccount()); |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 488 | |
| 489 | EXPECT_EQ(0, kerberos_files_changed_count_[kUser]); |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 490 | EXPECT_EQ(ERROR_NONE, AcquireTgt()); |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 491 | EXPECT_EQ(1, kerberos_files_changed_count_[kUser]); |
| 492 | } |
| 493 | |
| 494 | // AcquireTgt() does not trigger KerberosFilesChanged on failure. |
| 495 | TEST_F(AccountManagerTest, AcquireTgtDoesNotTriggerKFCOnFailure) { |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 496 | ignore_result(AddAccount()); |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 497 | |
| 498 | krb5_->set_acquire_tgt_error(ERROR_UNKNOWN); |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 499 | EXPECT_EQ(ERROR_UNKNOWN, AcquireTgt()); |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 500 | EXPECT_EQ(0, kerberos_files_changed_count_[kUser]); |
| 501 | } |
| 502 | |
Lutz Justen | c569050 | 2019-05-24 17:00:54 +0200 | [diff] [blame] | 503 | // AcquireTgt() saves password to disk if |remember_password| is true and |
| 504 | // removes the file again if |remember_password| is false. |
| 505 | TEST_F(AccountManagerTest, AcquireTgtRemembersPasswordsIfWanted) { |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 506 | ignore_result(AddAccount()); |
Lutz Justen | c569050 | 2019-05-24 17:00:54 +0200 | [diff] [blame] | 507 | |
| 508 | EXPECT_FALSE(base::PathExists(password_path_)); |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 509 | EXPECT_EQ(ERROR_NONE, |
| 510 | manager_->AcquireTgt(kUser, kPassword, kRememberPassword, |
| 511 | kDontUseLoginPassword)); |
Lutz Justen | c569050 | 2019-05-24 17:00:54 +0200 | [diff] [blame] | 512 | EXPECT_TRUE(base::PathExists(password_path_)); |
| 513 | |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 514 | EXPECT_EQ(ERROR_NONE, |
| 515 | manager_->AcquireTgt(kUser, kPassword, kDontRememberPassword, |
| 516 | kDontUseLoginPassword)); |
Lutz Justen | c569050 | 2019-05-24 17:00:54 +0200 | [diff] [blame] | 517 | EXPECT_FALSE(base::PathExists(password_path_)); |
| 518 | } |
| 519 | |
| 520 | // AcquireTgt() uses saved password if none is given, no matter if it should be |
| 521 | // remembered again or not. |
| 522 | TEST_F(AccountManagerTest, AcquireTgtLoadsRememberedPassword) { |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 523 | ignore_result(AddAccount()); |
| 524 | ignore_result(manager_->AcquireTgt(kUser, kPassword, kRememberPassword, |
| 525 | kDontUseLoginPassword)); |
Lutz Justen | c569050 | 2019-05-24 17:00:54 +0200 | [diff] [blame] | 526 | |
| 527 | // This should load stored password and keep it. |
| 528 | EXPECT_EQ(ERROR_NONE, |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 529 | manager_->AcquireTgt(kUser, kEmptyPassword, kRememberPassword, |
| 530 | kDontUseLoginPassword)); |
Lutz Justen | c569050 | 2019-05-24 17:00:54 +0200 | [diff] [blame] | 531 | EXPECT_TRUE(base::PathExists(password_path_)); |
| 532 | |
| 533 | // This should load stored password, but erase it afterwards. |
| 534 | EXPECT_EQ(ERROR_NONE, |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 535 | manager_->AcquireTgt(kUser, kEmptyPassword, kDontRememberPassword, |
| 536 | kDontUseLoginPassword)); |
Lutz Justen | c569050 | 2019-05-24 17:00:54 +0200 | [diff] [blame] | 537 | EXPECT_FALSE(base::PathExists(password_path_)); |
| 538 | |
| 539 | // Check that the fake krb5 interface returns an error for a missing password. |
| 540 | // This verifies that the above AcquireTgt() call actually loaded the |
| 541 | // password from disk. |
| 542 | EXPECT_EQ(ERROR_BAD_PASSWORD, |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 543 | manager_->AcquireTgt(kUser, kEmptyPassword, kDontRememberPassword, |
| 544 | kDontUseLoginPassword)); |
| 545 | } |
| 546 | |
| 547 | // AcquireTgt() uses the login password if saved. |
| 548 | TEST_F(AccountManagerTest, AcquireTgtUsesLoginPassword) { |
| 549 | ignore_result(AddAccount()); |
| 550 | |
| 551 | // Shouldn't explode if the login password not set yet. |
| 552 | EXPECT_EQ(ERROR_BAD_PASSWORD, |
| 553 | manager_->AcquireTgt(kUser, kEmptyPassword, kDontRememberPassword, |
| 554 | kUseLoginPassword)); |
| 555 | |
| 556 | SaveLoginPassword(kPassword); |
| 557 | krb5_->set_expected_password(kPassword); |
| 558 | |
| 559 | // Uses the login password. |
| 560 | EXPECT_EQ(ERROR_NONE, |
| 561 | manager_->AcquireTgt(kUser, kEmptyPassword, kDontRememberPassword, |
| 562 | kUseLoginPassword)); |
| 563 | |
| 564 | // Check if auth fails without kUseLoginPassword. |
| 565 | EXPECT_EQ(ERROR_BAD_PASSWORD, |
| 566 | manager_->AcquireTgt(kUser, kEmptyPassword, kDontRememberPassword, |
| 567 | kDontUseLoginPassword)); |
| 568 | } |
| 569 | |
| 570 | // AcquireTgt() wipes a saved password if the login password is used. |
| 571 | TEST_F(AccountManagerTest, AcquireTgtWipesStoredPasswordOnUsesLoginPassword) { |
| 572 | ignore_result(AddAccount()); |
| 573 | ignore_result(manager_->AcquireTgt(kUser, kPassword, kRememberPassword, |
| 574 | kDontUseLoginPassword)); |
| 575 | EXPECT_TRUE(base::PathExists(password_path_)); |
| 576 | |
| 577 | SaveLoginPassword(kPassword); |
| 578 | |
| 579 | // Note: kRememberPassword gets ignored if kUseLoginPassword is passed. |
| 580 | EXPECT_EQ(ERROR_NONE, |
| 581 | manager_->AcquireTgt(kUser, kEmptyPassword, kRememberPassword, |
| 582 | kUseLoginPassword)); |
| 583 | EXPECT_FALSE(base::PathExists(password_path_)); |
| 584 | } |
| 585 | |
| 586 | // AcquireTgt() ignores the passed password if the login password is used. |
| 587 | TEST_F(AccountManagerTest, AcquireTgtIgnoresPassedPasswordOnUsesLoginPassword) { |
| 588 | ignore_result(AddAccount()); |
| 589 | |
| 590 | SaveLoginPassword(kPassword); |
| 591 | krb5_->set_expected_password(kPassword); |
| 592 | |
| 593 | // Auth works despite passed kPassword2 != expected kPassword because the |
| 594 | // login kPassword is used. |
| 595 | EXPECT_EQ(ERROR_NONE, |
| 596 | manager_->AcquireTgt(kUser, kPassword2, kDontRememberPassword, |
| 597 | kUseLoginPassword)); |
Lutz Justen | c569050 | 2019-05-24 17:00:54 +0200 | [diff] [blame] | 598 | } |
| 599 | |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 600 | // RemoveAccount() removes the credential cache file. |
| 601 | TEST_F(AccountManagerTest, RemoveAccountRemovesCC) { |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 602 | ignore_result(AddAccount()); |
| 603 | ignore_result(AcquireTgt()); |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 604 | |
| 605 | EXPECT_TRUE(base::PathExists(krb5cc_path_)); |
| 606 | EXPECT_EQ(ERROR_NONE, manager_->RemoveAccount(kUser)); |
| 607 | EXPECT_FALSE(base::PathExists(krb5cc_path_)); |
| 608 | } |
| 609 | |
Lutz Justen | c569050 | 2019-05-24 17:00:54 +0200 | [diff] [blame] | 610 | // RemoveAccount() removes saved passwords. |
| 611 | TEST_F(AccountManagerTest, RemoveAccountRemovesPassword) { |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 612 | ignore_result(AddAccount()); |
| 613 | ignore_result(manager_->AcquireTgt(kUser, kPassword, kRememberPassword, |
| 614 | kDontUseLoginPassword)); |
Lutz Justen | c569050 | 2019-05-24 17:00:54 +0200 | [diff] [blame] | 615 | |
| 616 | EXPECT_TRUE(base::PathExists(password_path_)); |
| 617 | EXPECT_EQ(ERROR_NONE, manager_->RemoveAccount(kUser)); |
| 618 | EXPECT_FALSE(base::PathExists(password_path_)); |
| 619 | } |
| 620 | |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 621 | // ListAccounts() succeeds and contains the expected data. |
| 622 | TEST_F(AccountManagerTest, ListAccountsSuccess) { |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 623 | ignore_result(manager_->AddAccount(kUser, kManaged)); |
| 624 | ignore_result(SetConfig()); |
| 625 | ignore_result(manager_->AcquireTgt(kUser, kPassword, kRememberPassword, |
| 626 | kDontUseLoginPassword)); |
| 627 | SaveLoginPassword(kPassword); |
| 628 | ignore_result(manager_->AddAccount(kUser2, kUnmanaged)); |
| 629 | // Note: kRememberPassword should be ignored here, see below. |
| 630 | ignore_result(manager_->AcquireTgt(kUser2, kPassword, kRememberPassword, |
| 631 | kUseLoginPassword)); |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 632 | EXPECT_TRUE(base::PathExists(krb5cc_path_)); |
| 633 | |
| 634 | // Set a fake tgt status. |
| 635 | constexpr int kRenewalSeconds = 10; |
| 636 | constexpr int kValiditySeconds = 90; |
Lutz Justen | d925d71 | 2019-06-27 17:34:00 +0200 | [diff] [blame] | 637 | krb5_->set_tgt_status( |
| 638 | Krb5Interface::TgtStatus(kValiditySeconds, kRenewalSeconds)); |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 639 | |
| 640 | // Verify that ListAccounts returns the expected account. |
| 641 | std::vector<Account> accounts; |
| 642 | EXPECT_EQ(ERROR_NONE, manager_->ListAccounts(&accounts)); |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 643 | ASSERT_EQ(2u, accounts.size()); |
| 644 | |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 645 | EXPECT_EQ(kUser, accounts[0].principal_name()); |
| 646 | EXPECT_EQ(kKrb5Conf, accounts[0].krb5conf()); |
| 647 | EXPECT_EQ(kRenewalSeconds, accounts[0].tgt_renewal_seconds()); |
| 648 | EXPECT_EQ(kValiditySeconds, accounts[0].tgt_validity_seconds()); |
| 649 | EXPECT_TRUE(accounts[0].is_managed()); |
Lutz Justen | c569050 | 2019-05-24 17:00:54 +0200 | [diff] [blame] | 650 | EXPECT_TRUE(accounts[0].password_was_remembered()); |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 651 | |
| 652 | EXPECT_EQ(kUser2, accounts[1].principal_name()); |
| 653 | EXPECT_FALSE(accounts[1].password_was_remembered()); |
| 654 | EXPECT_TRUE(accounts[1].use_login_password()); |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | // ListAccounts() ignores failures in GetTgtStatus() and loading the config. |
| 658 | TEST_F(AccountManagerTest, ListAccountsIgnoresFailures) { |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 659 | ignore_result(AddAccount()); |
| 660 | ignore_result(SetConfig()); |
| 661 | ignore_result(AcquireTgt()); |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 662 | EXPECT_TRUE(base::PathExists(krb5cc_path_)); |
| 663 | |
| 664 | // Make reading the config fail. |
| 665 | EXPECT_TRUE(base::SetPosixFilePermissions(krb5conf_path_, 0)); |
| 666 | |
| 667 | // Make GetTgtStatus() fail. |
| 668 | krb5_->set_get_tgt_status_error(ERROR_UNKNOWN); |
| 669 | |
| 670 | // ListAccounts() should still work, despite the errors. |
| 671 | std::vector<Account> accounts; |
| 672 | EXPECT_EQ(ERROR_NONE, manager_->ListAccounts(&accounts)); |
| 673 | ASSERT_EQ(1u, accounts.size()); |
| 674 | EXPECT_EQ(kUser, accounts[0].principal_name()); |
| 675 | |
| 676 | // The config should not be set since we made reading the file fail. |
| 677 | EXPECT_FALSE(accounts[0].has_krb5conf()); |
| 678 | |
| 679 | // tgt_*_seconds should not be set since we made GetTgtStatus() fail. |
| 680 | EXPECT_FALSE(accounts[0].has_tgt_renewal_seconds()); |
| 681 | EXPECT_FALSE(accounts[0].has_tgt_validity_seconds()); |
| 682 | } |
| 683 | |
| 684 | // GetKerberosFiles returns empty KerberosFiles if there is no credential cache, |
| 685 | // even if there is a config. |
| 686 | TEST_F(AccountManagerTest, GetKerberosFilesSucceesWithoutCC) { |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 687 | ignore_result(AddAccount()); |
| 688 | ignore_result(SetConfig()); |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 689 | |
| 690 | KerberosFiles files; |
| 691 | EXPECT_EQ(ERROR_NONE, manager_->GetKerberosFiles(kUser, &files)); |
| 692 | EXPECT_FALSE(files.has_krb5cc()); |
| 693 | EXPECT_FALSE(files.has_krb5conf()); |
| 694 | } |
| 695 | |
| 696 | // GetKerberosFiles returns the expected KerberosFiles if there is a credential |
| 697 | // cache. |
| 698 | TEST_F(AccountManagerTest, GetKerberosFilesSucceesWithCC) { |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 699 | ignore_result(AddAccount()); |
| 700 | ignore_result(SetConfig()); |
| 701 | ignore_result(AcquireTgt()); |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 702 | |
| 703 | KerberosFiles files; |
| 704 | EXPECT_EQ(ERROR_NONE, manager_->GetKerberosFiles(kUser, &files)); |
| 705 | EXPECT_FALSE(files.krb5cc().empty()); |
| 706 | EXPECT_EQ(kKrb5Conf, files.krb5conf()); |
| 707 | } |
| 708 | |
| 709 | // Most methods return ERROR_UNKNOWN_PRINCIPAL if called with such a principal. |
| 710 | TEST_F(AccountManagerTest, MethodsReturnUnknownPrincipal) { |
| 711 | KerberosFiles files; |
| 712 | EXPECT_EQ(ERROR_UNKNOWN_PRINCIPAL_NAME, manager_->RemoveAccount(kUser)); |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 713 | EXPECT_EQ(ERROR_UNKNOWN_PRINCIPAL_NAME, SetConfig()); |
| 714 | EXPECT_EQ(ERROR_UNKNOWN_PRINCIPAL_NAME, AcquireTgt()); |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 715 | EXPECT_EQ(ERROR_UNKNOWN_PRINCIPAL_NAME, |
| 716 | manager_->GetKerberosFiles(kUser, &files)); |
| 717 | } |
| 718 | |
| 719 | // Accounts can be saved to disk and loaded from disk. |
| 720 | TEST_F(AccountManagerTest, SerializationSuccess) { |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 721 | ignore_result(manager_->AddAccount(kUser, kManaged)); |
| 722 | ignore_result(manager_->AcquireTgt(kUser, kPassword, kDontRememberPassword, |
| 723 | kUseLoginPassword)); |
| 724 | |
| 725 | ignore_result(manager_->AddAccount(kUser2, kUnmanaged)); |
| 726 | ignore_result(manager_->AcquireTgt(kUser2, kPassword, kDontRememberPassword, |
| 727 | kDontUseLoginPassword)); |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 728 | |
| 729 | EXPECT_EQ(ERROR_NONE, manager_->SaveAccounts()); |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 730 | AccountManager other_manager( |
| 731 | storage_dir_.GetPath(), kerberos_files_changed_, |
Lutz Justen | 8eeb4a2 | 2019-06-19 17:00:32 +0200 | [diff] [blame] | 732 | kerberos_ticket_expiring_, std::make_unique<FakeKrb5Interface>(), |
Lutz Justen | 2d857e1 | 2019-07-16 15:58:09 +0200 | [diff] [blame^] | 733 | std::make_unique<password_provider::FakePasswordProvider>(), |
| 734 | metrics_.get()); |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 735 | other_manager.LoadAccounts(); |
| 736 | std::vector<Account> accounts; |
Lutz Justen | 82d07ce | 2019-05-16 10:46:14 +0200 | [diff] [blame] | 737 | EXPECT_EQ(ERROR_NONE, other_manager.ListAccounts(&accounts)); |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 738 | ASSERT_EQ(2u, accounts.size()); |
| 739 | |
| 740 | EXPECT_EQ(kUser, accounts[0].principal_name()); |
| 741 | EXPECT_EQ(kUser2, accounts[1].principal_name()); |
| 742 | |
| 743 | EXPECT_TRUE(accounts[0].is_managed()); |
| 744 | EXPECT_FALSE(accounts[1].is_managed()); |
| 745 | |
Lutz Justen | 04afd27 | 2019-06-14 16:31:26 +0200 | [diff] [blame] | 746 | EXPECT_TRUE(accounts[0].use_login_password()); |
| 747 | EXPECT_FALSE(accounts[1].use_login_password()); |
| 748 | |
Lutz Justen | 6f58298 | 2019-05-15 10:24:22 +0200 | [diff] [blame] | 749 | // TODO(https://crbug.com/952239): Check additional Account properties. |
| 750 | } |
Lutz Justen | b7ef580 | 2019-05-14 14:03:20 +0200 | [diff] [blame] | 751 | |
Lutz Justen | d925d71 | 2019-06-27 17:34:00 +0200 | [diff] [blame] | 752 | // The StartObservingTickets() method triggers KerberosTicketExpiring for |
| 753 | // expired signals and starts observing valid tickets. |
| 754 | TEST_F(AccountManagerTest, StartObservingTickets) { |
| 755 | krb5_->set_tgt_status(kValidTgt); |
Lutz Justen | 8eeb4a2 | 2019-06-19 17:00:32 +0200 | [diff] [blame] | 756 | ignore_result(AddAccount()); |
| 757 | ignore_result(SetConfig()); |
| 758 | ignore_result(AcquireTgt()); |
| 759 | EXPECT_EQ(0, kerberos_ticket_expiring_count_[kUser]); |
Lutz Justen | d925d71 | 2019-06-27 17:34:00 +0200 | [diff] [blame] | 760 | task_runner_->ClearPendingTasks(); |
Lutz Justen | 8eeb4a2 | 2019-06-19 17:00:32 +0200 | [diff] [blame] | 761 | |
Lutz Justen | d925d71 | 2019-06-27 17:34:00 +0200 | [diff] [blame] | 762 | // Fake an expired ticket. Check that KerberosTicketExpiring is triggered, but |
| 763 | // no renewal task is scheduled. |
| 764 | krb5_->set_tgt_status(kExpiredTgt); |
| 765 | manager_->StartObservingTickets(); |
Lutz Justen | 8eeb4a2 | 2019-06-19 17:00:32 +0200 | [diff] [blame] | 766 | EXPECT_EQ(1, kerberos_ticket_expiring_count_[kUser]); |
Lutz Justen | d925d71 | 2019-06-27 17:34:00 +0200 | [diff] [blame] | 767 | EXPECT_EQ(0, task_runner_->GetPendingTaskCount()); |
Lutz Justen | 8eeb4a2 | 2019-06-19 17:00:32 +0200 | [diff] [blame] | 768 | |
Lutz Justen | d925d71 | 2019-06-27 17:34:00 +0200 | [diff] [blame] | 769 | // Fake a valid ticket. Check that KerberosTicketExpiring is NOT triggered, |
| 770 | // but a renewal task is scheduled. |
| 771 | krb5_->set_tgt_status(kValidTgt); |
| 772 | EXPECT_EQ(0, task_runner_->GetPendingTaskCount()); |
| 773 | manager_->StartObservingTickets(); |
| 774 | EXPECT_EQ(1, task_runner_->GetPendingTaskCount()); |
Lutz Justen | 8eeb4a2 | 2019-06-19 17:00:32 +0200 | [diff] [blame] | 775 | EXPECT_EQ(1, kerberos_ticket_expiring_count_[kUser]); |
Lutz Justen | d925d71 | 2019-06-27 17:34:00 +0200 | [diff] [blame] | 776 | EXPECT_EQ(0, krb5_->renew_tgt_call_count()); |
| 777 | task_runner_->FastForwardBy(task_runner_->NextPendingTaskDelay()); |
| 778 | EXPECT_EQ(1, krb5_->renew_tgt_call_count()); |
| 779 | } |
| 780 | |
| 781 | // When a TGT is acquired successfully, automatic renewal is scheduled. |
| 782 | TEST_F(AccountManagerTest, AcquireTgtSchedulesRenewalOnSuccess) { |
| 783 | ignore_result(AddAccount()); |
| 784 | |
| 785 | krb5_->set_tgt_status(kValidTgt); |
| 786 | EXPECT_EQ(0, task_runner_->GetPendingTaskCount()); |
| 787 | EXPECT_EQ(ERROR_NONE, AcquireTgt()); |
| 788 | EXPECT_EQ(1, task_runner_->GetPendingTaskCount()); |
| 789 | } |
| 790 | |
| 791 | // When a TGT fails to be acquired, no automatic renewal is scheduled. |
| 792 | TEST_F(AccountManagerTest, AcquireTgtDoesNotScheduleRenewalOnFailure) { |
| 793 | ignore_result(AddAccount()); |
| 794 | |
| 795 | krb5_->set_tgt_status(kValidTgt); |
| 796 | krb5_->set_acquire_tgt_error(ERROR_UNKNOWN); |
| 797 | EXPECT_EQ(0, task_runner_->GetPendingTaskCount()); |
| 798 | EXPECT_EQ(ERROR_UNKNOWN, AcquireTgt()); |
| 799 | EXPECT_EQ(0, task_runner_->GetPendingTaskCount()); |
| 800 | } |
| 801 | |
| 802 | // A scheduled TGT renewal task calls |krb5_->RenewTgt()|. |
| 803 | TEST_F(AccountManagerTest, AutoRenewalCallsRenewTgt) { |
| 804 | krb5_->set_tgt_status(kValidTgt); |
| 805 | ignore_result(AddAccount()); |
| 806 | ignore_result(AcquireTgt()); |
| 807 | int initial_acquire_tgt_call_count = krb5_->acquire_tgt_call_count(); |
| 808 | |
| 809 | // Set some return value for the RenewTgt() call and fast forward to scheduled |
| 810 | // renewal task. |
| 811 | const ErrorType expected_error = ERROR_UNKNOWN; |
| 812 | krb5_->set_renew_tgt_error(expected_error); |
| 813 | RunScheduledRenewalTask(); |
| 814 | |
| 815 | EXPECT_EQ(initial_acquire_tgt_call_count, krb5_->acquire_tgt_call_count()); |
| 816 | EXPECT_EQ(expected_error, manager_->last_renew_tgt_error_for_testing()); |
| 817 | } |
| 818 | |
| 819 | // A scheduled TGT renewal task calls |krb5_->AcquireTgt()| using the login |
| 820 | // password if the call to |krb5_->RenewTgt()| fails and the login password was |
| 821 | // used for the initial AcquireTgt() call. |
| 822 | TEST_F(AccountManagerTest, AutoRenewalUsesLoginPasswordIfRenewalFails) { |
| 823 | krb5_->set_tgt_status(kValidTgt); |
| 824 | ignore_result(AddAccount()); |
| 825 | |
| 826 | // Acquire TGT with login password. |
| 827 | SaveLoginPassword(kPassword); |
| 828 | krb5_->set_expected_password(kPassword); |
| 829 | EXPECT_EQ(ERROR_NONE, |
| 830 | manager_->AcquireTgt(kUser, std::string(), kDontRememberPassword, |
| 831 | kUseLoginPassword)); |
| 832 | int initial_acquire_tgt_call_count = krb5_->acquire_tgt_call_count(); |
| 833 | |
| 834 | krb5_->set_renew_tgt_error(ERROR_UNKNOWN); |
| 835 | RunScheduledRenewalTask(); |
| 836 | |
| 837 | // The scheduled renewal task should have called AcquireTgt() with the login |
| 838 | // password and succeeded. |
| 839 | EXPECT_EQ(initial_acquire_tgt_call_count + 1, |
| 840 | krb5_->acquire_tgt_call_count()); |
| 841 | EXPECT_EQ(ERROR_NONE, manager_->last_renew_tgt_error_for_testing()); |
| 842 | } |
| 843 | |
| 844 | // A scheduled TGT renewal task calls |krb5_->AcquireTgt()| using the remembered |
| 845 | // password if the call to |krb5_->RenewTgt()| fails and the password was |
| 846 | // remembered for the initial AcquireTgt() call. |
| 847 | TEST_F(AccountManagerTest, AutoRenewalUsesRememberedPasswordIfRenewalFails) { |
| 848 | krb5_->set_tgt_status(kValidTgt); |
| 849 | ignore_result(AddAccount()); |
| 850 | |
| 851 | // Acquire TGT and remember password. |
| 852 | krb5_->set_expected_password(kPassword); |
| 853 | EXPECT_EQ(ERROR_NONE, |
| 854 | manager_->AcquireTgt(kUser, kPassword, kRememberPassword, |
| 855 | kDontUseLoginPassword)); |
| 856 | int initial_acquire_tgt_call_count = krb5_->acquire_tgt_call_count(); |
| 857 | |
| 858 | krb5_->set_renew_tgt_error(ERROR_UNKNOWN); |
| 859 | RunScheduledRenewalTask(); |
| 860 | |
| 861 | // The scheduled renewal task should have called AcquireTgt() with the |
| 862 | // remembered password and succeeded. |
| 863 | EXPECT_EQ(initial_acquire_tgt_call_count + 1, |
| 864 | krb5_->acquire_tgt_call_count()); |
| 865 | EXPECT_EQ(ERROR_NONE, manager_->last_renew_tgt_error_for_testing()); |
| 866 | } |
| 867 | |
| 868 | // A scheduled TGT renewal task does not call |krb5_->AcquireTgt()| using the |
| 869 | // remembered password if the call to |krb5_->RenewTgt()| succeeds and the |
| 870 | // password was remembered for the initial AcquireTgt() call (similar for login |
| 871 | // password, but we don't test that). |
| 872 | TEST_F(AccountManagerTest, AutoRenewalDoesNotCallAcquireTgtIfRenewalSucceeds) { |
| 873 | krb5_->set_tgt_status(kValidTgt); |
| 874 | ignore_result(AddAccount()); |
| 875 | |
| 876 | // Acquire TGT and remember password. |
| 877 | krb5_->set_expected_password(kPassword); |
| 878 | EXPECT_EQ(ERROR_NONE, |
| 879 | manager_->AcquireTgt(kUser, kPassword, kRememberPassword, |
| 880 | kDontUseLoginPassword)); |
| 881 | int initial_acquire_tgt_call_count = krb5_->acquire_tgt_call_count(); |
| 882 | |
| 883 | krb5_->set_renew_tgt_error(ERROR_NONE); |
| 884 | RunScheduledRenewalTask(); |
| 885 | |
| 886 | // The scheduled renewal task should NOT have called AcquireTgt() again since |
| 887 | // |krb5_->RenewTgt()|. |
| 888 | EXPECT_EQ(initial_acquire_tgt_call_count, krb5_->acquire_tgt_call_count()); |
| 889 | EXPECT_EQ(ERROR_NONE, manager_->last_renew_tgt_error_for_testing()); |
Lutz Justen | 8eeb4a2 | 2019-06-19 17:00:32 +0200 | [diff] [blame] | 890 | } |
| 891 | |
Lutz Justen | 3360180 | 2019-07-03 22:40:57 +0200 | [diff] [blame] | 892 | // Verifies that all files written have the expected access permissions. |
| 893 | // Unfortunately, file ownership can't be tested as the test won't run as |
| 894 | // kerberosd user nor can it switch to it. |
| 895 | TEST_F(AccountManagerTest, FilePermissions) { |
| 896 | constexpr int kFileMode_rw = |
| 897 | base::FILE_PERMISSION_READ_BY_USER | base::FILE_PERMISSION_WRITE_BY_USER; |
| 898 | constexpr int kFileMode_rw_r = |
| 899 | kFileMode_rw | base::FILE_PERMISSION_READ_BY_GROUP; |
| 900 | constexpr int kFileMode_rw_r__r = |
| 901 | kFileMode_rw_r | base::FILE_PERMISSION_READ_BY_OTHERS; |
| 902 | constexpr int kFileMode_rwxrwx = |
| 903 | base::FILE_PERMISSION_USER_MASK | base::FILE_PERMISSION_GROUP_MASK; |
| 904 | |
| 905 | // Wrap the fake krb5 in a jail wrapper to get the file permissions of krb5cc |
| 906 | // right. Note that we can't use a Krb5JailWrapper for the whole test since |
| 907 | // that would break the counters in FakeKrb5Interface (they would be inc'ed in |
| 908 | // another process!). |
| 909 | manager_->WrapKrb5ForTesting(); |
| 910 | |
| 911 | // Can't set user in this test. |
| 912 | Krb5JailWrapper::DisableChangeUserForTesting(true); |
| 913 | |
| 914 | EXPECT_EQ(ERROR_NONE, AddAccount()); |
| 915 | EXPECT_EQ(ERROR_NONE, SetConfig()); |
| 916 | EXPECT_EQ(ERROR_NONE, |
| 917 | manager_->AcquireTgt(kUser, kPassword, kRememberPassword, |
| 918 | kDontUseLoginPassword)); |
| 919 | |
| 920 | int mode; |
| 921 | |
| 922 | EXPECT_TRUE(GetPosixFilePermissions(accounts_path_, &mode)); |
| 923 | EXPECT_EQ(kFileMode_rw, mode); |
| 924 | |
| 925 | EXPECT_TRUE(GetPosixFilePermissions(account_dir_, &mode)); |
| 926 | EXPECT_EQ(kFileMode_rwxrwx, mode); |
| 927 | |
| 928 | EXPECT_TRUE(GetPosixFilePermissions(krb5cc_path_, &mode)); |
| 929 | EXPECT_EQ(kFileMode_rw_r, mode); |
| 930 | |
| 931 | EXPECT_TRUE(GetPosixFilePermissions(krb5conf_path_, &mode)); |
| 932 | EXPECT_EQ(kFileMode_rw_r__r, mode); |
| 933 | |
| 934 | EXPECT_TRUE(GetPosixFilePermissions(password_path_, &mode)); |
| 935 | EXPECT_EQ(kFileMode_rw, mode); |
| 936 | } |
| 937 | |
Lutz Justen | 2d857e1 | 2019-07-16 15:58:09 +0200 | [diff] [blame^] | 938 | // Tests that [Should]ReportDailyUsageStats is called as advertised. |
| 939 | TEST_F(AccountManagerTest, ReportDailyUsageStats) { |
| 940 | // ShouldReportDailyUsageStats() should be called by GetKerberosFiles() even |
| 941 | // if there is no account, and if that returns true, ReportDailyUsageStats() |
| 942 | // should be called as well. |
| 943 | EXPECT_CALL(*metrics_, ShouldReportDailyUsageStats()).WillOnce(Return(true)); |
| 944 | EXPECT_CALL(*metrics_, ReportDailyUsageStats(0, 0, 0, 0, 0)); |
| 945 | KerberosFiles files; |
| 946 | EXPECT_EQ(ERROR_UNKNOWN_PRINCIPAL_NAME, |
| 947 | manager_->GetKerberosFiles(kUser, &files)); |
| 948 | Mock::VerifyAndClearExpectations(metrics_.get()); |
| 949 | |
| 950 | AddAccount(); |
| 951 | |
| 952 | // ShouldReportDailyUsageStats() should be called by AcquireTgt(), but if that |
| 953 | // returns false, ReportDailyUsageStats() should NOT be called. |
| 954 | EXPECT_CALL(*metrics_, ShouldReportDailyUsageStats()).WillOnce(Return(false)); |
| 955 | EXPECT_CALL(*metrics_, ReportDailyUsageStats(_, _, _, _, _)).Times(0); |
| 956 | AcquireTgt(); |
| 957 | Mock::VerifyAndClearExpectations(metrics_.get()); |
| 958 | |
| 959 | // ShouldReportDailyUsageStats() should be called by AcquireTgt(), and if that |
| 960 | // returns true, ReportDailyUsageStats() should be called as well. |
| 961 | EXPECT_CALL(*metrics_, ShouldReportDailyUsageStats()).WillOnce(Return(true)); |
| 962 | EXPECT_CALL(*metrics_, ReportDailyUsageStats(_, _, _, _, _)); |
| 963 | AcquireTgt(); |
| 964 | Mock::VerifyAndClearExpectations(metrics_.get()); |
| 965 | } |
| 966 | |
| 967 | TEST_F(AccountManagerTest, AccountStats) { |
| 968 | SaveLoginPassword(kPassword); |
| 969 | |
| 970 | EXPECT_EQ(ERROR_NONE, manager_->AddAccount(kUser, kManaged)); |
| 971 | EXPECT_EQ(ERROR_NONE, manager_->AddAccount(kUser2, kManaged)); |
| 972 | EXPECT_EQ(ERROR_NONE, manager_->AddAccount(kUser3, kUnmanaged)); |
| 973 | |
| 974 | // Set metrics up so that the stats are reported on the last call. |
| 975 | EXPECT_CALL(*metrics_, ShouldReportDailyUsageStats()) |
| 976 | .WillOnce(Return(false)) |
| 977 | .WillOnce(Return(false)) |
| 978 | .WillOnce(Return(true)); |
| 979 | |
| 980 | // 3 accounts total, 2 managed, 1 unmanaged, 1 remembering the password, 1 |
| 981 | // using the login password. |
| 982 | EXPECT_CALL(*metrics_, ReportDailyUsageStats(3, 2, 1, 1, 1)); |
| 983 | |
| 984 | EXPECT_EQ(ERROR_NONE, |
| 985 | manager_->AcquireTgt(kUser, kPassword, kDontRememberPassword, |
| 986 | kDontUseLoginPassword)); |
| 987 | EXPECT_EQ(ERROR_NONE, |
| 988 | manager_->AcquireTgt(kUser2, kPassword, kRememberPassword, |
| 989 | kDontUseLoginPassword)); |
| 990 | EXPECT_EQ(ERROR_NONE, |
| 991 | manager_->AcquireTgt(kUser3, kEmptyPassword, kDontRememberPassword, |
| 992 | kUseLoginPassword)); |
| 993 | } |
| 994 | |
Lutz Justen | b7ef580 | 2019-05-14 14:03:20 +0200 | [diff] [blame] | 995 | } // namespace kerberos |