Merge "Make the ftw(3) tests a bit more thorough."
diff --git a/libc/include/sgidefs.h b/libc/arch-mips/include/sgidefs.h
similarity index 100%
rename from libc/include/sgidefs.h
rename to libc/arch-mips/include/sgidefs.h
diff --git a/libc/bionic/flockfile.cpp b/libc/bionic/flockfile.cpp
index 3381e8e..b73907cb 100644
--- a/libc/bionic/flockfile.cpp
+++ b/libc/bionic/flockfile.cpp
@@ -36,12 +36,20 @@
 // struct __sfileext (see fileext.h).
 
 void flockfile(FILE* fp) {
+  if (!__sdidinit) {
+    __sinit();
+  }
+
   if (fp != NULL) {
     pthread_mutex_lock(&_FLOCK(fp));
   }
 }
 
 int ftrylockfile(FILE* fp) {
+  if (!__sdidinit) {
+    __sinit();
+  }
+
   // The specification for ftrylockfile() says it returns 0 on success,
   // or non-zero on error. So return an errno code directly on error.
   if (fp == NULL) {
@@ -52,6 +60,10 @@
 }
 
 void funlockfile(FILE* fp) {
+  if (!__sdidinit) {
+    __sinit();
+  }
+
   if (fp != NULL) {
     pthread_mutex_unlock(&_FLOCK(fp));
   }
diff --git a/libc/bionic/stubs.cpp b/libc/bionic/stubs.cpp
index b1e38be..88e5ac5 100644
--- a/libc/bionic/stubs.cpp
+++ b/libc/bionic/stubs.cpp
@@ -40,10 +40,10 @@
 #include "private/android_filesystem_config.h"
 #include "private/ErrnoRestorer.h"
 #include "private/libc_logging.h"
+#include "private/ThreadLocalBuffer.h"
 
-// Thread-specific state for the non-reentrant functions.
-static pthread_once_t stubs_once = PTHREAD_ONCE_INIT;
-static pthread_key_t stubs_key;
+GLOBAL_INIT_THREAD_LOCAL_BUFFER(stubs);
+
 struct stubs_state_t {
   passwd passwd_;
   group group_;
@@ -113,39 +113,14 @@
   return do_getpw_r(0, NULL, uid, pwd, buf, byte_count, result);
 }
 
-static stubs_state_t* stubs_state_alloc() {
-  stubs_state_t*  s = static_cast<stubs_state_t*>(calloc(1, sizeof(*s)));
-  if (s != NULL) {
-    s->group_.gr_mem = s->group_members_;
-  }
-  return s;
-}
-
-static void stubs_state_free(void* ptr) {
-  stubs_state_t* state = static_cast<stubs_state_t*>(ptr);
-  free(state);
-}
-
-static void __stubs_key_init() {
-  pthread_key_create(&stubs_key, stubs_state_free);
-}
-
 static stubs_state_t* __stubs_state() {
-  pthread_once(&stubs_once, __stubs_key_init);
-  stubs_state_t* s = static_cast<stubs_state_t*>(pthread_getspecific(stubs_key));
-  if (s == NULL) {
-    s = stubs_state_alloc();
-    if (s == NULL) {
-      errno = ENOMEM;  // Just in case.
-    } else {
-      if (pthread_setspecific(stubs_key, s) != 0) {
-        stubs_state_free(s);
-        errno = ENOMEM;
-        s = NULL;
-      }
-    }
+  LOCAL_INIT_THREAD_LOCAL_BUFFER(stubs_state_t*, stubs, sizeof(stubs_state_t));
+
+  if (stubs_tls_buffer != NULL) {
+    memset(stubs_tls_buffer, 0, sizeof(stubs_state_t));
+    stubs_tls_buffer->group_.gr_mem = stubs_tls_buffer->group_members_;
   }
-  return s;
+  return stubs_tls_buffer;
 }
 
 static passwd* android_iinfo_to_passwd(stubs_state_t* state,
@@ -167,7 +142,6 @@
   gr->gr_name   = (char*) iinfo->name;
   gr->gr_gid    = iinfo->aid;
   gr->gr_mem[0] = gr->gr_name;
-  gr->gr_mem[1] = NULL;
   return gr;
 }
 
@@ -208,18 +182,27 @@
 }
 
 // Translate a user/group name to the corresponding user/group id.
+// all_a1234 -> 0 * AID_USER + AID_SHARED_GID_START + 1234 (group name only)
 // u0_a1234 -> 0 * AID_USER + AID_APP + 1234
 // u2_i1000 -> 2 * AID_USER + AID_ISOLATED_START + 1000
 // u1_system -> 1 * AID_USER + android_ids['system']
 // returns 0 and sets errno to ENOENT in case of error
-static unsigned app_id_from_name(const char* name) {
-  if (name[0] != 'u' || !isdigit(name[1])) {
+static unsigned app_id_from_name(const char* name, bool is_group) {
+  char* end;
+  unsigned long userid;
+  bool is_shared_gid = false;
+
+  if (is_group && name[0] == 'a' && name[1] == 'l' && name[2] == 'l') {
+    end = const_cast<char*>(name+3);
+    userid = 0;
+    is_shared_gid = true;
+  } else if (name[0] == 'u' && isdigit(name[1])) {
+    userid = strtoul(name+1, &end, 10);
+  } else {
     errno = ENOENT;
     return 0;
   }
 
-  char* end;
-  unsigned long userid = strtoul(name+1, &end, 10);
   if (end[0] != '_' || end[1] == 0) {
     errno = ENOENT;
     return 0;
@@ -227,8 +210,17 @@
 
   unsigned long appid = 0;
   if (end[1] == 'a' && isdigit(end[2])) {
-    // end will point to \0 if the strtoul below succeeds.
-    appid = strtoul(end+2, &end, 10) + AID_APP;
+    if (is_shared_gid) {
+      // end will point to \0 if the strtoul below succeeds.
+      appid = strtoul(end+2, &end, 10) + AID_SHARED_GID_START;
+      if (appid > AID_SHARED_GID_END) {
+        errno = ENOENT;
+        return 0;
+      }
+    } else {
+      // end will point to \0 if the strtoul below succeeds.
+      appid = strtoul(end+2, &end, 10) + AID_APP;
+    }
   } else if (end[1] == 'i' && isdigit(end[2])) {
     // end will point to \0 if the strtoul below succeeds.
     appid = strtoul(end+2, &end, 10) + AID_ISOLATED_START;
@@ -263,12 +255,11 @@
   return (unsigned)(appid + userid*AID_USER);
 }
 
-static void print_app_name_from_appid_userid(const uid_t appid,
-    const uid_t userid, char* buffer, const int bufferlen) {
+static void print_app_name_from_uid(const uid_t uid, char* buffer, const int bufferlen) {
+  const uid_t appid = uid % AID_USER;
+  const uid_t userid = uid / AID_USER;
   if (appid >= AID_ISOLATED_START) {
     snprintf(buffer, bufferlen, "u%u_i%u", userid, appid - AID_ISOLATED_START);
-  } else if (userid == 0 && appid >= AID_SHARED_GID_START) {
-    snprintf(buffer, bufferlen, "all_a%u", appid - AID_SHARED_GID_START);
   } else if (appid < AID_APP) {
     for (size_t n = 0; n < android_id_count; n++) {
       if (android_ids[n].aid == appid) {
@@ -281,10 +272,23 @@
   }
 }
 
-static void print_app_name_from_uid(const uid_t uid, char* buffer, const int bufferlen) {
-  const uid_t appid = uid % AID_USER;
-  const uid_t userid = uid / AID_USER;
-  return print_app_name_from_appid_userid(appid, userid, buffer, bufferlen);
+static void print_app_name_from_gid(const gid_t gid, char* buffer, const int bufferlen) {
+  const uid_t appid = gid % AID_USER;
+  const uid_t userid = gid / AID_USER;
+  if (appid >= AID_ISOLATED_START) {
+    snprintf(buffer, bufferlen, "u%u_i%u", userid, appid - AID_ISOLATED_START);
+  } else if (userid == 0 && appid >= AID_SHARED_GID_START && appid <= AID_SHARED_GID_END) {
+    snprintf(buffer, bufferlen, "all_a%u", appid - AID_SHARED_GID_START);
+  } else if (appid < AID_APP) {
+    for (size_t n = 0; n < android_id_count; n++) {
+      if (android_ids[n].aid == appid) {
+        snprintf(buffer, bufferlen, "u%u_%s", userid, android_ids[n].name);
+        return;
+      }
+    }
+  } else {
+    snprintf(buffer, bufferlen, "u%u_a%u", userid, appid - AID_APP);
+  }
 }
 
 // Translate a uid into the corresponding name.
@@ -301,12 +305,9 @@
     return NULL;
   }
 
+  print_app_name_from_uid(uid, state->app_name_buffer_, sizeof(state->app_name_buffer_));
+
   const uid_t appid = uid % AID_USER;
-  const uid_t userid = uid / AID_USER;
-
-  print_app_name_from_appid_userid(appid, userid, state->app_name_buffer_,
-                                   sizeof(state->app_name_buffer_));
-
   if (appid < AID_APP) {
       snprintf(state->dir_buffer_, sizeof(state->dir_buffer_), "/");
   } else {
@@ -332,14 +333,13 @@
     return NULL;
   }
 
-  print_app_name_from_uid(gid, state->group_name_buffer_,
-                          sizeof(state->group_name_buffer_));
+  print_app_name_from_gid(gid, state->group_name_buffer_, sizeof(state->group_name_buffer_));
 
   group* gr = &state->group_;
   gr->gr_name   = state->group_name_buffer_;
   gr->gr_gid    = gid;
   gr->gr_mem[0] = gr->gr_name;
-  gr->gr_mem[1] = NULL;
+
   return gr;
 }
 
@@ -367,7 +367,7 @@
   if (pw != NULL) {
     return pw;
   }
-  return app_id_to_passwd(app_id_from_name(login), state);
+  return app_id_to_passwd(app_id_from_name(login, false), state);
 }
 
 // All users are in just one group, the one passed in.
@@ -395,7 +395,6 @@
   if (gr != NULL) {
     return gr;
   }
-
   return app_id_to_group(gid, state);
 }
 
@@ -408,8 +407,7 @@
   if (android_name_to_group(&state->group_, name) != 0) {
     return &state->group_;
   }
-
-  return app_id_to_group(app_id_from_name(name), state);
+  return app_id_to_group(app_id_from_name(name, true), state);
 }
 
 // We don't have an /etc/networks, so all inputs return NULL.
diff --git a/libc/stdio/fileext.h b/libc/stdio/fileext.h
index dc89fff..25b7bda 100644
--- a/libc/stdio/fileext.h
+++ b/libc/stdio/fileext.h
@@ -34,6 +34,8 @@
 
 #include <pthread.h>
 
+__BEGIN_DECLS
+
 /*
  * file extension
  */
@@ -61,4 +63,6 @@
 	_FILEEXT_INIT(f); \
 } while (0)
 
+__END_DECLS
+
 #endif /* _FILEEXT_H_ */
diff --git a/libc/stdio/glue.h b/libc/stdio/glue.h
index 4ead20a..a9e5d10 100644
--- a/libc/stdio/glue.h
+++ b/libc/stdio/glue.h
@@ -32,6 +32,10 @@
  * SUCH DAMAGE.
  */
 
+#include <sys/cdefs.h>
+
+__BEGIN_DECLS
+
 /*
  * The first few FILEs are statically allocated; others are dynamically
  * allocated and linked in via this glue structure.
@@ -44,3 +48,5 @@
 
 /* This was referenced by a couple of different pieces of middleware and the Crystax NDK. */
 __LIBC64_HIDDEN__ extern struct glue __sglue;
+
+__END_DECLS
diff --git a/libc/stdio/local.h b/libc/stdio/local.h
index 13188ee..46b11f1 100644
--- a/libc/stdio/local.h
+++ b/libc/stdio/local.h
@@ -41,6 +41,8 @@
 #include "wcio.h"
 #include "fileext.h"
 
+__BEGIN_DECLS
+
 /*
  * Android <= KitKat had getc/putc macros in <stdio.h> that referred
  * to __srget/__swbuf, so those symbols need to be public for LP32
@@ -137,3 +139,5 @@
 wint_t __fputwc_unlock(wchar_t wc, FILE *fp);
 
 #pragma GCC visibility pop
+
+__END_DECLS
diff --git a/libc/stdio/wcio.h b/libc/stdio/wcio.h
index 584a3f2..2c1fa3c 100644
--- a/libc/stdio/wcio.h
+++ b/libc/stdio/wcio.h
@@ -32,6 +32,10 @@
 #ifndef _WCIO_H_
 #define _WCIO_H_
 
+#include <sys/cdefs.h>
+
+__BEGIN_DECLS
+
 /* minimal requirement of SUSv2 */
 #define WCIO_UNGETWC_BUFSIZE 1
 
@@ -78,4 +82,6 @@
 #define WCIO_INIT(fp) \
 	memset(&(_EXT(fp)->_wcio), 0, sizeof(struct wchar_io_data))
 
+__END_DECLS
+
 #endif /*_WCIO_H_*/
diff --git a/tests/Android.build.mk b/tests/Android.build.mk
index 0754a7b..5b2b417 100644
--- a/tests/Android.build.mk
+++ b/tests/Android.build.mk
@@ -42,6 +42,8 @@
 
 LOCAL_FORCE_STATIC_EXECUTABLE := $($(module)_force_static_executable)
 
+LOCAL_ALLOW_UNDEFINED_SYMBOLS := $($(module)_allow_undefined_symbols)
+
 ifneq ($($(module)_multilib),)
     LOCAL_MULTILIB := $($(module)_multilib)
 endif
diff --git a/tests/dlfcn_test.cpp b/tests/dlfcn_test.cpp
index 88f0b19..ea20869 100644
--- a/tests/dlfcn_test.cpp
+++ b/tests/dlfcn_test.cpp
@@ -278,6 +278,44 @@
   ASSERT_EQ(0, dlclose(handle));
 }
 
+TEST(dlfcn, dlopen_check_order_reloc_grandchild) {
+  // This is how this one works:
+  // we lookup and call grandchild_get_answer which is defined in '_2.so'
+  // and in turn calls external get_answer_impl() defined in '_c_1.so and _c_2.so'
+  // the correct _impl() is implemented by '_c_1.so';
+  //
+  // Here is the picture of subtree:
+  //
+  // libtest_check_order_reloc_siblings.so
+  // |
+  // +-> ..._2.so <- grandchild_get_answer()
+  //     |
+  //     +-> ..._c.so <- empty
+  //     |   |
+  //     |   +-> _c_1.so <- exports correct answer_impl()
+  //     |   |
+  //     |   +-> _c_2.so <- exports incorrect answer_impl()
+  //     |
+  //     +-> ..._d.so <- empty
+
+  void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
+  ASSERT_TRUE(handle == nullptr);
+#ifdef __BIONIC__
+  // TODO: glibc returns nullptr on dlerror() here. Is it bug?
+  ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
+#endif
+
+  handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
+  ASSERT_TRUE(handle != nullptr) << dlerror();
+
+  typedef int (*fn_t) (void);
+  fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_grandchild_get_answer"));
+  ASSERT_TRUE(fn != nullptr) << dlerror();
+  ASSERT_EQ(42, fn());
+
+  ASSERT_EQ(0, dlclose(handle));
+}
+
 TEST(dlfcn, dlopen_check_order_reloc_nephew) {
   // This is how this one works:
   // we lookup and call nephew_get_answer which is defined in '_2.so'
diff --git a/tests/libs/Android.build.dlopen_check_order_reloc_siblings.mk b/tests/libs/Android.build.dlopen_check_order_reloc_siblings.mk
index 0f1a2b4..bd35a51 100644
--- a/tests/libs/Android.build.dlopen_check_order_reloc_siblings.mk
+++ b/tests/libs/Android.build.dlopen_check_order_reloc_siblings.mk
@@ -37,12 +37,13 @@
 # ..._2.so - empty
 # -----------------------------------------------------------------------------
 libtest_check_order_reloc_siblings_2_src_files := \
-    empty.cpp
+    dlopen_check_order_reloc_grandchild_answer.cpp
 
 libtest_check_order_reloc_siblings_2_shared_libraries := \
     libtest_check_order_reloc_siblings_c \
     libtest_check_order_reloc_siblings_d
 
+libtest_check_order_reloc_siblings_2_allow_undefined_symbols := true
 module := libtest_check_order_reloc_siblings_2
 include $(LOCAL_PATH)/Android.build.testlib.mk
 
@@ -86,6 +87,10 @@
     dlopen_check_order_reloc_answer_impl.cpp
 
 libtest_check_order_reloc_siblings_c_cflags := -D__ANSWER=2
+libtest_check_order_reloc_siblings_c_shared_libraries := \
+    libtest_check_order_reloc_siblings_c_1 \
+    libtest_check_order_reloc_siblings_c_2
+
 module := libtest_check_order_reloc_siblings_c
 include $(LOCAL_PATH)/Android.build.testlib.mk
 
@@ -119,6 +124,26 @@
 include $(LOCAL_PATH)/Android.build.testlib.mk
 
 # -----------------------------------------------------------------------------
+# ..._c_1.so
+# -----------------------------------------------------------------------------
+libtest_check_order_reloc_siblings_c_1_src_files := \
+    dlopen_check_order_reloc_grandchild_answer_impl.cpp
+
+libtest_check_order_reloc_siblings_c_1_cflags := -D__ANSWER=42
+module := libtest_check_order_reloc_siblings_c_1
+include $(LOCAL_PATH)/Android.build.testlib.mk
+
+# -----------------------------------------------------------------------------
+# ..._c_2.so
+# -----------------------------------------------------------------------------
+libtest_check_order_reloc_siblings_c_2_src_files := \
+    dlopen_check_order_reloc_grandchild_answer_impl.cpp
+
+libtest_check_order_reloc_siblings_c_2_cflags := -D__ANSWER=0
+module := libtest_check_order_reloc_siblings_c_2
+include $(LOCAL_PATH)/Android.build.testlib.mk
+
+# -----------------------------------------------------------------------------
 # libtest_check_order_reloc_siblings.so
 # -----------------------------------------------------------------------------
 libtest_check_order_reloc_siblings_src_files := \
diff --git a/tests/libs/dlopen_check_order_reloc_grandchild_answer.cpp b/tests/libs/dlopen_check_order_reloc_grandchild_answer.cpp
new file mode 100644
index 0000000..afb5f2c
--- /dev/null
+++ b/tests/libs/dlopen_check_order_reloc_grandchild_answer.cpp
@@ -0,0 +1,22 @@
+/*
+ * Copyright (C) 2014 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.
+ */
+
+extern "C" int check_order_reloc_grandchild_get_answer_impl();
+
+extern "C" int check_order_reloc_grandchild_get_answer() {
+  return check_order_reloc_grandchild_get_answer_impl();
+}
+
diff --git a/tests/libs/dlopen_check_order_reloc_grandchild_answer_impl.cpp b/tests/libs/dlopen_check_order_reloc_grandchild_answer_impl.cpp
new file mode 100644
index 0000000..32d2b24
--- /dev/null
+++ b/tests/libs/dlopen_check_order_reloc_grandchild_answer_impl.cpp
@@ -0,0 +1,19 @@
+/*
+ * Copyright (C) 2014 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.
+ */
+
+extern "C" int check_order_reloc_grandchild_get_answer_impl() {
+  return __ANSWER;
+}
diff --git a/tests/stubs_test.cpp b/tests/stubs_test.cpp
index 9b0c231..89d67cb 100644
--- a/tests/stubs_test.cpp
+++ b/tests/stubs_test.cpp
@@ -16,98 +16,246 @@
 
 #include <gtest/gtest.h>
 
-#include <sys/types.h>
-#include <sys/cdefs.h>
+// Below are the header files we want to test.
+#include <grp.h>
 #include <pwd.h>
+
 #include <errno.h>
 #include <limits.h>
+#include <sys/cdefs.h>
+#include <sys/types.h>
 #include <unistd.h>
 
-#if defined(__BIONIC__)
-#define CHECK_GETPWNAM_FOR(username, uid, uid_type) \
-    SCOPED_TRACE(username); \
-    ASSERT_NO_FATAL_FAILURE(check_getpwnam(username, uid, uid_type));
-
-typedef enum {
+enum uid_type_t {
   TYPE_SYSTEM,
   TYPE_APP
-} uid_type_t;
+};
 
-static void check_getpwnam(const char* username, uid_t uid, uid_type_t uid_type) {
-  errno = 0;
-  passwd* pwd = getpwuid(uid);
+#if defined(__BIONIC__)
+
+static void check_passwd(const passwd* pwd, const char* username, uid_t uid, uid_type_t uid_type) {
   ASSERT_TRUE(pwd != NULL);
-  ASSERT_EQ(0, errno);
-  EXPECT_STREQ(username, pwd->pw_name);
-  EXPECT_EQ(uid, pwd->pw_uid);
-  EXPECT_EQ(uid, pwd->pw_gid);
+  ASSERT_STREQ(username, pwd->pw_name);
+  ASSERT_EQ(uid, pwd->pw_uid);
+  ASSERT_EQ(uid, pwd->pw_gid);
   ASSERT_EQ(NULL, pwd->pw_passwd);
 #ifdef __LP64__
   ASSERT_EQ(NULL, pwd->pw_gecos);
 #endif
 
   if (uid_type == TYPE_SYSTEM) {
-    EXPECT_STREQ("/", pwd->pw_dir);
-  } else if (uid_type == TYPE_APP) {
-    EXPECT_STREQ("/data", pwd->pw_dir);
+    ASSERT_STREQ("/", pwd->pw_dir);
+  } else {
+    ASSERT_STREQ("/data", pwd->pw_dir);
   }
-
-  EXPECT_STREQ("/system/bin/sh", pwd->pw_shell);
+  ASSERT_STREQ("/system/bin/sh", pwd->pw_shell);
 }
-#else
-#define CHECK_GETPWNAM_FOR(username, uid, uid_type) \
-  GTEST_LOG_(INFO) << "This test does nothing.\n";
+
+static void check_getpwuid(const char* username, uid_t uid, uid_type_t uid_type) {
+  errno = 0;
+  passwd* pwd = getpwuid(uid);
+  ASSERT_EQ(0, errno);
+  SCOPED_TRACE("getpwuid");
+  check_passwd(pwd, username, uid, uid_type);
+}
+
+static void check_getpwnam(const char* username, uid_t uid, uid_type_t uid_type) {
+  errno = 0;
+  passwd* pwd = getpwnam(username);
+  ASSERT_EQ(0, errno);
+  SCOPED_TRACE("getpwnam");
+  check_passwd(pwd, username, uid, uid_type);
+}
+
+static void check_getpwuid_r(const char* username, uid_t uid, uid_type_t uid_type) {
+  passwd pwd_storage;
+  char buf[512];
+  int result;
+
+  errno = 0;
+  passwd* pwd = NULL;
+  result = getpwuid_r(uid, &pwd_storage, buf, sizeof(buf), &pwd);
+  ASSERT_EQ(0, result);
+  ASSERT_EQ(0, errno);
+  SCOPED_TRACE("getpwuid_r");
+  check_passwd(pwd, username, uid, uid_type);
+}
+
+static void check_getpwnam_r(const char* username, uid_t uid, uid_type_t uid_type) {
+  passwd pwd_storage;
+  char buf[512];
+  int result;
+
+  errno = 0;
+  passwd* pwd = NULL;
+  result = getpwnam_r(username, &pwd_storage, buf, sizeof(buf), &pwd);
+  ASSERT_EQ(0, result);
+  ASSERT_EQ(0, errno);
+  SCOPED_TRACE("getpwnam_r");
+  check_passwd(pwd, username, uid, uid_type);
+}
+
+static void check_get_passwd(const char* username, uid_t uid, uid_type_t uid_type) {
+  check_getpwuid(username, uid, uid_type);
+  check_getpwnam(username, uid, uid_type);
+  check_getpwuid_r(username, uid, uid_type);
+  check_getpwnam_r(username, uid, uid_type);
+}
+
+#else // !defined(__BIONIC__)
+
+static void check_get_passwd(const char* /* username */, uid_t /* uid */, uid_type_t /* uid_type */) {
+  GTEST_LOG_(INFO) << "This test is about uid/username translation for Android, which does nothing on libc other than bionic.\n";
+}
+
 #endif
 
 TEST(getpwnam, system_id_root) {
-  CHECK_GETPWNAM_FOR("root", 0, TYPE_SYSTEM);
+  check_get_passwd("root", 0, TYPE_SYSTEM);
 }
 
 TEST(getpwnam, system_id_system) {
-  CHECK_GETPWNAM_FOR("system", 1000, TYPE_SYSTEM);
+  check_get_passwd("system", 1000, TYPE_SYSTEM);
 }
 
 TEST(getpwnam, app_id_radio) {
-  CHECK_GETPWNAM_FOR("radio", 1001, TYPE_SYSTEM);
+  check_get_passwd("radio", 1001, TYPE_SYSTEM);
 }
 
 TEST(getpwnam, app_id_nobody) {
-  CHECK_GETPWNAM_FOR("nobody", 9999, TYPE_SYSTEM);
-}
-
-TEST(getpwnam, app_id_all_a0) {
-  CHECK_GETPWNAM_FOR("all_a0", 50000, TYPE_APP);
-}
-
-TEST(getpwnam, app_id_u1_a40000) {
-  CHECK_GETPWNAM_FOR("u1_a40000", 150000, TYPE_APP);
+  check_get_passwd("nobody", 9999, TYPE_SYSTEM);
 }
 
 TEST(getpwnam, app_id_u0_a0) {
-  CHECK_GETPWNAM_FOR("u0_a0", 10000, TYPE_APP);
+  check_get_passwd("u0_a0", 10000, TYPE_APP);
 }
 
 TEST(getpwnam, app_id_u0_a1234) {
-  CHECK_GETPWNAM_FOR("u0_a1234", 11234, TYPE_APP);
+  check_get_passwd("u0_a1234", 11234, TYPE_APP);
 }
 
-TEST(getpwnam, app_id_u0_a9999) {
-  CHECK_GETPWNAM_FOR("u0_a9999", 19999, TYPE_APP);
+// Test the difference between uid and shared gid.
+TEST(getpwnam, app_id_u0_a49999) {
+  check_get_passwd("u0_a49999", 59999, TYPE_APP);
 }
 
-// nonsensical, but expected
+TEST(getpwnam, app_id_u0_i1) {
+  check_get_passwd("u0_i1", 99001, TYPE_APP);
+}
+
 TEST(getpwnam, app_id_u1_root) {
-  CHECK_GETPWNAM_FOR("u1_root", 100000, TYPE_SYSTEM);
+  check_get_passwd("u1_root", 100000, TYPE_SYSTEM);
 }
 
 TEST(getpwnam, app_id_u1_radio) {
-  CHECK_GETPWNAM_FOR("u1_radio", 101001, TYPE_SYSTEM);
+  check_get_passwd("u1_radio", 101001, TYPE_SYSTEM);
 }
 
 TEST(getpwnam, app_id_u1_a0) {
-  CHECK_GETPWNAM_FOR("u1_a0", 110000, TYPE_APP);
+  check_get_passwd("u1_a0", 110000, TYPE_APP);
+}
+
+TEST(getpwnam, app_id_u1_a40000) {
+  check_get_passwd("u1_a40000", 150000, TYPE_APP);
 }
 
 TEST(getpwnam, app_id_u1_i0) {
-  CHECK_GETPWNAM_FOR("u1_i0", 199000, TYPE_APP);
+  check_get_passwd("u1_i0", 199000, TYPE_APP);
+}
+
+#if defined(__BIONIC__)
+
+static void check_group(const group* grp, const char* group_name, gid_t gid) {
+  ASSERT_TRUE(grp != NULL);
+  ASSERT_STREQ(group_name, grp->gr_name);
+  ASSERT_EQ(gid, grp->gr_gid);
+  ASSERT_TRUE(grp->gr_mem != NULL);
+  ASSERT_STREQ(group_name, grp->gr_mem[0]);
+  ASSERT_TRUE(grp->gr_mem[1] == NULL);
+}
+
+static void check_getgrgid(const char* group_name, gid_t gid) {
+  errno = 0;
+  group* grp = getgrgid(gid);
+  ASSERT_EQ(0, errno);
+  SCOPED_TRACE("getgrgid");
+  check_group(grp, group_name, gid);
+}
+
+static void check_getgrnam(const char* group_name, gid_t gid) {
+  errno = 0;
+  group* grp = getgrnam(group_name);
+  ASSERT_EQ(0, errno);
+  SCOPED_TRACE("getgrnam");
+  check_group(grp, group_name, gid);
+}
+
+static void check_get_group(const char* group_name, gid_t gid) {
+  check_getgrgid(group_name, gid);
+  check_getgrnam(group_name, gid);
+}
+
+#else // !defined(__BIONIC__)
+
+static void check_get_group(const char* /* group_name */, gid_t /* gid */) {
+  GTEST_LOG_(INFO) << "This test is about gid/group_name translation for Android, which does nothing on libc other than bionic.\n";
+}
+
+#endif
+
+TEST(getgrnam, system_id_root) {
+  check_get_group("root", 0);
+}
+
+TEST(getgrnam, system_id_system) {
+  check_get_group("system", 1000);
+}
+
+TEST(getgrnam, app_id_radio) {
+  check_get_group("radio", 1001);
+}
+
+TEST(getgrnam, app_id_nobody) {
+  check_get_group("nobody", 9999);
+}
+
+TEST(getgrnam, app_id_u0_a0) {
+  check_get_group("u0_a0", 10000);
+}
+
+TEST(getgrnam, app_id_u0_a1234) {
+  check_get_group("u0_a1234", 11234);
+}
+
+TEST(getgrnam, app_id_u0_a9999) {
+  check_get_group("u0_a9999", 19999);
+}
+
+// Test the difference between uid and shared gid.
+TEST(getgrnam, app_id_all_a9999) {
+  check_get_group("all_a9999", 59999);
+}
+
+TEST(getgrnam, app_id_u0_i1) {
+  check_get_group("u0_i1", 99001);
+}
+
+TEST(getgrnam, app_id_u1_root) {
+  check_get_group("u1_root", 100000);
+}
+
+TEST(getgrnam, app_id_u1_radio) {
+  check_get_group("u1_radio", 101001);
+}
+
+TEST(getgrnam, app_id_u1_a0) {
+  check_get_group("u1_a0", 110000);
+}
+
+TEST(getgrnam, app_id_u1_a40000) {
+  check_get_group("u1_a40000", 150000);
+}
+
+TEST(getgrnam, app_id_u1_i0) {
+  check_get_group("u1_i0", 199000);
 }