aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYabin Cui <yabinc@google.com>2017-01-13 15:52:31 -0800
committerYabin Cui <yabinc@google.com>2017-02-14 13:40:57 -0800
commit5f9167db373b3f162b5847d40fbd2b7df96426d3 (patch)
treedb6cccb5f854d4eeed95ae4e9ecebe22352f2c84
parent90bc60c06ca961116d13a0e07bf945067b61c011 (diff)
downloadgoogletest-5f9167db373b3f162b5847d40fbd2b7df96426d3.tar.gz
gtest: fix Android temp dir.android-o-preview-1o-preview
/sdcard is no longer available for processes running in app context starting from Android O. Instead, first try /data/local/tmp, which is usually used as the temporary directory, then try the current directory, which is usually accessible in app context. Bug: http://b/18790309 Test: Run each gtest unit test in both shell context and app context. Test: Tests *DeathTestInChangedDir can't pass in app context because Test: both /data/local/tmp and / are not writable in app context. Change-Id: Ie7918dbe7dccd24395dccc0c651359893350e2a2
-rw-r--r--googletest/src/gtest-port.cc37
1 files changed, 19 insertions, 18 deletions
diff --git a/googletest/src/gtest-port.cc b/googletest/src/gtest-port.cc
index 0162fac4..e8f8d89a 100644
--- a/googletest/src/gtest-port.cc
+++ b/googletest/src/gtest-port.cc
@@ -952,24 +952,10 @@ class CapturedStream {
# else
// There's no guarantee that a test has write access to the current
// directory, so we create the temporary file in the /tmp directory
- // instead. We use /tmp on most systems, and /sdcard on Android.
- // That's because Android doesn't have /tmp.
+ // instead.
# if GTEST_OS_LINUX_ANDROID
- // Note: Android applications are expected to call the framework's
- // Context.getExternalStorageDirectory() method through JNI to get
- // the location of the world-writable SD Card directory. However,
- // this requires a Context handle, which cannot be retrieved
- // globally from native code. Doing so also precludes running the
- // code as part of a regular standalone executable, which doesn't
- // run in a Dalvik process (e.g. when running it through 'adb shell').
- //
- // The location /sdcard is directly accessible from native code
- // and is the only location (unofficially) supported by the Android
- // team. It's generally a symlink to the real SD Card mount point
- // which can be /mnt/sdcard, /mnt/sdcard0, /system/media/sdcard, or
- // other OEM-customized locations. Never rely on these, and always
- // use /sdcard.
- char name_template[] = "/sdcard/gtest_captured_stream.XXXXXX";
+ ::std::string name_template_buf = TempDir() + "gtest_captured_stream.XXXXXX";
+ char* name_template = &name_template_buf[0];
# else
char name_template[] = "/tmp/captured_stream.XXXXXX";
# endif // GTEST_OS_LINUX_ANDROID
@@ -1067,7 +1053,22 @@ std::string TempDir() {
else
return std::string(temp_dir) + "\\";
#elif GTEST_OS_LINUX_ANDROID
- return "/sdcard/";
+ // Android doesn't have /tmp, and /sdcard is no longer accessible from
+ // app context starting from Android O. On Android, /data/local/tmp
+ // is usually used as the temporary directory. But processes running
+ // in app context can't write to /data/local/tmp, so also try the
+ // current directory.
+ if (access("/data/local/tmp", R_OK | W_OK | X_OK) == 0) {
+ return "/data/local/tmp/";
+ }
+ std::string result = "./";
+ char* cwd = getcwd(NULL, 0);
+ if (cwd != NULL) {
+ result = cwd;
+ result += "/";
+ free(cwd);
+ }
+ return result;
#else
return "/tmp/";
#endif // GTEST_OS_WINDOWS_MOBILE