Merge "Modify unit tests to run on-device as well as on the dev machine."
diff --git a/Android.mk b/Android.mk
index 216fe31..1c86ccd 100644
--- a/Android.mk
+++ b/Android.mk
@@ -93,10 +93,32 @@
 	system/security/keystore \
 	$(LOCAL_PATH)/include
 LOCAL_CFLAGS = -Wall -Werror
-# Ignore benigh warnings for now.
 LOCAL_CLANG_CFLAGS += -Wno-error=unused-const-variable
 LOCAL_SHARED_LIBRARIES := libkeymaster_messages libkeymaster1 liblog libcrypto
 LOCAL_MODULE_TAGS := optional
 LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
 LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
 include $(BUILD_SHARED_LIBRARY)
+
+# Unit tests for libkeymaster
+include $(CLEAR_VARS)
+LOCAL_MODULE := libkeymaster_test
+LOCAL_SRC_FILES := \
+	abstract_factory_registry_test.cpp \
+	authorization_set_test.cpp \
+	google_keymaster_messages_test.cpp \
+	google_keymaster_test.cpp \
+	google_keymaster_test_utils.cpp \
+	hkdf_test.cpp \
+	hmac_test.cpp \
+	key_blob_test.cpp \
+	keymaster_enforcement.cpp \
+	keymaster_enforcement_test.cpp
+LOCAL_C_INCLUDES := \
+	$(LOCAL_PATH)/include
+LOCAL_CFLAGS = -Wall -Werror
+LOCAL_MODULE_TAGS := tests
+LOCAL_SHARED_LIBRARIES := libsoftkeymasterdevice libkeymaster_messages libkeymaster1 libcrypto
+LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
+include $(BUILD_NATIVE_TEST)
+
diff --git a/Makefile b/Makefile
index 119fb48..85d9c6f 100644
--- a/Makefile
+++ b/Makefile
@@ -44,12 +44,13 @@
 	google_keymaster_test.cpp \
 	google_keymaster_test_utils.cpp \
 	google_keymaster_utils.cpp \
+	gtest_main.cpp \
 	hkdf.cpp \
 	hkdf_test.cpp \
 	hmac.cpp \
-	hmac_test.cpp \
 	hmac_key.cpp \
 	hmac_operation.cpp \
+	hmac_test.cpp \
 	key.cpp \
 	key_blob.cpp \
 	key_blob_test.cpp \
@@ -124,13 +125,15 @@
 
 massif: $(BINARIES:=.massif)
 
+GTEST_OBJS = $(GTEST)/src/gtest-all.o gtest_main.o
+
 hmac_test: hmac_test.o \
 	authorization_set.o \
 	google_keymaster_test_utils.o \
 	hmac.o \
 	logger.o \
 	serializable.o \
-	$(GTEST)/src/gtest-all.o
+	$(GTEST_OBJS)
 
 hkdf_test: hkdf_test.o \
 	authorization_set.o \
@@ -139,14 +142,14 @@
 	hmac.o \
 	logger.o \
 	serializable.o \
-	$(GTEST)/src/gtest-all.o
+	$(GTEST_OBJS)
 
 authorization_set_test: authorization_set_test.o \
 	authorization_set.o \
 	google_keymaster_test_utils.o \
 	logger.o \
 	serializable.o \
-	$(GTEST)/src/gtest-all.o
+	$(GTEST_OBJS)
 
 key_blob_test: key_blob_test.o \
 	authorization_set.o \
@@ -157,7 +160,7 @@
 	openssl_err.o \
 	serializable.o \
 	unencrypted_key_blob.o \
-	$(GTEST)/src/gtest-all.o
+	$(GTEST_OBJS)
 
 google_keymaster_messages_test: google_keymaster_messages_test.o \
 	authorization_set.o \
@@ -166,7 +169,7 @@
 	google_keymaster_utils.o \
 	logger.o \
 	serializable.o \
-	$(GTEST)/src/gtest-all.o
+	$(GTEST_OBJS)
 
 google_keymaster_test: google_keymaster_test.o \
 	aead_mode_operation.o \
@@ -196,11 +199,11 @@
 	soft_keymaster_device.o \
 	symmetric_key.o \
 	unencrypted_key_blob.o \
-	$(GTEST)/src/gtest-all.o
+	$(GTEST_OBJS)
 
 abstract_factory_registry_test: abstract_factory_registry_test.o \
 	logger.o \
-	$(GTEST)/src/gtest-all.o
+	$(GTEST_OBJS)
 
 keymaster_enforcement_test: keymaster_enforcement_test.o \
 	keymaster_enforcement.o \
@@ -210,7 +213,7 @@
 	google_keymaster_test_utils.o \
 	logger.o \
 	serializable.o \
-	$(GTEST)/src/gtest-all.o
+	$(GTEST_OBJS)
 
 $(GTEST)/src/gtest-all.o: CXXFLAGS:=$(subst -Wmissing-declarations,,$(CXXFLAGS))
 ocb.o: CFLAGS=$(CLANG_TEST_DEFINE)
diff --git a/abstract_factory_registry_test.cpp b/abstract_factory_registry_test.cpp
index b0a5626..e08f1ef 100644
--- a/abstract_factory_registry_test.cpp
+++ b/abstract_factory_registry_test.cpp
@@ -21,12 +21,6 @@
 
 #include <gtest/gtest.h>
 
-int main(int argc, char** argv) {
-    ::testing::InitGoogleTest(&argc, argv);
-    int result = RUN_ALL_TESTS();
-    return result;
-}
-
 namespace keymaster {
 
 class TestAbstractFactory {
diff --git a/authorization_set_test.cpp b/authorization_set_test.cpp
index 8971c6c..590bbe4 100644
--- a/authorization_set_test.cpp
+++ b/authorization_set_test.cpp
@@ -21,12 +21,6 @@
 
 #include "google_keymaster_test_utils.h"
 
-int main(int argc, char** argv) {
-    ::testing::InitGoogleTest(&argc, argv);
-    int result = RUN_ALL_TESTS();
-    return result;
-}
-
 namespace keymaster {
 
 namespace test {
@@ -62,11 +56,11 @@
     };
 
     AuthorizationSet set1(params, 0);
-    EXPECT_EQ(0, set1.size());
+    EXPECT_EQ(0U, set1.size());
     EXPECT_EQ(AuthorizationSet::OK, set1.is_valid());
 
     AuthorizationSet set2(reinterpret_cast<keymaster_key_param_t*>(NULL), array_length(params));
-    EXPECT_EQ(0, set2.size());
+    EXPECT_EQ(0U, set2.size());
     EXPECT_EQ(AuthorizationSet::OK, set2.is_valid());
 }
 
diff --git a/google_keymaster_messages_test.cpp b/google_keymaster_messages_test.cpp
index 0f93de9..107822f 100644
--- a/google_keymaster_messages_test.cpp
+++ b/google_keymaster_messages_test.cpp
@@ -24,12 +24,6 @@
 #include "google_keymaster_test_utils.h"
 #include "google_softkeymaster.h"
 
-int main(int argc, char** argv) {
-    ::testing::InitGoogleTest(&argc, argv);
-    int result = RUN_ALL_TESTS();
-    return result;
-}
-
 namespace keymaster {
 namespace test {
 
diff --git a/google_keymaster_test.cpp b/google_keymaster_test.cpp
index a86e1cf..efa6f13 100644
--- a/google_keymaster_test.cpp
+++ b/google_keymaster_test.cpp
@@ -33,17 +33,6 @@
 using std::string;
 using std::vector;
 
-int main(int argc, char** argv) {
-    ERR_load_crypto_strings();
-    ::testing::InitGoogleTest(&argc, argv);
-    int result = RUN_ALL_TESTS();
-    // Clean up stuff OpenSSL leaves around, so Valgrind doesn't complain.
-    CRYPTO_cleanup_all_ex_data();
-    ERR_remove_thread_state(NULL);
-    ERR_free_strings();
-    return result;
-}
-
 template <typename T> std::ostream& operator<<(std::ostream& os, const std::vector<T>& vec) {
     os << "{ ";
     bool first = true;
@@ -1365,12 +1354,12 @@
     string iv1;
     string ciphertext1 = EncryptMessage(message, &iv1);
     EXPECT_EQ(message.size(), ciphertext1.size());
-    EXPECT_EQ(16, iv1.size());
+    EXPECT_EQ(16U, iv1.size());
 
     string iv2;
     string ciphertext2 = EncryptMessage(message, &iv2);
     EXPECT_EQ(message.size(), ciphertext2.size());
-    EXPECT_EQ(16, iv2.size());
+    EXPECT_EQ(16U, iv2.size());
 
     // IVs should be random, so ciphertexts should differ.
     EXPECT_NE(iv1, iv2);
diff --git a/google_keymaster_test_utils.cpp b/google_keymaster_test_utils.cpp
index 8e1f7d3..95d0a5d 100644
--- a/google_keymaster_test_utils.cpp
+++ b/google_keymaster_test_utils.cpp
@@ -212,7 +212,7 @@
     keymaster_error_t error =
         device()->begin(device(), purpose, &blob_, client_params_, array_length(client_params_),
                         &out_params, &out_params_count, &op_handle_);
-    EXPECT_EQ(0, out_params_count);
+    EXPECT_EQ(0U, out_params_count);
     EXPECT_TRUE(out_params == NULL);
     return error;
 }
@@ -235,7 +235,7 @@
         if (output_set) {
             output_set->Reinitialize(out_params, out_params_count);
         } else {
-            EXPECT_EQ(0, out_params_count);
+            EXPECT_EQ(0U, out_params_count);
             EXPECT_TRUE(out_params == NULL);
         }
         keymaster_free_param_values(out_params, out_params_count);
@@ -347,7 +347,7 @@
 void Keymaster1Test::SignMessage(const string& message, string* signature, bool use_client_params) {
     SCOPED_TRACE("SignMessage");
     *signature = ProcessMessage(KM_PURPOSE_SIGN, message, use_client_params);
-    EXPECT_GT(signature->size(), 0);
+    EXPECT_GT(signature->size(), 0U);
 }
 
 void Keymaster1Test::VerifyMessage(const string& message, const string& signature,
@@ -440,7 +440,7 @@
                                      KM_KEY_FORMAT_RAW, key));
     string signature;
     SignMessage(message, &signature);
-    EXPECT_EQ(expected_mac, signature) << "Test vector didn't match for digest " << digest;
+    EXPECT_EQ(expected_mac, signature) << "Test vector didn't match for digest " << (int)digest;
 }
 
 void Keymaster1Test::CheckAesCtrTestVector(const string& key, const string& nonce,
diff --git a/gtest_main.cpp b/gtest_main.cpp
new file mode 100644
index 0000000..c991d4e
--- /dev/null
+++ b/gtest_main.cpp
@@ -0,0 +1,30 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <gtest/gtest.h>
+
+#include <openssl/engine.h>
+
+int main(int argc, char** argv) {
+    ERR_load_crypto_strings();
+    ::testing::InitGoogleTest(&argc, argv);
+    int result = RUN_ALL_TESTS();
+    // Clean up stuff OpenSSL leaves around, so Valgrind doesn't complain.
+    CRYPTO_cleanup_all_ex_data();
+    ERR_remove_thread_state(NULL);
+    ERR_free_strings();
+    return result;
+}
diff --git a/hkdf_test.cpp b/hkdf_test.cpp
index dd126aa..81f32ce 100644
--- a/hkdf_test.cpp
+++ b/hkdf_test.cpp
@@ -23,12 +23,6 @@
 
 using std::string;
 
-int main(int argc, char** argv) {
-    ::testing::InitGoogleTest(&argc, argv);
-    int result = RUN_ALL_TESTS();
-    return result;
-}
-
 namespace keymaster {
 
 namespace test {
diff --git a/hmac_test.cpp b/hmac_test.cpp
index 06f6377..2f6c568 100644
--- a/hmac_test.cpp
+++ b/hmac_test.cpp
@@ -23,12 +23,6 @@
 
 using std::string;
 
-int main(int argc, char** argv) {
-    ::testing::InitGoogleTest(&argc, argv);
-    int result = RUN_ALL_TESTS();
-    return result;
-}
-
 namespace keymaster {
 
 namespace test {
diff --git a/key_blob_test.cpp b/key_blob_test.cpp
index aed88b7..1d62422 100644
--- a/key_blob_test.cpp
+++ b/key_blob_test.cpp
@@ -27,15 +27,6 @@
 
 #include "unencrypted_key_blob.h"
 
-int main(int argc, char** argv) {
-    ::testing::InitGoogleTest(&argc, argv);
-    int result = RUN_ALL_TESTS();
-    // Clean up stuff OpenSSL leaves around, so Valgrind doesn't complain.
-    CRYPTO_cleanup_all_ex_data();
-    ERR_free_strings();
-    return result;
-}
-
 namespace keymaster {
 
 namespace test {
diff --git a/keymaster_enforcement_test.cpp b/keymaster_enforcement_test.cpp
index d6c9ccd..8cded39 100644
--- a/keymaster_enforcement_test.cpp
+++ b/keymaster_enforcement_test.cpp
@@ -24,12 +24,6 @@
 
 #include "keymaster_enforcement.h"
 
-int main(int argc, char** argv) {
-    ::testing::InitGoogleTest(&argc, argv);
-    int result = RUN_ALL_TESTS();
-    return result;
-}
-
 namespace keymaster {
 namespace test {