boringssl/crypto/test/test_util.h
David Benjamin 3ecd0a5fca Convert aes_test to GTest.
This introduces machinery to start embedding the test data files into
the crypto_test binary. Figuring out every CI's test data story is more
trouble than is worth it. The GTest FileTest runner is considerably
different from the old one:

- It returns void and expects failures to use the GTest EXPECT_* and
  ASSERT_* macros, rather than ExpectBytesEqual. This is more monkey
  work to convert, but ultimately less work to add new tests. I think
  it's also valuable for our FileTest and normal test patterns to align
  as much as possible. The line number is emitted via SCOPED_TRACE.

- I've intentionally omitted the Error attribute handling, since that
  doesn't work very well with the new callback. This means evp_test.cc
  will take a little more work to convert, but this is again to keep our
  two test patterns aligned.

- The callback takes a std::function rather than a C-style void pointer.
  This means we can go nuts with lambdas. It also places the path first
  so clang-format doesn't go nuts.

BUG=129

Change-Id: I0d1920a342b00e64043e3ea05f5f5af57bfe77b3
Reviewed-on: https://boringssl-review.googlesource.com/16507
Reviewed-by: Adam Langley <agl@google.com>
2017-05-23 22:33:25 +00:00

66 lines
2.3 KiB
C++

/* Copyright (c) 2015, Google Inc.
*
* 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>
#include <vector>
#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)
: data(data_arg), len(len_arg) {}
Bytes(const char *data_arg, size_t len_arg)
: data(reinterpret_cast<const uint8_t *>(data_arg)), len(len_arg) {}
explicit Bytes(const char *str)
: data(reinterpret_cast<const uint8_t *>(str)), len(strlen(str)) {}
explicit Bytes(const std::string &str)
: data(reinterpret_cast<const uint8_t *>(str.data())), len(str.size()) {}
explicit Bytes(const std::vector<uint8_t> &vec)
: data(vec.data()), len(vec.size()) {}
template <size_t N>
explicit Bytes(const uint8_t (&array)[N]) : data(array), len(N) {}
const uint8_t *data;
size_t len;
};
inline bool operator==(const Bytes &a, const Bytes &b) {
return a.len == b.len && OPENSSL_memcmp(a.data, b.data, a.len) == 0;
}
inline bool operator!=(const Bytes &a, const Bytes &b) { return !(a == b); }
std::ostream &operator<<(std::ostream &os, const Bytes &in);
#endif /* OPENSSL_HEADER_CRYPTO_TEST_TEST_UTIL_H */