blob: b9d4b92173d04cd00419d3cf7cf670fc3ff20d11 [file] [log] [blame]
/* Copyright 2015 The BoringSSL Authors
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
#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_view>
#include <vector>
#include <gtest/gtest.h>
#include <openssl/span.h>
#include "../internal.h"
// hexdump writes |msg| to |fp| followed by the hex encoding of |len| bytes
// from |in|.
void hexdump(FILE *fp, const char *msg, const void *in, size_t len);
// Bytes is a wrapper over a byte slice which may be compared for equality. This
// allows it to be used in EXPECT_EQ macros.
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(std::string_view str) : span_(bssl::StringAsBytes(str)) {}
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); }
// Declassified returns a declassified copy of some input.
inline std::vector<uint8_t> Declassified(bssl::Span<const uint8_t> in) {
std::vector<uint8_t> copy(in.begin(), in.end());
CONSTTIME_DECLASSIFY(copy.data(), copy.size());
return copy;
}
std::ostream &operator<<(std::ostream &os, const Bytes &in);
// DecodeHex decodes |in| from hexadecimal and writes the output to |out|. It
// returns true on success and false if |in| is not a valid hexadecimal byte
// string.
bool DecodeHex(std::vector<uint8_t> *out, const std::string &in);
// EncodeHex returns |in| encoded in hexadecimal.
std::string EncodeHex(bssl::Span<const uint8_t> in);
// ErrorEquals asserts that |err| is an error with library |lib| and reason
// |reason|.
testing::AssertionResult ErrorEquals(uint32_t err, int lib, int reason);
#endif // OPENSSL_HEADER_CRYPTO_TEST_TEST_UTIL_H