Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(528)

Side by Side Diff: blimp/client/core/session/assignment_source_unittest.cc

Issue 2211613002: Add AssignmentSource to BlimpClientContextImpl (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: merge origin/master Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « blimp/client/core/session/assignment_source.cc ('k') | blimp/client/public/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "blimp/client/core/session/assignment_source.h" 5 #include "blimp/client/core/session/assignment_source.h"
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/files/file_path.h" 8 #include "base/files/file_path.h"
9 #include "base/files/file_util.h" 9 #include "base/files/file_util.h"
10 #include "base/json/json_reader.h" 10 #include "base/json/json_reader.h"
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
143 // Send the fake response back. 143 // Send the fake response back.
144 fetcher->set_response_code(response_code); 144 fetcher->set_response_code(response_code);
145 fetcher->set_status(net::URLRequestStatus::FromError(status)); 145 fetcher->set_status(net::URLRequestStatus::FromError(status));
146 fetcher->SetResponseString(response); 146 fetcher->SetResponseString(response);
147 fetcher->delegate()->OnURLFetchComplete(fetcher); 147 fetcher->delegate()->OnURLFetchComplete(fetcher);
148 148
149 base::RunLoop().RunUntilIdle(); 149 base::RunLoop().RunUntilIdle();
150 } 150 }
151 151
152 MOCK_METHOD2(AssignmentResponse, 152 MOCK_METHOD2(AssignmentResponse,
153 void(AssignmentSource::Result, const Assignment&)); 153 void(AssignmentRequestResult, const Assignment&));
154 154
155 protected: 155 protected:
156 Assignment BuildSslAssignment(); 156 Assignment BuildSslAssignment();
157 157
158 // Builds simulated JSON response from the Assigner service. 158 // Builds simulated JSON response from the Assigner service.
159 std::unique_ptr<base::DictionaryValue> BuildAssignerResponse(); 159 std::unique_ptr<base::DictionaryValue> BuildAssignerResponse();
160 160
161 // Used to drive all AssignmentSource tasks. 161 // Used to drive all AssignmentSource tasks.
162 // MessageLoop is required by TestingJsonParser's self-deletion logic. 162 // MessageLoop is required by TestingJsonParser's self-deletion logic.
163 // TODO(bauerb): Replace this with a TestSimpleTaskRunner once 163 // TODO(bauerb): Replace this with a TestSimpleTaskRunner once
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
214 cmd_line->AppendSwitchASCII(switches::kEngineIP, kTestIpAddressString); 214 cmd_line->AppendSwitchASCII(switches::kEngineIP, kTestIpAddressString);
215 cmd_line->AppendSwitchASCII(switches::kEnginePort, 215 cmd_line->AppendSwitchASCII(switches::kEnginePort,
216 std::to_string(kTestPort)); 216 std::to_string(kTestPort));
217 cmd_line->AppendSwitchASCII(switches::kEngineTransport, kTcpTransportName); 217 cmd_line->AppendSwitchASCII(switches::kEngineTransport, kTcpTransportName);
218 cmd_line->AppendSwitchASCII(kClientTokenPath, client_token_path_.value()); 218 cmd_line->AppendSwitchASCII(kClientTokenPath, client_token_path_.value());
219 219
220 assignment.client_token = GetClientToken(*cmd_line); 220 assignment.client_token = GetClientToken(*cmd_line);
221 221
222 CHECK_EQ("MyVoiceIsMyPassport", assignment.client_token); 222 CHECK_EQ("MyVoiceIsMyPassport", assignment.client_token);
223 223
224 EXPECT_CALL(*this, AssignmentResponse(AssignmentSource::Result::RESULT_OK, 224 EXPECT_CALL(*this, AssignmentResponse(ASSIGNMENT_REQUEST_RESULT_OK,
225 AssignmentEquals(assignment))) 225 AssignmentEquals(assignment)))
226 .Times(1); 226 .Times(1);
227 227
228 GetAlternateAssignment(); 228 GetAlternateAssignment();
229 } 229 }
230 230
231 TEST_F(AssignmentSourceTest, TestSSLAlternateEndpointSuccess) { 231 TEST_F(AssignmentSourceTest, TestSSLAlternateEndpointSuccess) {
232 Assignment assignment; 232 Assignment assignment;
233 assignment.transport_protocol = Assignment::TransportProtocol::SSL; 233 assignment.transport_protocol = Assignment::TransportProtocol::SSL;
234 assignment.engine_endpoint = net::IPEndPoint(kTestIpAddress, kTestPort); 234 assignment.engine_endpoint = net::IPEndPoint(kTestIpAddress, kTestPort);
235 assignment.cert = cert_; 235 assignment.cert = cert_;
236 236
237 auto* cmd_line = base::CommandLine::ForCurrentProcess(); 237 auto* cmd_line = base::CommandLine::ForCurrentProcess();
238 238
239 cmd_line->AppendSwitchASCII(switches::kEngineIP, kTestIpAddressString); 239 cmd_line->AppendSwitchASCII(switches::kEngineIP, kTestIpAddressString);
240 cmd_line->AppendSwitchASCII(switches::kEnginePort, 240 cmd_line->AppendSwitchASCII(switches::kEnginePort,
241 std::to_string(kTestPort)); 241 std::to_string(kTestPort));
242 cmd_line->AppendSwitchASCII(switches::kEngineTransport, kSslTransportName); 242 cmd_line->AppendSwitchASCII(switches::kEngineTransport, kSslTransportName);
243 cmd_line->AppendSwitchASCII(switches::kEngineCertPath, cert_path_.value()); 243 cmd_line->AppendSwitchASCII(switches::kEngineCertPath, cert_path_.value());
244 cmd_line->AppendSwitchASCII(kClientTokenPath, client_token_path_.value()); 244 cmd_line->AppendSwitchASCII(kClientTokenPath, client_token_path_.value());
245 245
246 assignment.client_token = GetClientToken(*cmd_line); 246 assignment.client_token = GetClientToken(*cmd_line);
247 247
248 EXPECT_CALL(*this, AssignmentResponse(AssignmentSource::Result::RESULT_OK, 248 EXPECT_CALL(*this, AssignmentResponse(ASSIGNMENT_REQUEST_RESULT_OK,
249 AssignmentEquals(assignment))) 249 AssignmentEquals(assignment)))
250 .Times(1); 250 .Times(1);
251 251
252 GetAlternateAssignment(); 252 GetAlternateAssignment();
253 } 253 }
254 254
255 TEST_F(AssignmentSourceTest, TestSuccess) { 255 TEST_F(AssignmentSourceTest, TestSuccess) {
256 Assignment assignment = BuildSslAssignment(); 256 Assignment assignment = BuildSslAssignment();
257 257
258 EXPECT_CALL(*this, AssignmentResponse(AssignmentSource::Result::RESULT_OK, 258 EXPECT_CALL(*this, AssignmentResponse(ASSIGNMENT_REQUEST_RESULT_OK,
259 AssignmentEquals(assignment))) 259 AssignmentEquals(assignment)))
260 .Times(1); 260 .Times(1);
261 261
262 GetNetworkAssignmentAndWaitForResponse( 262 GetNetworkAssignmentAndWaitForResponse(
263 net::HTTP_OK, net::Error::OK, ValueToString(*BuildAssignerResponse()), 263 net::HTTP_OK, net::Error::OK, ValueToString(*BuildAssignerResponse()),
264 kTestAuthToken, kProtocolVersion); 264 kTestAuthToken, kProtocolVersion);
265 } 265 }
266 266
267 TEST_F(AssignmentSourceTest, TestValidAfterError) { 267 TEST_F(AssignmentSourceTest, TestValidAfterError) {
268 InSequence sequence; 268 InSequence sequence;
269 Assignment assignment = BuildSslAssignment(); 269 Assignment assignment = BuildSslAssignment();
270 270
271 EXPECT_CALL(*this, AssignmentResponse( 271 EXPECT_CALL(*this,
272 AssignmentSource::Result::RESULT_NETWORK_FAILURE, _)) 272 AssignmentResponse(ASSIGNMENT_REQUEST_RESULT_NETWORK_FAILURE, _))
273 .Times(1) 273 .Times(1)
274 .RetiresOnSaturation(); 274 .RetiresOnSaturation();
275 275
276 EXPECT_CALL(*this, AssignmentResponse(AssignmentSource::Result::RESULT_OK, 276 EXPECT_CALL(*this, AssignmentResponse(ASSIGNMENT_REQUEST_RESULT_OK,
277 AssignmentEquals(assignment))) 277 AssignmentEquals(assignment)))
278 .Times(1) 278 .Times(1)
279 .RetiresOnSaturation(); 279 .RetiresOnSaturation();
280 280
281 GetNetworkAssignmentAndWaitForResponse(net::HTTP_OK, 281 GetNetworkAssignmentAndWaitForResponse(net::HTTP_OK,
282 net::Error::ERR_INSUFFICIENT_RESOURCES, 282 net::Error::ERR_INSUFFICIENT_RESOURCES,
283 "", kTestAuthToken, kProtocolVersion); 283 "", kTestAuthToken, kProtocolVersion);
284 284
285 GetNetworkAssignmentAndWaitForResponse( 285 GetNetworkAssignmentAndWaitForResponse(
286 net::HTTP_OK, net::Error::OK, ValueToString(*BuildAssignerResponse()), 286 net::HTTP_OK, net::Error::OK, ValueToString(*BuildAssignerResponse()),
287 kTestAuthToken, kProtocolVersion); 287 kTestAuthToken, kProtocolVersion);
288 } 288 }
289 289
290 TEST_F(AssignmentSourceTest, TestNetworkFailure) { 290 TEST_F(AssignmentSourceTest, TestNetworkFailure) {
291 EXPECT_CALL(*this, AssignmentResponse( 291 EXPECT_CALL(*this,
292 AssignmentSource::Result::RESULT_NETWORK_FAILURE, _)); 292 AssignmentResponse(ASSIGNMENT_REQUEST_RESULT_NETWORK_FAILURE, _));
293 GetNetworkAssignmentAndWaitForResponse(net::HTTP_OK, 293 GetNetworkAssignmentAndWaitForResponse(net::HTTP_OK,
294 net::Error::ERR_INSUFFICIENT_RESOURCES, 294 net::Error::ERR_INSUFFICIENT_RESOURCES,
295 "", kTestAuthToken, kProtocolVersion); 295 "", kTestAuthToken, kProtocolVersion);
296 } 296 }
297 297
298 TEST_F(AssignmentSourceTest, TestBadRequest) { 298 TEST_F(AssignmentSourceTest, TestBadRequest) {
299 EXPECT_CALL(*this, AssignmentResponse( 299 EXPECT_CALL(*this,
300 AssignmentSource::Result::RESULT_BAD_REQUEST, _)); 300 AssignmentResponse(ASSIGNMENT_REQUEST_RESULT_BAD_REQUEST, _));
301 GetNetworkAssignmentAndWaitForResponse(net::HTTP_BAD_REQUEST, net::Error::OK, 301 GetNetworkAssignmentAndWaitForResponse(net::HTTP_BAD_REQUEST, net::Error::OK,
302 "", kTestAuthToken, kProtocolVersion); 302 "", kTestAuthToken, kProtocolVersion);
303 } 303 }
304 304
305 TEST_F(AssignmentSourceTest, TestUnauthorized) { 305 TEST_F(AssignmentSourceTest, TestUnauthorized) {
306 EXPECT_CALL(*this, 306 EXPECT_CALL(*this, AssignmentResponse(
307 AssignmentResponse( 307 ASSIGNMENT_REQUEST_RESULT_EXPIRED_ACCESS_TOKEN, _));
308 AssignmentSource::Result::RESULT_EXPIRED_ACCESS_TOKEN, _));
309 GetNetworkAssignmentAndWaitForResponse(net::HTTP_UNAUTHORIZED, net::Error::OK, 308 GetNetworkAssignmentAndWaitForResponse(net::HTTP_UNAUTHORIZED, net::Error::OK,
310 "", kTestAuthToken, kProtocolVersion); 309 "", kTestAuthToken, kProtocolVersion);
311 } 310 }
312 311
313 TEST_F(AssignmentSourceTest, TestForbidden) { 312 TEST_F(AssignmentSourceTest, TestForbidden) {
314 EXPECT_CALL(*this, AssignmentResponse( 313 EXPECT_CALL(*this,
315 AssignmentSource::Result::RESULT_USER_INVALID, _)); 314 AssignmentResponse(ASSIGNMENT_REQUEST_RESULT_USER_INVALID, _));
316 GetNetworkAssignmentAndWaitForResponse(net::HTTP_FORBIDDEN, net::Error::OK, 315 GetNetworkAssignmentAndWaitForResponse(net::HTTP_FORBIDDEN, net::Error::OK,
317 "", kTestAuthToken, kProtocolVersion); 316 "", kTestAuthToken, kProtocolVersion);
318 } 317 }
319 318
320 TEST_F(AssignmentSourceTest, TestTooManyRequests) { 319 TEST_F(AssignmentSourceTest, TestTooManyRequests) {
321 EXPECT_CALL(*this, AssignmentResponse( 320 EXPECT_CALL(*this,
322 AssignmentSource::Result::RESULT_OUT_OF_VMS, _)); 321 AssignmentResponse(ASSIGNMENT_REQUEST_RESULT_OUT_OF_VMS, _));
323 GetNetworkAssignmentAndWaitForResponse(static_cast<net::HttpStatusCode>(429), 322 GetNetworkAssignmentAndWaitForResponse(static_cast<net::HttpStatusCode>(429),
324 net::Error::OK, "", kTestAuthToken, 323 net::Error::OK, "", kTestAuthToken,
325 kProtocolVersion); 324 kProtocolVersion);
326 } 325 }
327 326
328 TEST_F(AssignmentSourceTest, TestInternalServerError) { 327 TEST_F(AssignmentSourceTest, TestInternalServerError) {
329 EXPECT_CALL(*this, AssignmentResponse( 328 EXPECT_CALL(*this,
330 AssignmentSource::Result::RESULT_SERVER_ERROR, _)); 329 AssignmentResponse(ASSIGNMENT_REQUEST_RESULT_SERVER_ERROR, _));
331 GetNetworkAssignmentAndWaitForResponse(net::HTTP_INTERNAL_SERVER_ERROR, 330 GetNetworkAssignmentAndWaitForResponse(net::HTTP_INTERNAL_SERVER_ERROR,
332 net::Error::OK, "", kTestAuthToken, 331 net::Error::OK, "", kTestAuthToken,
333 kProtocolVersion); 332 kProtocolVersion);
334 } 333 }
335 334
336 TEST_F(AssignmentSourceTest, TestUnexpectedNetCodeFallback) { 335 TEST_F(AssignmentSourceTest, TestUnexpectedNetCodeFallback) {
337 EXPECT_CALL(*this, AssignmentResponse( 336 EXPECT_CALL(*this,
338 AssignmentSource::Result::RESULT_BAD_RESPONSE, _)); 337 AssignmentResponse(ASSIGNMENT_REQUEST_RESULT_BAD_RESPONSE, _));
339 GetNetworkAssignmentAndWaitForResponse(net::HTTP_NOT_IMPLEMENTED, 338 GetNetworkAssignmentAndWaitForResponse(net::HTTP_NOT_IMPLEMENTED,
340 net::Error::OK, "", kTestAuthToken, 339 net::Error::OK, "", kTestAuthToken,
341 kProtocolVersion); 340 kProtocolVersion);
342 } 341 }
343 342
344 TEST_F(AssignmentSourceTest, TestInvalidJsonResponse) { 343 TEST_F(AssignmentSourceTest, TestInvalidJsonResponse) {
345 Assignment assignment = BuildSslAssignment(); 344 Assignment assignment = BuildSslAssignment();
346 345
347 // Remove half the response. 346 // Remove half the response.
348 std::string response = ValueToString(*BuildAssignerResponse()); 347 std::string response = ValueToString(*BuildAssignerResponse());
349 response = response.substr(response.size() / 2); 348 response = response.substr(response.size() / 2);
350 349
351 EXPECT_CALL(*this, AssignmentResponse( 350 EXPECT_CALL(*this,
352 AssignmentSource::Result::RESULT_BAD_RESPONSE, _)); 351 AssignmentResponse(ASSIGNMENT_REQUEST_RESULT_BAD_RESPONSE, _));
353 GetNetworkAssignmentAndWaitForResponse(net::HTTP_OK, net::Error::OK, response, 352 GetNetworkAssignmentAndWaitForResponse(net::HTTP_OK, net::Error::OK, response,
354 kTestAuthToken, kProtocolVersion); 353 kTestAuthToken, kProtocolVersion);
355 } 354 }
356 355
357 TEST_F(AssignmentSourceTest, TestMissingResponsePort) { 356 TEST_F(AssignmentSourceTest, TestMissingResponsePort) {
358 std::unique_ptr<base::DictionaryValue> response = BuildAssignerResponse(); 357 std::unique_ptr<base::DictionaryValue> response = BuildAssignerResponse();
359 response->Remove("port", nullptr); 358 response->Remove("port", nullptr);
360 EXPECT_CALL(*this, AssignmentResponse( 359 EXPECT_CALL(*this,
361 AssignmentSource::Result::RESULT_BAD_RESPONSE, _)); 360 AssignmentResponse(ASSIGNMENT_REQUEST_RESULT_BAD_RESPONSE, _));
362 GetNetworkAssignmentAndWaitForResponse(net::HTTP_OK, net::Error::OK, 361 GetNetworkAssignmentAndWaitForResponse(net::HTTP_OK, net::Error::OK,
363 ValueToString(*response), 362 ValueToString(*response),
364 kTestAuthToken, kProtocolVersion); 363 kTestAuthToken, kProtocolVersion);
365 } 364 }
366 365
367 TEST_F(AssignmentSourceTest, TestInvalidIPAddress) { 366 TEST_F(AssignmentSourceTest, TestInvalidIPAddress) {
368 std::unique_ptr<base::DictionaryValue> response = BuildAssignerResponse(); 367 std::unique_ptr<base::DictionaryValue> response = BuildAssignerResponse();
369 response->SetString("host", "happywhales.test"); 368 response->SetString("host", "happywhales.test");
370 369
371 EXPECT_CALL(*this, AssignmentResponse( 370 EXPECT_CALL(*this,
372 AssignmentSource::Result::RESULT_BAD_RESPONSE, _)); 371 AssignmentResponse(ASSIGNMENT_REQUEST_RESULT_BAD_RESPONSE, _));
373 GetNetworkAssignmentAndWaitForResponse(net::HTTP_OK, net::Error::OK, 372 GetNetworkAssignmentAndWaitForResponse(net::HTTP_OK, net::Error::OK,
374 ValueToString(*response), 373 ValueToString(*response),
375 kTestAuthToken, kProtocolVersion); 374 kTestAuthToken, kProtocolVersion);
376 } 375 }
377 376
378 TEST_F(AssignmentSourceTest, TestMissingCert) { 377 TEST_F(AssignmentSourceTest, TestMissingCert) {
379 std::unique_ptr<base::DictionaryValue> response = BuildAssignerResponse(); 378 std::unique_ptr<base::DictionaryValue> response = BuildAssignerResponse();
380 response->Remove("certificate", nullptr); 379 response->Remove("certificate", nullptr);
381 EXPECT_CALL(*this, AssignmentResponse( 380 EXPECT_CALL(*this,
382 AssignmentSource::Result::RESULT_BAD_RESPONSE, _)); 381 AssignmentResponse(ASSIGNMENT_REQUEST_RESULT_BAD_RESPONSE, _));
383 GetNetworkAssignmentAndWaitForResponse(net::HTTP_OK, net::Error::OK, 382 GetNetworkAssignmentAndWaitForResponse(net::HTTP_OK, net::Error::OK,
384 ValueToString(*response), 383 ValueToString(*response),
385 kTestAuthToken, kProtocolVersion); 384 kTestAuthToken, kProtocolVersion);
386 } 385 }
387 386
388 TEST_F(AssignmentSourceTest, TestInvalidCert) { 387 TEST_F(AssignmentSourceTest, TestInvalidCert) {
389 std::unique_ptr<base::DictionaryValue> response = BuildAssignerResponse(); 388 std::unique_ptr<base::DictionaryValue> response = BuildAssignerResponse();
390 response->SetString("certificate", "h4x0rz!"); 389 response->SetString("certificate", "h4x0rz!");
391 EXPECT_CALL(*this, AssignmentResponse( 390 EXPECT_CALL(*this,
392 AssignmentSource::Result::RESULT_INVALID_CERT, _)); 391 AssignmentResponse(ASSIGNMENT_REQUEST_RESULT_INVALID_CERT, _));
393 GetNetworkAssignmentAndWaitForResponse(net::HTTP_OK, net::Error::OK, 392 GetNetworkAssignmentAndWaitForResponse(net::HTTP_OK, net::Error::OK,
394 ValueToString(*response), 393 ValueToString(*response),
395 kTestAuthToken, kProtocolVersion); 394 kTestAuthToken, kProtocolVersion);
396 } 395 }
397 396
398 } // namespace 397 } // namespace
399 } // namespace client 398 } // namespace client
400 } // namespace blimp 399 } // namespace blimp
OLDNEW
« no previous file with comments | « blimp/client/core/session/assignment_source.cc ('k') | blimp/client/public/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698