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/ssl/ssl_info.h" | 14 #include "net/ssl/ssl_info.h" |
| 15 #include "net/test/gtest_util.h" |
| 16 #include "testing/gmock/include/gmock/gmock.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
16 | 18 |
| 19 using net::test::IsOk; |
| 20 |
17 namespace net { | 21 namespace net { |
18 | 22 |
19 namespace { | 23 namespace { |
20 | 24 |
21 const char* const kSimpleChallenge = | 25 const char* const kSimpleChallenge = |
22 "Digest realm=\"Oblivion\", nonce=\"nonce-value\""; | 26 "Digest realm=\"Oblivion\", nonce=\"nonce-value\""; |
23 | 27 |
24 // RespondToChallenge creates an HttpAuthHandlerDigest for the specified | 28 // RespondToChallenge creates an HttpAuthHandlerDigest for the specified |
25 // |challenge|, and generates a response to the challenge which is returned in | 29 // |challenge|, and generates a response to the challenge which is returned in |
26 // |token|. | 30 // |token|. |
(...skipping 326 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
353 GURL origin("http://www.example.com"); | 357 GURL origin("http://www.example.com"); |
354 std::unique_ptr<HttpAuthHandlerDigest::Factory> factory( | 358 std::unique_ptr<HttpAuthHandlerDigest::Factory> factory( |
355 new HttpAuthHandlerDigest::Factory()); | 359 new HttpAuthHandlerDigest::Factory()); |
356 for (size_t i = 0; i < arraysize(tests); ++i) { | 360 for (size_t i = 0; i < arraysize(tests); ++i) { |
357 SSLInfo null_ssl_info; | 361 SSLInfo null_ssl_info; |
358 std::unique_ptr<HttpAuthHandler> handler; | 362 std::unique_ptr<HttpAuthHandler> handler; |
359 int rv = factory->CreateAuthHandlerFromString( | 363 int rv = factory->CreateAuthHandlerFromString( |
360 tests[i].challenge, HttpAuth::AUTH_SERVER, null_ssl_info, origin, | 364 tests[i].challenge, HttpAuth::AUTH_SERVER, null_ssl_info, origin, |
361 BoundNetLog(), &handler); | 365 BoundNetLog(), &handler); |
362 if (tests[i].parsed_success) { | 366 if (tests[i].parsed_success) { |
363 EXPECT_EQ(OK, rv); | 367 EXPECT_THAT(rv, IsOk()); |
364 } else { | 368 } else { |
365 EXPECT_NE(OK, rv); | 369 EXPECT_NE(OK, rv); |
366 EXPECT_TRUE(handler.get() == NULL); | 370 EXPECT_TRUE(handler.get() == NULL); |
367 continue; | 371 continue; |
368 } | 372 } |
369 ASSERT_TRUE(handler.get() != NULL); | 373 ASSERT_TRUE(handler.get() != NULL); |
370 HttpAuthHandlerDigest* digest = | 374 HttpAuthHandlerDigest* digest = |
371 static_cast<HttpAuthHandlerDigest*>(handler.get()); | 375 static_cast<HttpAuthHandlerDigest*>(handler.get()); |
372 EXPECT_STREQ(tests[i].parsed_realm, digest->realm_.c_str()); | 376 EXPECT_STREQ(tests[i].parsed_realm, digest->realm_.c_str()); |
373 EXPECT_STREQ(tests[i].parsed_nonce, digest->nonce_.c_str()); | 377 EXPECT_STREQ(tests[i].parsed_nonce, digest->nonce_.c_str()); |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
516 }; | 520 }; |
517 GURL origin("http://www.example.com"); | 521 GURL origin("http://www.example.com"); |
518 std::unique_ptr<HttpAuthHandlerDigest::Factory> factory( | 522 std::unique_ptr<HttpAuthHandlerDigest::Factory> factory( |
519 new HttpAuthHandlerDigest::Factory()); | 523 new HttpAuthHandlerDigest::Factory()); |
520 for (size_t i = 0; i < arraysize(tests); ++i) { | 524 for (size_t i = 0; i < arraysize(tests); ++i) { |
521 SSLInfo null_ssl_info; | 525 SSLInfo null_ssl_info; |
522 std::unique_ptr<HttpAuthHandler> handler; | 526 std::unique_ptr<HttpAuthHandler> handler; |
523 int rv = factory->CreateAuthHandlerFromString( | 527 int rv = factory->CreateAuthHandlerFromString( |
524 tests[i].challenge, HttpAuth::AUTH_SERVER, null_ssl_info, origin, | 528 tests[i].challenge, HttpAuth::AUTH_SERVER, null_ssl_info, origin, |
525 BoundNetLog(), &handler); | 529 BoundNetLog(), &handler); |
526 EXPECT_EQ(OK, rv); | 530 EXPECT_THAT(rv, IsOk()); |
527 ASSERT_TRUE(handler != NULL); | 531 ASSERT_TRUE(handler != NULL); |
528 | 532 |
529 HttpAuthHandlerDigest* digest = | 533 HttpAuthHandlerDigest* digest = |
530 static_cast<HttpAuthHandlerDigest*>(handler.get()); | 534 static_cast<HttpAuthHandlerDigest*>(handler.get()); |
531 std::string creds = | 535 std::string creds = |
532 digest->AssembleCredentials(tests[i].req_method, | 536 digest->AssembleCredentials(tests[i].req_method, |
533 tests[i].req_path, | 537 tests[i].req_path, |
534 AuthCredentials( | 538 AuthCredentials( |
535 base::ASCIIToUTF16(tests[i].username), | 539 base::ASCIIToUTF16(tests[i].username), |
536 base::ASCIIToUTF16(tests[i].password)), | 540 base::ASCIIToUTF16(tests[i].password)), |
537 tests[i].cnonce, | 541 tests[i].cnonce, |
538 tests[i].nonce_count); | 542 tests[i].nonce_count); |
539 | 543 |
540 EXPECT_STREQ(tests[i].expected_creds, creds.c_str()); | 544 EXPECT_STREQ(tests[i].expected_creds, creds.c_str()); |
541 } | 545 } |
542 } | 546 } |
543 | 547 |
544 TEST(HttpAuthHandlerDigest, HandleAnotherChallenge) { | 548 TEST(HttpAuthHandlerDigest, HandleAnotherChallenge) { |
545 std::unique_ptr<HttpAuthHandlerDigest::Factory> factory( | 549 std::unique_ptr<HttpAuthHandlerDigest::Factory> factory( |
546 new HttpAuthHandlerDigest::Factory()); | 550 new HttpAuthHandlerDigest::Factory()); |
547 std::unique_ptr<HttpAuthHandler> handler; | 551 std::unique_ptr<HttpAuthHandler> handler; |
548 std::string default_challenge = | 552 std::string default_challenge = |
549 "Digest realm=\"Oblivion\", nonce=\"nonce-value\""; | 553 "Digest realm=\"Oblivion\", nonce=\"nonce-value\""; |
550 GURL origin("intranet.google.com"); | 554 GURL origin("intranet.google.com"); |
551 SSLInfo null_ssl_info; | 555 SSLInfo null_ssl_info; |
552 int rv = factory->CreateAuthHandlerFromString( | 556 int rv = factory->CreateAuthHandlerFromString( |
553 default_challenge, HttpAuth::AUTH_SERVER, null_ssl_info, origin, | 557 default_challenge, HttpAuth::AUTH_SERVER, null_ssl_info, origin, |
554 BoundNetLog(), &handler); | 558 BoundNetLog(), &handler); |
555 EXPECT_EQ(OK, rv); | 559 EXPECT_THAT(rv, IsOk()); |
556 ASSERT_TRUE(handler.get() != NULL); | 560 ASSERT_TRUE(handler.get() != NULL); |
557 HttpAuthChallengeTokenizer tok_default(default_challenge.begin(), | 561 HttpAuthChallengeTokenizer tok_default(default_challenge.begin(), |
558 default_challenge.end()); | 562 default_challenge.end()); |
559 EXPECT_EQ(HttpAuth::AUTHORIZATION_RESULT_REJECT, | 563 EXPECT_EQ(HttpAuth::AUTHORIZATION_RESULT_REJECT, |
560 handler->HandleAnotherChallenge(&tok_default)); | 564 handler->HandleAnotherChallenge(&tok_default)); |
561 | 565 |
562 std::string stale_challenge = default_challenge + ", stale=true"; | 566 std::string stale_challenge = default_challenge + ", stale=true"; |
563 HttpAuthChallengeTokenizer tok_stale(stale_challenge.begin(), | 567 HttpAuthChallengeTokenizer tok_stale(stale_challenge.begin(), |
564 stale_challenge.end()); | 568 stale_challenge.end()); |
565 EXPECT_EQ(HttpAuth::AUTHORIZATION_RESULT_STALE, | 569 EXPECT_EQ(HttpAuth::AUTHORIZATION_RESULT_STALE, |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
690 EXPECT_EQ("Digest username=\"foo\", realm=\"Oblivion\", " | 694 EXPECT_EQ("Digest username=\"foo\", realm=\"Oblivion\", " |
691 "nonce=\"nonce-value\", uri=\"/path/to/resource\", " | 695 "nonce=\"nonce-value\", uri=\"/path/to/resource\", " |
692 "response=\"5b1459beda5cee30d6ff9e970a69c0ea\", " | 696 "response=\"5b1459beda5cee30d6ff9e970a69c0ea\", " |
693 "opaque=\"opaque text\", " | 697 "opaque=\"opaque text\", " |
694 "qop=auth, nc=00000001, cnonce=\"client_nonce\"", | 698 "qop=auth, nc=00000001, cnonce=\"client_nonce\"", |
695 auth_token); | 699 auth_token); |
696 } | 700 } |
697 | 701 |
698 | 702 |
699 } // namespace net | 703 } // namespace net |
OLD | NEW |