Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / chrome / test / nacl / pnacl_header_test.cc
blobe4922aa5867f96e9a8a3a069aee37c589b11546e
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
3 // found in the LICENSE file.
5 #include "chrome/test/nacl/pnacl_header_test.h"
7 #include "base/bind.h"
8 #include "base/path_service.h"
9 #include "base/test/scoped_path_override.h"
10 #include "chrome/browser/ui/browser.h"
11 #include "chrome/browser/ui/tabs/tab_strip_model.h"
12 #include "chrome/common/chrome_paths.h"
13 #include "chrome/test/base/ui_test_utils.h"
14 #include "chrome/test/nacl/nacl_browsertest_util.h"
15 #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/http_request.h"
18 #include "net/test/embedded_test_server/http_response.h"
20 using net::test_server::BasicHttpResponse;
21 using net::test_server::EmbeddedTestServer;
22 using net::test_server::HttpRequest;
23 using net::test_server::HttpResponse;
25 PnaclHeaderTest::PnaclHeaderTest() : noncors_loads_(0), cors_loads_(0) {}
27 PnaclHeaderTest::~PnaclHeaderTest() {}
29 void PnaclHeaderTest::StartServer() {
30 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
32 // For most requests, just serve files, but register a special test handler
33 // that watches for the .pexe fetch also.
34 base::FilePath test_data_dir;
35 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir));
36 embedded_test_server()->RegisterRequestHandler(
37 base::Bind(&PnaclHeaderTest::WatchForPexeFetch, base::Unretained(this)));
38 embedded_test_server()->ServeFilesFromDirectory(test_data_dir);
41 void PnaclHeaderTest::RunLoadTest(const std::string& url,
42 int expected_noncors,
43 int expected_cors) {
44 StartServer();
45 LoadTestMessageHandler handler;
46 content::JavascriptTestObserver observer(
47 browser()->tab_strip_model()->GetActiveWebContents(),
48 &handler);
50 // Make sure this is able to do a pexe fetch, even without access
51 // to the PNaCl component files (make DIR_PNACL_COMPONENT empty).
52 // 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
54 // is the pexe fetch that helps trigger an on-demand installation
55 // which installs the files to disk (if that hasn't already happened
56 // in the background).
57 base::ScopedPathOverride component_dir(chrome::DIR_PNACL_COMPONENT);
59 ui_test_utils::NavigateToURL(browser(), embedded_test_server()->GetURL(url));
60 // Wait until the NMF and pexe are also loaded, not just the HTML.
61 // Do this by waiting till the LoadTestMessageHandler responds.
62 EXPECT_TRUE(observer.Run()) << handler.error_message();
63 EXPECT_TRUE(handler.test_passed()) << "Test failed.";
64 EXPECT_EQ(expected_noncors, noncors_loads_);
65 EXPECT_EQ(expected_cors, cors_loads_);
68 scoped_ptr<HttpResponse> PnaclHeaderTest::WatchForPexeFetch(
69 const HttpRequest& request) {
70 // Avoid favicon.ico warning by giving it a dummy icon.
71 if (request.relative_url.find("favicon.ico") != std::string::npos) {
72 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse());
73 http_response->set_code(net::HTTP_OK);
74 http_response->set_content("");
75 http_response->set_content_type("application/octet-stream");
76 return http_response.PassAs<HttpResponse>();
79 // Skip other non-pexe files and let ServeFilesFromDirectory handle it.
80 GURL absolute_url = embedded_test_server()->GetURL(request.relative_url);
81 if (absolute_url.path().find(".pexe") == std::string::npos)
82 return scoped_ptr<HttpResponse>();
84 // For pexe files, check for the special Accept header.
85 // This must match whatever is in:
86 // ppapi/native_client/src/trusted/plugin/pnacl_coordinator.cc
87 EXPECT_NE(0U, request.headers.count("Accept"));
88 std::map<std::string, std::string>::const_iterator it =
89 request.headers.find("Accept");
90 EXPECT_NE(std::string::npos, it->second.find("application/x-pnacl"));
91 EXPECT_NE(std::string::npos, it->second.find("*/*"));
93 // Also make sure that other headers like CORS-related headers
94 // are preserved when injecting the special Accept header.
95 if (absolute_url.path().find("cors") == std::string::npos) {
96 EXPECT_EQ(0U, request.headers.count("Origin"));
97 noncors_loads_ += 1;
98 } else {
99 EXPECT_EQ(1U, request.headers.count("Origin"));
100 cors_loads_ += 1;
103 // After checking the header, just return a 404. We don't need to actually
104 // compile and stopping with a 404 is faster.
105 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse());
106 http_response->set_code(net::HTTP_NOT_FOUND);
107 http_response->set_content("PEXE ... not found");
108 http_response->set_content_type("application/octet-stream");
109 return http_response.PassAs<HttpResponse>();
112 IN_PROC_BROWSER_TEST_F(PnaclHeaderTest, TestHasPnaclHeader) {
113 // Load 2 pexes, one same origin and one cross orgin.
114 RunLoadTest("/nacl/pnacl_request_header/pnacl_request_header.html", 1, 1);