OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <ostream> | 5 #include <ostream> |
6 #include <vector> | 6 #include <vector> |
7 | 7 |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/run_loop.h" |
10 #include "base/strings/string_split.h" | 10 #include "base/strings/string_split.h" |
11 #include "chrome/browser/policy/cloud/cloud_policy_constants.h" | 11 #include "chrome/browser/policy/cloud/cloud_policy_constants.h" |
12 #include "chrome/browser/policy/cloud/device_management_service.h" | 12 #include "chrome/browser/policy/cloud/device_management_service.h" |
13 #include "chrome/test/base/testing_browser_process.h" | 13 #include "chrome/test/base/testing_browser_process.h" |
14 #include "content/public/test/test_browser_thread.h" | 14 #include "content/public/test/test_browser_thread_bundle.h" |
15 #include "net/base/escape.h" | 15 #include "net/base/escape.h" |
16 #include "net/base/load_flags.h" | 16 #include "net/base/load_flags.h" |
17 #include "net/base/net_errors.h" | 17 #include "net/base/net_errors.h" |
18 #include "net/http/http_response_headers.h" | 18 #include "net/http/http_response_headers.h" |
19 #include "net/url_request/test_url_fetcher_factory.h" | 19 #include "net/url_request/test_url_fetcher_factory.h" |
20 #include "net/url_request/url_request_status.h" | 20 #include "net/url_request/url_request_status.h" |
21 #include "net/url_request/url_request_test_util.h" | 21 #include "net/url_request/url_request_test_util.h" |
22 #include "testing/gmock/include/gmock/gmock.h" | 22 #include "testing/gmock/include/gmock/gmock.h" |
23 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
24 | 24 |
25 using content::BrowserThread; | |
26 using testing::Mock; | 25 using testing::Mock; |
27 using testing::_; | 26 using testing::_; |
28 | 27 |
29 namespace em = enterprise_management; | 28 namespace em = enterprise_management; |
30 | 29 |
31 namespace policy { | 30 namespace policy { |
32 | 31 |
33 const char kServiceUrl[] = "https://example.com/management_service"; | 32 const char kServiceUrl[] = "https://example.com/management_service"; |
34 | 33 |
35 // Encoded empty response messages for testing the error code paths. | 34 // Encoded empty response messages for testing the error code paths. |
36 const char kResponseEmpty[] = "\x08\x00"; | 35 const char kResponseEmpty[] = "\x08\x00"; |
37 | 36 |
38 #define PROTO_STRING(name) (std::string(name, arraysize(name) - 1)) | 37 #define PROTO_STRING(name) (std::string(name, arraysize(name) - 1)) |
39 | 38 |
40 // Some helper constants. | 39 // Some helper constants. |
41 const char kGaiaAuthToken[] = "gaia-auth-token"; | 40 const char kGaiaAuthToken[] = "gaia-auth-token"; |
42 const char kOAuthToken[] = "oauth-token"; | 41 const char kOAuthToken[] = "oauth-token"; |
43 const char kDMToken[] = "device-management-token"; | 42 const char kDMToken[] = "device-management-token"; |
44 const char kClientID[] = "device-id"; | 43 const char kClientID[] = "device-id"; |
45 const char kRobotAuthCode[] = "robot-oauth-auth-code"; | 44 const char kRobotAuthCode[] = "robot-oauth-auth-code"; |
46 | 45 |
47 // Unit tests for the device management policy service. The tests are run | 46 // Unit tests for the device management policy service. The tests are run |
48 // against a TestURLFetcherFactory that is used to short-circuit the request | 47 // against a TestURLFetcherFactory that is used to short-circuit the request |
49 // without calling into the actual network stack. | 48 // without calling into the actual network stack. |
50 class DeviceManagementServiceTestBase : public testing::Test { | 49 class DeviceManagementServiceTestBase : public testing::Test { |
51 protected: | 50 protected: |
52 DeviceManagementServiceTestBase() | 51 DeviceManagementServiceTestBase() { |
53 : ui_thread_(BrowserThread::UI, &loop_), | |
54 io_thread_(BrowserThread::IO, &loop_) { | |
55 ResetService(); | 52 ResetService(); |
56 InitializeService(); | 53 InitializeService(); |
57 } | 54 } |
58 | 55 |
59 virtual void TearDown() { | 56 ~DeviceManagementServiceTestBase() { |
60 service_.reset(); | 57 service_.reset(); |
61 loop_.RunUntilIdle(); | 58 base::RunLoop().RunUntilIdle(); |
62 } | 59 } |
63 | 60 |
64 void ResetService() { | 61 void ResetService() { |
65 service_.reset(new DeviceManagementService(kServiceUrl)); | 62 service_.reset(new DeviceManagementService(kServiceUrl)); |
66 } | 63 } |
67 | 64 |
68 void InitializeService() { | 65 void InitializeService() { |
69 service_->ScheduleInitialization(0); | 66 service_->ScheduleInitialization(0); |
70 loop_.RunUntilIdle(); | 67 base::RunLoop().RunUntilIdle(); |
71 } | 68 } |
72 | 69 |
73 net::TestURLFetcher* GetFetcher() { | 70 net::TestURLFetcher* GetFetcher() { |
74 return factory_.GetFetcherByID(DeviceManagementService::kURLFetcherID); | 71 return factory_.GetFetcherByID(DeviceManagementService::kURLFetcherID); |
75 } | 72 } |
76 | 73 |
77 DeviceManagementRequestJob* StartRegistrationJob() { | 74 DeviceManagementRequestJob* StartRegistrationJob() { |
78 DeviceManagementRequestJob* job = | 75 DeviceManagementRequestJob* job = |
79 service_->CreateJob(DeviceManagementRequestJob::TYPE_REGISTRATION); | 76 service_->CreateJob(DeviceManagementRequestJob::TYPE_REGISTRATION); |
80 job->SetGaiaToken(kGaiaAuthToken); | 77 job->SetGaiaToken(kGaiaAuthToken); |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 | 156 |
160 MOCK_METHOD3(OnJobDone, void(DeviceManagementStatus, int, | 157 MOCK_METHOD3(OnJobDone, void(DeviceManagementStatus, int, |
161 const em::DeviceManagementResponse&)); | 158 const em::DeviceManagementResponse&)); |
162 | 159 |
163 MOCK_METHOD1(OnJobRetry, void(DeviceManagementRequestJob*)); | 160 MOCK_METHOD1(OnJobRetry, void(DeviceManagementRequestJob*)); |
164 | 161 |
165 net::TestURLFetcherFactory factory_; | 162 net::TestURLFetcherFactory factory_; |
166 scoped_ptr<DeviceManagementService> service_; | 163 scoped_ptr<DeviceManagementService> service_; |
167 | 164 |
168 private: | 165 private: |
169 base::MessageLoopForUI loop_; | 166 content::TestBrowserThreadBundle thread_bundle_; |
170 content::TestBrowserThread ui_thread_; | |
171 content::TestBrowserThread io_thread_; | |
172 }; | 167 }; |
173 | 168 |
174 struct FailedRequestParams { | 169 struct FailedRequestParams { |
175 FailedRequestParams(DeviceManagementStatus expected_status, | 170 FailedRequestParams(DeviceManagementStatus expected_status, |
176 net::URLRequestStatus::Status request_status, | 171 net::URLRequestStatus::Status request_status, |
177 int http_status, | 172 int http_status, |
178 const std::string& response) | 173 const std::string& response) |
179 : expected_status_(expected_status), | 174 : expected_status_(expected_status), |
180 request_status_(request_status, 0), | 175 request_status_(request_status, 0), |
181 http_status_(http_status), | 176 http_status_(http_status), |
(...skipping 509 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
691 EXPECT_CALL(*this, OnJobDone(DM_STATUS_REQUEST_FAILED, _, _)); | 686 EXPECT_CALL(*this, OnJobDone(DM_STATUS_REQUEST_FAILED, _, _)); |
692 EXPECT_CALL(*this, OnJobRetry(_)).Times(0); | 687 EXPECT_CALL(*this, OnJobRetry(_)).Times(0); |
693 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED, | 688 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED, |
694 net::ERR_NETWORK_CHANGED)); | 689 net::ERR_NETWORK_CHANGED)); |
695 fetcher->set_url(GURL(kServiceUrl)); | 690 fetcher->set_url(GURL(kServiceUrl)); |
696 fetcher->delegate()->OnURLFetchComplete(fetcher); | 691 fetcher->delegate()->OnURLFetchComplete(fetcher); |
697 Mock::VerifyAndClearExpectations(this); | 692 Mock::VerifyAndClearExpectations(this); |
698 } | 693 } |
699 | 694 |
700 } // namespace policy | 695 } // namespace policy |
OLD | NEW |