diff options
author | kjellander@webrtc.org <kjellander@webrtc.org@4adac7df-926f-26a2-2b94-8c16560cd09d> | 2014-04-16 08:04:26 +0000 |
---|---|---|
committer | kjellander@webrtc.org <kjellander@webrtc.org@4adac7df-926f-26a2-2b94-8c16560cd09d> | 2014-04-16 08:04:26 +0000 |
commit | 1ed7008c34205d4f606ed4436c3bdd8902c4428a (patch) | |
tree | 8a3c6c5e0a6bf9649400ed805d265431fd23615e /test | |
parent | feb904c475d5e8e420d5c9d495fb7dda9a4c119c (diff) | |
download | webrtc-1ed7008c34205d4f606ed4436c3bdd8902c4428a.tar.gz |
Remove use of tmpnam.
This solves compilation with the Mac SDK 10.9.
BUG=3120, 3151
TEST=git try -t modules_tests:VideoProcessorIntegrationTest*
R=fischman@webrtc.org, henrike@webrtc.org, stefan@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/10739005
git-svn-id: http://webrtc.googlecode.com/svn/trunk/webrtc@5917 4adac7df-926f-26a2-2b94-8c16560cd09d
Diffstat (limited to 'test')
-rw-r--r-- | test/testsupport/fileutils.cc | 40 | ||||
-rw-r--r-- | test/testsupport/fileutils.h | 9 | ||||
-rw-r--r-- | test/testsupport/fileutils_unittest.cc | 13 |
3 files changed, 56 insertions, 6 deletions
diff --git a/test/testsupport/fileutils.cc b/test/testsupport/fileutils.cc index c89f9bd1..9d04ab02 100644 --- a/test/testsupport/fileutils.cc +++ b/test/testsupport/fileutils.cc @@ -11,11 +11,18 @@ #include "webrtc/test/testsupport/fileutils.h" #ifdef WIN32 +#include <assert.h> #include <direct.h> +#include <tchar.h> +#include <windows.h> #include <algorithm> + +#include "webrtc/system_wrappers/interface/utf_util_win.h" #define GET_CURRENT_DIR _getcwd #else #include <unistd.h> + +#include "webrtc/system_wrappers/interface/scoped_ptr.h" #define GET_CURRENT_DIR getcwd #endif @@ -25,6 +32,7 @@ #endif #include <stdio.h> +#include <stdlib.h> #include <string.h> #include "webrtc/typedefs.h" // For architecture defines @@ -92,7 +100,7 @@ std::string OutputPathImpl() { return kFallbackPath; } path += kOutputDirName; - if (!CreateDirectory(path)) { + if (!CreateDir(path)) { return kFallbackPath; } return path + kPathDelimiter; @@ -154,7 +162,35 @@ std::string WorkingDir() { #endif // !WEBRTC_ANDROID -bool CreateDirectory(std::string directory_name) { +// Generate a temporary filename in a safe way. +// Largely copied from talk/base/{unixfilesystem,win32filesystem}.cc. +std::string TempFilename(const std::string &dir, const std::string &prefix) { +#ifdef WIN32 + wchar_t filename[MAX_PATH]; + if (::GetTempFileName(ToUtf16(dir).c_str(), + ToUtf16(prefix).c_str(), 0, filename) != 0) + return ToUtf8(filename); + assert(false); + return ""; +#else + int len = dir.size() + prefix.size() + 2 + 6; + scoped_ptr<char[]> tempname(new char[len]); + + snprintf(tempname.get(), len, "%s/%sXXXXXX", dir.c_str(), + prefix.c_str()); + int fd = ::mkstemp(tempname.get()); + if (fd == -1) { + assert(false); + return ""; + } else { + ::close(fd); + } + std::string ret(tempname.get()); + return ret; +#endif +} + +bool CreateDir(std::string directory_name) { struct stat path_info = {0}; // Check if the path exists already: if (stat(directory_name.c_str(), &path_info) == 0) { diff --git a/test/testsupport/fileutils.h b/test/testsupport/fileutils.h index d51bbde2..78789fa8 100644 --- a/test/testsupport/fileutils.h +++ b/test/testsupport/fileutils.h @@ -103,6 +103,10 @@ std::string ProjectRootPath(); // found, the current working directory ("./") is returned as a fallback. std::string OutputPath(); +// Generates an empty file with a unique name in the specified directory and +// returns the file name and path. +std::string TempFilename(const std::string &dir, const std::string &prefix); + // Returns a path to a resource file for the currently executing platform. // Adapts to what filenames are currently present in the // [project-root]/resources/ dir. @@ -132,7 +136,10 @@ std::string WorkingDir(); // Creates a directory if it not already exists. // Returns true if successful. Will print an error message to stderr and return // false if a file with the same name already exists. -bool CreateDirectory(std::string directory_name); +bool CreateDir(std::string directory_name); + +// Checks if a file exists. +bool FileExists(std::string& file_name); // File size of the supplied file in bytes. Will return 0 if the file is // empty or if the file does not exist/is readable. diff --git a/test/testsupport/fileutils_unittest.cc b/test/testsupport/fileutils_unittest.cc index c38e4533..4cd45137 100644 --- a/test/testsupport/fileutils_unittest.cc +++ b/test/testsupport/fileutils_unittest.cc @@ -46,7 +46,7 @@ class FileUtilsTest : public testing::Test { original_working_dir_ = webrtc::test::WorkingDir(); std::string resources_path = original_working_dir_ + kPathDelimiter + kResourcesDir + kPathDelimiter; - webrtc::test::CreateDirectory(resources_path); + webrtc::test::CreateDir(resources_path); files_.push_back(resources_path + kTestName + "." + kExtension); files_.push_back(resources_path + kTestName + "_32." + kExtension); @@ -117,12 +117,19 @@ TEST_F(FileUtilsTest, DISABLED_ON_ANDROID(OutputPathFromRootWorkingDir)) { ASSERT_EQ("./", webrtc::test::OutputPath()); } +TEST_F(FileUtilsTest, DISABLED_ON_ANDROID(TempFilename)) { + std::string temp_filename = webrtc::test::TempFilename( + webrtc::test::OutputPath(), "TempFilenameTest"); + ASSERT_TRUE(webrtc::test::FileExists(temp_filename)); + remove(temp_filename.c_str()); +} + // Only tests that the code executes -TEST_F(FileUtilsTest, CreateDirectory) { +TEST_F(FileUtilsTest, CreateDir) { std::string directory = "fileutils-unittest-empty-dir"; // Make sure it's removed if a previous test has failed: remove(directory.c_str()); - ASSERT_TRUE(webrtc::test::CreateDirectory(directory)); + ASSERT_TRUE(webrtc::test::CreateDir(directory)); remove(directory.c_str()); } |