Home | History | Annotate | Download | only in test
      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>
     19 #include <stdint.h>
     20 #include <stdio.h>
     21 #include <string.h>
     22 
     23 #include <iosfwd>
     24 #include <string>
     25 #include <vector>
     26 
     27 #include "../internal.h"
     28 
     29 
     30 // hexdump writes |msg| to |fp| followed by the hex encoding of |len| bytes
     31 // from |in|.
     32 void hexdump(FILE *fp, const char *msg, const void *in, size_t len);
     33 
     34 // 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.
     36 struct Bytes {
     37   Bytes(const uint8_t *data_arg, size_t len_arg)
     38       : data(data_arg), len(len_arg) {}
     39   Bytes(const char *data_arg, size_t len_arg)
     40       : data(reinterpret_cast<const uint8_t *>(data_arg)), len(len_arg) {}
     41 
     42   explicit Bytes(const char *str)
     43       : data(reinterpret_cast<const uint8_t *>(str)), len(strlen(str)) {}
     44   explicit Bytes(const std::string &str)
     45       : data(reinterpret_cast<const uint8_t *>(str.data())), len(str.size()) {}
     46   explicit Bytes(const std::vector<uint8_t> &vec)
     47       : data(vec.data()), len(vec.size()) {}
     48 
     49   template <size_t N>
     50   explicit Bytes(const uint8_t (&array)[N]) : data(array), len(N) {}
     51 
     52   const uint8_t *data;
     53   size_t len;
     54 };
     55 
     56 inline bool operator==(const Bytes &a, const Bytes &b) {
     57   return a.len == b.len && OPENSSL_memcmp(a.data, b.data, a.len) == 0;
     58 }
     59 
     60 inline bool operator!=(const Bytes &a, const Bytes &b) { return !(a == b); }
     61 
     62 std::ostream &operator<<(std::ostream &os, const Bytes &in);
     63 
     64 
     65 #endif /* OPENSSL_HEADER_CRYPTO_TEST_TEST_UTIL_H */
     66