This source file includes following definitions.
- TEST
- TEST
- TEST
#include <string>
#include "base/basictypes.h"
#include "base/memory/scoped_ptr.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "net/base/net_errors.h"
#include "net/http/http_auth_challenge_tokenizer.h"
#include "net/http/http_auth_handler_basic.h"
#include "net/http/http_request_info.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace net {
TEST(HttpAuthHandlerBasicTest, GenerateAuthToken) {
static const struct {
const char* username;
const char* password;
const char* expected_credentials;
} tests[] = {
{ "foo", "bar", "Basic Zm9vOmJhcg==" },
{ "", "foobar", "Basic OmZvb2Jhcg==" },
{ "anon", "", "Basic YW5vbjo=" },
{ "", "", "Basic Og==" },
};
GURL origin("http://www.example.com");
HttpAuthHandlerBasic::Factory factory;
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
std::string challenge = "Basic realm=\"Atlantis\"";
scoped_ptr<HttpAuthHandler> basic;
EXPECT_EQ(OK, factory.CreateAuthHandlerFromString(
challenge, HttpAuth::AUTH_SERVER, origin, BoundNetLog(), &basic));
AuthCredentials credentials(base::ASCIIToUTF16(tests[i].username),
base::ASCIIToUTF16(tests[i].password));
HttpRequestInfo request_info;
std::string auth_token;
int rv = basic->GenerateAuthToken(&credentials, &request_info,
CompletionCallback(), &auth_token);
EXPECT_EQ(OK, rv);
EXPECT_STREQ(tests[i].expected_credentials, auth_token.c_str());
}
}
TEST(HttpAuthHandlerBasicTest, HandleAnotherChallenge) {
static const struct {
const char* challenge;
HttpAuth::AuthorizationResult expected_rv;
} tests[] = {
{
"Basic realm=\"First\"",
HttpAuth::AUTHORIZATION_RESULT_REJECT
},
{
"Basic realm=\"Second\"",
HttpAuth::AUTHORIZATION_RESULT_DIFFERENT_REALM
},
{
"Basic realm=\"Second\",realm=\"First\"",
HttpAuth::AUTHORIZATION_RESULT_REJECT
},
{
"basic realm=\"First\",realm=\"Second\"",
HttpAuth::AUTHORIZATION_RESULT_DIFFERENT_REALM
}
};
GURL origin("http://www.example.com");
HttpAuthHandlerBasic::Factory factory;
scoped_ptr<HttpAuthHandler> basic;
EXPECT_EQ(OK, factory.CreateAuthHandlerFromString(
tests[0].challenge, HttpAuth::AUTH_SERVER, origin,
BoundNetLog(), &basic));
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
std::string challenge(tests[i].challenge);
HttpAuthChallengeTokenizer tok(challenge.begin(),
challenge.end());
EXPECT_EQ(tests[i].expected_rv, basic->HandleAnotherChallenge(&tok));
}
}
TEST(HttpAuthHandlerBasicTest, InitFromChallenge) {
static const struct {
const char* challenge;
int expected_rv;
const char* expected_realm;
} tests[] = {
{
"Basic",
OK,
"",
},
{
"Basic realm=\"\"",
OK,
"",
},
{
"Basic realm=\"test_realm\"",
OK,
"test_realm",
},
{
"Basic realm=\"test_realm\",unknown_token=foobar",
OK,
"test_realm",
},
{
"Basic unknown_token=foobar,realm=\"test_realm\"",
OK,
"test_realm",
},
#if 0
{
"Basic realm=\"test_realm\" unknown_token=foobar",
OK,
"test_realm",
},
{
"Basic unknown_token=foobar realm=\"test_realm\"",
OK,
"test_realm",
},
#endif
{
"Negotiate",
ERR_INVALID_RESPONSE,
""
},
{
"Basic realm=\"foo\",realm=\"bar\"",
OK,
"bar",
},
{
"Basic realm=\"foo-\xE5\"",
OK,
"foo-\xC3\xA5",
},
};
HttpAuthHandlerBasic::Factory factory;
GURL origin("http://www.example.com");
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
std::string challenge = tests[i].challenge;
scoped_ptr<HttpAuthHandler> basic;
int rv = factory.CreateAuthHandlerFromString(
challenge, HttpAuth::AUTH_SERVER, origin, BoundNetLog(), &basic);
EXPECT_EQ(tests[i].expected_rv, rv);
if (rv == OK)
EXPECT_EQ(tests[i].expected_realm, basic->realm());
}
}
}