3cd4d4
commit 9a1e7257a4292d3aea45c8317df3956f4331d8ce
3cd4d4
Author: Florian Weimer <fweimer@redhat.com>
3cd4d4
Date:   Mon Sep 2 12:40:38 2019 +0200
3cd4d4
3cd4d4
    Add misc/tst-mntent-autofs, testing autofs "ignore" filtering
3cd4d4
3cd4d4
diff -rupN a/misc/Makefile b/misc/Makefile
3cd4d4
--- a/misc/Makefile	2020-01-20 15:48:16.472243494 -0500
3cd4d4
+++ b/misc/Makefile	2020-01-20 16:03:01.291550472 -0500
3cd4d4
@@ -76,7 +76,8 @@ install-lib := libbsd-compat.a libg.a
3cd4d4
 gpl2lgpl := error.c error.h
3cd4d4
 
3cd4d4
 tests := tst-dirname tst-tsearch tst-fdset tst-efgcvt tst-mntent tst-hsearch \
3cd4d4
-	 tst-error1 tst-pselect tst-insremque tst-mntent2 bug-hsearch1 bug18240
3cd4d4
+	 tst-error1 tst-pselect tst-insremque tst-mntent2 bug-hsearch1 bug18240 \
3cd4d4
+	 tst-mntent-autofs
3cd4d4
 ifeq ($(run-built-tests),yes)
3cd4d4
 tests: $(objpfx)tst-error1-mem
3cd4d4
 endif
3cd4d4
diff -rupN a/misc/tst-mntent-autofs.c b/misc/tst-mntent-autofs.c
3cd4d4
--- a/misc/tst-mntent-autofs.c	1969-12-31 19:00:00.000000000 -0500
3cd4d4
+++ b/misc/tst-mntent-autofs.c	2020-01-20 16:01:37.233483270 -0500
3cd4d4
@@ -0,0 +1,141 @@
3cd4d4
+/* Test autofs "ignore" filtering for getment_r.
3cd4d4
+   Copyright (C) 2019 Free Software Foundation, Inc.
3cd4d4
+   This file is part of the GNU C Library.
3cd4d4
+
3cd4d4
+   The GNU C Library is free software; you can redistribute it and/or
3cd4d4
+   modify it under the terms of the GNU Lesser General Public
3cd4d4
+   License as published by the Free Software Foundation; either
3cd4d4
+   version 2.1 of the License, or (at your option) any later version.
3cd4d4
+
3cd4d4
+   The GNU C Library is distributed in the hope that it will be useful,
3cd4d4
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
3cd4d4
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
3cd4d4
+   Lesser General Public License for more details.
3cd4d4
+
3cd4d4
+   You should have received a copy of the GNU Lesser General Public
3cd4d4
+   License along with the GNU C Library; if not, see
3cd4d4
+   <http://www.gnu.org/licenses/>.  */
3cd4d4
+
3cd4d4
+#include <array_length.h>
3cd4d4
+#include <errno.h>
3cd4d4
+#include <mntent.h>
3cd4d4
+#include <stdio.h>
3cd4d4
+#include <stdlib.h>
3cd4d4
+#include <string.h>
3cd4d4
+#include <support/check.h>
3cd4d4
+#include <support/temp_file.h>
3cd4d4
+#include <support/xstdio.h>
3cd4d4
+#include <support/xunistd.h>
3cd4d4
+
3cd4d4
+struct test_case
3cd4d4
+{
3cd4d4
+  const char *line;
3cd4d4
+  struct
3cd4d4
+  {
3cd4d4
+    /* Like struct mntent, but with const pointers.  */
3cd4d4
+    const char *mnt_fsname;
3cd4d4
+    const char *mnt_dir;
3cd4d4
+    const char *mnt_type;
3cd4d4
+    const char *mnt_opts;
3cd4d4
+    int mnt_freq;
3cd4d4
+    int mnt_passno;
3cd4d4
+  } expected;
3cd4d4
+};
3cd4d4
+
3cd4d4
+static struct test_case test_cases[] =
3cd4d4
+  {
3cd4d4
+    { "/etc/auto.direct /mnt/auto/1 autofs defaults 0 0",
3cd4d4
+      { "/etc/auto.direct", "/mnt/auto/1", "autofs", "defaults", 0, 0 } },
3cd4d4
+
3cd4d4
+    /* These entries are filtered out.  */
3cd4d4
+    { "/etc/auto.2 /mnt/auto/2 autofs ignore 0 0", { NULL, } },
3cd4d4
+    { "/etc/auto.3 /mnt/auto/3 autofs ignore,other 1 2", { NULL, } },
3cd4d4
+    { "/etc/auto.4 /mnt/auto/4 autofs other,ignore 3 4", { NULL, } },
3cd4d4
+    { "/etc/auto.5 /mnt/auto/5 autofs opt1,ignore,opt2 5 6", { NULL, } },
3cd4d4
+
3cd4d4
+    /* Dummy entry to make the desynchronization more obvious.  */
3cd4d4
+    { "/dev/sda1 / xfs defaults 0 0",
3cd4d4
+      { "/dev/sda1", "/", "xfs", "defaults", 0, 0 } },
3cd4d4
+
3cd4d4
+    /* These are not filtered because the file system is not autofs.  */
3cd4d4
+    { "/etc/auto.direct /mnt/auto/6 autofs1 ignore 0 0",
3cd4d4
+      { "/etc/auto.direct", "/mnt/auto/6", "autofs1", "ignore", 0, 0 } },
3cd4d4
+    { "/etc/auto.direct /mnt/auto/7 autofs1 ignore,other 0 0",
3cd4d4
+      { "/etc/auto.direct", "/mnt/auto/7", "autofs1", "ignore,other", 0, 0 } },
3cd4d4
+    { "/etc/auto.direct /mnt/auto/8 autofs1 other,ignore 0 0",
3cd4d4
+      { "/etc/auto.direct", "/mnt/auto/8", "autofs1", "other,ignore", 0, 0 } },
3cd4d4
+    { "/etc/auto.direct /mnt/auto/9 autofs1 opt1,ignore,opt2 0 0",
3cd4d4
+      { "/etc/auto.direct", "/mnt/auto/9", "autofs1", "opt1,ignore,opt2", } },
3cd4d4
+
3cd4d4
+    /* These are not filtered because the string "ignore" is not an
3cd4d4
+       option name.  */
3cd4d4
+    { "/etc/auto.direct /mnt/auto/10 autofs noignore 1 2",
3cd4d4
+      { "/etc/auto.direct", "/mnt/auto/10", "autofs", "noignore", 1, 2 } },
3cd4d4
+    { "/etc/auto.direct /mnt/auto/11 autofs noignore,other 0 0",
3cd4d4
+      { "/etc/auto.direct", "/mnt/auto/11", "autofs", "noignore,other", } },
3cd4d4
+    { "/etc/auto.direct /mnt/auto/12 autofs other,noignore 0 0",
3cd4d4
+      { "/etc/auto.direct", "/mnt/auto/12", "autofs", "other,noignore", } },
3cd4d4
+    { "/etc/auto.direct /mnt/auto/13 autofs errors=ignore 0 0",
3cd4d4
+      { "/etc/auto.direct", "/mnt/auto/13", "autofs", "errors=ignore", } },
3cd4d4
+    { "/etc/auto.direct /mnt/auto/14 autofs errors=ignore,other 0 0",
3cd4d4
+      { "/etc/auto.direct", "/mnt/auto/14", "autofs",
3cd4d4
+        "errors=ignore,other", } },
3cd4d4
+    { "/etc/auto.direct /mnt/auto/15 autofs other,errors=ignore 0 0",
3cd4d4
+      { "/etc/auto.direct", "/mnt/auto/15", "autofs",
3cd4d4
+        "other,errors=ignore", } },
3cd4d4
+
3cd4d4
+    /* These are not filtered because the string is escaped.  '\151'
3cd4d4
+       is 'i', but it is not actually decoded by the parser.  */
3cd4d4
+    { "/etc/auto.\\151gnore /mnt/auto/16 autofs \\151gnore 0 0",
3cd4d4
+      { "/etc/auto.\\151gnore", "/mnt/auto/16", "autofs",
3cd4d4
+        "\\151gnore", } },
3cd4d4
+  };
3cd4d4
+
3cd4d4
+static int
3cd4d4
+do_test (void)
3cd4d4
+{
3cd4d4
+  char *path;
3cd4d4
+  xclose (create_temp_file ("tst-mntent-autofs-", &path));
3cd4d4
+
3cd4d4
+  /* Write the test file.  */
3cd4d4
+  FILE *fp = xfopen (path, "w");
3cd4d4
+  for (size_t i = 0; i < array_length (test_cases); ++i)
3cd4d4
+    fprintf (fp, "%s\n", test_cases[i].line);
3cd4d4
+  xfclose (fp);
3cd4d4
+
3cd4d4
+  /* Open the test file again, this time for parsing.  */
3cd4d4
+  fp = setmntent (path, "r");
3cd4d4
+  TEST_VERIFY_EXIT (fp != NULL);
3cd4d4
+  char buffer[512];
3cd4d4
+  struct mntent me;
3cd4d4
+
3cd4d4
+  for (size_t i = 0; i < array_length (test_cases); ++i)
3cd4d4
+    {
3cd4d4
+      if (test_cases[i].expected.mnt_type == NULL)
3cd4d4
+        continue;
3cd4d4
+
3cd4d4
+      memset (buffer, 0xcc, sizeof (buffer));
3cd4d4
+      memset (&me, 0xcc, sizeof (me));
3cd4d4
+      struct mntent *pme = getmntent_r (fp, &me, buffer, sizeof (buffer));
3cd4d4
+      TEST_VERIFY_EXIT (pme != NULL);
3cd4d4
+      TEST_VERIFY (pme == &me);
3cd4d4
+      TEST_COMPARE_STRING (test_cases[i].expected.mnt_fsname, me.mnt_fsname);
3cd4d4
+      TEST_COMPARE_STRING (test_cases[i].expected.mnt_dir, me.mnt_dir);
3cd4d4
+      TEST_COMPARE_STRING (test_cases[i].expected.mnt_type, me.mnt_type);
3cd4d4
+      TEST_COMPARE_STRING (test_cases[i].expected.mnt_opts, me.mnt_opts);
3cd4d4
+      TEST_COMPARE (test_cases[i].expected.mnt_freq, me.mnt_freq);
3cd4d4
+      TEST_COMPARE (test_cases[i].expected.mnt_passno, me.mnt_passno);
3cd4d4
+    }
3cd4d4
+
3cd4d4
+  TEST_VERIFY (getmntent_r (fp, &me, buffer, sizeof (buffer)) == NULL);
3cd4d4
+
3cd4d4
+  TEST_COMPARE (feof (fp), 1);
3cd4d4
+  TEST_COMPARE (ferror (fp), 0);
3cd4d4
+  errno = 0;
3cd4d4
+  TEST_COMPARE (endmntent (fp), 1);
3cd4d4
+  TEST_COMPARE (errno, 0);
3cd4d4
+  free (path);
3cd4d4
+  return 0;
3cd4d4
+}
3cd4d4
+
3cd4d4
+#include <support/test-driver.c>