Commit 812664fa authored by Roman Sorokin's avatar Roman Sorokin Committed by Commit Bot

AuthPolicyClient: Add WaitForServiceToBeAvailable method

BUG=chromium:843177
TEST=FakeAuthPolicyClientTest.WaitForServiceToBeAvailableCalled
TBR=alemate@chromium.org,pmarko@chromium.org

Change-Id: I83004c7a4910d96dd9b71849326dd2c92186692a
Reviewed-on: https://chromium-review.googlesource.com/1060053
Commit-Queue: Roman Sorokin <rsorokin@chromium.org>
Reviewed-by: default avatarRyo Hashimoto <hashimoto@chromium.org>
Cr-Commit-Position: refs/heads/master@{#565619}
parent 40c20642
......@@ -71,7 +71,7 @@ constexpr char kCloseButtonId[] = "closeButton";
class TestAuthPolicyClient : public FakeAuthPolicyClient {
public:
TestAuthPolicyClient() { FakeAuthPolicyClient::set_started(true); }
TestAuthPolicyClient() { FakeAuthPolicyClient::SetStarted(true); }
void AuthenticateUser(const authpolicy::AuthenticateUserRequest& request,
int password_fd,
......
......@@ -69,6 +69,12 @@ class TestAuthPolicyClient : public chromeos::AuthPolicyClient {
NOTIMPLEMENTED();
}
void WaitForServiceToBeAvailable(
dbus::ObjectProxy::WaitForServiceToBeAvailableCallback callback)
override {
NOTIMPLEMENTED();
}
void SetRefreshUserPolicyCallbackError(authpolicy::ErrorType error) {
refresh_user_policy_callback_error_ = error;
}
......
......@@ -159,6 +159,12 @@ class AuthPolicyClientImpl : public AuthPolicyClient {
std::move(on_connected_callback));
}
void WaitForServiceToBeAvailable(
dbus::ObjectProxy::WaitForServiceToBeAvailableCallback callback)
override {
proxy_->WaitForServiceToBeAvailable(std::move(callback));
}
protected:
void Init(dbus::Bus* bus) override {
bus_ = bus;
......
......@@ -87,6 +87,9 @@ class CHROMEOS_EXPORT AuthPolicyClient : public DBusClient {
dbus::ObjectProxy::SignalCallback signal_callback,
dbus::ObjectProxy::OnConnectedCallback on_connected_callback) = 0;
virtual void WaitForServiceToBeAvailable(
dbus::ObjectProxy::WaitForServiceToBeAvailableCallback callback) = 0;
protected:
// Create() should be used instead.
AuthPolicyClient();
......
......@@ -260,6 +260,27 @@ void FakeAuthPolicyClient::ConnectToSignal(
dbus_operation_delay_);
}
void FakeAuthPolicyClient::WaitForServiceToBeAvailable(
dbus::ObjectProxy::WaitForServiceToBeAvailableCallback callback) {
if (started_) {
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE,
base::BindOnce(std::move(callback), true /* service_is_available */));
return;
}
wait_for_service_to_be_available_callbacks_.push_back(std::move(callback));
}
void FakeAuthPolicyClient::SetStarted(bool started) {
started_ = started;
if (started_) {
std::vector<WaitForServiceToBeAvailableCallback> callbacks;
callbacks.swap(wait_for_service_to_be_available_callbacks_);
for (size_t i = 0; i < callbacks.size(); ++i)
std::move(callbacks[i]).Run(true /* service_is_available*/);
}
}
void FakeAuthPolicyClient::OnDevicePolicyRetrieved(
RefreshPolicyCallback callback,
SessionManagerClient::RetrievePolicyResponseType response_type,
......
......@@ -65,10 +65,14 @@ class CHROMEOS_EXPORT FakeAuthPolicyClient : public AuthPolicyClient {
dbus::ObjectProxy::SignalCallback signal_callback,
dbus::ObjectProxy::OnConnectedCallback on_connected_callback) override;
void WaitForServiceToBeAvailable(
dbus::ObjectProxy::WaitForServiceToBeAvailableCallback callback) override;
// Mark service as started. It's getting started by the
// UpstartClient::StartAuthPolicyService on the Active Directory managed
// devices.
void set_started(bool started) { started_ = started; }
// devices. If |started| is true, it triggers calling
// |wait_for_service_to_be_available_callbacks_|.
void SetStarted(bool started);
bool started() const { return started_; }
......@@ -133,6 +137,9 @@ class CHROMEOS_EXPORT FakeAuthPolicyClient : public AuthPolicyClient {
base::TimeDelta::FromMilliseconds(100);
enterprise_management::ChromeDeviceSettingsProto device_policy_;
std::vector<WaitForServiceToBeAvailableCallback>
wait_for_service_to_be_available_callbacks_;
base::WeakPtrFactory<FakeAuthPolicyClient> weak_factory_{this};
DISALLOW_COPY_AND_ASSIGN(FakeAuthPolicyClient);
......
......@@ -89,6 +89,19 @@ class FakeAuthPolicyClientTest : public ::testing::Test {
std::string()));
}
void WaitForServiceToBeAvailable() {
authpolicy_client()->WaitForServiceToBeAvailable(base::BindOnce(
&FakeAuthPolicyClientTest::OnWaitForServiceToBeAvailableCalled,
base::Unretained(this)));
}
void OnWaitForServiceToBeAvailableCalled(bool is_service_available) {
EXPECT_TRUE(is_service_available);
service_is_available_called_num_++;
}
int service_is_available_called_num_ = 0;
private:
FakeAuthPolicyClient* auth_policy_client_ptr_; // not owned.
FakeSessionManagerClient* session_manager_client_ptr_; // not owned.
......@@ -99,7 +112,7 @@ class FakeAuthPolicyClientTest : public ::testing::Test {
// Tests parsing machine name.
TEST_F(FakeAuthPolicyClientTest, JoinAdDomain_ParseMachineName) {
authpolicy_client()->set_started(true);
authpolicy_client()->SetStarted(true);
JoinAdDomain("correct_length1", kCorrectUserName,
base::BindOnce(
[](authpolicy::ErrorType error, const std::string& domain) {
......@@ -139,7 +152,7 @@ TEST_F(FakeAuthPolicyClientTest, JoinAdDomain_ParseMachineName) {
// Tests join to a different machine domain.
TEST_F(FakeAuthPolicyClientTest, JoinAdDomain_MachineDomain) {
authpolicy_client()->set_started(true);
authpolicy_client()->SetStarted(true);
JoinAdDomainWithMachineDomain(kCorrectMachineName, kMachineDomain,
kCorrectUserName,
base::BindOnce([](authpolicy::ErrorType error,
......@@ -163,7 +176,7 @@ TEST_F(FakeAuthPolicyClientTest, JoinAdDomain_MachineDomain) {
// Tests parsing user name.
TEST_F(FakeAuthPolicyClientTest, JoinAdDomain_ParseUPN) {
authpolicy_client()->set_started(true);
authpolicy_client()->SetStarted(true);
JoinAdDomain(kCorrectMachineName, kCorrectUserName,
base::BindOnce(
[](authpolicy::ErrorType error, const std::string& domain) {
......@@ -209,7 +222,7 @@ TEST_F(FakeAuthPolicyClientTest, JoinAdDomain_ParseUPN) {
// Tests that fake server does not support legacy encryption types.
TEST_F(FakeAuthPolicyClientTest, JoinAdDomain_NotSupportedEncType) {
authpolicy_client()->set_started(true);
authpolicy_client()->SetStarted(true);
base::RunLoop loop;
authpolicy::JoinDomainRequest request;
request.set_machine_name(kCorrectMachineName);
......@@ -232,7 +245,7 @@ TEST_F(FakeAuthPolicyClientTest, JoinAdDomain_NotSupportedEncType) {
// Test AuthenticateUser.
TEST_F(FakeAuthPolicyClientTest, AuthenticateUser_ByAccountId) {
authpolicy_client()->set_started(true);
authpolicy_client()->SetStarted(true);
LockDeviceActiveDirectory();
// Check that account_id do not change.
AuthenticateUser(
......@@ -279,7 +292,7 @@ TEST_F(FakeAuthPolicyClientTest, NotStartedAuthPolicyService) {
// Tests RefreshDevicePolicy. On a not locked device it should cache policy. On
// a locked device it should send policy to session_manager.
TEST_F(FakeAuthPolicyClientTest, NotLockedDeviceCachesPolicy) {
authpolicy_client()->set_started(true);
authpolicy_client()->SetStarted(true);
authpolicy_client()->RefreshDevicePolicy(
base::BindOnce([](authpolicy::ErrorType error) {
EXPECT_EQ(authpolicy::ERROR_DEVICE_POLICY_CACHED_BUT_NOT_SENT, error);
......@@ -297,7 +310,7 @@ TEST_F(FakeAuthPolicyClientTest, NotLockedDeviceCachesPolicy) {
// Tests that RefreshDevicePolicy stores device policy in the session manager.
TEST_F(FakeAuthPolicyClientTest, RefreshDevicePolicyStoresPolicy) {
authpolicy_client()->set_started(true);
authpolicy_client()->SetStarted(true);
LockDeviceActiveDirectory();
{
......@@ -335,4 +348,17 @@ TEST_F(FakeAuthPolicyClientTest, RefreshDevicePolicyStoresPolicy) {
}
}
TEST_F(FakeAuthPolicyClientTest, WaitForServiceToBeAvailableCalled) {
WaitForServiceToBeAvailable();
WaitForServiceToBeAvailable();
base::RunLoop().RunUntilIdle();
EXPECT_EQ(0, service_is_available_called_num_);
authpolicy_client()->SetStarted(true);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(2, service_is_available_called_num_);
WaitForServiceToBeAvailable();
base::RunLoop().RunUntilIdle();
EXPECT_EQ(3, service_is_available_called_num_);
}
} // namespace chromeos
......@@ -20,7 +20,7 @@ void FakeUpstartClient::Init(dbus::Bus* bus) {}
void FakeUpstartClient::StartAuthPolicyService() {
static_cast<FakeAuthPolicyClient*>(
DBusThreadManager::Get()->GetAuthPolicyClient())
->set_started(true);
->SetStarted(true);
}
void FakeUpstartClient::RestartAuthPolicyService() {
......@@ -28,7 +28,7 @@ void FakeUpstartClient::RestartAuthPolicyService() {
DBusThreadManager::Get()->GetAuthPolicyClient());
DLOG_IF(WARNING, !authpolicy_client->started())
<< "Trying to restart authpolicyd which is not started";
authpolicy_client->set_started(true);
authpolicy_client->SetStarted(true);
}
void FakeUpstartClient::StartMediaAnalytics(
......
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