This source file includes following definitions.
- username_digest_
- SetAuthFor
- auth_cancelled_count_
- AddHandler
- RemoveHandler
- Register
- remaining_notification_count_
- ExpectedTitleFromAuth
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
#include <algorithm>
#include <list>
#include <map>
#include "base/metrics/field_trial.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/prerender/prerender_manager.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/login/login_prompt.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_source.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test_utils.h"
#include "net/base/auth.h"
#include "net/dns/mock_host_resolver.h"
using content::NavigationController;
using content::OpenURLParams;
using content::Referrer;
namespace {
class LoginPromptBrowserTest : public InProcessBrowserTest {
public:
LoginPromptBrowserTest()
: bad_password_("incorrect"),
bad_username_("nouser"),
password_("secret"),
username_basic_("basicuser"),
username_digest_("digestuser") {
auth_map_["foo"] = AuthInfo("testuser", "foopassword");
auth_map_["bar"] = AuthInfo("testuser", "barpassword");
auth_map_["testrealm"] = AuthInfo(username_basic_, password_);
}
protected:
struct AuthInfo {
std::string username_;
std::string password_;
AuthInfo() {}
AuthInfo(const std::string& username,
const std::string& password)
: username_(username), password_(password) {}
};
typedef std::map<std::string, AuthInfo> AuthMap;
void SetAuthFor(LoginHandler* handler);
AuthMap auth_map_;
std::string bad_password_;
std::string bad_username_;
std::string password_;
std::string username_basic_;
std::string username_digest_;
};
void LoginPromptBrowserTest::SetAuthFor(LoginHandler* handler) {
const net::AuthChallengeInfo* challenge = handler->auth_info();
ASSERT_TRUE(challenge);
AuthMap::iterator i = auth_map_.find(challenge->realm);
EXPECT_TRUE(auth_map_.end() != i);
if (i != auth_map_.end()) {
const AuthInfo& info = i->second;
handler->SetAuth(base::UTF8ToUTF16(info.username_),
base::UTF8ToUTF16(info.password_));
}
}
class LoginPromptBrowserTestObserver : public content::NotificationObserver {
public:
LoginPromptBrowserTestObserver()
: auth_needed_count_(0),
auth_supplied_count_(0),
auth_cancelled_count_(0) {}
virtual void Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) OVERRIDE;
void AddHandler(LoginHandler* handler);
void RemoveHandler(LoginHandler* handler);
void Register(const content::NotificationSource& source);
std::list<LoginHandler*> handlers_;
int auth_needed_count_;
int auth_supplied_count_;
int auth_cancelled_count_;
private:
content::NotificationRegistrar registrar_;
DISALLOW_COPY_AND_ASSIGN(LoginPromptBrowserTestObserver);
};
void LoginPromptBrowserTestObserver::Observe(
int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
if (type == chrome::NOTIFICATION_AUTH_NEEDED) {
LoginNotificationDetails* login_details =
content::Details<LoginNotificationDetails>(details).ptr();
AddHandler(login_details->handler());
auth_needed_count_++;
} else if (type == chrome::NOTIFICATION_AUTH_SUPPLIED) {
AuthSuppliedLoginNotificationDetails* login_details =
content::Details<AuthSuppliedLoginNotificationDetails>(details).ptr();
RemoveHandler(login_details->handler());
auth_supplied_count_++;
} else if (type == chrome::NOTIFICATION_AUTH_CANCELLED) {
LoginNotificationDetails* login_details =
content::Details<LoginNotificationDetails>(details).ptr();
RemoveHandler(login_details->handler());
auth_cancelled_count_++;
}
}
void LoginPromptBrowserTestObserver::AddHandler(LoginHandler* handler) {
std::list<LoginHandler*>::iterator i = std::find(handlers_.begin(),
handlers_.end(),
handler);
EXPECT_TRUE(i == handlers_.end());
if (i == handlers_.end())
handlers_.push_back(handler);
}
void LoginPromptBrowserTestObserver::RemoveHandler(LoginHandler* handler) {
std::list<LoginHandler*>::iterator i = std::find(handlers_.begin(),
handlers_.end(),
handler);
EXPECT_TRUE(i != handlers_.end());
if (i != handlers_.end())
handlers_.erase(i);
}
void LoginPromptBrowserTestObserver::Register(
const content::NotificationSource& source) {
registrar_.Add(this, chrome::NOTIFICATION_AUTH_NEEDED, source);
registrar_.Add(this, chrome::NOTIFICATION_AUTH_SUPPLIED, source);
registrar_.Add(this, chrome::NOTIFICATION_AUTH_CANCELLED, source);
}
template <int T>
class WindowedNavigationObserver
: public content::WindowedNotificationObserver {
public:
explicit WindowedNavigationObserver(NavigationController* controller)
: content::WindowedNotificationObserver(
T, content::Source<NavigationController>(controller)) {}
};
class WindowedLoadStopObserver
: public WindowedNavigationObserver<content::NOTIFICATION_LOAD_STOP> {
public:
WindowedLoadStopObserver(NavigationController* controller,
int notification_count)
: WindowedNavigationObserver<content::NOTIFICATION_LOAD_STOP>(controller),
remaining_notification_count_(notification_count) {}
protected:
virtual void Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) OVERRIDE;
private:
int remaining_notification_count_;
};
void WindowedLoadStopObserver::Observe(
int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
if (--remaining_notification_count_ == 0)
WindowedNotificationObserver::Observe(type, source, details);
}
typedef WindowedNavigationObserver<chrome::NOTIFICATION_AUTH_NEEDED>
WindowedAuthNeededObserver;
typedef WindowedNavigationObserver<chrome::NOTIFICATION_AUTH_CANCELLED>
WindowedAuthCancelledObserver;
typedef WindowedNavigationObserver<chrome::NOTIFICATION_AUTH_SUPPLIED>
WindowedAuthSuppliedObserver;
const char kPrefetchAuthPage[] = "files/login/prefetch.html";
const char kMultiRealmTestPage[] = "files/login/multi_realm.html";
const int kMultiRealmTestRealmCount = 2;
const char kSingleRealmTestPage[] = "files/login/single_realm.html";
const char* kAuthBasicPage = "auth-basic";
const char* kAuthDigestPage = "auth-digest";
base::string16 ExpectedTitleFromAuth(const base::string16& username,
const base::string16& password) {
return username + base::UTF8ToUTF16("/") + password;
}
IN_PROC_BROWSER_TEST_F(LoginPromptBrowserTest, PrefetchAuthCancels) {
ASSERT_TRUE(test_server()->Start());
GURL test_page = test_server()->GetURL(kPrefetchAuthPage);
class SetPrefetchForTest {
public:
explicit SetPrefetchForTest(bool prefetch)
: old_prerender_mode_(prerender::PrerenderManager::GetMode()) {
std::string exp_group = prefetch ? "ExperimentYes" : "ExperimentNo";
base::FieldTrialList::CreateFieldTrial("Prefetch", exp_group);
prerender::PrerenderManager::SetMode(
prerender::PrerenderManager::PRERENDER_MODE_DISABLED);
}
~SetPrefetchForTest() {
prerender::PrerenderManager::SetMode(old_prerender_mode_);
}
private:
prerender::PrerenderManager::PrerenderManagerMode old_prerender_mode_;
} set_prefetch_for_test(true);
content::WebContents* contents =
browser()->tab_strip_model()->GetActiveWebContents();
NavigationController* controller = &contents->GetController();
LoginPromptBrowserTestObserver observer;
observer.Register(content::Source<NavigationController>(controller));
WindowedLoadStopObserver load_stop_waiter(controller, 1);
browser()->OpenURL(OpenURLParams(
test_page, Referrer(), CURRENT_TAB, content::PAGE_TRANSITION_TYPED,
false));
load_stop_waiter.Wait();
EXPECT_TRUE(observer.handlers_.empty());
EXPECT_TRUE(test_server()->Stop());
}
IN_PROC_BROWSER_TEST_F(LoginPromptBrowserTest, TestBasicAuth) {
ASSERT_TRUE(test_server()->Start());
GURL test_page = test_server()->GetURL(kAuthBasicPage);
content::WebContents* contents =
browser()->tab_strip_model()->GetActiveWebContents();
NavigationController* controller = &contents->GetController();
LoginPromptBrowserTestObserver observer;
observer.Register(content::Source<NavigationController>(controller));
{
WindowedAuthNeededObserver auth_needed_waiter(controller);
browser()->OpenURL(OpenURLParams(
test_page, Referrer(), CURRENT_TAB, content::PAGE_TRANSITION_TYPED,
false));
auth_needed_waiter.Wait();
}
ASSERT_FALSE(observer.handlers_.empty());
{
WindowedAuthNeededObserver auth_needed_waiter(controller);
WindowedAuthSuppliedObserver auth_supplied_waiter(controller);
LoginHandler* handler = *observer.handlers_.begin();
ASSERT_TRUE(handler);
handler->SetAuth(base::UTF8ToUTF16(bad_username_),
base::UTF8ToUTF16(bad_password_));
auth_supplied_waiter.Wait();
auth_needed_waiter.Wait();
}
ASSERT_EQ(1u, observer.handlers_.size());
WindowedAuthSuppliedObserver auth_supplied_waiter(controller);
LoginHandler* handler = *observer.handlers_.begin();
SetAuthFor(handler);
auth_supplied_waiter.Wait();
base::string16 expected_title =
ExpectedTitleFromAuth(base::ASCIIToUTF16("basicuser"),
base::ASCIIToUTF16("secret"));
content::TitleWatcher title_watcher(contents, expected_title);
EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle());
}
IN_PROC_BROWSER_TEST_F(LoginPromptBrowserTest, TestDigestAuth) {
ASSERT_TRUE(test_server()->Start());
GURL test_page = test_server()->GetURL(kAuthDigestPage);
content::WebContents* contents =
browser()->tab_strip_model()->GetActiveWebContents();
NavigationController* controller = &contents->GetController();
LoginPromptBrowserTestObserver observer;
observer.Register(content::Source<NavigationController>(controller));
{
WindowedAuthNeededObserver auth_needed_waiter(controller);
browser()->OpenURL(OpenURLParams(
test_page, Referrer(), CURRENT_TAB, content::PAGE_TRANSITION_TYPED,
false));
auth_needed_waiter.Wait();
}
ASSERT_FALSE(observer.handlers_.empty());
{
WindowedAuthNeededObserver auth_needed_waiter(controller);
WindowedAuthSuppliedObserver auth_supplied_waiter(controller);
LoginHandler* handler = *observer.handlers_.begin();
ASSERT_TRUE(handler);
handler->SetAuth(base::UTF8ToUTF16(bad_username_),
base::UTF8ToUTF16(bad_password_));
auth_supplied_waiter.Wait();
auth_needed_waiter.Wait();
}
ASSERT_EQ(1u, observer.handlers_.size());
WindowedAuthSuppliedObserver auth_supplied_waiter(controller);
LoginHandler* handler = *observer.handlers_.begin();
base::string16 username(base::UTF8ToUTF16(username_digest_));
base::string16 password(base::UTF8ToUTF16(password_));
handler->SetAuth(username, password);
auth_supplied_waiter.Wait();
base::string16 expected_title = ExpectedTitleFromAuth(username, password);
content::TitleWatcher title_watcher(contents, expected_title);
EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle());
}
IN_PROC_BROWSER_TEST_F(LoginPromptBrowserTest, TestTwoAuths) {
ASSERT_TRUE(test_server()->Start());
content::WebContents* contents1 =
browser()->tab_strip_model()->GetActiveWebContents();
NavigationController* controller1 = &contents1->GetController();
LoginPromptBrowserTestObserver observer;
observer.Register(content::Source<NavigationController>(controller1));
ui_test_utils::NavigateToURLWithDisposition(
browser(),
GURL("about:blank"),
NEW_FOREGROUND_TAB,
ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB);
content::WebContents* contents2 =
browser()->tab_strip_model()->GetActiveWebContents();
ASSERT_NE(contents1, contents2);
NavigationController* controller2 = &contents2->GetController();
observer.Register(content::Source<NavigationController>(controller2));
{
WindowedAuthNeededObserver auth_needed_waiter(controller1);
contents1->OpenURL(OpenURLParams(
test_server()->GetURL(kAuthBasicPage), Referrer(),
CURRENT_TAB, content::PAGE_TRANSITION_TYPED, false));
auth_needed_waiter.Wait();
}
{
WindowedAuthNeededObserver auth_needed_waiter(controller2);
contents2->OpenURL(OpenURLParams(
test_server()->GetURL(kAuthDigestPage), Referrer(),
CURRENT_TAB, content::PAGE_TRANSITION_TYPED, false));
auth_needed_waiter.Wait();
}
ASSERT_EQ(2u, observer.handlers_.size());
LoginHandler* handler1 = *observer.handlers_.begin();
LoginHandler* handler2 = *(++(observer.handlers_.begin()));
base::string16 expected_title1 = ExpectedTitleFromAuth(
base::UTF8ToUTF16(username_basic_), base::UTF8ToUTF16(password_));
base::string16 expected_title2 = ExpectedTitleFromAuth(
base::UTF8ToUTF16(username_digest_), base::UTF8ToUTF16(password_));
content::TitleWatcher title_watcher1(contents1, expected_title1);
content::TitleWatcher title_watcher2(contents2, expected_title2);
handler1->SetAuth(base::UTF8ToUTF16(username_basic_),
base::UTF8ToUTF16(password_));
handler2->SetAuth(base::UTF8ToUTF16(username_digest_),
base::UTF8ToUTF16(password_));
EXPECT_EQ(expected_title1, title_watcher1.WaitAndGetTitle());
EXPECT_EQ(expected_title2, title_watcher2.WaitAndGetTitle());
}
IN_PROC_BROWSER_TEST_F(LoginPromptBrowserTest, TestCancelAuth) {
ASSERT_TRUE(test_server()->Start());
GURL auth_page = test_server()->GetURL(kAuthBasicPage);
GURL no_auth_page_1 = test_server()->GetURL("a");
GURL no_auth_page_2 = test_server()->GetURL("b");
GURL no_auth_page_3 = test_server()->GetURL("c");
content::WebContents* contents =
browser()->tab_strip_model()->GetActiveWebContents();
NavigationController* controller = &contents->GetController();
LoginPromptBrowserTestObserver observer;
observer.Register(content::Source<NavigationController>(controller));
ui_test_utils::NavigateToURL(browser(), no_auth_page_1);
{
WindowedLoadStopObserver load_stop_waiter(controller, 2);
WindowedAuthNeededObserver auth_needed_waiter(controller);
browser()->OpenURL(OpenURLParams(
auth_page, Referrer(), CURRENT_TAB, content::PAGE_TRANSITION_TYPED,
false));
auth_needed_waiter.Wait();
WindowedAuthCancelledObserver auth_cancelled_waiter(controller);
browser()->OpenURL(OpenURLParams(
no_auth_page_2, Referrer(), CURRENT_TAB, content::PAGE_TRANSITION_TYPED,
false));
auth_cancelled_waiter.Wait();
load_stop_waiter.Wait();
EXPECT_TRUE(observer.handlers_.empty());
}
{
WindowedLoadStopObserver load_stop_waiter(controller, 2);
WindowedAuthNeededObserver auth_needed_waiter(controller);
browser()->OpenURL(OpenURLParams(
auth_page, Referrer(), CURRENT_TAB, content::PAGE_TRANSITION_TYPED,
false));
auth_needed_waiter.Wait();
WindowedAuthCancelledObserver auth_cancelled_waiter(controller);
ASSERT_TRUE(chrome::CanGoBack(browser()));
chrome::GoBack(browser(), CURRENT_TAB);
auth_cancelled_waiter.Wait();
load_stop_waiter.Wait();
EXPECT_TRUE(observer.handlers_.empty());
}
ui_test_utils::NavigateToURL(browser(), no_auth_page_3);
{
WindowedLoadStopObserver load_stop_waiter(controller, 1);
chrome::GoBack(browser(), CURRENT_TAB);
load_stop_waiter.Wait();
}
{
WindowedLoadStopObserver load_stop_waiter(controller, 2);
WindowedAuthNeededObserver auth_needed_waiter(controller);
browser()->OpenURL(OpenURLParams(
auth_page, Referrer(), CURRENT_TAB, content::PAGE_TRANSITION_TYPED,
false));
auth_needed_waiter.Wait();
WindowedAuthCancelledObserver auth_cancelled_waiter(controller);
ASSERT_TRUE(chrome::CanGoForward(browser()));
chrome::GoForward(browser(), CURRENT_TAB);
auth_cancelled_waiter.Wait();
load_stop_waiter.Wait();
EXPECT_TRUE(observer.handlers_.empty());
}
{
WindowedLoadStopObserver load_stop_waiter(controller, 1);
WindowedAuthNeededObserver auth_needed_waiter(controller);
browser()->OpenURL(OpenURLParams(
auth_page, Referrer(), CURRENT_TAB, content::PAGE_TRANSITION_TYPED,
false));
auth_needed_waiter.Wait();
WindowedAuthCancelledObserver auth_cancelled_waiter(controller);
LoginHandler* handler = *observer.handlers_.begin();
ASSERT_TRUE(handler);
handler->CancelAuth();
auth_cancelled_waiter.Wait();
load_stop_waiter.Wait();
EXPECT_TRUE(observer.handlers_.empty());
}
}
IN_PROC_BROWSER_TEST_F(LoginPromptBrowserTest, MultipleRealmCancellation) {
ASSERT_TRUE(test_server()->Start());
GURL test_page = test_server()->GetURL(kMultiRealmTestPage);
content::WebContents* contents =
browser()->tab_strip_model()->GetActiveWebContents();
NavigationController* controller = &contents->GetController();
LoginPromptBrowserTestObserver observer;
observer.Register(content::Source<NavigationController>(controller));
WindowedLoadStopObserver load_stop_waiter(controller, 1);
{
WindowedAuthNeededObserver auth_needed_waiter(controller);
browser()->OpenURL(OpenURLParams(
test_page, Referrer(), CURRENT_TAB, content::PAGE_TRANSITION_TYPED,
false));
auth_needed_waiter.Wait();
}
int n_handlers = 0;
while (n_handlers < kMultiRealmTestRealmCount) {
WindowedAuthNeededObserver auth_needed_waiter(controller);
while (!observer.handlers_.empty()) {
WindowedAuthCancelledObserver auth_cancelled_waiter(controller);
LoginHandler* handler = *observer.handlers_.begin();
ASSERT_TRUE(handler);
n_handlers++;
handler->CancelAuth();
auth_cancelled_waiter.Wait();
}
if (n_handlers < kMultiRealmTestRealmCount)
auth_needed_waiter.Wait();
}
load_stop_waiter.Wait();
EXPECT_EQ(kMultiRealmTestRealmCount, n_handlers);
EXPECT_EQ(0, observer.auth_supplied_count_);
EXPECT_LT(0, observer.auth_needed_count_);
EXPECT_LT(0, observer.auth_cancelled_count_);
EXPECT_TRUE(test_server()->Stop());
}
IN_PROC_BROWSER_TEST_F(LoginPromptBrowserTest, MultipleRealmConfirmation) {
ASSERT_TRUE(test_server()->Start());
GURL test_page = test_server()->GetURL(kMultiRealmTestPage);
content::WebContents* contents =
browser()->tab_strip_model()->GetActiveWebContents();
NavigationController* controller = &contents->GetController();
LoginPromptBrowserTestObserver observer;
observer.Register(content::Source<NavigationController>(controller));
WindowedLoadStopObserver load_stop_waiter(controller, 1);
int n_handlers = 0;
{
WindowedAuthNeededObserver auth_needed_waiter(controller);
browser()->OpenURL(OpenURLParams(
test_page, Referrer(), CURRENT_TAB, content::PAGE_TRANSITION_TYPED,
false));
auth_needed_waiter.Wait();
}
while (n_handlers < kMultiRealmTestRealmCount) {
WindowedAuthNeededObserver auth_needed_waiter(controller);
while (!observer.handlers_.empty()) {
WindowedAuthSuppliedObserver auth_supplied_waiter(controller);
LoginHandler* handler = *observer.handlers_.begin();
ASSERT_TRUE(handler);
n_handlers++;
SetAuthFor(handler);
auth_supplied_waiter.Wait();
}
if (n_handlers < kMultiRealmTestRealmCount)
auth_needed_waiter.Wait();
}
load_stop_waiter.Wait();
EXPECT_EQ(kMultiRealmTestRealmCount, n_handlers);
EXPECT_LT(0, observer.auth_needed_count_);
EXPECT_LT(0, observer.auth_supplied_count_);
EXPECT_EQ(0, observer.auth_cancelled_count_);
EXPECT_TRUE(test_server()->Stop());
}
IN_PROC_BROWSER_TEST_F(LoginPromptBrowserTest, IncorrectConfirmation) {
ASSERT_TRUE(test_server()->Start());
GURL test_page = test_server()->GetURL(kSingleRealmTestPage);
content::WebContents* contents =
browser()->tab_strip_model()->GetActiveWebContents();
NavigationController* controller = &contents->GetController();
LoginPromptBrowserTestObserver observer;
observer.Register(content::Source<NavigationController>(controller));
{
WindowedAuthNeededObserver auth_needed_waiter(controller);
browser()->OpenURL(OpenURLParams(
test_page, Referrer(), CURRENT_TAB, content::PAGE_TRANSITION_TYPED,
false));
auth_needed_waiter.Wait();
}
EXPECT_FALSE(observer.handlers_.empty());
if (!observer.handlers_.empty()) {
WindowedAuthNeededObserver auth_needed_waiter(controller);
WindowedAuthSuppliedObserver auth_supplied_waiter(controller);
LoginHandler* handler = *observer.handlers_.begin();
ASSERT_TRUE(handler);
handler->SetAuth(base::UTF8ToUTF16(bad_username_),
base::UTF8ToUTF16(bad_password_));
auth_supplied_waiter.Wait();
auth_needed_waiter.Wait();
}
int n_handlers = 0;
while (n_handlers < 1) {
WindowedAuthNeededObserver auth_needed_waiter(controller);
while (!observer.handlers_.empty()) {
WindowedAuthSuppliedObserver auth_supplied_waiter(controller);
LoginHandler* handler = *observer.handlers_.begin();
ASSERT_TRUE(handler);
n_handlers++;
SetAuthFor(handler);
auth_supplied_waiter.Wait();
}
if (n_handlers < 1)
auth_needed_waiter.Wait();
}
EXPECT_EQ(1, n_handlers);
EXPECT_LT(0, observer.auth_needed_count_);
EXPECT_EQ(0, observer.auth_cancelled_count_);
EXPECT_EQ(observer.auth_needed_count_, observer.auth_supplied_count_);
EXPECT_TRUE(test_server()->Stop());
}
IN_PROC_BROWSER_TEST_F(LoginPromptBrowserTest, NoLoginPromptForFavicon) {
const char* kFaviconTestPage = "files/login/has_favicon.html";
const char* kFaviconResource = "auth-basic/favicon.gif";
ASSERT_TRUE(test_server()->Start());
content::WebContents* contents =
browser()->tab_strip_model()->GetActiveWebContents();
NavigationController* controller = &contents->GetController();
LoginPromptBrowserTestObserver observer;
observer.Register(content::Source<NavigationController>(controller));
{
GURL test_page = test_server()->GetURL(kFaviconTestPage);
WindowedLoadStopObserver load_stop_waiter(controller, 1);
browser()->OpenURL(OpenURLParams(
test_page, Referrer(), CURRENT_TAB, content::PAGE_TRANSITION_TYPED,
false));
load_stop_waiter.Wait();
}
{
GURL test_page = test_server()->GetURL(kFaviconResource);
WindowedLoadStopObserver load_stop_waiter(controller, 1);
WindowedAuthNeededObserver auth_needed_waiter(controller);
browser()->OpenURL(OpenURLParams(
test_page, Referrer(), CURRENT_TAB, content::PAGE_TRANSITION_TYPED,
false));
auth_needed_waiter.Wait();
ASSERT_EQ(1u, observer.handlers_.size());
while (!observer.handlers_.empty()) {
WindowedAuthCancelledObserver auth_cancelled_waiter(controller);
LoginHandler* handler = *observer.handlers_.begin();
ASSERT_TRUE(handler);
handler->CancelAuth();
auth_cancelled_waiter.Wait();
}
load_stop_waiter.Wait();
}
EXPECT_EQ(0, observer.auth_supplied_count_);
EXPECT_EQ(1, observer.auth_needed_count_);
EXPECT_EQ(1, observer.auth_cancelled_count_);
EXPECT_TRUE(test_server()->Stop());
}
IN_PROC_BROWSER_TEST_F(LoginPromptBrowserTest,
BlockCrossdomainPrompt) {
const char* kTestPage = "files/login/load_img_from_b.html";
host_resolver()->AddRule("www.a.com", "127.0.0.1");
host_resolver()->AddRule("www.b.com", "127.0.0.1");
ASSERT_TRUE(test_server()->Start());
content::WebContents* contents =
browser()->tab_strip_model()->GetActiveWebContents();
NavigationController* controller = &contents->GetController();
LoginPromptBrowserTestObserver observer;
observer.Register(content::Source<NavigationController>(controller));
{
GURL test_page = test_server()->GetURL(kTestPage);
ASSERT_EQ("127.0.0.1", test_page.host());
std::string new_host("www.a.com");
GURL::Replacements replacements;
replacements.SetHostStr(new_host);
test_page = test_page.ReplaceComponents(replacements);
WindowedLoadStopObserver load_stop_waiter(controller, 1);
browser()->OpenURL(OpenURLParams(
test_page, Referrer(), CURRENT_TAB, content::PAGE_TRANSITION_TYPED,
false));
load_stop_waiter.Wait();
}
EXPECT_EQ(0, observer.auth_needed_count_);
{
GURL test_page = test_server()->GetURL(kTestPage);
ASSERT_EQ("127.0.0.1", test_page.host());
std::string new_host("www.b.com");
GURL::Replacements replacements;
replacements.SetHostStr(new_host);
test_page = test_page.ReplaceComponents(replacements);
WindowedAuthNeededObserver auth_needed_waiter(controller);
browser()->OpenURL(OpenURLParams(
test_page, Referrer(), CURRENT_TAB, content::PAGE_TRANSITION_TYPED,
false));
auth_needed_waiter.Wait();
ASSERT_EQ(1u, observer.handlers_.size());
while (!observer.handlers_.empty()) {
WindowedAuthCancelledObserver auth_cancelled_waiter(controller);
LoginHandler* handler = *observer.handlers_.begin();
ASSERT_TRUE(handler);
handler->CancelAuth();
auth_cancelled_waiter.Wait();
}
}
EXPECT_EQ(1, observer.auth_needed_count_);
EXPECT_TRUE(test_server()->Stop());
}
IN_PROC_BROWSER_TEST_F(LoginPromptBrowserTest,
AllowCrossdomainPrompt) {
const char* kTestPage = "files/login/load_iframe_from_b.html";
host_resolver()->AddRule("www.a.com", "127.0.0.1");
host_resolver()->AddRule("www.b.com", "127.0.0.1");
ASSERT_TRUE(test_server()->Start());
content::WebContents* contents =
browser()->tab_strip_model()->GetActiveWebContents();
NavigationController* controller = &contents->GetController();
LoginPromptBrowserTestObserver observer;
observer.Register(content::Source<NavigationController>(controller));
{
GURL test_page = test_server()->GetURL(kTestPage);
ASSERT_EQ("127.0.0.1", test_page.host());
std::string new_host("www.a.com");
GURL::Replacements replacements;
replacements.SetHostStr(new_host);
test_page = test_page.ReplaceComponents(replacements);
WindowedAuthNeededObserver auth_needed_waiter(controller);
browser()->OpenURL(OpenURLParams(
test_page, Referrer(), CURRENT_TAB, content::PAGE_TRANSITION_TYPED,
false));
auth_needed_waiter.Wait();
ASSERT_EQ(1u, observer.handlers_.size());
while (!observer.handlers_.empty()) {
WindowedAuthCancelledObserver auth_cancelled_waiter(controller);
LoginHandler* handler = *observer.handlers_.begin();
ASSERT_TRUE(handler);
handler->CancelAuth();
auth_cancelled_waiter.Wait();
}
}
EXPECT_EQ(1, observer.auth_needed_count_);
EXPECT_TRUE(test_server()->Stop());
}
IN_PROC_BROWSER_TEST_F(LoginPromptBrowserTest, SupplyRedundantAuths) {
ASSERT_TRUE(test_server()->Start());
content::WebContents* contents_1 =
browser()->tab_strip_model()->GetActiveWebContents();
NavigationController* controller_1 = &contents_1->GetController();
ui_test_utils::NavigateToURLWithDisposition(
browser(),
GURL("about:blank"),
NEW_FOREGROUND_TAB,
ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB);
content::WebContents* contents_2 =
browser()->tab_strip_model()->GetActiveWebContents();
ASSERT_NE(contents_1, contents_2);
NavigationController* controller_2 = &contents_2->GetController();
LoginPromptBrowserTestObserver observer;
observer.Register(content::Source<NavigationController>(controller_1));
observer.Register(content::Source<NavigationController>(controller_2));
{
WindowedAuthNeededObserver auth_needed_waiter_1(controller_1);
WindowedAuthNeededObserver auth_needed_waiter_2(controller_2);
contents_1->OpenURL(OpenURLParams(
test_server()->GetURL("auth-basic/1"),
content::Referrer(),
CURRENT_TAB,
content::PAGE_TRANSITION_TYPED,
false));
contents_2->OpenURL(OpenURLParams(
test_server()->GetURL("auth-basic/2"),
content::Referrer(),
CURRENT_TAB,
content::PAGE_TRANSITION_TYPED,
false));
auth_needed_waiter_1.Wait();
auth_needed_waiter_2.Wait();
ASSERT_EQ(2U, observer.handlers_.size());
WindowedAuthSuppliedObserver auth_supplied_waiter_1(controller_1);
WindowedAuthSuppliedObserver auth_supplied_waiter_2(controller_2);
LoginHandler* handler_1 = *observer.handlers_.begin();
ASSERT_TRUE(handler_1);
SetAuthFor(handler_1);
auth_supplied_waiter_1.Wait();
auth_supplied_waiter_2.Wait();
}
EXPECT_EQ(2, observer.auth_needed_count_);
EXPECT_EQ(2, observer.auth_supplied_count_);
EXPECT_EQ(0, observer.auth_cancelled_count_);
EXPECT_TRUE(test_server()->Stop());
}
IN_PROC_BROWSER_TEST_F(LoginPromptBrowserTest, CancelRedundantAuths) {
ASSERT_TRUE(test_server()->Start());
content::WebContents* contents_1 =
browser()->tab_strip_model()->GetActiveWebContents();
NavigationController* controller_1 = &contents_1->GetController();
ui_test_utils::NavigateToURLWithDisposition(
browser(),
GURL("about:blank"),
NEW_FOREGROUND_TAB,
ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB);
content::WebContents* contents_2 =
browser()->tab_strip_model()->GetActiveWebContents();
ASSERT_NE(contents_1, contents_2);
NavigationController* controller_2 = &contents_2->GetController();
LoginPromptBrowserTestObserver observer;
observer.Register(content::Source<NavigationController>(controller_1));
observer.Register(content::Source<NavigationController>(controller_2));
{
WindowedAuthNeededObserver auth_needed_waiter_1(controller_1);
WindowedAuthNeededObserver auth_needed_waiter_2(controller_2);
contents_1->OpenURL(OpenURLParams(
test_server()->GetURL("auth-basic/1"),
content::Referrer(),
CURRENT_TAB,
content::PAGE_TRANSITION_TYPED,
false));
contents_2->OpenURL(OpenURLParams(
test_server()->GetURL("auth-basic/2"),
content::Referrer(),
CURRENT_TAB,
content::PAGE_TRANSITION_TYPED,
false));
auth_needed_waiter_1.Wait();
auth_needed_waiter_2.Wait();
ASSERT_EQ(2U, observer.handlers_.size());
WindowedAuthCancelledObserver auth_cancelled_waiter_1(controller_1);
WindowedAuthCancelledObserver auth_cancelled_waiter_2(controller_2);
LoginHandler* handler_1 = *observer.handlers_.begin();
ASSERT_TRUE(handler_1);
handler_1->CancelAuth();
auth_cancelled_waiter_1.Wait();
auth_cancelled_waiter_2.Wait();
}
EXPECT_EQ(2, observer.auth_needed_count_);
EXPECT_EQ(0, observer.auth_supplied_count_);
EXPECT_EQ(2, observer.auth_cancelled_count_);
EXPECT_TRUE(test_server()->Stop());
}
IN_PROC_BROWSER_TEST_F(LoginPromptBrowserTest,
SupplyRedundantAuthsMultiProfile) {
ASSERT_TRUE(test_server()->Start());
content::WebContents* contents =
browser()->tab_strip_model()->GetActiveWebContents();
NavigationController* controller = &contents->GetController();
Browser* browser_incognito = CreateIncognitoBrowser();
content::WebContents* contents_incognito =
browser_incognito->tab_strip_model()->GetActiveWebContents();
ASSERT_NE(contents, contents_incognito);
NavigationController* controller_incognito =
&contents_incognito->GetController();
LoginPromptBrowserTestObserver observer;
observer.Register(content::Source<NavigationController>(controller));
LoginPromptBrowserTestObserver observer_incognito;
observer_incognito.Register(
content::Source<NavigationController>(controller_incognito));
{
WindowedAuthNeededObserver auth_needed_waiter(controller);
WindowedAuthNeededObserver auth_needed_waiter_incognito(
controller_incognito);
contents->OpenURL(OpenURLParams(
test_server()->GetURL("auth-basic/1"),
content::Referrer(),
CURRENT_TAB,
content::PAGE_TRANSITION_TYPED,
false));
contents_incognito->OpenURL(OpenURLParams(
test_server()->GetURL("auth-basic/2"),
content::Referrer(),
CURRENT_TAB,
content::PAGE_TRANSITION_TYPED,
false));
auth_needed_waiter.Wait();
auth_needed_waiter_incognito.Wait();
ASSERT_EQ(1U, observer.handlers_.size());
ASSERT_EQ(1U, observer_incognito.handlers_.size());
WindowedAuthSuppliedObserver auth_supplied_waiter(controller);
LoginHandler* handler = *observer.handlers_.begin();
ASSERT_TRUE(handler);
SetAuthFor(handler);
auth_supplied_waiter.Wait();
content::RunAllPendingInMessageLoop();
}
EXPECT_EQ(1, observer.auth_needed_count_);
EXPECT_EQ(1, observer.auth_supplied_count_);
EXPECT_EQ(0, observer.auth_cancelled_count_);
EXPECT_EQ(1, observer_incognito.auth_needed_count_);
EXPECT_EQ(0, observer_incognito.auth_supplied_count_);
EXPECT_EQ(0, observer_incognito.auth_cancelled_count_);
EXPECT_TRUE(test_server()->Stop());
}
IN_PROC_BROWSER_TEST_F(LoginPromptBrowserTest,
NoLoginPromptForXHRWithBadCredentials) {
const char* kXHRTestPage = "files/login/xhr_with_credentials.html#incorrect";
ASSERT_TRUE(test_server()->Start());
content::WebContents* contents =
browser()->tab_strip_model()->GetActiveWebContents();
NavigationController* controller = &contents->GetController();
LoginPromptBrowserTestObserver observer;
observer.Register(content::Source<NavigationController>(controller));
{
GURL test_page = test_server()->GetURL(kXHRTestPage);
WindowedLoadStopObserver load_stop_waiter(controller, 1);
browser()->OpenURL(OpenURLParams(
test_page, Referrer(), CURRENT_TAB, content::PAGE_TRANSITION_TYPED,
false));
load_stop_waiter.Wait();
}
base::string16 expected_title(base::UTF8ToUTF16("status=401"));
EXPECT_EQ(expected_title, contents->GetTitle());
EXPECT_EQ(0, observer.auth_supplied_count_);
EXPECT_EQ(0, observer.auth_needed_count_);
EXPECT_EQ(0, observer.auth_cancelled_count_);
EXPECT_TRUE(test_server()->Stop());
}
IN_PROC_BROWSER_TEST_F(LoginPromptBrowserTest,
NoLoginPromptForXHRWithGoodCredentials) {
const char* kXHRTestPage = "files/login/xhr_with_credentials.html#secret";
ASSERT_TRUE(test_server()->Start());
content::WebContents* contents =
browser()->tab_strip_model()->GetActiveWebContents();
NavigationController* controller = &contents->GetController();
LoginPromptBrowserTestObserver observer;
observer.Register(content::Source<NavigationController>(controller));
{
GURL test_page = test_server()->GetURL(kXHRTestPage);
WindowedLoadStopObserver load_stop_waiter(controller, 1);
browser()->OpenURL(OpenURLParams(
test_page, Referrer(), CURRENT_TAB, content::PAGE_TRANSITION_TYPED,
false));
load_stop_waiter.Wait();
}
base::string16 expected_title(base::UTF8ToUTF16("status=200"));
EXPECT_EQ(expected_title, contents->GetTitle());
EXPECT_EQ(0, observer.auth_supplied_count_);
EXPECT_EQ(0, observer.auth_needed_count_);
EXPECT_EQ(0, observer.auth_cancelled_count_);
EXPECT_TRUE(test_server()->Stop());
}
IN_PROC_BROWSER_TEST_F(LoginPromptBrowserTest,
LoginPromptForXHRWithoutCredentials) {
const char* kXHRTestPage = "files/login/xhr_without_credentials.html";
ASSERT_TRUE(test_server()->Start());
content::WebContents* contents =
browser()->tab_strip_model()->GetActiveWebContents();
NavigationController* controller = &contents->GetController();
LoginPromptBrowserTestObserver observer;
observer.Register(content::Source<NavigationController>(controller));
{
GURL test_page = test_server()->GetURL(kXHRTestPage);
WindowedAuthNeededObserver auth_needed_waiter(controller);
browser()->OpenURL(OpenURLParams(
test_page, Referrer(), CURRENT_TAB, content::PAGE_TRANSITION_TYPED,
false));
auth_needed_waiter.Wait();
}
ASSERT_FALSE(observer.handlers_.empty());
{
WindowedAuthNeededObserver auth_needed_waiter(controller);
WindowedAuthSuppliedObserver auth_supplied_waiter(controller);
LoginHandler* handler = *observer.handlers_.begin();
ASSERT_TRUE(handler);
handler->SetAuth(base::UTF8ToUTF16(bad_username_),
base::UTF8ToUTF16(bad_password_));
auth_supplied_waiter.Wait();
auth_needed_waiter.Wait();
}
ASSERT_EQ(1u, observer.handlers_.size());
WindowedAuthSuppliedObserver auth_supplied_waiter(controller);
LoginHandler* handler = *observer.handlers_.begin();
base::string16 username(base::UTF8ToUTF16(username_digest_));
base::string16 password(base::UTF8ToUTF16(password_));
handler->SetAuth(username, password);
auth_supplied_waiter.Wait();
WindowedLoadStopObserver load_stop_waiter(controller, 1);
load_stop_waiter.Wait();
base::string16 expected_title(base::UTF8ToUTF16("status=200"));
EXPECT_EQ(expected_title, contents->GetTitle());
EXPECT_EQ(2, observer.auth_supplied_count_);
EXPECT_EQ(2, observer.auth_needed_count_);
EXPECT_EQ(0, observer.auth_cancelled_count_);
EXPECT_TRUE(test_server()->Stop());
}
IN_PROC_BROWSER_TEST_F(LoginPromptBrowserTest,
LoginPromptForXHRWithoutCredentialsCancelled) {
const char* kXHRTestPage = "files/login/xhr_without_credentials.html";
ASSERT_TRUE(test_server()->Start());
content::WebContents* contents =
browser()->tab_strip_model()->GetActiveWebContents();
NavigationController* controller = &contents->GetController();
LoginPromptBrowserTestObserver observer;
observer.Register(content::Source<NavigationController>(controller));
{
GURL test_page = test_server()->GetURL(kXHRTestPage);
WindowedAuthNeededObserver auth_needed_waiter(controller);
browser()->OpenURL(OpenURLParams(
test_page, Referrer(), CURRENT_TAB, content::PAGE_TRANSITION_TYPED,
false));
auth_needed_waiter.Wait();
}
ASSERT_EQ(1u, observer.handlers_.size());
WindowedAuthCancelledObserver auth_cancelled_waiter(controller);
LoginHandler* handler = *observer.handlers_.begin();
handler->CancelAuth();
auth_cancelled_waiter.Wait();
WindowedLoadStopObserver load_stop_waiter(controller, 1);
load_stop_waiter.Wait();
base::string16 expected_title(base::UTF8ToUTF16("status=401"));
EXPECT_EQ(expected_title, contents->GetTitle());
EXPECT_EQ(0, observer.auth_supplied_count_);
EXPECT_EQ(1, observer.auth_needed_count_);
EXPECT_EQ(1, observer.auth_cancelled_count_);
EXPECT_TRUE(test_server()->Stop());
}
}