| 1 |
// Copyright (c) 2023, Google Inc. |
| 2 |
// SPDX-License-Identifier: ISC |
| 3 |
|
| 4 |
#include "file_util.h" |
| 5 |
|
| 6 |
#include <stdlib.h> |
| 7 |
|
| 8 |
#if defined(OPENSSL_WINDOWS) |
| 9 |
OPENSSL_MSVC_PRAGMA(warning(push, 3)) |
| 10 |
#include <windows.h> |
| 11 |
OPENSSL_MSVC_PRAGMA(warning(pop)) |
| 12 |
#else |
| 13 |
#include <fcntl.h> |
| 14 |
#include <sys/stat.h> |
| 15 |
#include <unistd.h> |
| 16 |
#endif |
| 17 |
|
| 18 |
#include <openssl/rand.h> |
| 19 |
|
| 20 |
#include "test_util.h" |
| 21 |
|
| 22 |
|
| 23 |
#if defined(OPENSSL_WINDOWS) |
| 24 |
static void PrintLastError(const char *s) { |
| 25 |
DWORD error = GetLastError(); |
| 26 |
char *buffer; |
| 27 |
DWORD len = FormatMessageA( |
| 28 |
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, 0, error, 0, |
| 29 |
reinterpret_cast<char *>(&buffer), 0, nullptr); |
| 30 |
std::string msg = "unknown error"; |
| 31 |
if (len > 0) { |
| 32 |
msg.assign(buffer, len); |
| 33 |
while (!msg.empty() && (msg.back() == '\r' || msg.back() == '\n')) { |
| 34 |
msg.resize(msg.size() - 1); |
| 35 |
} |
| 36 |
} |
| 37 |
LocalFree(buffer); |
| 38 |
fprintf(stderr, "%s: %s (0x%lx)\n", s, msg.c_str(), error); |
| 39 |
} |
| 40 |
#endif // OPENSSL_WINDOWS |
| 41 |
|
| 42 |
// GetTempDir returns the path to the temporary directory, or the empty string |
| 43 |
// on error. On success, the result will include the directory separator. |
| 44 |
static std::string GetTempDir() { |
| 45 |
#if defined(OPENSSL_WINDOWS) |
| 46 |
char buf[MAX_PATH + 1]; |
| 47 |
DWORD len = GetTempPathA(sizeof(buf), buf); |
| 48 |
return std::string(buf, len); |
| 49 |
#else |
| 50 |
const char *tmpdir = getenv("TMPDIR"); |
| 51 |
if (tmpdir != nullptr && *tmpdir != '\0') { |
| 52 |
std::string ret = tmpdir; |
| 53 |
if (ret.back() != '/') { |
| 54 |
ret.push_back('/'); |
| 55 |
} |
| 56 |
return ret; |
| 57 |
} |
| 58 |
#if defined(OPENSSL_ANDROID) |
| 59 |
return "/data/local/tmp/"; |
| 60 |
#else |
| 61 |
return "/tmp/"; |
| 62 |
#endif |
| 63 |
#endif |
| 64 |
} |
| 65 |
|
| 66 |
bool SkipTempFileTests() { |
| 67 |
#if defined(OPENSSL_ANDROID) |
| 68 |
// When running in an APK context, /data/local/tmp is unreadable. Android |
| 69 |
// versions before https://android-review.googlesource.com/c/1821337 do not |
| 70 |
// set TMPDIR to a suitable replacement. |
| 71 |
if (getenv("TMPDIR") == nullptr) { |
| 72 |
static bool should_skip = [] { |
| 73 |
TemporaryFile file; |
| 74 |
return !file.Init(); |
| 75 |
}(); |
| 76 |
if (should_skip) { |
| 77 |
fprintf(stderr, "Skipping tests with temporary files.\n"); |
| 78 |
return true; |
| 79 |
} |
| 80 |
} |
| 81 |
#endif |
| 82 |
return false; |
| 83 |
} |
| 84 |
|
| 85 |
TemporaryFile::~TemporaryFile() { |
| 86 |
#if defined(OPENSSL_WINDOWS) |
| 87 |
if (!path_.empty() && !DeleteFileA(path_.c_str())) { |
| 88 |
PrintLastError("Could not delete file"); |
| 89 |
} |
| 90 |
#else |
| 91 |
if (!path_.empty() && unlink(path_.c_str()) != 0) { |
| 92 |
perror("Could not delete file"); |
| 93 |
} |
| 94 |
#endif |
| 95 |
} |
| 96 |
|
| 97 |
bool TemporaryFile::Init(bssl::Span<const uint8_t> content) { |
| 98 |
std::string temp_dir = GetTempDir(); |
| 99 |
if (temp_dir.empty()) { |
| 100 |
return false; |
| 101 |
} |
| 102 |
|
| 103 |
#if defined(OPENSSL_WINDOWS) |
| 104 |
char path[MAX_PATH]; |
| 105 |
if (GetTempFileNameA(temp_dir.c_str(), "bssl", |
| 106 |
/*uUnique=*/0, path) == 0) { |
| 107 |
PrintLastError("Could not create temporary"); |
| 108 |
return false; |
| 109 |
} |
| 110 |
path_ = path; |
| 111 |
#else |
| 112 |
std::string path = temp_dir + "bssl_tmp_file.XXXXXX"; |
| 113 |
// TODO(davidben): Use |path.data()| when we require C++17. |
| 114 |
mode_t prev_umask = umask(S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); |
| 115 |
int fd = mkstemp(&path[0]); |
| 116 |
umask(prev_umask); |
| 117 |
if (fd < 0) { |
| 118 |
perror("Could not create temporary file"); |
| 119 |
return false; |
| 120 |
} |
| 121 |
close(fd); |
| 122 |
path_ = std::move(path); |
| 123 |
#endif |
| 124 |
|
| 125 |
ScopedFILE file = Open("wb"); |
| 126 |
if (file == nullptr) { |
| 127 |
perror("Could not open temporary file"); |
| 128 |
return false; |
| 129 |
} |
| 130 |
if (!content.empty() && |
| 131 |
fwrite(content.data(), content.size(), /*nitems=*/1, file.get()) != 1) { |
| 132 |
perror("Could not write temporary file"); |
| 133 |
return false; |
| 134 |
} |
| 135 |
return true; |
| 136 |
} |
| 137 |
|
| 138 |
ScopedFILE TemporaryFile::Open(const char *mode) const { |
| 139 |
if (path_.empty()) { |
| 140 |
return nullptr; |
| 141 |
} |
| 142 |
return ScopedFILE(fopen(path_.c_str(), mode)); |
| 143 |
} |
| 144 |
|
| 145 |
ScopedFD TemporaryFile::OpenFD(int flags) const { |
| 146 |
if (path_.empty()) { |
| 147 |
return ScopedFD(); |
| 148 |
} |
| 149 |
#if defined(OPENSSL_WINDOWS) |
| 150 |
return ScopedFD(_open(path_.c_str(), flags)); |
| 151 |
#else |
| 152 |
return ScopedFD(open(path_.c_str(), flags)); |
| 153 |
#endif |
| 154 |
} |