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

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: cleanup 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
« no previous file with comments | « no previous file | chrome/browser/ui/browser.h » ('j') | chrome/browser/ui/browser.h » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 Browser subclass 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 Browser {
1131 public:
1132 explicit ConsoleWebContentsDelegate(const Browser::CreateParams& params)
1133 : Browser(params) {}
1134 ~ConsoleWebContentsDelegate() override {}
1135
1136 const std::vector<base::string16>& console_messages() const {
1137 return console_messages_;
1138 }
1139
1140 void set_console_message_callback(const base::Closure& callback) {
1141 console_message_callback_ = callback;
1142 }
1143
1144 void ClearConsoleMessages() { console_messages_.clear(); }
1145
1146 // content::WebContentsDelegate
1147 bool AddMessageToConsole(content::WebContents* source,
1148 int32_t level,
1149 const base::string16& message,
1150 int32_t line_no,
1151 const base::string16& source_id) override {
1152 console_messages_.push_back(message);
1153 if (!console_message_callback_.is_null()) {
1154 console_message_callback_.Run();
1155 console_message_callback_.Reset();
1156 }
1157 return true;
1158 }
1159
1160 private:
1161 std::vector<base::string16> console_messages_;
1162 base::Closure console_message_callback_;
1163
1164 DISALLOW_COPY_AND_ASSIGN(ConsoleWebContentsDelegate);
1165 };
1166
1167 // Checks that |delegate| has observed exactly one console message for
1168 // HTTP_SHOW_WARNING. This does not check for the exact string (for fear
1169 // of being too brittle) but rather just a keyword ("not secure").
1170 void CheckForOneHttpWarningConsoleMessage(
1171 ConsoleWebContentsDelegate* delegate) {
1172 const std::vector<base::string16>& messages = delegate->console_messages();
1173 ASSERT_EQ(1u, messages.size());
1174 EXPECT_NE(base::string16::npos,
1175 messages[0].find(base::ASCIIToUTF16("not secure")));
1176 }
1177
1178 // Tests that console messages are printed upon a call to
1179 // GetSecurityInfo() on an HTTP_SHOW_WARNING page, exactly once per
1180 // main-frame navigation.
elawrence 2016/10/14 15:12:27 Do we need a test for "navigation-like" operations
estark 2016/10/14 17:57:06 Done. I think (?) we can distinguish with Navigati
1181 IN_PROC_BROWSER_TEST_F(ChromeSecurityStateModelClientTestWithPasswordCcSwitch,
1182 ConsoleMessage) {
1183 ConsoleWebContentsDelegate* delegate = new ConsoleWebContentsDelegate(
1184 Browser::CreateParams(browser()->profile()));
1185 content::WebContents* original_contents =
1186 browser()->tab_strip_model()->GetActiveWebContents();
1187 content::WebContents* contents =
1188 content::WebContents::Create(content::WebContents::CreateParams(
1189 original_contents->GetBrowserContext()));
1190 ASSERT_TRUE(contents);
1191 contents->SetDelegate(delegate);
1192 delegate->tab_strip_model()->AppendWebContents(contents, true);
1193 int index = delegate->tab_strip_model()->GetIndexOfWebContents(contents);
1194 delegate->tab_strip_model()->ActivateTabAt(index, true);
1195 ASSERT_EQ(contents, delegate->tab_strip_model()->GetActiveWebContents());
1196
1197 // Navigate to an HTTP page. Use a non-local hostname so that is it
1198 // not considered secure.
1199 GURL http_url =
1200 GetURLWithNonLocalHostname(embedded_test_server(), "/title1.html");
1201 ui_test_utils::NavigateToURL(delegate, http_url);
1202 content::NavigationEntry* entry = contents->GetController().GetVisibleEntry();
1203 ASSERT_TRUE(entry);
1204 EXPECT_EQ(http_url, entry->GetURL());
1205 EXPECT_TRUE(delegate->console_messages().empty());
1206
1207 // Trigger the HTTP_SHOW_WARNING state.
1208 base::RunLoop first_message;
1209 delegate->set_console_message_callback(first_message.QuitClosure());
1210 contents->OnPasswordInputShownOnHttp();
1211 first_message.Run();
1212
1213 // Check that the HTTP_SHOW_WARNING state was actually triggered.
1214 ChromeSecurityStateModelClient* client =
1215 ChromeSecurityStateModelClient::FromWebContents(contents);
1216 ASSERT_TRUE(client);
1217 security_state::SecurityStateModel::SecurityInfo security_info;
1218 client->GetSecurityInfo(&security_info);
1219 EXPECT_EQ(security_state::SecurityStateModel::HTTP_SHOW_WARNING,
1220 security_info.security_level);
1221
1222 // Check that the expected console message is present.
1223 ASSERT_NO_FATAL_FAILURE(CheckForOneHttpWarningConsoleMessage(delegate));
1224 delegate->ClearConsoleMessages();
1225
1226 // Two subsequent triggers of VisibleSSLStateChanged -- one on the
1227 // same navigation and one on another navigation -- should only result
1228 // in one additional console message.
1229 contents->OnCreditCardInputShownOnHttp();
1230 GURL second_http_url =
1231 GetURLWithNonLocalHostname(embedded_test_server(), "/title2.html");
1232 ui_test_utils::NavigateToURL(delegate, second_http_url);
1233 entry = contents->GetController().GetVisibleEntry();
1234 ASSERT_TRUE(entry);
1235 EXPECT_EQ(second_http_url, entry->GetURL());
1236
1237 base::RunLoop second_message;
1238 delegate->set_console_message_callback(second_message.QuitClosure());
1239 contents->OnPasswordInputShownOnHttp();
1240 second_message.Run();
1241
1242 client->GetSecurityInfo(&security_info);
1243 EXPECT_EQ(security_state::SecurityStateModel::HTTP_SHOW_WARNING,
1244 security_info.security_level);
1245
1246 ASSERT_NO_FATAL_FAILURE(CheckForOneHttpWarningConsoleMessage(delegate));
1247 }
1248
1249 // Tests that new HTTP_SHOW_WARNING console messages are not printed for
1250 // subframe navigations.
elawrence 2016/10/14 15:12:27 I think this means "Test that additional HTTP_SHOW
estark 2016/10/14 17:57:06 I clarified the comment, but I don't think it's ne
1251 IN_PROC_BROWSER_TEST_F(ChromeSecurityStateModelClientTestWithPasswordCcSwitch,
1252 ConsoleMessageNotPrintedForFrameNavigation) {
1253 ConsoleWebContentsDelegate* delegate = new ConsoleWebContentsDelegate(
1254 Browser::CreateParams(browser()->profile()));
1255 content::WebContents* original_contents =
1256 browser()->tab_strip_model()->GetActiveWebContents();
1257 content::WebContents* contents =
1258 content::WebContents::Create(content::WebContents::CreateParams(
1259 original_contents->GetBrowserContext()));
1260 ASSERT_TRUE(contents);
1261 contents->SetDelegate(delegate);
1262 delegate->tab_strip_model()->AppendWebContents(contents, true);
1263 int index = delegate->tab_strip_model()->GetIndexOfWebContents(contents);
1264 delegate->tab_strip_model()->ActivateTabAt(index, true);
1265 ASSERT_EQ(contents, delegate->tab_strip_model()->GetActiveWebContents());
1266
1267 // Navigate to an HTTP page. Use a non-local hostname so that is it
1268 // not considered secure.
1269 GURL http_url = GetURLWithNonLocalHostname(embedded_test_server(),
1270 "/ssl/page_with_frame.html");
1271 ui_test_utils::NavigateToURL(delegate, http_url);
1272 content::NavigationEntry* entry = contents->GetController().GetVisibleEntry();
1273 ASSERT_TRUE(entry);
1274 EXPECT_EQ(http_url, entry->GetURL());
1275 EXPECT_TRUE(delegate->console_messages().empty());
1276
1277 // Trigger the HTTP_SHOW_WARNING state.
1278 base::RunLoop first_message;
1279 delegate->set_console_message_callback(first_message.QuitClosure());
1280 contents->OnPasswordInputShownOnHttp();
1281 first_message.Run();
1282
1283 // Check that the HTTP_SHOW_WARNING state was actually triggered.
1284 ChromeSecurityStateModelClient* client =
1285 ChromeSecurityStateModelClient::FromWebContents(contents);
1286 ASSERT_TRUE(client);
1287 security_state::SecurityStateModel::SecurityInfo security_info;
1288 client->GetSecurityInfo(&security_info);
1289 EXPECT_EQ(security_state::SecurityStateModel::HTTP_SHOW_WARNING,
1290 security_info.security_level);
1291
1292 // Check that the expected console message is present.
1293 ASSERT_NO_FATAL_FAILURE(CheckForOneHttpWarningConsoleMessage(delegate));
1294 delegate->ClearConsoleMessages();
1295
1296 // Navigate the subframe and trigger VisibleSSLStateChanged
1297 // again. While the security level is still HTTP_SHOW_WARNING, an
1298 // additional console message should not be logged because there was
1299 // already a console message logged for the current main-frame
1300 // navigation.
1301 content::WindowedNotificationObserver subframe_observer(
1302 content::NOTIFICATION_LOAD_STOP,
1303 content::Source<content::NavigationController>(
1304 &contents->GetController()));
1305 EXPECT_TRUE(content::ExecuteScript(
1306 contents, "document.getElementById('navFrame').src = '/title2.html';"));
1307 subframe_observer.Wait();
1308 contents->OnCreditCardInputShownOnHttp();
1309 EXPECT_EQ(security_state::SecurityStateModel::HTTP_SHOW_WARNING,
1310 security_info.security_level);
1311
1312 // Do a main frame navigation and then trigger HTTP_SHOW_WARNING
1313 // again. From the above subframe navigation and this main-frame
1314 // navigation, exactly one console message is expected.
1315 GURL second_http_url =
1316 GetURLWithNonLocalHostname(embedded_test_server(), "/title2.html");
1317 ui_test_utils::NavigateToURL(delegate, second_http_url);
1318 entry = contents->GetController().GetVisibleEntry();
1319 ASSERT_TRUE(entry);
1320 EXPECT_EQ(second_http_url, entry->GetURL());
1321
1322 base::RunLoop second_message;
1323 delegate->set_console_message_callback(second_message.QuitClosure());
1324 contents->OnPasswordInputShownOnHttp();
1325 second_message.Run();
1326
1327 client->GetSecurityInfo(&security_info);
1328 EXPECT_EQ(security_state::SecurityStateModel::HTTP_SHOW_WARNING,
1329 security_info.security_level);
1330 ASSERT_NO_FATAL_FAILURE(CheckForOneHttpWarningConsoleMessage(delegate));
1331 }
1332
1125 // Tests that the SecurityStateModel for a WebContents is up to date 1333 // Tests that the SecurityStateModel for a WebContents is up to date
1126 // when the WebContents is inserted into a Browser's TabStripModel. 1334 // when the WebContents is inserted into a Browser's TabStripModel.
1127 IN_PROC_BROWSER_TEST_F(ChromeSecurityStateModelClientTest, AddedTab) { 1335 IN_PROC_BROWSER_TEST_F(ChromeSecurityStateModelClientTest, AddedTab) {
1128 ASSERT_TRUE(https_server_.Start()); 1336 ASSERT_TRUE(https_server_.Start());
1129 SetUpMockCertVerifierForHttpsServer(0, net::OK); 1337 SetUpMockCertVerifierForHttpsServer(0, net::OK);
1130 1338
1131 content::WebContents* tab = 1339 content::WebContents* tab =
1132 browser()->tab_strip_model()->GetActiveWebContents(); 1340 browser()->tab_strip_model()->GetActiveWebContents();
1133 ASSERT_TRUE(tab); 1341 ASSERT_TRUE(tab);
1134 1342
(...skipping 537 matching lines...) Expand 10 before | Expand all | Expand 10 after
1672 ChromeSecurityStateModelClient* model_client = 1880 ChromeSecurityStateModelClient* model_client =
1673 ChromeSecurityStateModelClient::FromWebContents(web_contents); 1881 ChromeSecurityStateModelClient::FromWebContents(web_contents);
1674 ASSERT_TRUE(model_client); 1882 ASSERT_TRUE(model_client);
1675 SecurityStateModel::SecurityInfo security_info; 1883 SecurityStateModel::SecurityInfo security_info;
1676 model_client->GetSecurityInfo(&security_info); 1884 model_client->GetSecurityInfo(&security_info);
1677 EXPECT_EQ(SecurityStateModel::SECURE, security_info.security_level); 1885 EXPECT_EQ(SecurityStateModel::SECURE, security_info.security_level);
1678 EXPECT_EQ(kTestSCTStatuses, security_info.sct_verify_statuses); 1886 EXPECT_EQ(kTestSCTStatuses, security_info.sct_verify_statuses);
1679 } 1887 }
1680 1888
1681 } // namespace 1889 } // namespace
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/ui/browser.h » ('j') | chrome/browser/ui/browser.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698