1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "testing/gtest/include/gtest/gtest.h" 6 7 #include "base/basictypes.h" 8 #include "net/http/http_auth_handler_basic.h" 9 10 namespace net { 11 12 TEST(HttpAuthHandlerBasicTest, GenerateCredentials) { 13 static const struct { 14 const wchar_t* username; 15 const wchar_t* password; 16 const char* expected_credentials; 17 } tests[] = { 18 { L"foo", L"bar", "Basic Zm9vOmJhcg==" }, 19 // Empty username 20 { L"", L"foobar", "Basic OmZvb2Jhcg==" }, 21 // Empty password 22 { L"anon", L"", "Basic YW5vbjo=" }, 23 // Empty username and empty password. 24 { L"", L"", "Basic Og==" }, 25 }; 26 GURL origin("http://www.example.com"); 27 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { 28 std::string challenge = "Basic realm=\"Atlantis\""; 29 scoped_refptr<HttpAuthHandlerBasic> basic = new HttpAuthHandlerBasic; 30 bool ok = basic->InitFromChallenge(challenge.begin(), challenge.end(), 31 HttpAuth::AUTH_SERVER, origin); 32 EXPECT_TRUE(ok); 33 std::string credentials = basic->GenerateCredentials(tests[i].username, 34 tests[i].password, 35 NULL, NULL); 36 EXPECT_STREQ(tests[i].expected_credentials, credentials.c_str()); 37 } 38 } 39 40 TEST(HttpAuthHandlerBasicTest, InitFromChallenge) { 41 static const struct { 42 const char* challenge; 43 bool expected_success; 44 const char* expected_realm; 45 } tests[] = { 46 // No realm (we allow this even though realm is supposed to be required 47 // according to RFC 2617.) 48 { 49 "Basic", 50 true, 51 "", 52 }, 53 54 // Realm is empty string. 55 { 56 "Basic realm=\"\"", 57 true, 58 "", 59 }, 60 }; 61 GURL origin("http://www.example.com"); 62 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { 63 std::string challenge = tests[i].challenge; 64 scoped_refptr<HttpAuthHandlerBasic> basic = new HttpAuthHandlerBasic; 65 bool ok = basic->InitFromChallenge(challenge.begin(), challenge.end(), 66 HttpAuth::AUTH_SERVER, origin); 67 EXPECT_EQ(tests[i].expected_success, ok); 68 if (ok) 69 EXPECT_EQ(tests[i].expected_realm, basic->realm()); 70 } 71 } 72 73 } // namespace net 74