Durable Storage: Refactor browser test and test the basic "deny" flow.
[chromium-blink-merge.git] / testing / android / native_test / native_test_launcher.cc
blob343f1180c6803ff600589777472790f348c87fc2
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.
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.
12 #include <android/log.h>
13 #include <signal.h>
15 #include "base/android/base_jni_registrar.h"
16 #include "base/android/fifo_utils.h"
17 #include "base/android/jni_android.h"
18 #include "base/android/jni_string.h"
19 #include "base/android/scoped_java_ref.h"
20 #include "base/at_exit.h"
21 #include "base/base_switches.h"
22 #include "base/command_line.h"
23 #include "base/files/file_path.h"
24 #include "base/files/file_util.h"
25 #include "base/logging.h"
26 #include "base/strings/stringprintf.h"
27 #include "gtest/gtest.h"
28 #include "jni/NativeTestActivity_jni.h"
29 #include "testing/android/native_test/native_test_util.h"
31 // The main function of the program to be wrapped as a test apk.
32 extern int main(int argc, char** argv);
34 namespace testing {
35 namespace android {
37 namespace {
39 // The test runner script writes the command line file in
40 // "/data/local/tmp".
41 static const char kCommandLineFilePath[] =
42 "/data/local/tmp/chrome-native-tests-command-line";
44 const char kLogTag[] = "chromium";
45 const char kCrashedMarker[] = "[ CRASHED ]\n";
47 // The list of signals which are considered to be crashes.
48 const int kExceptionSignals[] = {
49 SIGSEGV, SIGABRT, SIGFPE, SIGILL, SIGBUS, -1
52 struct sigaction g_old_sa[NSIG];
54 // This function runs in a compromised context. It should not allocate memory.
55 void SignalHandler(int sig, siginfo_t* info, void* reserved) {
56 // Output the crash marker.
57 write(STDOUT_FILENO, kCrashedMarker, sizeof(kCrashedMarker));
58 g_old_sa[sig].sa_sigaction(sig, info, reserved);
61 // Writes printf() style string to Android's logger where |priority| is one of
62 // the levels defined in <android/log.h>.
63 void AndroidLog(int priority, const char* format, ...) {
64 va_list args;
65 va_start(args, format);
66 __android_log_vprint(priority, kLogTag, format, args);
67 va_end(args);
70 } // namespace
72 static void RunTests(JNIEnv* env,
73 jobject obj,
74 jstring jcommand_line_flags,
75 jstring jcommand_line_file_path,
76 jstring jstdout_file_path,
77 jboolean jstdout_fifo,
78 jobject app_context) {
79 // Command line initialized basically, will be fully initialized later.
80 static const char* const kInitialArgv[] = { "ChromeTestActivity" };
81 base::CommandLine::Init(arraysize(kInitialArgv), kInitialArgv);
83 // Set the application context in base.
84 base::android::ScopedJavaLocalRef<jobject> scoped_context(
85 env, env->NewLocalRef(app_context));
86 base::android::InitApplicationContext(env, scoped_context);
87 base::android::RegisterJni(env);
89 std::vector<std::string> args;
91 const std::string command_line_file_path(
92 base::android::ConvertJavaStringToUTF8(env, jcommand_line_file_path));
93 if (command_line_file_path.empty())
94 ParseArgsFromCommandLineFile(kCommandLineFilePath, &args);
95 else
96 ParseArgsFromCommandLineFile(command_line_file_path.c_str(), &args);
98 const std::string command_line_flags(
99 base::android::ConvertJavaStringToUTF8(env, jcommand_line_flags));
100 ParseArgsFromString(command_line_flags, &args);
102 std::vector<char*> argv;
103 int argc = ArgsToArgv(args, &argv);
105 // Fully initialize command line with arguments.
106 base::CommandLine::ForCurrentProcess()->AppendArguments(
107 base::CommandLine(argc, &argv[0]), false);
108 const base::CommandLine& command_line =
109 *base::CommandLine::ForCurrentProcess();
111 base::FilePath stdout_file_path(
112 base::android::ConvertJavaStringToUTF8(env, jstdout_file_path));
114 // A few options, such "--gtest_list_tests", will just use printf directly
115 // Always redirect stdout to a known file.
116 unlink(stdout_file_path.value().c_str());
117 if (jstdout_fifo) {
118 if (!base::android::CreateFIFO(stdout_file_path, 0666)) {
119 AndroidLog(ANDROID_LOG_ERROR, "Failed to create fifo %s: %s\n",
120 stdout_file_path.value().c_str(), strerror(errno));
121 exit(EXIT_FAILURE);
124 if (!base::android::RedirectStream(stdout, stdout_file_path, "w+")) {
125 AndroidLog(ANDROID_LOG_ERROR, "Failed to redirect stream to file: %s: %s\n",
126 stdout_file_path.value().c_str(), strerror(errno));
127 exit(EXIT_FAILURE);
129 dup2(STDOUT_FILENO, STDERR_FILENO);
131 if (command_line.HasSwitch(switches::kWaitForDebugger)) {
132 AndroidLog(ANDROID_LOG_VERBOSE,
133 "Native test waiting for GDB because flag %s was supplied",
134 switches::kWaitForDebugger);
135 base::debug::WaitForDebugger(24 * 60 * 60, false);
138 ScopedMainEntryLogger scoped_main_entry_logger;
139 main(argc, &argv[0]);
142 bool RegisterNativeTestJNI(JNIEnv* env) {
143 return RegisterNativesImpl(env);
146 // TODO(nileshagrawal): now that we're using FIFO, test scripts can detect EOF.
147 // Remove the signal handlers.
148 void InstallHandlers() {
149 struct sigaction sa;
150 memset(&sa, 0, sizeof(sa));
152 sa.sa_sigaction = SignalHandler;
153 sa.sa_flags = SA_SIGINFO;
155 for (unsigned int i = 0; kExceptionSignals[i] != -1; ++i) {
156 sigaction(kExceptionSignals[i], &sa, &g_old_sa[kExceptionSignals[i]]);
160 } // namespace android
161 } // namespace testing