Add EncodeHex and DecodeHex functions to test_util.h.

We have enough copies of these.

Change-Id: I1ff8915b8ca781dc070e802e634d1dc12832e272
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/39304
Commit-Queue: Adam Langley <agl@google.com>
Reviewed-by: Adam Langley <agl@google.com>
diff --git a/crypto/test/file_test.cc b/crypto/test/file_test.cc
index 895185c..c39c078 100644
--- a/crypto/test/file_test.cc
+++ b/crypto/test/file_test.cc
@@ -28,6 +28,7 @@
 #include <openssl/err.h>
 
 #include "../internal.h"
+#include "./test_util.h"
 
 
 FileTest::FileTest(std::unique_ptr<FileTest::LineReader> reader,
@@ -312,31 +313,6 @@
   return GetAttribute(&value, key) && ConvertToBytes(out, value);
 }
 
-static std::string EncodeHex(const uint8_t *in, size_t in_len) {
-  static const char kHexDigits[] = "0123456789abcdef";
-  std::string ret;
-  ret.reserve(in_len * 2);
-  for (size_t i = 0; i < in_len; i++) {
-    ret += kHexDigits[in[i] >> 4];
-    ret += kHexDigits[in[i] & 0xf];
-  }
-  return ret;
-}
-
-bool FileTest::ExpectBytesEqual(const uint8_t *expected, size_t expected_len,
-                                const uint8_t *actual, size_t actual_len) {
-  if (expected_len == actual_len &&
-      OPENSSL_memcmp(expected, actual, expected_len) == 0) {
-    return true;
-  }
-
-  std::string expected_hex = EncodeHex(expected, expected_len);
-  std::string actual_hex = EncodeHex(actual, actual_len);
-  PrintLine("Expected: %s", expected_hex.c_str());
-  PrintLine("Actual:   %s", actual_hex.c_str());
-  return false;
-}
-
 void FileTest::ClearTest() {
   start_line_ = 0;
   type_.clear();
@@ -360,22 +336,6 @@
   unused_instructions_.erase(key);
 }
 
-static bool FromHexDigit(uint8_t *out, char c) {
-  if ('0' <= c && c <= '9') {
-    *out = c - '0';
-    return true;
-  }
-  if ('a' <= c && c <= 'f') {
-    *out = c - 'a' + 10;
-    return true;
-  }
-  if ('A' <= c && c <= 'F') {
-    *out = c - 'A' + 10;
-    return true;
-  }
-  return false;
-}
-
 bool FileTest::ConvertToBytes(std::vector<uint8_t> *out,
                               const std::string &value) {
   if (value.size() >= 2 && value[0] == '"' && value[value.size() - 1] == '"') {
@@ -383,20 +343,10 @@
     return true;
   }
 
-  if (value.size() % 2 != 0) {
+  if (!DecodeHex(out, value)) {
     PrintLine("Error decoding value: %s", value.c_str());
     return false;
   }
-  out->clear();
-  out->reserve(value.size() / 2);
-  for (size_t i = 0; i < value.size(); i += 2) {
-    uint8_t hi, lo;
-    if (!FromHexDigit(&hi, value[i]) || !FromHexDigit(&lo, value[i + 1])) {
-      PrintLine("Error decoding value: %s", value.c_str());
-      return false;
-    }
-    out->push_back((hi << 4) | lo);
-  }
   return true;
 }
 
diff --git a/crypto/test/file_test.h b/crypto/test/file_test.h
index 70a93e9..87f306f 100644
--- a/crypto/test/file_test.h
+++ b/crypto/test/file_test.h
@@ -164,11 +164,6 @@
   // success and returns false with an error to |stderr| on failure.
   bool GetBytes(std::vector<uint8_t> *out, const std::string &key);
 
-  // ExpectBytesEqual returns true if |expected| and |actual| are equal.
-  // Otherwise, it returns false and prints a message to |stderr|.
-  bool ExpectBytesEqual(const uint8_t *expected, size_t expected_len,
-                        const uint8_t *actual, size_t actual_len);
-
   // AtNewInstructionBlock returns true if the current test was immediately
   // preceded by an instruction block.
   bool IsAtNewInstructionBlock() const;
diff --git a/crypto/test/test_util.cc b/crypto/test/test_util.cc
index 29be702..7f95413 100644
--- a/crypto/test/test_util.cc
+++ b/crypto/test/test_util.cc
@@ -35,10 +35,51 @@
   }
 
   // Print a byte slice as hex.
-  static const char hex[] = "0123456789abcdef";
-  for (uint8_t b : in.span_) {
-    os << hex[b >> 4];
-    os << hex[b & 0xf];
-  }
+  os << EncodeHex(in.span_);
   return os;
 }
+
+static bool FromHexDigit(uint8_t *out, char c) {
+  if ('0' <= c && c <= '9') {
+    *out = c - '0';
+    return true;
+  }
+  if ('a' <= c && c <= 'f') {
+    *out = c - 'a' + 10;
+    return true;
+  }
+  if ('A' <= c && c <= 'F') {
+    *out = c - 'A' + 10;
+    return true;
+  }
+  return false;
+}
+
+bool DecodeHex(std::vector<uint8_t> *out, const std::string &in) {
+  out->clear();
+  if (in.size() % 2 != 0) {
+    return false;
+  }
+  out->reserve(in.size() / 2);
+  for (size_t i = 0; i < in.size(); i += 2) {
+    uint8_t hi, lo;
+    if (!FromHexDigit(&hi, in[i]) ||
+        !FromHexDigit(&lo, in[i + 1])) {
+      return false;
+    }
+    out->push_back((hi << 4) | lo);
+  }
+  return true;
+}
+
+std::string EncodeHex(bssl::Span<const uint8_t> in) {
+  static const char kHexDigits[] = "0123456789abcdef";
+  std::string ret;
+  ret.reserve(in.size() * 2);
+  for (uint8_t b : in) {
+    ret += kHexDigits[b >> 4];
+    ret += kHexDigits[b & 0xf];
+  }
+  return ret;
+}
+
diff --git a/crypto/test/test_util.h b/crypto/test/test_util.h
index 2ef3f19..796c931 100644
--- a/crypto/test/test_util.h
+++ b/crypto/test/test_util.h
@@ -59,5 +59,13 @@
 
 std::ostream &operator<<(std::ostream &os, const Bytes &in);
 
+// DecodeHex decodes |in| from hexadecimal and writes the output to |out|. It
+// returns true on success and false if |in| is not a valid hexadecimal byte
+// string.
+bool DecodeHex(std::vector<uint8_t> *out, const std::string &in);
+
+// EncodeHex returns |in| encoded in hexadecimal.
+std::string EncodeHex(bssl::Span<const uint8_t> in);
+
 
 #endif  // OPENSSL_HEADER_CRYPTO_TEST_TEST_UTIL_H