Prevent using static-allocated pthread keys before creation.

Bug: 19993460

Change-Id: I244dea7f5df3c8384f88aa48d635348fafc9cbaf
diff --git a/tests/pthread_test.cpp b/tests/pthread_test.cpp
index f96ccf9..6dbd964 100644
--- a/tests/pthread_test.cpp
+++ b/tests/pthread_test.cpp
@@ -181,6 +181,19 @@
   ASSERT_EQ(0, pthread_key_delete(key));
 }
 
+TEST(pthread, static_pthread_key_used_before_creation) {
+#if defined(__BIONIC__)
+  // See http://b/19625804. The bug is about a static/global pthread key being used before creation.
+  // So here tests if the static/global default value 0 can be detected as invalid key.
+  static pthread_key_t key;
+  ASSERT_EQ(nullptr, pthread_getspecific(key));
+  ASSERT_EQ(EINVAL, pthread_setspecific(key, nullptr));
+  ASSERT_EQ(EINVAL, pthread_key_delete(key));
+#else
+  GTEST_LOG_(INFO) << "This test tests bionic pthread key implementation detail.\n";
+#endif
+}
+
 static void* IdFn(void* arg) {
   return arg;
 }