Convert spake25519_test to GTest.

BUG=129

Change-Id: I5f812c87d6a02f5b4de0d9153afe7399e124382b
Reviewed-on: https://boringssl-review.googlesource.com/15465
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 caf7d4e..5996577 100644
--- a/crypto/CMakeLists.txt
+++ b/crypto/CMakeLists.txt
@@ -241,6 +241,7 @@
   cmac/cmac_test.cc
   compiler_test.cc
   constant_time_test.cc
+  curve25519/spake25519_test.cc
   curve25519/x25519_test.cc
   dh/dh_test.cc
   dsa/dsa_test.cc
diff --git a/crypto/curve25519/CMakeLists.txt b/crypto/curve25519/CMakeLists.txt
index 198d6af..ae95be0 100644
--- a/crypto/curve25519/CMakeLists.txt
+++ b/crypto/curve25519/CMakeLists.txt
@@ -37,12 +37,3 @@
 
 target_link_libraries(ed25519_test crypto)
 add_dependencies(all_tests ed25519_test)
-
-add_executable(
-  spake25519_test
-
-  spake25519_test.cc
-)
-
-target_link_libraries(spake25519_test crypto)
-add_dependencies(all_tests spake25519_test)
diff --git a/crypto/curve25519/spake25519_test.cc b/crypto/curve25519/spake25519_test.cc
index 3af073d..97f17b7 100644
--- a/crypto/curve25519/spake25519_test.cc
+++ b/crypto/curve25519/spake25519_test.cc
@@ -12,17 +12,21 @@
  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
 
+#include <openssl/curve25519.h>
+
 #include <string>
 
 #include <stdint.h>
 #include <stdio.h>
 #include <string.h>
 
-#include <openssl/curve25519.h>
+#include <gtest/gtest.h>
 
 #include "../internal.h"
 
 
+/* TODO(agl): add tests with fixed vectors once SPAKE2 is nailed down. */
+
 struct SPAKE2Run {
   bool Run() {
     bssl::UniquePtr<SPAKE2_CTX> alice(SPAKE2_CTX_new(
@@ -92,79 +96,34 @@
   bool key_matches_ = false;
 };
 
-static bool TestSPAKE2() {
+TEST(SPAKE25519Test, SPAKE2) {
   for (unsigned i = 0; i < 20; i++) {
     SPAKE2Run spake2;
-    if (!spake2.Run()) {
-      fprintf(stderr, "TestSPAKE2: SPAKE2 failed.\n");
-      return false;
-    }
-
-    if (!spake2.key_matches()) {
-      fprintf(stderr, "Key didn't match for equal passwords.\n");
-      return false;
-    }
+    ASSERT_TRUE(spake2.Run());
+    EXPECT_TRUE(spake2.key_matches());
   }
-
-  return true;
 }
 
-static bool TestWrongPassword() {
+TEST(SPAKE25519Test, WrongPassword) {
   SPAKE2Run spake2;
   spake2.bob_password = "wrong password";
-  if (!spake2.Run()) {
-    fprintf(stderr, "TestSPAKE2: SPAKE2 failed.\n");
-    return false;
-  }
-
-  if (spake2.key_matches()) {
-    fprintf(stderr, "Key matched for unequal passwords.\n");
-    return false;
-  }
-
-  return true;
+  ASSERT_TRUE(spake2.Run());
+  EXPECT_FALSE(spake2.key_matches()) << "Key matched for unequal passwords.";
 }
 
-static bool TestWrongNames() {
+TEST(SPAKE25519Test, WrongNames) {
   SPAKE2Run spake2;
   spake2.alice_names.second = "charlie";
   spake2.bob_names.second = "charlie";
-  if (!spake2.Run()) {
-    fprintf(stderr, "TestSPAKE2: SPAKE2 failed.\n");
-    return false;
-  }
-
-  if (spake2.key_matches()) {
-    fprintf(stderr, "Key matched for unequal names.\n");
-    return false;
-  }
-
-  return true;
+  ASSERT_TRUE(spake2.Run());
+  EXPECT_FALSE(spake2.key_matches()) << "Key matched for unequal names.";
 }
 
-static bool TestCorruptMessages() {
+TEST(SPAKE25519Test, CorruptMessages) {
   for (int i = 0; i < 8 * SPAKE2_MAX_MSG_SIZE; i++) {
     SPAKE2Run spake2;
     spake2.alice_corrupt_msg_bit = i;
-    if (spake2.Run() && spake2.key_matches()) {
-      fprintf(stderr, "Passed after corrupting Alice's message, bit %d\n", i);
-      return false;
-    }
+    EXPECT_FALSE(spake2.Run() && spake2.key_matches())
+        << "Passed after corrupting Alice's message, bit " << i;
   }
-
-  return true;
-}
-
-/* TODO(agl): add tests with fixed vectors once SPAKE2 is nailed down. */
-
-int main(int argc, char **argv) {
-  if (!TestSPAKE2() ||
-      !TestWrongPassword() ||
-      !TestWrongNames() ||
-      !TestCorruptMessages()) {
-    return 1;
-  }
-
-  printf("PASS\n");
-  return 0;
 }
diff --git a/util/all_tests.json b/util/all_tests.json
index 2c02e3e..83dc5e3 100644
--- a/util/all_tests.json
+++ b/util/all_tests.json
@@ -34,7 +34,6 @@
 	["crypto/cipher/cipher_test", "crypto/cipher/test/nist_cavp/tdes_ecb.txt"],
 	["crypto/crypto_test"],
 	["crypto/curve25519/ed25519_test", "crypto/curve25519/ed25519_tests.txt"],
-	["crypto/curve25519/spake25519_test"],
 	["crypto/digest_extra/digest_test"],
 	["crypto/ecdh/ecdh_test", "crypto/ecdh/ecdh_tests.txt"],
 	["crypto/ecdsa/ecdsa_sign_test", "crypto/ecdsa/ecdsa_sign_tests.txt"],