1 /* Copyright (c) 2015, Google Inc. 2 * 3 * Permission to use, copy, modify, and/or distribute this software for any 4 * purpose with or without fee is hereby granted, provided that the above 5 * copyright notice and this permission notice appear in all copies. 6 * 7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION 12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ 14 15 #ifndef OPENSSL_HEADER_CRYPTO_TEST_TEST_UTIL_H 16 #define OPENSSL_HEADER_CRYPTO_TEST_TEST_UTIL_H 17 18 #include <stddef.h> 19 #include <stdint.h> 20 #include <stdio.h> 21 #include <string.h> 22 23 #include <iosfwd> 24 #include <string> 25 #include <vector> 26 27 #include <gtest/gtest.h> 28 29 #include <openssl/span.h> 30 31 #include "../internal.h" 32 33 34 // hexdump writes |msg| to |fp| followed by the hex encoding of |len| bytes 35 // from |in|. 36 void hexdump(FILE *fp, const char *msg, const void *in, size_t len); 37 38 // Bytes is a wrapper over a byte slice which may be compared for equality. This 39 // allows it to be used in EXPECT_EQ macros. 40 struct Bytes { BytesBytes41 Bytes(const uint8_t *data_arg, size_t len_arg) 42 : span_(data_arg, len_arg) {} BytesBytes43 Bytes(const char *data_arg, size_t len_arg) 44 : span_(reinterpret_cast<const uint8_t *>(data_arg), len_arg) {} 45 BytesBytes46 explicit Bytes(const char *str) 47 : span_(reinterpret_cast<const uint8_t *>(str), strlen(str)) {} BytesBytes48 explicit Bytes(const std::string &str) 49 : span_(reinterpret_cast<const uint8_t *>(str.data()), str.size()) {} BytesBytes50 explicit Bytes(bssl::Span<const uint8_t> span) 51 : span_(span) {} 52 53 bssl::Span<const uint8_t> span_; 54 }; 55 56 inline bool operator==(const Bytes &a, const Bytes &b) { 57 return a.span_ == b.span_; 58 } 59 60 inline bool operator!=(const Bytes &a, const Bytes &b) { return !(a == b); } 61 62 std::ostream &operator<<(std::ostream &os, const Bytes &in); 63 64 // DecodeHex decodes |in| from hexadecimal and writes the output to |out|. It 65 // returns true on success and false if |in| is not a valid hexadecimal byte 66 // string. 67 bool DecodeHex(std::vector<uint8_t> *out, const std::string &in); 68 69 // EncodeHex returns |in| encoded in hexadecimal. 70 std::string EncodeHex(bssl::Span<const uint8_t> in); 71 72 // ErrorEquals asserts that |err| is an error with library |lib| and reason 73 // |reason|. 74 testing::AssertionResult ErrorEquals(uint32_t err, int lib, int reason); 75 76 77 #endif // OPENSSL_HEADER_CRYPTO_TEST_TEST_UTIL_H 78