| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <vector> | 5 #include <vector> |
| 6 | 6 |
| 7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
| 8 #include "base/string_split.h" | 8 #include "base/string_split.h" |
| 9 #include "chrome/browser/browser_thread.h" | 9 #include "chrome/browser/browser_thread.h" |
| 10 #include "chrome/browser/policy/device_management_backend_impl.h" | 10 #include "chrome/browser/policy/device_management_backend_impl.h" |
| 11 #include "chrome/browser/policy/device_management_backend_mock.h" | 11 #include "chrome/browser/policy/device_management_backend_mock.h" |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 scoped_ptr<DeviceManagementService> service_; | 75 scoped_ptr<DeviceManagementService> service_; |
| 76 scoped_ptr<DeviceManagementBackend> backend_; | 76 scoped_ptr<DeviceManagementBackend> backend_; |
| 77 | 77 |
| 78 private: | 78 private: |
| 79 MessageLoopForUI loop_; | 79 MessageLoopForUI loop_; |
| 80 BrowserThread io_thread_; | 80 BrowserThread io_thread_; |
| 81 }; | 81 }; |
| 82 | 82 |
| 83 struct FailedRequestParams { | 83 struct FailedRequestParams { |
| 84 FailedRequestParams(DeviceManagementBackend::ErrorCode expected_error, | 84 FailedRequestParams(DeviceManagementBackend::ErrorCode expected_error, |
| 85 URLRequestStatus::Status request_status, | 85 net::URLRequestStatus::Status request_status, |
| 86 int http_status, | 86 int http_status, |
| 87 const std::string& response) | 87 const std::string& response) |
| 88 : expected_error_(expected_error), | 88 : expected_error_(expected_error), |
| 89 request_status_(request_status, 0), | 89 request_status_(request_status, 0), |
| 90 http_status_(http_status), | 90 http_status_(http_status), |
| 91 response_(response) {} | 91 response_(response) {} |
| 92 | 92 |
| 93 DeviceManagementBackend::ErrorCode expected_error_; | 93 DeviceManagementBackend::ErrorCode expected_error_; |
| 94 URLRequestStatus request_status_; | 94 net::URLRequestStatus request_status_; |
| 95 int http_status_; | 95 int http_status_; |
| 96 std::string response_; | 96 std::string response_; |
| 97 }; | 97 }; |
| 98 | 98 |
| 99 // A parameterized test case for erroneous response situations, they're mostly | 99 // A parameterized test case for erroneous response situations, they're mostly |
| 100 // the same for all kinds of requests. | 100 // the same for all kinds of requests. |
| 101 class DeviceManagementServiceFailedRequestTest | 101 class DeviceManagementServiceFailedRequestTest |
| 102 : public DeviceManagementServiceTestBase< | 102 : public DeviceManagementServiceTestBase< |
| 103 testing::TestWithParam<FailedRequestParams> > { | 103 testing::TestWithParam<FailedRequestParams> > { |
| 104 }; | 104 }; |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 ResponseCookies(), | 154 ResponseCookies(), |
| 155 GetParam().response_); | 155 GetParam().response_); |
| 156 } | 156 } |
| 157 | 157 |
| 158 INSTANTIATE_TEST_CASE_P( | 158 INSTANTIATE_TEST_CASE_P( |
| 159 DeviceManagementServiceFailedRequestTestInstance, | 159 DeviceManagementServiceFailedRequestTestInstance, |
| 160 DeviceManagementServiceFailedRequestTest, | 160 DeviceManagementServiceFailedRequestTest, |
| 161 testing::Values( | 161 testing::Values( |
| 162 FailedRequestParams( | 162 FailedRequestParams( |
| 163 DeviceManagementBackend::kErrorRequestFailed, | 163 DeviceManagementBackend::kErrorRequestFailed, |
| 164 URLRequestStatus::FAILED, | 164 net::URLRequestStatus::FAILED, |
| 165 200, | 165 200, |
| 166 PROTO_STRING(kResponseEmpty)), | 166 PROTO_STRING(kResponseEmpty)), |
| 167 FailedRequestParams( | 167 FailedRequestParams( |
| 168 DeviceManagementBackend::kErrorHttpStatus, | 168 DeviceManagementBackend::kErrorHttpStatus, |
| 169 URLRequestStatus::SUCCESS, | 169 net::URLRequestStatus::SUCCESS, |
| 170 500, | 170 500, |
| 171 PROTO_STRING(kResponseEmpty)), | 171 PROTO_STRING(kResponseEmpty)), |
| 172 FailedRequestParams( | 172 FailedRequestParams( |
| 173 DeviceManagementBackend::kErrorResponseDecoding, | 173 DeviceManagementBackend::kErrorResponseDecoding, |
| 174 URLRequestStatus::SUCCESS, | 174 net::URLRequestStatus::SUCCESS, |
| 175 200, | 175 200, |
| 176 PROTO_STRING("Not a protobuf.")), | 176 PROTO_STRING("Not a protobuf.")), |
| 177 FailedRequestParams( | 177 FailedRequestParams( |
| 178 DeviceManagementBackend::kErrorServiceManagementNotSupported, | 178 DeviceManagementBackend::kErrorServiceManagementNotSupported, |
| 179 URLRequestStatus::SUCCESS, | 179 net::URLRequestStatus::SUCCESS, |
| 180 200, | 180 200, |
| 181 PROTO_STRING(kResponseErrorManagementNotSupported)), | 181 PROTO_STRING(kResponseErrorManagementNotSupported)), |
| 182 FailedRequestParams( | 182 FailedRequestParams( |
| 183 DeviceManagementBackend::kErrorServiceDeviceNotFound, | 183 DeviceManagementBackend::kErrorServiceDeviceNotFound, |
| 184 URLRequestStatus::SUCCESS, | 184 net::URLRequestStatus::SUCCESS, |
| 185 200, | 185 200, |
| 186 PROTO_STRING(kResponseErrorDeviceNotFound)), | 186 PROTO_STRING(kResponseErrorDeviceNotFound)), |
| 187 FailedRequestParams( | 187 FailedRequestParams( |
| 188 DeviceManagementBackend::kErrorServiceManagementTokenInvalid, | 188 DeviceManagementBackend::kErrorServiceManagementTokenInvalid, |
| 189 URLRequestStatus::SUCCESS, | 189 net::URLRequestStatus::SUCCESS, |
| 190 200, | 190 200, |
| 191 PROTO_STRING(kResponseErrorManagementTokenInvalid)), | 191 PROTO_STRING(kResponseErrorManagementTokenInvalid)), |
| 192 FailedRequestParams( | 192 FailedRequestParams( |
| 193 DeviceManagementBackend::kErrorServiceActivationPending, | 193 DeviceManagementBackend::kErrorServiceActivationPending, |
| 194 URLRequestStatus::SUCCESS, | 194 net::URLRequestStatus::SUCCESS, |
| 195 200, | 195 200, |
| 196 PROTO_STRING(kResponseErrorActivationPending)))); | 196 PROTO_STRING(kResponseErrorActivationPending)))); |
| 197 | 197 |
| 198 // Simple query parameter parser for testing. | 198 // Simple query parameter parser for testing. |
| 199 class QueryParams { | 199 class QueryParams { |
| 200 public: | 200 public: |
| 201 explicit QueryParams(const std::string& query) { | 201 explicit QueryParams(const std::string& query) { |
| 202 base::SplitStringIntoKeyValuePairs(query, '=', '&', ¶ms_); | 202 base::SplitStringIntoKeyValuePairs(query, '=', '&', ¶ms_); |
| 203 } | 203 } |
| 204 | 204 |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 292 ASSERT_TRUE(expected_request_wrapper.SerializeToString( | 292 ASSERT_TRUE(expected_request_wrapper.SerializeToString( |
| 293 &expected_request_data)); | 293 &expected_request_data)); |
| 294 EXPECT_EQ(expected_request_data, fetcher->upload_data()); | 294 EXPECT_EQ(expected_request_data, fetcher->upload_data()); |
| 295 | 295 |
| 296 // Generate the response. | 296 // Generate the response. |
| 297 std::string response_data; | 297 std::string response_data; |
| 298 em::DeviceManagementResponse response_wrapper; | 298 em::DeviceManagementResponse response_wrapper; |
| 299 response_wrapper.set_error(em::DeviceManagementResponse::SUCCESS); | 299 response_wrapper.set_error(em::DeviceManagementResponse::SUCCESS); |
| 300 response_wrapper.mutable_register_response()->CopyFrom(expected_response); | 300 response_wrapper.mutable_register_response()->CopyFrom(expected_response); |
| 301 ASSERT_TRUE(response_wrapper.SerializeToString(&response_data)); | 301 ASSERT_TRUE(response_wrapper.SerializeToString(&response_data)); |
| 302 URLRequestStatus status(URLRequestStatus::SUCCESS, 0); | 302 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0); |
| 303 fetcher->delegate()->OnURLFetchComplete(fetcher, | 303 fetcher->delegate()->OnURLFetchComplete(fetcher, |
| 304 GURL(kServiceUrl), | 304 GURL(kServiceUrl), |
| 305 status, | 305 status, |
| 306 200, | 306 200, |
| 307 ResponseCookies(), | 307 ResponseCookies(), |
| 308 response_data); | 308 response_data); |
| 309 } | 309 } |
| 310 | 310 |
| 311 TEST_F(DeviceManagementServiceTest, UnregisterRequest) { | 311 TEST_F(DeviceManagementServiceTest, UnregisterRequest) { |
| 312 DeviceUnregisterResponseDelegateMock mock; | 312 DeviceUnregisterResponseDelegateMock mock; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 335 ASSERT_TRUE(expected_request_wrapper.SerializeToString( | 335 ASSERT_TRUE(expected_request_wrapper.SerializeToString( |
| 336 &expected_request_data)); | 336 &expected_request_data)); |
| 337 EXPECT_EQ(expected_request_data, fetcher->upload_data()); | 337 EXPECT_EQ(expected_request_data, fetcher->upload_data()); |
| 338 | 338 |
| 339 // Generate the response. | 339 // Generate the response. |
| 340 std::string response_data; | 340 std::string response_data; |
| 341 em::DeviceManagementResponse response_wrapper; | 341 em::DeviceManagementResponse response_wrapper; |
| 342 response_wrapper.set_error(em::DeviceManagementResponse::SUCCESS); | 342 response_wrapper.set_error(em::DeviceManagementResponse::SUCCESS); |
| 343 response_wrapper.mutable_unregister_response()->CopyFrom(expected_response); | 343 response_wrapper.mutable_unregister_response()->CopyFrom(expected_response); |
| 344 ASSERT_TRUE(response_wrapper.SerializeToString(&response_data)); | 344 ASSERT_TRUE(response_wrapper.SerializeToString(&response_data)); |
| 345 URLRequestStatus status(URLRequestStatus::SUCCESS, 0); | 345 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0); |
| 346 fetcher->delegate()->OnURLFetchComplete(fetcher, | 346 fetcher->delegate()->OnURLFetchComplete(fetcher, |
| 347 GURL(kServiceUrl), | 347 GURL(kServiceUrl), |
| 348 status, | 348 status, |
| 349 200, | 349 200, |
| 350 ResponseCookies(), | 350 ResponseCookies(), |
| 351 response_data); | 351 response_data); |
| 352 } | 352 } |
| 353 | 353 |
| 354 TEST_F(DeviceManagementServiceTest, PolicyRequest) { | 354 TEST_F(DeviceManagementServiceTest, PolicyRequest) { |
| 355 DevicePolicyResponseDelegateMock mock; | 355 DevicePolicyResponseDelegateMock mock; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 390 ASSERT_TRUE(expected_request_wrapper.SerializeToString( | 390 ASSERT_TRUE(expected_request_wrapper.SerializeToString( |
| 391 &expected_request_data)); | 391 &expected_request_data)); |
| 392 EXPECT_EQ(expected_request_data, fetcher->upload_data()); | 392 EXPECT_EQ(expected_request_data, fetcher->upload_data()); |
| 393 | 393 |
| 394 // Generate the response. | 394 // Generate the response. |
| 395 std::string response_data; | 395 std::string response_data; |
| 396 em::DeviceManagementResponse response_wrapper; | 396 em::DeviceManagementResponse response_wrapper; |
| 397 response_wrapper.set_error(em::DeviceManagementResponse::SUCCESS); | 397 response_wrapper.set_error(em::DeviceManagementResponse::SUCCESS); |
| 398 response_wrapper.mutable_policy_response()->CopyFrom(expected_response); | 398 response_wrapper.mutable_policy_response()->CopyFrom(expected_response); |
| 399 ASSERT_TRUE(response_wrapper.SerializeToString(&response_data)); | 399 ASSERT_TRUE(response_wrapper.SerializeToString(&response_data)); |
| 400 URLRequestStatus status(URLRequestStatus::SUCCESS, 0); | 400 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0); |
| 401 fetcher->delegate()->OnURLFetchComplete(fetcher, | 401 fetcher->delegate()->OnURLFetchComplete(fetcher, |
| 402 GURL(kServiceUrl), | 402 GURL(kServiceUrl), |
| 403 status, | 403 status, |
| 404 200, | 404 200, |
| 405 ResponseCookies(), | 405 ResponseCookies(), |
| 406 response_data); | 406 response_data); |
| 407 } | 407 } |
| 408 | 408 |
| 409 TEST_F(DeviceManagementServiceTest, CancelRegisterRequest) { | 409 TEST_F(DeviceManagementServiceTest, CancelRegisterRequest) { |
| 410 DeviceRegisterResponseDelegateMock mock; | 410 DeviceRegisterResponseDelegateMock mock; |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 466 fetcher = factory_.GetFetcherByID(0); | 466 fetcher = factory_.GetFetcherByID(0); |
| 467 ASSERT_TRUE(fetcher); | 467 ASSERT_TRUE(fetcher); |
| 468 factory_.RemoveFetcherFromMap(0); | 468 factory_.RemoveFetcherFromMap(0); |
| 469 | 469 |
| 470 // Check that the request is processed as expected. | 470 // Check that the request is processed as expected. |
| 471 std::string response_data; | 471 std::string response_data; |
| 472 em::DeviceManagementResponse response_wrapper; | 472 em::DeviceManagementResponse response_wrapper; |
| 473 response_wrapper.set_error(em::DeviceManagementResponse::SUCCESS); | 473 response_wrapper.set_error(em::DeviceManagementResponse::SUCCESS); |
| 474 response_wrapper.mutable_register_response()->CopyFrom(expected_response); | 474 response_wrapper.mutable_register_response()->CopyFrom(expected_response); |
| 475 ASSERT_TRUE(response_wrapper.SerializeToString(&response_data)); | 475 ASSERT_TRUE(response_wrapper.SerializeToString(&response_data)); |
| 476 URLRequestStatus status(URLRequestStatus::SUCCESS, 0); | 476 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0); |
| 477 fetcher->delegate()->OnURLFetchComplete(fetcher, | 477 fetcher->delegate()->OnURLFetchComplete(fetcher, |
| 478 GURL(kServiceUrl), | 478 GURL(kServiceUrl), |
| 479 status, | 479 status, |
| 480 200, | 480 200, |
| 481 ResponseCookies(), | 481 ResponseCookies(), |
| 482 response_data); | 482 response_data); |
| 483 } | 483 } |
| 484 | 484 |
| 485 } // namespace policy | 485 } // namespace policy |
| OLD | NEW |