blob: e03bff44288e7ef3f1fbc4ee074e586cee8a5b94 [file] [log] [blame]
Kenny Rootb8494592015-09-25 02:29:14 +00001/* 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>
Robert Sloan5d625782017-02-13 09:55:39 -080019#include <stdint.h>
Kenny Rootb8494592015-09-25 02:29:14 +000020#include <stdio.h>
Robert Sloana94fe052017-02-21 08:49:28 -080021#include <string.h>
Kenny Rootb8494592015-09-25 02:29:14 +000022
Robert Sloan5d625782017-02-13 09:55:39 -080023#include <iosfwd>
Robert Sloan572a4e22017-04-17 10:52:19 -070024#include <string>
Robert Sloan5d625782017-02-13 09:55:39 -080025
26#include "../internal.h"
Kenny Rootb8494592015-09-25 02:29:14 +000027
28
Robert Sloan5d625782017-02-13 09:55:39 -080029// hexdump writes |msg| to |fp| followed by the hex encoding of |len| bytes
30// from |in|.
Kenny Rootb8494592015-09-25 02:29:14 +000031void hexdump(FILE *fp, const char *msg, const void *in, size_t len);
32
Robert Sloan5d625782017-02-13 09:55:39 -080033// Bytes is a wrapper over a byte slice which may be compared for equality. This
34// allows it to be used in EXPECT_EQ macros.
35struct Bytes {
36 Bytes(const uint8_t *data_arg, size_t len_arg)
37 : data(data_arg), len(len_arg) {}
Robert Sloan6d0d00e2017-03-27 07:13:07 -070038 Bytes(const char *data_arg, size_t len_arg)
39 : data(reinterpret_cast<const uint8_t *>(data_arg)), len(len_arg) {}
Kenny Rootb8494592015-09-25 02:29:14 +000040
Robert Sloan572a4e22017-04-17 10:52:19 -070041 explicit Bytes(const char *str)
Robert Sloana94fe052017-02-21 08:49:28 -080042 : data(reinterpret_cast<const uint8_t *>(str)), len(strlen(str)) {}
Robert Sloan572a4e22017-04-17 10:52:19 -070043 explicit Bytes(const std::string &str)
44 : data(reinterpret_cast<const uint8_t *>(str.data())), len(str.size()) {}
Robert Sloana94fe052017-02-21 08:49:28 -080045
Robert Sloan5d625782017-02-13 09:55:39 -080046 template <size_t N>
Robert Sloan572a4e22017-04-17 10:52:19 -070047 explicit Bytes(const uint8_t (&array)[N]) : data(array), len(N) {}
Robert Sloan5d625782017-02-13 09:55:39 -080048
49 const uint8_t *data;
50 size_t len;
51};
52
53inline bool operator==(const Bytes &a, const Bytes &b) {
54 return a.len == b.len && OPENSSL_memcmp(a.data, b.data, a.len) == 0;
Kenny Rootb8494592015-09-25 02:29:14 +000055}
Robert Sloan5d625782017-02-13 09:55:39 -080056
57inline bool operator!=(const Bytes &a, const Bytes &b) { return !(a == b); }
58
59std::ostream &operator<<(std::ostream &os, const Bytes &in);
60
Kenny Rootb8494592015-09-25 02:29:14 +000061
62#endif /* OPENSSL_HEADER_CRYPTO_TEST_TEST_UTIL_H */