#ifndef OPENSSL_HEADER_CRYPTO_TEST_TEST_UTIL_H
#define OPENSSL_HEADER_CRYPTO_TEST_TEST_UTIL_H
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <iosfwd>
#include <string>
#include <vector>
#include <gtest/gtest.h>
#include <openssl/span.h>
#include "../internal.h"
void hexdump(FILE *fp, const char *msg, const void *in, size_t len);
struct Bytes {
Bytes(const uint8_t *data_arg, size_t len_arg) : span_(data_arg, len_arg) {}
Bytes(const char *data_arg, size_t len_arg)
: span_(reinterpret_cast<const uint8_t *>(data_arg), len_arg) {}
explicit Bytes(const char *str)
: span_(reinterpret_cast<const uint8_t *>(str), strlen(str)) {}
explicit Bytes(const std::string &str)
: span_(reinterpret_cast<const uint8_t *>(str.data()), str.size()) {}
explicit Bytes(bssl::Span<const uint8_t> span) : span_(span) {}
bssl::Span<const uint8_t> span_;
};
inline bool operator==(const Bytes &a, const Bytes &b) {
return a.span_ == b.span_;
}
inline bool operator!=(const Bytes &a, const Bytes &b) { return !(a == b); }
std::ostream &operator<<(std::ostream &os, const Bytes &in);
bool DecodeHex(std::vector<uint8_t> *out, const std::string &in);
std::vector<uint8_t> HexToBytes(const char *str);
std::string EncodeHex(bssl::Span<const uint8_t> in);
bssl::UniquePtr<X509> CertFromPEM(const char *pem);
bssl::UniquePtr<STACK_OF(X509)> CertsToStack(const std::vector<X509 *> &certs);
bssl::UniquePtr<RSA> RSAFromPEM(const char *pem);
bool PEM_to_DER(const char *pem_str, uint8_t **out_der, long *out_der_len);
static const int64_t kReferenceTime = 1474934400;
bssl::UniquePtr<X509> MakeTestCert(const char *issuer,
const char *subject, EVP_PKEY *key,
bool is_ca);
struct TempFileCloser {
void operator()(FILE *f) const { fclose(f); }
};
using TempFILE = std::unique_ptr<FILE, TempFileCloser>;
#if defined(OPENSSL_WINDOWS)
#include <windows.h>
#ifndef PATH_MAX
#define PATH_MAX MAX_PATH
#endif
#else
#include <limits.h>
#endif
size_t createTempFILEpath(char buffer[PATH_MAX]);
FILE* createRawTempFILE();
TempFILE createTempFILE();
size_t createTempDirPath(char buffer[PATH_MAX]);
#if defined(OPENSSL_WINDOWS)
testing::AssertionResult WaitForFileAccessible(const char *path);
#endif
bool osIsAmazonLinux(void);
bool threadTest(const size_t numberOfThreads,
std::function<void(bool*)> testFunc);
bool forkAndRunTest(std::function<bool()> child_func,
std::function<bool()> parent_func);
void maybeDisableSomeForkDetectMechanisms(void);
bool runtimeEmulationIsIntelSde(void);
bool addressSanitizerIsEnabled(void);
typedef struct {
int custom_data;
} CustomData;
void CustomDataFree(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
int index, long argl, void *argp);
testing::AssertionResult ErrorEquals(uint32_t err, int lib, int reason);
template <typename T>
void ExpectParse(T *(*d2i)(T **, const uint8_t **, long),
const std::vector<uint8_t> &in, bool expected) {
SCOPED_TRACE(Bytes(in));
const uint8_t *ptr = in.data();
bssl::UniquePtr<T> obj(d2i(nullptr, &ptr, in.size()));
if (expected) {
EXPECT_TRUE(obj);
} else {
EXPECT_FALSE(obj);
uint32_t err = ERR_get_error();
EXPECT_EQ(ERR_LIB_ASN1, ERR_GET_LIB(err));
ERR_clear_error();
}
}
#endif