| OLD | NEW |
| 1 // Copyright (c) 2011 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 <string> | 5 #include <string> |
| 6 | 6 |
| 7 #include "base/strings/string_util.h" | 7 #include "base/strings/string_util.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "net/base/net_errors.h" | 9 #include "net/base/net_errors.h" |
| 10 #include "net/base/test_completion_callback.h" | 10 #include "net/base/test_completion_callback.h" |
| 11 #include "net/http/http_auth_challenge_tokenizer.h" | 11 #include "net/http/http_auth_challenge_tokenizer.h" |
| 12 #include "net/http/http_auth_handler_digest.h" | 12 #include "net/http/http_auth_handler_digest.h" |
| 13 #include "net/http/http_request_info.h" | 13 #include "net/http/http_request_info.h" |
| 14 #include "net/http/http_response_info.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 16 |
| 16 namespace net { | 17 namespace net { |
| 17 | 18 |
| 18 namespace { | 19 namespace { |
| 19 | 20 |
| 20 const char* const kSimpleChallenge = | 21 const char* const kSimpleChallenge = |
| 21 "Digest realm=\"Oblivion\", nonce=\"nonce-value\""; | 22 "Digest realm=\"Oblivion\", nonce=\"nonce-value\""; |
| 22 | 23 |
| 23 // RespondToChallenge creates an HttpAuthHandlerDigest for the specified | 24 // RespondToChallenge creates an HttpAuthHandlerDigest for the specified |
| (...skipping 21 matching lines...) Expand all Loading... |
| 45 | 46 |
| 46 token->clear(); | 47 token->clear(); |
| 47 scoped_ptr<HttpAuthHandlerDigest::Factory> factory( | 48 scoped_ptr<HttpAuthHandlerDigest::Factory> factory( |
| 48 new HttpAuthHandlerDigest::Factory()); | 49 new HttpAuthHandlerDigest::Factory()); |
| 49 HttpAuthHandlerDigest::NonceGenerator* nonce_generator = | 50 HttpAuthHandlerDigest::NonceGenerator* nonce_generator = |
| 50 new HttpAuthHandlerDigest::FixedNonceGenerator("client_nonce"); | 51 new HttpAuthHandlerDigest::FixedNonceGenerator("client_nonce"); |
| 51 factory->set_nonce_generator(nonce_generator); | 52 factory->set_nonce_generator(nonce_generator); |
| 52 scoped_ptr<HttpAuthHandler> handler; | 53 scoped_ptr<HttpAuthHandler> handler; |
| 53 | 54 |
| 54 // Create a handler for a particular challenge. | 55 // Create a handler for a particular challenge. |
| 56 HttpResponseInfo response_info; |
| 55 GURL url_origin(target == HttpAuth::AUTH_SERVER ? request_url : proxy_name); | 57 GURL url_origin(target == HttpAuth::AUTH_SERVER ? request_url : proxy_name); |
| 56 int rv_create = factory->CreateAuthHandlerFromString( | 58 int rv_create = factory->CreateAuthHandlerFromString( |
| 57 challenge, target, url_origin.GetOrigin(), BoundNetLog(), &handler); | 59 challenge, target, response_info, url_origin.GetOrigin(), BoundNetLog(), |
| 60 &handler); |
| 58 if (rv_create != OK || handler.get() == NULL) { | 61 if (rv_create != OK || handler.get() == NULL) { |
| 59 ADD_FAILURE() << "Unable to create auth handler."; | 62 ADD_FAILURE() << "Unable to create auth handler."; |
| 60 return false; | 63 return false; |
| 61 } | 64 } |
| 62 | 65 |
| 63 // Create a token in response to the challenge. | 66 // Create a token in response to the challenge. |
| 64 // NOTE: HttpAuthHandlerDigest's implementation of GenerateAuthToken always | 67 // NOTE: HttpAuthHandlerDigest's implementation of GenerateAuthToken always |
| 65 // completes synchronously. That's why this test can get away with a | 68 // completes synchronously. That's why this test can get away with a |
| 66 // TestCompletionCallback without an IO thread. | 69 // TestCompletionCallback without an IO thread. |
| 67 TestCompletionCallback callback; | 70 TestCompletionCallback callback; |
| (...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 344 false, | 347 false, |
| 345 HttpAuthHandlerDigest::ALGORITHM_UNSPECIFIED, | 348 HttpAuthHandlerDigest::ALGORITHM_UNSPECIFIED, |
| 346 HttpAuthHandlerDigest::QOP_UNSPECIFIED | 349 HttpAuthHandlerDigest::QOP_UNSPECIFIED |
| 347 }, | 350 }, |
| 348 }; | 351 }; |
| 349 | 352 |
| 350 GURL origin("http://www.example.com"); | 353 GURL origin("http://www.example.com"); |
| 351 scoped_ptr<HttpAuthHandlerDigest::Factory> factory( | 354 scoped_ptr<HttpAuthHandlerDigest::Factory> factory( |
| 352 new HttpAuthHandlerDigest::Factory()); | 355 new HttpAuthHandlerDigest::Factory()); |
| 353 for (size_t i = 0; i < arraysize(tests); ++i) { | 356 for (size_t i = 0; i < arraysize(tests); ++i) { |
| 357 HttpResponseInfo null_response_info; |
| 354 scoped_ptr<HttpAuthHandler> handler; | 358 scoped_ptr<HttpAuthHandler> handler; |
| 355 int rv = factory->CreateAuthHandlerFromString(tests[i].challenge, | 359 int rv = factory->CreateAuthHandlerFromString( |
| 356 HttpAuth::AUTH_SERVER, | 360 tests[i].challenge, HttpAuth::AUTH_SERVER, null_response_info, origin, |
| 357 origin, | 361 BoundNetLog(), &handler); |
| 358 BoundNetLog(), | |
| 359 &handler); | |
| 360 if (tests[i].parsed_success) { | 362 if (tests[i].parsed_success) { |
| 361 EXPECT_EQ(OK, rv); | 363 EXPECT_EQ(OK, rv); |
| 362 } else { | 364 } else { |
| 363 EXPECT_NE(OK, rv); | 365 EXPECT_NE(OK, rv); |
| 364 EXPECT_TRUE(handler.get() == NULL); | 366 EXPECT_TRUE(handler.get() == NULL); |
| 365 continue; | 367 continue; |
| 366 } | 368 } |
| 367 ASSERT_TRUE(handler.get() != NULL); | 369 ASSERT_TRUE(handler.get() != NULL); |
| 368 HttpAuthHandlerDigest* digest = | 370 HttpAuthHandlerDigest* digest = |
| 369 static_cast<HttpAuthHandlerDigest*>(handler.get()); | 371 static_cast<HttpAuthHandlerDigest*>(handler.get()); |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 509 "Digest username=\"USER\", realm=\"Baztastic\", " | 511 "Digest username=\"USER\", realm=\"Baztastic\", " |
| 510 "nonce=\"AAAAAAAA\", uri=\"/\", algorithm=MD5-sess, " | 512 "nonce=\"AAAAAAAA\", uri=\"/\", algorithm=MD5-sess, " |
| 511 "response=\"cbc1139821ee7192069580570c541a03\", " | 513 "response=\"cbc1139821ee7192069580570c541a03\", " |
| 512 "qop=auth, nc=00000001, cnonce=\"15c07961ed8575c4\"" | 514 "qop=auth, nc=00000001, cnonce=\"15c07961ed8575c4\"" |
| 513 } | 515 } |
| 514 }; | 516 }; |
| 515 GURL origin("http://www.example.com"); | 517 GURL origin("http://www.example.com"); |
| 516 scoped_ptr<HttpAuthHandlerDigest::Factory> factory( | 518 scoped_ptr<HttpAuthHandlerDigest::Factory> factory( |
| 517 new HttpAuthHandlerDigest::Factory()); | 519 new HttpAuthHandlerDigest::Factory()); |
| 518 for (size_t i = 0; i < arraysize(tests); ++i) { | 520 for (size_t i = 0; i < arraysize(tests); ++i) { |
| 521 HttpResponseInfo null_response_info; |
| 519 scoped_ptr<HttpAuthHandler> handler; | 522 scoped_ptr<HttpAuthHandler> handler; |
| 520 int rv = factory->CreateAuthHandlerFromString(tests[i].challenge, | 523 int rv = factory->CreateAuthHandlerFromString( |
| 521 HttpAuth::AUTH_SERVER, | 524 tests[i].challenge, HttpAuth::AUTH_SERVER, null_response_info, origin, |
| 522 origin, | 525 BoundNetLog(), &handler); |
| 523 BoundNetLog(), | |
| 524 &handler); | |
| 525 EXPECT_EQ(OK, rv); | 526 EXPECT_EQ(OK, rv); |
| 526 ASSERT_TRUE(handler != NULL); | 527 ASSERT_TRUE(handler != NULL); |
| 527 | 528 |
| 528 HttpAuthHandlerDigest* digest = | 529 HttpAuthHandlerDigest* digest = |
| 529 static_cast<HttpAuthHandlerDigest*>(handler.get()); | 530 static_cast<HttpAuthHandlerDigest*>(handler.get()); |
| 530 std::string creds = | 531 std::string creds = |
| 531 digest->AssembleCredentials(tests[i].req_method, | 532 digest->AssembleCredentials(tests[i].req_method, |
| 532 tests[i].req_path, | 533 tests[i].req_path, |
| 533 AuthCredentials( | 534 AuthCredentials( |
| 534 base::ASCIIToUTF16(tests[i].username), | 535 base::ASCIIToUTF16(tests[i].username), |
| 535 base::ASCIIToUTF16(tests[i].password)), | 536 base::ASCIIToUTF16(tests[i].password)), |
| 536 tests[i].cnonce, | 537 tests[i].cnonce, |
| 537 tests[i].nonce_count); | 538 tests[i].nonce_count); |
| 538 | 539 |
| 539 EXPECT_STREQ(tests[i].expected_creds, creds.c_str()); | 540 EXPECT_STREQ(tests[i].expected_creds, creds.c_str()); |
| 540 } | 541 } |
| 541 } | 542 } |
| 542 | 543 |
| 543 TEST(HttpAuthHandlerDigest, HandleAnotherChallenge) { | 544 TEST(HttpAuthHandlerDigest, HandleAnotherChallenge) { |
| 544 scoped_ptr<HttpAuthHandlerDigest::Factory> factory( | 545 scoped_ptr<HttpAuthHandlerDigest::Factory> factory( |
| 545 new HttpAuthHandlerDigest::Factory()); | 546 new HttpAuthHandlerDigest::Factory()); |
| 546 scoped_ptr<HttpAuthHandler> handler; | 547 scoped_ptr<HttpAuthHandler> handler; |
| 547 std::string default_challenge = | 548 std::string default_challenge = |
| 548 "Digest realm=\"Oblivion\", nonce=\"nonce-value\""; | 549 "Digest realm=\"Oblivion\", nonce=\"nonce-value\""; |
| 549 GURL origin("intranet.google.com"); | 550 GURL origin("intranet.google.com"); |
| 551 HttpResponseInfo null_response_info; |
| 550 int rv = factory->CreateAuthHandlerFromString( | 552 int rv = factory->CreateAuthHandlerFromString( |
| 551 default_challenge, HttpAuth::AUTH_SERVER, origin, BoundNetLog(), | 553 default_challenge, HttpAuth::AUTH_SERVER, null_response_info, origin, |
| 552 &handler); | 554 BoundNetLog(), &handler); |
| 553 EXPECT_EQ(OK, rv); | 555 EXPECT_EQ(OK, rv); |
| 554 ASSERT_TRUE(handler.get() != NULL); | 556 ASSERT_TRUE(handler.get() != NULL); |
| 555 HttpAuthChallengeTokenizer tok_default(default_challenge.begin(), | 557 HttpAuthChallengeTokenizer tok_default(default_challenge.begin(), |
| 556 default_challenge.end()); | 558 default_challenge.end()); |
| 557 EXPECT_EQ(HttpAuth::AUTHORIZATION_RESULT_REJECT, | 559 EXPECT_EQ(HttpAuth::AUTHORIZATION_RESULT_REJECT, |
| 558 handler->HandleAnotherChallenge(&tok_default)); | 560 handler->HandleAnotherChallenge(&tok_default)); |
| 559 | 561 |
| 560 std::string stale_challenge = default_challenge + ", stale=true"; | 562 std::string stale_challenge = default_challenge + ", stale=true"; |
| 561 HttpAuthChallengeTokenizer tok_stale(stale_challenge.begin(), | 563 HttpAuthChallengeTokenizer tok_stale(stale_challenge.begin(), |
| 562 stale_challenge.end()); | 564 stale_challenge.end()); |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 688 EXPECT_EQ("Digest username=\"foo\", realm=\"Oblivion\", " | 690 EXPECT_EQ("Digest username=\"foo\", realm=\"Oblivion\", " |
| 689 "nonce=\"nonce-value\", uri=\"/path/to/resource\", " | 691 "nonce=\"nonce-value\", uri=\"/path/to/resource\", " |
| 690 "response=\"5b1459beda5cee30d6ff9e970a69c0ea\", " | 692 "response=\"5b1459beda5cee30d6ff9e970a69c0ea\", " |
| 691 "opaque=\"opaque text\", " | 693 "opaque=\"opaque text\", " |
| 692 "qop=auth, nc=00000001, cnonce=\"client_nonce\"", | 694 "qop=auth, nc=00000001, cnonce=\"client_nonce\"", |
| 693 auth_token); | 695 auth_token); |
| 694 } | 696 } |
| 695 | 697 |
| 696 | 698 |
| 697 } // namespace net | 699 } // namespace net |
| OLD | NEW |