OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium 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 "base/run_loop.h" |
| 6 #include "base/strings/stringprintf.h" |
| 7 #include "base/test/test_mock_time_task_runner.h" |
| 8 #include "base/thread_task_runner_handle.h" |
| 9 #include "base/time/time.h" |
| 10 #include "chrome/browser/chromeos/policy/system_log_upload_job.h" |
| 11 #include "chrome/browser/chromeos/policy/upload_job.h" |
| 12 #include "content/public/test/test_browser_thread.h" |
| 13 #include "net/http/http_request_headers.h" |
| 14 #include "testing/gmock/include/gmock/gmock.h" |
| 15 |
| 16 namespace policy { |
| 17 |
| 18 namespace { |
| 19 |
| 20 // The list of tested system log file names. |
| 21 const char* const kTestSystemLogFileNames[] = {"my_system_log_file.txt"}; |
| 22 |
| 23 // Number of the system log files to upload in the list. |
| 24 const int kMaxNumTestSystemLogFileNames = 1; |
| 25 |
| 26 class MockUploadJob : public policy::UploadJob { |
| 27 public: |
| 28 // If |error_code| is a null pointer OnSuccess() will be invoked when the |
| 29 // Start() method is called, otherwise OnFailure() will be invoked with the |
| 30 // respective |error_code|. |
| 31 MockUploadJob(const GURL& upload_url, |
| 32 UploadJob::Delegate* delegate, |
| 33 scoped_ptr<UploadJob::ErrorCode> error_code); |
| 34 ~MockUploadJob() override; |
| 35 |
| 36 // policy::UploadJob: |
| 37 void AddDataSegment(const std::string& name, |
| 38 const std::string& filename, |
| 39 const std::map<std::string, std::string>& header_entries, |
| 40 scoped_ptr<std::string> data) override; |
| 41 void Start() override; |
| 42 |
| 43 protected: |
| 44 UploadJob::Delegate* delegate_; |
| 45 scoped_ptr<UploadJob::ErrorCode> error_code_; |
| 46 int file_index_; |
| 47 }; |
| 48 |
| 49 MockUploadJob::MockUploadJob(const GURL& upload_url, |
| 50 UploadJob::Delegate* delegate, |
| 51 scoped_ptr<UploadJob::ErrorCode> error_code) |
| 52 : delegate_(delegate), error_code_(error_code.Pass()), file_index_(0) { |
| 53 } |
| 54 |
| 55 MockUploadJob::~MockUploadJob() { |
| 56 } |
| 57 |
| 58 void MockUploadJob::AddDataSegment( |
| 59 const std::string& name, |
| 60 const std::string& filename, |
| 61 const std::map<std::string, std::string>& header_entries, |
| 62 scoped_ptr<std::string> data) { |
| 63 // Test all fields to upload. |
| 64 EXPECT_LT(file_index_, kMaxNumTestSystemLogFileNames); |
| 65 EXPECT_GE(file_index_, 0); |
| 66 |
| 67 EXPECT_EQ(base::StringPrintf(SystemLogUploadJob::kNameFieldTemplate, |
| 68 file_index_ + 1), |
| 69 name); |
| 70 |
| 71 EXPECT_EQ(kTestSystemLogFileNames[file_index_], filename); |
| 72 |
| 73 EXPECT_EQ(2U, header_entries.size()); |
| 74 EXPECT_EQ( |
| 75 SystemLogUploadJob::kFileTypeLogFile, |
| 76 header_entries.find(SystemLogUploadJob::kFileTypeHeaderName)->second); |
| 77 EXPECT_EQ(SystemLogUploadJob::kContentTypePlainText, |
| 78 header_entries.find(net::HttpRequestHeaders::kContentType)->second); |
| 79 |
| 80 EXPECT_EQ(kTestSystemLogFileNames[file_index_], *data); |
| 81 |
| 82 file_index_++; |
| 83 } |
| 84 |
| 85 void MockUploadJob::Start() { |
| 86 DCHECK(delegate_); |
| 87 if (error_code_) { |
| 88 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 89 FROM_HERE, base::Bind(&UploadJob::Delegate::OnFailure, |
| 90 base::Unretained(delegate_), *error_code_)); |
| 91 } else { |
| 92 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 93 FROM_HERE, base::Bind(&UploadJob::Delegate::OnSuccess, |
| 94 base::Unretained(delegate_))); |
| 95 } |
| 96 } |
| 97 |
| 98 // Generate the fake system log files. |
| 99 scoped_ptr<SystemLogUploadJob::SystemLogs> GenerateTestSystemLogFiles() { |
| 100 scoped_ptr<SystemLogUploadJob::SystemLogs> system_logs( |
| 101 new SystemLogUploadJob::SystemLogs()); |
| 102 for (auto const file_path : kTestSystemLogFileNames) { |
| 103 system_logs->push_back(std::make_pair(file_path, file_path)); |
| 104 } |
| 105 return system_logs; |
| 106 } |
| 107 |
| 108 class MockSystemLogDelegate : public SystemLogUploadJob::Delegate { |
| 109 public: |
| 110 explicit MockSystemLogDelegate( |
| 111 scoped_ptr<UploadJob::ErrorCode> upload_job_error_code); |
| 112 ~MockSystemLogDelegate() override; |
| 113 |
| 114 void LoadSystemLogs(const LogUploadCallback& upload_callback) override; |
| 115 scoped_ptr<UploadJob> CreateUploadJob(const GURL&, |
| 116 UploadJob::Delegate*) override; |
| 117 |
| 118 private: |
| 119 scoped_ptr<UploadJob::ErrorCode> upload_job_error_code_; |
| 120 }; |
| 121 |
| 122 MockSystemLogDelegate::MockSystemLogDelegate( |
| 123 scoped_ptr<UploadJob::ErrorCode> upload_job_error_code) |
| 124 : upload_job_error_code_(upload_job_error_code.Pass()) { |
| 125 } |
| 126 |
| 127 MockSystemLogDelegate::~MockSystemLogDelegate() { |
| 128 } |
| 129 |
| 130 void MockSystemLogDelegate::LoadSystemLogs( |
| 131 const LogUploadCallback& upload_callback) { |
| 132 scoped_ptr<SystemLogUploadJob::SystemLogs> test_files = |
| 133 GenerateTestSystemLogFiles(); |
| 134 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 135 FROM_HERE, |
| 136 base::Bind(upload_callback, base::Owned(test_files.release()))); |
| 137 } |
| 138 |
| 139 scoped_ptr<UploadJob> MockSystemLogDelegate::CreateUploadJob( |
| 140 const GURL& upload_url, |
| 141 UploadJob::Delegate* delegate) { |
| 142 return make_scoped_ptr( |
| 143 new MockUploadJob(upload_url, delegate, upload_job_error_code_.Pass())); |
| 144 } |
| 145 |
| 146 } // namespace |
| 147 |
| 148 class SystemLogUploadTest : public testing::Test { |
| 149 public: |
| 150 // If |error_code| is a null pointer, the VerifyResult expects the success |
| 151 // callback is invoked, otherwise - the failure callback with the respective |
| 152 // |error_code|. |
| 153 void RunTest(scoped_ptr<UploadJob::ErrorCode> error_code); |
| 154 |
| 155 protected: |
| 156 SystemLogUploadTest() |
| 157 : ui_thread_(content::BrowserThread::UI, &run_loop_), |
| 158 task_runner_(new base::TestMockTimeTaskRunner()), |
| 159 weak_factory_(this) {} |
| 160 |
| 161 ~SystemLogUploadTest() override {} |
| 162 |
| 163 void VerifyResults(bool expected_call) { |
| 164 EXPECT_TRUE(expected_call); |
| 165 run_loop_.Quit(); |
| 166 } |
| 167 |
| 168 base::MessageLoopForUI run_loop_; |
| 169 content::TestBrowserThread ui_thread_; |
| 170 |
| 171 private: |
| 172 scoped_refptr<base::TestMockTimeTaskRunner> task_runner_; |
| 173 |
| 174 base::WeakPtrFactory<SystemLogUploadTest> weak_factory_; |
| 175 DISALLOW_COPY_AND_ASSIGN(SystemLogUploadTest); |
| 176 }; |
| 177 |
| 178 void SystemLogUploadTest::RunTest(scoped_ptr<UploadJob::ErrorCode> error_code) { |
| 179 bool success = !error_code; |
| 180 |
| 181 scoped_ptr<SystemLogUploadJob> job(new SystemLogUploadJob( |
| 182 make_scoped_ptr(new MockSystemLogDelegate(error_code.Pass())), |
| 183 base::Bind(&SystemLogUploadTest::VerifyResults, |
| 184 weak_factory_.GetWeakPtr(), success), |
| 185 base::Bind(&SystemLogUploadTest::VerifyResults, |
| 186 weak_factory_.GetWeakPtr(), !success))); |
| 187 |
| 188 job->Run(); |
| 189 run_loop_.Run(); |
| 190 } |
| 191 |
| 192 TEST_F(SystemLogUploadTest, Success) { |
| 193 RunTest(nullptr); |
| 194 } |
| 195 |
| 196 TEST_F(SystemLogUploadTest, Failure) { |
| 197 scoped_ptr<UploadJob::ErrorCode> error_code( |
| 198 new UploadJob::ErrorCode(UploadJob::AUTHENTICATION_ERROR)); |
| 199 RunTest(error_code.Pass()); |
| 200 } |
| 201 |
| 202 } // namespace policy |
OLD | NEW |