File: extension_api_frame_id_map_browsertest.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (92 lines) | stat: -rw-r--r-- 3,727 bytes parent folder | download | duplicates (5)
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
// 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 "base/uuid.h"
#include "chrome/browser/extensions/extension_browsertest.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/test/browser_test.h"
#include "extensions/browser/extension_api_frame_id_map.h"
#include "extensions/common/extension.h"
#include "extensions/test/test_extension_dir.h"
#include "ui/base/window_open_disposition.h"

namespace extensions {

using ExtensionApiFrameIdMapBrowserTest = ExtensionBrowserTest;

// Tests that extension frames have unique context IDs.
IN_PROC_BROWSER_TEST_F(ExtensionApiFrameIdMapBrowserTest, ContextIdsAreUnique) {
  static constexpr char kManifest[] =
      R"({
           "name": "My extension",
           "manifest_version": 3,
           "version": "0.1"
         })";
  TestExtensionDir test_dir;
  test_dir.WriteManifest(kManifest);
  test_dir.WriteFile(FILE_PATH_LITERAL("page1.html"), "<html>Foo</html>");
  test_dir.WriteFile(FILE_PATH_LITERAL("page2.html"), "<html>Bar</html>");

  const Extension* extension = LoadExtension(test_dir.UnpackedPath());
  ASSERT_TRUE(extension);

  // Open three frames, two of which point to page1.html.
  content::RenderFrameHost* page1_a_host =
      ui_test_utils::NavigateToURLWithDisposition(
          browser(), extension->GetResourceURL("page1.html"),
          WindowOpenDisposition::NEW_FOREGROUND_TAB,
          ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP);
  ASSERT_TRUE(page1_a_host);

  content::RenderFrameHost* page1_b_host =
      ui_test_utils::NavigateToURLWithDisposition(
          browser(), extension->GetResourceURL("page1.html"),
          WindowOpenDisposition::NEW_FOREGROUND_TAB,
          ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP);
  ASSERT_TRUE(page1_b_host);

  content::RenderFrameHost* page2_host =
      ui_test_utils::NavigateToURLWithDisposition(
          browser(), extension->GetResourceURL("page2.html"),
          WindowOpenDisposition::NEW_FOREGROUND_TAB,
          ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP);
  ASSERT_TRUE(page2_host);

  base::Uuid page1_a_context_id =
      ExtensionApiFrameIdMap::GetContextId(page1_a_host);
  base::Uuid page1_b_context_id =
      ExtensionApiFrameIdMap::GetContextId(page1_b_host);
  base::Uuid page2_context_id =
      ExtensionApiFrameIdMap::GetContextId(page2_host);

  // Re-fetching the IDs for the same host should return the same result.
  EXPECT_EQ(page1_a_context_id,
            ExtensionApiFrameIdMap::GetContextId(page1_a_host));
  EXPECT_EQ(page1_b_context_id,
            ExtensionApiFrameIdMap::GetContextId(page1_b_host));
  EXPECT_EQ(page2_context_id, ExtensionApiFrameIdMap::GetContextId(page2_host));

  // All three frames should have unique IDs (even though two show the same
  // resource).
  EXPECT_NE(page1_a_context_id, page1_b_context_id);
  EXPECT_NE(page1_a_context_id, page2_context_id);
  EXPECT_NE(page1_b_context_id, page2_context_id);

  // Navigate page2 to page2 (again). It should have a new (unique) context ID
  // since it's a new document.
  content::RenderFrameHost* page2_new_host =
      ui_test_utils::NavigateToURLWithDisposition(
          browser(), extension->GetResourceURL("page2.html"),
          WindowOpenDisposition::CURRENT_TAB,
          ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP);
  ASSERT_TRUE(page2_new_host);
  base::Uuid page2_new_context_id =
      ExtensionApiFrameIdMap::GetContextId(page2_new_host);

  EXPECT_NE(page2_new_context_id, page1_a_context_id);
  EXPECT_NE(page2_new_context_id, page1_b_context_id);
  EXPECT_NE(page2_new_context_id, page2_context_id);
}

}  // namespace extensions