Chromium Code Reviews| Index: components/policy/core/common/cloud/cloud_policy_client_unittest.cc |
| diff --git a/components/policy/core/common/cloud/cloud_policy_client_unittest.cc b/components/policy/core/common/cloud/cloud_policy_client_unittest.cc |
| index dc9faf0b5d09063131891c5c606e383495d5e8ed..5123ef09bfe56f4f7fe70f734a562221287224c9 100644 |
| --- a/components/policy/core/common/cloud/cloud_policy_client_unittest.cc |
| +++ b/components/policy/core/common/cloud/cloud_policy_client_unittest.cc |
| @@ -4,6 +4,8 @@ |
| #include "components/policy/core/common/cloud/cloud_policy_client.h" |
| +#include <stdint.h> |
| + |
| #include <map> |
| #include <set> |
| @@ -21,6 +23,7 @@ |
| #include "testing/gmock/include/gmock/gmock.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| +using testing::ElementsAre; |
| using testing::Mock; |
| using testing::Return; |
| using testing::SaveArg; |
| @@ -41,6 +44,11 @@ const char kDMToken[] = "fake-dm-token"; |
| const char kDeviceCertificate[] = "fake-device-certificate"; |
| const char kRequisition[] = "fake-requisition"; |
| const char kStateKey[] = "fake-state-key"; |
| +const char kPayload[] = "input_payload"; |
| +const char kResultPayLoad[] = "output_payload"; |
|
bartfab (slow)
2015/02/18 11:12:53
Nit: s/PayLoad/Payload/
binjin
2015/02/18 15:37:50
Done.
|
| + |
| +const int64_t kLastCommandId = 123456789; |
| +const int64_t kTimeStamp = 987654321; |
|
bartfab (slow)
2015/02/18 11:12:53
Nit: s/TimeStamp/Timestamp/
binjin
2015/02/18 15:37:50
Done.
|
| MATCHER_P(MatchProto, expected, "matches protobuf") { |
| return arg.SerializePartialAsString() == expected.SerializePartialAsString(); |
| @@ -55,6 +63,17 @@ class MockUploadObserver { |
| MOCK_METHOD1(OnUploadComplete, void(bool)); |
| }; |
| +// A mock class to allow us to set expectations on remote commands fetch |
| +// callbacks. |
| +class MockRemoteCommandsObserver { |
| + public: |
| + MockRemoteCommandsObserver() {} |
| + virtual ~MockRemoteCommandsObserver() {} |
|
bartfab (slow)
2015/02/18 11:12:53
Nit 1: No need for virtual.
Nit 2: No need for an
binjin
2015/02/18 15:37:50
Done.
|
| + |
| + MOCK_METHOD1(OnRemoteCommandsFetched, |
| + void(const std::vector<em::RemoteCommand>&)); |
| +}; |
| + |
| } // namespace |
| class CloudPolicyClientTest : public testing::Test { |
| @@ -88,13 +107,32 @@ class CloudPolicyClientTest : public testing::Test { |
| upload_status_request_.mutable_device_status_report_request(); |
| upload_status_request_.mutable_session_status_report_request(); |
| + |
| + remote_command_request_.mutable_remote_command_request() |
| + ->set_last_command_unique_id(kLastCommandId); |
| + em::RemoteCommandResult* command_result = |
| + remote_command_request_.mutable_remote_command_request() |
| + ->add_command_results(); |
| + command_result->set_unique_id(kLastCommandId); |
| + command_result->set_result( |
| + em::RemoteCommandResult_ResultType_RESULT_SUCCESS); |
| + command_result->set_payload(kResultPayLoad); |
| + command_result->set_timestamp(kTimeStamp); |
| + |
| + em::RemoteCommand* command = |
| + remote_command_response_.mutable_remote_command_response() |
| + ->add_commands(); |
| + command->set_timestamp(kTimeStamp + 1); |
| + command->set_payload(kPayload); |
| + command->set_unique_id(kLastCommandId + 1); |
| + command->set_type(em::RemoteCommand_Type_COMMAND_ECHO_TEST); |
| } |
| - virtual void SetUp() override { |
| + void SetUp() override { |
| CreateClient(USER_AFFILIATION_NONE); |
| } |
| - virtual void TearDown() override { |
| + void TearDown() override { |
| client_->RemoveObserver(&observer_); |
| } |
| @@ -175,6 +213,17 @@ class CloudPolicyClientTest : public testing::Test { |
| client_id_, MatchProto(upload_status_request_))); |
| } |
| + void ExpectFetchRemoteCommands() { |
| + EXPECT_CALL(service_, |
| + CreateJob(DeviceManagementRequestJob::TYPE_REMOTE_COMMANDS, |
| + request_context_)) |
| + .WillOnce(service_.SucceedJob(remote_command_response_)); |
| + EXPECT_CALL(service_, |
| + StartJob(dm_protocol::kValueRequestRemoteCommands, |
| + std::string(), std::string(), kDMToken, std::string(), |
| + client_id_, MatchProto(remote_command_request_))); |
| + } |
| + |
| void CheckPolicyResponse() { |
| ASSERT_TRUE(client_->GetPolicyFor(policy_type_, std::string())); |
| EXPECT_THAT(*client_->GetPolicyFor(policy_type_, std::string()), |
| @@ -194,6 +243,7 @@ class CloudPolicyClientTest : public testing::Test { |
| em::DeviceManagementRequest unregistration_request_; |
| em::DeviceManagementRequest upload_certificate_request_; |
| em::DeviceManagementRequest upload_status_request_; |
| + em::DeviceManagementRequest remote_command_request_; |
| // Protobufs used in successful responses. |
| em::DeviceManagementResponse registration_response_; |
| @@ -201,6 +251,7 @@ class CloudPolicyClientTest : public testing::Test { |
| em::DeviceManagementResponse unregistration_response_; |
| em::DeviceManagementResponse upload_certificate_response_; |
| em::DeviceManagementResponse upload_status_response_; |
| + em::DeviceManagementResponse remote_command_response_; |
| base::MessageLoop loop_; |
| std::string client_id_; |
| @@ -208,6 +259,7 @@ class CloudPolicyClientTest : public testing::Test { |
| MockDeviceManagementService service_; |
| StrictMock<MockCloudPolicyClientObserver> observer_; |
| StrictMock<MockUploadObserver> upload_observer_; |
| + StrictMock<MockRemoteCommandsObserver> remote_commands_observer_; |
| scoped_ptr<CloudPolicyClient> client_; |
| // Pointer to the client's request context. |
| scoped_refptr<net::URLRequestContextGetter> request_context_; |
| @@ -717,4 +769,24 @@ TEST_F(CloudPolicyClientTest, RequestCancelOnUnregister) { |
| EXPECT_EQ(0, client_->GetActiveRequestCountForTest()); |
| } |
| +TEST_F(CloudPolicyClientTest, FetchRemoteCommands) { |
| + Register(); |
| + |
| + ExpectFetchRemoteCommands(); |
| + EXPECT_CALL( |
| + remote_commands_observer_, |
| + OnRemoteCommandsFetched(ElementsAre(MatchProto( |
| + remote_command_response_.remote_command_response().commands(0))))) |
| + .Times(1); |
| + CloudPolicyClient::RemoteCommandCallback callback = |
|
bartfab (slow)
2015/02/18 11:12:53
Nit: const.
binjin
2015/02/18 15:37:50
Done.
|
| + base::Bind(&MockRemoteCommandsObserver::OnRemoteCommandsFetched, |
| + base::Unretained(&remote_commands_observer_)); |
| + |
| + const std::vector<enterprise_management::RemoteCommandResult> command_results( |
| + 1, remote_command_request_.remote_command_request().command_results(0)); |
| + client_->FetchRemoteCommands(kLastCommandId, command_results, callback); |
| + |
| + EXPECT_EQ(DM_STATUS_SUCCESS, client_->status()); |
| +} |
| + |
| } // namespace policy |