summaryrefslogtreecommitdiffstats
path: root/extensions/renderer/script_context_browsertest.cc
blob: 2559a69291081025ee3330a064a630a416c1fb18 (plain)
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
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/test/base/chrome_render_view_test.h"
#include "content/public/renderer/render_frame.h"
#include "content/public/test/frame_load_waiter.h"
#include "extensions/renderer/script_context.h"
#include "third_party/WebKit/public/web/WebDocument.h"
#include "third_party/WebKit/public/web/WebLocalFrame.h"
#include "url/gurl.h"

using blink::WebFrame;

namespace extensions {
namespace {

class ScriptContextTest : public ChromeRenderViewTest {
 protected:
  GURL GetEffectiveDocumentURL(const WebFrame* frame) {
    return ScriptContext::GetEffectiveDocumentURL(
        frame, frame->document().url(), true);
  }
};

TEST_F(ScriptContextTest, GetEffectiveDocumentURL) {
  GURL top_url("http://example.com/");
  GURL different_url("http://example.net/");
  GURL blank_url("about:blank");
  GURL srcdoc_url("about:srcdoc");

  const char frame_html[] =
      "<iframe name='frame1' srcdoc=\""
      "  <iframe name='frame1_1'></iframe>"
      "  <iframe name='frame1_2' sandbox=''></iframe>"
      "\"></iframe>"
      "<iframe name='frame2' sandbox='' srcdoc=\""
      "  <iframe name='frame2_1'></iframe>"
      "\"></iframe>"
      "<iframe name='frame3'></iframe>";

  const char frame3_html[] = "<iframe name='frame3_1'></iframe>";

  WebFrame* frame = GetMainFrame();
  ASSERT_TRUE(frame);

  frame->loadHTMLString(frame_html, top_url);
  content::FrameLoadWaiter(content::RenderFrame::FromWebFrame(frame)).Wait();

  WebFrame* frame1 = frame->findChildByName("frame1");
  ASSERT_TRUE(frame1);
  WebFrame* frame1_1 = frame1->findChildByName("frame1_1");
  ASSERT_TRUE(frame1_1);
  WebFrame* frame1_2 = frame1->findChildByName("frame1_2");
  ASSERT_TRUE(frame1_2);
  WebFrame* frame2 = frame->findChildByName("frame2");
  ASSERT_TRUE(frame2);
  WebFrame* frame2_1 = frame2->findChildByName("frame2_1");
  ASSERT_TRUE(frame2_1);
  WebFrame* frame3 = frame->findChildByName("frame3");
  ASSERT_TRUE(frame3);

  // Load a blank document in a frame from a different origin.
  frame3->loadHTMLString(frame3_html, different_url);
  content::FrameLoadWaiter(content::RenderFrame::FromWebFrame(frame3)).Wait();

  WebFrame* frame3_1 = frame->findChildByName("frame3");
  ASSERT_TRUE(frame3_1);

  // Top-level frame
  EXPECT_EQ(GetEffectiveDocumentURL(frame), top_url);
  // top -> srcdoc = inherit
  EXPECT_EQ(GetEffectiveDocumentURL(frame1), top_url);
  // top -> srcdoc -> about:blank = inherit
  EXPECT_EQ(GetEffectiveDocumentURL(frame1_1), top_url);
  // top -> srcdoc -> about:blank sandboxed = same URL
  EXPECT_EQ(GetEffectiveDocumentURL(frame1_2), blank_url);

  // top -> srcdoc [sandboxed] = same URL
  EXPECT_EQ(GetEffectiveDocumentURL(frame2), srcdoc_url);
  // top -> srcdoc [sandboxed] -> about:blank = same URL
  EXPECT_EQ(GetEffectiveDocumentURL(frame2_1), blank_url);

  // top -> different origin = different origin
  EXPECT_EQ(GetEffectiveDocumentURL(frame3), different_url);
  // top -> different origin -> about:blank = inherit
  EXPECT_EQ(GetEffectiveDocumentURL(frame3_1), different_url);
}

}  // namespace
}  // namespace extensions