diff options
author | Abseil Team <absl-team@google.com> | 2021-05-24 14:53:07 (GMT) |
---|---|---|
committer | Derek Mauro <dmauro@google.com> | 2021-05-25 17:34:32 (GMT) |
commit | c0777e6565a5ab58db699de75cd122ef9b8de847 (patch) | |
tree | 0a783873914a0ce3fc973a179cf868f4d5200e0f /googletest/src | |
parent | 9741c42200b66abc708ee6da269a29c8bd912cee (diff) | |
download | googletest-c0777e6565a5ab58db699de75cd122ef9b8de847.zip googletest-c0777e6565a5ab58db699de75cd122ef9b8de847.tar.gz googletest-c0777e6565a5ab58db699de75cd122ef9b8de847.tar.bz2 |
Googletest export
Remove the dependency on Objective C++ in iOS builds.
252ce9c52d304659eff6be558209c811b7191963 introduced the use of
NSTemporaryDirectory() on iOS, which requires Core Foundation, and
Objective C++.
This CL replaces NSTemporaryDirectory() with an equivalent solution
(according to Apple's documentation at [1]) available to C/C++ code.
Avoiding Objective C++ and Core Foundation makes it easier to integrate
googletest in projects that can't use the supplied Bazel build files.
[1] https://developer.apple.com/library/archive/documentation/Security/Conceptual/SecureCodingGuide/Articles/RaceConditions.html#//apple_ref/doc/uid/TP40002585-SW10
PiperOrigin-RevId: 375474990
Diffstat (limited to 'googletest/src')
-rw-r--r-- | googletest/src/gtest-port.cc | 53 |
1 files changed, 36 insertions, 17 deletions
diff --git a/googletest/src/gtest-port.cc b/googletest/src/gtest-port.cc index 26bc857..53a4d37 100644 --- a/googletest/src/gtest-port.cc +++ b/googletest/src/gtest-port.cc @@ -80,10 +80,6 @@ # include <zircon/syscalls.h> #endif // GTEST_OS_FUCHSIA -#if GTEST_OS_IOS -#import <Foundation/Foundation.h> -#endif // GTEST_OS_IOS - #include "gtest/gtest-spi.h" #include "gtest/gtest-message.h" #include "gtest/internal/gtest-internal.h" @@ -692,8 +688,8 @@ class ThreadLocalRegistryImpl { static Mutex thread_map_mutex_; }; -Mutex ThreadLocalRegistryImpl::mutex_(Mutex::kStaticMutex); -Mutex ThreadLocalRegistryImpl::thread_map_mutex_(Mutex::kStaticMutex); +Mutex ThreadLocalRegistryImpl::mutex_(Mutex::kStaticMutex); // NOLINT +Mutex ThreadLocalRegistryImpl::thread_map_mutex_(Mutex::kStaticMutex); // NOLINT ThreadLocalValueHolderBase* ThreadLocalRegistry::GetValueOnCurrentThread( const ThreadLocalBase* thread_local_instance) { @@ -1099,9 +1095,9 @@ class CapturedStream { filename_ = temp_file_path; # 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. + // directory, so we create the temporary file in a temporary directory. + std::string name_template; + # if GTEST_OS_LINUX_ANDROID // Note: Android applications are expected to call the framework's // Context.getExternalStorageDirectory() method through JNI to get @@ -1114,23 +1110,46 @@ 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. - char name_template[] = "/data/local/tmp/gtest_captured_stream.XXXXXX"; + name_template = "/data/local/tmp/"; # elif GTEST_OS_IOS - NSString* temp_path = [NSTemporaryDirectory() - stringByAppendingPathComponent:@"gtest_captured_stream.XXXXXX"]; + char user_temp_dir[PATH_MAX + 1]; - char name_template[PATH_MAX + 1]; - strncpy(name_template, [temp_path UTF8String], PATH_MAX); + // Documented alternative to NSTemporaryDirectory() (for obtaining creating + // a temporary directory) at + // https://developer.apple.com/library/archive/documentation/Security/Conceptual/SecureCodingGuide/Articles/RaceConditions.html#//apple_ref/doc/uid/TP40002585-SW10 + // + // _CS_DARWIN_USER_TEMP_DIR (as well as _CS_DARWIN_USER_CACHE_DIR) is not + // documented in the confstr() man page at + // https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/confstr.3.html#//apple_ref/doc/man/3/confstr + // but are still available, according to the WebKit patches at + // https://trac.webkit.org/changeset/262004/webkit + // https://trac.webkit.org/changeset/263705/webkit + // + // The confstr() implementation falls back to getenv("TMPDIR"). See + // https://opensource.apple.com/source/Libc/Libc-1439.100.3/gen/confstr.c.auto.html + ::confstr(_CS_DARWIN_USER_TEMP_DIR, user_temp_dir, sizeof(user_temp_dir)); + + name_template = user_temp_dir; + if (name_template.back() != GTEST_PATH_SEP_[0]) + name_template.push_back(GTEST_PATH_SEP_[0]); # else - char name_template[] = "/tmp/captured_stream.XXXXXX"; + name_template = "/tmp/"; # endif - const int captured_fd = mkstemp(name_template); + name_template.append("gtest_captured_stream.XXXXXX"); + + // mkstemp() modifies the string bytes in place, and does not go beyond the + // string's length. This results in well-defined behavior in C++17. + // + // The const_cast is needed below C++17. The constraints on std::string + // implementations in C++11 and above make assumption behind the const_cast + // fairly safe. + const int captured_fd = ::mkstemp(const_cast<char*>(name_template.data())); if (captured_fd == -1) { GTEST_LOG_(WARNING) << "Failed to create tmp file " << name_template << " for test; does the test have access to the /tmp directory?"; } - filename_ = name_template; + filename_ = std::move(name_template); # endif // GTEST_OS_WINDOWS fflush(nullptr); dup2(captured_fd, fd_); |