Home | History | Annotate | Download | only in nacl
      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 "chrome/browser/ui/browser.h"
     10 #include "chrome/browser/ui/tabs/tab_strip_model.h"
     11 #include "chrome/common/chrome_paths.h"
     12 #include "chrome/test/base/ui_test_utils.h"
     13 #include "chrome/test/nacl/nacl_browsertest_util.h"
     14 #include "content/public/browser/web_contents.h"
     15 #include "net/test/embedded_test_server/embedded_test_server.h"
     16 #include "net/test/embedded_test_server/http_request.h"
     17 #include "net/test/embedded_test_server/http_response.h"
     18 
     19 using net::test_server::BasicHttpResponse;
     20 using net::test_server::EmbeddedTestServer;
     21 using net::test_server::HttpRequest;
     22 using net::test_server::HttpResponse;
     23 
     24 PnaclHeaderTest::PnaclHeaderTest() : noncors_loads_(0), cors_loads_(0) {}
     25 
     26 PnaclHeaderTest::~PnaclHeaderTest() {}
     27 
     28 void PnaclHeaderTest::StartServer() {
     29   ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
     30 
     31   // For most requests, just serve files, but register a special test handler
     32   // that watches for the .pexe fetch also.
     33   base::FilePath test_data_dir;
     34   ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir));
     35   embedded_test_server()->RegisterRequestHandler(
     36       base::Bind(&PnaclHeaderTest::WatchForPexeFetch, base::Unretained(this)));
     37   embedded_test_server()->ServeFilesFromDirectory(test_data_dir);
     38 }
     39 
     40 void PnaclHeaderTest::RunLoadTest(const std::string& url,
     41                                   int expected_noncors,
     42                                   int expected_cors) {
     43   StartServer();
     44   LoadTestMessageHandler handler;
     45   content::JavascriptTestObserver observer(
     46       browser()->tab_strip_model()->GetActiveWebContents(),
     47       &handler);
     48   ui_test_utils::NavigateToURL(browser(), embedded_test_server()->GetURL(url));
     49   // Wait until the NMF and pexe are also loaded, not just the HTML.
     50   // Do this by waiting till the LoadTestMessageHandler responds.
     51   EXPECT_TRUE(observer.Run()) << handler.error_message();
     52   EXPECT_TRUE(handler.test_passed()) << "Test failed.";
     53   EXPECT_EQ(expected_noncors, noncors_loads_);
     54   EXPECT_EQ(expected_cors, cors_loads_);
     55 }
     56 
     57 scoped_ptr<HttpResponse> PnaclHeaderTest::WatchForPexeFetch(
     58     const HttpRequest& request) {
     59   // Avoid favicon.ico warning by giving it a dummy icon.
     60   if (request.relative_url.find("favicon.ico") != std::string::npos) {
     61     scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse());
     62     http_response->set_code(net::HTTP_OK);
     63     http_response->set_content("");
     64     http_response->set_content_type("application/octet-stream");
     65     return http_response.PassAs<HttpResponse>();
     66   }
     67 
     68   // Skip other non-pexe files and let ServeFilesFromDirectory handle it.
     69   GURL absolute_url = embedded_test_server()->GetURL(request.relative_url);
     70   if (absolute_url.path().find(".pexe") == std::string::npos)
     71     return scoped_ptr<HttpResponse>();
     72 
     73   // For pexe files, check for the special Accept header.
     74   // This must match whatever is in:
     75   // ppapi/native_client/src/trusted/plugin/pnacl_coordinator.cc
     76   EXPECT_NE(0U, request.headers.count("Accept"));
     77   std::map<std::string, std::string>::const_iterator it =
     78       request.headers.find("Accept");
     79   EXPECT_NE(std::string::npos, it->second.find("application/x-pnacl"));
     80   EXPECT_NE(std::string::npos, it->second.find("*/*"));
     81 
     82   // Also make sure that other headers like CORS-related headers
     83   // are preserved when injecting the special Accept header.
     84   if (absolute_url.path().find("cors") == std::string::npos) {
     85     EXPECT_EQ(0U, request.headers.count("Origin"));
     86     noncors_loads_ += 1;
     87   } else {
     88     EXPECT_EQ(1U, request.headers.count("Origin"));
     89     cors_loads_ += 1;
     90   }
     91 
     92   // After checking the header, just return a 404. We don't need to actually
     93   // compile and stopping with a 404 is faster.
     94   scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse());
     95   http_response->set_code(net::HTTP_NOT_FOUND);
     96   http_response->set_content("PEXE ... not found");
     97   http_response->set_content_type("application/octet-stream");
     98   return http_response.PassAs<HttpResponse>();
     99 }
    100 
    101 IN_PROC_BROWSER_TEST_F(PnaclHeaderTest, TestHasPnaclHeader) {
    102   // Load 2 pexes, one same origin and one cross orgin.
    103   RunLoadTest("/nacl/pnacl_request_header/pnacl_request_header.html", 1, 1);
    104 }
    105