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