OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/test/nacl/pnacl_header_test.h" | 5 #include "chrome/test/nacl/pnacl_header_test.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/path_service.h" | 8 #include "base/path_service.h" |
9 #include "base/test/scoped_path_override.h" | 9 #include "base/test/scoped_path_override.h" |
10 #include "chrome/browser/ui/browser.h" | 10 #include "chrome/browser/ui/browser.h" |
11 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 11 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
12 #include "chrome/common/chrome_paths.h" | 12 #include "chrome/common/chrome_paths.h" |
13 #include "chrome/test/base/ui_test_utils.h" | 13 #include "chrome/test/base/ui_test_utils.h" |
14 #include "chrome/test/nacl/nacl_browsertest_util.h" | 14 #include "chrome/test/nacl/nacl_browsertest_util.h" |
| 15 #include "content/public/browser/resource_dispatcher_host.h" |
15 #include "content/public/browser/web_contents.h" | 16 #include "content/public/browser/web_contents.h" |
16 #include "net/test/embedded_test_server/embedded_test_server.h" | 17 #include "net/test/embedded_test_server/embedded_test_server.h" |
17 #include "net/test/embedded_test_server/http_request.h" | 18 #include "net/test/embedded_test_server/http_request.h" |
18 #include "net/test/embedded_test_server/http_response.h" | 19 #include "net/test/embedded_test_server/http_response.h" |
| 20 #include "net/url_request/url_request.h" |
19 | 21 |
20 using net::test_server::BasicHttpResponse; | 22 using net::test_server::BasicHttpResponse; |
21 using net::test_server::EmbeddedTestServer; | 23 using net::test_server::EmbeddedTestServer; |
22 using net::test_server::HttpRequest; | 24 using net::test_server::HttpRequest; |
23 using net::test_server::HttpResponse; | 25 using net::test_server::HttpResponse; |
24 | 26 |
| 27 void TestDispatcherHostDelegate::RequestBeginning( |
| 28 net::URLRequest* request, |
| 29 content::ResourceContext* resource_context, |
| 30 content::AppCacheService* appcache_service, |
| 31 content::ResourceType resource_type, |
| 32 ScopedVector<content::ResourceThrottle>* throttles) { |
| 33 // This checks the same condition as the one for PNaCl in |
| 34 // AppendComponentUpdaterThrottles. |
| 35 if (resource_type == content::RESOURCE_TYPE_OBJECT) { |
| 36 const net::HttpRequestHeaders& headers = request->extra_request_headers(); |
| 37 std::string accept_headers; |
| 38 if (headers.GetHeader("Accept", &accept_headers)) { |
| 39 if (accept_headers.find("application/x-pnacl") != std::string::npos) |
| 40 found_pnacl_header_ = true; |
| 41 } |
| 42 } |
| 43 } |
| 44 |
25 PnaclHeaderTest::PnaclHeaderTest() : noncors_loads_(0), cors_loads_(0) {} | 45 PnaclHeaderTest::PnaclHeaderTest() : noncors_loads_(0), cors_loads_(0) {} |
26 | 46 |
27 PnaclHeaderTest::~PnaclHeaderTest() {} | 47 PnaclHeaderTest::~PnaclHeaderTest() {} |
28 | 48 |
29 void PnaclHeaderTest::StartServer() { | 49 void PnaclHeaderTest::StartServer() { |
30 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | 50 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
31 | 51 |
32 // For most requests, just serve files, but register a special test handler | 52 // For most requests, just serve files, but register a special test handler |
33 // that watches for the .pexe fetch also. | 53 // that watches for the .pexe fetch also. |
34 base::FilePath test_data_dir; | 54 base::FilePath test_data_dir; |
35 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir)); | 55 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir)); |
36 embedded_test_server()->RegisterRequestHandler( | 56 embedded_test_server()->RegisterRequestHandler( |
37 base::Bind(&PnaclHeaderTest::WatchForPexeFetch, base::Unretained(this))); | 57 base::Bind(&PnaclHeaderTest::WatchForPexeFetch, base::Unretained(this))); |
38 embedded_test_server()->ServeFilesFromDirectory(test_data_dir); | 58 embedded_test_server()->ServeFilesFromDirectory(test_data_dir); |
39 } | 59 } |
40 | 60 |
41 void PnaclHeaderTest::RunLoadTest(const std::string& url, | 61 void PnaclHeaderTest::RunLoadTest(const std::string& url, |
42 int expected_noncors, | 62 int expected_noncors, |
43 int expected_cors) { | 63 int expected_cors) { |
| 64 content::ResourceDispatcherHost::Get()->SetDelegate(&test_delegate_); |
44 StartServer(); | 65 StartServer(); |
45 LoadTestMessageHandler handler; | 66 LoadTestMessageHandler handler; |
46 content::JavascriptTestObserver observer( | 67 content::JavascriptTestObserver observer( |
47 browser()->tab_strip_model()->GetActiveWebContents(), | 68 browser()->tab_strip_model()->GetActiveWebContents(), |
48 &handler); | 69 &handler); |
49 | 70 |
50 // Make sure this is able to do a pexe fetch, even without access | 71 // Make sure this is able to do a pexe fetch, even without access |
51 // to the PNaCl component files (make DIR_PNACL_COMPONENT empty). | 72 // to the PNaCl component files (make DIR_PNACL_COMPONENT empty). |
52 // The pexe fetch that is done with special headers must be able to | 73 // The pexe fetch that is done with special headers must be able to |
53 // start before the component files are on disk. This is because it | 74 // start before the component files are on disk. This is because it |
54 // is the pexe fetch that helps trigger an on-demand installation | 75 // is the pexe fetch that helps trigger an on-demand installation |
55 // which installs the files to disk (if that hasn't already happened | 76 // which installs the files to disk (if that hasn't already happened |
56 // in the background). | 77 // in the background). |
57 base::ScopedPathOverride component_dir(chrome::DIR_PNACL_COMPONENT); | 78 base::ScopedPathOverride component_dir(chrome::DIR_PNACL_COMPONENT); |
58 | 79 |
59 ui_test_utils::NavigateToURL(browser(), embedded_test_server()->GetURL(url)); | 80 ui_test_utils::NavigateToURL(browser(), embedded_test_server()->GetURL(url)); |
| 81 |
| 82 content::ResourceDispatcherHost::Get()->SetDelegate(NULL); |
| 83 |
60 // Wait until the NMF and pexe are also loaded, not just the HTML. | 84 // Wait until the NMF and pexe are also loaded, not just the HTML. |
61 // Do this by waiting till the LoadTestMessageHandler responds. | 85 // Do this by waiting till the LoadTestMessageHandler responds. |
62 EXPECT_TRUE(observer.Run()) << handler.error_message(); | 86 EXPECT_TRUE(observer.Run()) << handler.error_message(); |
63 EXPECT_TRUE(handler.test_passed()) << "Test failed."; | 87 EXPECT_TRUE(handler.test_passed()) << "Test failed."; |
64 EXPECT_EQ(expected_noncors, noncors_loads_); | 88 EXPECT_EQ(expected_noncors, noncors_loads_); |
65 EXPECT_EQ(expected_cors, cors_loads_); | 89 EXPECT_EQ(expected_cors, cors_loads_); |
66 } | 90 } |
67 | 91 |
68 scoped_ptr<HttpResponse> PnaclHeaderTest::WatchForPexeFetch( | 92 scoped_ptr<HttpResponse> PnaclHeaderTest::WatchForPexeFetch( |
69 const HttpRequest& request) { | 93 const HttpRequest& request) { |
70 // Avoid favicon.ico warning by giving it a dummy icon. | 94 // Avoid favicon.ico warning by giving it a dummy icon. |
71 if (request.relative_url.find("favicon.ico") != std::string::npos) { | 95 if (request.relative_url.find("favicon.ico") != std::string::npos) { |
72 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse()); | 96 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse()); |
73 http_response->set_code(net::HTTP_OK); | 97 http_response->set_code(net::HTTP_OK); |
74 http_response->set_content(""); | 98 http_response->set_content(""); |
75 http_response->set_content_type("application/octet-stream"); | 99 http_response->set_content_type("application/octet-stream"); |
76 return http_response.PassAs<HttpResponse>(); | 100 return http_response.PassAs<HttpResponse>(); |
77 } | 101 } |
78 | 102 |
79 // Skip other non-pexe files and let ServeFilesFromDirectory handle it. | 103 // Skip other non-pexe files and let ServeFilesFromDirectory handle it. |
80 GURL absolute_url = embedded_test_server()->GetURL(request.relative_url); | 104 GURL absolute_url = embedded_test_server()->GetURL(request.relative_url); |
81 if (absolute_url.path().find(".pexe") == std::string::npos) | 105 if (absolute_url.path().find(".pexe") == std::string::npos) |
82 return scoped_ptr<HttpResponse>(); | 106 return scoped_ptr<HttpResponse>(); |
83 | 107 |
84 // For pexe files, check for the special Accept header. | 108 // For pexe files, check for the special Accept header, |
85 // This must match whatever is in: | 109 // along with the expected ResourceType of the URL request. |
86 // ppapi/native_client/src/trusted/plugin/pnacl_coordinator.cc | |
87 EXPECT_NE(0U, request.headers.count("Accept")); | 110 EXPECT_NE(0U, request.headers.count("Accept")); |
88 std::map<std::string, std::string>::const_iterator it = | 111 std::map<std::string, std::string>::const_iterator it = |
89 request.headers.find("Accept"); | 112 request.headers.find("Accept"); |
90 EXPECT_NE(std::string::npos, it->second.find("application/x-pnacl")); | 113 EXPECT_NE(std::string::npos, it->second.find("application/x-pnacl")); |
91 EXPECT_NE(std::string::npos, it->second.find("*/*")); | 114 EXPECT_NE(std::string::npos, it->second.find("*/*")); |
| 115 EXPECT_TRUE(test_delegate_.found_pnacl_header()); |
92 | 116 |
93 // Also make sure that other headers like CORS-related headers | 117 // Also make sure that other headers like CORS-related headers |
94 // are preserved when injecting the special Accept header. | 118 // are preserved when injecting the special Accept header. |
95 if (absolute_url.path().find("cors") == std::string::npos) { | 119 if (absolute_url.path().find("cors") == std::string::npos) { |
96 EXPECT_EQ(0U, request.headers.count("Origin")); | 120 EXPECT_EQ(0U, request.headers.count("Origin")); |
97 noncors_loads_ += 1; | 121 noncors_loads_ += 1; |
98 } else { | 122 } else { |
99 EXPECT_EQ(1U, request.headers.count("Origin")); | 123 EXPECT_EQ(1U, request.headers.count("Origin")); |
100 cors_loads_ += 1; | 124 cors_loads_ += 1; |
101 } | 125 } |
102 | 126 |
103 // After checking the header, just return a 404. We don't need to actually | 127 // After checking the header, just return a 404. We don't need to actually |
104 // compile and stopping with a 404 is faster. | 128 // compile and stopping with a 404 is faster. |
105 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse()); | 129 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse()); |
106 http_response->set_code(net::HTTP_NOT_FOUND); | 130 http_response->set_code(net::HTTP_NOT_FOUND); |
107 http_response->set_content("PEXE ... not found"); | 131 http_response->set_content("PEXE ... not found"); |
108 http_response->set_content_type("application/octet-stream"); | 132 http_response->set_content_type("application/octet-stream"); |
109 return http_response.PassAs<HttpResponse>(); | 133 return http_response.PassAs<HttpResponse>(); |
110 } | 134 } |
111 | 135 |
112 IN_PROC_BROWSER_TEST_F(PnaclHeaderTest, TestHasPnaclHeader) { | 136 IN_PROC_BROWSER_TEST_F(PnaclHeaderTest, TestHasPnaclHeader) { |
113 // Load 2 pexes, one same origin and one cross orgin. | 137 // Load 2 pexes, one same origin and one cross orgin. |
114 RunLoadTest("/nacl/pnacl_request_header/pnacl_request_header.html", 1, 1); | 138 RunLoadTest("/nacl/pnacl_request_header/pnacl_request_header.html", 1, 1); |
115 } | 139 } |
OLD | NEW |