| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <string> | |
| 6 | |
| 7 #include "base/strings/string_util.h" | 5 #include "base/strings/string_util.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 6 #include "base/strings/utf_string_conversions.h" |
| 9 #include "chrome/browser/ui/browser.h" | 7 #include "chrome/browser/ui/browser.h" |
| 10 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 8 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 11 #include "chrome/test/base/in_process_browser_test.h" | 9 #include "chrome/test/base/in_process_browser_test.h" |
| 12 #include "chrome/test/base/ui_test_utils.h" | 10 #include "chrome/test/base/ui_test_utils.h" |
| 13 #include "content/public/test/browser_test_utils.h" | 11 #include "content/public/test/browser_test_utils.h" |
| 14 #include "net/base/test_data_directory.h" | 12 #include "net/base/test_data_directory.h" |
| 15 #include "net/test/spawned_test_server/spawned_test_server.h" | 13 #include "net/test/spawned_test_server/spawned_test_server.h" |
| 16 #include "url/gurl.h" | |
| 17 | 14 |
| 18 namespace { | 15 namespace { |
| 19 | 16 |
| 20 class WebSocketBrowserTest : public InProcessBrowserTest { | 17 class WebSocketBrowserTest : public InProcessBrowserTest { |
| 21 public: | 18 public: |
| 22 WebSocketBrowserTest() | 19 WebSocketBrowserTest() |
| 23 : ws_server_(net::SpawnedTestServer::TYPE_WS, | 20 : ws_server_(net::SpawnedTestServer::TYPE_WS, |
| 24 net::SpawnedTestServer::kLocalhost, | 21 net::SpawnedTestServer::kLocalhost, |
| 25 net::GetWebSocketTestDataDirectory()), | 22 net::GetWebSocketTestDataDirectory()), |
| 26 wss_server_(net::SpawnedTestServer::TYPE_WSS, | 23 wss_server_(net::SpawnedTestServer::TYPE_WSS, |
| 27 SSLOptions(SSLOptions::CERT_OK), | 24 SSLOptions(SSLOptions::CERT_OK), |
| 28 net::GetWebSocketTestDataDirectory()) {} | 25 net::GetWebSocketTestDataDirectory()) { |
| 26 } |
| 29 | 27 |
| 30 protected: | 28 protected: |
| 31 void NavigateToHTTP(const std::string& path) { | |
| 32 // Visit a HTTP page for testing. | |
| 33 std::string scheme("http"); | |
| 34 GURL::Replacements replacements; | |
| 35 replacements.SetSchemeStr(scheme); | |
| 36 ui_test_utils::NavigateToURL( | |
| 37 browser(), ws_server_.GetURL(path).ReplaceComponents(replacements)); | |
| 38 } | |
| 39 | |
| 40 void NavigateToHTTPS(const std::string& path) { | |
| 41 // Visit a HTTPS page for testing. | |
| 42 std::string scheme("https"); | |
| 43 GURL::Replacements replacements; | |
| 44 replacements.SetSchemeStr(scheme); | |
| 45 ui_test_utils::NavigateToURL( | |
| 46 browser(), wss_server_.GetURL(path).ReplaceComponents(replacements)); | |
| 47 } | |
| 48 | |
| 49 net::SpawnedTestServer ws_server_; | 29 net::SpawnedTestServer ws_server_; |
| 50 net::SpawnedTestServer wss_server_; | 30 net::SpawnedTestServer wss_server_; |
| 51 | 31 |
| 52 private: | 32 private: |
| 53 typedef net::SpawnedTestServer::SSLOptions SSLOptions; | 33 typedef net::SpawnedTestServer::SSLOptions SSLOptions; |
| 54 | 34 |
| 55 DISALLOW_COPY_AND_ASSIGN(WebSocketBrowserTest); | 35 DISALLOW_COPY_AND_ASSIGN(WebSocketBrowserTest); |
| 56 }; | 36 }; |
| 57 | 37 |
| 58 // Test that the browser can handle a WebSocket frame split into multiple TCP | 38 // Test that the browser can handle a WebSocket frame split into multiple TCP |
| 59 // segments. | 39 // segments. |
| 60 IN_PROC_BROWSER_TEST_F(WebSocketBrowserTest, WebSocketSplitSegments) { | 40 IN_PROC_BROWSER_TEST_F(WebSocketBrowserTest, WebSocketSplitSegments) { |
| 61 // Launch a WebSocket server. | 41 // Launch a WebSocket server. |
| 62 ASSERT_TRUE(ws_server_.Start()); | 42 ASSERT_TRUE(ws_server_.Start()); |
| 63 | 43 |
| 64 // Setup page title observer. | 44 // Setup page title observer. |
| 65 content::WebContents* tab = | 45 content::WebContents* tab = |
| 66 browser()->tab_strip_model()->GetActiveWebContents(); | 46 browser()->tab_strip_model()->GetActiveWebContents(); |
| 67 content::TitleWatcher watcher(tab, base::ASCIIToUTF16("PASS")); | 47 content::TitleWatcher watcher(tab, base::ASCIIToUTF16("PASS")); |
| 68 watcher.AlsoWaitForTitle(base::ASCIIToUTF16("FAIL")); | 48 watcher.AlsoWaitForTitle(base::ASCIIToUTF16("FAIL")); |
| 69 | 49 |
| 70 NavigateToHTTP("split_packet_check.html"); | 50 // Visit a HTTP page for testing. |
| 51 std::string scheme("http"); |
| 52 GURL::Replacements replacements; |
| 53 replacements.SetSchemeStr(scheme); |
| 54 ui_test_utils::NavigateToURL( |
| 55 browser(), |
| 56 ws_server_.GetURL( |
| 57 "split_packet_check.html").ReplaceComponents(replacements)); |
| 71 | 58 |
| 72 const base::string16 result = watcher.WaitAndGetTitle(); | 59 const base::string16 result = watcher.WaitAndGetTitle(); |
| 73 EXPECT_EQ(base::ASCIIToUTF16("PASS"), result); | 60 EXPECT_TRUE(EqualsASCII(result, "PASS")); |
| 74 } | 61 } |
| 75 | 62 |
| 76 IN_PROC_BROWSER_TEST_F(WebSocketBrowserTest, SecureWebSocketSplitRecords) { | 63 IN_PROC_BROWSER_TEST_F(WebSocketBrowserTest, SecureWebSocketSplitRecords) { |
| 77 // Launch a secure WebSocket server. | 64 // Launch a secure WebSocket server. |
| 78 ASSERT_TRUE(wss_server_.Start()); | 65 ASSERT_TRUE(wss_server_.Start()); |
| 79 | 66 |
| 80 // Setup page title observer. | 67 // Setup page title observer. |
| 81 content::WebContents* tab = | 68 content::WebContents* tab = |
| 82 browser()->tab_strip_model()->GetActiveWebContents(); | 69 browser()->tab_strip_model()->GetActiveWebContents(); |
| 83 content::TitleWatcher watcher(tab, base::ASCIIToUTF16("PASS")); | 70 content::TitleWatcher watcher(tab, base::ASCIIToUTF16("PASS")); |
| 84 watcher.AlsoWaitForTitle(base::ASCIIToUTF16("FAIL")); | 71 watcher.AlsoWaitForTitle(base::ASCIIToUTF16("FAIL")); |
| 85 | 72 |
| 86 NavigateToHTTPS("split_packet_check.html"); | 73 // Visit a HTTPS page for testing. |
| 74 std::string scheme("https"); |
| 75 GURL::Replacements replacements; |
| 76 replacements.SetSchemeStr(scheme); |
| 77 ui_test_utils::NavigateToURL( |
| 78 browser(), |
| 79 wss_server_.GetURL( |
| 80 "split_packet_check.html").ReplaceComponents(replacements)); |
| 87 | 81 |
| 88 const base::string16 result = watcher.WaitAndGetTitle(); | 82 const base::string16 result = watcher.WaitAndGetTitle(); |
| 89 EXPECT_EQ(base::ASCIIToUTF16("PASS"), result); | 83 EXPECT_TRUE(EqualsASCII(result, "PASS")); |
| 90 } | |
| 91 | |
| 92 IN_PROC_BROWSER_TEST_F(WebSocketBrowserTest, SendCloseFrameWhenTabIsClosed) { | |
| 93 // Launch a WebSocket server. | |
| 94 ASSERT_TRUE(ws_server_.Start()); | |
| 95 | |
| 96 content::WebContents* tab = | |
| 97 browser()->tab_strip_model()->GetActiveWebContents(); | |
| 98 { | |
| 99 // Create a new tab, establish a WebSocket connection and close the tab. | |
| 100 content::WebContents* new_tab = content::WebContents::Create( | |
| 101 content::WebContents::CreateParams(tab->GetBrowserContext())); | |
| 102 browser()->tab_strip_model()->AppendWebContents(new_tab, true); | |
| 103 ASSERT_EQ(new_tab, browser()->tab_strip_model()->GetWebContentsAt(1)); | |
| 104 | |
| 105 content::TitleWatcher connected_title_watcher( | |
| 106 new_tab, base::ASCIIToUTF16("CONNECTED")); | |
| 107 connected_title_watcher.AlsoWaitForTitle(base::ASCIIToUTF16("CLOSED")); | |
| 108 NavigateToHTTP("counted_connection.html"); | |
| 109 const base::string16 result = connected_title_watcher.WaitAndGetTitle(); | |
| 110 EXPECT_TRUE(EqualsASCII(result, "CONNECTED")); | |
| 111 | |
| 112 content::WebContentsDestroyedWatcher destroyed_watcher(new_tab); | |
| 113 browser()->tab_strip_model()->CloseWebContentsAt(1, 0); | |
| 114 destroyed_watcher.Wait(); | |
| 115 } | |
| 116 | |
| 117 content::TitleWatcher title_watcher(tab, base::ASCIIToUTF16("PASS")); | |
| 118 title_watcher.AlsoWaitForTitle(base::ASCIIToUTF16("FAIL")); | |
| 119 | |
| 120 NavigateToHTTP("count_connection.html"); | |
| 121 const base::string16 result = title_watcher.WaitAndGetTitle(); | |
| 122 EXPECT_EQ(base::ASCIIToUTF16("PASS"), result); | |
| 123 } | 84 } |
| 124 | 85 |
| 125 } // namespace | 86 } // namespace |
| OLD | NEW |