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