summaryrefslogtreecommitdiff
path: root/base
diff options
context:
space:
mode:
authorDan Albert <danalbert@google.com>2015-04-29 11:32:23 -0700
committerDan Albert <danalbert@google.com>2015-04-29 17:11:28 -0700
commit0c4b3a319d2bc3dbc917e869af34e24788e6fc48 (patch)
treeeae38a44691e9d19114aa508fc419d9c27ce2278 /base
parent850188fc040e8b1f345359f795165c7925617506 (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')
-rw-r--r--base/file_test.cpp4
-rw-r--r--base/stringprintf_test.cpp3
-rw-r--r--base/test_main.cpp5
-rw-r--r--base/test_utils.cpp27
4 files changed, 36 insertions, 3 deletions
diff --git a/base/file_test.cpp b/base/file_test.cpp
index a7f070d75..cbb275130 100644
--- a/base/file_test.cpp
+++ b/base/file_test.cpp
@@ -55,6 +55,9 @@ TEST(file, WriteStringToFile) {
EXPECT_EQ("abc", s);
}
+// WriteStringToFile2 is explicitly for setting Unix permissions, which make no
+// sense on Windows.
+#if !defined(_WIN32)
TEST(file, WriteStringToFile2) {
TemporaryFile tf;
ASSERT_TRUE(tf.fd != -1);
@@ -71,6 +74,7 @@ TEST(file, WriteStringToFile2) {
<< strerror(errno);
EXPECT_EQ("abc", s);
}
+#endif
TEST(file, WriteStringToFd) {
TemporaryFile tf;
diff --git a/base/stringprintf_test.cpp b/base/stringprintf_test.cpp
index 5cc20864f..54b2b6c9e 100644
--- a/base/stringprintf_test.cpp
+++ b/base/stringprintf_test.cpp
@@ -20,11 +20,14 @@
#include <string>
+// The z size sepcifier isn't supported on Windows, so this test isn't useful.
+#if !defined(_WIN32)
TEST(StringPrintfTest, HexSizeT) {
size_t size = 0x00107e59;
EXPECT_EQ("00107e59", android::base::StringPrintf("%08zx", size));
EXPECT_EQ("0x00107e59", android::base::StringPrintf("0x%08zx", size));
}
+#endif
TEST(StringPrintfTest, StringAppendF) {
std::string s("a");
diff --git a/base/test_main.cpp b/base/test_main.cpp
index 546923d9b..c362b6c51 100644
--- a/base/test_main.cpp
+++ b/base/test_main.cpp
@@ -20,6 +20,11 @@
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
+
+ // No logging on Windows yet.
+#if !defined(_WIN32)
android::base::InitLogging(argv, android::base::StderrLogger);
+#endif
+
return RUN_ALL_TESTS();
}
diff --git a/base/test_utils.cpp b/base/test_utils.cpp
index 1f6d3cfff..0517bc713 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
}