Support the allocating case of i2d_ASN1_{BOOLEAN,OBJECT}.

Imported from upstream's 0971432f6f6d8b40d797133621809bd31eb7bf4e and
7d4c97add12cfa5d4589880b09d6139c3203e2f4, but with missing tests added. Along
the way, make Bytes work with any Span<const uint8_t>-convertable type.

Change-Id: If365f981fe8a8274e12000309ffd99b1bb719842
Reviewed-on: https://boringssl-review.googlesource.com/31086
Reviewed-by: Adam Langley <agl@google.com>
diff --git a/crypto/test/test_util.cc b/crypto/test/test_util.cc
index 4ad777f..29be702 100644
--- a/crypto/test/test_util.cc
+++ b/crypto/test/test_util.cc
@@ -30,15 +30,15 @@
 }
 
 std::ostream &operator<<(std::ostream &os, const Bytes &in) {
-  if (in.len == 0) {
+  if (in.span_.empty()) {
     return os << "<empty Bytes>";
   }
 
   // Print a byte slice as hex.
   static const char hex[] = "0123456789abcdef";
-  for (size_t i = 0; i < in.len; i++) {
-    os << hex[in.data[i] >> 4];
-    os << hex[in.data[i] & 0xf];
+  for (uint8_t b : in.span_) {
+    os << hex[b >> 4];
+    os << hex[b & 0xf];
   }
   return os;
 }
diff --git a/crypto/test/test_util.h b/crypto/test/test_util.h
index 9c9ef58..2ef3f19 100644
--- a/crypto/test/test_util.h
+++ b/crypto/test/test_util.h
@@ -24,6 +24,8 @@
 #include <string>
 #include <vector>
 
+#include <openssl/span.h>
+
 #include "../internal.h"
 
 
@@ -35,26 +37,22 @@
 // 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) {}
+      : span_(data_arg, len_arg) {}
   Bytes(const char *data_arg, size_t len_arg)
-      : data(reinterpret_cast<const uint8_t *>(data_arg)), len(len_arg) {}
+      : span_(reinterpret_cast<const uint8_t *>(data_arg), len_arg) {}
 
   explicit Bytes(const char *str)
-      : data(reinterpret_cast<const uint8_t *>(str)), len(strlen(str)) {}
+      : span_(reinterpret_cast<const uint8_t *>(str), 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()) {}
+      : span_(reinterpret_cast<const uint8_t *>(str.data()), str.size()) {}
+  explicit Bytes(bssl::Span<const uint8_t> span)
+      : span_(span) {}
 
-  template <size_t N>
-  explicit Bytes(const uint8_t (&array)[N]) : data(array), len(N) {}
-
-  const uint8_t *data;
-  size_t len;
+  bssl::Span<const uint8_t> span_;
 };
 
 inline bool operator==(const Bytes &a, const Bytes &b) {
-  return a.len == b.len && OPENSSL_memcmp(a.data, b.data, a.len) == 0;
+  return a.span_ == b.span_;
 }
 
 inline bool operator!=(const Bytes &a, const Bytes &b) { return !(a == b); }