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

content / browser / devtools / devtools_issue_storage_browsertest.cc [blame]

// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/command_line.h"
#include "base/test/scoped_feature_list.h"
#include "content/browser/devtools/devtools_instrumentation.h"
#include "content/browser/devtools/protocol/audits.h"
#include "content/browser/devtools/protocol/devtools_protocol_test_support.h"
#include "content/browser/devtools/render_frame_devtools_agent_host.h"
#include "content/browser/renderer_host/frame_tree_node.h"
#include "content/browser/renderer_host/render_frame_host_impl.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/public/browser/devtools_agent_host.h"
#include "content/public/browser/devtools_agent_host_client.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_features.h"
#include "content/public/common/url_constants.h"
#include "content/public/test/back_forward_cache_util.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_base.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/content_browser_test.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/public/test/fenced_frame_test_util.h"
#include "content/public/test/prerender_test_util.h"
#include "content/public/test/test_utils.h"
#include "content/shell/browser/shell.h"
#include "net/dns/mock_host_resolver.h"

namespace content {

class DevToolsIssueStorageBrowserTest : public DevToolsProtocolTest {
 public:
  void SetUpOnMainThread() override {
    host_resolver()->AddRule("*", "127.0.0.1");
    SetupCrossSiteRedirector(embedded_test_server());
  }

 protected:
  WebContentsImpl* web_contents() {
    return static_cast<WebContentsImpl*>(shell()->web_contents());
  }

  RenderFrameHostImpl* main_frame_host() {
    return web_contents()->GetPrimaryFrameTree().GetMainFrame();
  }

  void WaitForDummyIssueNotification() {
    base::Value::Dict notification =
        WaitForNotification("Audits.issueAdded", true);
    EXPECT_EQ(*notification.FindStringByDottedPath("issue.code"),
              protocol::Audits::InspectorIssueCodeEnum::CookieIssue);
  }
};

namespace {

void ReportDummyIssue(RenderFrameHostImpl* rfh) {
  auto issueDetails = protocol::Audits::InspectorIssueDetails::Create();
  auto inspector_issue =
      protocol::Audits::InspectorIssue::Create()
          .SetCode(protocol::Audits::InspectorIssueCodeEnum::CookieIssue)
          .SetDetails(issueDetails.Build())
          .Build();
  devtools_instrumentation::ReportBrowserInitiatedIssue(rfh,
                                                        inspector_issue.get());
}

}  // namespace

IN_PROC_BROWSER_TEST_F(DevToolsIssueStorageBrowserTest,
                       DevToolsReceivesBrowserIssues) {
  EXPECT_TRUE(NavigateToURL(shell(), GURL("about:blank")));
  // Report an empty SameSite cookie issue.
  ReportDummyIssue(main_frame_host());
  Attach();
  SendCommandSync("Audits.enable");
  // Verify we have received the SameSite issue.
  WaitForDummyIssueNotification();
}

IN_PROC_BROWSER_TEST_F(DevToolsIssueStorageBrowserTest,
                       DevToolsReceivesBrowserIssuesWhileAttached) {
  EXPECT_TRUE(NavigateToURL(shell(), GURL("about:blank")));
  Attach();
  SendCommandSync("Audits.enable");
  // Report an empty SameSite cookie issue.
  ReportDummyIssue(main_frame_host());
  // Verify we have received the SameSite issue.
  WaitForDummyIssueNotification();
}

IN_PROC_BROWSER_TEST_F(DevToolsIssueStorageBrowserTest,
                       DeleteSubframeWithIssue) {
  // 1) Navigate to a page with an OOP iframe.
  ASSERT_TRUE(embedded_test_server()->Start());
  GURL test_url =
      embedded_test_server()->GetURL("/devtools/page-with-oopif.html");
  EXPECT_TRUE(NavigateToURL(shell(), test_url));

  // 2) Report an empty SameSite cookie issue in the iframe.
  RenderFrameHostImpl* main_frame = main_frame_host();
  EXPECT_EQ(main_frame->child_count(), static_cast<unsigned>(1));
  RenderFrameHostImpl* iframe = main_frame->child_at(0)->current_frame_host();
  EXPECT_FALSE(iframe->is_main_frame());

  ReportDummyIssue(iframe);

  // 3) Delete the iframe from the page. This should cause the issue to be
  // re-assigned
  //    to the root frame.
  main_frame->RemoveChild(iframe->frame_tree_node());

  // 4) Open DevTools and enable Audits domain.
  Attach();
  SendCommandSync("Audits.enable");

  // 5) Verify we have received the SameSite issue on the main target.
  WaitForDummyIssueNotification();
}

IN_PROC_BROWSER_TEST_F(DevToolsIssueStorageBrowserTest,
                       MainFrameNavigationClearsIssues) {
  // 1) Navigate to about:blank.
  EXPECT_TRUE(NavigateToURL(shell(), GURL("about:blank")));

  // 2) Report an empty SameSite cookie issue.
  ReportDummyIssue(main_frame_host());

  // 3) Navigate to /devtools/navigation.html
  ASSERT_TRUE(embedded_test_server()->Start());
  GURL test_url = embedded_test_server()->GetURL("/devtools/navigation.html");
  EXPECT_TRUE(NavigateToURL(shell(), test_url));

  // 4) Open DevTools and enable Audits domain.
  Attach();
  SendCommandSync("Audits.enable");

  // 5) Verify that we haven't received any notifications.
  ASSERT_FALSE(HasExistingNotification());
}

class DevToolsIssueStorageWithBackForwardCacheBrowserTest
    : public DevToolsIssueStorageBrowserTest {
 public:
  void SetUpCommandLine(base::CommandLine* command_line) override {
    // Enable BackForwardCache, omitting this feature results in a crash.
    feature_list_.InitWithFeaturesAndParameters(
        GetDefaultEnabledBackForwardCacheFeaturesForTesting(),
        GetDefaultDisabledBackForwardCacheFeaturesForTesting());
  }

 protected:
  base::test::ScopedFeatureList feature_list_;
};

IN_PROC_BROWSER_TEST_F(DevToolsIssueStorageWithBackForwardCacheBrowserTest,
                       BackForwardCacheGoBack) {
  ASSERT_TRUE(embedded_test_server()->Start());
  GURL url_a(embedded_test_server()->GetURL("a.com", "/title1.html"));
  GURL url_b(embedded_test_server()->GetURL("b.com", "/title1.html"));

  // 1) Navigate to A.
  EXPECT_TRUE(NavigateToURL(shell(), url_a));
  RenderFrameHostImpl* rfh_a = main_frame_host();
  RenderFrameDeletedObserver rfh_a_deleted(rfh_a);

  // 2) Report an empty SameSite cookie issue.
  ReportDummyIssue(rfh_a);

  // 3) Navigate to B.
  //    The previous test verifies that the issue storage is cleared at
  //    this point.
  EXPECT_TRUE(NavigateToURL(shell(), url_b));
  EXPECT_TRUE(rfh_a->IsInBackForwardCache());

  // 4) Go back to A and expect that it is restored from the back-forward cache.
  web_contents()->GetController().GoBack();
  EXPECT_TRUE(WaitForLoadStop(shell()->web_contents()));
  EXPECT_FALSE(rfh_a_deleted.deleted());
  EXPECT_EQ(main_frame_host(), rfh_a);

  // 5) Open DevTools and enable Audits domain.
  Attach();
  SendCommandSync("Audits.enable");

  // 6) Verify we have received the SameSite issue on the main target.
  WaitForDummyIssueNotification();
}

class DevToolsIssueStorageWithPrerenderBrowserTest
    : public DevToolsIssueStorageBrowserTest {
 public:
  DevToolsIssueStorageWithPrerenderBrowserTest()
      : prerender_test_helper_(base::BindRepeating(
            &DevToolsIssueStorageWithPrerenderBrowserTest::GetWebContents,
            base::Unretained(this))) {}

  void SetUp() override {
    prerender_test_helper().RegisterServerRequestMonitor(
        embedded_test_server());
    DevToolsIssueStorageBrowserTest::SetUp();
  }

  test::PrerenderTestHelper& prerender_test_helper() {
    return prerender_test_helper_;
  }

 private:
  WebContents* GetWebContents() { return web_contents(); }
  test::PrerenderTestHelper prerender_test_helper_;
};

IN_PROC_BROWSER_TEST_F(DevToolsIssueStorageWithPrerenderBrowserTest,
                       IssueWhilePrerendering) {
  ASSERT_TRUE(embedded_test_server()->Start());
  GURL main_url(embedded_test_server()->GetURL("/empty.html"));
  GURL prerender_url(embedded_test_server()->GetURL("/title1.html"));

  // 1) Navigate to |main_url|.
  EXPECT_TRUE(NavigateToURL(shell(), main_url));

  // 2) Prerender |prerender_url|.
  FrameTreeNodeId host_id = prerender_test_helper().AddPrerender(prerender_url);
  RenderFrameHostImpl* prerender_rfh = static_cast<RenderFrameHostImpl*>(
      prerender_test_helper().GetPrerenderedMainFrameHost(host_id));

  // 3) Report an empty SameSite cookie issue in prerendering page.
  ReportDummyIssue(prerender_rfh);

  // 4) Activate prerendering page.
  prerender_test_helper().NavigatePrimaryPage(prerender_url);

  // 5) Open DevTools and enable Audits domain.
  Attach();
  SendCommandSync("Audits.enable");

  // 6) Verify we have received the SameSite issue on the main target.
  WaitForDummyIssueNotification();
}

class DevToolsIssueStorageFencedFrameTest
    : public DevToolsIssueStorageBrowserTest {
 public:
  content::test::FencedFrameTestHelper& fenced_frame_test_helper() {
    return fenced_frame_helper_;
  }

 protected:
  content::test::FencedFrameTestHelper fenced_frame_helper_;
};

IN_PROC_BROWSER_TEST_F(DevToolsIssueStorageFencedFrameTest,
                       DeleteFencedFrameWithIssue) {
  // 1) Navigate to a page.
  ASSERT_TRUE(embedded_test_server()->Start());
  GURL test_url = embedded_test_server()->GetURL("/title1.html");
  EXPECT_TRUE(NavigateToURL(shell(), test_url));

  // 2) Create a fenced frame.
  GURL fenced_frame_url =
      embedded_test_server()->GetURL("/fenced_frames/title1.html");
  content::RenderFrameHostImpl* fenced_frame_rfh =
      static_cast<RenderFrameHostImpl*>(
          fenced_frame_test_helper().CreateFencedFrame(
              web_contents()->GetPrimaryMainFrame(), fenced_frame_url));
  EXPECT_NE(nullptr, fenced_frame_rfh);

  // 3) Report an empty SameSite cookie issue in the fenced frame.
  ReportDummyIssue(fenced_frame_rfh);

  // 4) Delete the fenced frame from the page. This should cause the issue to be
  // re-assigned to the primary root frame.
  EXPECT_TRUE(ExecJs(shell()->web_contents(),
                     "document.querySelector('fencedframe').remove()"));

  // 5) Open DevTools and enable Audits domain.
  Attach();
  SendCommandSync("Audits.enable");

  // 6) Verify we have received the SameSite issue on the main target.
  WaitForDummyIssueNotification();
}

}  // namespace content