[email protected] | 5e56df8 | 2011-04-18 17:00:15 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/common/content_settings.h" |
| 6 | #include "chrome/common/render_messages.h" |
| 7 | #include "chrome/renderer/content_settings_observer.h" |
[email protected] | c6d068ff | 2011-10-14 17:28:23 | [diff] [blame^] | 8 | #include "chrome/test/base/chrome_render_view_test.h" |
[email protected] | 5e56df8 | 2011-04-18 17:00:15 | [diff] [blame] | 9 | #include "content/common/view_messages.h" |
[email protected] | 068970d | 2011-10-11 00:05:16 | [diff] [blame] | 10 | #include "content/public/renderer/render_view.h" |
[email protected] | 4dad4a8 | 2011-04-27 15:44:52 | [diff] [blame] | 11 | #include "ipc/ipc_message_macros.h" |
| 12 | #include "testing/gmock/include/gmock/gmock.h" |
[email protected] | 5e56df8 | 2011-04-18 17:00:15 | [diff] [blame] | 13 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 566fa0f | 2011-06-01 23:26:06 | [diff] [blame] | 14 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
| 15 | |
| 16 | using testing::_; |
| 17 | using testing::DeleteArg; |
[email protected] | 5e56df8 | 2011-04-18 17:00:15 | [diff] [blame] | 18 | |
[email protected] | 4dad4a8 | 2011-04-27 15:44:52 | [diff] [blame] | 19 | namespace { |
| 20 | |
| 21 | class MockContentSettingsObserver : public ContentSettingsObserver { |
| 22 | public: |
[email protected] | 310ebd630 | 2011-10-10 19:06:28 | [diff] [blame] | 23 | explicit MockContentSettingsObserver(content::RenderView* render_view); |
[email protected] | 4dad4a8 | 2011-04-27 15:44:52 | [diff] [blame] | 24 | |
| 25 | virtual bool Send(IPC::Message* message); |
| 26 | |
| 27 | MOCK_METHOD2(OnContentBlocked, |
| 28 | void(ContentSettingsType, const std::string&)); |
[email protected] | 566fa0f | 2011-06-01 23:26:06 | [diff] [blame] | 29 | |
| 30 | MOCK_METHOD5(OnAllowDOMStorage, |
| 31 | void(int, const GURL&, const GURL&, bool, IPC::Message*)); |
[email protected] | 4dad4a8 | 2011-04-27 15:44:52 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | MockContentSettingsObserver::MockContentSettingsObserver( |
[email protected] | 310ebd630 | 2011-10-10 19:06:28 | [diff] [blame] | 35 | content::RenderView* render_view) |
[email protected] | 4dad4a8 | 2011-04-27 15:44:52 | [diff] [blame] | 36 | : ContentSettingsObserver(render_view) { |
| 37 | } |
| 38 | |
| 39 | bool MockContentSettingsObserver::Send(IPC::Message* message) { |
| 40 | IPC_BEGIN_MESSAGE_MAP(MockContentSettingsObserver, *message) |
[email protected] | 2ccf45c | 2011-08-19 23:35:50 | [diff] [blame] | 41 | IPC_MESSAGE_HANDLER(ChromeViewHostMsg_ContentBlocked, OnContentBlocked) |
| 42 | IPC_MESSAGE_HANDLER_DELAY_REPLY(ChromeViewHostMsg_AllowDOMStorage, |
[email protected] | 566fa0f | 2011-06-01 23:26:06 | [diff] [blame] | 43 | OnAllowDOMStorage) |
[email protected] | 4dad4a8 | 2011-04-27 15:44:52 | [diff] [blame] | 44 | IPC_MESSAGE_UNHANDLED(ADD_FAILURE()) |
| 45 | IPC_END_MESSAGE_MAP() |
| 46 | |
| 47 | // Our super class deletes the message. |
| 48 | return RenderViewObserver::Send(message); |
| 49 | } |
| 50 | |
| 51 | } // namespace |
| 52 | |
[email protected] | c6d068ff | 2011-10-14 17:28:23 | [diff] [blame^] | 53 | TEST_F(ChromeRenderViewTest, DidBlockContentType) { |
[email protected] | 4dad4a8 | 2011-04-27 15:44:52 | [diff] [blame] | 54 | MockContentSettingsObserver observer(view_); |
| 55 | EXPECT_CALL(observer, |
| 56 | OnContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES, std::string())); |
| 57 | observer.DidBlockContentType(CONTENT_SETTINGS_TYPE_COOKIES, std::string()); |
| 58 | |
| 59 | // Blocking the same content type a second time shouldn't send a notification. |
| 60 | observer.DidBlockContentType(CONTENT_SETTINGS_TYPE_COOKIES, std::string()); |
| 61 | ::testing::Mock::VerifyAndClearExpectations(&observer); |
| 62 | |
| 63 | // Blocking two different plugins should send two notifications. |
| 64 | std::string kFooPlugin = "foo"; |
| 65 | std::string kBarPlugin = "bar"; |
| 66 | EXPECT_CALL(observer, |
| 67 | OnContentBlocked(CONTENT_SETTINGS_TYPE_PLUGINS, kFooPlugin)); |
| 68 | EXPECT_CALL(observer, |
| 69 | OnContentBlocked(CONTENT_SETTINGS_TYPE_PLUGINS, kBarPlugin)); |
| 70 | observer.DidBlockContentType(CONTENT_SETTINGS_TYPE_PLUGINS, kFooPlugin); |
| 71 | observer.DidBlockContentType(CONTENT_SETTINGS_TYPE_PLUGINS, kBarPlugin); |
| 72 | } |
| 73 | |
[email protected] | 566fa0f | 2011-06-01 23:26:06 | [diff] [blame] | 74 | // Tests that multiple invokations of AllowDOMStorage result in a single IPC. |
[email protected] | c6d068ff | 2011-10-14 17:28:23 | [diff] [blame^] | 75 | TEST_F(ChromeRenderViewTest, AllowDOMStorage) { |
[email protected] | 566fa0f | 2011-06-01 23:26:06 | [diff] [blame] | 76 | // Load some HTML, so we have a valid security origin. |
| 77 | LoadHTML("<html></html>"); |
| 78 | MockContentSettingsObserver observer(view_); |
| 79 | ON_CALL(observer, |
| 80 | OnAllowDOMStorage(_, _, _, _, _)).WillByDefault(DeleteArg<4>()); |
| 81 | EXPECT_CALL(observer, |
| 82 | OnAllowDOMStorage(_, _, _, _, _)); |
[email protected] | 068970d | 2011-10-11 00:05:16 | [diff] [blame] | 83 | observer.AllowStorage(view_->GetWebView()->focusedFrame(), true); |
[email protected] | 566fa0f | 2011-06-01 23:26:06 | [diff] [blame] | 84 | |
| 85 | // Accessing localStorage from the same origin again shouldn't result in a |
| 86 | // new IPC. |
[email protected] | 068970d | 2011-10-11 00:05:16 | [diff] [blame] | 87 | observer.AllowStorage(view_->GetWebView()->focusedFrame(), true); |
[email protected] | 566fa0f | 2011-06-01 23:26:06 | [diff] [blame] | 88 | ::testing::Mock::VerifyAndClearExpectations(&observer); |
| 89 | } |
| 90 | |
[email protected] | 5e56df8 | 2011-04-18 17:00:15 | [diff] [blame] | 91 | // Regression test for http://crbug.com/35011 |
[email protected] | c6d068ff | 2011-10-14 17:28:23 | [diff] [blame^] | 92 | TEST_F(ChromeRenderViewTest, JSBlockSentAfterPageLoad) { |
[email protected] | 5e56df8 | 2011-04-18 17:00:15 | [diff] [blame] | 93 | // 1. Load page with JS. |
| 94 | std::string html = "<html>" |
[email protected] | 4dad4a8 | 2011-04-27 15:44:52 | [diff] [blame] | 95 | "<head>" |
| 96 | "<script>document.createElement('div');</script>" |
| 97 | "</head>" |
| 98 | "<body>" |
| 99 | "</body>" |
| 100 | "</html>"; |
[email protected] | c6d068ff | 2011-10-14 17:28:23 | [diff] [blame^] | 101 | render_thread_->sink().ClearMessages(); |
[email protected] | 5e56df8 | 2011-04-18 17:00:15 | [diff] [blame] | 102 | LoadHTML(html.c_str()); |
| 103 | |
| 104 | // 2. Block JavaScript. |
| 105 | ContentSettings settings; |
| 106 | for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) |
| 107 | settings.settings[i] = CONTENT_SETTING_ALLOW; |
| 108 | settings.settings[CONTENT_SETTINGS_TYPE_JAVASCRIPT] = CONTENT_SETTING_BLOCK; |
| 109 | ContentSettingsObserver* observer = ContentSettingsObserver::Get(view_); |
| 110 | observer->SetContentSettings(settings); |
[email protected] | 6326d8b | 2011-06-17 10:39:09 | [diff] [blame] | 111 | ContentSettingsObserver::SetDefaultContentSettings(settings); |
[email protected] | 5e56df8 | 2011-04-18 17:00:15 | [diff] [blame] | 112 | |
| 113 | // Make sure no pending messages are in the queue. |
| 114 | ProcessPendingMessages(); |
[email protected] | c6d068ff | 2011-10-14 17:28:23 | [diff] [blame^] | 115 | render_thread_->sink().ClearMessages(); |
[email protected] | 5e56df8 | 2011-04-18 17:00:15 | [diff] [blame] | 116 | |
| 117 | // 3. Reload page. |
| 118 | ViewMsg_Navigate_Params params; |
| 119 | std::string url_str = "data:text/html;charset=utf-8,"; |
| 120 | url_str.append(html); |
| 121 | GURL url(url_str); |
| 122 | params.url = url; |
| 123 | params.navigation_type = ViewMsg_Navigate_Type::RELOAD; |
[email protected] | 068970d | 2011-10-11 00:05:16 | [diff] [blame] | 124 | OnNavigate(params); |
[email protected] | 5e56df8 | 2011-04-18 17:00:15 | [diff] [blame] | 125 | ProcessPendingMessages(); |
| 126 | |
| 127 | // 4. Verify that the notification that javascript was blocked is sent after |
| 128 | // the navigation notifiction is sent. |
| 129 | int navigation_index = -1; |
| 130 | int block_index = -1; |
[email protected] | c6d068ff | 2011-10-14 17:28:23 | [diff] [blame^] | 131 | for (size_t i = 0; i < render_thread_->sink().message_count(); ++i) { |
| 132 | const IPC::Message* msg = render_thread_->sink().GetMessageAt(i); |
[email protected] | 5e56df8 | 2011-04-18 17:00:15 | [diff] [blame] | 133 | if (msg->type() == ViewHostMsg_FrameNavigate::ID) |
| 134 | navigation_index = i; |
[email protected] | 2ccf45c | 2011-08-19 23:35:50 | [diff] [blame] | 135 | if (msg->type() == ChromeViewHostMsg_ContentBlocked::ID) |
[email protected] | 5e56df8 | 2011-04-18 17:00:15 | [diff] [blame] | 136 | block_index = i; |
| 137 | } |
| 138 | EXPECT_NE(-1, navigation_index); |
| 139 | EXPECT_NE(-1, block_index); |
| 140 | EXPECT_LT(navigation_index, block_index); |
| 141 | } |
[email protected] | 134efc3 | 2011-08-02 18:14:21 | [diff] [blame] | 142 | |
[email protected] | c6d068ff | 2011-10-14 17:28:23 | [diff] [blame^] | 143 | TEST_F(ChromeRenderViewTest, PluginsTemporarilyAllowed) { |
[email protected] | 134efc3 | 2011-08-02 18:14:21 | [diff] [blame] | 144 | // Load some HTML. |
| 145 | LoadHTML("<html>Foo</html>"); |
| 146 | |
| 147 | // Block plugins. |
| 148 | ContentSettings settings; |
| 149 | for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) |
| 150 | settings.settings[i] = CONTENT_SETTING_ALLOW; |
| 151 | settings.settings[CONTENT_SETTINGS_TYPE_PLUGINS] = CONTENT_SETTING_BLOCK; |
| 152 | ContentSettingsObserver* observer = ContentSettingsObserver::Get(view_); |
| 153 | observer->SetContentSettings(settings); |
| 154 | ContentSettingsObserver::SetDefaultContentSettings(settings); |
| 155 | EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 156 | observer->GetContentSetting(CONTENT_SETTINGS_TYPE_PLUGINS)); |
| 157 | |
| 158 | // Temporarily allow plugins. |
[email protected] | 068970d | 2011-10-11 00:05:16 | [diff] [blame] | 159 | OnMessageReceived(ChromeViewMsg_LoadBlockedPlugins(MSG_ROUTING_NONE)); |
[email protected] | 134efc3 | 2011-08-02 18:14:21 | [diff] [blame] | 160 | EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 161 | observer->GetContentSetting(CONTENT_SETTINGS_TYPE_PLUGINS)); |
| 162 | |
| 163 | // Simulate a navigation within the page. |
[email protected] | 068970d | 2011-10-11 00:05:16 | [diff] [blame] | 164 | DidNavigateWithinPage(GetMainFrame(), true); |
[email protected] | 134efc3 | 2011-08-02 18:14:21 | [diff] [blame] | 165 | EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 166 | observer->GetContentSetting(CONTENT_SETTINGS_TYPE_PLUGINS)); |
| 167 | |
| 168 | // Navigate to a different page. |
| 169 | LoadHTML("<html>Bar</html>"); |
| 170 | EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 171 | observer->GetContentSetting(CONTENT_SETTINGS_TYPE_PLUGINS)); |
| 172 | } |