This source file includes following definitions.
- SetUp
- StartJob
- CancelJob
- OnJobFinished
- GetAndResetCallbackCount
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "components/policy/core/common/cloud/external_policy_data_fetcher.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/compiler_specific.h"
#include "base/sequenced_task_runner.h"
#include "base/test/test_simple_task_runner.h"
#include "net/base/net_errors.h"
#include "net/url_request/test_url_fetcher_factory.h"
#include "net/url_request/url_fetcher.h"
#include "net/url_request/url_fetcher_delegate.h"
#include "net/url_request/url_request_context_getter.h"
#include "net/url_request/url_request_status.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace policy {
namespace {
const char* kExternalPolicyDataURLs[] = {
"http://localhost/data_1",
"http://localhost/data_2"
};
const int64 kExternalPolicyDataMaxSize = 5 * 1024 * 1024;
const char* kExternalPolicyDataPayload = "External policy data";
}
class ExternalPolicyDataFetcherTest : public testing::Test {
protected:
ExternalPolicyDataFetcherTest();
virtual ~ExternalPolicyDataFetcherTest();
virtual void SetUp() OVERRIDE;
void StartJob(int index);
void CancelJob(int index);
void OnJobFinished(int job_index,
ExternalPolicyDataFetcher::Result result,
scoped_ptr<std::string> data);
int GetAndResetCallbackCount();
net::TestURLFetcherFactory fetcher_factory_;
scoped_refptr<base::TestSimpleTaskRunner> owner_task_runner_;
scoped_refptr<base::TestSimpleTaskRunner> io_task_runner_;
scoped_ptr<ExternalPolicyDataFetcherBackend> fetcher_backend_;
scoped_ptr<ExternalPolicyDataFetcher> fetcher_;
std::map<int, ExternalPolicyDataFetcher::Job*> jobs_;
int callback_count_;
int callback_job_index_;
ExternalPolicyDataFetcher::Result callback_result_;
scoped_ptr<std::string> callback_data_;
DISALLOW_COPY_AND_ASSIGN(ExternalPolicyDataFetcherTest);
};
ExternalPolicyDataFetcherTest::ExternalPolicyDataFetcherTest()
: callback_count_(0) {
}
ExternalPolicyDataFetcherTest::~ExternalPolicyDataFetcherTest() {
}
void ExternalPolicyDataFetcherTest::SetUp() {
fetcher_factory_.set_remove_fetcher_on_delete(true);
io_task_runner_ = new base::TestSimpleTaskRunner();
owner_task_runner_ = new base::TestSimpleTaskRunner();
fetcher_backend_.reset(new ExternalPolicyDataFetcherBackend(
io_task_runner_,
scoped_refptr<net::URLRequestContextGetter>()));
fetcher_.reset(
fetcher_backend_->CreateFrontend(owner_task_runner_).release());
}
void ExternalPolicyDataFetcherTest::StartJob(int index) {
jobs_[index] = fetcher_->StartJob(
GURL(kExternalPolicyDataURLs[index]),
kExternalPolicyDataMaxSize,
base::Bind(&ExternalPolicyDataFetcherTest::OnJobFinished,
base::Unretained(this), index));
io_task_runner_->RunUntilIdle();
}
void ExternalPolicyDataFetcherTest::CancelJob(int index) {
std::map<int, ExternalPolicyDataFetcher::Job*>::iterator it =
jobs_.find(index);
ASSERT_TRUE(it != jobs_.end());
ExternalPolicyDataFetcher::Job* job = it->second;
jobs_.erase(it);
fetcher_->CancelJob(job);
}
void ExternalPolicyDataFetcherTest::OnJobFinished(
int job_index,
ExternalPolicyDataFetcher::Result result,
scoped_ptr<std::string> data) {
++callback_count_;
callback_job_index_ = job_index;
callback_result_ = result;
callback_data_.reset(data.release());
jobs_.erase(job_index);
}
int ExternalPolicyDataFetcherTest::GetAndResetCallbackCount() {
const int callback_count = callback_count_;
callback_count_ = 0;
return callback_count;
}
TEST_F(ExternalPolicyDataFetcherTest, Success) {
StartJob(0);
net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
fetcher->set_response_code(200);
fetcher->SetResponseString(kExternalPolicyDataPayload);
fetcher->delegate()->OnURLFetchComplete(fetcher);
EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
owner_task_runner_->RunUntilIdle();
EXPECT_EQ(1, GetAndResetCallbackCount());
EXPECT_EQ(0, callback_job_index_);
EXPECT_EQ(ExternalPolicyDataFetcher::SUCCESS, callback_result_);
ASSERT_TRUE(callback_data_);
EXPECT_EQ(kExternalPolicyDataPayload, *callback_data_);
}
TEST_F(ExternalPolicyDataFetcherTest, MaxSizeExceeded) {
StartJob(0);
net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
fetcher->delegate()->OnURLFetchDownloadProgress(
fetcher,
kExternalPolicyDataMaxSize + 1,
-1);
EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
owner_task_runner_->RunUntilIdle();
EXPECT_EQ(1, GetAndResetCallbackCount());
EXPECT_EQ(0, callback_job_index_);
EXPECT_EQ(ExternalPolicyDataFetcher::MAX_SIZE_EXCEEDED, callback_result_);
EXPECT_FALSE(callback_data_);
}
TEST_F(ExternalPolicyDataFetcherTest, ConnectionInterrupted) {
StartJob(0);
net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED,
net::ERR_CONNECTION_RESET));
fetcher->delegate()->OnURLFetchComplete(fetcher);
EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
owner_task_runner_->RunUntilIdle();
EXPECT_EQ(1, GetAndResetCallbackCount());
EXPECT_EQ(0, callback_job_index_);
EXPECT_EQ(ExternalPolicyDataFetcher::CONNECTION_INTERRUPTED,
callback_result_);
EXPECT_FALSE(callback_data_);
}
TEST_F(ExternalPolicyDataFetcherTest, NetworkError) {
StartJob(0);
net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED,
net::ERR_NETWORK_CHANGED));
fetcher->delegate()->OnURLFetchComplete(fetcher);
EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
owner_task_runner_->RunUntilIdle();
EXPECT_EQ(1, GetAndResetCallbackCount());
EXPECT_EQ(0, callback_job_index_);
EXPECT_EQ(ExternalPolicyDataFetcher::NETWORK_ERROR, callback_result_);
EXPECT_FALSE(callback_data_);
}
TEST_F(ExternalPolicyDataFetcherTest, ServerError) {
StartJob(0);
net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
fetcher->set_response_code(500);
fetcher->delegate()->OnURLFetchComplete(fetcher);
EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
owner_task_runner_->RunUntilIdle();
EXPECT_EQ(1, GetAndResetCallbackCount());
EXPECT_EQ(0, callback_job_index_);
EXPECT_EQ(ExternalPolicyDataFetcher::SERVER_ERROR, callback_result_);
EXPECT_FALSE(callback_data_);
}
TEST_F(ExternalPolicyDataFetcherTest, ClientError) {
StartJob(0);
net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
fetcher->set_response_code(400);
fetcher->delegate()->OnURLFetchComplete(fetcher);
EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
owner_task_runner_->RunUntilIdle();
EXPECT_EQ(1, GetAndResetCallbackCount());
EXPECT_EQ(0, callback_job_index_);
EXPECT_EQ(ExternalPolicyDataFetcher::CLIENT_ERROR, callback_result_);
EXPECT_FALSE(callback_data_);
}
TEST_F(ExternalPolicyDataFetcherTest, HTTPError) {
StartJob(0);
net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
fetcher->set_response_code(300);
fetcher->delegate()->OnURLFetchComplete(fetcher);
EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
owner_task_runner_->RunUntilIdle();
EXPECT_EQ(1, GetAndResetCallbackCount());
EXPECT_EQ(0, callback_job_index_);
EXPECT_EQ(ExternalPolicyDataFetcher::HTTP_ERROR, callback_result_);
EXPECT_FALSE(callback_data_);
}
TEST_F(ExternalPolicyDataFetcherTest, Canceled) {
StartJob(0);
net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
CancelJob(0);
io_task_runner_->RunUntilIdle();
EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
owner_task_runner_->RunUntilIdle();
EXPECT_EQ(0, GetAndResetCallbackCount());
}
TEST_F(ExternalPolicyDataFetcherTest, SuccessfulCanceled) {
StartJob(0);
net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
fetcher->set_response_code(200);
fetcher->SetResponseString(kExternalPolicyDataPayload);
fetcher->delegate()->OnURLFetchComplete(fetcher);
EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
CancelJob(0);
owner_task_runner_->RunUntilIdle();
EXPECT_EQ(0, GetAndResetCallbackCount());
}
TEST_F(ExternalPolicyDataFetcherTest, ParallelJobs) {
StartJob(0);
StartJob(1);
net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(1);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[1]), fetcher->GetOriginalURL());
fetcher = fetcher_factory_.GetFetcherByID(0);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
fetcher->set_response_code(200);
fetcher->SetResponseString(kExternalPolicyDataPayload);
fetcher->delegate()->OnURLFetchComplete(fetcher);
EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
owner_task_runner_->RunUntilIdle();
EXPECT_EQ(1, GetAndResetCallbackCount());
EXPECT_EQ(0, callback_job_index_);
EXPECT_EQ(ExternalPolicyDataFetcher::SUCCESS, callback_result_);
ASSERT_TRUE(callback_data_);
EXPECT_EQ(kExternalPolicyDataPayload, *callback_data_);
fetcher = fetcher_factory_.GetFetcherByID(1);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[1]), fetcher->GetOriginalURL());
fetcher->set_response_code(200);
fetcher->SetResponseString(kExternalPolicyDataPayload);
fetcher->delegate()->OnURLFetchComplete(fetcher);
EXPECT_FALSE(fetcher_factory_.GetFetcherByID(1));
owner_task_runner_->RunUntilIdle();
EXPECT_EQ(1, GetAndResetCallbackCount());
EXPECT_EQ(1, callback_job_index_);
EXPECT_EQ(ExternalPolicyDataFetcher::SUCCESS, callback_result_);
ASSERT_TRUE(callback_data_);
EXPECT_EQ(kExternalPolicyDataPayload, *callback_data_);
}
TEST_F(ExternalPolicyDataFetcherTest, ParallelJobsFinishingOutOfOrder) {
StartJob(0);
StartJob(1);
net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
fetcher = fetcher_factory_.GetFetcherByID(1);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[1]), fetcher->GetOriginalURL());
fetcher->set_response_code(200);
fetcher->SetResponseString(kExternalPolicyDataPayload);
fetcher->delegate()->OnURLFetchComplete(fetcher);
EXPECT_FALSE(fetcher_factory_.GetFetcherByID(1));
owner_task_runner_->RunUntilIdle();
EXPECT_EQ(1, GetAndResetCallbackCount());
EXPECT_EQ(1, callback_job_index_);
EXPECT_EQ(ExternalPolicyDataFetcher::SUCCESS, callback_result_);
ASSERT_TRUE(callback_data_);
EXPECT_EQ(kExternalPolicyDataPayload, *callback_data_);
fetcher = fetcher_factory_.GetFetcherByID(0);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
fetcher->set_response_code(200);
fetcher->SetResponseString(kExternalPolicyDataPayload);
fetcher->delegate()->OnURLFetchComplete(fetcher);
EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
owner_task_runner_->RunUntilIdle();
EXPECT_EQ(1, GetAndResetCallbackCount());
EXPECT_EQ(0, callback_job_index_);
EXPECT_EQ(ExternalPolicyDataFetcher::SUCCESS, callback_result_);
ASSERT_TRUE(callback_data_);
EXPECT_EQ(kExternalPolicyDataPayload, *callback_data_);
}
TEST_F(ExternalPolicyDataFetcherTest, ParallelJobsWithCancel) {
StartJob(0);
StartJob(1);
net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(1);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[1]), fetcher->GetOriginalURL());
fetcher = fetcher_factory_.GetFetcherByID(0);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[0]), fetcher->GetOriginalURL());
CancelJob(0);
io_task_runner_->RunUntilIdle();
EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
owner_task_runner_->RunUntilIdle();
EXPECT_EQ(0, GetAndResetCallbackCount());
fetcher = fetcher_factory_.GetFetcherByID(1);
ASSERT_TRUE(fetcher);
EXPECT_EQ(GURL(kExternalPolicyDataURLs[1]), fetcher->GetOriginalURL());
fetcher->set_response_code(200);
fetcher->SetResponseString(kExternalPolicyDataPayload);
fetcher->delegate()->OnURLFetchComplete(fetcher);
EXPECT_FALSE(fetcher_factory_.GetFetcherByID(1));
owner_task_runner_->RunUntilIdle();
EXPECT_EQ(1, GetAndResetCallbackCount());
EXPECT_EQ(1, callback_job_index_);
EXPECT_EQ(ExternalPolicyDataFetcher::SUCCESS, callback_result_);
ASSERT_TRUE(callback_data_);
EXPECT_EQ(kExternalPolicyDataPayload, *callback_data_);
}
}