Brian Smith | f9f72b3 | 2015-07-22 22:36:57 -0400 | [diff] [blame] | 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> |
David Benjamin | 7d7554b | 2017-02-04 11:48:59 -0500 | [diff] [blame] | 19 | #include <stdint.h> |
Brian Smith | f9f72b3 | 2015-07-22 22:36:57 -0400 | [diff] [blame] | 20 | #include <stdio.h> |
David Benjamin | b91b9a8 | 2017-02-05 02:52:55 -0500 | [diff] [blame^] | 21 | #include <string.h> |
Brian Smith | f9f72b3 | 2015-07-22 22:36:57 -0400 | [diff] [blame] | 22 | |
David Benjamin | 7d7554b | 2017-02-04 11:48:59 -0500 | [diff] [blame] | 23 | #include <iosfwd> |
| 24 | |
| 25 | #include "../internal.h" |
Brian Smith | f9f72b3 | 2015-07-22 22:36:57 -0400 | [diff] [blame] | 26 | |
| 27 | |
David Benjamin | 7d7554b | 2017-02-04 11:48:59 -0500 | [diff] [blame] | 28 | // hexdump writes |msg| to |fp| followed by the hex encoding of |len| bytes |
| 29 | // from |in|. |
Brian Smith | f9f72b3 | 2015-07-22 22:36:57 -0400 | [diff] [blame] | 30 | void hexdump(FILE *fp, const char *msg, const void *in, size_t len); |
| 31 | |
David Benjamin | 7d7554b | 2017-02-04 11:48:59 -0500 | [diff] [blame] | 32 | // Bytes is a wrapper over a byte slice which may be compared for equality. This |
| 33 | // allows it to be used in EXPECT_EQ macros. |
| 34 | struct Bytes { |
| 35 | Bytes(const uint8_t *data_arg, size_t len_arg) |
| 36 | : data(data_arg), len(len_arg) {} |
Brian Smith | f9f72b3 | 2015-07-22 22:36:57 -0400 | [diff] [blame] | 37 | |
David Benjamin | b91b9a8 | 2017-02-05 02:52:55 -0500 | [diff] [blame^] | 38 | Bytes(const char *str) |
| 39 | : data(reinterpret_cast<const uint8_t *>(str)), len(strlen(str)) {} |
| 40 | |
David Benjamin | 7d7554b | 2017-02-04 11:48:59 -0500 | [diff] [blame] | 41 | template <size_t N> |
| 42 | Bytes(const uint8_t (&array)[N]) : data(array), len(N) {} |
| 43 | |
| 44 | const uint8_t *data; |
| 45 | size_t len; |
| 46 | }; |
| 47 | |
| 48 | inline bool operator==(const Bytes &a, const Bytes &b) { |
| 49 | return a.len == b.len && OPENSSL_memcmp(a.data, b.data, a.len) == 0; |
Brian Smith | f9f72b3 | 2015-07-22 22:36:57 -0400 | [diff] [blame] | 50 | } |
David Benjamin | 7d7554b | 2017-02-04 11:48:59 -0500 | [diff] [blame] | 51 | |
| 52 | inline bool operator!=(const Bytes &a, const Bytes &b) { return !(a == b); } |
| 53 | |
| 54 | std::ostream &operator<<(std::ostream &os, const Bytes &in); |
| 55 | |
Brian Smith | f9f72b3 | 2015-07-22 22:36:57 -0400 | [diff] [blame] | 56 | |
| 57 | #endif /* OPENSSL_HEADER_CRYPTO_TEST_TEST_UTIL_H */ |