| 1 |
// Copyright (c) 2015, Google Inc. |
| 2 |
// SPDX-License-Identifier: ISC |
| 3 |
|
| 4 |
#ifndef OPENSSL_HEADER_CRYPTO_TEST_TEST_UTIL_H |
| 5 |
#define OPENSSL_HEADER_CRYPTO_TEST_TEST_UTIL_H |
| 6 |
|
| 7 |
#include <stddef.h> |
| 8 |
#include <stdint.h> |
| 9 |
#include <stdio.h> |
| 10 |
#include <string.h> |
| 11 |
|
| 12 |
#include <iosfwd> |
| 13 |
#include <string> |
| 14 |
#include <vector> |
| 15 |
|
| 16 |
#include <gtest/gtest.h> |
| 17 |
|
| 18 |
#include <openssl/span.h> |
| 19 |
|
| 20 |
#include "../internal.h" |
| 21 |
|
| 22 |
|
| 23 |
// hexdump writes |msg| to |fp| followed by the hex encoding of |len| bytes |
| 24 |
// from |in|. |
| 25 |
void hexdump(FILE *fp, const char *msg, const void *in, size_t len); |
| 26 |
|
| 27 |
// Bytes is a wrapper over a byte slice which may be compared for equality. This |
| 28 |
// allows it to be used in EXPECT_EQ macros. |
| 29 |
struct Bytes { |
| 30 |
Bytes(const uint8_t *data_arg, size_t len_arg) : span_(data_arg, len_arg) {} |
| 31 |
Bytes(const char *data_arg, size_t len_arg) |
| 32 |
: span_(reinterpret_cast<const uint8_t *>(data_arg), len_arg) {} |
| 33 |
|
| 34 |
explicit Bytes(const char *str) |
| 35 |
: span_(reinterpret_cast<const uint8_t *>(str), strlen(str)) {} |
| 36 |
explicit Bytes(const std::string &str) |
| 37 |
: span_(reinterpret_cast<const uint8_t *>(str.data()), str.size()) {} |
| 38 |
explicit Bytes(bssl::Span<const uint8_t> span) : span_(span) {} |
| 39 |
|
| 40 |
bssl::Span<const uint8_t> span_; |
| 41 |
}; |
| 42 |
|
| 43 |
inline bool operator==(const Bytes &a, const Bytes &b) { |
| 44 |
return a.span_ == b.span_; |
| 45 |
} |
| 46 |
|
| 47 |
inline bool operator!=(const Bytes &a, const Bytes &b) { return !(a == b); } |
| 48 |
|
| 49 |
std::ostream &operator<<(std::ostream &os, const Bytes &in); |
| 50 |
|
| 51 |
// DecodeHex decodes |in| from hexadecimal and writes the output to |out|. It |
| 52 |
// returns true on success and false if |in| is not a valid hexadecimal byte |
| 53 |
// string. |
| 54 |
bool DecodeHex(std::vector<uint8_t> *out, const std::string &in); |
| 55 |
|
| 56 |
// HexToBytes decodes |str| from hexadecimal and returns a new vector of bytes. |
| 57 |
// If |str| is invalid it aborts. |
| 58 |
std::vector<uint8_t> HexToBytes(const char *str); |
| 59 |
|
| 60 |
// EncodeHex returns |in| encoded in hexadecimal. |
| 61 |
std::string EncodeHex(bssl::Span<const uint8_t> in); |
| 62 |
|
| 63 |
// CertFromPEM parses the given, NUL-terminated pem block and returns an |
| 64 |
// |X509*|. |
| 65 |
bssl::UniquePtr<X509> CertFromPEM(const char *pem); |
| 66 |
|
| 67 |
// CertsToStack converts a vector of |X509*| to an OpenSSL STACK_OF(X509), |
| 68 |
// bumping the reference counts for each certificate in question. |
| 69 |
bssl::UniquePtr<STACK_OF(X509)> CertsToStack(const std::vector<X509 *> &certs); |
| 70 |
|
| 71 |
// RSAFromPEM parses the given, NUL-terminated pem block and returns an |
| 72 |
// |RSA*|. |
| 73 |
bssl::UniquePtr<RSA> RSAFromPEM(const char *pem); |
| 74 |
|
| 75 |
// Helper function that: |
| 76 |
// 1. Creates a BIO |
| 77 |
// 2. Reads the provided |pem_string| into bio |
| 78 |
// 3. Reads the PEM into DER encoding |
| 79 |
// 4. Returns the DER data and length |
| 80 |
bool PEM_to_DER(const char *pem_str, uint8_t **out_der, long *out_der_len); |
| 81 |
|
| 82 |
// kReferenceTime is the reference time used by certs created by |MakeTestCert|. |
| 83 |
// It is the unix timestamp for Sep 27th, 2016. |
| 84 |
static const int64_t kReferenceTime = 1474934400; |
| 85 |
|
| 86 |
// MakeTestCert creates an X509 certificate for use in testing. It is configured |
| 87 |
// to be valid from 1 day prior |kReferenceTime| until 1 day after |
| 88 |
// |kReferenceTime|. |
| 89 |
bssl::UniquePtr<X509> MakeTestCert(const char *issuer, |
| 90 |
const char *subject, EVP_PKEY *key, |
| 91 |
bool is_ca); |
| 92 |
|
| 93 |
// unique_ptr will automatically call fclose on the file descriptior when the |
| 94 |
// variable goes out of scope, so we need to specify BIO_NOCLOSE close flags |
| 95 |
// to avoid a double-free condition. |
| 96 |
struct TempFileCloser { |
| 97 |
void operator()(FILE *f) const { fclose(f); } |
| 98 |
}; |
| 99 |
|
| 100 |
using TempFILE = std::unique_ptr<FILE, TempFileCloser>; |
| 101 |
|
| 102 |
#if defined(OPENSSL_WINDOWS) |
| 103 |
#include <windows.h> |
| 104 |
#ifndef PATH_MAX |
| 105 |
#define PATH_MAX MAX_PATH |
| 106 |
#endif |
| 107 |
#else |
| 108 |
#include <limits.h> |
| 109 |
#endif |
| 110 |
|
| 111 |
size_t createTempFILEpath(char buffer[PATH_MAX]); |
| 112 |
FILE* createRawTempFILE(); |
| 113 |
TempFILE createTempFILE(); |
| 114 |
size_t createTempDirPath(char buffer[PATH_MAX]); |
| 115 |
|
| 116 |
#if defined(OPENSSL_WINDOWS) |
| 117 |
// On Windows, antivirus software (e.g. Windows Defender), file indexing |
| 118 |
// services, or other background processes can briefly lock files after they are |
| 119 |
// created or modified. This causes transient ERROR_SHARING_VIOLATION failures |
| 120 |
// when a test immediately tries to reopen the file. This helper retries |
| 121 |
// opening the file for reading in a loop to wait out the lock. |
| 122 |
testing::AssertionResult WaitForFileAccessible(const char *path); |
| 123 |
#endif |
| 124 |
|
| 125 |
// Returns true if operating system is Amazon Linux and false otherwise. |
| 126 |
// Determined at run-time and requires read-permissions to /etc. |
| 127 |
bool osIsAmazonLinux(void); |
| 128 |
|
| 129 |
// Executes |testFunc| simultaneously in |numberThreads| number of threads. If |
| 130 |
// OPENSSL_THREADS is not defined, executes |testFunc| a single time |
| 131 |
// non-concurrently. |
| 132 |
bool threadTest(const size_t numberOfThreads, |
| 133 |
std::function<void(bool*)> testFunc); |
| 134 |
|
| 135 |
bool forkAndRunTest(std::function<bool()> child_func, |
| 136 |
std::function<bool()> parent_func); |
| 137 |
|
| 138 |
void maybeDisableSomeForkUbeDetectMechanisms(void); |
| 139 |
bool runtimeEmulationIsIntelSde(void); |
| 140 |
bool addressSanitizerIsEnabled(void); |
| 141 |
|
| 142 |
// CustomData is for testing new structs that we add support for |ex_data|. |
| 143 |
typedef struct { |
| 144 |
int custom_data; |
| 145 |
} CustomData; |
| 146 |
|
| 147 |
void CustomDataFree(void *parent, void *ptr, CRYPTO_EX_DATA *ad, |
| 148 |
int index, long argl, void *argp); |
| 149 |
// ErrorEquals asserts that |err| is an error with library |lib| and reason |
| 150 |
// |reason|. |
| 151 |
testing::AssertionResult ErrorEquals(uint32_t err, int lib, int reason); |
| 152 |
|
| 153 |
// HexToBIGNUM decodes |hex| as a hexadecimal, big-endian, unsigned integer and |
| 154 |
// returns it as a |BIGNUM|, or nullptr on error. |
| 155 |
bssl::UniquePtr<BIGNUM> HexToBIGNUM(const char *hex); |
| 156 |
|
| 157 |
// ExpectParse does a d2i parse using the corresponding template and function |
| 158 |
// pointer. |
| 159 |
template <typename T> |
| 160 |
void ExpectParse(T *(*d2i)(T **, const uint8_t **, long), |
| 161 |
const std::vector<uint8_t> &in, bool expected) { |
| 162 |
SCOPED_TRACE(Bytes(in)); |
| 163 |
const uint8_t *ptr = in.data(); |
| 164 |
bssl::UniquePtr<T> obj(d2i(nullptr, &ptr, in.size())); |
| 165 |
if (expected) { |
| 166 |
EXPECT_TRUE(obj); |
| 167 |
} else { |
| 168 |
EXPECT_FALSE(obj); |
| 169 |
uint32_t err = ERR_get_error(); |
| 170 |
EXPECT_EQ(ERR_LIB_ASN1, ERR_GET_LIB(err)); |
| 171 |
ERR_clear_error(); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
#endif // OPENSSL_HEADER_CRYPTO_TEST_TEST_UTIL_H |