Home | History | Annotate | Download | only in nacl
      1 // Copyright (c) 2012 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 <stdio.h>
      6 #if defined(OS_POSIX)
      7 #include <unistd.h>
      8 #elif defined(OS_WIN)
      9 #include <windows.h>
     10 #endif
     11 
     12 #define TELEMETRY 1
     13 
     14 #include "base/command_line.h"
     15 #include "base/environment.h"
     16 #include "base/path_service.h"
     17 #include "base/process/kill.h"
     18 #include "base/process/launch.h"
     19 #include "base/strings/string_number_conversions.h"
     20 #include "base/win/windows_version.h"
     21 #include "chrome/common/chrome_paths.h"
     22 #include "chrome/common/chrome_switches.h"
     23 #include "chrome/test/nacl/nacl_browsertest_util.h"
     24 #include "components/nacl/browser/nacl_browser.h"
     25 #include "components/nacl/common/nacl_switches.h"
     26 #include "content/public/common/content_switches.h"
     27 
     28 namespace {
     29 
     30 #if defined(OS_WIN)
     31 // crbug.com/98721
     32 #  define MAYBE_SysconfNprocessorsOnln DISABLED_SysconfNprocessorsOnln
     33 #else
     34 #  define MAYBE_SysconfNprocessorsOnln SysconfNprocessorsOnln
     35 #endif
     36 
     37 NACL_BROWSER_TEST_F(NaClBrowserTest, SimpleLoad, {
     38   RunLoadTest(FILE_PATH_LITERAL("nacl_load_test.html"));
     39 })
     40 
     41 IN_PROC_BROWSER_TEST_F(NaClBrowserTestNonSfiMode, MAYBE_NONSFI(Messaging)) {
     42   RunLoadTest(FILE_PATH_LITERAL("libc_free.html"));
     43 }
     44 
     45 IN_PROC_BROWSER_TEST_F(NaClBrowserTestNonSfiMode, MAYBE_NONSFI(Irt)) {
     46   RunNaClIntegrationTest(FILE_PATH_LITERAL("irt_test.html"));
     47 }
     48 
     49 NACL_BROWSER_TEST_F(NaClBrowserTest, ExitStatus0, {
     50   RunNaClIntegrationTest(FILE_PATH_LITERAL(
     51       "pm_exit_status_test.html?trigger=exit0&expected_exit=0"));
     52 })
     53 
     54 NACL_BROWSER_TEST_F(NaClBrowserTest, ExitStatus254, {
     55   RunNaClIntegrationTest(FILE_PATH_LITERAL(
     56       "pm_exit_status_test.html?trigger=exit254&expected_exit=254"));
     57 })
     58 
     59 NACL_BROWSER_TEST_F(NaClBrowserTest, ExitStatusNeg2, {
     60   RunNaClIntegrationTest(FILE_PATH_LITERAL(
     61       "pm_exit_status_test.html?trigger=exitneg2&expected_exit=254"));
     62 })
     63 
     64 #if defined(ADDRESS_SANITIZER)
     65 #define Maybe_PPAPICore DISABLED_PPAPICore
     66 #else
     67 #define Maybe_PPAPICore PPAPICore
     68 #endif
     69 NACL_BROWSER_TEST_F(NaClBrowserTest, Maybe_PPAPICore, {
     70   RunNaClIntegrationTest(FILE_PATH_LITERAL("ppapi_ppb_core.html"));
     71 })
     72 
     73 NACL_BROWSER_TEST_F(NaClBrowserTest, PPAPIPPBInstance, {
     74   RunNaClIntegrationTest(FILE_PATH_LITERAL("ppapi_ppb_instance.html"));
     75 })
     76 
     77 NACL_BROWSER_TEST_F(NaClBrowserTest, PPAPIPPPInstance, {
     78   RunNaClIntegrationTest(FILE_PATH_LITERAL("ppapi_ppp_instance.html"));
     79 })
     80 
     81 NACL_BROWSER_TEST_F(NaClBrowserTest, ProgressEvents, {
     82   RunNaClIntegrationTest(FILE_PATH_LITERAL("ppapi_progress_events.html"));
     83 })
     84 
     85 // Note: currently not run on PNaCl because crash throttling causes the last few
     86 // tests to fail for the wrong reasons.  Enabling this test would also require
     87 // creating a new set of manifests because shared NaCl/PNaCl manifests are not
     88 // allowed.  Also not run on GLibc because it's a large test that is at risk of
     89 // causing timeouts.
     90 // crbug/338444
     91 #if defined(OS_WIN)
     92 #define MAYBE_Bad DISABLED_Bad
     93 #else
     94 #define MAYBE_Bad Bad
     95 #endif
     96 IN_PROC_BROWSER_TEST_F(NaClBrowserTestNewlib, MAYBE_Bad) {
     97   RunNaClIntegrationTest(FILE_PATH_LITERAL("ppapi_bad.html"));
     98 }
     99 
    100 // partially_invalid.c does not have an ARM version of its asm.
    101 #if !defined(__arm__)
    102 IN_PROC_BROWSER_TEST_F(NaClBrowserTestNewlib, BadNative) {
    103   RunNaClIntegrationTest(FILE_PATH_LITERAL("ppapi_bad_native.html"));
    104 }
    105 #endif
    106 
    107 #if defined(OS_WIN)
    108 // crbug.com/98721
    109 #  define MAYBE_Crash DISABLED_Crash
    110 #else
    111 #  define MAYBE_Crash Crash
    112 #endif
    113 NACL_BROWSER_TEST_F(NaClBrowserTest, MAYBE_Crash, {
    114   RunNaClIntegrationTest(FILE_PATH_LITERAL("ppapi_crash.html"));
    115 })
    116 
    117 IN_PROC_BROWSER_TEST_F(NaClBrowserTestNewlib, IrtManifestFile) {
    118   RunNaClIntegrationTest(FILE_PATH_LITERAL("irt_manifest_file_test.html"));
    119 }
    120 IN_PROC_BROWSER_TEST_F(NaClBrowserTestPnaclNonSfi,
    121                        MAYBE_PNACL_NONSFI(IrtManifestFile)) {
    122   RunNaClIntegrationTest(FILE_PATH_LITERAL("irt_manifest_file_test.html"));
    123 }
    124 
    125 #if defined(OS_WIN)
    126 // http://crbug.com/416272
    127 #define MAYBE_IrtException DISABLED_IrtException
    128 #else
    129 #define MAYBE_IrtException IrtException
    130 #endif
    131 IN_PROC_BROWSER_TEST_F(NaClBrowserTestNewlib, MAYBE_IrtException) {
    132   RunNaClIntegrationTest(FILE_PATH_LITERAL("irt_exception_test.html"));
    133 }
    134 IN_PROC_BROWSER_TEST_F(NaClBrowserTestPnaclNonSfi,
    135                        MAYBE_PNACL_NONSFI(IrtException)) {
    136   RunNaClIntegrationTest(FILE_PATH_LITERAL("irt_exception_test.html"));
    137 }
    138 
    139 // Some versions of Visual Studio does not like preprocessor
    140 // conditionals inside the argument of a macro, so we put the
    141 // conditionals on a helper function.  We are already in an anonymous
    142 // namespace, so the name of the helper is not visible in external
    143 // scope.
    144 #if defined(OS_POSIX)
    145 base::FilePath::StringType NumberOfCoresAsFilePathString() {
    146   char string_rep[23];
    147   long nprocessors = sysconf(_SC_NPROCESSORS_ONLN);
    148 #if TELEMETRY
    149   fprintf(stderr, "browser says nprocessors = %ld\n", nprocessors);
    150   fflush(NULL);
    151 #endif
    152   snprintf(string_rep, sizeof string_rep, "%ld", nprocessors);
    153   return string_rep;
    154 }
    155 #elif defined(OS_WIN)
    156 base::FilePath::StringType NumberOfCoresAsFilePathString() {
    157   wchar_t string_rep[23];
    158   SYSTEM_INFO system_info;
    159   GetSystemInfo(&system_info);
    160 #if TELEMETRY
    161   fprintf(stderr, "browser says nprocessors = %lu\n",
    162           system_info.dwNumberOfProcessors);
    163   fflush(NULL);
    164 #endif
    165   _snwprintf_s(string_rep, sizeof string_rep / sizeof string_rep[0], _TRUNCATE,
    166                L"%u", system_info.dwNumberOfProcessors);
    167   return string_rep;
    168 }
    169 #endif
    170 
    171 #if TELEMETRY
    172 static void PathTelemetry(base::FilePath::StringType const &path) {
    173 # if defined(OS_WIN)
    174     fwprintf(stderr, L"path = %s\n", path.c_str());
    175 # else
    176     fprintf(stderr, "path = %s\n", path.c_str());
    177 # endif
    178     fflush(NULL);
    179 }
    180 #else
    181 static void PathTelemetry(base::FilePath::StringType const &path) {
    182   (void) path;
    183 }
    184 #endif
    185 
    186 NACL_BROWSER_TEST_F(NaClBrowserTest, MAYBE_SysconfNprocessorsOnln, {
    187     base::FilePath::StringType path =
    188       FILE_PATH_LITERAL("sysconf_nprocessors_onln_test.html?cpu_count=");
    189     path = path + NumberOfCoresAsFilePathString();
    190     PathTelemetry(path);
    191     RunNaClIntegrationTest(path);
    192 })
    193 
    194 IN_PROC_BROWSER_TEST_F(NaClBrowserTestStatic, CrossOriginCORS) {
    195   RunLoadTest(FILE_PATH_LITERAL("cross_origin/cors.html"));
    196 }
    197 
    198 IN_PROC_BROWSER_TEST_F(NaClBrowserTestStatic, CrossOriginFail) {
    199   RunLoadTest(FILE_PATH_LITERAL("cross_origin/fail.html"));
    200 }
    201 
    202 IN_PROC_BROWSER_TEST_F(NaClBrowserTestStatic, SameOriginCookie) {
    203   RunLoadTest(FILE_PATH_LITERAL("cross_origin/same_origin_cookie.html"));
    204 }
    205 
    206 IN_PROC_BROWSER_TEST_F(NaClBrowserTestStatic, CORSNoCookie) {
    207   RunLoadTest(FILE_PATH_LITERAL("cross_origin/cors_no_cookie.html"));
    208 }
    209 
    210 IN_PROC_BROWSER_TEST_F(NaClBrowserTestStatic, RelativeManifest) {
    211   RunLoadTest(FILE_PATH_LITERAL("manifest/relative_manifest.html"));
    212 }
    213 
    214 // Test with the NaCl debug flag turned on.
    215 class NaClBrowserTestPnaclDebug : public NaClBrowserTestPnacl {
    216  public:
    217   virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
    218     NaClBrowserTestPnacl::SetUpCommandLine(command_line);
    219     // Turn on debugging to influence the PNaCl URL loaded
    220     command_line->AppendSwitch(switches::kEnableNaClDebug);
    221     // On windows, the debug stub requires --no-sandbox:
    222     // crbug.com/265624
    223 #if defined(OS_WIN)
    224     command_line->AppendSwitch(switches::kNoSandbox);
    225 #endif
    226   }
    227 
    228   // On some platforms this test does not work.
    229   bool TestIsBroken() {
    230     // TODO(jvoung): Make this test work on Windows 32-bit. When --no-sandbox
    231     // is used, the required 1GB sandbox address space is not reserved.
    232     // (see note in chrome/browser/nacl_host/test/nacl_gdb_browsertest.cc)
    233 #if defined(OS_WIN)
    234     if (base::win::OSInfo::GetInstance()->wow64_status() ==
    235         base::win::OSInfo::WOW64_DISABLED &&
    236         base::win::OSInfo::GetInstance()->architecture() ==
    237         base::win::OSInfo::X86_ARCHITECTURE) {
    238       return true;
    239     }
    240 #endif
    241     return false;
    242   }
    243 
    244   void StartTestScript(base::ProcessHandle* test_process,
    245                        int debug_stub_port) {
    246     // We call a python script that speaks to the debug stub, and
    247     // lets the app continue, so that the load progress event completes.
    248     CommandLine cmd(base::FilePath(FILE_PATH_LITERAL("python")));
    249     base::FilePath script;
    250     PathService::Get(chrome::DIR_TEST_DATA, &script);
    251     script = script.AppendASCII("nacl/debug_stub_browser_tests.py");
    252     cmd.AppendArgPath(script);
    253     cmd.AppendArg(base::IntToString(debug_stub_port));
    254     cmd.AppendArg("continue");
    255     LOG(INFO) << cmd.GetCommandLineString();
    256     base::LaunchProcess(cmd, base::LaunchOptions(), test_process);
    257   }
    258 
    259   void RunWithTestDebugger(const base::FilePath::StringType& test_url) {
    260     base::ProcessHandle test_script;
    261     scoped_ptr<base::Environment> env(base::Environment::Create());
    262     nacl::NaClBrowser::GetInstance()->SetGdbDebugStubPortListener(
    263         base::Bind(&NaClBrowserTestPnaclDebug::StartTestScript,
    264                    base::Unretained(this), &test_script));
    265     // Turn on debug stub logging.
    266     env->SetVar("NACLVERBOSITY", "1");
    267     RunLoadTest(test_url);
    268     env->UnSetVar("NACLVERBOSITY");
    269     nacl::NaClBrowser::GetInstance()->ClearGdbDebugStubPortListener();
    270     int exit_code;
    271     LOG(INFO) << "Waiting for script to exit (which waits for embed to die).";
    272     base::WaitForExitCode(test_script, &exit_code);
    273     EXPECT_EQ(0, exit_code);
    274   }
    275 };
    276 
    277 // Test with the NaCl debug flag turned on, but mask off every URL
    278 // so that nothing is actually debugged.
    279 class NaClBrowserTestPnaclDebugMasked : public NaClBrowserTestPnaclDebug {
    280  public:
    281   virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
    282     NaClBrowserTestPnaclDebug::SetUpCommandLine(command_line);
    283     command_line->AppendSwitchASCII(switches::kNaClDebugMask,
    284                                     "!<all_urls>");
    285   }
    286 };
    287 
    288 // The tests which actually start a debug session must use the debug stub
    289 // to continue the app startup. However, NaCl on windows can't open the
    290 // debug stub socket in the browser process as needed by the test.
    291 // See http://crbug.com/157312.
    292 #if defined(OS_WIN)
    293 #define MAYBE_PnaclDebugURLFlagAndURL DISABLED_PnaclDebugURLFlagAndURL
    294 #define MAYBE_PnaclDebugURLFlagNoURL DISABLED_PnaclDebugURLFlagNoURL
    295 #else
    296 #define MAYBE_PnaclDebugURLFlagAndURL PnaclDebugURLFlagAndURL
    297 #define MAYBE_PnaclDebugURLFlagNoURL PnaclDebugURLFlagNoURL
    298 #endif
    299 IN_PROC_BROWSER_TEST_F(NaClBrowserTestPnaclDebug,
    300                        MAYBE_PnaclDebugURLFlagAndURL) {
    301   RunWithTestDebugger(FILE_PATH_LITERAL(
    302       "pnacl_debug_url.html?nmf_file=pnacl_has_debug.nmf"));
    303 }
    304 
    305 IN_PROC_BROWSER_TEST_F(NaClBrowserTestPnaclDebug,
    306                        MAYBE_PnaclDebugURLFlagNoURL) {
    307   RunWithTestDebugger(FILE_PATH_LITERAL(
    308       "pnacl_debug_url.html?nmf_file=pnacl_no_debug.nmf"));
    309 }
    310 
    311 IN_PROC_BROWSER_TEST_F(NaClBrowserTestPnacl,
    312                        MAYBE_PNACL(PnaclDebugURLFlagOff)) {
    313   RunLoadTest(FILE_PATH_LITERAL(
    314       "pnacl_debug_url.html?nmf_file=pnacl_has_debug_flag_off.nmf"));
    315 }
    316 
    317 IN_PROC_BROWSER_TEST_F(NaClBrowserTestPnaclDebugMasked,
    318                        MAYBE_PNACL(PnaclDebugURLFlagMaskedOff)) {
    319   if (TestIsBroken()) {
    320     return;
    321   }
    322   // If the mask excludes debugging, it's as if the flag was off.
    323   RunLoadTest(FILE_PATH_LITERAL(
    324       "pnacl_debug_url.html?nmf_file=pnacl_has_debug_flag_off.nmf"));
    325 }
    326 
    327 IN_PROC_BROWSER_TEST_F(NaClBrowserTestPnacl,
    328                        MAYBE_PNACL(PnaclErrorHandling)) {
    329   RunNaClIntegrationTest(FILE_PATH_LITERAL("pnacl_error_handling.html"));
    330 }
    331 
    332 IN_PROC_BROWSER_TEST_F(NaClBrowserTestPnacl,
    333                        MAYBE_PNACL(PnaclNMFOptionsO0)) {
    334   RunLoadTest(FILE_PATH_LITERAL("pnacl_options.html?use_nmf=o_0"));
    335 }
    336 
    337 IN_PROC_BROWSER_TEST_F(NaClBrowserTestPnacl,
    338                        MAYBE_PNACL(PnaclNMFOptionsO2)) {
    339   RunLoadTest(FILE_PATH_LITERAL("pnacl_options.html?use_nmf=o_2"));
    340 }
    341 
    342 IN_PROC_BROWSER_TEST_F(NaClBrowserTestPnacl,
    343                        MAYBE_PNACL(PnaclNMFOptionsOlarge)) {
    344   RunLoadTest(FILE_PATH_LITERAL("pnacl_options.html?use_nmf=o_large"));
    345 }
    346 
    347 IN_PROC_BROWSER_TEST_F(NaClBrowserTestPnacl,
    348                        MAYBE_PNACL(PnaclDyncodeSyscallDisabled)) {
    349   RunNaClIntegrationTest(FILE_PATH_LITERAL(
    350       "pnacl_dyncode_syscall_disabled.html"));
    351 }
    352 
    353 IN_PROC_BROWSER_TEST_F(NaClBrowserTestPnacl,
    354                        MAYBE_PNACL(PnaclExceptionHandlingDisabled)) {
    355   RunNaClIntegrationTest(FILE_PATH_LITERAL(
    356       "pnacl_hw_eh_disabled.html"));
    357 }
    358 
    359 IN_PROC_BROWSER_TEST_F(NaClBrowserTestPnacl, PnaclMimeType) {
    360   RunLoadTest(FILE_PATH_LITERAL("pnacl_mime_type.html"));
    361 }
    362 
    363 // TODO(ncbray) support glibc and PNaCl
    364 // flaky: crbug.com/375894
    365 IN_PROC_BROWSER_TEST_F(NaClBrowserTestNewlibExtension, DISABLED_MimeHandler) {
    366   RunNaClIntegrationTest(FILE_PATH_LITERAL(
    367       "ppapi_extension_mime_handler.html"));
    368 }
    369 
    370 }  // namespace
    371