Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(67)

Side by Side Diff: chrome/browser/ssl/chrome_security_state_model_client_browser_tests.cc

Issue 2410043003: Add a console messsage for HTTP-bad (Closed)
Patch Set: add test for subframe navigation Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/ssl/chrome_security_state_model_client.h" 5 #include "chrome/browser/ssl/chrome_security_state_model_client.h"
6 6
7 #include <openssl/ssl.h> 7 #include <openssl/ssl.h>
8 8
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/files/file_path.h" 10 #include "base/files/file_path.h"
(...skipping 1104 matching lines...) Expand 10 before | Expand all | Expand 10 after
1115 security_info.security_level); 1115 security_info.security_level);
1116 1116
1117 // The SSLStatus flags should only be set if the top-level page load was HTTP, 1117 // The SSLStatus flags should only be set if the top-level page load was HTTP,
1118 // which it was not in this case. 1118 // which it was not in this case.
1119 content::NavigationEntry* entry = contents->GetController().GetVisibleEntry(); 1119 content::NavigationEntry* entry = contents->GetController().GetVisibleEntry();
1120 ASSERT_TRUE(entry); 1120 ASSERT_TRUE(entry);
1121 EXPECT_FALSE(entry->GetSSL().content_status & 1121 EXPECT_FALSE(entry->GetSSL().content_status &
1122 content::SSLStatus::DISPLAYED_PASSWORD_FIELD_ON_HTTP); 1122 content::SSLStatus::DISPLAYED_PASSWORD_FIELD_ON_HTTP);
1123 } 1123 }
1124 1124
1125 // A WebContentsDelegate that keeps track of messages that have been
1126 // added to the console. Messages can be retrieved or cleared with
1127 // console_messages() and ClearConsoleMessages(). The user of this class
1128 // can set a callback to run when the next console message notification
1129 // arrives.
1130 class ConsoleWebContentsDelegate : public content::WebContentsDelegate {
1131 public:
1132 ConsoleWebContentsDelegate() : content::WebContentsDelegate() {}
1133 ~ConsoleWebContentsDelegate() override {}
1134
1135 const std::vector<base::string16>& console_messages() const {
1136 return console_messages_;
1137 }
1138
1139 void set_console_message_callback(const base::Closure& callback) {
1140 console_message_callback_ = callback;
1141 }
1142
1143 void ClearConsoleMessages() { console_messages_.clear(); }
1144
1145 // content::WebContentsDelegate
1146 bool AddMessageToConsole(content::WebContents* source,
1147 int32_t level,
1148 const base::string16& message,
1149 int32_t line_no,
1150 const base::string16& source_id) override {
1151 console_messages_.push_back(message);
1152 if (!console_message_callback_.is_null()) {
1153 console_message_callback_.Run();
1154 console_message_callback_.Reset();
1155 }
1156 return true;
1157 }
1158
1159 private:
1160 std::vector<base::string16> console_messages_;
1161 base::Closure console_message_callback_;
1162
1163 DISALLOW_COPY_AND_ASSIGN(ConsoleWebContentsDelegate);
1164 };
1165
1166 // Checks that |delegate| has observed exactly one console message for
1167 // HTTP_SHOW_WARNING. This does not check for the exact string (for fear
1168 // of being too brittle) but rather just a keyword ("not secure").
1169 void CheckForOneHttpWarningConsoleMessage(
1170 const ConsoleWebContentsDelegate& delegate) {
1171 const std::vector<base::string16>& messages = delegate.console_messages();
1172 ASSERT_EQ(1u, messages.size());
1173 EXPECT_NE(base::string16::npos,
1174 messages[0].find(base::ASCIIToUTF16("not secure")));
1175 }
1176
1177 // Tests that console messages are printed upon a call to
1178 // GetSecurityInfo() on an HTTP_SHOW_WARNING page, exactly once per
1179 // main-frame navigation.
1180 IN_PROC_BROWSER_TEST_F(ChromeSecurityStateModelClientTestWithPasswordCcSwitch,
1181 ConsoleMessage) {
1182 ConsoleWebContentsDelegate delegate;
1183 content::WebContents* contents =
1184 browser()->tab_strip_model()->GetActiveWebContents();
1185 ASSERT_TRUE(contents);
1186 contents->SetDelegate(&delegate);
1187
1188 // Navigate to an HTTP page. Use a non-local hostname so that is it
1189 // not considered secure.
1190 GURL http_url =
1191 GetURLWithNonLocalHostname(embedded_test_server(), "/title1.html");
1192 ui_test_utils::NavigateToURL(browser(), http_url);
1193 content::NavigationEntry* entry = contents->GetController().GetVisibleEntry();
1194 ASSERT_TRUE(entry);
1195 EXPECT_EQ(http_url, entry->GetURL());
1196 EXPECT_TRUE(delegate.console_messages().empty());
1197
1198 // Trigger the HTTP_SHOW_WARNING state.
1199 base::RunLoop first_message;
1200 contents->OnPasswordInputShownOnHttp();
1201 ChromeSecurityStateModelClient* client =
1202 ChromeSecurityStateModelClient::FromWebContents(contents);
1203 ASSERT_TRUE(client);
1204 security_state::SecurityStateModel::SecurityInfo security_info;
1205
1206 // After calling GetSecurityInfo(), wait for a console message.
1207 delegate.set_console_message_callback(first_message.QuitClosure());
1208 client->GetSecurityInfo(&security_info);
1209 first_message.Run();
1210 EXPECT_EQ(security_state::SecurityStateModel::HTTP_SHOW_WARNING,
1211 security_info.security_level);
1212
1213 // Check that the expected console message is present.
1214 ASSERT_NO_FATAL_FAILURE(CheckForOneHttpWarningConsoleMessage(delegate));
1215 delegate.ClearConsoleMessages();
1216
1217 // Two subsequent calls to GetSecurityInfo() -- one on the same
1218 // navigation and one on another navigation -- should only result in
1219 // one additional console message.
estark 2016/10/12 22:57:09 Note: these tests only sort of make sense. We real
1220 client->GetSecurityInfo(&security_info);
1221 GURL second_http_url =
1222 GetURLWithNonLocalHostname(embedded_test_server(), "/title2.html");
1223 ui_test_utils::NavigateToURL(browser(), second_http_url);
1224 entry = contents->GetController().GetVisibleEntry();
1225 ASSERT_TRUE(entry);
1226 EXPECT_EQ(second_http_url, entry->GetURL());
1227 client->GetSecurityInfo(&security_info);
1228 EXPECT_EQ(security_state::SecurityStateModel::NONE,
1229 security_info.security_level);
1230 contents->OnPasswordInputShownOnHttp();
1231 base::RunLoop second_message;
1232 delegate.set_console_message_callback(second_message.QuitClosure());
1233 client->GetSecurityInfo(&security_info);
1234 second_message.Run();
1235 ASSERT_NO_FATAL_FAILURE(CheckForOneHttpWarningConsoleMessage(delegate));
1236 }
1237
1238 // Tests that new HTTP_SHOW_WARNING console messages are not printed for
1239 // subframe navigations.
1240 IN_PROC_BROWSER_TEST_F(ChromeSecurityStateModelClientTestWithPasswordCcSwitch,
1241 ConsoleMessageNotPrintedForFrameNavigation) {
1242 ConsoleWebContentsDelegate delegate;
1243 content::WebContents* contents =
1244 browser()->tab_strip_model()->GetActiveWebContents();
1245 ASSERT_TRUE(contents);
1246 contents->SetDelegate(&delegate);
1247
1248 // Navigate to an HTTP page. Use a non-local hostname so that is it
1249 // not considered secure.
1250 GURL http_url = GetURLWithNonLocalHostname(embedded_test_server(),
1251 "/ssl/page_with_frame.html");
1252 ui_test_utils::NavigateToURL(browser(), http_url);
1253 content::NavigationEntry* entry = contents->GetController().GetVisibleEntry();
1254 ASSERT_TRUE(entry);
1255 EXPECT_EQ(http_url, entry->GetURL());
1256 EXPECT_TRUE(delegate.console_messages().empty());
1257
1258 // Trigger the HTTP_SHOW_WARNING state.
1259 base::RunLoop first_message;
1260 contents->OnPasswordInputShownOnHttp();
1261 ChromeSecurityStateModelClient* client =
1262 ChromeSecurityStateModelClient::FromWebContents(contents);
1263 ASSERT_TRUE(client);
1264 security_state::SecurityStateModel::SecurityInfo security_info;
1265
1266 // After calling GetSecurityInfo(), wait for a console message.
1267 delegate.set_console_message_callback(first_message.QuitClosure());
1268 client->GetSecurityInfo(&security_info);
1269 first_message.Run();
1270 EXPECT_EQ(security_state::SecurityStateModel::HTTP_SHOW_WARNING,
1271 security_info.security_level);
1272
1273 // Check that the expected console message is present.
1274 ASSERT_NO_FATAL_FAILURE(CheckForOneHttpWarningConsoleMessage(delegate));
1275 delegate.ClearConsoleMessages();
1276
1277 // Navigate the subframe and call GetSecurityInfo(). While the
1278 // security level is still HTTP_SHOW_WARNING, an additional console
1279 // message should not be logged because there was already a console
1280 // message logged for the current main-frame navigation.
1281 content::WindowedNotificationObserver subframe_observer(
1282 content::NOTIFICATION_LOAD_STOP,
1283 content::Source<content::NavigationController>(
1284 &contents->GetController()));
1285 EXPECT_TRUE(content::ExecuteScript(
1286 contents, "document.getElementById('navFrame').src = '/title2.html';"));
1287 subframe_observer.Wait();
1288 client->GetSecurityInfo(&security_info);
1289 EXPECT_EQ(security_state::SecurityStateModel::HTTP_SHOW_WARNING,
1290 security_info.security_level);
1291
1292 // Do a main frame navigation and then call GetSecurityInfo()
1293 // again. From the above subframe navigation and this main-frame
1294 // navigation, exactly one console message is expected.
1295 GURL second_http_url =
1296 GetURLWithNonLocalHostname(embedded_test_server(), "/title2.html");
1297 ui_test_utils::NavigateToURL(browser(), second_http_url);
1298 entry = contents->GetController().GetVisibleEntry();
1299 ASSERT_TRUE(entry);
1300 EXPECT_EQ(second_http_url, entry->GetURL());
1301 client->GetSecurityInfo(&security_info);
1302 EXPECT_EQ(security_state::SecurityStateModel::NONE,
1303 security_info.security_level);
1304
1305 contents->OnPasswordInputShownOnHttp();
1306 base::RunLoop second_message;
1307 delegate.set_console_message_callback(second_message.QuitClosure());
1308 client->GetSecurityInfo(&security_info);
1309 second_message.Run();
1310 ASSERT_NO_FATAL_FAILURE(CheckForOneHttpWarningConsoleMessage(delegate));
1311 }
1312
1125 // Tests that the SecurityStateModel for a WebContents is up to date 1313 // Tests that the SecurityStateModel for a WebContents is up to date
1126 // when the WebContents is inserted into a Browser's TabStripModel. 1314 // when the WebContents is inserted into a Browser's TabStripModel.
1127 IN_PROC_BROWSER_TEST_F(ChromeSecurityStateModelClientTest, AddedTab) { 1315 IN_PROC_BROWSER_TEST_F(ChromeSecurityStateModelClientTest, AddedTab) {
1128 ASSERT_TRUE(https_server_.Start()); 1316 ASSERT_TRUE(https_server_.Start());
1129 SetUpMockCertVerifierForHttpsServer(0, net::OK); 1317 SetUpMockCertVerifierForHttpsServer(0, net::OK);
1130 1318
1131 content::WebContents* tab = 1319 content::WebContents* tab =
1132 browser()->tab_strip_model()->GetActiveWebContents(); 1320 browser()->tab_strip_model()->GetActiveWebContents();
1133 ASSERT_TRUE(tab); 1321 ASSERT_TRUE(tab);
1134 1322
(...skipping 537 matching lines...) Expand 10 before | Expand all | Expand 10 after
1672 ChromeSecurityStateModelClient* model_client = 1860 ChromeSecurityStateModelClient* model_client =
1673 ChromeSecurityStateModelClient::FromWebContents(web_contents); 1861 ChromeSecurityStateModelClient::FromWebContents(web_contents);
1674 ASSERT_TRUE(model_client); 1862 ASSERT_TRUE(model_client);
1675 SecurityStateModel::SecurityInfo security_info; 1863 SecurityStateModel::SecurityInfo security_info;
1676 model_client->GetSecurityInfo(&security_info); 1864 model_client->GetSecurityInfo(&security_info);
1677 EXPECT_EQ(SecurityStateModel::SECURE, security_info.security_level); 1865 EXPECT_EQ(SecurityStateModel::SECURE, security_info.security_level);
1678 EXPECT_EQ(kTestSCTStatuses, security_info.sct_verify_statuses); 1866 EXPECT_EQ(kTestSCTStatuses, security_info.sct_verify_statuses);
1679 } 1867 }
1680 1868
1681 } // namespace 1869 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698