olga / rpms / glibc

Forked from rpms/glibc 5 years ago
Clone

Blame SOURCES/glibc-rh1298349.patch

00db10
commit 0897c551c0a098020f145885de06a5c10e5cc96b
00db10
Author: Carlos O'Donell <carlos@systemhalted.org>
00db10
Date:   Wed Jan 21 10:08:18 2015 -0500
00db10
00db10
    tst-getpw: Rewrite.
00db10
    
00db10
    The test is rewritten to look for the testable conditions and
00db10
    exit once they are all detected. This prevents the test from
00db10
    iterating over 2000 UIDs and looking up each one. It speeds up
00db10
    the test and prevents it from failing if the system under test
00db10
    has an NSS-based passwd that is slower than the test timeout.
00db10
    
00db10
    See:
00db10
    https://sourceware.org/ml/libc-alpha/2015-01/msg00394.html
00db10
00db10
diff --git a/pwd/tst-getpw.c b/pwd/tst-getpw.c
00db10
index 059c9e0..e3e101b 100644
00db10
--- a/pwd/tst-getpw.c
00db10
+++ b/pwd/tst-getpw.c
00db10
@@ -1,4 +1,4 @@
00db10
-/* Copyright (C) 1999 Free Software Foundation, Inc.
00db10
+/* Copyright (C) 1999-2016 Free Software Foundation, Inc.
00db10
    This file is part of the GNU C Library.
00db10
 
00db10
    The GNU C Library is free software; you can redistribute it and/or
00db10
@@ -15,26 +15,100 @@
00db10
    License along with the GNU C Library; if not, see
00db10
    <http://www.gnu.org/licenses/>.  */
00db10
 
00db10
+#include <stdio.h>
00db10
 #include <pwd.h>
00db10
+#include <errno.h>
00db10
+#include <stdbool.h>
00db10
+
00db10
+/* We want to test getpw by calling it with a uid that does
00db10
+   exist and one that doesn't exist. We track if we've met those
00db10
+   conditions and exit. We also track if we've failed due to lack
00db10
+   of memory. That constitutes all of the standard failure cases.  */
00db10
+bool seen_hit;
00db10
+bool seen_miss;
00db10
+bool seen_oom;
00db10
+
00db10
+/* How many errors we've had while running the test.  */
00db10
+int errors;
00db10
 
00db10
 static void
00db10
 check (uid_t uid)
00db10
 {
00db10
+  int ret;
00db10
   char buf[1024];
00db10
 
00db10
-  (void) getpw (uid, buf);
00db10
+  ret = getpw (uid, buf);
00db10
+
00db10
+  /* Successfully read a password line.  */
00db10
+  if (ret == 0 && !seen_hit)
00db10
+    {
00db10
+      printf ("PASS: Read a password line given a uid.\n");
00db10
+      seen_hit = true;
00db10
+    }
00db10
+
00db10
+  /* Failed to read a password line. Why?  */
00db10
+  if (ret == -1)
00db10
+    {
00db10
+      /* No entry?  Technically the errno could be any number
00db10
+	 of values including ESRCH, EBADP or EPERM depending
00db10
+	 on the quality of the nss module that implements the
00db10
+	 underlying lookup. It should be 0 for getpw.*/
00db10
+      if (errno == 0 && !seen_miss)
00db10
+	{
00db10
+	  printf ("PASS: Found an invalid uid.\n");
00db10
+	  seen_miss = true;
00db10
+	  return;
00db10
+	}
00db10
+
00db10
+      /* Out of memory?  */
00db10
+      if (errno == ENOMEM && !seen_oom)
00db10
+	{
00db10
+	  printf ("FAIL: Failed with ENOMEM.\n");
00db10
+	  seen_oom = true;
00db10
+	  errors++;
00db10
+	}
00db10
+
00db10
+      /* We don't expect any other values for errno.  */
00db10
+      if (errno != ENOMEM && errno != 0)
00db10
+	errors++;
00db10
+    }
00db10
 }
00db10
 
00db10
-int
00db10
-main (void)
00db10
+static int
00db10
+do_test (void)
00db10
 {
00db10
+  int ret;
00db10
   uid_t uid;
00db10
 
00db10
-  /* Just call it a different number of times the range should be
00db10
-     large enough to find some existing and some non existing uids.  */
00db10
+  /* Should return -1 and set errnot to EINVAL.  */
00db10
+  ret = getpw (0, NULL);
00db10
+  if (ret == -1 && errno == EINVAL)
00db10
+    {
00db10
+      printf ("PASS: NULL buffer returns -1 and sets errno to EINVAL.\n");
00db10
+    }
00db10
+  else
00db10
+    {
00db10
+      printf ("FAIL: NULL buffer did not return -1 or set errno to EINVAL.\n");
00db10
+      errors++;
00db10
+    }
00db10
+
00db10
+  /* Look for one matching uid, one non-found uid and then stop.
00db10
+     Set an upper limit at the 16-bit UID mark; no need to go farther.  */
00db10
+  for (uid = 0; uid < ((uid_t) 65535); ++uid)
00db10
+    {
00db10
+      check (uid);
00db10
+      if (seen_miss && seen_hit)
00db10
+	break;
00db10
+    }
00db10
 
00db10
-  for (uid = 0; uid < 2000; ++uid)
00db10
-    check (uid);
00db10
+  if (!seen_hit)
00db10
+    printf ("FAIL: Did not read even one password line given a uid.\n");
00db10
 
00db10
-  return 0;
00db10
+  if (!seen_miss)
00db10
+    printf ("FAIL: Did not find even one invalid uid.\n");
00db10
+
00db10
+  return errors;
00db10
 }
00db10
+
00db10
+#define TEST_FUNCTION do_test ()
00db10
+#include "../test-skeleton.c"
00db10
    License along with the GNU C Library; if not, see
00db10
    <http://www.gnu.org/licenses/>.  */
00db10
 
00db10
+#include <stdio.h>
00db10
 #include <pwd.h>
00db10
+#include <errno.h>
00db10
+#include <stdbool.h>
00db10
+
00db10
+/* We want to test getpw by calling it with a uid that does
00db10
+   exist and one that doesn't exist. We track if we've met those
00db10
+   conditions and exit. We also track if we've failed due to lack
00db10
+   of memory. That constitutes all of the standard failure cases.  */
00db10
+bool seen_hit;
00db10
+bool seen_miss;
00db10
+bool seen_oom;
00db10
+
00db10
+/* How many errors we've had while running the test.  */
00db10
+int errors;
00db10
 
00db10
 static void
00db10
 check (uid_t uid)
00db10
 {
00db10
+  int ret;
00db10
   char buf[1024];
00db10
 
00db10
-  (void) getpw (uid, buf);
00db10
+  ret = getpw (uid, buf);
00db10
+
00db10
+  /* Successfully read a password line.  */
00db10
+  if (ret == 0 && !seen_hit)
00db10
+    {
00db10
+      printf ("PASS: Read a password line given a uid.\n");
00db10
+      seen_hit = true;
00db10
+    }
00db10
+
00db10
+  /* Failed to read a password line. Why?  */
00db10
+  if (ret == -1)
00db10
+    {
00db10
+      /* No entry?  Technically the errno could be any number
00db10
+	 of values including ESRCH, EBADP or EPERM depending
00db10
+	 on the quality of the nss module that implements the
00db10
+	 underlying lookup. It should be 0 for getpw.*/
00db10
+      if (errno == 0 && !seen_miss)
00db10
+	{
00db10
+	  printf ("PASS: Found an invalid uid.\n");
00db10
+	  seen_miss = true;
00db10
+	  return;
00db10
+	}
00db10
+
00db10
+      /* Out of memory?  */
00db10
+      if (errno == ENOMEM && !seen_oom)
00db10
+	{
00db10
+	  printf ("FAIL: Failed with ENOMEM.\n");
00db10
+	  seen_oom = true;
00db10
+	  errors++;
00db10
+	}
00db10
+
00db10
+      /* We don't expect any other values for errno.  */
00db10
+      if (errno != ENOMEM && errno != 0)
00db10
+	errors++;
00db10
+    }
00db10
 }
00db10
 
00db10
 static int
00db10
 do_test (void)
00db10
 {
00db10
+  int ret;
00db10
   uid_t uid;
00db10
 
00db10
-  /* Just call it a different number of times the range should be
00db10
-     large enough to find some existing and some non existing uids.  */
00db10
+  /* Should return -1 and set errnot to EINVAL.  */
00db10
+  ret = getpw (0, NULL);
00db10
+  if (ret == -1 && errno == EINVAL)
00db10
+    {
00db10
+      printf ("PASS: NULL buffer returns -1 and sets errno to EINVAL.\n");
00db10
+    }
00db10
+  else
00db10
+    {
00db10
+      printf ("FAIL: NULL buffer did not return -1 or set errno to EINVAL.\n");
00db10
+      errors++;
00db10
+    }
00db10
+
00db10
+  /* Look for one matching uid, one non-found uid and then stop.
00db10
+     Set an upper limit at the 16-bit UID mark; no need to go farther.  */
00db10
+  for (uid = 0; uid < ((uid_t) 65535); ++uid)
00db10
+    {
00db10
+      check (uid);
00db10
+      if (seen_miss && seen_hit)
00db10
+	break;
00db10
+    }
00db10
+
00db10
+  if (!seen_hit)
00db10
+    printf ("FAIL: Did not read even one password line given a uid.\n");
00db10
 
00db10
-  for (uid = 0; uid < 2000; ++uid)
00db10
-    check (uid);
00db10
+  if (!seen_miss)
00db10
+    printf ("FAIL: Did not find even one invalid uid.\n");
00db10
 
00db10
-  return 0;
00db10
+  return errors;
00db10
 }
00db10
 
00db10
 #define TEST_FUNCTION do_test ()