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,
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>
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
);
39 // The test runner script writes the command line file in
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
, ...) {
65 va_start(args
, format
);
66 __android_log_vprint(priority
, kLogTag
, format
, args
);
72 static void RunTests(JNIEnv
* env
,
73 const JavaParamRef
<jobject
>& obj
,
74 const JavaParamRef
<jstring
>& jcommand_line_flags
,
75 const JavaParamRef
<jstring
>& jcommand_line_file_path
,
76 const JavaParamRef
<jstring
>& jstdout_file_path
,
77 jboolean jstdout_fifo
,
78 const JavaParamRef
<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::InitApplicationContext(env
, app_context
);
85 base::android::RegisterJni(env
);
87 std::vector
<std::string
> args
;
89 const std::string
command_line_file_path(
90 base::android::ConvertJavaStringToUTF8(env
, jcommand_line_file_path
));
91 if (command_line_file_path
.empty())
92 ParseArgsFromCommandLineFile(kCommandLineFilePath
, &args
);
94 ParseArgsFromCommandLineFile(command_line_file_path
.c_str(), &args
);
96 const std::string
command_line_flags(
97 base::android::ConvertJavaStringToUTF8(env
, jcommand_line_flags
));
98 ParseArgsFromString(command_line_flags
, &args
);
100 std::vector
<char*> argv
;
101 int argc
= ArgsToArgv(args
, &argv
);
103 // Fully initialize command line with arguments.
104 base::CommandLine::ForCurrentProcess()->AppendArguments(
105 base::CommandLine(argc
, &argv
[0]), false);
106 const base::CommandLine
& command_line
=
107 *base::CommandLine::ForCurrentProcess();
109 base::FilePath
stdout_file_path(
110 base::android::ConvertJavaStringToUTF8(env
, jstdout_file_path
));
112 // A few options, such "--gtest_list_tests", will just use printf directly
113 // Always redirect stdout to a known file.
114 unlink(stdout_file_path
.value().c_str());
116 if (!base::android::CreateFIFO(stdout_file_path
, 0666)) {
117 AndroidLog(ANDROID_LOG_ERROR
, "Failed to create fifo %s: %s\n",
118 stdout_file_path
.value().c_str(), strerror(errno
));
122 if (!base::android::RedirectStream(stdout
, stdout_file_path
, "w+")) {
123 AndroidLog(ANDROID_LOG_ERROR
, "Failed to redirect stream to file: %s: %s\n",
124 stdout_file_path
.value().c_str(), strerror(errno
));
127 dup2(STDOUT_FILENO
, STDERR_FILENO
);
129 if (command_line
.HasSwitch(switches::kWaitForDebugger
)) {
130 AndroidLog(ANDROID_LOG_VERBOSE
,
131 "Native test waiting for GDB because flag %s was supplied",
132 switches::kWaitForDebugger
);
133 base::debug::WaitForDebugger(24 * 60 * 60, false);
136 ScopedMainEntryLogger scoped_main_entry_logger
;
137 main(argc
, &argv
[0]);
140 bool RegisterNativeTestJNI(JNIEnv
* env
) {
141 return RegisterNativesImpl(env
);
144 // TODO(nileshagrawal): now that we're using FIFO, test scripts can detect EOF.
145 // Remove the signal handlers.
146 void InstallHandlers() {
148 memset(&sa
, 0, sizeof(sa
));
150 sa
.sa_sigaction
= SignalHandler
;
151 sa
.sa_flags
= SA_SIGINFO
;
153 for (unsigned int i
= 0; kExceptionSignals
[i
] != -1; ++i
) {
154 sigaction(kExceptionSignals
[i
], &sa
, &g_old_sa
[kExceptionSignals
[i
]]);
158 } // namespace android
159 } // namespace testing