Convert evp_test to GTest.

This is a fairly shallow conversion because of the somewhat screwy Error
lines in the test which may target random functions like
EVP_PKEY_CTX_set_signature_md. We probably should revise this, perhaps
moving those to normal tests and leaving error codes to the core
operation itself.

BUG=129

Change-Id: I27dcc945058911b2de40cd48466d4e0366813a12
Reviewed-on: https://boringssl-review.googlesource.com/16988
Commit-Queue: David Benjamin <davidben@google.com>
Commit-Queue: Steven Valdez <svaldez@google.com>
Reviewed-by: Steven Valdez <svaldez@google.com>
CQ-Verified: CQ bot account: commit-bot@chromium.org <commit-bot@chromium.org>
diff --git a/crypto/CMakeLists.txt b/crypto/CMakeLists.txt
index 2a8e9cf..f4430eb 100644
--- a/crypto/CMakeLists.txt
+++ b/crypto/CMakeLists.txt
@@ -236,6 +236,7 @@
   dsa/dsa_test.cc
   err/err_test.cc
   evp/evp_extra_test.cc
+  evp/evp_test.cc
   evp/pbkdf_test.cc
   fipsmodule/aes/aes_test.cc
   fipsmodule/bn/bn_test.cc
diff --git a/crypto/evp/CMakeLists.txt b/crypto/evp/CMakeLists.txt
index 48caf9e..c3b6d90 100644
--- a/crypto/evp/CMakeLists.txt
+++ b/crypto/evp/CMakeLists.txt
@@ -20,15 +20,3 @@
   print.c
   sign.c
 )
-
-
-add_executable(
-  evp_test
-
-  evp_test.cc
-
-  $<TARGET_OBJECTS:test_support>
-)
-
-target_link_libraries(evp_test crypto)
-add_dependencies(all_tests evp_test)
diff --git a/crypto/evp/evp_test.cc b/crypto/evp/evp_test.cc
index 24de98d..d06fa43 100644
--- a/crypto/evp/evp_test.cc
+++ b/crypto/evp/evp_test.cc
@@ -68,6 +68,8 @@
 
 OPENSSL_MSVC_PRAGMA(warning(pop))
 
+#include <gtest/gtest.h>
+
 #include <openssl/bytestring.h>
 #include <openssl/crypto.h>
 #include <openssl/digest.h>
@@ -75,6 +77,7 @@
 #include <openssl/rsa.h>
 
 #include "../test/file_test.h"
+#include "../test/test_util.h"
 
 
 // evp_test dispatches between multiple test types. PrivateKey tests take a key
@@ -96,7 +99,7 @@
   } else if (name == "SHA512") {
     return EVP_sha512();
   }
-  t->PrintLine("Unknown digest: '%s'", name.c_str());
+  ADD_FAILURE() << "Unknown digest: " << name;
   return nullptr;
 }
 
@@ -113,7 +116,7 @@
   if (name == "Ed25519") {
     return EVP_PKEY_ED25519;
   }
-  t->PrintLine("Unknown key type: '%s'", name.c_str());
+  ADD_FAILURE() << "Unknown key type: " << name;
   return EVP_PKEY_NONE;
 }
 
@@ -130,7 +133,7 @@
     *out = RSA_PKCS1_OAEP_PADDING;
     return true;
   }
-  t->PrintLine("Unknown RSA padding mode: '%s'", name.c_str());
+  ADD_FAILURE() << "Unknown RSA padding mode: " << name;
   return false;
 }
 
@@ -155,10 +158,7 @@
   if (!t->GetAttribute(&key_type, "Type")) {
     return false;
   }
-  if (EVP_PKEY_id(pkey.get()) != GetKeyType(t, key_type)) {
-    t->PrintLine("Bad key type.");
-    return false;
-  }
+  EXPECT_EQ(GetKeyType(t, key_type), EVP_PKEY_id(pkey.get()));
 
   // The key must re-encode correctly.
   bssl::ScopedCBB cbb;
@@ -176,23 +176,16 @@
       !t->GetBytes(&output, "Output")) {
     return false;
   }
-  if (!t->ExpectBytesEqual(output.data(), output.size(), der, der_len)) {
-    t->PrintLine("Re-encoding the key did not match.");
-    return false;
-  }
+  EXPECT_EQ(Bytes(output), Bytes(der, der_len)) << "Re-encoding the key did not match.";
 
   // Save the key for future tests.
   const std::string &key_name = t->GetParameter();
-  if (key_map->count(key_name) > 0) {
-    t->PrintLine("Duplicate key '%s'.", key_name.c_str());
-    return false;
-  }
+  EXPECT_EQ(0u, key_map->count(key_name)) << "Duplicate key: " << key_name;
   (*key_map)[key_name] = std::move(pkey);
   return true;
 }
 
-static bool TestEVP(FileTest *t, void *arg) {
-  KeyMap *key_map = reinterpret_cast<KeyMap*>(arg);
+static bool TestEVP(FileTest *t, KeyMap *key_map) {
   if (t->GetType() == "PrivateKey") {
     return ImportKey(t, key_map, EVP_parse_private_key,
                      EVP_marshal_private_key);
@@ -223,14 +216,14 @@
     key_op_init = EVP_PKEY_verify_init;
     verify_op = EVP_PKEY_verify_message;
   } else {
-    t->PrintLine("Unknown test '%s'", t->GetType().c_str());
+    ADD_FAILURE() << "Unknown test " << t->GetType();
     return false;
   }
 
   // Load the key.
   const std::string &key_name = t->GetParameter();
   if (key_map->count(key_name) == 0) {
-    t->PrintLine("Could not find key '%s'.", key_name.c_str());
+    ADD_FAILURE() << "Could not find key " << key_name;
     return false;
   }
   EVP_PKEY *key = (*key_map)[key_name].get();
@@ -292,20 +285,24 @@
     return false;
   }
   actual.resize(len);
-  if (!t->GetBytes(&output, "Output") ||
-      !t->ExpectBytesEqual(output.data(), output.size(), actual.data(), len)) {
+  if (!t->GetBytes(&output, "Output")) {
     return false;
   }
+
+  EXPECT_EQ(Bytes(output), Bytes(actual));
   return true;
 }
 
-int main(int argc, char *argv[]) {
-  CRYPTO_library_init();
-  if (argc != 2) {
-    fprintf(stderr, "%s <test file.txt>\n", argv[0]);
-    return 1;
-  }
-
-  KeyMap map;
-  return FileTestMain(TestEVP, &map, argv[1]);
+TEST(EVPTest, TestVectors) {
+  KeyMap key_map;
+  FileTestGTest("crypto/evp/evp_tests.txt", [&](FileTest *t) {
+    bool result = TestEVP(t, &key_map);
+    if (t->HasAttribute("Error")) {
+      ASSERT_FALSE(result) << "Operation unexpectedly succeeded.";
+      uint32_t err = ERR_peek_error();
+      EXPECT_EQ(t->GetAttributeOrDie("Error"), ERR_reason_error_string(err));
+    } else {
+      ASSERT_TRUE(result) << "Operation unexpectedly failed.";
+    }
+  });
 }
diff --git a/sources.cmake b/sources.cmake
index f795c51..3947c93 100644
--- a/sources.cmake
+++ b/sources.cmake
@@ -38,6 +38,7 @@
   crypto/cipher_extra/test/nist_cavp/tdes_ecb.txt
   crypto/curve25519/ed25519_tests.txt
   crypto/ecdh/ecdh_tests.txt
+  crypto/evp/evp_tests.txt
   crypto/fipsmodule/aes/aes_tests.txt
   crypto/fipsmodule/bn/bn_tests.txt
   crypto/fipsmodule/ecdsa/ecdsa_sign_tests.txt
diff --git a/util/all_tests.json b/util/all_tests.json
index b82970e..49d89c8 100644
--- a/util/all_tests.json
+++ b/util/all_tests.json
@@ -1,6 +1,5 @@
 [
 	["crypto/crypto_test"],
-	["crypto/evp/evp_test", "crypto/evp/evp_tests.txt"],
 	["crypto/fipsmodule/example_mul"],
 	["crypto/fipsmodule/p256-x86_64_test", "crypto/fipsmodule/ec/p256-x86_64_tests.txt"],
 	["crypto/x509v3/tab_test"],