summaryrefslogtreecommitdiff
path: root/src/crypto/test/test_util.h
blob: 9c9ef58f1f15a55fb8bd293c1d300c50559f954b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/* Copyright (c) 2015, Google Inc.
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */

#ifndef OPENSSL_HEADER_CRYPTO_TEST_TEST_UTIL_H
#define OPENSSL_HEADER_CRYPTO_TEST_TEST_UTIL_H

#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>

#include <iosfwd>
#include <string>
#include <vector>

#include "../internal.h"


// hexdump writes |msg| to |fp| followed by the hex encoding of |len| bytes
// from |in|.
void hexdump(FILE *fp, const char *msg, const void *in, size_t len);

// Bytes is a wrapper over a byte slice which may be compared for equality. This
// allows it to be used in EXPECT_EQ macros.
struct Bytes {
  Bytes(const uint8_t *data_arg, size_t len_arg)
      : data(data_arg), len(len_arg) {}
  Bytes(const char *data_arg, size_t len_arg)
      : data(reinterpret_cast<const uint8_t *>(data_arg)), len(len_arg) {}

  explicit Bytes(const char *str)
      : data(reinterpret_cast<const uint8_t *>(str)), len(strlen(str)) {}
  explicit Bytes(const std::string &str)
      : data(reinterpret_cast<const uint8_t *>(str.data())), len(str.size()) {}
  explicit Bytes(const std::vector<uint8_t> &vec)
      : data(vec.data()), len(vec.size()) {}

  template <size_t N>
  explicit Bytes(const uint8_t (&array)[N]) : data(array), len(N) {}

  const uint8_t *data;
  size_t len;
};

inline bool operator==(const Bytes &a, const Bytes &b) {
  return a.len == b.len && OPENSSL_memcmp(a.data, b.data, a.len) == 0;
}

inline bool operator!=(const Bytes &a, const Bytes &b) { return !(a == b); }

std::ostream &operator<<(std::ostream &os, const Bytes &in);


#endif  // OPENSSL_HEADER_CRYPTO_TEST_TEST_UTIL_H