• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2012 The Chromium Authors
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 <errno.h>
14 #include <signal.h>
15 #include <string.h>
16 
17 #include <iterator>
18 
19 #include "base/android/jni_string.h"
20 #include "base/android/scoped_java_ref.h"
21 #include "base/at_exit.h"
22 #include "base/base_switches.h"
23 #include "base/clang_profiling_buildflags.h"
24 #include "base/command_line.h"
25 #include "base/debug/debugger.h"
26 #include "base/files/file_path.h"
27 #include "base/files/file_util.h"
28 #include "base/logging.h"
29 #include "base/test/test_support_android.h"
30 #include "base/threading/thread_restrictions.h"
31 #include "gtest/gtest.h"
32 #include "testing/android/native_test/main_runner.h"
33 #include "testing/android/native_test/native_test_jni_headers/NativeTest_jni.h"
34 #include "testing/android/native_test/native_test_util.h"
35 
36 #if BUILDFLAG(CLANG_PROFILING)
37 #include "base/test/clang_profiling.h"
38 #endif
39 
40 using base::android::JavaParamRef;
41 
42 // The main function of the program to be wrapped as a test apk.
43 extern int main(int argc, char** argv);
44 
45 namespace testing {
46 namespace android {
47 
48 namespace {
49 
50 const char kLogTag[] = "chromium";
51 const char kCrashedMarker[] = "[ CRASHED      ]\n";
52 
53 // The list of signals which are considered to be crashes.
54 const int kExceptionSignals[] = {
55   SIGSEGV, SIGABRT, SIGFPE, SIGILL, SIGBUS, -1
56 };
57 
58 struct sigaction g_old_sa[NSIG];
59 
60 // This function runs in a compromised context. It should not allocate memory.
SignalHandler(int sig,siginfo_t * info,void * reserved)61 void SignalHandler(int sig, siginfo_t* info, void* reserved) {
62   // Output the crash marker.
63   write(STDOUT_FILENO, kCrashedMarker, sizeof(kCrashedMarker) - 1);
64   g_old_sa[sig].sa_sigaction(sig, info, reserved);
65 }
66 
67 // Writes printf() style string to Android's logger where |priority| is one of
68 // the levels defined in <android/log.h>.
AndroidLog(int priority,const char * format,...)69 void AndroidLog(int priority, const char* format, ...) {
70   va_list args;
71   va_start(args, format);
72   __android_log_vprint(priority, kLogTag, format, args);
73   va_end(args);
74 }
75 
76 }  // namespace
77 
JNI_NativeTest_RunTests(JNIEnv * env,const JavaParamRef<jstring> & jcommand_line_flags,const JavaParamRef<jstring> & jcommand_line_file_path,const JavaParamRef<jstring> & jstdout_file_path,const JavaParamRef<jobject> & app_context,const JavaParamRef<jstring> & jtest_data_dir)78 static void JNI_NativeTest_RunTests(
79     JNIEnv* env,
80     const JavaParamRef<jstring>& jcommand_line_flags,
81     const JavaParamRef<jstring>& jcommand_line_file_path,
82     const JavaParamRef<jstring>& jstdout_file_path,
83     const JavaParamRef<jobject>& app_context,
84     const JavaParamRef<jstring>& jtest_data_dir) {
85   base::ScopedAllowBlockingForTesting allow;
86 
87   // Command line initialized basically, will be fully initialized later.
88   static const char* const kInitialArgv[] = { "ChromeTestActivity" };
89   base::CommandLine::Init(std::size(kInitialArgv), kInitialArgv);
90 
91   std::vector<std::string> args;
92 
93   const std::string command_line_file_path(
94       base::android::ConvertJavaStringToUTF8(env, jcommand_line_file_path));
95   if (command_line_file_path.empty())
96     args.push_back("_");
97   else
98     ParseArgsFromCommandLineFile(command_line_file_path.c_str(), &args);
99 
100   const std::string command_line_flags(
101       base::android::ConvertJavaStringToUTF8(env, jcommand_line_flags));
102   ParseArgsFromString(command_line_flags, &args);
103 
104   std::vector<char*> argv;
105   int argc = ArgsToArgv(args, &argv);
106 
107   // Fully initialize command line with arguments.
108   base::CommandLine::ForCurrentProcess()->AppendArguments(
109       base::CommandLine(argc, &argv[0]), false);
110   const base::CommandLine& command_line =
111       *base::CommandLine::ForCurrentProcess();
112 
113   base::FilePath stdout_file_path(
114       base::android::ConvertJavaStringToUTF8(env, jstdout_file_path));
115 
116   // A few options, such "--gtest_list_tests", will just use printf directly
117   // Always redirect stdout to a known file.
118   if (freopen(stdout_file_path.value().c_str(), "a+", stdout) == NULL) {
119     AndroidLog(ANDROID_LOG_ERROR, "Failed to redirect stream to file: %s: %s\n",
120                stdout_file_path.value().c_str(), strerror(errno));
121     exit(EXIT_FAILURE);
122   }
123   // TODO(jbudorick): Remove this after resolving crbug.com/726880
124   AndroidLog(ANDROID_LOG_INFO, "Redirecting stdout to file: %s\n",
125              stdout_file_path.value().c_str());
126   dup2(STDOUT_FILENO, STDERR_FILENO);
127 
128   if (command_line.HasSwitch(switches::kWaitForDebugger)) {
129     AndroidLog(ANDROID_LOG_VERBOSE,
130                "Native test waiting for GDB because flag %s was supplied",
131                switches::kWaitForDebugger);
132     base::debug::WaitForDebugger(24 * 60 * 60, true);
133   }
134 
135   base::FilePath test_data_dir(
136       base::android::ConvertJavaStringToUTF8(env, jtest_data_dir));
137   base::InitAndroidTestPaths(test_data_dir);
138 
139   ScopedMainEntryLogger scoped_main_entry_logger;
140   main(argc, &argv[0]);
141 
142 // Explicitly write profiling data to LLVM profile file.
143 #if BUILDFLAG(CLANG_PROFILING)
144   base::WriteClangProfilingProfile();
145 #endif
146 }
147 
148 // TODO(nileshagrawal): now that we're using FIFO, test scripts can detect EOF.
149 // Remove the signal handlers.
InstallHandlers()150 void InstallHandlers() {
151   struct sigaction sa;
152   memset(&sa, 0, sizeof(sa));
153 
154   sa.sa_sigaction = SignalHandler;
155   sa.sa_flags = SA_SIGINFO;
156 
157   for (unsigned int i = 0; kExceptionSignals[i] != -1; ++i) {
158     sigaction(kExceptionSignals[i], &sa, &g_old_sa[kExceptionSignals[i]]);
159   }
160 }
161 
162 }  // namespace android
163 }  // namespace testing
164