google / googletest

GoogleTest - Google Testing and Mocking Framework

Home Page:https://google.github.io/googletest/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Gtest doesn't use TMPDIR if it's set

ZijunZhaoCCK opened this issue · comments

Describe the bug

TMPDIR isn't used if it is set.

name_template = "/data/local/tmp/";
is not this one:
return "/data/local/tmp/";

TMPDIR is not set on most versions of Android.
The only way for apps to find their temp dir pre S (or maybe even T) is with a Java API.
Gtest doesn't read TMPDIR. It's hard coded to use /data/local/tmp, which is not accessible to apps.

FILE* const file = posix::FOpen(filename_.c_str(), "r");
if (file == nullptr) {
GTEST_LOG_(FATAL) << "Failed to open tmp file " << filename_
<< " for capturing stream.";
}
. The path it's trying to read is /data/local/tmp/gtest_captured_stream.9HGG34. Apps haven't been allowed to access /data/local/tmp for a while . For new OSs (S and newer maybe?), TMPDIR is set for apps.

Does the bug persist in the most recent commit?

Yes.

What operating system and version are you using?

Debian GNU/Linux rodete

On Android, the environment variable is called TEST_TMPDIR, not TMPDIR:

const char* temp_dir = internal::posix::GetEnv("TEST_TMPDIR");

this must also be changed in gtest-port.cc

name_template = TempDir();

diff --git a/googletest/src/gtest-port.cc b/googletest/src/gtest-port.cc
index 994fabd4..53cae875 100644
--- a/googletest/src/gtest-port.cc
+++ b/googletest/src/gtest-port.cc
@@ -1060,7 +1060,7 @@ class CapturedStream {
     // The location /data/local/tmp is directly accessible from native code.
     // '/sdcard' and other variants cannot be relied on, as they are not
     // guaranteed to be mounted, or may have a delay in mounting.
-    name_template = "/data/local/tmp/";
+    name_template = TempDir();
 #elif defined(GTEST_OS_IOS)
     char user_temp_dir[PATH_MAX + 1];