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