| 1 |
// Copyright (c) 2023, Google Inc. |
| 2 |
// SPDX-License-Identifier: ISC |
| 3 |
|
| 4 |
#ifndef OPENSSL_HEADER_CRYPTO_TEST_FILE_UTIL_H |
| 5 |
#define OPENSSL_HEADER_CRYPTO_TEST_FILE_UTIL_H |
| 6 |
|
| 7 |
#include <stdio.h> |
| 8 |
|
| 9 |
#include <memory> |
| 10 |
#include <string> |
| 11 |
#include <utility> |
| 12 |
|
| 13 |
#include <openssl/span.h> |
| 14 |
|
| 15 |
#if defined(OPENSSL_WINDOWS) |
| 16 |
#include <io.h> |
| 17 |
#else |
| 18 |
#include <unistd.h> |
| 19 |
#endif |
| 20 |
|
| 21 |
|
| 22 |
struct FileDeleter { |
| 23 |
void operator()(FILE *f) const { |
| 24 |
if (f != nullptr) { |
| 25 |
fclose(f); |
| 26 |
} |
| 27 |
} |
| 28 |
}; |
| 29 |
|
| 30 |
using ScopedFILE = std::unique_ptr<FILE, FileDeleter>; |
| 31 |
|
| 32 |
class ScopedFD { |
| 33 |
public: |
| 34 |
ScopedFD() = default; |
| 35 |
explicit ScopedFD(int fd) : fd_(fd) {} |
| 36 |
~ScopedFD() { reset(); } |
| 37 |
|
| 38 |
ScopedFD(ScopedFD &&other) noexcept { *this = std::move(other); } |
| 39 |
ScopedFD &operator=(ScopedFD&& other) { |
| 40 |
reset(other.release()); |
| 41 |
return *this; |
| 42 |
} |
| 43 |
|
| 44 |
ScopedFD(const ScopedFD &other) = delete; |
| 45 |
ScopedFD &operator=(ScopedFD& other) = delete; |
| 46 |
|
| 47 |
bool is_valid() const { return fd_ >= 0; } |
| 48 |
int get() const { return fd_; } |
| 49 |
|
| 50 |
int release() { |
| 51 |
int old_fd = fd_; |
| 52 |
fd_ = -1; |
| 53 |
return old_fd; |
| 54 |
} |
| 55 |
void reset(int fd = -1) { |
| 56 |
if (is_valid()) { |
| 57 |
#if defined(OPENSSL_WINDOWS) |
| 58 |
_close(fd_); |
| 59 |
#else |
| 60 |
close(fd_); |
| 61 |
#endif |
| 62 |
} |
| 63 |
fd_ = fd; |
| 64 |
} |
| 65 |
|
| 66 |
private: |
| 67 |
int fd_ = -1; |
| 68 |
}; |
| 69 |
|
| 70 |
// SkipTempFileTests returns true and prints a warning if tests involving |
| 71 |
// temporary files should be skipped because of platform issues. |
| 72 |
bool SkipTempFileTests(); |
| 73 |
|
| 74 |
// TemporaryFile manages a temporary file for testing. |
| 75 |
class TemporaryFile { |
| 76 |
public: |
| 77 |
TemporaryFile() = default; |
| 78 |
~TemporaryFile(); |
| 79 |
|
| 80 |
TemporaryFile(TemporaryFile&& other) noexcept { *this = std::move(other); } |
| 81 |
TemporaryFile& operator=(TemporaryFile&&other) { |
| 82 |
// Ensure |path_| is empty so it doesn't try to delete the File. |
| 83 |
auto old_other_path = other.path_; |
| 84 |
other.path_ = {}; |
| 85 |
path_ = old_other_path; |
| 86 |
return *this; |
| 87 |
} |
| 88 |
|
| 89 |
TemporaryFile(const TemporaryFile&) = delete; |
| 90 |
TemporaryFile& operator=(const TemporaryFile&) = delete; |
| 91 |
|
| 92 |
// Init initializes the temporary file with the specified content. It returns |
| 93 |
// true on success and false on error. On error, callers should call |
| 94 |
// |IgnoreTempFileErrors| to determine whether to ignore the error. |
| 95 |
bool Init(bssl::Span<const uint8_t> content = {}); |
| 96 |
bool Init(const std::string &content) { |
| 97 |
return Init(bssl::MakeConstSpan( |
| 98 |
reinterpret_cast<const uint8_t *>(content.data()), content.size())); |
| 99 |
} |
| 100 |
|
| 101 |
// Open opens the file as a |FILE| with the specified mode. |
| 102 |
ScopedFILE Open(const char *mode) const; |
| 103 |
|
| 104 |
// Open opens the file as a file descriptor with the specified flags. |
| 105 |
ScopedFD OpenFD(int flags) const; |
| 106 |
|
| 107 |
// path returns the path to the temporary file. |
| 108 |
const std::string &path() const { return path_; } |
| 109 |
|
| 110 |
private: |
| 111 |
std::string path_; |
| 112 |
}; |
| 113 |
|
| 114 |
#endif // OPENSSL_HEADER_CRYPTO_TEST_FILE_UTIL_H |