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