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
diff --git a/base/test_utils.cpp b/base/test_utils.cpp
index 1f6d3cf..0517bc7 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 @@
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
}