diff options
author | Dan Albert <danalbert@google.com> | 2015-04-29 11:32:23 -0700 |
---|---|---|
committer | Dan Albert <danalbert@google.com> | 2015-04-29 17:11:28 -0700 |
commit | 0c4b3a319d2bc3dbc917e869af34e24788e6fc48 (patch) | |
tree | eae38a44691e9d19114aa508fc419d9c27ce2278 /base/test_utils.cpp | |
parent | 850188fc040e8b1f345359f795165c7925617506 (diff) |
Get libbase tests working on Windows.
Tests using files from /proc still fail on Windows (obviously), but
all tests are passing when run in Wine.
Change-Id: Ie4c3ba65b642202f8fcaec73332a53bee6115fba
Diffstat (limited to 'base/test_utils.cpp')
-rw-r--r-- | base/test_utils.cpp | 27 |
1 files changed, 24 insertions, 3 deletions
diff --git a/base/test_utils.cpp b/base/test_utils.cpp index 1f6d3cfff7..0517bc7138 100644 --- a/base/test_utils.cpp +++ b/base/test_utils.cpp @@ -16,15 +16,26 @@ #include "test_utils.h" +#include <fcntl.h> #include <stdio.h> #include <stdlib.h> +#include <sys/stat.h> #include <unistd.h> +#if defined(_WIN32) +#include <windows.h> +#endif + TemporaryFile::TemporaryFile() { +#if defined(__ANDROID__) init("/data/local/tmp"); - if (fd == -1) { - init("/tmp"); - } +#elif defined(_WIN32) + char wd[MAX_PATH] = {}; + _getcwd(wd, sizeof(wd)); + init(wd); +#else + init("/tmp"); +#endif } TemporaryFile::~TemporaryFile() { @@ -34,5 +45,15 @@ TemporaryFile::~TemporaryFile() { void TemporaryFile::init(const char* tmp_dir) { snprintf(filename, sizeof(filename), "%s/TemporaryFile-XXXXXX", tmp_dir); +#if !defined(_WIN32) fd = mkstemp(filename); +#else + // Windows doesn't have mkstemp, and tmpfile creates the file in the root + // directory, requiring root (?!) permissions. We have to settle for mktemp. + if (mktemp(filename) == nullptr) { + abort(); + } + + fd = open(filename, O_RDWR | O_NOINHERIT | O_CREAT, _S_IREAD | _S_IWRITE); +#endif } |