bionic tests: migrate gethostname test to unistd_test.cpp from system/extras

The old tests are implemented in file
    system/extras/tests/bionic/libc/common/test_gethostname.c
Here migrate the test to the tests/unistd_test.cpp file and
add some more checks

Change-Id: Iab1e3da873bb333d1ddefc03108d536933792db2
Signed-off-by: Yongqin Liu <yongqin.liu@linaro.org>
diff --git a/tests/unistd_test.cpp b/tests/unistd_test.cpp
index 38c8efa..a5b92f8 100644
--- a/tests/unistd_test.cpp
+++ b/tests/unistd_test.cpp
@@ -25,6 +25,7 @@
 #include <stdint.h>
 #include <sys/syscall.h>
 #include <sys/types.h>
+#include <sys/utsname.h>
 #include <sys/wait.h>
 #include <unistd.h>
 
@@ -478,3 +479,24 @@
   // all we can do for sethostname(2).
   ASSERT_EQ(-1, sethostname("", -1));
 }
+
+TEST(unistd, gethostname) {
+  char hostname[HOST_NAME_MAX + 1];
+
+  memset(hostname, 0, sizeof(hostname));
+
+  ASSERT_EQ(0, gethostname(hostname, HOST_NAME_MAX));
+  utsname buf;
+  ASSERT_EQ(0, uname(&buf));
+  ASSERT_EQ(0, strncmp(hostname, buf.nodename, SYS_NMLN));
+  ASSERT_GT(strlen(hostname), 0U);
+
+  errno = 0;
+  ASSERT_EQ(-1, gethostname(hostname, strlen(hostname)));
+  ASSERT_EQ(ENAMETOOLONG, errno);
+
+  errno = 0;
+  ASSERT_EQ(0, gethostname(hostname, -2));
+  ASSERT_EQ(0, errno);
+  GTEST_LOG_(INFO) << "hostname=" << hostname << "\n";
+}