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 "base/command_line.h"
6#include "base/environment.h"
7#include "base/path_service.h"
8#include "base/process/kill.h"
9#include "base/process/launch.h"
10#include "base/strings/string_number_conversions.h"
11#include "chrome/common/chrome_paths.h"
12#include "chrome/test/ppapi/ppapi_test.h"
13#include "components/nacl/browser/nacl_browser.h"
14#include "components/nacl/common/nacl_switches.h"
15#include "content/public/test/test_utils.h"
16
17class NaClGdbDebugStubTest : public PPAPINaClNewlibTest {
18 public:
19  NaClGdbDebugStubTest() {
20  }
21
22  virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE;
23
24  void StartTestScript(base::ProcessHandle* test_process,
25                       std::string test_name, int debug_stub_port);
26  void RunDebugStubTest(const std::string& nacl_module,
27                        const std::string& test_name);
28};
29
30void NaClGdbDebugStubTest::SetUpCommandLine(CommandLine* command_line) {
31  PPAPINaClNewlibTest::SetUpCommandLine(command_line);
32  command_line->AppendSwitch(switches::kEnableNaClDebug);
33}
34
35void NaClGdbDebugStubTest::StartTestScript(base::ProcessHandle* test_process,
36                                           std::string test_name,
37                                           int debug_stub_port) {
38  // We call python script to reuse GDB RSP protocol implementation.
39  CommandLine cmd(base::FilePath(FILE_PATH_LITERAL("python")));
40  base::FilePath script;
41  PathService::Get(chrome::DIR_TEST_DATA, &script);
42  script = script.AppendASCII("nacl/debug_stub_browser_tests.py");
43  cmd.AppendArgPath(script);
44  cmd.AppendArg(base::IntToString(debug_stub_port));
45  cmd.AppendArg(test_name);
46  LOG(INFO) << cmd.GetCommandLineString();
47  base::LaunchProcess(cmd, base::LaunchOptions(), test_process);
48}
49
50void NaClGdbDebugStubTest::RunDebugStubTest(const std::string& nacl_module,
51                                            const std::string& test_name) {
52  base::ProcessHandle test_script;
53  scoped_ptr<base::Environment> env(base::Environment::Create());
54  nacl::NaClBrowser::GetInstance()->SetGdbDebugStubPortListener(
55      base::Bind(&NaClGdbDebugStubTest::StartTestScript,
56                 base::Unretained(this), &test_script, test_name));
57  // Turn on debug stub logging.
58  env->SetVar("NACLVERBOSITY", "1");
59  RunTestViaHTTP(nacl_module);
60  env->UnSetVar("NACLVERBOSITY");
61  nacl::NaClBrowser::GetInstance()->ClearGdbDebugStubPortListener();
62  int exit_code;
63  base::WaitForExitCode(test_script, &exit_code);
64  EXPECT_EQ(0, exit_code);
65}
66
67// NaCl tests are disabled under ASAN because of qualification test.
68#if defined(ADDRESS_SANITIZER)
69# define MAYBE_Empty DISABLED_Empty
70#else
71# define MAYBE_Empty Empty
72#endif
73
74IN_PROC_BROWSER_TEST_F(NaClGdbDebugStubTest, MAYBE_Empty) {
75  RunDebugStubTest("Empty", "continue");
76}
77
78#if defined(ADDRESS_SANITIZER)
79# define MAYBE_Breakpoint DISABLED_Breakpoint
80#elif defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(ARCH_CPU_ARM_FAMILY)
81// Timing out on ARM linux: http://crbug.com/238469
82# define MAYBE_Breakpoint DISABLED_Breakpoint
83#else
84# define MAYBE_Breakpoint Breakpoint
85#endif
86
87IN_PROC_BROWSER_TEST_F(NaClGdbDebugStubTest, MAYBE_Breakpoint) {
88  RunDebugStubTest("Empty", "breakpoint");
89}
90