Commit f3d5644c authored by Lutz Justen's avatar Lutz Justen Committed by Commit Bot

Add unit test for KerberosCredentialsManager

First batch of KerberosCredentialsManager unit tests. Also adds a test
interface to FakeKerberosClient to
- Set the task delay (tests set it to 0 to speed up the tests),
- Record which functions are called for testing.

BUG=chromium:952251
TEST=Unit tests succeed

Change-Id: I14b3790d1dfb9809e0bc6b7b59a9b58d196e1929
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1760951
Commit-Queue: Lutz Justen <ljusten@chromium.org>
Reviewed-by: default avatarRoman Sorokin [CET] <rsorokin@chromium.org>
Reviewed-by: default avatarLutz Justen <ljusten@chromium.org>
Auto-Submit: Lutz Justen <ljusten@chromium.org>
Cr-Commit-Position: refs/heads/master@{#693140}
parent 44920976
...@@ -2580,6 +2580,7 @@ source_set("unit_tests") { ...@@ -2580,6 +2580,7 @@ source_set("unit_tests") {
"input_method/input_method_engine_unittest.cc", "input_method/input_method_engine_unittest.cc",
"input_method/input_method_manager_impl_unittest.cc", "input_method/input_method_manager_impl_unittest.cc",
"input_method/input_method_persistence_unittest.cc", "input_method/input_method_persistence_unittest.cc",
"kerberos/kerberos_credentials_manager_test.cc",
"kerberos/kerberos_ticket_expiry_notification_test.cc", "kerberos/kerberos_ticket_expiry_notification_test.cc",
"locale_change_guard_unittest.cc", "locale_change_guard_unittest.cc",
"lock_screen_apps/app_manager_impl_unittest.cc", "lock_screen_apps/app_manager_impl_unittest.cc",
......
...@@ -741,7 +741,7 @@ void KerberosCredentialsManager::DoValidateActivePrincipal( ...@@ -741,7 +741,7 @@ void KerberosCredentialsManager::DoValidateActivePrincipal(
found |= response.accounts(n).principal_name() == active_principal; found |= response.accounts(n).principal_name() == active_principal;
if (!found) { if (!found) {
LOG(ERROR) << "Active principal got removed. Restoring."; VLOG(1) << "Active principal got removed. Restoring.";
if (response.accounts_size() > 0) if (response.accounts_size() > 0)
SetActivePrincipalName(response.accounts(0).principal_name()); SetActivePrincipalName(response.accounts(0).principal_name());
else else
...@@ -803,8 +803,8 @@ void KerberosCredentialsManager::UpdateAccountsFromPref() { ...@@ -803,8 +803,8 @@ void KerberosCredentialsManager::UpdateAccountsFromPref() {
NotifyRequiresLoginPassword(false); NotifyRequiresLoginPassword(false);
// https://crbug.com/963824: The active principal is empty if there are no // https://crbug.com/963824: The active principal is empty if there are no
// accounts, so no need to remove accounts. It would just up the daemon // accounts, so no need to remove accounts. It would just start up the
// unnecessarily. // daemon unnecessarily.
if (!GetActivePrincipalName().empty()) if (!GetActivePrincipalName().empty())
RemoveAllManagedAccountsExcept({}); RemoveAllManagedAccountsExcept({});
return; return;
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include <unordered_set> #include <unordered_set>
#include <vector> #include <vector>
#include "base/optional.h"
#include "chromeos/dbus/kerberos/kerberos_client.h" #include "chromeos/dbus/kerberos/kerberos_client.h"
#include "chromeos/dbus/kerberos/kerberos_service.pb.h" #include "chromeos/dbus/kerberos/kerberos_service.pb.h"
#include "dbus/object_proxy.h" #include "dbus/object_proxy.h"
...@@ -17,7 +18,8 @@ ...@@ -17,7 +18,8 @@
namespace chromeos { namespace chromeos {
class COMPONENT_EXPORT(CHROMEOS_DBUS) FakeKerberosClient class COMPONENT_EXPORT(CHROMEOS_DBUS) FakeKerberosClient
: public KerberosClient { : public KerberosClient,
public KerberosClient::TestInterface {
public: public:
FakeKerberosClient(); FakeKerberosClient();
~FakeKerberosClient() override; ~FakeKerberosClient() override;
...@@ -44,6 +46,12 @@ class COMPONENT_EXPORT(CHROMEOS_DBUS) FakeKerberosClient ...@@ -44,6 +46,12 @@ class COMPONENT_EXPORT(CHROMEOS_DBUS) FakeKerberosClient
KerberosFilesChangedCallback callback) override; KerberosFilesChangedCallback callback) override;
void ConnectToKerberosTicketExpiringSignal( void ConnectToKerberosTicketExpiringSignal(
KerberosTicketExpiringCallback callback) override; KerberosTicketExpiringCallback callback) override;
KerberosClient::TestInterface* GetTestInterface() override;
// KerberosClient::TestInterface:
void SetTaskDelay(base::TimeDelta delay) override;
void StartRecordingFunctionCalls() override;
std::string StopRecordingAndGetRecordedFunctionCalls() override;
private: private:
struct AccountData { struct AccountData {
...@@ -83,9 +91,18 @@ class COMPONENT_EXPORT(CHROMEOS_DBUS) FakeKerberosClient ...@@ -83,9 +91,18 @@ class COMPONENT_EXPORT(CHROMEOS_DBUS) FakeKerberosClient
// otherwise. // otherwise.
AccountData* GetAccountData(const std::string& principal_name); AccountData* GetAccountData(const std::string& principal_name);
// Appends |function_name| to |recorded_function_calls_| if the latter is set.
void MaybeRecordFunctionCallForTesting(const char* function_name);
// Maps principal name (user@REALM.COM) to account data. // Maps principal name (user@REALM.COM) to account data.
std::vector<AccountData> accounts_; std::vector<AccountData> accounts_;
// For recording which methods have been called (for testing).
base::Optional<std::string> recorded_function_calls_;
// Fake delay for any asynchronous operation.
base::TimeDelta mTaskDelay = base::TimeDelta::FromMilliseconds(100);
KerberosFilesChangedCallback kerberos_files_changed_callback_; KerberosFilesChangedCallback kerberos_files_changed_callback_;
KerberosTicketExpiringCallback kerberos_ticket_expiring_callback_; KerberosTicketExpiringCallback kerberos_ticket_expiring_callback_;
......
...@@ -170,6 +170,8 @@ class KerberosClientImpl : public KerberosClient { ...@@ -170,6 +170,8 @@ class KerberosClientImpl : public KerberosClient {
} }
private: private:
TestInterface* GetTestInterface() override { return nullptr; }
// Calls kerberosd's |method_name| method, passing in |request| as input. Once // Calls kerberosd's |method_name| method, passing in |request| as input. Once
// the (asynchronous) call finishes, |callback| is called with the response // the (asynchronous) call finishes, |callback| is called with the response
// proto (on the same thread as this call). // proto (on the same thread as this call).
......
...@@ -43,6 +43,25 @@ class COMPONENT_EXPORT(KERBEROS) KerberosClient { ...@@ -43,6 +43,25 @@ class COMPONENT_EXPORT(KERBEROS) KerberosClient {
using KerberosTicketExpiringCallback = using KerberosTicketExpiringCallback =
base::RepeatingCallback<void(const std::string& principal_name)>; base::RepeatingCallback<void(const std::string& principal_name)>;
// Interface with testing functionality. Accessed through GetTestInterface(),
// only implemented in the fake implementation.
class TestInterface {
public:
// Sets the artificial delay for asynchronous function calls.
// Should be set to 0 for tests.
virtual void SetTaskDelay(base::TimeDelta delay) = 0;
// Starts recording which functions are called.
virtual void StartRecordingFunctionCalls() = 0;
// Stops recording which functions are called and returns calls as a
// comma separated list, e.g. "AddAccount,ListAccounts".
virtual std::string StopRecordingAndGetRecordedFunctionCalls() = 0;
protected:
virtual ~TestInterface() {}
};
// Creates and initializes the global instance. |bus| must not be null. // Creates and initializes the global instance. |bus| must not be null.
static void Initialize(dbus::Bus* bus); static void Initialize(dbus::Bus* bus);
...@@ -91,6 +110,9 @@ class COMPONENT_EXPORT(KERBEROS) KerberosClient { ...@@ -91,6 +110,9 @@ class COMPONENT_EXPORT(KERBEROS) KerberosClient {
virtual void ConnectToKerberosTicketExpiringSignal( virtual void ConnectToKerberosTicketExpiringSignal(
KerberosTicketExpiringCallback callback) = 0; KerberosTicketExpiringCallback callback) = 0;
// Returns an interface for testing (fake only), or returns nullptr.
virtual TestInterface* GetTestInterface() = 0;
protected: protected:
// Initialize/Shutdown should be used instead. // Initialize/Shutdown should be used instead.
KerberosClient(); KerberosClient();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment