diff options
author | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2023-03-08 04:08:41 +0000 |
---|---|---|
committer | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2023-03-08 04:08:41 +0000 |
commit | f0fa0ebeede31132a0604cdfa46531a3f76baa5b (patch) | |
tree | 0b7deebea10acd29d1d672ca6f5b5975db6f3b64 | |
parent | 7fd34ae92f8b407e73e2aebbf49ae54c5cb01f42 (diff) | |
parent | c2f359b2335f3f3b6b8cca59b2e83de3ca4b64e8 (diff) | |
download | googletest-f0fa0ebeede31132a0604cdfa46531a3f76baa5b.tar.gz |
Snap for 9705326 from c2f359b2335f3f3b6b8cca59b2e83de3ca4b64e8 to udc-release
Change-Id: I12c37535a3d6891b8e7409c440ec0848dfd47d2f
-rw-r--r-- | googletest/include/gtest/internal/custom/gtest.h | 50 |
1 files changed, 46 insertions, 4 deletions
diff --git a/googletest/include/gtest/internal/custom/gtest.h b/googletest/include/gtest/internal/custom/gtest.h index 67ce67f1..3d855613 100644 --- a/googletest/include/gtest/internal/custom/gtest.h +++ b/googletest/include/gtest/internal/custom/gtest.h @@ -35,13 +35,21 @@ #define GOOGLETEST_INCLUDE_GTEST_INTERNAL_CUSTOM_GTEST_H_ #if GTEST_OS_LINUX_ANDROID -# define GTEST_CUSTOM_TEMPDIR_FUNCTION_ GetAndroidTempDir -# include <unistd.h> +#include <dlfcn.h> +#include <unistd.h> + +#define GTEST_CUSTOM_TEMPDIR_FUNCTION_ GetAndroidTempDir +#define GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_(argc, argv) \ + internal::InitGoogleTestImpl(argc, argv); \ + SetAndroidTestLogger() + static inline std::string GetAndroidTempDir() { // Android doesn't have /tmp, and /sdcard is no longer accessible from // an app context starting from Android O. On Android, /data/local/tmp // is usually used as the temporary directory, so try that first... - if (access("/data/local/tmp", R_OK | W_OK | X_OK) == 0) return "/data/local/tmp/"; + if (access("/data/local/tmp", R_OK | W_OK | X_OK) == 0) { + return "/data/local/tmp/"; + } // Processes running in an app context can't write to /data/local/tmp, // so fall back to the current directory... @@ -54,6 +62,40 @@ static inline std::string GetAndroidTempDir() { } return result; } -#endif //GTEST_OS_LINUX_ANDROID + +static inline void SetAndroidTestLogger() { + // By default, Android log messages are only written to the log buffer, where + // GTest cannot see them. This breaks death tests, which need to check the + // crash message to ensure that the process died for the expected reason. + // To fix this, send log messages to both logd and stderr if we are in a death + // test child process. + struct LogMessage; + using LoggerFunction = void (*)(const LogMessage*); + using SetLoggerFunction = void (*)(LoggerFunction logger); + + static void* liblog = dlopen("liblog.so", RTLD_NOW); + if (liblog == nullptr) { + return; + } + + static SetLoggerFunction set_logger = reinterpret_cast<SetLoggerFunction>( + dlsym(liblog, "__android_log_set_logger")); + static LoggerFunction logd_logger = reinterpret_cast<LoggerFunction>( + dlsym(liblog, "__android_log_logd_logger")); + static LoggerFunction stderr_logger = reinterpret_cast<LoggerFunction>( + dlsym(liblog, "__android_log_stderr_logger")); + if (set_logger == nullptr || logd_logger == nullptr || + stderr_logger == nullptr) { + return; + } + + set_logger([](const LogMessage* message) { + logd_logger(message); + if (::testing::internal::InDeathTestChild()) { + stderr_logger(message); + } + }); +} +#endif // GTEST_OS_LINUX_ANDROID #endif // GOOGLETEST_INCLUDE_GTEST_INTERNAL_CUSTOM_GTEST_H_ |