| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 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_view> |
| 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 { |
| 41 | Bytes(const uint8_t *data_arg, size_t len_arg) |
| 42 | : span_(data_arg, len_arg) {} |
| 43 | Bytes(const char *data_arg, size_t len_arg) |
| 44 | : span_(reinterpret_cast<const uint8_t *>(data_arg), len_arg) {} |
| 45 |
| 46 | explicit Bytes(std::string_view str) : span_(bssl::StringAsBytes(str)) {} |
| 47 | explicit Bytes(bssl::Span<const uint8_t> span) : span_(span) {} |
| 48 |
| 49 | bssl::Span<const uint8_t> span_; |
| 50 | }; |
| 51 |
| 52 | inline bool operator==(const Bytes &a, const Bytes &b) { |
| 53 | return a.span_ == b.span_; |
| 54 | } |
| 55 |
| 56 | inline bool operator!=(const Bytes &a, const Bytes &b) { return !(a == b); } |
| 57 |
| 58 | // Declassified returns a declassified copy of some input. |
| 59 | inline std::vector<uint8_t> Declassified(bssl::Span<const uint8_t> in) { |
| 60 | std::vector<uint8_t> copy(in.begin(), in.end()); |
| 61 | CONSTTIME_DECLASSIFY(copy.data(), copy.size()); |
| 62 | return copy; |
| 63 | } |
| 64 |
| 65 | std::ostream &operator<<(std::ostream &os, const Bytes &in); |
| 66 |
| 67 | // DecodeHex decodes |in| from hexadecimal and writes the output to |out|. It |
| 68 | // returns true on success and false if |in| is not a valid hexadecimal byte |
| 69 | // string. |
| 70 | bool DecodeHex(std::vector<uint8_t> *out, const std::string &in); |
| 71 |
| 72 | // EncodeHex returns |in| encoded in hexadecimal. |
| 73 | std::string EncodeHex(bssl::Span<const uint8_t> in); |
| 74 |
| 75 | // ErrorEquals asserts that |err| is an error with library |lib| and reason |
| 76 | // |reason|. |
| 77 | testing::AssertionResult ErrorEquals(uint32_t err, int lib, int reason); |
| 78 |
| 79 |
| 80 | #endif // OPENSSL_HEADER_CRYPTO_TEST_TEST_UTIL_H |
| 81 |