blob: 9c9ef58f1f15a55fb8bd293c1d300c50559f954b [file] [log] [blame]
Brian Smithf9f72b32015-07-22 22:36:57 -04001/* 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>
David Benjamin7d7554b2017-02-04 11:48:59 -050019#include <stdint.h>
Brian Smithf9f72b32015-07-22 22:36:57 -040020#include <stdio.h>
David Benjaminb91b9a82017-02-05 02:52:55 -050021#include <string.h>
Brian Smithf9f72b32015-07-22 22:36:57 -040022
David Benjamin7d7554b2017-02-04 11:48:59 -050023#include <iosfwd>
David Benjamin76dd1802017-04-17 10:04:39 -040024#include <string>
David Benjamin3ecd0a52017-05-19 15:26:18 -040025#include <vector>
David Benjamin7d7554b2017-02-04 11:48:59 -050026
27#include "../internal.h"
Brian Smithf9f72b32015-07-22 22:36:57 -040028
29
David Benjamin7d7554b2017-02-04 11:48:59 -050030// hexdump writes |msg| to |fp| followed by the hex encoding of |len| bytes
31// from |in|.
Brian Smithf9f72b32015-07-22 22:36:57 -040032void hexdump(FILE *fp, const char *msg, const void *in, size_t len);
33
David Benjamin7d7554b2017-02-04 11:48:59 -050034// Bytes is a wrapper over a byte slice which may be compared for equality. This
35// allows it to be used in EXPECT_EQ macros.
36struct Bytes {
37 Bytes(const uint8_t *data_arg, size_t len_arg)
38 : data(data_arg), len(len_arg) {}
David Benjamin5c127782017-03-09 01:13:07 -050039 Bytes(const char *data_arg, size_t len_arg)
40 : data(reinterpret_cast<const uint8_t *>(data_arg)), len(len_arg) {}
Brian Smithf9f72b32015-07-22 22:36:57 -040041
David Benjamin76dd1802017-04-17 10:04:39 -040042 explicit Bytes(const char *str)
David Benjaminb91b9a82017-02-05 02:52:55 -050043 : data(reinterpret_cast<const uint8_t *>(str)), len(strlen(str)) {}
David Benjamin76dd1802017-04-17 10:04:39 -040044 explicit Bytes(const std::string &str)
45 : data(reinterpret_cast<const uint8_t *>(str.data())), len(str.size()) {}
David Benjamin3ecd0a52017-05-19 15:26:18 -040046 explicit Bytes(const std::vector<uint8_t> &vec)
47 : data(vec.data()), len(vec.size()) {}
David Benjaminb91b9a82017-02-05 02:52:55 -050048
David Benjamin7d7554b2017-02-04 11:48:59 -050049 template <size_t N>
David Benjamin76dd1802017-04-17 10:04:39 -040050 explicit Bytes(const uint8_t (&array)[N]) : data(array), len(N) {}
David Benjamin7d7554b2017-02-04 11:48:59 -050051
52 const uint8_t *data;
53 size_t len;
54};
55
56inline bool operator==(const Bytes &a, const Bytes &b) {
57 return a.len == b.len && OPENSSL_memcmp(a.data, b.data, a.len) == 0;
Brian Smithf9f72b32015-07-22 22:36:57 -040058}
David Benjamin7d7554b2017-02-04 11:48:59 -050059
60inline bool operator!=(const Bytes &a, const Bytes &b) { return !(a == b); }
61
62std::ostream &operator<<(std::ostream &os, const Bytes &in);
63
Brian Smithf9f72b32015-07-22 22:36:57 -040064
David Benjamin808f8322017-08-18 14:06:02 -040065#endif // OPENSSL_HEADER_CRYPTO_TEST_TEST_UTIL_H