native_test_launcher.cc revision 5821806d5e7f356e8fa4b058a389a808ea183019
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// This class sets up the environment for running the native tests inside an
6// android application. It outputs (to a fifo) markers identifying the
7// START/PASSED/CRASH of the test suite, FAILURE/SUCCESS of individual tests,
8// etc.
9// These markers are read by the test runner script to generate test results.
10// It installs signal handlers to detect crashes.
11
12#include <android/log.h>
13#include <signal.h>
14#include <stdarg.h>
15#include <stdio.h>
16
17#include "base/android/base_jni_registrar.h"
18#include "base/android/jni_android.h"
19#include "base/android/jni_string.h"
20#include "base/android/locale_utils.h"
21#include "base/android/path_utils.h"
22#include "base/android/scoped_java_ref.h"
23#include "base/at_exit.h"
24#include "base/base_switches.h"
25#include "base/command_line.h"
26#include "base/file_path.h"
27#include "base/file_util.h"
28#include "base/logging.h"
29#include "base/stringprintf.h"
30#include "base/string_tokenizer.h"
31#include "base/string_util.h"
32#include "gtest/gtest.h"
33#include "testing/jni/ChromeNativeTestActivity_jni.h"
34
35// The main function of the program to be wrapped as a test apk.
36extern int main(int argc, char** argv);
37
38namespace {
39
40// These two command line flags are supported for DumpRenderTree, which needs
41// three fifos rather than a combined one: one for stderr, stdin and stdout.
42const char kSeparateStderrFifo[] = "separate-stderr-fifo";
43const char kCreateStdinFifo[] = "create-stdin-fifo";
44
45const char kLogTag[] = "chromium";
46const char kCrashedMarker[] = "[ CRASHED      ]\n";
47
48void AndroidLogError(const char* format, ...) {
49  va_list args;
50  va_start(args, format);
51  __android_log_vprint(ANDROID_LOG_ERROR, kLogTag, format, args);
52  va_end(args);
53}
54
55// The list of signals which are considered to be crashes.
56const int kExceptionSignals[] = {
57  SIGSEGV, SIGABRT, SIGFPE, SIGILL, SIGBUS, -1
58};
59
60struct sigaction g_old_sa[NSIG];
61
62// This function runs in a compromised context. It should not allocate memory.
63void SignalHandler(int sig, siginfo_t* info, void* reserved) {
64  // Output the crash marker.
65  write(STDOUT_FILENO, kCrashedMarker, sizeof(kCrashedMarker));
66  g_old_sa[sig].sa_sigaction(sig, info, reserved);
67}
68
69// TODO(nileshagrawal): now that we're using FIFO, test scripts can detect EOF.
70// Remove the signal handlers.
71void InstallHandlers() {
72  struct sigaction sa;
73  memset(&sa, 0, sizeof(sa));
74
75  sa.sa_sigaction = SignalHandler;
76  sa.sa_flags = SA_SIGINFO;
77
78  for (unsigned int i = 0; kExceptionSignals[i] != -1; ++i) {
79    sigaction(kExceptionSignals[i], &sa, &g_old_sa[kExceptionSignals[i]]);
80  }
81}
82
83void ParseArgsFromString(const std::string& command_line,
84                         std::vector<std::string>* args) {
85  StringTokenizer tokenizer(command_line, kWhitespaceASCII);
86  tokenizer.set_quote_chars("\"");
87  while (tokenizer.GetNext()) {
88    std::string token;
89    RemoveChars(tokenizer.token(), "\"", &token);
90    args->push_back(token);
91  }
92}
93
94void ParseArgsFromCommandLineFile(std::vector<std::string>* args) {
95  // The test runner script writes the command line file in
96  // "/data/local/tmp".
97  static const char kCommandLineFilePath[] =
98      "/data/local/tmp/chrome-native-tests-command-line";
99  FilePath command_line(kCommandLineFilePath);
100  std::string command_line_string;
101  if (file_util::ReadFileToString(command_line, &command_line_string)) {
102    ParseArgsFromString(command_line_string, args);
103  }
104}
105
106int ArgsToArgv(const std::vector<std::string>& args,
107                std::vector<char*>* argv) {
108  // We need to pass in a non-const char**.
109  int argc = args.size();
110
111  argv->resize(argc + 1);
112  for (int i = 0; i < argc; ++i)
113    (*argv)[i] = const_cast<char*>(args[i].c_str());
114  (*argv)[argc] = NULL;  // argv must be NULL terminated.
115
116  return argc;
117}
118
119void CreateFIFO(const char* fifo_path) {
120  unlink(fifo_path);
121  // Default permissions for mkfifo is ignored, chmod is required.
122  if (mkfifo(fifo_path, 0666) || chmod(fifo_path, 0666)) {
123    AndroidLogError("Failed to create fifo %s: %s\n",
124                    fifo_path, strerror(errno));
125    exit(EXIT_FAILURE);
126  }
127}
128
129void Redirect(FILE* stream, const char* path, const char* mode) {
130  if (!freopen(path, mode, stream)) {
131    AndroidLogError("Failed to redirect stream to file: %s: %s\n",
132                    path, strerror(errno));
133    exit(EXIT_FAILURE);
134  }
135}
136
137class ScopedMainEntryLogger {
138 public:
139  ScopedMainEntryLogger() {
140    printf(">>ScopedMainEntryLogger\n");
141  }
142
143  ~ScopedMainEntryLogger() {
144    printf("<<ScopedMainEntryLogger\n");
145    fflush(stdout);
146    fflush(stderr);
147  }
148};
149
150}  // namespace
151
152// This method is called on a separate java thread so that we won't trigger
153// an ANR.
154static void RunTests(JNIEnv* env,
155                     jobject obj,
156                     jstring jfiles_dir,
157                     jobject app_context) {
158  base::AtExitManager exit_manager;
159
160  // Command line initialized basically, will be fully initialized later.
161  static const char* const kInitialArgv[] = { "ChromeTestActivity" };
162  CommandLine::Init(arraysize(kInitialArgv), kInitialArgv);
163
164  // Set the application context in base.
165  base::android::ScopedJavaLocalRef<jobject> scoped_context(
166      env, env->NewLocalRef(app_context));
167  base::android::InitApplicationContext(scoped_context);
168  base::android::RegisterJni(env);
169
170  std::vector<std::string> args;
171  ParseArgsFromCommandLineFile(&args);
172
173  // We need to pass in a non-const char**.
174  std::vector<char*> argv;
175  int argc = ArgsToArgv(args, &argv);
176
177  // Fully initialize command line with arguments.
178  CommandLine::ForCurrentProcess()->AppendArguments(
179      CommandLine(argc, &argv[0]), false);
180  const CommandLine& command_line = *CommandLine::ForCurrentProcess();
181
182  FilePath files_dir(base::android::ConvertJavaStringToUTF8(env, jfiles_dir));
183
184  // A few options, such "--gtest_list_tests", will just use printf directly
185  // Always redirect stdout to a known file.
186  FilePath fifo_path(files_dir.Append(FilePath("test.fifo")));
187  CreateFIFO(fifo_path.value().c_str());
188
189  FilePath stderr_fifo_path, stdin_fifo_path;
190
191  // DumpRenderTree needs a separate fifo for the stderr output. For all
192  // other tests, insert stderr content to the same fifo we use for stdout.
193  if (command_line.HasSwitch(kSeparateStderrFifo)) {
194    stderr_fifo_path = files_dir.Append(FilePath("stderr.fifo"));
195    CreateFIFO(stderr_fifo_path.value().c_str());
196  }
197
198  // DumpRenderTree uses stdin to receive input about which test to run.
199  if (command_line.HasSwitch(kCreateStdinFifo)) {
200    stdin_fifo_path = files_dir.Append(FilePath("stdin.fifo"));
201    CreateFIFO(stdin_fifo_path.value().c_str());
202  }
203
204  // Only redirect the streams after all fifos have been created.
205  Redirect(stdout, fifo_path.value().c_str(), "w");
206  if (!stdin_fifo_path.empty())
207    Redirect(stdin, stdin_fifo_path.value().c_str(), "r");
208  if (!stderr_fifo_path.empty())
209    Redirect(stderr, stderr_fifo_path.value().c_str(), "w");
210  else
211    dup2(STDOUT_FILENO, STDERR_FILENO);
212
213  if (command_line.HasSwitch(switches::kWaitForDebugger)) {
214    std::string msg = StringPrintf("Native test waiting for GDB because "
215                                   "flag %s was supplied",
216                                   switches::kWaitForDebugger);
217    __android_log_write(ANDROID_LOG_VERBOSE, kLogTag, msg.c_str());
218    base::debug::WaitForDebugger(24 * 60 * 60, false);
219  }
220
221  ScopedMainEntryLogger scoped_main_entry_logger;
222  main(argc, &argv[0]);
223}
224
225// This is called by the VM when the shared library is first loaded.
226JNI_EXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) {
227  // Install signal handlers to detect crashes.
228  InstallHandlers();
229
230  base::android::InitVM(vm);
231  JNIEnv* env = base::android::AttachCurrentThread();
232  if (!RegisterNativesImpl(env)) {
233    return -1;
234  }
235
236  return JNI_VERSION_1_4;
237}
238