OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/proximity_auth/cryptauth/cryptauth_client.h" | 5 #include "components/proximity_auth/cryptauth/cryptauth_client_impl.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/test/null_task_runner.h" | 8 #include "base/test/null_task_runner.h" |
9 #include "components/proximity_auth/cryptauth/cryptauth_access_token_fetcher.h" | 9 #include "components/proximity_auth/cryptauth/cryptauth_access_token_fetcher.h" |
10 #include "components/proximity_auth/cryptauth/cryptauth_api_call_flow.h" | 10 #include "components/proximity_auth/cryptauth/cryptauth_api_call_flow.h" |
11 #include "components/proximity_auth/cryptauth/cryptauth_client_factory.h" | |
12 #include "components/proximity_auth/cryptauth/proto/cryptauth_api.pb.h" | 11 #include "components/proximity_auth/cryptauth/proto/cryptauth_api.pb.h" |
13 #include "components/proximity_auth/switches.h" | 12 #include "components/proximity_auth/switches.h" |
14 #include "google_apis/gaia/fake_oauth2_token_service.h" | 13 #include "google_apis/gaia/fake_oauth2_token_service.h" |
15 #include "net/url_request/test_url_fetcher_factory.h" | 14 #include "net/url_request/test_url_fetcher_factory.h" |
16 #include "net/url_request/url_request_test_util.h" | 15 #include "net/url_request/url_request_test_util.h" |
17 #include "testing/gmock/include/gmock/gmock.h" | 16 #include "testing/gmock/include/gmock/gmock.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 #include "url/gurl.h" |
19 | 19 |
20 using testing::_; | 20 using testing::_; |
21 using testing::DoAll; | 21 using testing::DoAll; |
22 using testing::Return; | 22 using testing::Return; |
23 using testing::SaveArg; | 23 using testing::SaveArg; |
24 using testing::StrictMock; | 24 using testing::StrictMock; |
25 | 25 |
26 namespace proximity_auth { | 26 namespace proximity_auth { |
27 | 27 |
28 namespace { | 28 namespace { |
(...skipping 25 matching lines...) Expand all Loading... |
54 access_token_ = access_token; | 54 access_token_ = access_token; |
55 }; | 55 }; |
56 | 56 |
57 private: | 57 private: |
58 std::string access_token_; | 58 std::string access_token_; |
59 }; | 59 }; |
60 | 60 |
61 // Mock CryptAuthApiCallFlow, which handles the HTTP requests to CryptAuth. | 61 // Mock CryptAuthApiCallFlow, which handles the HTTP requests to CryptAuth. |
62 class MockCryptAuthApiCallFlow : public CryptAuthApiCallFlow { | 62 class MockCryptAuthApiCallFlow : public CryptAuthApiCallFlow { |
63 public: | 63 public: |
64 MockCryptAuthApiCallFlow() : CryptAuthApiCallFlow(GURL(std::string())) {} | 64 MockCryptAuthApiCallFlow() : CryptAuthApiCallFlow() {} |
65 virtual ~MockCryptAuthApiCallFlow() {} | 65 virtual ~MockCryptAuthApiCallFlow() {} |
66 | 66 |
67 MOCK_METHOD5(Start, | 67 MOCK_METHOD6(Start, |
68 void(net::URLRequestContextGetter* context, | 68 void(const GURL&, |
| 69 net::URLRequestContextGetter* context, |
69 const std::string& access_token, | 70 const std::string& access_token, |
70 const std::string& serialized_request, | 71 const std::string& serialized_request, |
71 const ResultCallback& result_callback, | 72 const ResultCallback& result_callback, |
72 const ErrorCallback& error_callback)); | 73 const ErrorCallback& error_callback)); |
73 | 74 |
74 private: | 75 private: |
75 DISALLOW_COPY_AND_ASSIGN(MockCryptAuthApiCallFlow); | 76 DISALLOW_COPY_AND_ASSIGN(MockCryptAuthApiCallFlow); |
76 }; | 77 }; |
77 | 78 |
78 // Subclass of CryptAuthClient to use as test harness. | |
79 class MockCryptAuthClient : public CryptAuthClient { | |
80 public: | |
81 // Ownership of |access_token_fetcher| is passed to the superclass. Due to the | |
82 // limitations of gmock, we need to use a raw pointer argument rather than a | |
83 // scoped_ptr. | |
84 MockCryptAuthClient( | |
85 CryptAuthAccessTokenFetcher* access_token_fetcher, | |
86 scoped_refptr<net::URLRequestContextGetter> url_request_context, | |
87 const cryptauth::DeviceClassifier& device_classifier) | |
88 : CryptAuthClient(make_scoped_ptr(access_token_fetcher), | |
89 url_request_context, | |
90 device_classifier) {} | |
91 virtual ~MockCryptAuthClient() {} | |
92 | |
93 MOCK_METHOD1(CreateFlowProxy, CryptAuthApiCallFlow*(const GURL& request_url)); | |
94 | |
95 scoped_ptr<CryptAuthApiCallFlow> CreateFlow( | |
96 const GURL& request_url) override { | |
97 return make_scoped_ptr(CreateFlowProxy(request_url)); | |
98 }; | |
99 | |
100 private: | |
101 DISALLOW_COPY_AND_ASSIGN(MockCryptAuthClient); | |
102 }; | |
103 | |
104 // Callback that should never be invoked. | 79 // Callback that should never be invoked. |
105 template <class T> | 80 template <class T> |
106 void NotCalled(const T& type) { | 81 void NotCalled(const T& type) { |
107 EXPECT_TRUE(false); | 82 EXPECT_TRUE(false); |
108 } | 83 } |
109 | 84 |
110 // Callback that saves the result returned by CryptAuthClient. | 85 // Callback that saves the result returned by CryptAuthClient. |
111 template <class T> | 86 template <class T> |
112 void SaveResult(T* out, const T& result) { | 87 void SaveResult(T* out, const T& result) { |
113 *out = result; | 88 *out = result; |
114 } | 89 } |
115 | 90 |
116 } // namespace | 91 } // namespace |
117 | 92 |
118 class ProximityAuthCryptAuthClientTest : public testing::Test { | 93 class ProximityAuthCryptAuthClientTest : public testing::Test { |
119 protected: | 94 protected: |
120 ProximityAuthCryptAuthClientTest() | 95 ProximityAuthCryptAuthClientTest() |
121 : access_token_fetcher_(new FakeCryptAuthAccessTokenFetcher()), | 96 : access_token_fetcher_(new FakeCryptAuthAccessTokenFetcher()), |
| 97 api_call_flow_(new StrictMock<MockCryptAuthApiCallFlow>()), |
122 url_request_context_( | 98 url_request_context_( |
123 new net::TestURLRequestContextGetter(new base::NullTaskRunner())), | 99 new net::TestURLRequestContextGetter(new base::NullTaskRunner())), |
124 serialized_request_(std::string()) {} | 100 serialized_request_(std::string()) {} |
125 | 101 |
126 void SetUp() override { | 102 void SetUp() override { |
127 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 103 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
128 switches::kCryptAuthHTTPHost, kTestGoogleApisUrl); | 104 switches::kCryptAuthHTTPHost, kTestGoogleApisUrl); |
129 | 105 |
130 cryptauth::DeviceClassifier device_classifier; | 106 cryptauth::DeviceClassifier device_classifier; |
131 device_classifier.set_device_os_version_code(kDeviceOsVersionCode); | 107 device_classifier.set_device_os_version_code(kDeviceOsVersionCode); |
132 device_classifier.set_device_software_version_code( | 108 device_classifier.set_device_software_version_code( |
133 kDeviceSoftwareVersionCode); | 109 kDeviceSoftwareVersionCode); |
134 device_classifier.set_device_software_package(kDeviceSoftwarePackage); | 110 device_classifier.set_device_software_package(kDeviceSoftwarePackage); |
135 device_classifier.set_device_type(kDeviceType); | 111 device_classifier.set_device_type(kDeviceType); |
136 | 112 |
137 client_.reset(new StrictMock<MockCryptAuthClient>( | 113 client_.reset(new CryptAuthClientImpl( |
138 access_token_fetcher_, url_request_context_, device_classifier)); | 114 make_scoped_ptr(api_call_flow_), make_scoped_ptr(access_token_fetcher_), |
| 115 url_request_context_, device_classifier)); |
139 } | 116 } |
140 | 117 |
141 // Sets up an expectation and captures a CryptAuth API request to | 118 // Sets up an expectation and captures a CryptAuth API request to |
142 // |request_url|. | 119 // |request_url|. |
143 void ExpectRequest(const std::string& request_url) { | 120 void ExpectRequest(const std::string& request_url) { |
144 StrictMock<MockCryptAuthApiCallFlow>* api_call_flow = | 121 GURL url(request_url); |
145 new StrictMock<MockCryptAuthApiCallFlow>(); | 122 EXPECT_CALL(*api_call_flow_, |
146 | 123 Start(url, url_request_context_.get(), kAccessToken, _, _, _)) |
147 EXPECT_CALL(*client_, CreateFlowProxy(GURL(request_url))) | 124 .WillOnce(DoAll(SaveArg<3>(&serialized_request_), |
148 .WillOnce(Return(api_call_flow)); | 125 SaveArg<4>(&flow_result_callback_), |
149 | 126 SaveArg<5>(&flow_error_callback_))); |
150 EXPECT_CALL(*api_call_flow, | |
151 Start(url_request_context_.get(), kAccessToken, _, _, _)) | |
152 .WillOnce(DoAll(SaveArg<2>(&serialized_request_), | |
153 SaveArg<3>(&flow_result_callback_), | |
154 SaveArg<4>(&flow_error_callback_))); | |
155 } | 127 } |
156 | 128 |
157 // Returns |response_proto| as the result to the current API request. | 129 // Returns |response_proto| as the result to the current API request. |
158 // ExpectResult() must have been called first. | 130 // ExpectResult() must have been called first. |
159 void FinishApiCallFlow(const google::protobuf::MessageLite* response_proto) { | 131 void FinishApiCallFlow(const google::protobuf::MessageLite* response_proto) { |
160 flow_result_callback_.Run(response_proto->SerializeAsString()); | 132 flow_result_callback_.Run(response_proto->SerializeAsString()); |
161 } | 133 } |
162 | 134 |
163 // Ends the current API request with |error_message|. ExpectResult() must have | 135 // Ends the current API request with |error_message|. ExpectResult() must have |
164 // been called first. | 136 // been called first. |
165 void FailApiCallFlow(const std::string& error_message) { | 137 void FailApiCallFlow(const std::string& error_message) { |
166 flow_error_callback_.Run(error_message); | 138 flow_error_callback_.Run(error_message); |
167 } | 139 } |
168 | 140 |
169 protected: | 141 protected: |
170 // Owned by |client_|. | 142 // Owned by |client_|. |
171 FakeCryptAuthAccessTokenFetcher* access_token_fetcher_; | 143 FakeCryptAuthAccessTokenFetcher* access_token_fetcher_; |
| 144 // Owned by |client_|. |
| 145 StrictMock<MockCryptAuthApiCallFlow>* api_call_flow_; |
172 | 146 |
173 scoped_refptr<net::URLRequestContextGetter> url_request_context_; | 147 scoped_refptr<net::URLRequestContextGetter> url_request_context_; |
174 scoped_ptr<StrictMock<MockCryptAuthClient>> client_; | 148 scoped_ptr<CryptAuthClient> client_; |
175 | 149 |
176 std::string serialized_request_; | 150 std::string serialized_request_; |
177 CryptAuthApiCallFlow::ResultCallback flow_result_callback_; | 151 CryptAuthApiCallFlow::ResultCallback flow_result_callback_; |
178 CryptAuthApiCallFlow::ErrorCallback flow_error_callback_; | 152 CryptAuthApiCallFlow::ErrorCallback flow_error_callback_; |
179 }; | 153 }; |
180 | 154 |
181 TEST_F(ProximityAuthCryptAuthClientTest, GetMyDevicesSuccess) { | 155 TEST_F(ProximityAuthCryptAuthClientTest, GetMyDevicesSuccess) { |
182 ExpectRequest( | 156 ExpectRequest( |
183 "https://www.testgoogleapis.com/cryptauth/v1/deviceSync/" | 157 "https://www.testgoogleapis.com/cryptauth/v1/deviceSync/" |
184 "getmydevices?alt=proto"); | 158 "getmydevices?alt=proto"); |
(...skipping 375 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
560 expected_request.device_classifier(); | 534 expected_request.device_classifier(); |
561 EXPECT_EQ(kDeviceOsVersionCode, device_classifier.device_os_version_code()); | 535 EXPECT_EQ(kDeviceOsVersionCode, device_classifier.device_os_version_code()); |
562 EXPECT_EQ(kDeviceSoftwareVersionCode, | 536 EXPECT_EQ(kDeviceSoftwareVersionCode, |
563 device_classifier.device_software_version_code()); | 537 device_classifier.device_software_version_code()); |
564 EXPECT_EQ(kDeviceSoftwarePackage, | 538 EXPECT_EQ(kDeviceSoftwarePackage, |
565 device_classifier.device_software_package()); | 539 device_classifier.device_software_package()); |
566 EXPECT_EQ(kDeviceType, device_classifier.device_type()); | 540 EXPECT_EQ(kDeviceType, device_classifier.device_type()); |
567 } | 541 } |
568 | 542 |
569 } // namespace proximity_auth | 543 } // namespace proximity_auth |
OLD | NEW |