* commit '07522aae91efb55a658c8514162e1343fd28499b': libbase_test: win32: get some tests working
This commit is contained in:
commit
a121a81f5c
3 changed files with 39 additions and 22 deletions
|
|
@ -35,7 +35,6 @@ class TemporaryFile {
|
||||||
DISALLOW_COPY_AND_ASSIGN(TemporaryFile);
|
DISALLOW_COPY_AND_ASSIGN(TemporaryFile);
|
||||||
};
|
};
|
||||||
|
|
||||||
#if !defined(_WIN32)
|
|
||||||
class TemporaryDir {
|
class TemporaryDir {
|
||||||
public:
|
public:
|
||||||
TemporaryDir();
|
TemporaryDir();
|
||||||
|
|
@ -48,6 +47,5 @@ class TemporaryDir {
|
||||||
|
|
||||||
DISALLOW_COPY_AND_ASSIGN(TemporaryDir);
|
DISALLOW_COPY_AND_ASSIGN(TemporaryDir);
|
||||||
};
|
};
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif // TEST_UTILS_H
|
#endif // TEST_UTILS_H
|
||||||
|
|
|
||||||
|
|
@ -20,14 +20,11 @@
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
// The z size sepcifier isn't supported on Windows, so this test isn't useful.
|
|
||||||
#if !defined(_WIN32)
|
|
||||||
TEST(StringPrintfTest, HexSizeT) {
|
TEST(StringPrintfTest, HexSizeT) {
|
||||||
size_t size = 0x00107e59;
|
size_t size = 0x00107e59;
|
||||||
EXPECT_EQ("00107e59", android::base::StringPrintf("%08zx", size));
|
EXPECT_EQ("00107e59", android::base::StringPrintf("%08zx", size));
|
||||||
EXPECT_EQ("0x00107e59", android::base::StringPrintf("0x%08zx", size));
|
EXPECT_EQ("0x00107e59", android::base::StringPrintf("0x%08zx", size));
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
TEST(StringPrintfTest, StringAppendF) {
|
TEST(StringPrintfTest, StringAppendF) {
|
||||||
std::string s("a");
|
std::string s("a");
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,9 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "base/logging.h"
|
||||||
#include "base/test_utils.h"
|
#include "base/test_utils.h"
|
||||||
|
#include "utils/Compat.h" // For OS_PATH_SEPARATOR.
|
||||||
|
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
@ -24,17 +26,47 @@
|
||||||
|
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
|
#include <direct.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
int mkstemp(char* template_name) {
|
||||||
|
if (_mktemp(template_name) == nullptr) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
// Use open() to match the close() that TemporaryFile's destructor does.
|
||||||
|
// Note that on Windows, this does CR/LF translation and _setmode() should
|
||||||
|
// be used to change that if appropriate.
|
||||||
|
return open(template_name, O_CREAT | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR);
|
||||||
|
}
|
||||||
|
|
||||||
|
char* mkdtemp(char* template_name) {
|
||||||
|
if (_mktemp(template_name) == nullptr) {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
if (_mkdir(template_name) == -1) {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
return template_name;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
static std::string GetSystemTempDir() {
|
static std::string GetSystemTempDir() {
|
||||||
#if defined(__ANDROID__)
|
#if defined(__ANDROID__)
|
||||||
return "/data/local/tmp";
|
return "/data/local/tmp";
|
||||||
#elif defined(_WIN32)
|
#elif defined(_WIN32)
|
||||||
char wd[MAX_PATH] = {};
|
char tmp_dir[MAX_PATH];
|
||||||
_getcwd(wd, sizeof(wd));
|
DWORD result = GetTempPathA(sizeof(tmp_dir), tmp_dir);
|
||||||
return wd;
|
CHECK_NE(result, 0ul) << "GetTempPathA failed, error: " << GetLastError();
|
||||||
|
CHECK_LT(result, sizeof(tmp_dir)) << "path truncated to: " << result;
|
||||||
|
|
||||||
|
// GetTempPath() returns a path with a trailing slash, but init()
|
||||||
|
// does not expect that, so remove it.
|
||||||
|
CHECK_EQ(tmp_dir[result - 1], '\\');
|
||||||
|
tmp_dir[result - 1] = '\0';
|
||||||
|
return tmp_dir;
|
||||||
#else
|
#else
|
||||||
return "/tmp";
|
return "/tmp";
|
||||||
#endif
|
#endif
|
||||||
|
|
@ -50,21 +82,11 @@ TemporaryFile::~TemporaryFile() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void TemporaryFile::init(const std::string& tmp_dir) {
|
void TemporaryFile::init(const std::string& tmp_dir) {
|
||||||
snprintf(path, sizeof(path), "%s/TemporaryFile-XXXXXX", tmp_dir.c_str());
|
snprintf(path, sizeof(path), "%s%cTemporaryFile-XXXXXX", tmp_dir.c_str(),
|
||||||
#if !defined(_WIN32)
|
OS_PATH_SEPARATOR);
|
||||||
fd = mkstemp(path);
|
fd = mkstemp(path);
|
||||||
#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(path) == nullptr) {
|
|
||||||
abort();
|
|
||||||
}
|
|
||||||
|
|
||||||
fd = open(path, O_RDWR | O_NOINHERIT | O_CREAT, _S_IREAD | _S_IWRITE);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#if !defined(_WIN32)
|
|
||||||
TemporaryDir::TemporaryDir() {
|
TemporaryDir::TemporaryDir() {
|
||||||
init(GetSystemTempDir());
|
init(GetSystemTempDir());
|
||||||
}
|
}
|
||||||
|
|
@ -74,7 +96,7 @@ TemporaryDir::~TemporaryDir() {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool TemporaryDir::init(const std::string& tmp_dir) {
|
bool TemporaryDir::init(const std::string& tmp_dir) {
|
||||||
snprintf(path, sizeof(path), "%s/TemporaryDir-XXXXXX", tmp_dir.c_str());
|
snprintf(path, sizeof(path), "%s%cTemporaryDir-XXXXXX", tmp_dir.c_str(),
|
||||||
|
OS_PATH_SEPARATOR);
|
||||||
return (mkdtemp(path) != nullptr);
|
return (mkdtemp(path) != nullptr);
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue