1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/apps/link_capturing/link_capturing_navigation_throttle.h"
#include <map>
#include <string>
#include "base/test/scoped_feature_list.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_features.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/mock_navigation_handle.h"
#include "content/public/test/mock_navigation_throttle_registry.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/page_transition_types.h"
#include "url/gurl.h"
namespace apps {
namespace {
TEST(LinkCapturingNavigationThrottleTest, TestIsGoogleRedirectorUrl) {
// Test that redirect urls with different TLDs are still recognized.
EXPECT_TRUE(LinkCapturingNavigationThrottle::IsGoogleRedirectorUrl(
GURL("https://www.google.com.au/url?q=whatever")));
EXPECT_TRUE(LinkCapturingNavigationThrottle::IsGoogleRedirectorUrl(
GURL("https://www.google.com.mx/url?q=hotpot")));
EXPECT_TRUE(LinkCapturingNavigationThrottle::IsGoogleRedirectorUrl(
GURL("https://www.google.co/url?q=query")));
// Non-google domains shouldn't be used as valid redirect links.
EXPECT_FALSE(LinkCapturingNavigationThrottle::IsGoogleRedirectorUrl(
GURL("https://www.not-google.com/url?q=query")));
EXPECT_FALSE(LinkCapturingNavigationThrottle::IsGoogleRedirectorUrl(
GURL("https://www.gooogle.com/url?q=legit_query")));
// This method only takes "/url" as a valid path, it needs to contain a query,
// we don't analyze that query as it will expand later on in the same
// throttle.
EXPECT_TRUE(LinkCapturingNavigationThrottle::IsGoogleRedirectorUrl(
GURL("https://www.google.com/url?q=who_dis")));
EXPECT_TRUE(LinkCapturingNavigationThrottle::IsGoogleRedirectorUrl(
GURL("http://www.google.com/url?q=who_dis")));
EXPECT_FALSE(LinkCapturingNavigationThrottle::IsGoogleRedirectorUrl(
GURL("https://www.google.com/url")));
EXPECT_FALSE(LinkCapturingNavigationThrottle::IsGoogleRedirectorUrl(
GURL("https://www.google.com/link?q=query")));
EXPECT_FALSE(LinkCapturingNavigationThrottle::IsGoogleRedirectorUrl(
GURL("https://www.google.com/link")));
}
TEST(LinkCapturingNavigationThrottleTest, TestShouldOverrideUrlLoading) {
// A navigation from chrome-extension scheme cannot be overridden.
EXPECT_FALSE(LinkCapturingNavigationThrottle::ShouldOverrideUrlIfRedirected(
GURL("chrome-extension://fake_document"), GURL("http://www.a.com")));
EXPECT_FALSE(LinkCapturingNavigationThrottle::ShouldOverrideUrlIfRedirected(
GURL("chrome-extension://fake_document"), GURL("https://www.a.com")));
EXPECT_FALSE(LinkCapturingNavigationThrottle::ShouldOverrideUrlIfRedirected(
GURL("chrome-extension://fake_a"), GURL("chrome-extension://fake_b")));
// Other navigations can be overridden.
EXPECT_TRUE(LinkCapturingNavigationThrottle::ShouldOverrideUrlIfRedirected(
GURL("http://www.google.com"), GURL("http://www.not-google.com/")));
EXPECT_TRUE(LinkCapturingNavigationThrottle::ShouldOverrideUrlIfRedirected(
GURL("http://www.not-google.com"), GURL("http://www.google.com/")));
EXPECT_TRUE(LinkCapturingNavigationThrottle::ShouldOverrideUrlIfRedirected(
GURL("http://www.google.com"), GURL("http://www.google.com/")));
EXPECT_TRUE(LinkCapturingNavigationThrottle::ShouldOverrideUrlIfRedirected(
GURL("http://a.google.com"), GURL("http://b.google.com/")));
EXPECT_TRUE(LinkCapturingNavigationThrottle::ShouldOverrideUrlIfRedirected(
GURL("http://a.not-google.com"), GURL("http://b.not-google.com")));
EXPECT_TRUE(LinkCapturingNavigationThrottle::ShouldOverrideUrlIfRedirected(
GURL("chrome://fake_document"), GURL("http://www.a.com")));
EXPECT_TRUE(LinkCapturingNavigationThrottle::ShouldOverrideUrlIfRedirected(
GURL("file://fake_document"), GURL("http://www.a.com")));
EXPECT_TRUE(LinkCapturingNavigationThrottle::ShouldOverrideUrlIfRedirected(
GURL("chrome://fake_document"), GURL("https://www.a.com")));
EXPECT_TRUE(LinkCapturingNavigationThrottle::ShouldOverrideUrlIfRedirected(
GURL("file://fake_document"), GURL("https://www.a.com")));
// A navigation going to a redirect url cannot be overridden, unless there's
// no query or the path is not valid.
EXPECT_FALSE(LinkCapturingNavigationThrottle::ShouldOverrideUrlIfRedirected(
GURL("http://www.google.com"), GURL("https://www.google.com/url?q=b")));
EXPECT_FALSE(LinkCapturingNavigationThrottle::ShouldOverrideUrlIfRedirected(
GURL("https://www.a.com"), GURL("https://www.google.com/url?q=a")));
EXPECT_TRUE(LinkCapturingNavigationThrottle::ShouldOverrideUrlIfRedirected(
GURL("https://www.a.com"), GURL("https://www.google.com/url")));
EXPECT_TRUE(LinkCapturingNavigationThrottle::ShouldOverrideUrlIfRedirected(
GURL("https://www.a.com"), GURL("https://www.google.com/link?q=a")));
}
// Tests that LinkCapturingNavigationThrottle::ShouldIgnoreNavigation returns
// false only for PAGE_TRANSITION_LINK when |allow_form_submit| is false and
// |is_in_fenced_frame_tree| is false.
TEST(LinkCapturingNavigationThrottleTest,
TestShouldIgnoreNavigationWithCoreTypes) {
EXPECT_TRUE(LinkCapturingNavigationThrottle::IsCapturableLinkNavigation(
ui::PAGE_TRANSITION_LINK, false, false, false));
EXPECT_FALSE(LinkCapturingNavigationThrottle::IsCapturableLinkNavigation(
ui::PAGE_TRANSITION_TYPED, false, false, false));
EXPECT_FALSE(LinkCapturingNavigationThrottle::IsCapturableLinkNavigation(
ui::PAGE_TRANSITION_AUTO_BOOKMARK, false, false, false));
EXPECT_FALSE(LinkCapturingNavigationThrottle::IsCapturableLinkNavigation(
ui::PAGE_TRANSITION_AUTO_SUBFRAME, false, false, false));
EXPECT_FALSE(LinkCapturingNavigationThrottle::IsCapturableLinkNavigation(
ui::PAGE_TRANSITION_MANUAL_SUBFRAME, false, false, false));
EXPECT_FALSE(LinkCapturingNavigationThrottle::IsCapturableLinkNavigation(
ui::PAGE_TRANSITION_GENERATED, false, false, false));
EXPECT_FALSE(LinkCapturingNavigationThrottle::IsCapturableLinkNavigation(
ui::PAGE_TRANSITION_AUTO_TOPLEVEL, false, false, false));
EXPECT_FALSE(LinkCapturingNavigationThrottle::IsCapturableLinkNavigation(
ui::PAGE_TRANSITION_FORM_SUBMIT, false, false, false));
EXPECT_FALSE(LinkCapturingNavigationThrottle::IsCapturableLinkNavigation(
ui::PAGE_TRANSITION_RELOAD, false, false, false));
EXPECT_FALSE(LinkCapturingNavigationThrottle::IsCapturableLinkNavigation(
ui::PAGE_TRANSITION_KEYWORD, false, false, false));
EXPECT_FALSE(LinkCapturingNavigationThrottle::IsCapturableLinkNavigation(
ui::PAGE_TRANSITION_KEYWORD_GENERATED, false, false, false));
static_assert(static_cast<int32_t>(ui::PAGE_TRANSITION_KEYWORD_GENERATED) ==
static_cast<int32_t>(ui::PAGE_TRANSITION_LAST_CORE),
"Not all core transition types are covered here");
}
// Test that LinkCapturingNavigationThrottle::ShouldIgnoreNavigation accepts
// FORM_SUBMIT when |allow_form_submit| is true.
TEST(LinkCapturingNavigationThrottleTest, TestFormSubmit) {
EXPECT_TRUE(LinkCapturingNavigationThrottle::IsCapturableLinkNavigation(
ui::PAGE_TRANSITION_FORM_SUBMIT, true, false, false));
EXPECT_FALSE(LinkCapturingNavigationThrottle::IsCapturableLinkNavigation(
ui::PAGE_TRANSITION_FORM_SUBMIT, false, false, false));
}
// Tests that LinkCapturingNavigationThrottle::ShouldIgnoreNavigation returns
// true when no qualifiers except client redirect and server redirect are
// provided when |is_in_fenced_frame_tree| is false.
TEST(LinkCapturingNavigationThrottleTest,
TestShouldIgnoreNavigationWithLinkWithQualifiers) {
// The navigation is triggered by Forward or Back button.
EXPECT_FALSE(LinkCapturingNavigationThrottle::IsCapturableLinkNavigation(
ui::PageTransitionFromInt(ui::PAGE_TRANSITION_LINK |
ui::PAGE_TRANSITION_FORWARD_BACK),
false, false, false));
EXPECT_FALSE(LinkCapturingNavigationThrottle::IsCapturableLinkNavigation(
ui::PageTransitionFromInt(ui::PAGE_TRANSITION_FORM_SUBMIT |
ui::PAGE_TRANSITION_FORWARD_BACK),
false, false, false));
// The user used the address bar to trigger the navigation.
EXPECT_FALSE(LinkCapturingNavigationThrottle::IsCapturableLinkNavigation(
ui::PageTransitionFromInt(ui::PAGE_TRANSITION_LINK |
ui::PAGE_TRANSITION_FROM_ADDRESS_BAR),
false, false, false));
// The user pressed the Home button.
EXPECT_FALSE(LinkCapturingNavigationThrottle::IsCapturableLinkNavigation(
ui::PageTransitionFromInt(ui::PAGE_TRANSITION_LINK |
ui::PAGE_TRANSITION_HOME_PAGE),
false, false, false));
// ARC (for example) opened the link in Chrome.
EXPECT_FALSE(LinkCapturingNavigationThrottle::IsCapturableLinkNavigation(
ui::PageTransitionFromInt(ui::PAGE_TRANSITION_LINK |
ui::PAGE_TRANSITION_FROM_API),
false, false, false));
// The navigation is triggered by a client side redirect.
EXPECT_TRUE(LinkCapturingNavigationThrottle::IsCapturableLinkNavigation(
ui::PageTransitionFromInt(ui::PAGE_TRANSITION_LINK |
ui::PAGE_TRANSITION_CLIENT_REDIRECT),
false, false, false));
// Also tests the case with 2+ qualifiers.
EXPECT_FALSE(LinkCapturingNavigationThrottle::IsCapturableLinkNavigation(
ui::PageTransitionFromInt(ui::PAGE_TRANSITION_LINK |
ui::PAGE_TRANSITION_FROM_ADDRESS_BAR |
ui::PAGE_TRANSITION_CLIENT_REDIRECT),
false, false, false));
// The navigation is triggered by a server side redirect.
EXPECT_TRUE(LinkCapturingNavigationThrottle::IsCapturableLinkNavigation(
ui::PageTransitionFromInt(ui::PAGE_TRANSITION_LINK |
ui::PAGE_TRANSITION_SERVER_REDIRECT),
false, false, false));
// Also tests the case with 2+ qualifiers.
EXPECT_FALSE(LinkCapturingNavigationThrottle::IsCapturableLinkNavigation(
ui::PageTransitionFromInt(ui::PAGE_TRANSITION_LINK |
ui::PAGE_TRANSITION_FROM_ADDRESS_BAR |
ui::PAGE_TRANSITION_SERVER_REDIRECT),
false, false, false));
}
// Just in case, does the same with ui::PAGE_TRANSITION_TYPED.
TEST(LinkCapturingNavigationThrottleTest,
TestShouldIgnoreNavigationWithTypedWithQualifiers) {
EXPECT_FALSE(LinkCapturingNavigationThrottle::IsCapturableLinkNavigation(
ui::PageTransitionFromInt(ui::PAGE_TRANSITION_TYPED |
ui::PAGE_TRANSITION_FORWARD_BACK),
false, false, false));
EXPECT_FALSE(LinkCapturingNavigationThrottle::IsCapturableLinkNavigation(
ui::PageTransitionFromInt(ui::PAGE_TRANSITION_TYPED |
ui::PAGE_TRANSITION_FORWARD_BACK),
false, false, false));
EXPECT_FALSE(LinkCapturingNavigationThrottle::IsCapturableLinkNavigation(
ui::PageTransitionFromInt(ui::PAGE_TRANSITION_TYPED |
ui::PAGE_TRANSITION_FROM_ADDRESS_BAR),
false, false, false));
EXPECT_FALSE(LinkCapturingNavigationThrottle::IsCapturableLinkNavigation(
ui::PageTransitionFromInt(ui::PAGE_TRANSITION_TYPED |
ui::PAGE_TRANSITION_HOME_PAGE),
false, false, false));
EXPECT_FALSE(LinkCapturingNavigationThrottle::IsCapturableLinkNavigation(
ui::PageTransitionFromInt(ui::PAGE_TRANSITION_TYPED |
ui::PAGE_TRANSITION_FROM_API),
false, false, false));
EXPECT_FALSE(LinkCapturingNavigationThrottle::IsCapturableLinkNavigation(
ui::PageTransitionFromInt(ui::PAGE_TRANSITION_TYPED |
ui::PAGE_TRANSITION_FROM_ADDRESS_BAR |
ui::PAGE_TRANSITION_CLIENT_REDIRECT),
false, false, false));
}
// Test that LinkCapturingNavigationThrottle::ShouldIgnoreNavigation accepts
// SERVER_REDIRECT and CLIENT_REDIRECT when |is_in_fenced_frame_tree| is false.
TEST(LinkCapturingNavigationThrottleTest,
TestShouldIgnoreNavigationWithClientRedirect) {
EXPECT_TRUE(LinkCapturingNavigationThrottle::IsCapturableLinkNavigation(
ui::PageTransitionFromInt(ui::PAGE_TRANSITION_LINK), false, false,
false));
EXPECT_TRUE(LinkCapturingNavigationThrottle::IsCapturableLinkNavigation(
ui::PageTransitionFromInt(ui::PAGE_TRANSITION_LINK |
ui::PAGE_TRANSITION_CLIENT_REDIRECT),
false, false, false));
EXPECT_TRUE(LinkCapturingNavigationThrottle::IsCapturableLinkNavigation(
ui::PageTransitionFromInt(ui::PAGE_TRANSITION_LINK |
ui::PAGE_TRANSITION_SERVER_REDIRECT),
false, false, false));
EXPECT_TRUE(LinkCapturingNavigationThrottle::IsCapturableLinkNavigation(
ui::PageTransitionFromInt(ui::PAGE_TRANSITION_LINK |
ui::PAGE_TRANSITION_SERVER_REDIRECT |
ui::PAGE_TRANSITION_SERVER_REDIRECT),
false, false, false));
EXPECT_FALSE(LinkCapturingNavigationThrottle::IsCapturableLinkNavigation(
ui::PageTransitionFromInt(ui::PAGE_TRANSITION_LINK |
ui::PAGE_TRANSITION_CLIENT_REDIRECT |
ui::PAGE_TRANSITION_HOME_PAGE),
false, false, false));
EXPECT_FALSE(LinkCapturingNavigationThrottle::IsCapturableLinkNavigation(
ui::PageTransitionFromInt(ui::PAGE_TRANSITION_LINK |
ui::PAGE_TRANSITION_HOME_PAGE),
false, false, false));
}
// Test that MaskOutPageTransition correctly remove a qualifier from a given
// |page_transition|.
TEST(LinkCapturingNavigationThrottleTest, TestMaskOutPageTransition) {
ui::PageTransition page_transition = ui::PageTransitionFromInt(
ui::PAGE_TRANSITION_LINK | ui::PAGE_TRANSITION_CLIENT_REDIRECT);
EXPECT_EQ(
static_cast<int>(ui::PAGE_TRANSITION_LINK),
static_cast<int>(LinkCapturingNavigationThrottle::MaskOutPageTransition(
page_transition, ui::PAGE_TRANSITION_CLIENT_REDIRECT)));
page_transition = ui::PageTransitionFromInt(
ui::PAGE_TRANSITION_LINK | ui::PAGE_TRANSITION_SERVER_REDIRECT);
EXPECT_EQ(
static_cast<int>(ui::PAGE_TRANSITION_LINK),
static_cast<int>(LinkCapturingNavigationThrottle::MaskOutPageTransition(
page_transition, ui::PAGE_TRANSITION_SERVER_REDIRECT)));
}
// Test that LinkCapturingNavigationThrottle::ShouldIgnoreNavigation accepts iff
// |has_user_gesture| is true when |is_in_fenced_frame_tree| is true.
TEST(LinkCapturingNavigationThrottleTest, TestInFencedFrameTree) {
EXPECT_FALSE(LinkCapturingNavigationThrottle::IsCapturableLinkNavigation(
ui::PageTransitionFromInt(ui::PAGE_TRANSITION_AUTO_SUBFRAME), false, true,
false));
EXPECT_TRUE(LinkCapturingNavigationThrottle::IsCapturableLinkNavigation(
ui::PageTransitionFromInt(ui::PAGE_TRANSITION_AUTO_SUBFRAME), false, true,
true));
}
class LinkCapturingNavThrottleReimplTest
: public testing::Test,
public testing::WithParamInterface<bool> {
public:
LinkCapturingNavThrottleReimplTest() {
std::map<std::string, std::string> parameters;
parameters["link_capturing_state"] = FlagBoolToReimpl();
scoped_feature_list_.InitAndEnableFeatureWithParameters(
features::kPwaNavigationCapturing, parameters);
}
std::string FlagBoolToReimpl() {
if (GetParam()) {
return "reimpl_default_on";
}
return "reimpl_default_off";
}
base::test::ScopedFeatureList scoped_feature_list_;
content::BrowserTaskEnvironment task_environment_;
};
TEST_P(LinkCapturingNavThrottleReimplTest, NotCreated) {
TestingProfile profile;
auto web_contents = content::WebContents::Create(
content::WebContents::CreateParams(&profile));
content::MockNavigationHandle handle(web_contents.get());
content::MockNavigationThrottleRegistry registry(
&handle,
content::MockNavigationThrottleRegistry::RegistrationMode::kHold);
EXPECT_FALSE(LinkCapturingNavigationThrottle::MaybeCreateAndAdd(
registry, /*delegate=*/nullptr));
}
INSTANTIATE_TEST_SUITE_P(All,
LinkCapturingNavThrottleReimplTest,
testing::Bool(),
[](const ::testing::TestParamInfo<bool> info) {
if (info.param) {
return "reimpl_default_on";
}
return "reimpl_default_off";
});
} // namespace
} // namespace apps
|