Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions browser/infobars/request_otr_infobar_delegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class RequestOTRInfoBarDelegate : public ConfirmInfoBarDelegate {
// Creates a request-otr infobar and delegate and adds the infobar to
// |infobar_manager|.
static void Create(infobars::ContentInfoBarManager* infobar_manager);
void TestAccept() { Accept(); }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add FRIEND_TEST_ALL_PREFIXES for the test class instead


private:
// ConfirmInfoBarDelegate:
Expand Down
239 changes: 239 additions & 0 deletions browser/request_otr/request_otr_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@
#include "chrome/test/base/ui_test_utils.h"
#include "components/history/core/browser/history_service.h"
#include "components/infobars/content/content_infobar_manager.h"
#include "components/infobars/core/infobar.h"
#include "components/infobars/core/infobar_manager.h"
#include "components/infobars/core/infobar_delegate.h"
#include "brave/browser/infobars/request_otr_infobar_delegate.h"
#include "components/security_interstitials/content/security_interstitial_tab_helper.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
Expand Down Expand Up @@ -181,6 +184,31 @@ class RequestOTRBrowserTestBase : public BaseLocalDataFilesBrowserTest {
return history_count;
}

std::string GetHistoryEntry(const GURL& url) {
history::HistoryService* history_service =
HistoryServiceFactory::GetForProfile(
browser()->profile(), ServiceAccessType::IMPLICIT_ACCESS);
CHECK(history_service);

std::string title = "";
base::RunLoop loop;
base::CancelableTaskTracker task_tracker;

history_service->QueryURL(
url,
/*want_visits=*/false,
base::BindLambdaForTesting([&](history::QueryURLResult result) {
if (result.success) {
title = base::UTF16ToUTF8(result.row.title());
}
loop.Quit(); // Exit the RunLoop once the query is complete
}),
&task_tracker);

loop.Run(); // Run the loop until Quit is called
return title;
}

private:
base::test::ScopedFeatureList scoped_feature_list_;
};
Expand Down Expand Up @@ -238,6 +266,7 @@ IN_PROC_BROWSER_TEST_F(RequestOTRBrowserTest, ShowInterstitialAlways) {
GURL url = embedded_test_server()->GetURL("sensitive.a.com", "/simple.html");
NavigateTo(url);
ASSERT_FALSE(IsShowingInterstitial());

// Request-OTR infobar should now have been shown, and our observer should
// have been called once.

Expand Down Expand Up @@ -276,6 +305,39 @@ IN_PROC_BROWSER_TEST_F(RequestOTRBrowserTest, ShowInterstitialAndProceedOTR) {
infobar_manager->RemoveObserver(&observer);
}

IN_PROC_BROWSER_TEST_F(RequestOTRBrowserTest, ShowInterstitialProceedOTRAndCheckHistory) {
ASSERT_TRUE(InstallMockExtension());

// If request-otr pref is 'ask', should show interstitial because
// this URL is included in config file. If user clicks 'Proceed
// Off-The-Record' then we should navigate to the originally requested page
// and show an infobar indicating to the user that they are navigating in
// off-the-record mode.
auto* model = browser()->tab_strip_model();
auto* contents = model->GetActiveWebContents();
auto* infobar_manager =
infobars::ContentInfoBarManager::FromWebContents(contents);
TestObserver observer;
// Set up expectation that an infobar will appear later.
EXPECT_CALL(observer, OnInfoBarAdded(_)).Times(1);
infobar_manager->AddObserver(&observer);

SetRequestOTRPref(RequestOTRService::RequestOTRActionOption::kAsk);
GURL url = embedded_test_server()->GetURL("sensitive.a.com", "/simple.html");
NavigateTo(url);
ASSERT_TRUE(IsShowingInterstitial());

// Simulate click on "Proceed Off-The-Record" button. This should navigate to
// the originally requested page in off-the-record mode.
ClickAndWaitForNavigation("primary-button");
ASSERT_FALSE(IsShowingInterstitial());
ASSERT_EQ(GetHistoryCount(), 0);
// Request-OTR infobar should now have been shown, and our observer should
// have been called once.

infobar_manager->RemoveObserver(&observer);
}

IN_PROC_BROWSER_TEST_F(RequestOTRBrowserTest,
ShowInterstitialAndProceedNormally) {
ASSERT_TRUE(InstallMockExtension());
Expand Down Expand Up @@ -308,6 +370,146 @@ IN_PROC_BROWSER_TEST_F(RequestOTRBrowserTest,
infobar_manager->RemoveObserver(&observer);
}

IN_PROC_BROWSER_TEST_F(RequestOTRBrowserTest, ShowInterstitialProceedOTRCheckHistoryAndNonOTR) {
ASSERT_TRUE(InstallMockExtension());

// If request-otr pref is 'ask', should show interstitial because
// this URL is included in config file. If user clicks 'Proceed
// Off-The-Record' then we should navigate to the originally requested page
// and show an infobar indicating to the user that they are navigating in
// off-the-record mode.
auto* model = browser()->tab_strip_model();
auto* contents = model->GetActiveWebContents();
auto* infobar_manager =
infobars::ContentInfoBarManager::FromWebContents(contents);
TestObserver observer;
// Set up expectation that an infobar will appear later.
EXPECT_CALL(observer, OnInfoBarAdded(_)).Times(1);
infobar_manager->AddObserver(&observer);

SetRequestOTRPref(RequestOTRService::RequestOTRActionOption::kAsk);
GURL url = embedded_test_server()->GetURL("sensitive.a.com", "/simple.html");
NavigateTo(url);
ASSERT_TRUE(IsShowingInterstitial());

// Simulate click on "Proceed Off-The-Record" button. This should navigate to
// the originally requested page in off-the-record mode.
ClickAndWaitForNavigation("primary-button");
ASSERT_FALSE(IsShowingInterstitial());
ASSERT_EQ(GetHistoryCount(), 0);

// Iterate over InfoBars to find RequestOTRInfoBarDelegate.
infobars::InfoBar* found_infobar = nullptr;
for (infobars::InfoBar* infobar : infobar_manager->infobars()) {
if (infobar->delegate()->GetIdentifier() == infobars::InfoBarDelegate::BRAVE_REQUEST_OTR_INFOBAR_DELEGATE) {
found_infobar = infobar;
break;
}
}

ASSERT_NE(found_infobar, nullptr);

// Now you have the specific InfoBar. Access its delegate.
auto* delegate = static_cast<RequestOTRInfoBarDelegate*>(found_infobar->delegate());
ASSERT_NE(delegate, nullptr);

// Simulate clicking the "Turn Off" button.
delegate->TestAccept();

NavigateTo(url);
ASSERT_TRUE(IsShowingInterstitial());

// Simulate click on "Proceed Normally" button. This should navigate to
// the originally requested page in "normal" mode.
ClickAndWaitForNavigation("back-button");
ASSERT_FALSE(IsShowingInterstitial());
ASSERT_EQ(GetHistoryCount(), 1);
// Request-OTR infobar should now have been shown, and our observer should
// have been called once.

infobar_manager->RemoveObserver(&observer);
}

IN_PROC_BROWSER_TEST_F(RequestOTRBrowserTest, ShowInterstitialProceedOTRCheckHistoryAndNonOTRAndOTRAgain) {
ASSERT_TRUE(InstallMockExtension());

// If request-otr pref is 'ask', should show interstitial because
// this URL is included in config file. If user clicks 'Proceed
// Off-The-Record' then we should navigate to the originally requested page
// and show an infobar indicating to the user that they are navigating in
// off-the-record mode.
auto* model = browser()->tab_strip_model();
auto* contents = model->GetActiveWebContents();
auto* infobar_manager =
infobars::ContentInfoBarManager::FromWebContents(contents);
TestObserver observer;
// Set up expectation that an infobar will appear later.
EXPECT_CALL(observer, OnInfoBarAdded(_)).Times(2);
infobar_manager->AddObserver(&observer);

SetRequestOTRPref(RequestOTRService::RequestOTRActionOption::kAsk);
GURL url = embedded_test_server()->GetURL("sensitive.a.com", "/simple.html");
NavigateTo(url);
ASSERT_TRUE(IsShowingInterstitial());

// Simulate click on "Proceed Off-The-Record" button. This should navigate to
// the originally requested page in off-the-record mode.
ClickAndWaitForNavigation("primary-button");
ASSERT_FALSE(IsShowingInterstitial());
ASSERT_EQ(GetHistoryCount(), 0);

// Iterate over InfoBars to find RequestOTRInfoBarDelegate.
infobars::InfoBar* found_infobar = nullptr;
for (infobars::InfoBar* infobar : infobar_manager->infobars()) {
if (infobar->delegate()->GetIdentifier() == infobars::InfoBarDelegate::BRAVE_REQUEST_OTR_INFOBAR_DELEGATE) {
found_infobar = infobar;
break;
}
}

ASSERT_NE(found_infobar, nullptr);

// Now you have the specific InfoBar. Access its delegate.
auto* delegate = static_cast<RequestOTRInfoBarDelegate*>(found_infobar->delegate());
ASSERT_NE(delegate, nullptr);

// Simulate clicking the 'Turn-Off' button.
delegate->TestAccept();

NavigateTo(url);
ASSERT_TRUE(IsShowingInterstitial());

// Simulate click on "Proceed Normally" button. This should navigate to
// the originally requested page in "normal" mode.
ClickAndWaitForNavigation("back-button");
ASSERT_FALSE(IsShowingInterstitial());
ASSERT_EQ(GetHistoryCount(), 1);

std::string title = GetHistoryEntry(url);
std::string expected = "sensitive.a.com:"; // We test only the start as the port changes
size_t pos = title.find(':');
std::string actual = title.substr(0, pos + 1);
ASSERT_EQ(actual, expected);

NavigateTo(url);
ASSERT_TRUE(IsShowingInterstitial());

// Simulate click on "Proceed Off-The-Record" button. This should navigate to
// the originally requested page in off-the-record mode.
ClickAndWaitForNavigation("primary-button");
ASSERT_FALSE(IsShowingInterstitial());
ASSERT_EQ(GetHistoryCount(), 1); // It is still one

title = GetHistoryEntry(url);
ASSERT_EQ(title, expected); // Now it sets it to "sensitive website" which is not ok


// Request-OTR infobar should now have been shown, and our observer should
// have been called once.

infobar_manager->RemoveObserver(&observer);
}

// Check that a URL affected by both include and exclude rules is properly
// excluded.
IN_PROC_BROWSER_TEST_F(RequestOTRBrowserTest,
Expand Down Expand Up @@ -358,6 +560,43 @@ IN_PROC_BROWSER_TEST_F(RequestOTRBrowserTest,
ASSERT_EQ(GetHistoryCount(), 0);
}

// Now check that URLs are added to history after navigation in
// Request-OTR-tab mode and then on non-Request-OTR-tab mode
IN_PROC_BROWSER_TEST_F(RequestOTRBrowserTest,
HistoryRecordedAfterOTRNavigationNonOTRNavigation) {
ASSERT_TRUE(InstallMockExtension());

ASSERT_EQ(GetHistoryCount(), 0);
SetRequestOTRPref(RequestOTRService::RequestOTRActionOption::kAlways);
NavigateTo(embedded_test_server()->GetURL("sensitive.a.com", "/simple.html"));
ASSERT_EQ(GetHistoryCount(), 0);

SetRequestOTRPref(RequestOTRService::RequestOTRActionOption::kNever);
NavigateTo(embedded_test_server()->GetURL("sensitive.a.com", "/simple.html"));
ASSERT_EQ(GetHistoryCount(), 1); // It fails here: should be one
}

// Now check that URLs are not added to history after navigation in
// Request-OTR-tab mode, then on non-Request-OTR-tab mode and then on Request-OTR-tab
// mode again
IN_PROC_BROWSER_TEST_F(RequestOTRBrowserTest,
HistoryNotRecordedAfterOTRNavigationNonOTRNavigationTwice) {
ASSERT_TRUE(InstallMockExtension());

ASSERT_EQ(GetHistoryCount(), 0);
SetRequestOTRPref(RequestOTRService::RequestOTRActionOption::kAlways);
NavigateTo(embedded_test_server()->GetURL("sensitive.a.com", "/simple.html"));
ASSERT_EQ(GetHistoryCount(), 0);

SetRequestOTRPref(RequestOTRService::RequestOTRActionOption::kNever);
NavigateTo(embedded_test_server()->GetURL("sensitive.a.com", "/simple.html"));
ASSERT_EQ(GetHistoryCount(), 1); // It fails here: should be one

SetRequestOTRPref(RequestOTRService::RequestOTRActionOption::kAlways);
NavigateTo(embedded_test_server()->GetURL("sensitive.a.com", "/simple.html"));
ASSERT_EQ(GetHistoryCount(), 1); // It fails here: should be one
}

IN_PROC_BROWSER_TEST_F(RequestOTRBrowserTest,
WindowOpenAfterStandardNavigationCrossOrigin) {
NavigateTo(embedded_test_server()->GetURL("sensitive.a.com", "/simple.html"));
Expand Down